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