2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 /** @file test/image_test.cc
22 * @brief Tests of the Image class.
24 * @see test/make_black_test.cc, test/pixel_formats_test.cc
27 #include <boost/test/unit_test.hpp>
29 #include "lib/image.h"
35 using boost::shared_ptr;
37 BOOST_AUTO_TEST_CASE (aligned_image_test)
39 Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), true);
40 BOOST_CHECK_EQUAL (s->planes(), 1);
41 /* 160 is 150 aligned to the nearest 32 bytes */
42 BOOST_CHECK_EQUAL (s->stride()[0], 160);
43 BOOST_CHECK_EQUAL (s->line_size()[0], 150);
44 BOOST_CHECK (s->data()[0]);
45 BOOST_CHECK (!s->data()[1]);
46 BOOST_CHECK (!s->data()[2]);
47 BOOST_CHECK (!s->data()[3]);
49 /* copy constructor */
50 Image* t = new Image (*s);
51 BOOST_CHECK_EQUAL (t->planes(), 1);
52 BOOST_CHECK_EQUAL (t->stride()[0], 160);
53 BOOST_CHECK_EQUAL (t->line_size()[0], 150);
54 BOOST_CHECK (t->data()[0]);
55 BOOST_CHECK (!t->data()[1]);
56 BOOST_CHECK (!t->data()[2]);
57 BOOST_CHECK (!t->data()[3]);
58 BOOST_CHECK (t->data() != s->data());
59 BOOST_CHECK (t->data()[0] != s->data()[0]);
60 BOOST_CHECK (t->line_size() != s->line_size());
61 BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
62 BOOST_CHECK (t->stride() != s->stride());
63 BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
65 /* assignment operator */
66 Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), false);
68 BOOST_CHECK_EQUAL (u->planes(), 1);
69 BOOST_CHECK_EQUAL (u->stride()[0], 160);
70 BOOST_CHECK_EQUAL (u->line_size()[0], 150);
71 BOOST_CHECK (u->data()[0]);
72 BOOST_CHECK (!u->data()[1]);
73 BOOST_CHECK (!u->data()[2]);
74 BOOST_CHECK (!u->data()[3]);
75 BOOST_CHECK (u->data() != s->data());
76 BOOST_CHECK (u->data()[0] != s->data()[0]);
77 BOOST_CHECK (u->line_size() != s->line_size());
78 BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
79 BOOST_CHECK (u->stride() != s->stride());
80 BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
87 BOOST_AUTO_TEST_CASE (compact_image_test)
89 Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), false);
90 BOOST_CHECK_EQUAL (s->planes(), 1);
91 BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
92 BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
93 BOOST_CHECK (s->data()[0]);
94 BOOST_CHECK (!s->data()[1]);
95 BOOST_CHECK (!s->data()[2]);
96 BOOST_CHECK (!s->data()[3]);
98 /* copy constructor */
99 Image* t = new Image (*s);
100 BOOST_CHECK_EQUAL (t->planes(), 1);
101 BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
102 BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
103 BOOST_CHECK (t->data()[0]);
104 BOOST_CHECK (!t->data()[1]);
105 BOOST_CHECK (!t->data()[2]);
106 BOOST_CHECK (!t->data()[3]);
107 BOOST_CHECK (t->data() != s->data());
108 BOOST_CHECK (t->data()[0] != s->data()[0]);
109 BOOST_CHECK (t->line_size() != s->line_size());
110 BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
111 BOOST_CHECK (t->stride() != s->stride());
112 BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
114 /* assignment operator */
115 Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), true);
117 BOOST_CHECK_EQUAL (u->planes(), 1);
118 BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
119 BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
120 BOOST_CHECK (u->data()[0]);
121 BOOST_CHECK (!u->data()[1]);
122 BOOST_CHECK (!u->data()[2]);
123 BOOST_CHECK (!u->data()[3]);
124 BOOST_CHECK (u->data() != s->data());
125 BOOST_CHECK (u->data()[0] != s->data()[0]);
126 BOOST_CHECK (u->line_size() != s->line_size());
127 BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
128 BOOST_CHECK (u->stride() != s->stride());
129 BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
136 /** Test Image::alpha_blend */
137 BOOST_AUTO_TEST_CASE (alpha_blend_test)
139 int const stride = 48 * 4;
141 shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
143 uint8_t* a = A->data()[0];
145 for (int y = 0; y < 48; ++y) {
146 uint8_t* p = a + y * stride;
147 for (int x = 0; x < 16; ++x) {
149 p[(x + 16) * 4 + 1] = 255;
150 p[(x + 32) * 4 + 2] = 255;
154 shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), true));
155 B->make_transparent ();
156 uint8_t* b = B->data()[0];
158 for (int y = 32; y < 48; ++y) {
159 uint8_t* p = b + y * stride;
160 for (int x = 0; x < 48; ++x) {
168 A->alpha_blend (B, Position<int> (0, 0));
170 for (int y = 0; y < 32; ++y) {
171 uint8_t* p = a + y * stride;
172 for (int x = 0; x < 16; ++x) {
173 BOOST_CHECK_EQUAL (p[x * 4], 255);
174 BOOST_CHECK_EQUAL (p[(x + 16) * 4 + 1], 255);
175 BOOST_CHECK_EQUAL (p[(x + 32) * 4 + 2], 255);
179 for (int y = 32; y < 48; ++y) {
180 uint8_t* p = a + y * stride;
181 for (int x = 0; x < 48; ++x) {
182 BOOST_CHECK_EQUAL (p[x * 4], 255);
183 BOOST_CHECK_EQUAL (p[x * 4 + 1], 255);
184 BOOST_CHECK_EQUAL (p[x * 4 + 2], 255);
185 BOOST_CHECK_EQUAL (p[x * 4 + 3], 255);
190 /** Test merge (list<PositionImage>) with a single image */
191 BOOST_AUTO_TEST_CASE (merge_test1)
193 int const stride = 48 * 4;
195 shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
196 A->make_transparent ();
197 uint8_t* a = A->data()[0];
199 for (int y = 0; y < 48; ++y) {
200 uint8_t* p = a + y * stride;
201 for (int x = 0; x < 16; ++x) {
209 list<PositionImage> all;
210 all.push_back (PositionImage (A, Position<int> (0, 0)));
211 PositionImage merged = merge (all);
213 BOOST_CHECK (merged.position == Position<int> (0, 0));
214 BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
217 /** Test merge (list<PositionImage>) with two images */
218 BOOST_AUTO_TEST_CASE (merge_test2)
220 shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
221 A->make_transparent ();
222 uint8_t* a = A->data()[0];
223 for (int x = 0; x < 16; ++x) {
230 shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
231 B->make_transparent ();
232 uint8_t* b = B->data()[0];
233 for (int x = 0; x < 16; ++x) {
235 b[(x + 32) * 4 + 2] = 255;
237 b[(x + 32) * 4 + 3] = 255;
240 list<PositionImage> all;
241 all.push_back (PositionImage (A, Position<int> (0, 0)));
242 all.push_back (PositionImage (B, Position<int> (0, 0)));
243 PositionImage merged = merge (all);
245 BOOST_CHECK (merged.position == Position<int> (0, 0));
247 uint8_t* m = merged.image->data()[0];
249 for (int x = 0; x < 16; ++x) {
250 BOOST_CHECK_EQUAL (m[x * 4], 255);
251 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
252 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
253 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
254 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);