Actually use YUV->RGB setting when converting.
[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 BOOST_AUTO_TEST_CASE (crop_image_test)
135 {
136         /* This was to check out a bug with valgrind, and is probably not very useful */
137         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (16, 16), true));
138         image->make_black ();
139         Crop crop;
140         crop.top = 3;
141         image->crop (crop, false);
142 }
143
144 /* Test cropping of a YUV 4:2:0 image by 1 pixel, which used to fail because
145    the U/V copying was not rounded up to the next sample.
146 */
147 BOOST_AUTO_TEST_CASE (crop_image_test2)
148 {
149         /* Here's a 1998 x 1080 image which is black */
150         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
151         image->make_black ();
152
153         /* Crop it by 1 pixel */
154         Crop crop;
155         crop.left = 1;
156         image = image->crop (crop, true);
157
158         /* Convert it back to RGB to make comparison to black easier */
159         image = image->scale (image->size(), dcp::YUV_TO_RGB_REC601, PIX_FMT_RGB24, true);
160
161         /* Check that its still black after the crop */
162         uint8_t* p = image->data()[0];
163         for (int y = 0; y < image->size().height; ++y) {
164                 uint8_t* q = p;
165                 for (int x = 0; x < image->size().width; ++x) {
166                         BOOST_CHECK_EQUAL (*q++, 0);
167                         BOOST_CHECK_EQUAL (*q++, 0);
168                         BOOST_CHECK_EQUAL (*q++, 0);
169                 }
170                 p += image->stride()[0];
171         }
172 }
173
174 static
175 boost::shared_ptr<Image>
176 read_file (string file)
177 {
178         Magick::Image magick_image (file.c_str ());
179         dcp::Size size (magick_image.columns(), magick_image.rows());
180
181         boost::shared_ptr<Image> image (new Image (PIX_FMT_RGB24, size, true));
182
183 #ifdef DCPOMATIC_IMAGE_MAGICK   
184         using namespace MagickCore;
185 #endif  
186         
187         uint8_t* p = image->data()[0];
188         for (int y = 0; y < size.height; ++y) {
189                 uint8_t* q = p;
190                 for (int x = 0; x < size.width; ++x) {
191                         Magick::Color c = magick_image.pixelColor (x, y);
192 #ifdef DCPOMATIC_IMAGE_MAGICK                   
193                         *q++ = c.redQuantum() * 255 / QuantumRange;
194                         *q++ = c.greenQuantum() * 255 / QuantumRange;
195                         *q++ = c.blueQuantum() * 255 / QuantumRange;
196 #else                   
197                         *q++ = c.redQuantum() * 255 / MaxRGB;
198                         *q++ = c.greenQuantum() * 255 / MaxRGB;
199                         *q++ = c.blueQuantum() * 255 / MaxRGB;
200 #endif                  
201                 }
202                 p += image->stride()[0];
203         }
204
205         return image;
206 }
207
208 static
209 void
210 write_file (shared_ptr<Image> image, string file)
211 {
212 #ifdef DCPOMATIC_IMAGE_MAGICK   
213         using namespace MagickCore;
214 #endif  
215         
216         Magick::Image magick_image (Magick::Geometry (image->size().width, image->size().height), Magick::Color (0, 0, 0));
217         uint8_t*p = image->data()[0];
218         for (int y = 0; y < image->size().height; ++y) {
219                 uint8_t* q = p;
220                 for (int x = 0; x < image->size().width; ++x) {
221 #ifdef DCPOMATIC_IMAGE_MAGICK
222                         Magick::Color c (q[0] * QuantumRange / 256, q[1] * QuantumRange / 256, q[2] * QuantumRange / 256);
223 #else                   
224                         Magick::Color c (q[0] * MaxRGB / 256, q[1] * MaxRGB / 256, q[2] * MaxRGB / 256);
225 #endif                  
226                         magick_image.pixelColor (x, y, c);
227                         q += 3;
228                 }
229                 p += image->stride()[0];
230         }
231         
232         magick_image.write (file.c_str ());
233 }
234
235 static
236 void
237 crop_scale_window_single (AVPixelFormat in_format, dcp::Size in_size, Crop crop, dcp::Size inter_size, dcp::Size out_size)
238 {
239         /* Set up our test image */
240         shared_ptr<Image> test (new Image (in_format, in_size, true));
241         uint8_t n = 0;
242         for (int c = 0; c < test->components(); ++c) {
243                 uint8_t* p = test->data()[c];
244                 for (int y = 0; y < test->lines(c); ++y) {
245                         for (int x = 0; x < test->stride()[c]; ++x) {
246                                 *p++ = n++;
247                         }
248                 }
249         }
250                                 
251         /* Convert using separate methods */
252         boost::shared_ptr<Image> sep = test->crop (crop, true);
253         sep = sep->scale (inter_size, dcp::YUV_TO_RGB_REC601, PIX_FMT_RGB24, true);
254         boost::shared_ptr<Image> sep_container (new Image (PIX_FMT_RGB24, out_size, true));
255         sep_container->make_black ();
256         sep_container->copy (sep, Position<int> ((out_size.width - inter_size.width) / 2, (out_size.height - inter_size.height) / 2));
257
258         /* Convert using the all-in-one method */
259         shared_ptr<Image> all = test->crop_scale_window (crop, inter_size, out_size, dcp::YUV_TO_RGB_REC601, PIX_FMT_RGB24, true);
260
261         /* Compare */
262         BOOST_CHECK_EQUAL (sep_container->size().width, all->size().width);
263         BOOST_CHECK_EQUAL (sep_container->size().height, all->size().height);
264
265         /* Assuming RGB on these */
266         BOOST_CHECK_EQUAL (sep_container->components(), 1);
267         BOOST_CHECK_EQUAL (all->components(), 1);
268
269         uint8_t* p = sep_container->data()[0];
270         uint8_t* q = all->data()[0];
271         for (int y = 0; y < all->size().height; ++y) {
272                 uint8_t* pp = p;
273                 uint8_t* qq = q;
274                 for (int x = 0; x < all->size().width * 3; ++x) {
275                         BOOST_CHECK_EQUAL (*pp++, *qq++);
276                 }
277                 p += sep_container->stride()[0];
278                 q += all->stride()[0];
279         }
280 }
281
282 /** Test Image::crop_scale_window against separate calls to crop/scale/copy */
283 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
284 {
285         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (), dcp::Size (640, 480), dcp::Size (640, 480));
286         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (2, 4, 6, 8), dcp::Size (640, 480), dcp::Size (640, 480));
287         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (2, 4, 6, 8), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
288         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (1, 4, 6, 8), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
289         crop_scale_window_single (AV_PIX_FMT_YUV420P, dcp::Size (640, 480), Crop (16, 16, 0, 0), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
290         crop_scale_window_single (AV_PIX_FMT_YUV420P, dcp::Size (640, 480), Crop (16, 3, 3, 0), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
291         crop_scale_window_single (AV_PIX_FMT_RGB24, dcp::Size (1000, 800), Crop (0, 0, 0, 0), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
292         crop_scale_window_single (AV_PIX_FMT_RGB24, dcp::Size (1000, 800), Crop (55, 0, 1, 9), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
293 }
294
295 /** Test Image::alpha_blend */
296 BOOST_AUTO_TEST_CASE (alpha_blend_test)
297 {
298         int const stride = 48 * 4;
299         
300         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
301         A->make_black ();
302         uint8_t* a = A->data()[0];
303
304         for (int y = 0; y < 48; ++y) {
305                 uint8_t* p = a + y * stride;
306                 for (int x = 0; x < 16; ++x) {
307                         p[x * 4] = 255;
308                         p[(x + 16) * 4 + 1] = 255;
309                         p[(x + 32) * 4 + 2] = 255;
310                 }
311         }
312
313         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), true));
314         B->make_transparent ();
315         uint8_t* b = B->data()[0];
316
317         for (int y = 32; y < 48; ++y) {
318                 uint8_t* p = b + y * stride;
319                 for (int x = 0; x < 48; ++x) {
320                         p[x * 4] = 255;
321                         p[x * 4 + 1] = 255;
322                         p[x * 4 + 2] = 255;
323                         p[x * 4 + 3] = 255;
324                 }
325         }
326
327         A->alpha_blend (B, Position<int> (0, 0));
328
329         for (int y = 0; y < 32; ++y) {
330                 uint8_t* p = a + y * stride;
331                 for (int x = 0; x < 16; ++x) {
332                         BOOST_CHECK_EQUAL (p[x * 4], 255);
333                         BOOST_CHECK_EQUAL (p[(x + 16) * 4 + 1], 255);
334                         BOOST_CHECK_EQUAL (p[(x + 32) * 4 + 2], 255);
335                 }
336         }
337
338         for (int y = 32; y < 48; ++y) {
339                 uint8_t* p = a + y * stride;
340                 for (int x = 0; x < 48; ++x) {
341                         BOOST_CHECK_EQUAL (p[x * 4], 255);
342                         BOOST_CHECK_EQUAL (p[x * 4 + 1], 255);
343                         BOOST_CHECK_EQUAL (p[x * 4 + 2], 255);
344                         BOOST_CHECK_EQUAL (p[x * 4 + 3], 255);
345                 }
346         }
347 }
348
349 /** Test merge (list<PositionImage>) with a single image */
350 BOOST_AUTO_TEST_CASE (merge_test1)
351 {
352         int const stride = 48 * 4;
353         
354         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
355         A->make_transparent ();
356         uint8_t* a = A->data()[0];
357
358         for (int y = 0; y < 48; ++y) {
359                 uint8_t* p = a + y * stride;
360                 for (int x = 0; x < 16; ++x) {
361                         /* red */
362                         p[x * 4] = 255;
363                         /* opaque */
364                         p[x * 4 + 3] = 255;
365                 }
366         }
367
368         list<PositionImage> all;
369         all.push_back (PositionImage (A, Position<int> (0, 0)));
370         PositionImage merged = merge (all);
371
372         BOOST_CHECK (merged.position == Position<int> (0, 0));
373         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
374 }
375
376 /** Test merge (list<PositionImage>) with two images */
377 BOOST_AUTO_TEST_CASE (merge_test2)
378 {
379         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
380         A->make_transparent ();
381         uint8_t* a = A->data()[0];
382         for (int x = 0; x < 16; ++x) {
383                 /* red */
384                 a[x * 4] = 255;
385                 /* opaque */
386                 a[x * 4 + 3] = 255;
387         }
388
389         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
390         B->make_transparent ();
391         uint8_t* b = B->data()[0];
392         for (int x = 0; x < 16; ++x) {
393                 /* blue */
394                 b[(x + 32) * 4 + 2] = 255;
395                 /* opaque */
396                 b[(x + 32) * 4 + 3] = 255;
397         }
398
399         list<PositionImage> all;
400         all.push_back (PositionImage (A, Position<int> (0, 0)));
401         all.push_back (PositionImage (B, Position<int> (0, 0)));
402         PositionImage merged = merge (all);
403
404         BOOST_CHECK (merged.position == Position<int> (0, 0));
405
406         uint8_t* m = merged.image->data()[0];
407
408         for (int x = 0; x < 16; ++x) {
409                 BOOST_CHECK_EQUAL (m[x * 4], 255);
410                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
411                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
412                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
413                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
414         }
415 }