Debian Codegraph @

tests/pixel_data_types.cc

raw
1 /*
2 libheif unit tests
3
4 MIT License
5
6 Copyright (c) 2024 Dirk Farin <[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 "pixelimage.h"
28 #include "catch_amalgamated.hpp"
29
30 TEST_CASE( "uint32_t" )
31 {
32 HeifPixelImage image;
33
34 auto* limits = heif_get_global_security_limits();
35 image.create(3,2, heif_colorspace_nonvisual, heif_chroma_undefined);
36 image.add_channel(heif_channel_Y, 3,2, heif_channel_datatype_unsigned_integer, 32, limits);
37
38 size_t stride;
39 uint32_t* data = image.get_channel<uint32_t>(heif_channel_Y, &stride);
40
41 REQUIRE(stride >= 3);
42 REQUIRE(image.get_width(heif_channel_Y) == 3);
43 REQUIRE(image.get_height(heif_channel_Y) == 2);
44 REQUIRE(image.get_bits_per_pixel(heif_channel_Y) == 32);
45 REQUIRE(image.get_storage_bits_per_pixel(heif_channel_Y) == 32);
46 REQUIRE(image.get_datatype(heif_channel_Y) == heif_channel_datatype_unsigned_integer);
47 REQUIRE(image.get_number_of_interleaved_components(heif_channel_Y) == 1);
48
49 data[0*stride + 0] = 0;
50 data[0*stride + 1] = 0xFFFFFFFFu;
51 data[0*stride + 2] = 1000;
52 data[1*stride + 0] = 0xFFFFFFFFu;
53 data[1*stride + 1] = 0;
54 data[1*stride + 2] = 2000;
55
56 REQUIRE(data[0*stride + 1] == 0xFFFFFFFFu);
57
58 // --- rotate data
59
60 std::shared_ptr<HeifPixelImage> rot;
61 auto rotationResult = image.rotate_ccw(90, limits);
62 REQUIRE(rotationResult.error().error_code == heif_error_Ok);
63 rot = *rotationResult;
64
65 data = rot->get_channel<uint32_t>(heif_channel_Y, &stride);
66
67 REQUIRE(data[0*stride + 0] == 1000);
68 REQUIRE(data[0*stride + 1] == 2000);
69 REQUIRE(data[1*stride + 0] == 0xFFFFFFFFu);
70 REQUIRE(data[1*stride + 1] == 0);
71 REQUIRE(data[2*stride + 0] == 0);
72 REQUIRE(data[2*stride + 1] == 0xFFFFFFFFu);
73
74 // --- mirror
75
76 rot->mirror_inplace(heif_transform_mirror_direction_horizontal, limits);
77
78 REQUIRE(data[0*stride + 1] == 1000);
79 REQUIRE(data[0*stride + 0] == 2000);
80 REQUIRE(data[1*stride + 1] == 0xFFFFFFFFu);
81 REQUIRE(data[1*stride + 0] == 0);
82 REQUIRE(data[2*stride + 1] == 0);
83 REQUIRE(data[2*stride + 0] == 0xFFFFFFFFu);
84
85 rot->mirror_inplace(heif_transform_mirror_direction_vertical, limits);
86
87 REQUIRE(data[2*stride + 1] == 1000);
88 REQUIRE(data[2*stride + 0] == 2000);
89 REQUIRE(data[1*stride + 1] == 0xFFFFFFFFu);
90 REQUIRE(data[1*stride + 0] == 0);
91 REQUIRE(data[0*stride + 1] == 0);
92 REQUIRE(data[0*stride + 0] == 0xFFFFFFFFu);
93
94 // --- crop
95
96 std::shared_ptr<HeifPixelImage> crop;
97 auto cropResult = image.crop(1,2,1,1, limits);
98 REQUIRE(cropResult.error().error_code == heif_error_Ok);
99 crop = *cropResult;
100
101 REQUIRE(crop->get_width(heif_channel_Y) == 2);
102 REQUIRE(crop->get_height(heif_channel_Y) == 1);
103
104 data = crop->get_channel<uint32_t>(heif_channel_Y, &stride);
105
106 REQUIRE(data[0*stride + 0] == 0);
107 REQUIRE(data[0*stride + 1] == 2000);
108
109 cropResult = image.crop(0,1,0,1, limits);
110 REQUIRE(cropResult.error().error_code == heif_error_Ok);
111 crop = *cropResult;
112
113 REQUIRE(crop->get_width(heif_channel_Y) == 2);
114 REQUIRE(crop->get_height(heif_channel_Y) == 2);
115
116 data = crop->get_channel<uint32_t>(heif_channel_Y, &stride);
117
118 REQUIRE(data[0*stride + 0] == 0);
119 REQUIRE(data[0*stride + 1] == 0xFFFFFFFFu);
120 REQUIRE(data[1*stride + 0] == 0xFFFFFFFFu);
121 REQUIRE(data[1*stride + 1] == 0);
122 }
123
124
125 TEST_CASE( "complex64_t" )
126 {
127 HeifPixelImage image;
128
129 auto* limits = heif_get_global_security_limits();
130 image.create(3,2, heif_colorspace_nonvisual, heif_chroma_undefined);
131 image.add_channel(heif_channel_Y, 3,2, heif_channel_datatype_complex_number, 128, limits);
132
133 size_t stride;
134 heif_complex64* data = image.get_channel<heif_complex64>(heif_channel_Y, &stride);
135
136 REQUIRE(stride >= 3);
137 REQUIRE(image.get_width(heif_channel_Y) == 3);
138 REQUIRE(image.get_height(heif_channel_Y) == 2);
139 REQUIRE(image.get_bits_per_pixel(heif_channel_Y) == 128);
140 REQUIRE(image.get_storage_bits_per_pixel(heif_channel_Y) == 128);
141 REQUIRE(image.get_datatype(heif_channel_Y) == heif_channel_datatype_complex_number);
142 REQUIRE(image.get_number_of_interleaved_components(heif_channel_Y) == 1);
143
144 data[0*stride + 0] = {0.0, -1.0};
145 data[0*stride + 1] = {1.0, 2.0};
146 data[0*stride + 2] = {2.0, -1.0};
147 data[1*stride + 0] = {0.25, 0.5};
148 data[1*stride + 1] = {0.0, 0.0};
149 data[1*stride + 2] = {-0.75, 0.125};
150
151 REQUIRE(data[0*stride + 1].real == 1.0);
152 REQUIRE(data[0*stride + 1].imaginary == 2.0);
153 }
154
155
156 TEST_CASE( "image datatype through public API" )
157 {
158 heif_image* image;
159 heif_error error = heif_image_create(3,2,heif_colorspace_nonvisual, heif_chroma_undefined, &image);
160 REQUIRE(!error.code);
161
162 heif_image_add_channel(image, heif_channel_Y, 3,2, heif_channel_datatype_unsigned_integer, 32);
163
164 size_t stride;
165 uint32_t* data = heif_image_get_channel_uint32(image, heif_channel_Y, &stride);
166 REQUIRE(data != nullptr);
167
168 REQUIRE(stride >= 3);
169 REQUIRE(heif_image_get_datatype(image, heif_channel_Y) == heif_channel_datatype_unsigned_integer);
170 REQUIRE(heif_image_get_bits_per_pixel_range(image, heif_channel_Y) == 32);
171
172 data[stride*0 + 0] = 0xFFFFFFFFu;
173 data[stride*0 + 1] = 0;
174 data[stride*0 + 2] = 1000;
175 data[stride*1 + 0] = 0xFFFFFFFFu;
176 data[stride*1 + 1] = 200;
177 data[stride*1 + 2] = 5;
178 }