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