Merge master.
[dcpomatic.git] / test / image_test.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/test/unit_test.hpp>
21 #include <Magick++.h>
22 #include "lib/image.h"
23 #include "lib/scaler.h"
24
25 using std::string;
26 using std::cout;
27 using boost::shared_ptr;
28
29 BOOST_AUTO_TEST_CASE (aligned_image_test)
30 {
31         Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), true);
32         BOOST_CHECK_EQUAL (s->components(), 1);
33         /* 160 is 150 aligned to the nearest 32 bytes */
34         BOOST_CHECK_EQUAL (s->stride()[0], 160);
35         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
36         BOOST_CHECK (s->data()[0]);
37         BOOST_CHECK (!s->data()[1]);
38         BOOST_CHECK (!s->data()[2]);
39         BOOST_CHECK (!s->data()[3]);
40
41         /* copy constructor */
42         Image* t = new Image (*s);
43         BOOST_CHECK_EQUAL (t->components(), 1);
44         BOOST_CHECK_EQUAL (t->stride()[0], 160);
45         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
46         BOOST_CHECK (t->data()[0]);
47         BOOST_CHECK (!t->data()[1]);
48         BOOST_CHECK (!t->data()[2]);
49         BOOST_CHECK (!t->data()[3]);
50         BOOST_CHECK (t->data() != s->data());
51         BOOST_CHECK (t->data()[0] != s->data()[0]);
52         BOOST_CHECK (t->line_size() != s->line_size());
53         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
54         BOOST_CHECK (t->stride() != s->stride());
55         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
56
57         /* assignment operator */
58         Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), false);
59         *u = *s;
60         BOOST_CHECK_EQUAL (u->components(), 1);
61         BOOST_CHECK_EQUAL (u->stride()[0], 160);
62         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
63         BOOST_CHECK (u->data()[0]);
64         BOOST_CHECK (!u->data()[1]);
65         BOOST_CHECK (!u->data()[2]);
66         BOOST_CHECK (!u->data()[3]);
67         BOOST_CHECK (u->data() != s->data());
68         BOOST_CHECK (u->data()[0] != s->data()[0]);
69         BOOST_CHECK (u->line_size() != s->line_size());
70         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
71         BOOST_CHECK (u->stride() != s->stride());
72         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
73
74         delete s;
75         delete t;
76         delete u;
77 }
78
79 BOOST_AUTO_TEST_CASE (compact_image_test)
80 {
81         Image* s = new Image (PIX_FMT_RGB24, dcp::Size (50, 50), false);
82         BOOST_CHECK_EQUAL (s->components(), 1);
83         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
84         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
85         BOOST_CHECK (s->data()[0]);
86         BOOST_CHECK (!s->data()[1]);
87         BOOST_CHECK (!s->data()[2]);
88         BOOST_CHECK (!s->data()[3]);
89
90         /* copy constructor */
91         Image* t = new Image (*s);
92         BOOST_CHECK_EQUAL (t->components(), 1);
93         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
94         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
95         BOOST_CHECK (t->data()[0]);
96         BOOST_CHECK (!t->data()[1]);
97         BOOST_CHECK (!t->data()[2]);
98         BOOST_CHECK (!t->data()[3]);
99         BOOST_CHECK (t->data() != s->data());
100         BOOST_CHECK (t->data()[0] != s->data()[0]);
101         BOOST_CHECK (t->line_size() != s->line_size());
102         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
103         BOOST_CHECK (t->stride() != s->stride());
104         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
105
106         /* assignment operator */
107         Image* u = new Image (PIX_FMT_YUV422P, dcp::Size (150, 150), true);
108         *u = *s;
109         BOOST_CHECK_EQUAL (u->components(), 1);
110         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
111         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
112         BOOST_CHECK (u->data()[0]);
113         BOOST_CHECK (!u->data()[1]);
114         BOOST_CHECK (!u->data()[2]);
115         BOOST_CHECK (!u->data()[3]);
116         BOOST_CHECK (u->data() != s->data());
117         BOOST_CHECK (u->data()[0] != s->data()[0]);
118         BOOST_CHECK (u->line_size() != s->line_size());
119         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
120         BOOST_CHECK (u->stride() != s->stride());
121         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
122
123         delete s;
124         delete t;
125         delete u;
126 }
127
128 BOOST_AUTO_TEST_CASE (crop_image_test)
129 {
130         /* This was to check out a bug with valgrind, and is probably not very useful */
131         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (16, 16), true));
132         image->make_black ();
133         Crop crop;
134         crop.top = 3;
135         image->crop (crop, false);
136 }
137
138 /* Test cropping of a YUV 4:2:0 image by 1 pixel, which used to fail because
139    the U/V copying was not rounded up to the next sample.
140 */
141 BOOST_AUTO_TEST_CASE (crop_image_test2)
142 {
143         /* Here's a 1998 x 1080 image which is black */
144         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, dcp::Size (1998, 1080), true));
145         image->make_black ();
146
147         /* Crop it by 1 pixel */
148         Crop crop;
149         crop.left = 1;
150         image = image->crop (crop, true);
151
152         /* Convert it back to RGB to make comparison to black easier */
153         image = image->scale (image->size(), Scaler::from_id ("bicubic"), PIX_FMT_RGB24, true);
154
155         /* Check that its still black after the crop */
156         uint8_t* p = image->data()[0];
157         for (int y = 0; y < image->size().height; ++y) {
158                 uint8_t* q = p;
159                 for (int x = 0; x < image->size().width; ++x) {
160                         BOOST_CHECK_EQUAL (*q++, 0);
161                         BOOST_CHECK_EQUAL (*q++, 0);
162                         BOOST_CHECK_EQUAL (*q++, 0);
163                 }
164                 p += image->stride()[0];
165         }
166 }
167
168 static
169 boost::shared_ptr<Image>
170 read_file (string file)
171 {
172         Magick::Image magick_image (file.c_str ());
173         dcp::Size size (magick_image.columns(), magick_image.rows());
174
175         boost::shared_ptr<Image> image (new Image (PIX_FMT_RGB24, size, true));
176
177         using namespace MagickCore;
178         
179         uint8_t* p = image->data()[0];
180         for (int y = 0; y < size.height; ++y) {
181                 uint8_t* q = p;
182                 for (int x = 0; x < size.width; ++x) {
183                         Magick::Color c = magick_image.pixelColor (x, y);
184                         *q++ = c.redQuantum() * 255 / QuantumRange;
185                         *q++ = c.greenQuantum() * 255 / QuantumRange;
186                         *q++ = c.blueQuantum() * 255 / QuantumRange;
187                 }
188                 p += image->stride()[0];
189         }
190
191         return image;
192 }
193
194 static
195 void
196 write_file (shared_ptr<Image> image, string file)
197 {
198         using namespace MagickCore;
199         
200         Magick::Image magick_image (Magick::Geometry (image->size().width, image->size().height), Magick::Color (0, 0, 0));
201         uint8_t*p = image->data()[0];
202         for (int y = 0; y < image->size().height; ++y) {
203                 uint8_t* q = p;
204                 for (int x = 0; x < image->size().width; ++x) {
205                         Magick::Color c (q[0] * QuantumRange / 256, q[1] * QuantumRange / 256, q[2] * QuantumRange / 256);
206                         magick_image.pixelColor (x, y, c);
207                         q += 3;
208                 }
209                 p += image->stride()[0];
210         }
211         
212         magick_image.write (file.c_str ());
213 }
214
215 static
216 void
217 crop_scale_window_single (AVPixelFormat in_format, dcp::Size in_size, Crop crop, dcp::Size inter_size, dcp::Size out_size)
218 {
219         /* Set up our test image */
220         shared_ptr<Image> test (new Image (in_format, in_size, true));
221         uint8_t n = 0;
222         for (int c = 0; c < test->components(); ++c) {
223                 uint8_t* p = test->data()[c];
224                 for (int y = 0; y < test->lines(c); ++y) {
225                         for (int x = 0; x < test->stride()[c]; ++x) {
226                                 *p++ = n++;
227                         }
228                 }
229         }
230                                 
231         /* Convert using separate methods */
232         boost::shared_ptr<Image> sep = test->crop (crop, true);
233         sep = sep->scale (inter_size, Scaler::from_id ("bicubic"), PIX_FMT_RGB24, true);
234         boost::shared_ptr<Image> sep_container (new Image (PIX_FMT_RGB24, out_size, true));
235         sep_container->make_black ();
236         sep_container->copy (sep, Position<int> ((out_size.width - inter_size.width) / 2, (out_size.height - inter_size.height) / 2));
237
238         /* Convert using the all-in-one method */
239         shared_ptr<Image> all = test->crop_scale_window (crop, inter_size, out_size, Scaler::from_id ("bicubic"), PIX_FMT_RGB24, true);
240
241         /* Compare */
242         BOOST_CHECK_EQUAL (sep_container->size().width, all->size().width);
243         BOOST_CHECK_EQUAL (sep_container->size().height, all->size().height);
244
245         /* Assuming RGB on these */
246         BOOST_CHECK_EQUAL (sep_container->components(), 1);
247         BOOST_CHECK_EQUAL (all->components(), 1);
248
249         uint8_t* p = sep_container->data()[0];
250         uint8_t* q = all->data()[0];
251         for (int y = 0; y < all->size().height; ++y) {
252                 uint8_t* pp = p;
253                 uint8_t* qq = q;
254                 for (int x = 0; x < all->size().width * 3; ++x) {
255                         BOOST_CHECK_EQUAL (*pp++, *qq++);
256                 }
257                 p += sep_container->stride()[0];
258                 q += all->stride()[0];
259         }
260 }
261
262 /** Test Image::crop_scale_window against separate calls to crop/scale/copy */
263 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
264 {
265         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (), dcp::Size (640, 480), dcp::Size (640, 480));
266         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (2, 4, 6, 8), dcp::Size (640, 480), dcp::Size (640, 480));
267         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (2, 4, 6, 8), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
268         crop_scale_window_single (AV_PIX_FMT_YUV422P, dcp::Size (640, 480), Crop (1, 4, 6, 8), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
269         crop_scale_window_single (AV_PIX_FMT_YUV420P, dcp::Size (640, 480), Crop (16, 16, 0, 0), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
270         crop_scale_window_single (AV_PIX_FMT_YUV420P, dcp::Size (640, 480), Crop (16, 3, 3, 0), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
271         crop_scale_window_single (AV_PIX_FMT_RGB24, dcp::Size (1000, 800), Crop (0, 0, 0, 0), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
272         crop_scale_window_single (AV_PIX_FMT_RGB24, dcp::Size (1000, 800), Crop (55, 0, 1, 9), dcp::Size (1920, 1080), dcp::Size (1998, 1080));
273 }