Missed update to private test repo version.
[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 static
149 void
150 alpha_blend_test_bgra_onto(AVPixelFormat format, string suffix)
151 {
152         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "prophet_frame.tiff");
153         auto raw = proxy->image(Image::Alignment::PADDED).image;
154         auto background = raw->convert_pixel_format (dcp::YUVToRGB::REC709, format, Image::Alignment::PADDED, false);
155
156         auto overlay = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size(431, 891), Image::Alignment::PADDED);
157         overlay->make_transparent ();
158
159         for (int y = 0; y < 128; ++y) {
160                 auto p = overlay->data()[0] + y * overlay->stride()[0];
161                 for (int x = 0; x < 128; ++x) {
162                         p[x * 4 + 2] = 255;
163                         p[x * 4 + 3] = 255;
164                 }
165         }
166
167         for (int y = 128; y < 256; ++y) {
168                 auto p = overlay->data()[0] + y * overlay->stride()[0];
169                 for (int x = 0; x < 128; ++x) {
170                         p[x * 4 + 1] = 255;
171                         p[x * 4 + 3] = 255;
172                 }
173         }
174
175         for (int y = 256; y < 384; ++y) {
176                 auto p = overlay->data()[0] + y * overlay->stride()[0];
177                 for (int x = 0; x < 128; ++x) {
178                         p[x * 4] = 255;
179                         p[x * 4 + 3] = 255;
180                 }
181         }
182
183         background->alpha_blend (overlay, Position<int> (13, 17));
184
185         auto save = background->convert_pixel_format (dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false);
186
187         write_image(save, "build/test/image_test_bgra_" + suffix + ".png");
188         check_image("build/test/image_test_bgra_" + suffix + ".png", TestPaths::private_data() / ("image_test_bgra_" + suffix + ".png"));
189 }
190
191
192 static
193 void
194 alpha_blend_test_rgba64be_onto(AVPixelFormat format, string suffix)
195 {
196         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "prophet_frame.tiff");
197         auto raw = proxy->image(Image::Alignment::PADDED).image;
198         auto background = raw->convert_pixel_format (dcp::YUVToRGB::REC709, format, Image::Alignment::PADDED, false);
199
200         auto overlay = make_shared<Image>(AV_PIX_FMT_RGBA64BE, dcp::Size(431, 891), Image::Alignment::PADDED);
201         overlay->make_transparent();
202
203         for (int y = 0; y < 128; ++y) {
204                 auto p = reinterpret_cast<uint16_t*>(overlay->data()[0] + y * overlay->stride()[0]);
205                 for (int x = 0; x < 128; ++x) {
206                         p[x * 4 + 0] = 65535;
207                         p[x * 4 + 3] = 65535;
208                 }
209         }
210
211         for (int y = 128; y < 256; ++y) {
212                 auto p = reinterpret_cast<uint16_t*>(overlay->data()[0] + y * overlay->stride()[0]);
213                 for (int x = 0; x < 128; ++x) {
214                         p[x * 4 + 1] = 65535;
215                         p[x * 4 + 3] = 65535;
216                 }
217         }
218
219         for (int y = 256; y < 384; ++y) {
220                 auto p = reinterpret_cast<uint16_t*>(overlay->data()[0] + y * overlay->stride()[0]);
221                 for (int x = 0; x < 128; ++x) {
222                         p[x * 4 + 2] = 65535;
223                         p[x * 4 + 3] = 65535;
224                 }
225         }
226
227         background->alpha_blend(overlay, Position<int>(13, 17));
228
229         auto save = background->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false);
230
231         write_image(save, "build/test/image_test_rgba64_" + suffix + ".png");
232         check_image("build/test/image_test_rgba64_" + suffix + ".png", TestPaths::private_data() / ("image_test_rgba64_" + suffix + ".png"));
233 }
234
235
236 /** Test Image::alpha_blend */
237 BOOST_AUTO_TEST_CASE (alpha_blend_test)
238 {
239         alpha_blend_test_bgra_onto(AV_PIX_FMT_RGB24, "rgb24");
240         alpha_blend_test_bgra_onto(AV_PIX_FMT_BGRA, "bgra");
241         alpha_blend_test_bgra_onto(AV_PIX_FMT_RGBA, "rgba");
242         alpha_blend_test_bgra_onto(AV_PIX_FMT_RGB48LE, "rgb48le");
243         alpha_blend_test_bgra_onto(AV_PIX_FMT_YUV420P, "yuv420p");
244         alpha_blend_test_bgra_onto(AV_PIX_FMT_YUV420P10, "yuv420p10");
245         alpha_blend_test_bgra_onto(AV_PIX_FMT_YUV422P9LE, "yuv422p9le");
246         alpha_blend_test_bgra_onto(AV_PIX_FMT_YUV422P10LE, "yuv422p10le");
247         alpha_blend_test_bgra_onto(AV_PIX_FMT_YUV444P9LE, "yuv444p9le");
248         alpha_blend_test_bgra_onto(AV_PIX_FMT_YUV444P10LE, "yuv444p10le");
249
250         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_RGB24, "rgb24");
251         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_BGRA, "bgra");
252         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_RGBA, "rgba");
253         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_RGB48LE, "rgb48le");
254         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_YUV420P, "yuv420p");
255         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_YUV420P10, "yuv420p10");
256         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_YUV422P9LE, "yuv422p9le");
257         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_YUV422P10LE, "yuv422p10le");
258         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_YUV444P9LE, "yuv444p9le");
259         alpha_blend_test_rgba64be_onto(AV_PIX_FMT_YUV444P10LE, "yuv444p10le");
260 }
261
262
263 /** Test Image::alpha_blend when blending RGBA onto XYZ12LE */
264 BOOST_AUTO_TEST_CASE(alpha_blend_test_rgba_onto_xyz)
265 {
266         Image xyz(AV_PIX_FMT_XYZ12LE, dcp::Size(50, 50), Image::Alignment::PADDED);
267         xyz.make_black();
268
269         auto overlay = make_shared<Image>(AV_PIX_FMT_RGBA, dcp::Size(8, 8), Image::Alignment::PADDED);
270         for (int y = 0; y < 8; ++y) {
271                 uint8_t* p = overlay->data()[0] + (y * overlay->stride()[0]);
272                 for (int x = 0; x < 8; ++x) {
273                         *p++ = 255;
274                         *p++ = 0;
275                         *p++ = 0;
276                         *p++ = 255;
277                 }
278         }
279
280         xyz.alpha_blend(overlay, Position<int>(4, 4));
281
282         for (int y = 0; y < 50; ++y) {
283                 uint16_t* p = reinterpret_cast<uint16_t*>(xyz.data()[0]) + (y * xyz.stride()[0] / 2);
284                 for (int x = 0; x < 50; ++x) {
285                         if (4 <= x && x < 12 && 4 <= y && y < 12) {
286                                 BOOST_REQUIRE_EQUAL(p[0], 45078U);
287                                 BOOST_REQUIRE_EQUAL(p[1], 34939U);
288                                 BOOST_REQUIRE_EQUAL(p[2], 13892U);
289                         } else {
290                                 BOOST_REQUIRE_EQUAL(p[0], 0U);
291                                 BOOST_REQUIRE_EQUAL(p[1], 0U);
292                                 BOOST_REQUIRE_EQUAL(p[2], 0U);
293                         }
294                         p += 3;
295                 }
296         }
297 }
298
299
300 /** Test Image::alpha_blend when blending RGBA64BE onto XYZ12LE */
301 BOOST_AUTO_TEST_CASE(alpha_blend_test_rgba64be_onto_xyz)
302 {
303         Image xyz(AV_PIX_FMT_XYZ12LE, dcp::Size(50, 50), Image::Alignment::PADDED);
304         xyz.make_black();
305
306         auto overlay = make_shared<Image>(AV_PIX_FMT_RGBA64BE, dcp::Size(8, 8), Image::Alignment::PADDED);
307         for (int y = 0; y < 8; ++y) {
308                 auto p = reinterpret_cast<uint16_t*>(overlay->data()[0] + (y * overlay->stride()[0]));
309                 for (int x = 0; x < 8; ++x) {
310                         *p++ = 65535;
311                         *p++ = 0;
312                         *p++ = 0;
313                         *p++ = 65535;
314                 }
315         }
316
317         xyz.alpha_blend(overlay, Position<int>(4, 4));
318
319         for (int y = 0; y < 50; ++y) {
320                 auto p = reinterpret_cast<uint16_t*>(xyz.data()[0]) + (y * xyz.stride()[0] / 2);
321                 for (int x = 0; x < 50; ++x) {
322                         if (4 <= x && x < 12 && 4 <= y && y < 12) {
323                                 BOOST_REQUIRE_EQUAL(p[0], 45078U);
324                                 BOOST_REQUIRE_EQUAL(p[1], 34939U);
325                                 BOOST_REQUIRE_EQUAL(p[2], 13892U);
326                         } else {
327                                 BOOST_REQUIRE_EQUAL(p[0], 0U);
328                                 BOOST_REQUIRE_EQUAL(p[1], 0U);
329                                 BOOST_REQUIRE_EQUAL(p[2], 0U);
330                         }
331                         p += 3;
332                 }
333         }
334 }
335
336
337 BOOST_AUTO_TEST_CASE(alpha_blend_text)
338 {
339         Image target(AV_PIX_FMT_RGB24, dcp::Size(1998, 1080), Image::Alignment::PADDED);
340         target.make_black();
341
342         FFmpegImageProxy subtitle_proxy(TestPaths::private_data() / "16-bit-sub.png");
343         auto subtitle = subtitle_proxy.image(Image::Alignment::COMPACT);
344
345         target.alpha_blend(subtitle.image, Position<int>(0, 0));
346         write_image(make_shared<Image>(target), "build/test/alpha_blend_text.png");
347         check_image("build/test/alpha_blend_text.png", TestPaths::private_data() / "16-bit-sub-blended.png");
348 }
349
350
351 /** Test merge (list<PositionImage>) with a single image */
352 BOOST_AUTO_TEST_CASE (merge_test1)
353 {
354         int const stride = 48 * 4;
355
356         auto A = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (48, 48), Image::Alignment::COMPACT);
357         A->make_transparent ();
358         auto a = A->data()[0];
359
360         for (int y = 0; y < 48; ++y) {
361                 auto p = a + y * stride;
362                 for (int x = 0; x < 16; ++x) {
363                         /* blue */
364                         p[x * 4] = 255;
365                         /* opaque */
366                         p[x * 4 + 3] = 255;
367                 }
368         }
369
370         list<PositionImage> all;
371         all.push_back (PositionImage (A, Position<int>(0, 0)));
372         auto merged = merge (all, Image::Alignment::COMPACT);
373
374         BOOST_CHECK (merged.position == Position<int>(0, 0));
375         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
376 }
377
378
379 /** Test merge (list<PositionImage>) with two images */
380 BOOST_AUTO_TEST_CASE (merge_test2)
381 {
382         auto A = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (48, 1), Image::Alignment::COMPACT);
383         A->make_transparent ();
384         auto a = A->data()[0];
385         for (int x = 0; x < 16; ++x) {
386                 /* blue */
387                 a[x * 4] = 255;
388                 /* opaque */
389                 a[x * 4 + 3] = 255;
390         }
391
392         auto B = make_shared<Image>(AV_PIX_FMT_BGRA, dcp::Size (48, 1), Image::Alignment::COMPACT);
393         B->make_transparent ();
394         auto b = B->data()[0];
395         for (int x = 0; x < 16; ++x) {
396                 /* red */
397                 b[(x + 32) * 4 + 2] = 255;
398                 /* opaque */
399                 b[(x + 32) * 4 + 3] = 255;
400         }
401
402         list<PositionImage> all;
403         all.push_back (PositionImage(A, Position<int>(0, 0)));
404         all.push_back (PositionImage(B, Position<int>(0, 0)));
405         auto merged = merge (all, Image::Alignment::COMPACT);
406
407         BOOST_CHECK (merged.position == Position<int>(0, 0));
408
409         auto m = merged.image->data()[0];
410
411         for (int x = 0; x < 16; ++x) {
412                 BOOST_CHECK_EQUAL (m[x * 4], 255);
413                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
414                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
415                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
416                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
417         }
418 }
419
420
421 /** Test Image::crop_scale_window with YUV420P and some windowing */
422 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
423 {
424         auto proxy = make_shared<FFmpegImageProxy>("test/data/flat_red.png");
425         auto raw = proxy->image(Image::Alignment::PADDED).image;
426         auto out = raw->crop_scale_window(
427                 Crop(), dcp::Size(1998, 836), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_YUV420P, VideoRange::FULL, Image::Alignment::PADDED, false
428                 );
429         auto save = out->scale(dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false);
430         write_image(save, "build/test/crop_scale_window_test.png");
431         check_image("test/data/crop_scale_window_test.png", "build/test/crop_scale_window_test.png");
432 }
433
434
435 /** Special cases of Image::crop_scale_window which triggered some valgrind warnings */
436 BOOST_AUTO_TEST_CASE (crop_scale_window_test2)
437 {
438         auto image = make_shared<Image>(AV_PIX_FMT_XYZ12LE, dcp::Size(2048, 858), Image::Alignment::PADDED);
439         image->crop_scale_window (
440                 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
441                 );
442         image->crop_scale_window (
443                 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
444                 );
445 }
446
447
448 BOOST_AUTO_TEST_CASE (crop_scale_window_test3)
449 {
450         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
451         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
452         auto cropped = xyz->crop_scale_window(
453                 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
454                 );
455         write_image(cropped, "build/test/crop_scale_window_test3.png");
456         check_image("test/data/crop_scale_window_test3.png", "build/test/crop_scale_window_test3.png");
457 }
458
459
460 BOOST_AUTO_TEST_CASE (crop_scale_window_test4)
461 {
462         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
463         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
464         auto cropped = xyz->crop_scale_window(
465                 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
466                 );
467         write_image(cropped, "build/test/crop_scale_window_test4.png");
468         check_image("test/data/crop_scale_window_test4.png", "build/test/crop_scale_window_test4.png");
469 }
470
471
472 BOOST_AUTO_TEST_CASE (crop_scale_window_test5)
473 {
474         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
475         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_XYZ12LE, Image::Alignment::PADDED, false);
476         auto cropped = xyz->crop_scale_window(
477                 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
478                 );
479         write_image(cropped, "build/test/crop_scale_window_test5.png");
480         check_image("test/data/crop_scale_window_test5.png", "build/test/crop_scale_window_test5.png");
481 }
482
483
484 BOOST_AUTO_TEST_CASE (crop_scale_window_test6)
485 {
486         auto proxy = make_shared<FFmpegImageProxy>(TestPaths::private_data() / "player_seek_test_0.png");
487         auto xyz = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_XYZ12LE, Image::Alignment::PADDED, false);
488         auto cropped = xyz->crop_scale_window(
489                 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
490                 );
491         write_image(cropped, "build/test/crop_scale_window_test6.png");
492         check_image("test/data/crop_scale_window_test6.png", "build/test/crop_scale_window_test6.png");
493 }
494
495
496 /** Test some small crops with an image that shows up errors in registration of the YUV planes (#1872) */
497 BOOST_AUTO_TEST_CASE (crop_scale_window_test7)
498 {
499         using namespace boost::filesystem;
500         for (int left_crop = 0; left_crop < 8; ++left_crop) {
501                 auto proxy = make_shared<FFmpegImageProxy>("test/data/rgb_grey_testcard.png");
502                 auto yuv = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_YUV420P, Image::Alignment::PADDED, false);
503                 int rounded = left_crop - (left_crop % 2);
504                 auto cropped = yuv->crop_scale_window(
505                         Crop(left_crop, 0, 0, 0),
506                         dcp::Size(1998 - rounded, 1080),
507                         dcp::Size(1998 - rounded, 1080),
508                         dcp::YUVToRGB::REC709,
509                         VideoRange::VIDEO,
510                         AV_PIX_FMT_RGB24,
511                         VideoRange::VIDEO,
512                         Image::Alignment::PADDED,
513                         false
514                         );
515                 path file = String::compose("crop_scale_window_test7-%1.png", left_crop);
516                 write_image(cropped, path("build") / "test" / file);
517                 check_image(path("test") / "data" / file, path("build") / "test" / file, 10);
518         }
519 }
520
521
522 BOOST_AUTO_TEST_CASE (crop_scale_window_test8)
523 {
524         using namespace boost::filesystem;
525
526         auto image = make_shared<Image>(AV_PIX_FMT_YUV420P, dcp::Size(800, 600), Image::Alignment::PADDED);
527         memset(image->data()[0], 41, image->stride()[0] * 600);
528         memset(image->data()[1], 240, image->stride()[1] * 300);
529         memset(image->data()[2], 41, image->stride()[2] * 300);
530         auto scaled = image->crop_scale_window(
531                 Crop(), dcp::Size(1435, 1080), dcp::Size(1998, 1080), dcp::YUVToRGB::REC709, VideoRange::FULL, AV_PIX_FMT_YUV420P, VideoRange::FULL, Image::Alignment::PADDED, false
532                 );
533         auto file = "crop_scale_window_test8.png";
534         write_image(scaled->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGB24, Image::Alignment::COMPACT, false), path("build") / "test" / file);
535         check_image(path("test") / "data" / file, path("build") / "test" / file, 10);
536 }
537
538
539 BOOST_AUTO_TEST_CASE (as_png_test)
540 {
541         auto proxy = make_shared<FFmpegImageProxy>("test/data/3d_test/000001.png");
542         auto image_rgb = proxy->image(Image::Alignment::PADDED).image;
543         auto image_bgr = image_rgb->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_BGRA, Image::Alignment::PADDED, false);
544         image_as_png(image_rgb).write ("build/test/as_png_rgb.png");
545         image_as_png(image_bgr).write ("build/test/as_png_bgr.png");
546
547         check_image ("test/data/3d_test/000001.png", "build/test/as_png_rgb.png");
548         check_image ("test/data/3d_test/000001.png", "build/test/as_png_bgr.png");
549 }
550
551
552 BOOST_AUTO_TEST_CASE (as_jpeg_test)
553 {
554         auto proxy = make_shared<FFmpegImageProxy>("test/data/3d_test/000001.png");
555         auto image_rgb = proxy->image(Image::Alignment::PADDED).image;
556         auto image_bgr = image_rgb->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_BGRA, Image::Alignment::PADDED, false);
557         image_as_jpeg(image_rgb, 60).write("build/test/as_jpeg_rgb.jpeg");
558         image_as_jpeg(image_bgr, 60).write("build/test/as_jpeg_bgr.jpeg");
559
560         check_image ("test/data/as_jpeg_rgb.jpeg", "build/test/as_jpeg_rgb.jpeg");
561         check_image ("test/data/as_jpeg_bgr.jpeg", "build/test/as_jpeg_bgr.jpeg");
562 }
563
564
565 /* Very dumb test to fade black to make sure it stays black */
566 static void
567 fade_test_format_black (AVPixelFormat f, string name)
568 {
569         Image yuv (f, dcp::Size(640, 480), Image::Alignment::PADDED);
570         yuv.make_black ();
571         yuv.fade (0);
572         string const filename = "fade_test_black_" + name + ".png";
573         image_as_png(yuv.convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGBA, Image::Alignment::PADDED, false)).write("build/test/" + filename);
574         check_image ("test/data/" + filename, "build/test/" + filename);
575 }
576
577
578 /* Fade red to make sure it stays red */
579 static void
580 fade_test_format_red (AVPixelFormat f, float amount, string name)
581 {
582         auto proxy = make_shared<FFmpegImageProxy>("test/data/flat_red.png");
583         auto red = proxy->image(Image::Alignment::PADDED).image->convert_pixel_format(dcp::YUVToRGB::REC709, f, Image::Alignment::PADDED, false);
584         red->fade (amount);
585         string const filename = "fade_test_red_" + name + ".png";
586         image_as_png(red->convert_pixel_format(dcp::YUVToRGB::REC709, AV_PIX_FMT_RGBA, Image::Alignment::PADDED, false)).write("build/test/" + filename);
587         check_image ("test/data/" + filename, "build/test/" + filename);
588 }
589
590
591 BOOST_AUTO_TEST_CASE (fade_test)
592 {
593         fade_test_format_black (AV_PIX_FMT_YUV420P,   "yuv420p");
594         fade_test_format_black (AV_PIX_FMT_YUV422P10, "yuv422p10");
595         fade_test_format_black (AV_PIX_FMT_RGB24,     "rgb24");
596         fade_test_format_black (AV_PIX_FMT_XYZ12LE,   "xyz12le");
597         fade_test_format_black (AV_PIX_FMT_RGB48LE,   "rgb48le");
598
599         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0,   "yuv420p_0");
600         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0.5, "yuv420p_50");
601         fade_test_format_red   (AV_PIX_FMT_YUV420P,   1,   "yuv420p_100");
602         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0,   "yuv422p10_0");
603         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0.5, "yuv422p10_50");
604         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 1,   "yuv422p10_100");
605         fade_test_format_red   (AV_PIX_FMT_RGB24,     0,   "rgb24_0");
606         fade_test_format_red   (AV_PIX_FMT_RGB24,     0.5, "rgb24_50");
607         fade_test_format_red   (AV_PIX_FMT_RGB24,     1,   "rgb24_100");
608         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0,   "xyz12le_0");
609         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0.5, "xyz12le_50");
610         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   1,   "xyz12le_100");
611         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0,   "rgb48le_0");
612         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0.5, "rgb48le_50");
613         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   1,   "rgb48le_100");
614 }
615
616
617 BOOST_AUTO_TEST_CASE (make_black_test)
618 {
619         dcp::Size in_size (512, 512);
620         dcp::Size out_size (1024, 1024);
621
622         list<AVPixelFormat> pix_fmts = {
623                 AV_PIX_FMT_RGB24, // 2
624                 AV_PIX_FMT_ARGB,
625                 AV_PIX_FMT_RGBA,
626                 AV_PIX_FMT_ABGR,
627                 AV_PIX_FMT_BGRA,
628                 AV_PIX_FMT_YUV420P, // 0
629                 AV_PIX_FMT_YUV411P,
630                 AV_PIX_FMT_YUV422P10LE,
631                 AV_PIX_FMT_YUV422P16LE,
632                 AV_PIX_FMT_YUV444P9LE,
633                 AV_PIX_FMT_YUV444P9BE,
634                 AV_PIX_FMT_YUV444P10LE,
635                 AV_PIX_FMT_YUV444P10BE,
636                 AV_PIX_FMT_UYVY422,
637                 AV_PIX_FMT_YUVJ420P,
638                 AV_PIX_FMT_YUVJ422P,
639                 AV_PIX_FMT_YUVJ444P,
640                 AV_PIX_FMT_YUVA420P9BE,
641                 AV_PIX_FMT_YUVA422P9BE,
642                 AV_PIX_FMT_YUVA444P9BE,
643                 AV_PIX_FMT_YUVA420P9LE,
644                 AV_PIX_FMT_YUVA422P9LE,
645                 AV_PIX_FMT_YUVA444P9LE,
646                 AV_PIX_FMT_YUVA420P10BE,
647                 AV_PIX_FMT_YUVA422P10BE,
648                 AV_PIX_FMT_YUVA444P10BE,
649                 AV_PIX_FMT_YUVA420P10LE,
650                 AV_PIX_FMT_YUVA422P10LE,
651                 AV_PIX_FMT_YUVA444P10LE,
652                 AV_PIX_FMT_YUVA420P16BE,
653                 AV_PIX_FMT_YUVA422P16BE,
654                 AV_PIX_FMT_YUVA444P16BE,
655                 AV_PIX_FMT_YUVA420P16LE,
656                 AV_PIX_FMT_YUVA422P16LE,
657                 AV_PIX_FMT_YUVA444P16LE,
658                 AV_PIX_FMT_RGB555LE, // 46
659         };
660
661         for (auto i: pix_fmts) {
662                 auto foo = make_shared<Image>(i, in_size, Image::Alignment::PADDED);
663                 foo->make_black ();
664                 auto bar = foo->scale (out_size, dcp::YUVToRGB::REC601, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
665
666                 uint8_t* p = bar->data()[0];
667                 for (int y = 0; y < bar->size().height; ++y) {
668                         uint8_t* q = p;
669                         for (int x = 0; x < bar->line_size()[0]; ++x) {
670                                 if (*q != 0) {
671                                         std::cerr << "x=" << x << ", (x%3)=" << (x%3) << "\n";
672                                 }
673                                 BOOST_CHECK_EQUAL (*q++, 0);
674                         }
675                         p += bar->stride()[0];
676                 }
677         }
678 }
679
680
681 BOOST_AUTO_TEST_CASE (make_part_black_test)
682 {
683         auto proxy = make_shared<FFmpegImageProxy>("test/data/flat_red.png");
684         auto original = proxy->image(Image::Alignment::PADDED).image;
685
686         list<AVPixelFormat> pix_fmts = {
687                 AV_PIX_FMT_RGB24,
688                 AV_PIX_FMT_ARGB,
689                 AV_PIX_FMT_RGBA,
690                 AV_PIX_FMT_ABGR,
691                 AV_PIX_FMT_BGRA,
692                 AV_PIX_FMT_YUV420P,
693                 AV_PIX_FMT_YUV422P10LE,
694                 AV_PIX_FMT_YUV444P10LE
695         };
696
697         list<std::pair<int, int>> positions = {
698                 { 0, 256 },
699                 { 128, 64 },
700         };
701
702         for (auto i: pix_fmts) {
703                 for (auto j: positions) {
704                         auto foo = original->convert_pixel_format(dcp::YUVToRGB::REC601, i, Image::Alignment::PADDED, false);
705                         foo->make_part_black (j.first, j.second);
706                         auto bar = foo->convert_pixel_format (dcp::YUVToRGB::REC601, AV_PIX_FMT_RGB24, Image::Alignment::PADDED, false);
707
708                         auto p = bar->data()[0];
709                         for (int y = 0; y < bar->size().height; ++y) {
710                                 auto q = p;
711                                 for (int x = 0; x < bar->size().width; ++x) {
712                                         int r = *q++;
713                                         int g = *q++;
714                                         int b = *q++;
715                                         if (x >= j.first && x < (j.first + j.second)) {
716                                                 BOOST_CHECK_MESSAGE (
717                                                         r < 3, "red=" << static_cast<int>(r) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
718                                                         );
719                                         } else {
720                                                 BOOST_CHECK_MESSAGE (
721                                                         r >= 252, "red=" << static_cast<int>(r) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
722                                                         );
723
724                                         }
725                                         BOOST_CHECK_MESSAGE (
726                                                 g == 0, "green=" << static_cast<int>(g) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
727                                                 );
728                                         BOOST_CHECK_MESSAGE (
729                                                 b == 0, "blue=" << static_cast<int>(b) << " at (" << x << "," << y << ") format " << i << " from " << j.first << " width " << j.second
730                                                 );
731                                 }
732                                 p += bar->stride()[0];
733                         }
734                 }
735         }
736 }
737
738
739 /** Make sure the image isn't corrupted if it is cropped too much.  This can happen when a
740  *  filler 128x128 black frame is emitted from the FFmpegDecoder and the overall crop in either direction
741  *  is greater than 128 pixels.
742  */
743 BOOST_AUTO_TEST_CASE (over_crop_test)
744 {
745         auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size(128, 128), Image::Alignment::PADDED);
746         image->make_black ();
747         auto scaled = image->crop_scale_window (
748                 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
749                 );
750         string const filename = "over_crop_test.png";
751         write_image (scaled, "build/test/" + filename);
752         check_image ("test/data/" + filename, "build/test/" + filename);
753 }