8378207cfd42b0971de89cb60ab5a78bff13e524
[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
38 BOOST_AUTO_TEST_CASE (aligned_image_test)
39 {
40         Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), true);
41         BOOST_CHECK_EQUAL (s->planes(), 1);
42         /* 160 is 150 aligned to the nearest 32 bytes */
43         BOOST_CHECK_EQUAL (s->stride()[0], 160);
44         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
45         BOOST_CHECK (s->data()[0]);
46         BOOST_CHECK (!s->data()[1]);
47         BOOST_CHECK (!s->data()[2]);
48         BOOST_CHECK (!s->data()[3]);
49
50         /* copy constructor */
51         Image* t = new Image (*s);
52         BOOST_CHECK_EQUAL (t->planes(), 1);
53         BOOST_CHECK_EQUAL (t->stride()[0], 160);
54         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
55         BOOST_CHECK (t->data()[0]);
56         BOOST_CHECK (!t->data()[1]);
57         BOOST_CHECK (!t->data()[2]);
58         BOOST_CHECK (!t->data()[3]);
59         BOOST_CHECK (t->data() != s->data());
60         BOOST_CHECK (t->data()[0] != s->data()[0]);
61         BOOST_CHECK (t->line_size() != s->line_size());
62         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
63         BOOST_CHECK (t->stride() != s->stride());
64         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
65
66         /* assignment operator */
67         Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), false);
68         *u = *s;
69         BOOST_CHECK_EQUAL (u->planes(), 1);
70         BOOST_CHECK_EQUAL (u->stride()[0], 160);
71         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
72         BOOST_CHECK (u->data()[0]);
73         BOOST_CHECK (!u->data()[1]);
74         BOOST_CHECK (!u->data()[2]);
75         BOOST_CHECK (!u->data()[3]);
76         BOOST_CHECK (u->data() != s->data());
77         BOOST_CHECK (u->data()[0] != s->data()[0]);
78         BOOST_CHECK (u->line_size() != s->line_size());
79         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
80         BOOST_CHECK (u->stride() != s->stride());
81         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
82
83         delete s;
84         delete t;
85         delete u;
86 }
87
88 BOOST_AUTO_TEST_CASE (compact_image_test)
89 {
90         Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), false);
91         BOOST_CHECK_EQUAL (s->planes(), 1);
92         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
93         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
94         BOOST_CHECK (s->data()[0]);
95         BOOST_CHECK (!s->data()[1]);
96         BOOST_CHECK (!s->data()[2]);
97         BOOST_CHECK (!s->data()[3]);
98
99         /* copy constructor */
100         Image* t = new Image (*s);
101         BOOST_CHECK_EQUAL (t->planes(), 1);
102         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
103         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
104         BOOST_CHECK (t->data()[0]);
105         BOOST_CHECK (!t->data()[1]);
106         BOOST_CHECK (!t->data()[2]);
107         BOOST_CHECK (!t->data()[3]);
108         BOOST_CHECK (t->data() != s->data());
109         BOOST_CHECK (t->data()[0] != s->data()[0]);
110         BOOST_CHECK (t->line_size() != s->line_size());
111         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
112         BOOST_CHECK (t->stride() != s->stride());
113         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
114
115         /* assignment operator */
116         Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), true);
117         *u = *s;
118         BOOST_CHECK_EQUAL (u->planes(), 1);
119         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
120         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
121         BOOST_CHECK (u->data()[0]);
122         BOOST_CHECK (!u->data()[1]);
123         BOOST_CHECK (!u->data()[2]);
124         BOOST_CHECK (!u->data()[3]);
125         BOOST_CHECK (u->data() != s->data());
126         BOOST_CHECK (u->data()[0] != s->data()[0]);
127         BOOST_CHECK (u->line_size() != s->line_size());
128         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
129         BOOST_CHECK (u->stride() != s->stride());
130         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
131
132         delete s;
133         delete t;
134         delete u;
135 }
136
137 void
138 alpha_blend_test_one (AVPixelFormat format, string suffix)
139 {
140         shared_ptr<FFmpegImageProxy> proxy (new FFmpegImageProxy (private_data / "prophet_frame.tiff"));
141         shared_ptr<Image> raw = proxy->image().first;
142         shared_ptr<Image> background = raw->convert_pixel_format (dcp::YUV_TO_RGB_REC709, format, true, false);
143
144         shared_ptr<Image> overlay (new Image (AV_PIX_FMT_BGRA, dcp::Size(431, 891), true));
145         overlay->make_transparent ();
146
147         for (int y = 0; y < 128; ++y) {
148                 uint8_t* p = overlay->data()[0] + y * overlay->stride()[0];
149                 for (int x = 0; x < 128; ++x) {
150                         p[x * 4 + 2] = 255;
151                         p[x * 4 + 3] = 255;
152                 }
153         }
154
155         for (int y = 128; y < 256; ++y) {
156                 uint8_t* p = overlay->data()[0] + y * overlay->stride()[0];
157                 for (int x = 0; x < 128; ++x) {
158                         p[x * 4 + 1] = 255;
159                         p[x * 4 + 3] = 255;
160                 }
161         }
162
163         for (int y = 256; y < 384; ++y) {
164                 uint8_t* p = overlay->data()[0] + y * overlay->stride()[0];
165                 for (int x = 0; x < 128; ++x) {
166                         p[x * 4] = 255;
167                         p[x * 4 + 3] = 255;
168                 }
169         }
170
171         background->alpha_blend (overlay, Position<int> (13, 17));
172
173         shared_ptr<Image> save = background->convert_pixel_format (dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
174
175         write_image (save, "build/test/image_test_" + suffix + ".png", "RGB");
176         check_image ("build/test/image_test_" + suffix + ".png", private_data / ("image_test_" + suffix + ".png"));
177 }
178
179 /** Test Image::alpha_blend */
180 BOOST_AUTO_TEST_CASE (alpha_blend_test)
181 {
182         alpha_blend_test_one (AV_PIX_FMT_RGB24, "rgb24");
183         alpha_blend_test_one (AV_PIX_FMT_BGRA, "bgra");
184         alpha_blend_test_one (AV_PIX_FMT_RGBA, "rgba");
185         alpha_blend_test_one (AV_PIX_FMT_RGB48LE, "rgb48le");
186         alpha_blend_test_one (AV_PIX_FMT_YUV420P, "yuv420p");
187         alpha_blend_test_one (AV_PIX_FMT_YUV420P10, "yuv420p10");
188         alpha_blend_test_one (AV_PIX_FMT_YUV422P10LE, "yuv422p10le");
189 }
190
191 /** Test merge (list<PositionImage>) with a single image */
192 BOOST_AUTO_TEST_CASE (merge_test1)
193 {
194         int const stride = 48 * 4;
195
196         shared_ptr<Image> A (new Image (AV_PIX_FMT_BGRA, dcp::Size (48, 48), false));
197         A->make_transparent ();
198         uint8_t* a = A->data()[0];
199
200         for (int y = 0; y < 48; ++y) {
201                 uint8_t* p = a + y * stride;
202                 for (int x = 0; x < 16; ++x) {
203                         /* blue */
204                         p[x * 4] = 255;
205                         /* opaque */
206                         p[x * 4 + 3] = 255;
207                 }
208         }
209
210         list<PositionImage> all;
211         all.push_back (PositionImage (A, Position<int> (0, 0)));
212         PositionImage merged = merge (all);
213
214         BOOST_CHECK (merged.position == Position<int> (0, 0));
215         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
216 }
217
218 /** Test merge (list<PositionImage>) with two images */
219 BOOST_AUTO_TEST_CASE (merge_test2)
220 {
221         shared_ptr<Image> A (new Image (AV_PIX_FMT_BGRA, dcp::Size (48, 1), false));
222         A->make_transparent ();
223         uint8_t* a = A->data()[0];
224         for (int x = 0; x < 16; ++x) {
225                 /* blue */
226                 a[x * 4] = 255;
227                 /* opaque */
228                 a[x * 4 + 3] = 255;
229         }
230
231         shared_ptr<Image> B (new Image (AV_PIX_FMT_BGRA, dcp::Size (48, 1), false));
232         B->make_transparent ();
233         uint8_t* b = B->data()[0];
234         for (int x = 0; x < 16; ++x) {
235                 /* red */
236                 b[(x + 32) * 4 + 2] = 255;
237                 /* opaque */
238                 b[(x + 32) * 4 + 3] = 255;
239         }
240
241         list<PositionImage> all;
242         all.push_back (PositionImage (A, Position<int> (0, 0)));
243         all.push_back (PositionImage (B, Position<int> (0, 0)));
244         PositionImage merged = merge (all);
245
246         BOOST_CHECK (merged.position == Position<int> (0, 0));
247
248         uint8_t* m = merged.image->data()[0];
249
250         for (int x = 0; x < 16; ++x) {
251                 BOOST_CHECK_EQUAL (m[x * 4], 255);
252                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
253                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
254                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
255                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
256         }
257 }
258
259 /** Test Image::crop_scale_window with YUV420P and some windowing */
260 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
261 {
262         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png"));
263         shared_ptr<Image> raw = proxy->image().first;
264         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);
265         shared_ptr<Image> save = out->scale(dcp::Size(1998, 1080), dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGB24, false, false);
266         write_image(save, "build/test/crop_scale_window_test.png", "RGB");
267         check_image("test/data/crop_scale_window_test.png", "build/test/crop_scale_window_test.png");
268 }
269
270 /** Special cases of Image::crop_scale_window which triggered some valgrind warnings */
271 BOOST_AUTO_TEST_CASE (crop_scale_window_test2)
272 {
273         /* This 2048 does the same as J2KImageProxy does when it makes an image */
274         shared_ptr<Image> image (new Image(AV_PIX_FMT_XYZ12LE, dcp::Size(2048, 858), true, 2048));
275         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);
276         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);
277 }
278
279 BOOST_AUTO_TEST_CASE (as_png_test)
280 {
281         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/3d_test/000001.png"));
282         shared_ptr<Image> image_rgb = proxy->image().first;
283         shared_ptr<Image> image_bgr = image_rgb->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_BGRA, true, false);
284         image_rgb->as_png().write ("build/test/as_png_rgb.png");
285         image_bgr->as_png().write ("build/test/as_png_bgr.png");
286
287         check_image ("test/data/3d_test/000001.png", "build/test/as_png_rgb.png");
288         check_image ("test/data/3d_test/000001.png", "build/test/as_png_bgr.png");
289 }
290
291 /* Very dumb test to fade black to make sure it stays black */
292 static void
293 fade_test_format_black (AVPixelFormat f, string name)
294 {
295         Image yuv (f, dcp::Size(640, 480), true);
296         yuv.make_black ();
297         yuv.fade (0);
298         string const filename = "fade_test_black_" + name + ".png";
299         yuv.convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGBA, true, false)->as_png().write("build/test/" + filename);
300         check_image ("test/data/" + filename, "build/test/" + filename);
301 }
302
303 /* Fade red to make sure it stays red */
304 static void
305 fade_test_format_red (AVPixelFormat f, float amount, string name)
306 {
307         shared_ptr<FFmpegImageProxy> proxy(new FFmpegImageProxy("test/data/flat_red.png"));
308         shared_ptr<Image> red = proxy->image().first->convert_pixel_format(dcp::YUV_TO_RGB_REC709, f, true, false);
309         red->fade (amount);
310         string const filename = "fade_test_red_" + name + ".png";
311         red->convert_pixel_format(dcp::YUV_TO_RGB_REC709, AV_PIX_FMT_RGBA, true, false)->as_png().write("build/test/" + filename);
312         check_image ("test/data/" + filename, "build/test/" + filename);
313 }
314
315 BOOST_AUTO_TEST_CASE (fade_test)
316 {
317         fade_test_format_black (AV_PIX_FMT_YUV420P,   "yuv420p");
318         fade_test_format_black (AV_PIX_FMT_YUV422P10, "yuv422p10");
319         fade_test_format_black (AV_PIX_FMT_RGB24,     "rgb24");
320         fade_test_format_black (AV_PIX_FMT_XYZ12LE,   "xyz12le");
321         fade_test_format_black (AV_PIX_FMT_RGB48LE,   "rgb48le");
322
323         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0,   "yuv420p_0");
324         fade_test_format_red   (AV_PIX_FMT_YUV420P,   0.5, "yuv420p_50");
325         fade_test_format_red   (AV_PIX_FMT_YUV420P,   1,   "yuv420p_100");
326         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0,   "yuv422p10_0");
327         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 0.5, "yuv422p10_50");
328         fade_test_format_red   (AV_PIX_FMT_YUV422P10, 1,   "yuv422p10_100");
329         fade_test_format_red   (AV_PIX_FMT_RGB24,     0,   "rgb24_0");
330         fade_test_format_red   (AV_PIX_FMT_RGB24,     0.5, "rgb24_50");
331         fade_test_format_red   (AV_PIX_FMT_RGB24,     1,   "rgb24_100");
332         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0,   "xyz12le_0");
333         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   0.5, "xyz12le_50");
334         fade_test_format_red   (AV_PIX_FMT_XYZ12LE,   1,   "xyz12le_100");
335         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0,   "rgb48le_0");
336         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   0.5, "rgb48le_50");
337         fade_test_format_red   (AV_PIX_FMT_RGB48LE,   1,   "rgb48le_100");
338 }