Debian Codegraph @

tests/jpeg2000.cc

raw
1 /*
2 libheif JPEG 2000 unit tests
3
4 MIT License
5
6 Copyright (c) 2023 Brad Hards <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 */
26
27 #include "catch_amalgamated.hpp"
28 #include "libheif/heif.h"
29 #include "codecs/jpeg2000_boxes.h"
30 #include <cstdint>
31 #include <iostream>
32
33
34 TEST_CASE( "cdef" )
35 {
36 std::shared_ptr<Box_cdef> cdef = std::make_shared<Box_cdef>();
37 REQUIRE(cdef->get_channels().size() == 0);
38 Box_cdef::Channel channel;
39 channel.channel_index = 1;
40 channel.channel_type = 2;
41 channel.channel_association = 0;
42 cdef->add_channel(channel);
43 REQUIRE(cdef->get_channels().size() == 1);
44 REQUIRE(cdef->get_channels()[0].channel_index == 1);
45 REQUIRE(cdef->get_channels()[0].channel_type == 2);
46 REQUIRE(cdef->get_channels()[0].channel_association == 0);
47
48 StreamWriter writer;
49 Error err = cdef->write(writer);
50 REQUIRE(err.error_code == heif_error_Ok);
51 const std::vector<uint8_t> bytes = writer.get_data();
52 std::vector<uint8_t> expected = {0x00, 0x00, 0x00, 0x10, 'c', 'd', 'e', 'f', 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00};
53 REQUIRE(bytes == expected);
54
55 Indent indent;
56 std::string dump_output = cdef->dump(indent);
57 REQUIRE(dump_output == "Box: cdef -----\nsize: 0 (header size: 0)\nchannel_index: 1, channel_type: 2, channel_association: 0\n");
58 }
59
60 TEST_CASE( "cmap" )
61 {
62 std::shared_ptr<Box_cmap> cmap = std::make_shared<Box_cmap>();
63 REQUIRE(cmap->get_components().size() == 0);
64 Box_cmap::Component component;
65 component.component_index = 2;
66 component.mapping_type = 1;
67 component.palette_colour = 3;
68 cmap->add_component(component);
69 Box_cmap::Component component2;
70 component2.component_index = 4;
71 component2.mapping_type = 0;
72 component2.palette_colour = 0;
73 cmap->add_component(component2);
74
75 REQUIRE(cmap->get_components().size() == 2);
76 REQUIRE(cmap->get_components()[0].component_index == 2);
77 REQUIRE(cmap->get_components()[0].mapping_type == 1);
78 REQUIRE(cmap->get_components()[0].palette_colour == 3);
79 REQUIRE(cmap->get_components()[1].component_index == 4);
80 REQUIRE(cmap->get_components()[1].mapping_type == 0);
81 REQUIRE(cmap->get_components()[1].palette_colour == 0);
82 StreamWriter writer;
83 Error err = cmap->write(writer);
84 REQUIRE(err.error_code == heif_error_Ok);
85 const std::vector<uint8_t> bytes = writer.get_data();
86 std::vector<uint8_t> expected = {0x00, 0x00, 0x00, 0x10, 'c', 'm', 'a', 'p', 0x00, 0x02, 0x01, 0x03, 0x00, 0x04, 0x00, 0x00};
87 REQUIRE(bytes == expected);
88
89 Indent indent;
90 std::string dump_output = cmap->dump(indent);
91 REQUIRE(dump_output == "Box: cmap -----\nsize: 0 (header size: 0)\ncomponent_index: 2, mapping_type: 1, palette_colour: 3\ncomponent_index: 4, mapping_type: 0, palette_colour: 0\n");
92 }
93
94 TEST_CASE( "pclr empty" )
95 {
96 std::shared_ptr<Box_pclr> pclr = std::make_shared<Box_pclr>();
97 REQUIRE(pclr->get_entries().size() == 0);
98 REQUIRE(pclr->get_num_entries() == 0);
99 REQUIRE(pclr->get_num_columns() == 0);
100 REQUIRE(pclr->get_bit_depths().size() == 0);
101
102 Indent indent;
103 std::string dump_output = pclr->dump(indent);
104 REQUIRE(dump_output == "Box: pclr -----\nsize: 0 (header size: 0)\nNE: 0, NPC: 0, B: \n");
105
106 StreamWriter writer;
107 Error err = pclr->write(writer);
108 REQUIRE(err.error_code == heif_error_Ok);
109 const std::vector<uint8_t> bytes = writer.get_data();
110 REQUIRE(bytes.size() == 0);
111 }
112
113 TEST_CASE( "pclr" )
114 {
115 std::shared_ptr<Box_pclr> pclr = std::make_shared<Box_pclr>();
116 pclr->set_columns(3, 8);
117 Box_pclr::PaletteEntry entry0;
118 entry0.columns = std::vector<uint16_t> { 1, 2, 3};
119 pclr->add_entry(entry0);
120 Box_pclr::PaletteEntry entry1;
121 entry1.columns = std::vector<uint16_t> { 255, 254, 253};
122 pclr->add_entry(entry1);
123 REQUIRE(pclr->get_entries().size() == 2);
124 REQUIRE(pclr->get_num_entries() == 2);
125 REQUIRE(pclr->get_num_columns() == 3);
126 REQUIRE(pclr->get_bit_depths().size() == 3);
127 REQUIRE(pclr->get_bit_depths()[0] == 8);
128 REQUIRE(pclr->get_bit_depths()[1] == 8);
129 REQUIRE(pclr->get_bit_depths()[2] == 8);
130 StreamWriter writer;
131 Error err = pclr->write(writer);
132 REQUIRE(err.error_code == heif_error_Ok);
133 const std::vector<uint8_t> bytes = writer.get_data();
134 std::vector<uint8_t> expected = {0x00, 0x00, 0x00, 0x14, 'p', 'c', 'l', 'r', 0x00, 0x02, 0x03, 0x08, 0x08, 0x08, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD};
135 REQUIRE(bytes == expected);
136 Indent indent;
137 std::string dump_output = pclr->dump(indent);
138 REQUIRE(dump_output == "Box: pclr -----\nsize: 0 (header size: 0)\nNE: 2, NPC: 3, B: 8, 8, 8, \n");
139 }
140
141 TEST_CASE( "pclr 12 bit" )
142 {
143 std::shared_ptr<Box_pclr> pclr = std::make_shared<Box_pclr>();
144 pclr->set_columns(3, 12);
145 Box_pclr::PaletteEntry entry0;
146 entry0.columns = std::vector<uint16_t> { 1, 2, 3};
147 pclr->add_entry(entry0);
148 Box_pclr::PaletteEntry entry1;
149 entry1.columns = std::vector<uint16_t> { 4095, 4094, 4093};
150 pclr->add_entry(entry1);
151 REQUIRE(pclr->get_entries().size() == 2);
152 REQUIRE(pclr->get_num_entries() == 2);
153 REQUIRE(pclr->get_num_columns() == 3);
154 REQUIRE(pclr->get_bit_depths().size() == 3);
155 REQUIRE(pclr->get_bit_depths()[0] == 12);
156 REQUIRE(pclr->get_bit_depths()[1] == 12);
157 REQUIRE(pclr->get_bit_depths()[2] == 12);
158 StreamWriter writer;
159 Error err = pclr->write(writer);
160 REQUIRE(err.error_code == heif_error_Ok);
161 const std::vector<uint8_t> bytes = writer.get_data();
162 std::vector<uint8_t> expected = {0x00, 0x00, 0x00, 0x1A, 'p', 'c', 'l', 'r', 0x00, 0x02, 0x03, 0x0C, 0x0C, 0x0C, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x0F, 0xFF, 0x0F, 0xFE, 0x0F, 0xFD};
163 REQUIRE(bytes == expected);
164 Indent indent;
165 std::string dump_output = pclr->dump(indent);
166 REQUIRE(dump_output == "Box: pclr -----\nsize: 0 (header size: 0)\nNE: 2, NPC: 3, B: 12, 12, 12, \n");
167 }
168
169 TEST_CASE( "j2kL" )
170 {
171 std::shared_ptr<Box_j2kL> j2kL = std::make_shared<Box_j2kL>();
172 REQUIRE(j2kL->get_layers().size() == 0);
173 Box_j2kL::Layer layer;
174 layer.layer_id = 1;
175 layer.discard_levels = 2;
176 layer.decode_layers = 3;
177 j2kL->add_layer(layer);
178 REQUIRE(j2kL->get_layers().size() == 1);
179 REQUIRE(j2kL->get_layers()[0].layer_id == 1);
180 REQUIRE(j2kL->get_layers()[0].discard_levels == 2);
181 REQUIRE(j2kL->get_layers()[0].decode_layers == 3);
182
183 StreamWriter writer;
184 Error err = j2kL->write(writer);
185 REQUIRE(err.error_code == heif_error_Ok);
186 const std::vector<uint8_t> bytes = writer.get_data();
187 std::vector<uint8_t> expected = {0x00, 0x00, 0x00, 0x13, 'j', '2', 'k', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x02, 0x00, 0x03};
188 REQUIRE(bytes == expected);
189
190 Indent indent;
191 std::string dump_output = j2kL->dump(indent);
192 REQUIRE(dump_output == "Box: j2kL -----\nsize: 0 (header size: 0)\nlayer_id: 1, discard_levels: 2, decode_layers: 3\n");
193 }
194
195
196 TEST_CASE( "codestream too short for SOC" )
197 {
198 std::vector<uint8_t> data = {0xFF};
199 JPEG2000MainHeader uut;
200 uut.setHeaderData(data);
201 Error err = uut.doParse();
202 REQUIRE(err.error_code == heif_error_Invalid_input);
203 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
204 }
205
206
207 TEST_CASE( "codestream missing SOC" )
208 {
209 std::vector<uint8_t> data = {0xFF, 0x4E};
210 JPEG2000MainHeader uut;
211 uut.setHeaderData(data);
212 Error err = uut.doParse();
213 REQUIRE(err.error_code == heif_error_Invalid_input);
214 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
215 }
216
217 TEST_CASE( "codestream too short for SIZ body" )
218 {
219 std::vector<uint8_t> data = {0xFF, 0x4F, 0xFF, 0x51};
220 JPEG2000MainHeader uut;
221 uut.setHeaderData(data);
222 Error err = uut.doParse();
223 REQUIRE(err.error_code == heif_error_Invalid_input);
224 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
225 }
226
227 TEST_CASE( "codestream - COD + SIZ" )
228 {
229 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
230 std::vector<uint8_t> data = {
231 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
232 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
234 };
235 JPEG2000MainHeader uut;
236 uut.setHeaderData(data);
237 Error err = uut.doParse();
238 REQUIRE(err.error_code == heif_error_Ok);
239 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
240 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
241 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
242 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
243 REQUIRE(uut.get_SIZ().tile_width == 1);
244 REQUIRE(uut.get_SIZ().tile_height == 9);
245 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
246 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
247 REQUIRE(uut.get_SIZ().components.size() == 1);
248 REQUIRE(uut.get_SIZ().components[0].precision == 8);
249 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
250 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
251 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
252 REQUIRE(uut.get_chroma_format() == heif_chroma_monochrome);
253 REQUIRE(uut.get_precision(0) == 8);
254 REQUIRE(uut.hasHighThroughputExtension() == false);
255 }
256
257 TEST_CASE( "codestream - COD + SIZ first plane subsampled" )
258 {
259 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
260 // with modifications
261 std::vector<uint8_t> data = {
262 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x02, 0xFF, 0x5C, 0x00,
265 };
266 JPEG2000MainHeader uut;
267 uut.setHeaderData(data);
268 Error err = uut.doParse();
269 REQUIRE(err.error_code == heif_error_Ok);
270 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
271 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
272 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
273 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
274 REQUIRE(uut.get_SIZ().tile_width == 1);
275 REQUIRE(uut.get_SIZ().tile_height == 9);
276 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
277 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
278 REQUIRE(uut.get_SIZ().components.size() == 1);
279 REQUIRE(uut.get_SIZ().components[0].precision == 8);
280 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
281 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
282 REQUIRE(uut.get_SIZ().components[0].v_separation == 2);
283 REQUIRE(uut.get_chroma_format() == heif_chroma_undefined);
284 REQUIRE(uut.get_precision(0) == 8);
285 REQUIRE(uut.hasHighThroughputExtension() == false);
286 }
287
288 TEST_CASE( "codestream - COD + SIZ 4:4:4" )
289 {
290 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
291 // with modifications
292 std::vector<uint8_t> data = {
293 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x01, 0x01, 0x06, 0x01, 0x01,
296 0x05, 0x01, 0x01, 0xFF, 0x5C, 0x00,
297 };
298 JPEG2000MainHeader uut;
299 uut.setHeaderData(data);
300 Error err = uut.doParse();
301 REQUIRE(err.error_code == heif_error_Ok);
302 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
303 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
304 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
305 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
306 REQUIRE(uut.get_SIZ().tile_width == 1);
307 REQUIRE(uut.get_SIZ().tile_height == 9);
308 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
309 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
310 REQUIRE(uut.get_SIZ().components.size() == 3);
311 REQUIRE(uut.get_SIZ().components[0].precision == 8);
312 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
313 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
314 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
315 REQUIRE(uut.get_SIZ().components[1].precision == 7);
316 REQUIRE(uut.get_SIZ().components[1].is_signed == false);
317 REQUIRE(uut.get_SIZ().components[1].h_separation == 1);
318 REQUIRE(uut.get_SIZ().components[1].v_separation == 1);
319 REQUIRE(uut.get_SIZ().components[2].precision == 6);
320 REQUIRE(uut.get_SIZ().components[2].is_signed == false);
321 REQUIRE(uut.get_SIZ().components[2].h_separation == 1);
322 REQUIRE(uut.get_SIZ().components[2].v_separation == 1);
323 REQUIRE(uut.get_chroma_format() == heif_chroma_444);
324 REQUIRE(uut.get_precision(0) == 8);
325 REQUIRE(uut.get_precision(1) == 7);
326 REQUIRE(uut.get_precision(2) == 6);
327 REQUIRE(uut.hasHighThroughputExtension() == false);
328 }
329
330 TEST_CASE( "codestream - COD + SIZ 4:2:2" )
331 {
332 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
333 // with modifications
334 std::vector<uint8_t> data = {
335 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
336 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
337 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x01, 0x01, 0x07, 0x02, 0x01,
338 0x07, 0x02, 0x01, 0xFF, 0x5C, 0x00,
339 };
340 JPEG2000MainHeader uut;
341 uut.setHeaderData(data);
342 Error err = uut.doParse();
343 REQUIRE(err.error_code == heif_error_Ok);
344 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
345 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
346 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
347 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
348 REQUIRE(uut.get_SIZ().tile_width == 1);
349 REQUIRE(uut.get_SIZ().tile_height == 9);
350 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
351 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
352 REQUIRE(uut.get_SIZ().components.size() == 3);
353 REQUIRE(uut.get_SIZ().components[0].precision == 8);
354 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
355 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
356 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
357 REQUIRE(uut.get_SIZ().components[1].precision == 8);
358 REQUIRE(uut.get_SIZ().components[1].is_signed == false);
359 REQUIRE(uut.get_SIZ().components[1].h_separation == 2);
360 REQUIRE(uut.get_SIZ().components[1].v_separation == 1);
361 REQUIRE(uut.get_SIZ().components[2].precision == 8);
362 REQUIRE(uut.get_SIZ().components[2].is_signed == false);
363 REQUIRE(uut.get_SIZ().components[2].h_separation == 2);
364 REQUIRE(uut.get_SIZ().components[2].v_separation == 1);
365 REQUIRE(uut.get_chroma_format() == heif_chroma_422);
366 REQUIRE(uut.get_precision(0) == 8);
367 REQUIRE(uut.get_precision(1) == 8);
368 REQUIRE(uut.get_precision(2) == 8);
369 REQUIRE(uut.hasHighThroughputExtension() == false);
370 }
371
372 TEST_CASE( "codestream - COD + SIZ 4:2:0" )
373 {
374 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
375 // with modifications
376 std::vector<uint8_t> data = {
377 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
378 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
379 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x01, 0x01, 0x07, 0x02, 0x02,
380 0x07, 0x02, 0x02, 0xFF, 0x5C, 0x00,
381 };
382 JPEG2000MainHeader uut;
383 uut.setHeaderData(data);
384 Error err = uut.doParse();
385 REQUIRE(err.error_code == heif_error_Ok);
386 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
387 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
388 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
389 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
390 REQUIRE(uut.get_SIZ().tile_width == 1);
391 REQUIRE(uut.get_SIZ().tile_height == 9);
392 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
393 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
394 REQUIRE(uut.get_SIZ().components.size() == 3);
395 REQUIRE(uut.get_SIZ().components[0].precision == 8);
396 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
397 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
398 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
399 REQUIRE(uut.get_SIZ().components[1].precision == 8);
400 REQUIRE(uut.get_SIZ().components[1].is_signed == false);
401 REQUIRE(uut.get_SIZ().components[1].h_separation == 2);
402 REQUIRE(uut.get_SIZ().components[1].v_separation == 2);
403 REQUIRE(uut.get_SIZ().components[2].precision == 8);
404 REQUIRE(uut.get_SIZ().components[2].is_signed == false);
405 REQUIRE(uut.get_SIZ().components[2].h_separation == 2);
406 REQUIRE(uut.get_SIZ().components[2].v_separation == 2);
407 REQUIRE(uut.get_chroma_format() == heif_chroma_420);
408 REQUIRE(uut.get_precision(0) == 8);
409 REQUIRE(uut.get_precision(1) == 8);
410 REQUIRE(uut.get_precision(2) == 8);
411 REQUIRE(uut.hasHighThroughputExtension() == false);
412 }
413
414
415 TEST_CASE( "codestream - COD + SIZ mismatched v subsampling" )
416 {
417 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
418 // with modifications
419 std::vector<uint8_t> data = {
420 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
421 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
422 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x01, 0x01, 0x07, 0x02, 0x02,
423 0x07, 0x02, 0x01, 0xFF, 0x5C, 0x00,
424 };
425 JPEG2000MainHeader uut;
426 uut.setHeaderData(data);
427 Error err = uut.doParse();
428 REQUIRE(err.error_code == heif_error_Ok);
429 REQUIRE(uut.get_SIZ().components.size() == 3);
430 REQUIRE(uut.get_SIZ().components[1].h_separation == 2);
431 REQUIRE(uut.get_SIZ().components[1].v_separation == 2);
432 REQUIRE(uut.get_SIZ().components[2].h_separation == 2);
433 REQUIRE(uut.get_SIZ().components[2].v_separation == 1);
434 REQUIRE(uut.get_chroma_format() == heif_chroma_undefined);
435 }
436
437 TEST_CASE( "codestream - COD + SIZ mismatched h subsampling" )
438 {
439 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
440 // with modifications
441 std::vector<uint8_t> data = {
442 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
443 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
444 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x01, 0x01, 0x07, 0x01, 0x02,
445 0x07, 0x02, 0x02, 0xFF, 0x5C, 0x00,
446 };
447 JPEG2000MainHeader uut;
448 uut.setHeaderData(data);
449 Error err = uut.doParse();
450 REQUIRE(err.error_code == heif_error_Ok);
451 REQUIRE(uut.get_SIZ().components.size() == 3);
452 REQUIRE(uut.get_SIZ().components[1].h_separation == 1);
453 REQUIRE(uut.get_SIZ().components[1].v_separation == 2);
454 REQUIRE(uut.get_SIZ().components[2].h_separation == 2);
455 REQUIRE(uut.get_SIZ().components[2].v_separation == 2);
456 REQUIRE(uut.get_chroma_format() == heif_chroma_undefined);
457 }
458
459 TEST_CASE( "codestream - COD + SIZ unsupported subsampling" )
460 {
461 // This data is a subset of the example in ISO/IEC 15444-1:2019 Section J.10.1 "Main header"
462 // with modifications
463 std::vector<uint8_t> data = {
464 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
465 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
466 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x01, 0x01, 0x07, 0x04, 0x01,
467 0x07, 0x04, 0x01, 0xFF, 0x5C, 0x00,
468 };
469 JPEG2000MainHeader uut;
470 uut.setHeaderData(data);
471 Error err = uut.doParse();
472 REQUIRE(err.error_code == heif_error_Ok);
473 REQUIRE(uut.get_SIZ().components.size() == 3);
474 REQUIRE(uut.get_SIZ().components[1].h_separation == 4);
475 REQUIRE(uut.get_SIZ().components[1].v_separation == 1);
476 REQUIRE(uut.get_SIZ().components[2].h_separation == 4);
477 REQUIRE(uut.get_SIZ().components[2].v_separation == 1);
478 REQUIRE(uut.get_chroma_format() == heif_chroma_undefined);
479 }
480
481
482 TEST_CASE( "codestream wrong marker SIZ" )
483 {
484 std::vector<uint8_t> data = {
485 0xFF, 0x4F, 0xFF, 0xEF, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
486 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
487 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
488 };
489 JPEG2000MainHeader uut;
490 uut.setHeaderData(data);
491 Error err = uut.doParse();
492 REQUIRE(err.error_code == heif_error_Invalid_input);
493 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
494 }
495
496 TEST_CASE( "codestream Lsiz too small" )
497 {
498 std::vector<uint8_t> data = {
499 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
500 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
501 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
502 };
503 JPEG2000MainHeader uut;
504 uut.setHeaderData(data);
505 Error err = uut.doParse();
506 REQUIRE(err.error_code == heif_error_Invalid_input);
507 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
508 }
509
510 TEST_CASE( "codestream Lsiz too large" )
511 {
512 std::vector<uint8_t> data = {
513 0xFF, 0x4F, 0xFF, 0x51, 0xC0, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
514 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
515 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
516 };
517 JPEG2000MainHeader uut;
518 uut.setHeaderData(data);
519 Error err = uut.doParse();
520 REQUIRE(err.error_code == heif_error_Invalid_input);
521 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
522 }
523
524 TEST_CASE( "codestream Csiz too small" )
525 {
526 std::vector<uint8_t> data = {
527 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
528 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
529 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
530 };
531 JPEG2000MainHeader uut;
532 uut.setHeaderData(data);
533 Error err = uut.doParse();
534 REQUIRE(err.error_code == heif_error_Invalid_input);
535 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
536 }
537
538 TEST_CASE( "codestream Csiz too large" )
539 {
540 std::vector<uint8_t> data = {
541 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
543 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
544 };
545 JPEG2000MainHeader uut;
546 uut.setHeaderData(data);
547 Error err = uut.doParse();
548 REQUIRE(err.error_code == heif_error_Invalid_input);
549 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
550 }
551
552
553 TEST_CASE( "codestream bad Csiz" )
554 {
555 std::vector<uint8_t> data = {
556 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
557 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
558 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x07, 0x01, 0x01, 0xFF, 0x5C, 0x00,
559 };
560 JPEG2000MainHeader uut;
561 uut.setHeaderData(data);
562 Error err = uut.doParse();
563 REQUIRE(err.error_code == heif_error_Invalid_input);
564 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
565 }
566
567
568 TEST_CASE( "codestream missing segments" )
569 {
570 std::vector<uint8_t> data = {
571 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
572 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
573 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01
574 };
575 JPEG2000MainHeader uut;
576 uut.setHeaderData(data);
577 Error err = uut.doParse();
578 REQUIRE(err.error_code == heif_error_Invalid_input);
579 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
580 }
581
582 TEST_CASE( "codestream - COD + SIZ + CAP" )
583 {
584 // This data is a modified version of subset of the example in ISO/IEC 15444-1:2019 Section
585 // J.10.1 "Main header"
586 std::vector<uint8_t> data = {
587 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
588 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
589 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01,
590 0xFF, 0x50, 0x00, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x22,
591 0xFF, 0x5C, 0x00,
592 };
593 JPEG2000MainHeader uut;
594 uut.setHeaderData(data);
595 Error err = uut.doParse();
596 REQUIRE(err.error_code == heif_error_Ok);
597 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
598 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
599 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
600 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
601 REQUIRE(uut.get_SIZ().tile_width == 1);
602 REQUIRE(uut.get_SIZ().tile_height == 9);
603 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
604 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
605 REQUIRE(uut.get_SIZ().components.size() == 1);
606 REQUIRE(uut.get_SIZ().components[0].precision == 8);
607 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
608 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
609 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
610 REQUIRE(uut.get_chroma_format() == heif_chroma_monochrome);
611 REQUIRE(uut.get_precision(0) == 8);
612 REQUIRE(uut.hasHighThroughputExtension() == true);
613 }
614
615 TEST_CASE( "codestream CAP short" )
616 {
617 std::vector<uint8_t> data = {
618 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
619 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
620 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01,
621 0xFF, 0x50, 0x00, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00
622 };
623 JPEG2000MainHeader uut;
624 uut.setHeaderData(data);
625 Error err = uut.doParse();
626 REQUIRE(err.error_code == heif_error_Invalid_input);
627 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
628 }
629
630 TEST_CASE( "codestream Lcap short" )
631 {
632 std::vector<uint8_t> data = {
633 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
634 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
635 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01,
636 0xFF, 0x50, 0x00, 0x07, 0x00, 0x02, 0x00, 0x00, 0x00, 0x22,
637 0xFF, 0x5C, 0x00,
638 };
639 JPEG2000MainHeader uut;
640 uut.setHeaderData(data);
641 Error err = uut.doParse();
642 REQUIRE(err.error_code == heif_error_Invalid_input);
643 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
644 }
645
646 TEST_CASE( "codestream Lcap long" )
647 {
648 std::vector<uint8_t> data = {
649 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
650 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
651 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01,
652 0xFF, 0x50, 0x00, 0x47, 0x00, 0x02, 0x00, 0x00, 0x00, 0x22,
653 0xFF, 0x5C, 0x00,
654 };
655 JPEG2000MainHeader uut;
656 uut.setHeaderData(data);
657 Error err = uut.doParse();
658 REQUIRE(err.error_code == heif_error_Invalid_input);
659 REQUIRE(err.sub_error_code == heif_suberror_Invalid_J2K_codestream);
660 }
661
662
663 TEST_CASE( "codestream - COD + SIZ + CAP multiple" )
664 {
665 // This data is a modified version of subset of the example in ISO/IEC 15444-1:2019 Section
666 // J.10.1 "Main header"
667 std::vector<uint8_t> data = {
668 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
669 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
670 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01,
671 0xFF, 0x50, 0x00, 0x0A, 0x00, 0x12, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x22,
672 0xFF, 0x5C, 0x00,
673 };
674 JPEG2000MainHeader uut;
675 uut.setHeaderData(data);
676 Error err = uut.doParse();
677 REQUIRE(err.error_code == heif_error_Ok);
678 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
679 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
680 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
681 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
682 REQUIRE(uut.get_SIZ().tile_width == 1);
683 REQUIRE(uut.get_SIZ().tile_height == 9);
684 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
685 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
686 REQUIRE(uut.get_SIZ().components.size() == 1);
687 REQUIRE(uut.get_SIZ().components[0].precision == 8);
688 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
689 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
690 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
691 REQUIRE(uut.get_chroma_format() == heif_chroma_monochrome);
692 REQUIRE(uut.get_precision(0) == 8);
693 REQUIRE(uut.hasHighThroughputExtension() == true);
694 }
695
696
697 TEST_CASE( "codestream - COD + SIZ + CAP other" )
698 {
699 // This data is a modified version of subset of the example in ISO/IEC 15444-1:2019 Section
700 // J.10.1 "Main header"
701 // Note that the ccap2 value may not be valid
702 std::vector<uint8_t> data = {
703 0xFF, 0x4F, 0xFF, 0x51, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
704 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09,
705 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x01,
706 0xFF, 0x50, 0x00, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x22,
707 0xFF, 0x5C, 0x00,
708 };
709 JPEG2000MainHeader uut;
710 uut.setHeaderData(data);
711 Error err = uut.doParse();
712 REQUIRE(err.error_code == heif_error_Ok);
713 REQUIRE(uut.get_SIZ().reference_grid_width == 1);
714 REQUIRE(uut.get_SIZ().reference_grid_height == 9);
715 REQUIRE(uut.get_SIZ().image_horizontal_offset == 0);
716 REQUIRE(uut.get_SIZ().image_vertical_offset == 0);
717 REQUIRE(uut.get_SIZ().tile_width == 1);
718 REQUIRE(uut.get_SIZ().tile_height == 9);
719 REQUIRE(uut.get_SIZ().tile_offset_x == 0);
720 REQUIRE(uut.get_SIZ().tile_offset_y == 0);
721 REQUIRE(uut.get_SIZ().components.size() == 1);
722 REQUIRE(uut.get_SIZ().components[0].precision == 8);
723 REQUIRE(uut.get_SIZ().components[0].is_signed == false);
724 REQUIRE(uut.get_SIZ().components[0].h_separation == 1);
725 REQUIRE(uut.get_SIZ().components[0].v_separation == 1);
726 REQUIRE(uut.get_chroma_format() == heif_chroma_monochrome);
727 REQUIRE(uut.get_precision(0) == 8);
728 REQUIRE(uut.hasHighThroughputExtension() == false);
729 }