Debian Codegraph @

tests/test_utils.cc

raw
1 /*
2 libheif test support utilities
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 "test_utils.h"
28 #include "libheif/heif.h"
29 #include "test-config.h"
30 #include <cstring>
31 #include "catch_amalgamated.hpp"
32
33
34 heif_context * get_context_for_test_file(std::string filename)
35 {
36 return get_context_for_local_file(tests_data_directory + "/" + filename);
37 }
38
39 heif_context * get_context_for_local_file(std::string filename)
40 {
41 heif_context* context;
42 heif_error err;
43 context = heif_context_alloc();
44 err = heif_context_read_from_file(context, filename.c_str(), NULL);
45 INFO(filename);
46 REQUIRE(err.code == heif_error_Ok);
47 return context;
48 }
49
50 heif_image_handle * get_primary_image_handle(heif_context *context)
51 {
52 heif_error err;
53 heif_image_handle * image_handle;
54 int num_images = heif_context_get_number_of_top_level_images(context);
55 REQUIRE(num_images == 1);
56 err = heif_context_get_primary_image_handle(context, &image_handle);
57 REQUIRE(err.code == heif_error_Ok);
58 return image_handle;
59 }
60
61 heif_image * get_primary_image(heif_image_handle * handle)
62 {
63 heif_error err;
64 heif_image* img;
65 err = heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_444, NULL);
66 REQUIRE(err.code == heif_error_Ok);
67 return img;
68 }
69
70 heif_image * get_primary_image_mono(heif_image_handle * handle)
71 {
72 heif_error err;
73 heif_image* img;
74 err = heif_decode_image(handle, &img, heif_colorspace_monochrome, heif_chroma_monochrome, NULL);
75 REQUIRE(err.code == heif_error_Ok);
76 return img;
77 }
78
79 heif_image * get_primary_image_ycbcr(heif_image_handle * handle, heif_chroma chroma)
80 {
81 heif_error err;
82 heif_image* img;
83 err = heif_decode_image(handle, &img, heif_colorspace_YCbCr, chroma, NULL);
84 REQUIRE(err.code == heif_error_Ok);
85 return img;
86 }
87
88 void fill_new_plane(heif_image* img, heif_channel channel, int w, int h)
89 {
90 heif_error err;
91
92 err = heif_image_add_plane(img, channel, w, h, 8);
93 REQUIRE(err.code == heif_error_Ok);
94
95 int stride;
96 uint8_t* p = heif_image_get_plane(img, channel, &stride);
97
98 for (int y = 0; y < h; y++) {
99 memset(p + y * stride, 128, w);
100 }
101 }
102
103 heif_image * createImage_RGB_planar()
104 {
105 heif_image *image;
106 heif_error err;
107 int w = 1024;
108 int h = 768;
109 err = heif_image_create(w, h, heif_colorspace_RGB,
110 heif_chroma_444, &image);
111 if (err.code) {
112 return nullptr;
113 }
114
115 err = heif_image_add_plane(image, heif_channel_R, w, h, 8);
116 REQUIRE(err.code == heif_error_Ok);
117 err = heif_image_add_plane(image, heif_channel_G, w, h, 8);
118 REQUIRE(err.code == heif_error_Ok);
119 err = heif_image_add_plane(image, heif_channel_B, w, h, 8);
120 REQUIRE(err.code == heif_error_Ok);
121
122
123 int stride;
124 uint8_t *r = heif_image_get_plane(image, heif_channel_R, &stride);
125 uint8_t *g = heif_image_get_plane(image, heif_channel_G, &stride);
126 uint8_t *b = heif_image_get_plane(image, heif_channel_B, &stride);
127
128 int y = 0;
129 for (; y < h / 2; y++) {
130 int x = 0;
131 for (; x < w / 3; x++) {
132 r[y * stride + x] = 1;
133 g[y * stride + x] = 255;
134 b[y * stride + x] = 2;
135 }
136 for (; x < 2 * w / 3; x++) {
137 r[y * stride + x] = 4;
138 g[y * stride + x] = 5;
139 b[y * stride + x] = 255;
140 }
141 for (; x < w; x++) {
142 r[y * stride + x] = 255;
143 g[y * stride + x] = 6;
144 b[y * stride + x] = 7;
145 }
146 }
147 for (; y < h; y++) {
148 int x = 0;
149 for (; x < w / 3; x++) {
150 r[y * stride + x]= 8;
151 g[y * stride + x] = 9;
152 b[y * stride + x] = 255;
153 }
154 for (; x < 2 * w / 3; x++) {
155 r[y * stride + x] = 253;
156 g[y * stride + x] = 10;
157 b[y * stride + x] = 11;
158 }
159 for (; x < w; x++) {
160 r[y * stride + x] = 13;
161 g[y * stride + x] = 252;
162 b[y * stride + x] = 12;
163 }
164 }
165 if (err.code) {
166 heif_image_release(image);
167 return nullptr;
168 }
169
170 return image;
171 }
172
173
174 std::string get_path_for_heifio_test_file(std::string filename)
175 {
176 return libheifio_tests_data_directory + "/" + filename;
177 }
178
179
180 heif_encoder* get_encoder_or_skip_test(heif_compression_format format)
181 {
182 heif_encoder* encoder = nullptr;
183 heif_error err = heif_context_get_encoder_for_format(nullptr, format, &encoder);
184 if (err.code != heif_error_Ok) {
185 if (format == heif_compression_HEVC) {
186 SKIP("Encoder for HEVC not found, skipping test");
187 }
188 else if (format == heif_compression_AV1) {
189 SKIP("Encoder for AV1 not found, skipping test");
190 }
191 else {
192 SKIP("Encoder not found, skipping test");
193 }
194 }
195
196 return encoder;
197 }
198
199
200 fs::path get_tests_output_dir()
201 {
202 if (const char* env_p = std::getenv("LIBHEIF_TEST_OUTPUT_DIR")) {
203 return fs::path(env_p);
204 }
205
206 static const fs::path output_dir = fs::current_path() / "libheif_test_output";
207
208 if (!fs::exists(output_dir)) {
209 fs::create_directories(output_dir);
210 }
211
212 return output_dir;
213 }
214
215
216 std::string get_tests_output_file_path(const char* filename)
217 {
218 fs::path dir = get_tests_output_dir();
219 dir /= filename;
220 return dir.string();
221 }