2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <boost/test/unit_test.hpp>
22 #include "lib/image.h"
23 #include "lib/scaler.h"
27 using boost::shared_ptr;
29 BOOST_AUTO_TEST_CASE (aligned_image_test)
31 Image* s = new Image (PIX_FMT_RGB24, libdcp::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]);
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 (t->line_size()[0] == s->line_size()[0]);
54 BOOST_CHECK (t->stride() != s->stride());
55 BOOST_CHECK (t->stride()[0] == s->stride()[0]);
57 /* assignment operator */
58 Image* u = new Image (PIX_FMT_YUV422P, libdcp::Size (150, 150), false);
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 (u->line_size()[0] == s->line_size()[0]);
71 BOOST_CHECK (u->stride() != s->stride());
72 BOOST_CHECK (u->stride()[0] == s->stride()[0]);
79 BOOST_AUTO_TEST_CASE (compact_image_test)
81 Image* s = new Image (PIX_FMT_RGB24, libdcp::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]);
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 (t->line_size()[0] == s->line_size()[0]);
103 BOOST_CHECK (t->stride() != s->stride());
104 BOOST_CHECK (t->stride()[0] == s->stride()[0]);
106 /* assignment operator */
107 Image* u = new Image (PIX_FMT_YUV422P, libdcp::Size (150, 150), true);
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 (u->line_size()[0] == s->line_size()[0]);
120 BOOST_CHECK (u->stride() != s->stride());
121 BOOST_CHECK (u->stride()[0] == s->stride()[0]);
128 BOOST_AUTO_TEST_CASE (crop_image_test)
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, libdcp::Size (16, 16), true));
132 image->make_black ();
135 image->crop (crop, false);
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.
141 BOOST_AUTO_TEST_CASE (crop_image_test2)
143 /* Here's a 1998 x 1080 image which is black */
144 shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, libdcp::Size (1998, 1080), true));
145 image->make_black ();
147 /* Crop it by 1 pixel */
150 image = image->crop (crop, true);
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);
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) {
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);
164 p += image->stride()[0];
169 boost::shared_ptr<Image>
170 read_file (string file)
172 Magick::Image magick_image (file.c_str ());
173 libdcp::Size size (magick_image.columns(), magick_image.rows());
175 boost::shared_ptr<Image> image (new Image (PIX_FMT_RGB24, size, true));
177 using namespace MagickCore;
179 uint8_t* p = image->data()[0];
180 for (int y = 0; y < size.height; ++y) {
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;
188 p += image->stride()[0];
196 write_file (shared_ptr<Image> image, string file)
198 using namespace MagickCore;
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) {
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);
209 p += image->stride()[0];
212 magick_image.write (file.c_str ());
217 crop_scale_window_single (AVPixelFormat in_format, libdcp::Size in_size, Crop crop, libdcp::Size inter_size, libdcp::Size out_size)
219 /* Set up our test image */
220 shared_ptr<Image> test (new Image (in_format, in_size, true));
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) {
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));
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);
242 BOOST_CHECK_EQUAL (sep_container->size().width, all->size().width);
243 BOOST_CHECK_EQUAL (sep_container->size().height, all->size().height);
245 /* Assuming RGB on these */
246 BOOST_CHECK_EQUAL (sep_container->components(), 1);
247 BOOST_CHECK_EQUAL (all->components(), 1);
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) {
254 for (int x = 0; x < all->size().width * 3; ++x) {
255 BOOST_CHECK_EQUAL (*pp++, *qq++);
257 p += sep_container->stride()[0];
258 q += all->stride()[0];
262 /** Test Image::crop_scale_window against separate calls to crop/scale/copy */
263 BOOST_AUTO_TEST_CASE (crop_scale_window_test)
265 crop_scale_window_single (AV_PIX_FMT_YUV422P, libdcp::Size (640, 480), Crop (), libdcp::Size (640, 480), libdcp::Size (640, 480));
266 crop_scale_window_single (AV_PIX_FMT_YUV422P, libdcp::Size (640, 480), Crop (2, 4, 6, 8), libdcp::Size (640, 480), libdcp::Size (640, 480));
267 crop_scale_window_single (AV_PIX_FMT_YUV422P, libdcp::Size (640, 480), Crop (2, 4, 6, 8), libdcp::Size (1920, 1080), libdcp::Size (1998, 1080));
268 crop_scale_window_single (AV_PIX_FMT_YUV422P, libdcp::Size (640, 480), Crop (1, 4, 6, 8), libdcp::Size (1920, 1080), libdcp::Size (1998, 1080));
269 crop_scale_window_single (AV_PIX_FMT_YUV420P, libdcp::Size (640, 480), Crop (16, 16, 0, 0), libdcp::Size (1920, 1080), libdcp::Size (1998, 1080));
270 crop_scale_window_single (AV_PIX_FMT_YUV420P, libdcp::Size (640, 480), Crop (16, 3, 3, 0), libdcp::Size (1920, 1080), libdcp::Size (1998, 1080));
271 crop_scale_window_single (AV_PIX_FMT_RGB24, libdcp::Size (1000, 800), Crop (0, 0, 0, 0), libdcp::Size (1920, 1080), libdcp::Size (1998, 1080));
272 crop_scale_window_single (AV_PIX_FMT_RGB24, libdcp::Size (1000, 800), Crop (55, 0, 1, 9), libdcp::Size (1920, 1080), libdcp::Size (1998, 1080));