More <iostream> includes for Arch.
[dcpomatic.git] / test / image_test.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  test/image_test.cc
21  *  @brief Tests of the Image class.
22  *
23  *  @see test/make_black_test.cc, test/pixel_formats_test.cc
24  */
25
26 #include <boost/test/unit_test.hpp>
27 #include <Magick++.h>
28 #include "lib/image.h"
29 #include <iostream>
30
31 using std::string;
32 using std::list;
33 using std::cout;
34 using boost::shared_ptr;
35
36 BOOST_AUTO_TEST_CASE (aligned_image_test)
37 {
38         Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), true);
39         BOOST_CHECK_EQUAL (s->planes(), 1);
40         /* 160 is 150 aligned to the nearest 32 bytes */
41         BOOST_CHECK_EQUAL (s->stride()[0], 160);
42         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
43         BOOST_CHECK (s->data()[0]);
44         BOOST_CHECK (!s->data()[1]);
45         BOOST_CHECK (!s->data()[2]);
46         BOOST_CHECK (!s->data()[3]);
47
48         /* copy constructor */
49         Image* t = new Image (*s);
50         BOOST_CHECK_EQUAL (t->planes(), 1);
51         BOOST_CHECK_EQUAL (t->stride()[0], 160);
52         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
53         BOOST_CHECK (t->data()[0]);
54         BOOST_CHECK (!t->data()[1]);
55         BOOST_CHECK (!t->data()[2]);
56         BOOST_CHECK (!t->data()[3]);
57         BOOST_CHECK (t->data() != s->data());
58         BOOST_CHECK (t->data()[0] != s->data()[0]);
59         BOOST_CHECK (t->line_size() != s->line_size());
60         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
61         BOOST_CHECK (t->stride() != s->stride());
62         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
63
64         /* assignment operator */
65         Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), false);
66         *u = *s;
67         BOOST_CHECK_EQUAL (u->planes(), 1);
68         BOOST_CHECK_EQUAL (u->stride()[0], 160);
69         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
70         BOOST_CHECK (u->data()[0]);
71         BOOST_CHECK (!u->data()[1]);
72         BOOST_CHECK (!u->data()[2]);
73         BOOST_CHECK (!u->data()[3]);
74         BOOST_CHECK (u->data() != s->data());
75         BOOST_CHECK (u->data()[0] != s->data()[0]);
76         BOOST_CHECK (u->line_size() != s->line_size());
77         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
78         BOOST_CHECK (u->stride() != s->stride());
79         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
80
81         delete s;
82         delete t;
83         delete u;
84 }
85
86 BOOST_AUTO_TEST_CASE (compact_image_test)
87 {
88         Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), false);
89         BOOST_CHECK_EQUAL (s->planes(), 1);
90         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
91         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
92         BOOST_CHECK (s->data()[0]);
93         BOOST_CHECK (!s->data()[1]);
94         BOOST_CHECK (!s->data()[2]);
95         BOOST_CHECK (!s->data()[3]);
96
97         /* copy constructor */
98         Image* t = new Image (*s);
99         BOOST_CHECK_EQUAL (t->planes(), 1);
100         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
101         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
102         BOOST_CHECK (t->data()[0]);
103         BOOST_CHECK (!t->data()[1]);
104         BOOST_CHECK (!t->data()[2]);
105         BOOST_CHECK (!t->data()[3]);
106         BOOST_CHECK (t->data() != s->data());
107         BOOST_CHECK (t->data()[0] != s->data()[0]);
108         BOOST_CHECK (t->line_size() != s->line_size());
109         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
110         BOOST_CHECK (t->stride() != s->stride());
111         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
112
113         /* assignment operator */
114         Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), true);
115         *u = *s;
116         BOOST_CHECK_EQUAL (u->planes(), 1);
117         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
118         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
119         BOOST_CHECK (u->data()[0]);
120         BOOST_CHECK (!u->data()[1]);
121         BOOST_CHECK (!u->data()[2]);
122         BOOST_CHECK (!u->data()[3]);
123         BOOST_CHECK (u->data() != s->data());
124         BOOST_CHECK (u->data()[0] != s->data()[0]);
125         BOOST_CHECK (u->line_size() != s->line_size());
126         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
127         BOOST_CHECK (u->stride() != s->stride());
128         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
129
130         delete s;
131         delete t;
132         delete u;
133 }
134
135 /** Test Image::alpha_blend */
136 BOOST_AUTO_TEST_CASE (alpha_blend_test)
137 {
138         int const stride = 48 * 4;
139
140         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
141         A->make_black ();
142         uint8_t* a = A->data()[0];
143
144         for (int y = 0; y < 48; ++y) {
145                 uint8_t* p = a + y * stride;
146                 for (int x = 0; x < 16; ++x) {
147                         p[x * 4] = 255;
148                         p[(x + 16) * 4 + 1] = 255;
149                         p[(x + 32) * 4 + 2] = 255;
150                 }
151         }
152
153         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), true));
154         B->make_transparent ();
155         uint8_t* b = B->data()[0];
156
157         for (int y = 32; y < 48; ++y) {
158                 uint8_t* p = b + y * stride;
159                 for (int x = 0; x < 48; ++x) {
160                         p[x * 4] = 255;
161                         p[x * 4 + 1] = 255;
162                         p[x * 4 + 2] = 255;
163                         p[x * 4 + 3] = 255;
164                 }
165         }
166
167         A->alpha_blend (B, Position<int> (0, 0));
168
169         for (int y = 0; y < 32; ++y) {
170                 uint8_t* p = a + y * stride;
171                 for (int x = 0; x < 16; ++x) {
172                         BOOST_CHECK_EQUAL (p[x * 4], 255);
173                         BOOST_CHECK_EQUAL (p[(x + 16) * 4 + 1], 255);
174                         BOOST_CHECK_EQUAL (p[(x + 32) * 4 + 2], 255);
175                 }
176         }
177
178         for (int y = 32; y < 48; ++y) {
179                 uint8_t* p = a + y * stride;
180                 for (int x = 0; x < 48; ++x) {
181                         BOOST_CHECK_EQUAL (p[x * 4], 255);
182                         BOOST_CHECK_EQUAL (p[x * 4 + 1], 255);
183                         BOOST_CHECK_EQUAL (p[x * 4 + 2], 255);
184                         BOOST_CHECK_EQUAL (p[x * 4 + 3], 255);
185                 }
186         }
187 }
188
189 /** Test merge (list<PositionImage>) with a single image */
190 BOOST_AUTO_TEST_CASE (merge_test1)
191 {
192         int const stride = 48 * 4;
193
194         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
195         A->make_transparent ();
196         uint8_t* a = A->data()[0];
197
198         for (int y = 0; y < 48; ++y) {
199                 uint8_t* p = a + y * stride;
200                 for (int x = 0; x < 16; ++x) {
201                         /* red */
202                         p[x * 4] = 255;
203                         /* opaque */
204                         p[x * 4 + 3] = 255;
205                 }
206         }
207
208         list<PositionImage> all;
209         all.push_back (PositionImage (A, Position<int> (0, 0)));
210         PositionImage merged = merge (all);
211
212         BOOST_CHECK (merged.position == Position<int> (0, 0));
213         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
214 }
215
216 /** Test merge (list<PositionImage>) with two images */
217 BOOST_AUTO_TEST_CASE (merge_test2)
218 {
219         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
220         A->make_transparent ();
221         uint8_t* a = A->data()[0];
222         for (int x = 0; x < 16; ++x) {
223                 /* red */
224                 a[x * 4] = 255;
225                 /* opaque */
226                 a[x * 4 + 3] = 255;
227         }
228
229         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
230         B->make_transparent ();
231         uint8_t* b = B->data()[0];
232         for (int x = 0; x < 16; ++x) {
233                 /* blue */
234                 b[(x + 32) * 4 + 2] = 255;
235                 /* opaque */
236                 b[(x + 32) * 4 + 3] = 255;
237         }
238
239         list<PositionImage> all;
240         all.push_back (PositionImage (A, Position<int> (0, 0)));
241         all.push_back (PositionImage (B, Position<int> (0, 0)));
242         PositionImage merged = merge (all);
243
244         BOOST_CHECK (merged.position == Position<int> (0, 0));
245
246         uint8_t* m = merged.image->data()[0];
247
248         for (int x = 0; x < 16; ++x) {
249                 BOOST_CHECK_EQUAL (m[x * 4], 255);
250                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
251                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
252                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
253                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
254         }
255 }