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