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