a529a39355f49a643e8fe38af3abaa1b23d5f20c
[dcpomatic.git] / test / image_test.cc
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file  test/image_test.cc
23  *  @brief Test Image class.
24  *  @ingroup selfcontained
25  *  @see test/pixel_formats_test.cc
26  */
27
28
29 #include "lib/compose.hpp"
30 #include "lib/image.h"
31 #include "lib/image_content.h"
32 #include "lib/image_decoder.h"
33 #include "lib/image_jpeg.h"
34 #include "lib/image_png.h"
35 #include "lib/ffmpeg_image_proxy.h"
36 #include "test.h"
37 #include <boost/test/unit_test.hpp>
38 #include <iostream>
39
40
41 using std::cout;
42 using std::list;
43 using std::make_shared;
44 using std::string;
45
46
47 BOOST_AUTO_TEST_CASE (aligned_image_test)
48 {
49         auto s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), Image::Alignment::PADDED);
50         BOOST_CHECK_EQUAL (s->planes(), 1);
51         /* 192 is 150 aligned to the nearest 64 bytes */
52         BOOST_CHECK_EQUAL (s->stride()[0], 192);
53         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
54         BOOST_CHECK (s->data()[0]);
55         BOOST_CHECK (!s->data()[1]);
56         BOOST_CHECK (!s->data()[2]);
57         BOOST_CHECK (!s->data()[3]);
58
59         /* copy constructor */
60         auto t = new Image (*s);
61         BOOST_CHECK_EQUAL (t->planes(), 1);
62         BOOST_CHECK_EQUAL (t->stride()[0], 192);
63         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
64         BOOST_CHECK (t->data()[0]);
65         BOOST_CHECK (!t->data()[1]);
66         BOOST_CHECK (!t->data()[2]);
67         BOOST_CHECK (!t->data()[3]);
68         BOOST_CHECK (t->data() != s->data());
69         BOOST_CHECK (t->data()[0] != s->data()[0]);
70         BOOST_CHECK (t->line_size() != s->line_size());
71         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
72         BOOST_CHECK (t->stride() != s->stride());
73         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
74
75         /* assignment operator */
76         auto u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), Image::Alignment::COMPACT);
77         *u = *s;
78         BOOST_CHECK_EQUAL (u->planes(), 1);
79         BOOST_CHECK_EQUAL (u->stride()[0], 192);
80         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
81         BOOST_CHECK (u->data()[0]);
82         BOOST_CHECK (!u->data()[1]);
83         BOOST_CHECK (!u->data()[2]);
84         BOOST_CHECK (!u->data()[3]);
85         BOOST_CHECK (u->data() != s->data());
86         BOOST_CHECK (u->data()[0] != s->data()[0]);
87         BOOST_CHECK (u->line_size() != s->line_size());
88         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
89         BOOST_CHECK (u->stride() != s->stride());
90         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
91
92         delete s;
93         delete t;
94         delete u;
95 }
96
97
98 BOOST_AUTO_TEST_CASE (compact_image_test)
99 {
100         auto s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), Image::Alignment::COMPACT);
101         BOOST_CHECK_EQUAL (s->planes(), 1);
102         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
103         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
104         BOOST_CHECK (s->data()[0]);
105         BOOST_CHECK (!s->data()[1]);
106         BOOST_CHECK (!s->data()[2]);
107         BOOST_CHECK (!s->data()[3]);
108
109         /* copy constructor */
110         auto t = new Image (*s);
111         BOOST_CHECK_EQUAL (t->planes(), 1);
112         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
113         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
114         BOOST_CHECK (t->data()[0]);
115         BOOST_CHECK (!t->data()[1]);
116         BOOST_CHECK (!t->data()[2]);
117         BOOST_CHECK (!t->data()[3]);
118         BOOST_CHECK (t->data() != s->data());
119         BOOST_CHECK (t->data()[0] != s->data()[0]);
120         BOOST_CHECK (t->line_size() != s->line_size());
121         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
122         BOOST_CHECK (t->stride() != s->stride());
123         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
124
125         /* assignment operator */
126         auto u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), Image::Alignment::PADDED);
127         *u = *s;
128         BOOST_CHECK_EQUAL (u->planes(), 1);
129         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
130         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
131         BOOST_CHECK (u->data()[0]);
132         BOOST_CHECK (!u->data()[1]);
133         BOOST_CHECK (!u->data()[2]);
134         BOOST_CHECK (!u->data()[3]);
135         BOOST_CHECK (u->data() != s->data());
136         BOOST_CHECK (u->data()[0] != s->data()[0]);
137         BOOST_CHECK (u->line_size() != s->line_size());
138         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
139         BOOST_CHECK (u->stride() != s->stride());
140         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
141
142         delete s;
143         delete t;
144         delete u;
145 }
146
147
148 void
149 alpha_blend_test_one (AVPixelFormat format, string suffix)
150 {
151         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "prophet_frame.tiff");
152         auto raw = proxy->image(Image::Alignment::PADDED).image;
153         auto background = raw->convert_pixel_format (dcp::YUVToRGB::REC709, format, Image::Alignment::PADDED, false);
154
155         auto overlay = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size(431, 891), Image::Alignment::PADDED);
156         overlay->make_transparent ();
157
158         for (int y = 0; y < 128; ++y) {
159                 auto p = overlay->data()[0] + y * overlay->stride()[0];
160                 for (int x = 0; x < 128; ++x) {
161                         p[x * 4 + 2] = 255;
162                         p[x * 4 + 3] = 255;
163                 }
164         }
165
166         for (int y = 128; y < 256; ++y) {
167                 auto p = overlay->data()[0] + y * overlay->stride()[0];
168                 for (int x = 0; x < 128; ++x) {
169                         p[x * 4 + 1] = 255;
170                         p[x * 4 + 3] = 255;
171                 }
172         }
173
174         for (int y = 256; y < 384; ++y) {
175                 auto p = overlay->data()[0] + y * overlay->stride()[0];
176                 for (int x = 0; x < 128; ++x) {
177                         p[x * 4] = 255;
178                         p[x * 4 + 3] = 255;
179                 }
180         }
181
182         background->alpha_blend (overlay, Position<int> (13, 17));
183
184         auto save = background->convert_pixel_format (dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false);
185
186         write_image (save, "build/test/image_test_" + suffix + ".png");
187         check_image ("build/test/image_test_" + suffix + ".png", TestPaths::private_data() / ("image_test_" + suffix + ".png"));
188 }
189
190
191 /** Test Image::alpha_blend */
192 BOOST_AUTO_TEST_CASE (alpha_blend_test)
193 {
194         alpha_blend_test_one (AV_PIX_FMT_RGB24, "rgb24");
195         alpha_blend_test_one (AV_PIX_FMT_BGRA, "bgra");
196         alpha_blend_test_one (AV_PIX_FMT_RGBA, "rgba");
197         alpha_blend_test_one (AV_PIX_FMT_RGB48LE, "rgb48le");
198         alpha_blend_test_one (AV_PIX_FMT_YUV420P, "yuv420p");
199         alpha_blend_test_one (AV_PIX_FMT_YUV420P10, "yuv420p10");
200         alpha_blend_test_one (AV_PIX_FMT_YUV422P10LE, "yuv422p10le");
201 }
202
203
204 /** Test Image::alpha_blend when the "base" image is XYZ12LE */
205 BOOST_AUTO_TEST_CASE (alpha_blend_test_onto_xyz)
206 {
207         Image xyz(AV_PIX_FMT_XYZ12LE, dcp::Size(50, 50), Image::Alignment::PADDED);
208         xyz.make_black();
209
210         auto overlay = make_shared<Image>(AV_PIX_FMT_RGBA, dcp::Size(8, 8), Image::Alignment::PADDED);
211         for (int y = 0; y < 8; ++y) {
212                 uint8_t* p = overlay->data()[0] + (y * overlay->stride()[0]);
213                 for (int x = 0; x < 8; ++x) {
214                         *p++ = 255;
215                         *p++ = 0;
216                         *p++ = 0;
217                         *p++ = 255;
218                 }
219         }
220
221         xyz.alpha_blend(overlay, Position<int>(4, 4));
222
223         for (int y = 0; y < 50; ++y) {
224                 uint16_t* p = reinterpret_cast<uint16_t*>(xyz.data()[0]) + (y * xyz.stride()[0] / 2);
225                 for (int x = 0; x < 50; ++x) {
226                         std::cout << "x=" << x << ", y=" << y << "\n";
227                         if (4 <= x && x < 12 && 4 <= y && y < 12) {
228                                 BOOST_REQUIRE_EQUAL(p[0], 45078U);
229                                 BOOST_REQUIRE_EQUAL(p[1], 34939U);
230                                 BOOST_REQUIRE_EQUAL(p[2], 13892U);
231                         } else {
232                                 BOOST_REQUIRE_EQUAL(p[0], 0U);
233                                 BOOST_REQUIRE_EQUAL(p[1], 0U);
234                                 BOOST_REQUIRE_EQUAL(p[2], 0U);
235                         }
236                         p += 3;
237                 }
238         }
239 }
240
241
242 /** Test merge (list<PositionImage>) with a single image */
243 BOOST_AUTO_TEST_CASE (merge_test1)
244 {
245         int const stride = 48 * 4;
246
247         auto A = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (48, 48), Image::Alignment::COMPACT);
248         A->make_transparent ();
249         auto a = A->data()[0];
250
251         for (int y = 0; y < 48; ++y) {
252                 auto p = a + y * stride;
253                 for (int x = 0; x < 16; ++x) {
254                         /* blue */
255                         p[x * 4] = 255;
256                         /* opaque */
257                         p[x * 4 + 3] = 255;
258                 }
259         }
260
261         list<PositionImage> all;
262         all.push_back (PositionImage (A, Position<int>(0, 0)));
263         auto merged = merge (all, Image::Alignment::COMPACT);
264
265         BOOST_CHECK (merged.position == Position<int>(0, 0));
266         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
267 }
268
269
270 /** Test merge (list<PositionImage>) with two images */
271 BOOST_AUTO_TEST_CASE (merge_test2)
272 {
273         auto A = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (48, 1), Image::Alignment::COMPACT);
274         A->make_transparent ();
275         auto a = A->data()[0];
276         for (int x = 0; x < 16; ++x) {
277                 /* blue */
278                 a[x * 4] = 255;
279                 /* opaque */
280                 a[x * 4 + 3] = 255;
281         }
282
283         auto B = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (48, 1), Image::Alignment::COMPACT);
284         B->make_transparent ();
285         auto b = B->data()[0];
286         for (int x = 0; x < 16; ++x) {
287                 /* red */
288                 b[(x + 32) * 4 + 2] = 255;
289                 /* opaque */
290                 b[(x + 32) * 4 + 3] = 255;
291         }
292
293         list<PositionImage> all;
294         all.push_back (PositionImage(A, Position<int>(0, 0)));
295         all.push_back (PositionImage(B, Position<int>(0, 0)));
296         auto merged = merge (all, Image::Alignment::COMPACT);
297
298         BOOST_CHECK (merged.position == Position<int>(0, 0));
299
300         auto m = merged.image->data()[0];
301
302         for (int x = 0; x < 16; ++x) {
303                 BOOST_CHECK_EQUAL (m[x * 4], 255);
304                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
305                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
306                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
307                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
308         }
309 }
310
311
312 /** Test Image::crop_scale_window with YUV420P and some windowing */
313 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
314 {
315         auto proxy = make_shared<FFmpegImageProxy>("test/data/flat_red.png");
316         auto raw = proxy->image(Image::Alignment::PADDED).image;
317         auto out = raw->crop_scale_window(
318                 Crop(), dcp::Size(1998, 836), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_YUV420P, VideoRange::FULL, Image::Alignment::PADDED, false
319                 );
320         auto save = out->scale(dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false);
321         write_image(save, "build/test/crop_scale_window_test.png");
322         check_image("test/data/crop_scale_window_test.png", "build/test/crop_scale_window_test.png");
323 }
324
325
326 /** Special cases of Image::crop_scale_window which triggered some valgrind warnings */
327 BOOST_AUTO_TEST_CASE (crop_scale_window_test2)
328 {
329         auto image = make_shared<Image>(AV_PIX_FMT_XYZ12LE, dcp::Size(2048, 858), Image::Alignment::PADDED);
330         image->crop_scale_window (
331                 Crop(279, 0, 0, 0), dcp::Size(1069, 448), dcp::Size(1069, 578), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_RGB24, VideoRange::FULL, Image::Alignment::COMPACT, false
332                 );
333         image->crop_scale_window (
334                 Crop(2048, 0, 0, 0), dcp::Size(1069, 448), dcp::Size(1069, 578), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_RGB24, VideoRange::FULL, Image::Alignment::COMPACT, false
335                 );
336 }
337
338
339 BOOST_AUTO_TEST_CASE (crop_scale_window_test3)
340 {
341         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
342         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
343         auto cropped = xyz->crop_scale_window(
344                 Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_RGB24, VideoRange::FULL, Image::Alignment::COMPACT, false
345                 );
346         write_image(cropped, "build/test/crop_scale_window_test3.png");
347         check_image("test/data/crop_scale_window_test3.png", "build/test/crop_scale_window_test3.png");
348 }
349
350
351 BOOST_AUTO_TEST_CASE (crop_scale_window_test4)
352 {
353         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
354         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
355         auto cropped = xyz->crop_scale_window(
356                 Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_XYZ12LE, VideoRange::FULL, Image::Alignment::COMPACT, false
357                 );
358         write_image(cropped, "build/test/crop_scale_window_test4.png");
359         check_image("test/data/crop_scale_window_test4.png", "build/test/crop_scale_window_test4.png", 35000);
360 }
361
362
363 BOOST_AUTO_TEST_CASE (crop_scale_window_test5)
364 {
365         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
366         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_XYZ12LE, Image::Alignment::PADDED, false);
367         auto cropped = xyz->crop_scale_window(
368                 Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_RGB24, VideoRange::FULL, Image::Alignment::COMPACT, false
369                 );
370         write_image(cropped, "build/test/crop_scale_window_test5.png");
371         check_image("test/data/crop_scale_window_test5.png", "build/test/crop_scale_window_test5.png");
372 }
373
374
375 BOOST_AUTO_TEST_CASE (crop_scale_window_test6)
376 {
377         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
378         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_XYZ12LE, Image::Alignment::PADDED, false);
379         auto cropped = xyz->crop_scale_window(
380                 Crop(512, 0, 0, 0), dcp::Size(1486, 1080), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_XYZ12LE, VideoRange::FULL, Image::Alignment::COMPACT, false
381                 );
382         write_image(cropped, "build/test/crop_scale_window_test6.png");
383         check_image("test/data/crop_scale_window_test6.png", "build/test/crop_scale_window_test6.png", 35000);
384 }
385
386
387 /** Test some small crops with an image that shows up errors in registration of the YUV planes (#1872) */
388 BOOST_AUTO_TEST_CASE (crop_scale_window_test7)
389 {
390         using namespace boost::filesystem;
391         for (int left_crop = 0; left_crop < 8; ++left_crop) {
392                 auto proxy = make_shared<FFmpegImageProxy>("test/data/rgb_grey_testcard.png");
393                 auto yuv = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_YUV420P, Image::Alignment::PADDED, false);
394                 int rounded = left_crop - (left_crop % 2);
395                 auto cropped = yuv->crop_scale_window(
396                         Crop(left_crop, 0, 0, 0),
397                         dcp::Size(1998 - rounded, 1080),
398                         dcp::Size(1998 - rounded, 1080),
399                         dcp::YUVToRGB::REC709,
400                         VideoRange::VIDEO,
401                         AV_PIX_FMT_RGB24,
402                         VideoRange::VIDEO,
403                         Image::Alignment::PADDED,
404                         false
405                         );
406                 path file = String::compose("crop_scale_window_test7-%1.png", left_crop);
407                 write_image(cropped, path("build") / "test" / file);
408                 check_image(path("test") / "data" / file, path("build") / "test" / file, 10);
409         }
410 }
411
412
413 BOOST_AUTO_TEST_CASE (as_png_test)
414 {
415         auto proxy = make_shared<FFmpegImageProxy>("test/data/3d_test/000001.png");
416         auto image_rgb = proxy->image(Image::Alignment::PADDED).image;
417         auto image_bgr = image_rgb->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_BGRA, Image::Alignment::PADDED, false);
418         image_as_png(image_rgb).write ("build/test/as_png_rgb.png");
419         image_as_png(image_bgr).write ("build/test/as_png_bgr.png");
420
421         check_image ("test/data/3d_test/000001.png", "build/test/as_png_rgb.png");
422         check_image ("test/data/3d_test/000001.png", "build/test/as_png_bgr.png");
423 }
424
425
426 BOOST_AUTO_TEST_CASE (as_jpeg_test)
427 {
428         auto proxy = make_shared<FFmpegImageProxy>("test/data/3d_test/000001.png");
429         auto image_rgb = proxy->image(Image::Alignment::PADDED).image;
430         auto image_bgr = image_rgb->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_BGRA, Image::Alignment::PADDED, false);
431         image_as_jpeg(image_rgb, 60).write("build/test/as_jpeg_rgb.jpeg");
432         image_as_jpeg(image_bgr, 60).write("build/test/as_jpeg_bgr.jpeg");
433
434         check_image ("test/data/as_jpeg_rgb.jpeg", "build/test/as_jpeg_rgb.jpeg");
435         check_image ("test/data/as_jpeg_bgr.jpeg", "build/test/as_jpeg_bgr.jpeg");
436 }
437
438
439 /* Very dumb test to fade black to make sure it stays black */
440 static void
441 fade_test_format_black (AVPixelFormat f, string name)
442 {
443         Image yuv (f, dcp::Size(640, 480), Image::Alignment::PADDED);
444         yuv.make_black ();
445         yuv.fade (0);
446         string const filename = "fade_test_black_" + name + ".png";
447         image_as_png(yuv.convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGBA, Image::Alignment::PADDED, false)).write("build/test/" + filename);
448         check_image ("test/data/" + filename, "build/test/" + filename);
449 }
450
451
452 /* Fade red to make sure it stays red */
453 static void
454 fade_test_format_red (AVPixelFormat f, float amount, string name)
455 {
456         auto proxy = make_shared<FFmpegImageProxy>("test/data/flat_red.png");
457         auto red = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, f, Image::Alignment::PADDED, false);
458         red->fade (amount);
459         string const filename = "fade_test_red_" + name + ".png";
460         image_as_png(red->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGBA, Image::Alignment::PADDED, false)).write("build/test/" + filename);
461         check_image ("test/data/" + filename, "build/test/" + filename);
462 }
463
464
465 BOOST_AUTO_TEST_CASE (fade_test)
466 {
467         fade_test_format_black (AV_PIX_FMT_YUV420P,   "yuv420p");
468         fade_test_format_black (AV_PIX_FMT_YUV422P10, "yuv422p10");
469         fade_test_format_black (AV_PIX_FMT_RGB24,     "rgb24");
470         fade_test_format_black (AV_PIX_FMT_XYZ12LE,   "xyz12le");
471         fade_test_format_black (AV_PIX_FMT_RGB48LE,   "rgb48le");
472
473         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0,   "yuv420p_0");
474         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0.5, "yuv420p_50");
475         fade_test_format_red   (AV_PIX_FMT_YUV420P,   1,   "yuv420p_100");
476         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0,   "yuv422p10_0");
477         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0.5, "yuv422p10_50");
478         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 1,   "yuv422p10_100");
479         fade_test_format_red   (AV_PIX_FMT_RGB24,     0,   "rgb24_0");
480         fade_test_format_red   (AV_PIX_FMT_RGB24,     0.5, "rgb24_50");
481         fade_test_format_red   (AV_PIX_FMT_RGB24,     1,   "rgb24_100");
482         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0,   "xyz12le_0");
483         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0.5, "xyz12le_50");
484         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   1,   "xyz12le_100");
485         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0,   "rgb48le_0");
486         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0.5, "rgb48le_50");
487         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   1,   "rgb48le_100");
488 }
489
490
491 BOOST_AUTO_TEST_CASE (make_black_test)
492 {
493         dcp::Size in_size (512, 512);
494         dcp::Size out_size (1024, 1024);
495
496         list<AVPixelFormat> pix_fmts = {
497                 AV_PIX_FMT_RGB24, // 2
498                 AV_PIX_FMT_ARGB,
499                 AV_PIX_FMT_RGBA,
500                 AV_PIX_FMT_ABGR,
501                 AV_PIX_FMT_BGRA,
502                 AV_PIX_FMT_YUV420P, // 0
503                 AV_PIX_FMT_YUV411P,
504                 AV_PIX_FMT_YUV422P10LE,
505                 AV_PIX_FMT_YUV422P16LE,
506                 AV_PIX_FMT_YUV444P9LE,
507                 AV_PIX_FMT_YUV444P9BE,
508                 AV_PIX_FMT_YUV444P10LE,
509                 AV_PIX_FMT_YUV444P10BE,
510                 AV_PIX_FMT_UYVY422,
511                 AV_PIX_FMT_YUVJ420P,
512                 AV_PIX_FMT_YUVJ422P,
513                 AV_PIX_FMT_YUVJ444P,
514                 AV_PIX_FMT_YUVA420P9BE,
515                 AV_PIX_FMT_YUVA422P9BE,
516                 AV_PIX_FMT_YUVA444P9BE,
517                 AV_PIX_FMT_YUVA420P9LE,
518                 AV_PIX_FMT_YUVA422P9LE,
519                 AV_PIX_FMT_YUVA444P9LE,
520                 AV_PIX_FMT_YUVA420P10BE,
521                 AV_PIX_FMT_YUVA422P10BE,
522                 AV_PIX_FMT_YUVA444P10BE,
523                 AV_PIX_FMT_YUVA420P10LE,
524                 AV_PIX_FMT_YUVA422P10LE,
525                 AV_PIX_FMT_YUVA444P10LE,
526                 AV_PIX_FMT_YUVA420P16BE,
527                 AV_PIX_FMT_YUVA422P16BE,
528                 AV_PIX_FMT_YUVA444P16BE,
529                 AV_PIX_FMT_YUVA420P16LE,
530                 AV_PIX_FMT_YUVA422P16LE,
531                 AV_PIX_FMT_YUVA444P16LE,
532                 AV_PIX_FMT_RGB555LE, // 46
533         };
534
535         int N = 0;
536         for (auto i: pix_fmts) {
537                 auto foo = make_shared<Image>(i, in_size, Image::Alignment::PADDED);
538                 foo->make_black ();
539                 auto bar = foo->scale (out_size, dcp::YUVToRGB::REC601, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
540
541                 uint8_t* p = bar->data()[0];
542                 for (int y = 0; y < bar->size().height; ++y) {
543                         uint8_t* q = p;
544                         for (int x = 0; x < bar->line_size()[0]; ++x) {
545                                 if (*q != 0) {
546                                         std::cerr << "x=" << x << ", (x%3)=" << (x%3) << "\n";
547                                 }
548                                 BOOST_CHECK_EQUAL (*q++, 0);
549                         }
550                         p += bar->stride()[0];
551                 }
552
553                 ++N;
554         }
555 }
556
557
558 BOOST_AUTO_TEST_CASE (make_part_black_test)
559 {
560         auto proxy = make_shared<FFmpegImageProxy>("test/data/flat_red.png");
561         auto original = proxy->image(Image::Alignment::PADDED).image;
562
563         list<AVPixelFormat> pix_fmts = {
564                 AV_PIX_FMT_RGB24,
565                 AV_PIX_FMT_ARGB,
566                 AV_PIX_FMT_RGBA,
567                 AV_PIX_FMT_ABGR,
568                 AV_PIX_FMT_BGRA,
569                 AV_PIX_FMT_YUV420P,
570                 AV_PIX_FMT_YUV422P10LE,
571         };
572
573         list<std::pair<int, int>> positions = {
574                 { 0, 256 },
575                 { 128, 64 },
576         };
577
578         int N = 0;
579         for (auto i: pix_fmts) {
580                 for (auto j: positions) {
581                         auto foo = original->convert_pixel_format(dcp::YUVToRGB::REC601, i, Image::Alignment::PADDED, false);
582                         foo->make_part_black (j.first, j.second);
583                         auto bar = foo->convert_pixel_format (dcp::YUVToRGB::REC601, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
584
585                         auto p = bar->data()[0];
586                         for (int y = 0; y < bar->size().height; ++y) {
587                                 auto q = p;
588                                 for (int x = 0; x < bar->size().width; ++x) {
589                                         int r = *q++;
590                                         int g = *q++;
591                                         int b = *q++;
592                                         if (x >= j.first && x < (j.first + j.second)) {
593                                                 BOOST_CHECK_MESSAGE (
594                                                         r < 3, "red=" << static_cast<int>(r) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
595                                                         );
596                                         } else {
597                                                 BOOST_CHECK_MESSAGE (
598                                                         r >= 252, "red=" << static_cast<int>(r) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
599                                                         );
600
601                                         }
602                                         BOOST_CHECK_MESSAGE (
603                                                 g == 0, "green=" << static_cast<int>(g) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
604                                                 );
605                                         BOOST_CHECK_MESSAGE (
606                                                 b == 0, "blue=" << static_cast<int>(b) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
607                                                 );
608                                 }
609                                 p += bar->stride()[0];
610                         }
611
612                         ++N;
613                 }
614         }
615 }
616
617
618 /** Make sure the image isn't corrupted if it is cropped too much.  This can happen when a
619  *  filler 128x128 black frame is emitted from the FFmpegDecoder and the overall crop in either direction
620  *  is greater than 128 pixels.
621  */
622 BOOST_AUTO_TEST_CASE (over_crop_test)
623 {
624         auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size(128, 128), Image::Alignment::PADDED);
625         image->make_black ();
626         auto scaled = image->crop_scale_window (
627                 Crop(0, 0, 128, 128), dcp::Size(1323, 565), dcp::Size(1349, 565), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_RGB24, VideoRange::FULL, Image::Alignment::PADDED, true
628                 );
629         string const filename = "over_crop_test.png";
630         write_image (scaled, "build/test/" + filename);
631         check_image ("test/data/" + filename, "build/test/" + filename);
632 }