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