8aebd6bafe138401de61a2365bdeb656c66127c9
[dcpomatic.git] / test / image_test.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  test/image_test.cc
22  *  @brief Test Image class.
23  *  @ingroup selfcontained
24  *  @see test/make_black_test.cc, test/pixel_formats_test.cc
25  */
26
27 #include "lib/compose.hpp"
28 #include "lib/image.h"
29 #include "lib/image_content.h"
30 #include "lib/image_decoder.h"
31 #include "lib/ffmpeg_image_proxy.h"
32 #include "test.h"
33 #include <boost/test/unit_test.hpp>
34 #include <iostream>
35
36 using std::string;
37 using std::list;
38 using std::cout;
39 using boost::shared_ptr;
40
41 BOOST_AUTO_TEST_CASE (aligned_image_test)
42 {
43         Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), true);
44         BOOST_CHECK_EQUAL (s->planes(), 1);
45         /* 192 is 150 aligned to the nearest 64 bytes */
46         BOOST_CHECK_EQUAL (s->stride()[0], 192);
47         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
48         BOOST_CHECK (s->data()[0]);
49         BOOST_CHECK (!s->data()[1]);
50         BOOST_CHECK (!s->data()[2]);
51         BOOST_CHECK (!s->data()[3]);
52
53         /* copy constructor */
54         Image* t = new Image (*s);
55         BOOST_CHECK_EQUAL (t->planes(), 1);
56         BOOST_CHECK_EQUAL (t->stride()[0], 192);
57         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
58         BOOST_CHECK (t->data()[0]);
59         BOOST_CHECK (!t->data()[1]);
60         BOOST_CHECK (!t->data()[2]);
61         BOOST_CHECK (!t->data()[3]);
62         BOOST_CHECK (t->data() != s->data());
63         BOOST_CHECK (t->data()[0] != s->data()[0]);
64         BOOST_CHECK (t->line_size() != s->line_size());
65         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
66         BOOST_CHECK (t->stride() != s->stride());
67         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
68
69         /* assignment operator */
70         Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), false);
71         *u = *s;
72         BOOST_CHECK_EQUAL (u->planes(), 1);
73         BOOST_CHECK_EQUAL (u->stride()[0], 192);
74         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
75         BOOST_CHECK (u->data()[0]);
76         BOOST_CHECK (!u->data()[1]);
77         BOOST_CHECK (!u->data()[2]);
78         BOOST_CHECK (!u->data()[3]);
79         BOOST_CHECK (u->data() != s->data());
80         BOOST_CHECK (u->data()[0] != s->data()[0]);
81         BOOST_CHECK (u->line_size() != s->line_size());
82         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
83         BOOST_CHECK (u->stride() != s->stride());
84         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
85
86         delete s;
87         delete t;
88         delete u;
89 }
90
91 BOOST_AUTO_TEST_CASE (compact_image_test)
92 {
93         Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), false);
94         BOOST_CHECK_EQUAL (s->planes(), 1);
95         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
96         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
97         BOOST_CHECK (s->data()[0]);
98         BOOST_CHECK (!s->data()[1]);
99         BOOST_CHECK (!s->data()[2]);
100         BOOST_CHECK (!s->data()[3]);
101
102         /* copy constructor */
103         Image* t = new Image (*s);
104         BOOST_CHECK_EQUAL (t->planes(), 1);
105         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
106         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
107         BOOST_CHECK (t->data()[0]);
108         BOOST_CHECK (!t->data()[1]);
109         BOOST_CHECK (!t->data()[2]);
110         BOOST_CHECK (!t->data()[3]);
111         BOOST_CHECK (t->data() != s->data());
112         BOOST_CHECK (t->data()[0] != s->data()[0]);
113         BOOST_CHECK (t->line_size() != s->line_size());
114         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
115         BOOST_CHECK (t->stride() != s->stride());
116         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
117
118         /* assignment operator */
119         Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), true);
120         *u = *s;
121         BOOST_CHECK_EQUAL (u->planes(), 1);
122         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
123         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
124         BOOST_CHECK (u->data()[0]);
125         BOOST_CHECK (!u->data()[1]);
126         BOOST_CHECK (!u->data()[2]);
127         BOOST_CHECK (!u->data()[3]);
128         BOOST_CHECK (u->data() != s->data());
129         BOOST_CHECK (u->data()[0] != s->data()[0]);
130         BOOST_CHECK (u->line_size() != s->line_size());
131         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
132         BOOST_CHECK (u->stride() != s->stride());
133         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
134
135         delete s;
136         delete t;
137         delete u;
138 }
139
140 void
141 alpha_blend_test_one (AVPixelFormat format, string suffix)
142 {
143         shared_ptr<FFmpegImageProxy> proxy (new FFmpegImageProxy (private_data / "prophet_frame.tiff"));
144         shared_ptr<Image> raw = proxy->image().first;
145         shared_ptr<Image> background = raw->convert_pixel_format (dcp::YUV_TO_RGB_REC709, format, true, false);
146
147         shared_ptr<Image> overlay (new Image (AV_PIX_FMT_BGRA, dcp::Size(431, 891), true));
148         overlay->make_transparent ();
149
150         for (int y = 0; y < 128; ++y) {
151                 uint8_t* p = overlay->data()[0] + y * overlay->stride()[0];
152                 for (int x = 0; x < 128; ++x) {
153                         p[x * 4 + 2] = 255;
154                         p[x * 4 + 3] = 255;
155                 }
156         }
157
158         for (int y = 128; y < 256; ++y) {
159                 uint8_t* p = overlay->data()[0] + y * overlay->stride()[0];
160                 for (int x = 0; x < 128; ++x) {
161                         p[x * 4 + 1] = 255;
162                         p[x * 4 + 3] = 255;
163                 }
164         }
165
166         for (int y = 256; y < 384; ++y) {
167                 uint8_t* p = overlay->data()[0] + y * overlay->stride()[0];
168                 for (int x = 0; x < 128; ++x) {
169                         p[x * 4] = 255;
170                         p[x * 4 + 3] = 255;
171                 }
172         }
173
174         background->alpha_blend (overlay, Position<int> (13, 17));
175
176         shared_ptr<Image> save = background->convert_pixel_format (dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
177
178         write_image (save, "build/test/image_test_" + suffix + ".png", "RGB");
179         check_image ("build/test/image_test_" + suffix + ".png", private_data / ("image_test_" + suffix + ".png"));
180 }
181
182 /** Test Image::alpha_blend */
183 BOOST_AUTO_TEST_CASE (alpha_blend_test)
184 {
185         alpha_blend_test_one (AV_PIX_FMT_RGB24, "rgb24");
186         alpha_blend_test_one (AV_PIX_FMT_BGRA, "bgra");
187         alpha_blend_test_one (AV_PIX_FMT_RGBA, "rgba");
188         alpha_blend_test_one (AV_PIX_FMT_RGB48LE, "rgb48le");
189         alpha_blend_test_one (AV_PIX_FMT_YUV420P, "yuv420p");
190         alpha_blend_test_one (AV_PIX_FMT_YUV420P10, "yuv420p10");
191         alpha_blend_test_one (AV_PIX_FMT_YUV422P10LE, "yuv422p10le");
192 }
193
194 /** Test merge (list<PositionImage>) with a single image */
195 BOOST_AUTO_TEST_CASE (merge_test1)
196 {
197         int const stride = 48 * 4;
198
199         shared_ptr<Image> A (new Image (AV_PIX_FMT_BGRA, dcp::Size (48, 48), false));
200         A->make_transparent ();
201         uint8_t* a = A->data()[0];
202
203         for (int y = 0; y < 48; ++y) {
204                 uint8_t* p = a + y * stride;
205                 for (int x = 0; x < 16; ++x) {
206                         /* blue */
207                         p[x * 4] = 255;
208                         /* opaque */
209                         p[x * 4 + 3] = 255;
210                 }
211         }
212
213         list<PositionImage> all;
214         all.push_back (PositionImage (A, Position<int> (0, 0)));
215         PositionImage merged = merge (all);
216
217         BOOST_CHECK (merged.position == Position<int> (0, 0));
218         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
219 }
220
221 /** Test merge (list<PositionImage>) with two images */
222 BOOST_AUTO_TEST_CASE (merge_test2)
223 {
224         shared_ptr<Image> A (new Image (AV_PIX_FMT_BGRA, dcp::Size (48, 1), false));
225         A->make_transparent ();
226         uint8_t* a = A->data()[0];
227         for (int x = 0; x < 16; ++x) {
228                 /* blue */
229                 a[x * 4] = 255;
230                 /* opaque */
231                 a[x * 4 + 3] = 255;
232         }
233
234         shared_ptr<Image> B (new Image (AV_PIX_FMT_BGRA, dcp::Size (48, 1), false));
235         B->make_transparent ();
236         uint8_t* b = B->data()[0];
237         for (int x = 0; x < 16; ++x) {
238                 /* red */
239                 b[(x + 32) * 4 + 2] = 255;
240                 /* opaque */
241                 b[(x + 32) * 4 + 3] = 255;
242         }
243
244         list<PositionImage> all;
245         all.push_back (PositionImage (A, Position<int> (0, 0)));
246         all.push_back (PositionImage (B, Position<int> (0, 0)));
247         PositionImage merged = merge (all);
248
249         BOOST_CHECK (merged.position == Position<int> (0, 0));
250
251         uint8_t* m = merged.image->data()[0];
252
253         for (int x = 0; x < 16; ++x) {
254                 BOOST_CHECK_EQUAL (m[x * 4], 255);
255                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
256                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
257                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
258                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
259         }
260 }
261
262 /** Test Image::crop_scale_window with YUV420P and some windowing */
263 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
264 {
265         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png"));
266         shared_ptr<Image> raw = proxy->image().first;
267         shared_ptr<Image> out = raw->crop_scale_window(Crop(), dcp::Size(1998, 836), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_YUV420P, true, false);
268         shared_ptr<Image> save = out->scale(dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
269         write_image(save, "build/test/crop_scale_window_test.png", "RGB");
270         check_image("test/data/crop_scale_window_test.png", "build/test/crop_scale_window_test.png");
271 }
272
273 /** Special cases of Image::crop_scale_window which triggered some valgrind warnings */
274 BOOST_AUTO_TEST_CASE (crop_scale_window_test2)
275 {
276         shared_ptr<Image> image (new Image(AV_PIX_FMT_XYZ12LE, dcp::Size(2048, 858), true));
277         image->crop_scale_window (Crop(279, 0, 0, 0), dcp::Size(1069, 448), dcp::Size(1069, 578), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
278         image->crop_scale_window (Crop(2048, 0, 0, 0), dcp::Size(1069, 448), dcp::Size(1069, 578), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
279 }
280
281 BOOST_AUTO_TEST_CASE (crop_scale_window_test3)
282 {
283         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
284         shared_ptr<Image> xyz = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, true, false);
285         shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
286         write_image(cropped, "build/test/crop_scale_window_test3.png", "RGB", MagickCore::CharPixel);
287 }
288
289 BOOST_AUTO_TEST_CASE (crop_scale_window_test4)
290 {
291         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
292         shared_ptr<Image> xyz = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, true, false);
293         shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_XYZ12LE, false, false);
294         write_image(cropped, "build/test/crop_scale_window_test4.png", "RGB", MagickCore::ShortPixel);
295 }
296
297 BOOST_AUTO_TEST_CASE (crop_scale_window_test5)
298 {
299         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
300         shared_ptr<Image> xyz = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_XYZ12LE, true, false);
301         shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
302         write_image(cropped, "build/test/crop_scale_window_test5.png", "RGB", MagickCore::CharPixel);
303 }
304
305 BOOST_AUTO_TEST_CASE (crop_scale_window_test6)
306 {
307         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/player_seek_test_0.png"));
308         shared_ptr<Image> xyz = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_XYZ12LE, true, false);
309         shared_ptr<Image> cropped = xyz->crop_scale_window(Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_XYZ12LE, false, false);
310         write_image(cropped, "build/test/crop_scale_window_test6.png", "RGB", MagickCore::ShortPixel);
311 }
312
313
314 /** Test some small crops with an image that shows up errors in registration of the YUV planes (#1872) */
315 BOOST_AUTO_TEST_CASE (crop_scale_window_test7)
316 {
317         using namespace boost::filesystem;
318         for (int left_crop = 0; left_crop < 8; ++left_crop) {
319                 shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/rgb_grey_testcard.png"));
320                 shared_ptr<Image> yuv = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_YUV420P, true, false);
321                 int rounded = left_crop - (left_crop % 2);
322                 shared_ptr<Image> cropped = yuv->crop_scale_window(
323                         Crop(left_crop, 0, 0, 0),
324                         dcp::Size(1998 - rounded, 1080),
325                         dcp::Size(1998 - rounded, 1080),
326                         dcp::YUV_TO_RGB_REC709,
327                         AV_PIX_FMT_RGB24,
328                         true,
329                         false
330                         );
331                 path file = String::compose("crop_scale_window_test7-%1.png", left_crop);
332                 write_image(cropped, path("build") / "test" / file, "RGB");
333                 check_image(path("test") / "data" / file, path("build") / "test" / file, 10);
334         }
335 }
336
337
338 BOOST_AUTO_TEST_CASE (as_png_test)
339 {
340         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/3d_test/000001.png"));
341         shared_ptr<Image> image_rgb = proxy->image().first;
342         shared_ptr<Image> image_bgr = image_rgb->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_BGRA, true, false);
343         image_rgb->as_png().write ("build/test/as_png_rgb.png");
344         image_bgr->as_png().write ("build/test/as_png_bgr.png");
345
346         check_image ("test/data/3d_test/000001.png", "build/test/as_png_rgb.png");
347         check_image ("test/data/3d_test/000001.png", "build/test/as_png_bgr.png");
348 }
349
350 /* Very dumb test to fade black to make sure it stays black */
351 static void
352 fade_test_format_black (AVPixelFormat f, string name)
353 {
354         Image yuv (f, dcp::Size(640, 480), true);
355         yuv.make_black ();
356         yuv.fade (0);
357         string const filename = "fade_test_black_" + name + ".png";
358         yuv.convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGBA, true, false)->as_png().write("build/test/" + filename);
359         check_image ("test/data/" + filename, "build/test/" + filename);
360 }
361
362 /* Fade red to make sure it stays red */
363 static void
364 fade_test_format_red (AVPixelFormat f, float amount, string name)
365 {
366         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png"));
367         shared_ptr<Image> red = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, f, true, false);
368         red->fade (amount);
369         string const filename = "fade_test_red_" + name + ".png";
370         red->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGBA, true, false)->as_png().write("build/test/" + filename);
371         check_image ("test/data/" + filename, "build/test/" + filename);
372 }
373
374 BOOST_AUTO_TEST_CASE (fade_test)
375 {
376         fade_test_format_black (AV_PIX_FMT_YUV420P,   "yuv420p");
377         fade_test_format_black (AV_PIX_FMT_YUV422P10, "yuv422p10");
378         fade_test_format_black (AV_PIX_FMT_RGB24,     "rgb24");
379         fade_test_format_black (AV_PIX_FMT_XYZ12LE,   "xyz12le");
380         fade_test_format_black (AV_PIX_FMT_RGB48LE,   "rgb48le");
381
382         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0,   "yuv420p_0");
383         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0.5, "yuv420p_50");
384         fade_test_format_red   (AV_PIX_FMT_YUV420P,   1,   "yuv420p_100");
385         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0,   "yuv422p10_0");
386         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0.5, "yuv422p10_50");
387         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 1,   "yuv422p10_100");
388         fade_test_format_red   (AV_PIX_FMT_RGB24,     0,   "rgb24_0");
389         fade_test_format_red   (AV_PIX_FMT_RGB24,     0.5, "rgb24_50");
390         fade_test_format_red   (AV_PIX_FMT_RGB24,     1,   "rgb24_100");
391         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0,   "xyz12le_0");
392         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0.5, "xyz12le_50");
393         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   1,   "xyz12le_100");
394         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0,   "rgb48le_0");
395         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0.5, "rgb48le_50");
396         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   1,   "rgb48le_100");
397 }
398
399
400 /** Make sure the image isn't corrupted if it is cropped too much.  This can happen when a
401  *  filler 128x128 black frame is emitted from the FFmpegDecoder and the overall crop in either direction
402  *  is greater than 128 pixels.
403  */
404 BOOST_AUTO_TEST_CASE (over_crop_test)
405 {
406         shared_ptr<Image> image (new Image (AV_PIX_FMT_RGB24, dcp::Size(128, 128), true));
407         image->make_black ();
408         shared_ptr<Image> scaled = image->crop_scale_window (Crop(0, 0, 128, 128), dcp::Size(1323, 565), dcp::Size(1349, 565), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, true, true);
409         string const filename = "over_crop_test.png";
410         write_image (scaled, "build/test/" + filename, "RGB");
411         check_image ("test/data/" + filename, "build/test/" + filename);
412 }