Split test compile up into individual files.
[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 "lib/image.h"
22 #include "lib/scaler.h"
23
24 using boost::shared_ptr;
25
26 BOOST_AUTO_TEST_CASE (aligned_image_test)
27 {
28         Image* s = new Image (PIX_FMT_RGB24, libdcp::Size (50, 50), true);
29         BOOST_CHECK_EQUAL (s->components(), 1);
30         /* 160 is 150 aligned to the nearest 32 bytes */
31         BOOST_CHECK_EQUAL (s->stride()[0], 160);
32         BOOST_CHECK_EQUAL (s->line_size()[0], 150);
33         BOOST_CHECK (s->data()[0]);
34         BOOST_CHECK (!s->data()[1]);
35         BOOST_CHECK (!s->data()[2]);
36         BOOST_CHECK (!s->data()[3]);
37
38         /* copy constructor */
39         Image* t = new Image (*s);
40         BOOST_CHECK_EQUAL (t->components(), 1);
41         BOOST_CHECK_EQUAL (t->stride()[0], 160);
42         BOOST_CHECK_EQUAL (t->line_size()[0], 150);
43         BOOST_CHECK (t->data()[0]);
44         BOOST_CHECK (!t->data()[1]);
45         BOOST_CHECK (!t->data()[2]);
46         BOOST_CHECK (!t->data()[3]);
47         BOOST_CHECK (t->data() != s->data());
48         BOOST_CHECK (t->data()[0] != s->data()[0]);
49         BOOST_CHECK (t->line_size() != s->line_size());
50         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
51         BOOST_CHECK (t->stride() != s->stride());
52         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
53
54         /* assignment operator */
55         Image* u = new Image (PIX_FMT_YUV422P, libdcp::Size (150, 150), false);
56         *u = *s;
57         BOOST_CHECK_EQUAL (u->components(), 1);
58         BOOST_CHECK_EQUAL (u->stride()[0], 160);
59         BOOST_CHECK_EQUAL (u->line_size()[0], 150);
60         BOOST_CHECK (u->data()[0]);
61         BOOST_CHECK (!u->data()[1]);
62         BOOST_CHECK (!u->data()[2]);
63         BOOST_CHECK (!u->data()[3]);
64         BOOST_CHECK (u->data() != s->data());
65         BOOST_CHECK (u->data()[0] != s->data()[0]);
66         BOOST_CHECK (u->line_size() != s->line_size());
67         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
68         BOOST_CHECK (u->stride() != s->stride());
69         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
70
71         delete s;
72         delete t;
73         delete u;
74 }
75
76 BOOST_AUTO_TEST_CASE (compact_image_test)
77 {
78         Image* s = new Image (PIX_FMT_RGB24, libdcp::Size (50, 50), false);
79         BOOST_CHECK_EQUAL (s->components(), 1);
80         BOOST_CHECK_EQUAL (s->stride()[0], 50 * 3);
81         BOOST_CHECK_EQUAL (s->line_size()[0], 50 * 3);
82         BOOST_CHECK (s->data()[0]);
83         BOOST_CHECK (!s->data()[1]);
84         BOOST_CHECK (!s->data()[2]);
85         BOOST_CHECK (!s->data()[3]);
86
87         /* copy constructor */
88         Image* t = new Image (*s);
89         BOOST_CHECK_EQUAL (t->components(), 1);
90         BOOST_CHECK_EQUAL (t->stride()[0], 50 * 3);
91         BOOST_CHECK_EQUAL (t->line_size()[0], 50 * 3);
92         BOOST_CHECK (t->data()[0]);
93         BOOST_CHECK (!t->data()[1]);
94         BOOST_CHECK (!t->data()[2]);
95         BOOST_CHECK (!t->data()[3]);
96         BOOST_CHECK (t->data() != s->data());
97         BOOST_CHECK (t->data()[0] != s->data()[0]);
98         BOOST_CHECK (t->line_size() != s->line_size());
99         BOOST_CHECK (t->line_size()[0] == s->line_size()[0]);
100         BOOST_CHECK (t->stride() != s->stride());
101         BOOST_CHECK (t->stride()[0] == s->stride()[0]);
102
103         /* assignment operator */
104         Image* u = new Image (PIX_FMT_YUV422P, libdcp::Size (150, 150), true);
105         *u = *s;
106         BOOST_CHECK_EQUAL (u->components(), 1);
107         BOOST_CHECK_EQUAL (u->stride()[0], 50 * 3);
108         BOOST_CHECK_EQUAL (u->line_size()[0], 50 * 3);
109         BOOST_CHECK (u->data()[0]);
110         BOOST_CHECK (!u->data()[1]);
111         BOOST_CHECK (!u->data()[2]);
112         BOOST_CHECK (!u->data()[3]);
113         BOOST_CHECK (u->data() != s->data());
114         BOOST_CHECK (u->data()[0] != s->data()[0]);
115         BOOST_CHECK (u->line_size() != s->line_size());
116         BOOST_CHECK (u->line_size()[0] == s->line_size()[0]);
117         BOOST_CHECK (u->stride() != s->stride());
118         BOOST_CHECK (u->stride()[0] == s->stride()[0]);
119
120         delete s;
121         delete t;
122         delete u;
123 }
124
125 BOOST_AUTO_TEST_CASE (crop_image_test)
126 {
127         /* This was to check out a bug with valgrind, and is probably not very useful */
128         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, libdcp::Size (16, 16), true));
129         image->make_black ();
130         Crop crop;
131         crop.top = 3;
132         image->crop (crop, false);
133 }
134
135 /* Test cropping of a YUV 4:2:0 image by 1 pixel, which used to fail because
136    the U/V copying was not rounded up to the next sample.
137 */
138 BOOST_AUTO_TEST_CASE (crop_image_test2)
139 {
140         /* Here's a 1998 x 1080 image which is black */
141         shared_ptr<Image> image (new Image (PIX_FMT_YUV420P, libdcp::Size (1998, 1080), true));
142         image->make_black ();
143
144         /* Crop it by 1 pixel */
145         Crop crop;
146         crop.left = 1;
147         image = image->crop (crop, true);
148
149         /* Convert it back to RGB to make comparison to black easier */
150         image = image->scale_and_convert_to_rgb (image->size(), Scaler::from_id ("bicubic"), true);
151
152         /* Check that its still black after the crop */
153         uint8_t* p = image->data()[0];
154         for (int y = 0; y < image->size().height; ++y) {
155                 uint8_t* q = p;
156                 for (int x = 0; x < image->size().width; ++x) {
157                         BOOST_CHECK_EQUAL (*q++, 0);
158                         BOOST_CHECK_EQUAL (*q++, 0);
159                         BOOST_CHECK_EQUAL (*q++, 0);
160                 }
161                 p += image->stride()[0];
162         }
163 }