Remove unused Image::crop.
[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
30 using std::string;
31 using std::list;
32 using std::cout;
33 using boost::shared_ptr;
34
35 BOOST_AUTO_TEST_CASE (aligned_image_test)
36 {
37         Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), true);
38         BOOST_CHECK_EQUAL (s->components(), 1);
39         /* 160 is 150 aligned to the nearest 32 bytes */
40         BOOST_CHECK_EQUAL (s->stride()[0], 160);
41         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
42         BOOST_CHECK (s->data()[0]);
43         BOOST_CHECK (!s->data()[1]);
44         BOOST_CHECK (!s->data()[2]);
45         BOOST_CHECK (!s->data()[3]);
46
47         /* copy constructor */
48         Image* t = new Image (*s);
49         BOOST_CHECK_EQUAL (t->components(), 1);
50         BOOST_CHECK_EQUAL (t->stride()[0], 160);
51         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
52         BOOST_CHECK (t->data()[0]);
53         BOOST_CHECK (!t->data()[1]);
54         BOOST_CHECK (!t->data()[2]);
55         BOOST_CHECK (!t->data()[3]);
56         BOOST_CHECK (t->data() != s->data());
57         BOOST_CHECK (t->data()[0] != s->data()[0]);
58         BOOST_CHECK (t->line_size() != s->line_size());
59         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
60         BOOST_CHECK (t->stride() != s->stride());
61         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
62
63         /* assignment operator */
64         Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), false);
65         *u = *s;
66         BOOST_CHECK_EQUAL (u->components(), 1);
67         BOOST_CHECK_EQUAL (u->stride()[0], 160);
68         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
69         BOOST_CHECK (u->data()[0]);
70         BOOST_CHECK (!u->data()[1]);
71         BOOST_CHECK (!u->data()[2]);
72         BOOST_CHECK (!u->data()[3]);
73         BOOST_CHECK (u->data() != s->data());
74         BOOST_CHECK (u->data()[0] != s->data()[0]);
75         BOOST_CHECK (u->line_size() != s->line_size());
76         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
77         BOOST_CHECK (u->stride() != s->stride());
78         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
79
80         delete s;
81         delete t;
82         delete u;
83 }
84
85 BOOST_AUTO_TEST_CASE (compact_image_test)
86 {
87         Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), false);
88         BOOST_CHECK_EQUAL (s->components(), 1);
89         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
90         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
91         BOOST_CHECK (s->data()[0]);
92         BOOST_CHECK (!s->data()[1]);
93         BOOST_CHECK (!s->data()[2]);
94         BOOST_CHECK (!s->data()[3]);
95
96         /* copy constructor */
97         Image* t = new Image (*s);
98         BOOST_CHECK_EQUAL (t->components(), 1);
99         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
100         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
101         BOOST_CHECK (t->data()[0]);
102         BOOST_CHECK (!t->data()[1]);
103         BOOST_CHECK (!t->data()[2]);
104         BOOST_CHECK (!t->data()[3]);
105         BOOST_CHECK (t->data() != s->data());
106         BOOST_CHECK (t->data()[0] != s->data()[0]);
107         BOOST_CHECK (t->line_size() != s->line_size());
108         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
109         BOOST_CHECK (t->stride() != s->stride());
110         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
111
112         /* assignment operator */
113         Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), true);
114         *u = *s;
115         BOOST_CHECK_EQUAL (u->components(), 1);
116         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
117         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
118         BOOST_CHECK (u->data()[0]);
119         BOOST_CHECK (!u->data()[1]);
120         BOOST_CHECK (!u->data()[2]);
121         BOOST_CHECK (!u->data()[3]);
122         BOOST_CHECK (u->data() != s->data());
123         BOOST_CHECK (u->data()[0] != s->data()[0]);
124         BOOST_CHECK (u->line_size() != s->line_size());
125         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
126         BOOST_CHECK (u->stride() != s->stride());
127         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
128
129         delete s;
130         delete t;
131         delete u;
132 }
133
134 /** Test Image::alpha_blend */
135 BOOST_AUTO_TEST_CASE (alpha_blend_test)
136 {
137         int const stride = 48 * 4;
138
139         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
140         A->make_black ();
141         uint8_t* a = A->data()[0];
142
143         for (int y = 0; y < 48; ++y) {
144                 uint8_t* p = a + y * stride;
145                 for (int x = 0; x < 16; ++x) {
146                         p[x * 4] = 255;
147                         p[(x + 16) * 4 + 1] = 255;
148                         p[(x + 32) * 4 + 2] = 255;
149                 }
150         }
151
152         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), true));
153         B->make_transparent ();
154         uint8_t* b = B->data()[0];
155
156         for (int y = 32; y < 48; ++y) {
157                 uint8_t* p = b + y * stride;
158                 for (int x = 0; x < 48; ++x) {
159                         p[x * 4] = 255;
160                         p[x * 4 + 1] = 255;
161                         p[x * 4 + 2] = 255;
162                         p[x * 4 + 3] = 255;
163                 }
164         }
165
166         A->alpha_blend (B, Position<int> (0, 0));
167
168         for (int y = 0; y < 32; ++y) {
169                 uint8_t* p = a + y * stride;
170                 for (int x = 0; x < 16; ++x) {
171                         BOOST_CHECK_EQUAL (p[x * 4], 255);
172                         BOOST_CHECK_EQUAL (p[(x + 16) * 4 + 1], 255);
173                         BOOST_CHECK_EQUAL (p[(x + 32) * 4 + 2], 255);
174                 }
175         }
176
177         for (int y = 32; y < 48; ++y) {
178                 uint8_t* p = a + y * stride;
179                 for (int x = 0; x < 48; ++x) {
180                         BOOST_CHECK_EQUAL (p[x * 4], 255);
181                         BOOST_CHECK_EQUAL (p[x * 4 + 1], 255);
182                         BOOST_CHECK_EQUAL (p[x * 4 + 2], 255);
183                         BOOST_CHECK_EQUAL (p[x * 4 + 3], 255);
184                 }
185         }
186 }
187
188 /** Test merge (list<PositionImage>) with a single image */
189 BOOST_AUTO_TEST_CASE (merge_test1)
190 {
191         int const stride = 48 * 4;
192
193         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
194         A->make_transparent ();
195         uint8_t* a = A->data()[0];
196
197         for (int y = 0; y < 48; ++y) {
198                 uint8_t* p = a + y * stride;
199                 for (int x = 0; x < 16; ++x) {
200                         /* red */
201                         p[x * 4] = 255;
202                         /* opaque */
203                         p[x * 4 + 3] = 255;
204                 }
205         }
206
207         list<PositionImage> all;
208         all.push_back (PositionImage (A, Position<int> (0, 0)));
209         PositionImage merged = merge (all);
210
211         BOOST_CHECK (merged.position == Position<int> (0, 0));
212         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
213 }
214
215 /** Test merge (list<PositionImage>) with two images */
216 BOOST_AUTO_TEST_CASE (merge_test2)
217 {
218         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
219         A->make_transparent ();
220         uint8_t* a = A->data()[0];
221         for (int x = 0; x < 16; ++x) {
222                 /* red */
223                 a[x * 4] = 255;
224                 /* opaque */
225                 a[x * 4 + 3] = 255;
226         }
227
228         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
229         B->make_transparent ();
230         uint8_t* b = B->data()[0];
231         for (int x = 0; x < 16; ++x) {
232                 /* blue */
233                 b[(x + 32) * 4 + 2] = 255;
234                 /* opaque */
235                 b[(x + 32) * 4 + 3] = 255;
236         }
237
238         list<PositionImage> all;
239         all.push_back (PositionImage (A, Position<int> (0, 0)));
240         all.push_back (PositionImage (B, Position<int> (0, 0)));
241         PositionImage merged = merge (all);
242
243         BOOST_CHECK (merged.position == Position<int> (0, 0));
244
245         uint8_t* m = merged.image->data()[0];
246
247         for (int x = 0; x < 16; ++x) {
248                 BOOST_CHECK_EQUAL (m[x * 4], 255);
249                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
250                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
251                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
252                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
253         }
254 }