summaryrefslogtreecommitdiffstats
path: root/gfx/tests/gtest/TestBufferRotation.cpp
blob: 871295e68cd7d64f403588c9fe522dafc536a83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "BufferUnrotate.h"

using mozilla::gfx::BufferUnrotate;

static unsigned char* GenerateBuffer(int bytesPerPixel, int width, int height,
                                     int stride, int xBoundary, int yBoundary) {
  unsigned char* buffer = new unsigned char[stride * height];
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int pos = ((yBoundary + y) % height) * stride +
                ((xBoundary + x) % width) * bytesPerPixel;
      for (int i = 0; i < bytesPerPixel; i++) {
        buffer[pos + i] = (x + y + i * 2) % 256;
      }
    }
  }
  return buffer;
}

static bool CheckBuffer(unsigned char* buffer, int bytesPerPixel, int width,
                        int height, int stride) {
  int xBoundary = 0;
  int yBoundary = 0;
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int pos = ((yBoundary + y) % height) * stride +
                ((xBoundary + x) % width) * bytesPerPixel;
      for (int i = 0; i < bytesPerPixel; i++) {
        if (buffer[pos + i] != (x + y + i * 2) % 256) {
          printf("Buffer differs at %i, %i, is %i\n", x, y,
                 (int)buffer[pos + i]);
          return false;
        }
      }
    }
  }
  return true;
}

TEST(Gfx, BufferUnrotateHorizontal)
{
  const int NUM_OF_TESTS = 8;
  int bytesPerPixelList[2] = {2, 4};
  int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99};
  int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99};
  int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31};
  int yBoundary[NUM_OF_TESTS] = {0, 0, 0, 0};

  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
    int bytesPerPixel = bytesPerPixelList[bytesPerId];
    int stride = 256 * bytesPerPixel;
    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
      unsigned char* buffer =
          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
                         xBoundary[testId], yBoundary[testId]);
      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
                     stride, xBoundary[testId] * bytesPerPixel,
                     yBoundary[testId]);

      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
                              height[testId], stride));
      delete[] buffer;
    }
  }
}

TEST(Gfx, BufferUnrotateVertical)
{
  const int NUM_OF_TESTS = 8;
  int bytesPerPixelList[2] = {2, 4};
  int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99};
  int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99};
  int xBoundary[NUM_OF_TESTS] = {0, 0, 0, 0};
  int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31};

  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
    int bytesPerPixel = bytesPerPixelList[bytesPerId];
    int stride = 256 * bytesPerPixel;
    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
      unsigned char* buffer =
          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
                         xBoundary[testId], yBoundary[testId]);
      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
                     stride, xBoundary[testId] * bytesPerPixel,
                     yBoundary[testId]);

      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
                              height[testId], stride));
      delete[] buffer;
    }
  }
}

TEST(Gfx, BufferUnrotateBoth)
{
  const int NUM_OF_TESTS = 16;
  int bytesPerPixelList[2] = {2, 4};
  int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99,
                             100, 100, 99, 99, 100, 100, 99, 99};
  int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99,
                              100, 99, 100, 99, 100, 99, 100, 99};
  int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31,
                                 30, 30, 30, 30, 31, 31, 31, 31};
  int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 30, 30, 30, 30,
                                 31, 31, 31, 31, 31, 31, 31, 31};

  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
    int bytesPerPixel = bytesPerPixelList[bytesPerId];
    int stride = 256 * bytesPerPixel;
    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
      unsigned char* buffer =
          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
                         xBoundary[testId], yBoundary[testId]);
      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
                     stride, xBoundary[testId] * bytesPerPixel,
                     yBoundary[testId]);

      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
                              height[testId], stride));
      delete[] buffer;
    }
  }
}

TEST(Gfx, BufferUnrotateUneven)
{
  const int NUM_OF_TESTS = 16;
  int bytesPerPixelList[2] = {2, 4};
  int width[NUM_OF_TESTS] = {10,  100, 99, 39, 100, 40, 99, 39,
                             100, 50,  39, 99, 74,  60, 99, 39};
  int height[NUM_OF_TESTS] = {100, 39, 10,  99, 10, 99, 40, 99,
                              73,  39, 100, 39, 67, 99, 84, 99};
  int xBoundary[NUM_OF_TESTS] = {0,  0,  30, 30, 99, 31, 0,  31,
                                 30, 30, 30, 30, 31, 31, 31, 38};
  int yBoundary[NUM_OF_TESTS] = {30, 30, 0,  30, 0,  30, 0,  30,
                                 31, 31, 31, 31, 31, 31, 31, 98};

  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
    int bytesPerPixel = bytesPerPixelList[bytesPerId];
    int stride = 256 * bytesPerPixel;
    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
      unsigned char* buffer =
          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
                         xBoundary[testId], yBoundary[testId]);
      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
                     stride, xBoundary[testId] * bytesPerPixel,
                     yBoundary[testId]);

      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
                              height[testId], stride));
      delete[] buffer;
    }
  }
}