d002630356d6b402fe2fe1707e88f9c507594219
[dcpomatic.git] / test / image_test.cc
1 /*
2     Copyright (C) 2012-2014 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 Tests of the Image class.
23  *
24  *  @see test/make_black_test.cc, test/pixel_formats_test.cc
25  */
26
27 #include <boost/test/unit_test.hpp>
28 #include <Magick++.h>
29 #include "lib/image.h"
30 #include <iostream>
31
32 using std::string;
33 using std::list;
34 using std::cout;
35 using boost::shared_ptr;
36
37 BOOST_AUTO_TEST_CASE (aligned_image_test)
38 {
39         Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), true);
40         BOOST_CHECK_EQUAL (s->planes(), 1);
41         /* 160 is 150 aligned to the nearest 32 bytes */
42         BOOST_CHECK_EQUAL (s->stride()[0], 160);
43         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
44         BOOST_CHECK (s->data()[0]);
45         BOOST_CHECK (!s->data()[1]);
46         BOOST_CHECK (!s->data()[2]);
47         BOOST_CHECK (!s->data()[3]);
48
49         /* copy constructor */
50         Image* t = new Image (*s);
51         BOOST_CHECK_EQUAL (t->planes(), 1);
52         BOOST_CHECK_EQUAL (t->stride()[0], 160);
53         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
54         BOOST_CHECK (t->data()[0]);
55         BOOST_CHECK (!t->data()[1]);
56         BOOST_CHECK (!t->data()[2]);
57         BOOST_CHECK (!t->data()[3]);
58         BOOST_CHECK (t->data() != s->data());
59         BOOST_CHECK (t->data()[0] != s->data()[0]);
60         BOOST_CHECK (t->line_size() != s->line_size());
61         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
62         BOOST_CHECK (t->stride() != s->stride());
63         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
64
65         /* assignment operator */
66         Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), false);
67         *u = *s;
68         BOOST_CHECK_EQUAL (u->planes(), 1);
69         BOOST_CHECK_EQUAL (u->stride()[0], 160);
70         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
71         BOOST_CHECK (u->data()[0]);
72         BOOST_CHECK (!u->data()[1]);
73         BOOST_CHECK (!u->data()[2]);
74         BOOST_CHECK (!u->data()[3]);
75         BOOST_CHECK (u->data() != s->data());
76         BOOST_CHECK (u->data()[0] != s->data()[0]);
77         BOOST_CHECK (u->line_size() != s->line_size());
78         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
79         BOOST_CHECK (u->stride() != s->stride());
80         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
81
82         delete s;
83         delete t;
84         delete u;
85 }
86
87 BOOST_AUTO_TEST_CASE (compact_image_test)
88 {
89         Image* s = new Image (AV_PIX_FMT_RGB24, dcp::Size (50, 50), false);
90         BOOST_CHECK_EQUAL (s->planes(), 1);
91         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
92         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
93         BOOST_CHECK (s->data()[0]);
94         BOOST_CHECK (!s->data()[1]);
95         BOOST_CHECK (!s->data()[2]);
96         BOOST_CHECK (!s->data()[3]);
97
98         /* copy constructor */
99         Image* t = new Image (*s);
100         BOOST_CHECK_EQUAL (t->planes(), 1);
101         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
102         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
103         BOOST_CHECK (t->data()[0]);
104         BOOST_CHECK (!t->data()[1]);
105         BOOST_CHECK (!t->data()[2]);
106         BOOST_CHECK (!t->data()[3]);
107         BOOST_CHECK (t->data() != s->data());
108         BOOST_CHECK (t->data()[0] != s->data()[0]);
109         BOOST_CHECK (t->line_size() != s->line_size());
110         BOOST_CHECK_EQUAL (t->line_size()[0], s->line_size()[0]);
111         BOOST_CHECK (t->stride() != s->stride());
112         BOOST_CHECK_EQUAL (t->stride()[0], s->stride()[0]);
113
114         /* assignment operator */
115         Image* u = new Image (AV_PIX_FMT_YUV422P, dcp::Size (150, 150), true);
116         *u = *s;
117         BOOST_CHECK_EQUAL (u->planes(), 1);
118         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
119         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
120         BOOST_CHECK (u->data()[0]);
121         BOOST_CHECK (!u->data()[1]);
122         BOOST_CHECK (!u->data()[2]);
123         BOOST_CHECK (!u->data()[3]);
124         BOOST_CHECK (u->data() != s->data());
125         BOOST_CHECK (u->data()[0] != s->data()[0]);
126         BOOST_CHECK (u->line_size() != s->line_size());
127         BOOST_CHECK_EQUAL (u->line_size()[0], s->line_size()[0]);
128         BOOST_CHECK (u->stride() != s->stride());
129         BOOST_CHECK_EQUAL (u->stride()[0], s->stride()[0]);
130
131         delete s;
132         delete t;
133         delete u;
134 }
135
136 /** Test Image::alpha_blend */
137 BOOST_AUTO_TEST_CASE (alpha_blend_test)
138 {
139         int const stride = 48 * 4;
140
141         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
142         A->make_black ();
143         uint8_t* a = A->data()[0];
144
145         for (int y = 0; y < 48; ++y) {
146                 uint8_t* p = a + y * stride;
147                 for (int x = 0; x < 16; ++x) {
148                         p[x * 4] = 255;
149                         p[(x + 16) * 4 + 1] = 255;
150                         p[(x + 32) * 4 + 2] = 255;
151                 }
152         }
153
154         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), true));
155         B->make_transparent ();
156         uint8_t* b = B->data()[0];
157
158         for (int y = 32; y < 48; ++y) {
159                 uint8_t* p = b + y * stride;
160                 for (int x = 0; x < 48; ++x) {
161                         p[x * 4] = 255;
162                         p[x * 4 + 1] = 255;
163                         p[x * 4 + 2] = 255;
164                         p[x * 4 + 3] = 255;
165                 }
166         }
167
168         A->alpha_blend (B, Position<int> (0, 0));
169
170         for (int y = 0; y < 32; ++y) {
171                 uint8_t* p = a + y * stride;
172                 for (int x = 0; x < 16; ++x) {
173                         BOOST_CHECK_EQUAL (p[x * 4], 255);
174                         BOOST_CHECK_EQUAL (p[(x + 16) * 4 + 1], 255);
175                         BOOST_CHECK_EQUAL (p[(x + 32) * 4 + 2], 255);
176                 }
177         }
178
179         for (int y = 32; y < 48; ++y) {
180                 uint8_t* p = a + y * stride;
181                 for (int x = 0; x < 48; ++x) {
182                         BOOST_CHECK_EQUAL (p[x * 4], 255);
183                         BOOST_CHECK_EQUAL (p[x * 4 + 1], 255);
184                         BOOST_CHECK_EQUAL (p[x * 4 + 2], 255);
185                         BOOST_CHECK_EQUAL (p[x * 4 + 3], 255);
186                 }
187         }
188 }
189
190 /** Test merge (list<PositionImage>) with a single image */
191 BOOST_AUTO_TEST_CASE (merge_test1)
192 {
193         int const stride = 48 * 4;
194
195         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 48), false));
196         A->make_transparent ();
197         uint8_t* a = A->data()[0];
198
199         for (int y = 0; y < 48; ++y) {
200                 uint8_t* p = a + y * stride;
201                 for (int x = 0; x < 16; ++x) {
202                         /* red */
203                         p[x * 4] = 255;
204                         /* opaque */
205                         p[x * 4 + 3] = 255;
206                 }
207         }
208
209         list<PositionImage> all;
210         all.push_back (PositionImage (A, Position<int> (0, 0)));
211         PositionImage merged = merge (all);
212
213         BOOST_CHECK (merged.position == Position<int> (0, 0));
214         BOOST_CHECK_EQUAL (memcmp (merged.image->data()[0], A->data()[0], stride * 48), 0);
215 }
216
217 /** Test merge (list<PositionImage>) with two images */
218 BOOST_AUTO_TEST_CASE (merge_test2)
219 {
220         shared_ptr<Image> A (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
221         A->make_transparent ();
222         uint8_t* a = A->data()[0];
223         for (int x = 0; x < 16; ++x) {
224                 /* red */
225                 a[x * 4] = 255;
226                 /* opaque */
227                 a[x * 4 + 3] = 255;
228         }
229
230         shared_ptr<Image> B (new Image (AV_PIX_FMT_RGBA, dcp::Size (48, 1), false));
231         B->make_transparent ();
232         uint8_t* b = B->data()[0];
233         for (int x = 0; x < 16; ++x) {
234                 /* blue */
235                 b[(x + 32) * 4 + 2] = 255;
236                 /* opaque */
237                 b[(x + 32) * 4 + 3] = 255;
238         }
239
240         list<PositionImage> all;
241         all.push_back (PositionImage (A, Position<int> (0, 0)));
242         all.push_back (PositionImage (B, Position<int> (0, 0)));
243         PositionImage merged = merge (all);
244
245         BOOST_CHECK (merged.position == Position<int> (0, 0));
246
247         uint8_t* m = merged.image->data()[0];
248
249         for (int x = 0; x < 16; ++x) {
250                 BOOST_CHECK_EQUAL (m[x * 4], 255);
251                 BOOST_CHECK_EQUAL (m[x * 4 + 3], 255);
252                 BOOST_CHECK_EQUAL (m[(x + 16) * 4 + 3], 0);
253                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 2], 255);
254                 BOOST_CHECK_EQUAL (m[(x + 32) * 4 + 3], 255);
255         }
256 }