Debian Codegraph @

tests/conversion.cc

raw
1 /*
2 libheif unit tests
3
4 MIT License
5
6 Copyright (c) 2019 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 <iomanip>
28 #include "catch_amalgamated.hpp"
29 #include "color-conversion/colorconversion.h"
30 #include "pixelimage.h"
31 #include <cmath>
32
33 // Enable for more verbose test output.
34 constexpr bool kEnableDebugOutput = false;
35
36 struct Plane {
37 heif_channel channel;
38 int width;
39 int height;
40 int bit_depth;
41 };
42
43 bool PlatformIsBigEndian() {
44 int i = 1;
45 return !*((char*)&i);
46 }
47
48 bool EndiannessMatchesPlatform(heif_chroma chroma) {
49 if (chroma == heif_chroma_interleaved_RRGGBB_BE ||
50 chroma == heif_chroma_interleaved_RRGGBBAA_BE) {
51 return PlatformIsBigEndian();
52 } else if (chroma == heif_chroma_interleaved_RRGGBB_LE ||
53 chroma == heif_chroma_interleaved_RRGGBBAA_LE) {
54 return !PlatformIsBigEndian();
55 }
56 return true;
57 }
58
59 uint16_t SwapBytes(uint16_t v) {
60 const uint8_t hi = static_cast<uint8_t>((v & 0xff00) >> 8);
61 const uint8_t lo = static_cast<uint8_t>(v & 0x00ff);
62 return (uint16_t)((lo << 8) | hi);
63 }
64
65 uint16_t SwapBytesIfNeeded(uint16_t v, heif_chroma chroma) {
66 return EndiannessMatchesPlatform(chroma) ? v : SwapBytes(v);
67 }
68
69 template <typename T>
70 std::string PrintChannel(const HeifPixelImage& image, heif_channel channel) {
71 heif_chroma chroma = image.get_chroma_format();
72 int num_interleaved = num_interleaved_pixels_per_plane(chroma);
73 bool is_interleaved = num_interleaved > 1;
74 uint32_t max_cols = is_interleaved ? 3 : 10;
75 uint32_t max_rows = 10;
76 uint32_t width = std::min(image.get_width(channel), max_cols);
77 uint32_t height = std::min(image.get_height(channel), max_rows);
78 size_t stride;
79 const T* p = (T*)image.get_plane(channel, &stride);
80 stride /= (int)sizeof(T);
81 int bpp = image.get_bits_per_pixel(channel);
82 int digits = (int)std::ceil(std::log10(1 << bpp)) + 1;
83
84 std::ostringstream os;
85 os << "channel=" << channel << " width=" << width << " height=" << height
86 << " bpp=" << bpp << "\n";
87 os << std::string(digits, ' ');
88 int header_width = digits * num_interleaved - 1 + (is_interleaved ? 3 : 0);
89 for (uint32_t x = 0; x < width; ++x) {
90 os << "|" << std::left << std::setw(header_width) << std::to_string(x);
91 }
92 os << "\n";
93 for (uint32_t y = 0; y < height; ++y) {
94 os << std::left << std::setw(digits) << std::to_string(y) << "|";
95 for (uint32_t x = 0; x < width; ++x) {
96 if (is_interleaved) os << "(";
97 for (int k = 0; k < num_interleaved; ++k) {
98 int v = SwapBytesIfNeeded(p[y * stride + x * num_interleaved + k], chroma);
99 os << std::left << std::setw(digits) << std::to_string(v);
100 }
101 if (is_interleaved) os << ") ";
102 }
103 os << "\n";
104 }
105 return os.str();
106 }
107
108 std::string PrintChannel(const HeifPixelImage& image, heif_channel channel) {
109 if (image.get_bits_per_pixel(channel) <= 8) {
110 return PrintChannel<uint8_t>(image, channel);
111 } else {
112 return PrintChannel<uint16_t>(image, channel);
113 }
114 }
115
116 // Returns the PSNR between 'original' and 'compressed' images.
117 // If expect_alpha_max is true, then alpha values in 'compressed' are expected
118 // to be equal to (1<<bpp)-1 rather than the alpha value in the original image.
119 template <typename T>
120 double GetPsnr(const HeifPixelImage& original, const HeifPixelImage& compressed,
121 heif_channel channel, bool expect_alpha_max) {
122 uint32_t w = original.get_width(channel);
123 uint32_t h = original.get_height(channel);
124 heif_chroma chroma = original.get_chroma_format();
125
126 if (w == 0 || h == 0) {
127 return 0;
128 }
129
130 size_t orig_stride;
131 size_t compressed_stride;
132 const T* orig_p = (T*)original.get_plane(channel, &orig_stride);
133 const T* compressed_p = (T*)compressed.get_plane(channel, &compressed_stride);
134 orig_stride /= (int)sizeof(T);
135 compressed_stride /= (int)sizeof(T);
136 double mse = 0.0;
137
138 int num_interleaved = num_interleaved_pixels_per_plane(chroma);
139 int alpha_max = (1 << original.get_bits_per_pixel(channel)) - 1;
140 CAPTURE(expect_alpha_max);
141 for (uint32_t y = 0; y < h; y++) {
142 for (uint32_t x = 0; x < w * num_interleaved; x++) {
143 int orig_v = SwapBytesIfNeeded(orig_p[y * orig_stride + x], chroma);
144 if (expect_alpha_max && (channel == heif_channel_Alpha ||
145 ((num_interleaved == 4) && x % 4 == 3))) {
146 orig_v = alpha_max;
147 }
148 int compressed_v = SwapBytesIfNeeded(compressed_p[y * compressed_stride + x], chroma);
149 int d = orig_v - compressed_v;
150 mse += d * d;
151 }
152 }
153 mse /= w * h;
154 int max = (1 << original.get_bits_per_pixel(channel)) - 1;
155 double psnr = 10 * log10(max * max / mse);
156 if (psnr < 0.) return 0.;
157 if (psnr > 100.) return 100.;
158 return psnr;
159 }
160
161 double GetPsnr(const HeifPixelImage& original, const HeifPixelImage& compressed,
162 heif_channel channel, bool expect_alpha_max) {
163 if (original.get_bits_per_pixel(channel) <= 8) {
164 return GetPsnr<uint8_t>(original, compressed, channel, expect_alpha_max);
165 } else {
166 return GetPsnr<uint16_t>(original, compressed, channel, expect_alpha_max);
167 }
168 }
169
170 std::vector<Plane> GetPlanes(const ColorState& state, int width, int height) {
171 std::vector<Plane> planes;
172 if (state.colorspace == heif_colorspace_monochrome) {
173 if (state.chroma != heif_chroma_monochrome) return {};
174 planes.push_back({heif_channel_Y, width, height, state.bits_per_pixel});
175 if (state.has_alpha) {
176 planes.push_back(
177 {heif_channel_Alpha, width, height, state.bits_per_pixel});
178 }
179 } else if (state.colorspace == heif_colorspace_YCbCr) {
180 if (state.chroma != heif_chroma_444 && state.chroma != heif_chroma_422 &&
181 state.chroma != heif_chroma_420 && state.chroma != heif_chroma_monochrome) {
182 return {};
183 }
184 planes.push_back({heif_channel_Y, width, height, state.bits_per_pixel});
185 if (state.chroma != heif_chroma_monochrome) {
186 int chroma_width = state.chroma == heif_chroma_444 ? width : width / 2;
187 int chroma_height =
188 state.chroma == heif_chroma_444 || state.chroma == heif_chroma_422
189 ? height
190 : height / 2;
191 planes.push_back(
192 {heif_channel_Cb, chroma_width, chroma_height, state.bits_per_pixel});
193 planes.push_back(
194 {heif_channel_Cr, chroma_width, chroma_height, state.bits_per_pixel});
195 }
196 if (state.has_alpha) {
197 planes.push_back(
198 {heif_channel_Alpha, width, height, state.bits_per_pixel});
199 }
200 } else if (state.colorspace == heif_colorspace_RGB) {
201 if (state.chroma != heif_chroma_444 &&
202 state.chroma != heif_chroma_interleaved_RGB &&
203 state.chroma != heif_chroma_interleaved_RGBA &&
204 state.chroma != heif_chroma_interleaved_RRGGBB_BE &&
205 state.chroma != heif_chroma_interleaved_RRGGBBAA_BE &&
206 state.chroma != heif_chroma_interleaved_RRGGBB_LE &&
207 state.chroma != heif_chroma_interleaved_RRGGBBAA_LE) {
208 return {};
209 }
210 if (state.chroma == heif_chroma_444) {
211 planes.push_back({heif_channel_R, width, height, state.bits_per_pixel});
212 planes.push_back({heif_channel_G, width, height, state.bits_per_pixel});
213 planes.push_back({heif_channel_B, width, height, state.bits_per_pixel});
214 if (state.has_alpha) {
215 planes.push_back(
216 {heif_channel_Alpha, width, height, state.bits_per_pixel});
217 }
218 } else {
219 planes.push_back(
220 {heif_channel_interleaved, width, height, state.bits_per_pixel});
221 }
222 } else {
223 return {}; // Unsupported colorspace.
224 }
225 return planes;
226 }
227
228 bool MakeTestImage(const ColorState& state, int width, int height,
229 HeifPixelImage* image) {
230 image->create(width, height, state.colorspace, state.chroma);
231 image->set_color_profile_nclx(state.nclx);
232 std::vector<Plane> planes = GetPlanes(state, width, height);
233 if (planes.empty()) return false;
234 for (size_t i = 0; i < planes.size(); ++i) {
235 const Plane& plane = planes[i];
236 int half_max = (1 << (plane.bit_depth -1));
237 uint16_t value = SwapBytesIfNeeded(
238 static_cast<uint16_t>(half_max + i * 10 + i), state.chroma);
239 auto err = image->fill_new_plane(plane.channel, value, plane.width, plane.height,
240 plane.bit_depth, nullptr);
241 if (err) {
242 return false;
243 }
244 }
245 return true;
246 }
247
248 static bool NclxMatches(heif_colorspace colorspace,
249 const nclx_profile& src_nclx,
250 const nclx_profile& dst_nclx) {
251 if (colorspace != heif_colorspace_YCbCr) {
252 return true;
253 }
254
255 if (src_nclx.get_full_range_flag() != dst_nclx.get_full_range_flag() ||
256 src_nclx.get_matrix_coefficients() !=
257 dst_nclx.get_matrix_coefficients()) {
258 return false;
259 }
260
261 return true;
262 }
263
264 void nclx_default_if_undefined(ColorState& state)
265 {
266 if (state.nclx.get_colour_primaries() == heif_color_primaries_unspecified) {
267 state.nclx.set_colour_primaries(1);
268 }
269
270 if (state.nclx.get_matrix_coefficients() == heif_matrix_coefficients_unspecified) {
271 state.nclx.set_matrix_coefficients(6);
272 }
273
274 if (state.nclx.get_transfer_characteristics() == heif_transfer_characteristic_unspecified) {
275 state.nclx.set_transfer_characteristics(13);
276 }
277 }
278
279
280 // Converts from 'input_state' to 'target_state' and checks that the resulting
281 // image has the expected shape. If the reverse conversion is also supported,
282 // does a round-trip back to the original state and checks the PSNR.
283 // If 'require_supported' is true, checks that the conversion from 'input_state'
284 // to 'target_state' is supported, otherwise, exists silently for unsupported
285 // conversions.
286 void TestConversion(const std::string& test_name, ColorState input_state,
287 ColorState target_state,
288 const heif_color_conversion_options& options,
289 const heif_color_conversion_options_ext& options_ext,
290 bool require_supported = true) {
291 nclx_default_if_undefined(input_state);
292 nclx_default_if_undefined(target_state);
293
294 INFO(test_name);
295 INFO("downsampling=" << options.preferred_chroma_downsampling_algorithm
296 << " upsampling="
297 << options.preferred_chroma_upsampling_algorithm
298 << " only_used_preferred="
299 << (bool)options.only_use_preferred_chroma_algorithm);
300
301 ColorConversionPipeline pipeline;
302 bool supported = pipeline.construct_pipeline(input_state, target_state, options, options_ext);
303 if (require_supported) REQUIRE(supported);
304 if (!supported) return;
305 INFO("conversion pipeline: " << pipeline.debug_dump_pipeline());
306
307 auto in_image = std::make_shared<HeifPixelImage>();
308 // Width and height are multiples of 4.
309 int width = 12;
310 int height = 8;
311 REQUIRE(MakeTestImage(input_state, width, height, in_image.get()));
312
313 auto out_image_result = pipeline.convert_image(in_image, nullptr);
314 REQUIRE(out_image_result);
315 std::shared_ptr<HeifPixelImage> out_image = *out_image_result;
316
317 REQUIRE(out_image != nullptr);
318 CHECK(out_image->get_colorspace() == target_state.colorspace);
319 CHECK(out_image->get_chroma_format() == target_state.chroma);
320 CHECK(out_image->has_alpha() == target_state.has_alpha);
321 for (const Plane& plane : GetPlanes(target_state, width, height)) {
322 INFO("Channel: " << plane.channel);
323 size_t stride;
324 CHECK(out_image->get_plane(plane.channel, &stride) != nullptr);
325 CHECK(out_image->get_bits_per_pixel(plane.channel) ==
326 target_state.bits_per_pixel);
327 // If an alpha plane was created from nothing, check that it's filled
328 // with the max alpha value.
329 if (plane.channel == heif_channel_Alpha && !input_state.has_alpha) {
330 double alpha_psnr = GetPsnr(*out_image, *out_image, heif_channel_Alpha,
331 /*expect_alpha_max=*/true);
332 REQUIRE(alpha_psnr == 100.f);
333 }
334 }
335
336
337 // Convert back in the other direction (if supported).
338 ColorConversionPipeline reverse_pipeline;
339 if (reverse_pipeline.construct_pipeline(target_state, input_state, options, options_ext)) {
340 INFO("reverse pipeline: " << reverse_pipeline.debug_dump_pipeline());
341 auto recovered_image_result =reverse_pipeline.convert_image(out_image, heif_get_disabled_security_limits());
342 REQUIRE(recovered_image_result);
343 std::shared_ptr<HeifPixelImage> recovered_image = *recovered_image_result;
344 // If the alpha plane was lost in the target state, it should come back
345 // as the max value for the given bpp, i.e. (1<<bpp)-1
346 bool expect_alpha_max = !target_state.has_alpha;
347 bool expect_lossless =
348 input_state.colorspace == target_state.colorspace &&
349 input_state.bits_per_pixel == target_state.bits_per_pixel &&
350 (input_state.chroma == target_state.chroma ||
351 (input_state.chroma != heif_chroma_420 &&
352 input_state.chroma != heif_chroma_422 &&
353 target_state.chroma != heif_chroma_420 &&
354 target_state.chroma != heif_chroma_422)) &&
355 NclxMatches(input_state.colorspace, input_state.nclx,
356 target_state.nclx);
357 double expected_psnr = expect_lossless ? 100. : 38.;
358
359 for (const Plane& plane : GetPlanes(input_state, width, height)) {
360 INFO("Channel: "
361 << plane.channel
362 << " (set kEnableDebugOutput to true in the code for more info)");
363 if (kEnableDebugOutput) {
364 UNSCOPED_INFO("Original:\n" << PrintChannel(*in_image, plane.channel));
365 UNSCOPED_INFO("Recovered:\n"
366 << PrintChannel(*recovered_image, plane.channel));
367 for (const Plane& converted_plane :
368 GetPlanes(target_state, width, height)) {
369 UNSCOPED_INFO("Converted channel "
370 << converted_plane.channel << ":\n"
371 << PrintChannel(*out_image, converted_plane.channel));
372 }
373 }
374 double psnr =
375 GetPsnr(*in_image, *recovered_image, plane.channel, expect_alpha_max);
376 CHECK(psnr >= expected_psnr);
377 }
378 }
379 }
380
381 void TestFailingConversion(const std::string& test_name,
382 const ColorState& input_state,
383 const ColorState& target_state,
384 const heif_color_conversion_options& options,
385 const heif_color_conversion_options_ext& options_ext) {
386 INFO(test_name);
387 INFO("downsampling=" << options.preferred_chroma_downsampling_algorithm
388 << " upsampling="
389 << options.preferred_chroma_upsampling_algorithm
390 << " only_used_preferred="
391 << (bool)options.only_use_preferred_chroma_algorithm);
392 ColorConversionPipeline pipeline;
393 bool construct_pipeline_res =
394 pipeline.construct_pipeline(input_state, target_state, options, options_ext);
395 INFO("conversion pipeline: " << pipeline.debug_dump_pipeline());
396 REQUIRE_FALSE(construct_pipeline_res);
397 }
398
399 // Returns the list of valid has_alpha values for a given heif_chroma.
400 std::vector<bool> GetValidHasAlpha(heif_chroma chroma) {
401 switch (chroma) {
402 case heif_chroma_monochrome:
403 case heif_chroma_420:
404 case heif_chroma_422:
405 case heif_chroma_444:
406 return {false, true};
407 case heif_chroma_interleaved_RGB:
408 case heif_chroma_interleaved_RRGGBB_BE:
409 case heif_chroma_interleaved_RRGGBB_LE:
410 return {false};
411 case heif_chroma_interleaved_RGBA:
412 case heif_chroma_interleaved_RRGGBBAA_BE:
413 case heif_chroma_interleaved_RRGGBBAA_LE:
414 return {true};
415 default:
416 return {};
417 }
418 }
419
420 // Returns some valid bits per pixels values for a given heif_chroma.
421 std::vector<int> GetValidBitsPerPixel(heif_chroma chroma) {
422 const std::vector<int> sdr = {8};
423 const std::vector<int> hdr = {12};
424 const std::vector<int> both = {8, 12};
425 switch (chroma) {
426 case heif_chroma_monochrome:
427 case heif_chroma_420:
428 case heif_chroma_422:
429 case heif_chroma_444:
430 return both;
431 case heif_chroma_interleaved_RGB:
432 case heif_chroma_interleaved_RGBA:
433 return sdr;
434 case heif_chroma_interleaved_RRGGBB_BE:
435 case heif_chroma_interleaved_RRGGBB_LE:
436 case heif_chroma_interleaved_RRGGBBAA_BE:
437 case heif_chroma_interleaved_RRGGBBAA_LE:
438 return hdr;
439 default:
440 return {};
441 }
442 }
443
444 // Returns a subset (to reduce the number of tests) of fully supported matrix
445 // coefficients.
446 const std::vector<heif_matrix_coefficients> GetSupportedMatrices() {
447 return {heif_matrix_coefficients_RGB_GBR,
448 heif_matrix_coefficients_SMPTE_240M};
449 }
450
451 // Returns matrix coefficients that are not currently supported by any operator.
452 const std::vector<heif_matrix_coefficients> GetUnsupportedMatrices() {
453 return {heif_matrix_coefficients_SMPTE_ST_2085,
454 heif_matrix_coefficients_ICtCp};
455 }
456
457 // Returns of list of all valid ColorState (valid combinations
458 // of a heif_colorspace/heif_chroma/has_alpha/bpp).
459 std::vector<ColorState> GetAllColorStates(const std::vector<heif_matrix_coefficients>& matrices) {
460 std::vector<ColorState> color_states;
461 for (heif_colorspace colorspace : {heif_colorspace_YCbCr, heif_colorspace_RGB, heif_colorspace_monochrome}) {
462 for (heif_chroma chroma : get_valid_chroma_values_for_colorspace(colorspace)) {
463 for (bool has_alpha : GetValidHasAlpha(chroma)) {
464 for (int bits_per_pixel : GetValidBitsPerPixel(chroma)) {
465 // Without nclx.
466 ColorState color_state(colorspace, chroma, has_alpha, bits_per_pixel);
467 color_states.push_back(color_state);
468
469 // With nclx.
470 if (colorspace == heif_colorspace_YCbCr) {
471 for (heif_matrix_coefficients matrix : matrices) {
472 for (bool full_range : {true, false}) {
473 color_state.nclx.set_full_range_flag(full_range);
474 color_state.nclx.set_matrix_coefficients(matrix);
475 color_state.nclx.set_colour_primaries(
476 heif_color_primaries_ITU_R_BT_709_5);
477 color_state.nclx.set_transfer_characteristics(
478 heif_color_primaries_SMPTE_240M);
479 color_states.push_back(color_state);
480 }
481 }
482 }
483 }
484 }
485 }
486 }
487 return color_states;
488 }
489
490 TEST_CASE("All conversions", "[heif_image]") {
491 bool only_use_preferred_chroma_algorithm = GENERATE(false, true);
492 heif_chroma_downsampling_algorithm downsampling =
493 heif_chroma_downsampling_nearest_neighbor;
494 heif_chroma_upsampling_algorithm upsampling =
495 heif_chroma_upsampling_nearest_neighbor;
496 if (only_use_preferred_chroma_algorithm) {
497 downsampling = GENERATE(heif_chroma_downsampling_nearest_neighbor,
498 heif_chroma_downsampling_average,
499 heif_chroma_downsampling_sharp_yuv);
500 upsampling = GENERATE(heif_chroma_upsampling_nearest_neighbor,
501 heif_chroma_upsampling_bilinear);
502 }
503 heif_color_conversion_options options = {
504 .preferred_chroma_downsampling_algorithm = downsampling,
505 .preferred_chroma_upsampling_algorithm = upsampling,
506 .only_use_preferred_chroma_algorithm = only_use_preferred_chroma_algorithm};
507
508 heif_color_conversion_options_ext options_ext = {
509 .alpha_composition_mode = heif_alpha_composition_mode_none
510 };
511
512 // Test all source and destination state combinations.
513 ColorState src_state = GENERATE(
514 from_range(GetAllColorStates(GetSupportedMatrices())));
515 ColorState dst_state = GENERATE(
516 from_range(GetAllColorStates(GetSupportedMatrices())));
517 // To debug a particular combination, hardcode the ColorState values
518 // instead:
519 // ColorState src_state(heif_colorspace_YCbCr, heif_chroma_420, false, 8);
520 // src_state.nclx_profile.set_matrix_coefficients(...);
521 // ColorState dst_state(...);
522
523 bool require_supported = true;
524 // Converting to monochrome is not supported at the moment.
525 if (dst_state.colorspace == heif_colorspace_monochrome ||
526 dst_state.chroma == heif_chroma_monochrome) {
527 require_supported = false;
528 }
529 // Some conversions might not be supported when the exact upsampling
530 // or downsampling algorithm is specified.
531 if (only_use_preferred_chroma_algorithm) {
532 require_supported = false;
533 }
534
535 std::ostringstream os;
536 os << "from: " << src_state << "\nto: " << dst_state;
537 TestConversion(os.str(), src_state, dst_state, options, options_ext, require_supported);
538 }
539
540 TEST_CASE("Unsupported matrices", "[heif_image]") {
541 bool only_use_preferred_chroma_algorithm = GENERATE(false, true);
542 heif_chroma_downsampling_algorithm downsampling =
543 heif_chroma_downsampling_nearest_neighbor;
544 heif_chroma_upsampling_algorithm upsampling =
545 heif_chroma_upsampling_nearest_neighbor;
546 if (only_use_preferred_chroma_algorithm) {
547 downsampling = GENERATE(heif_chroma_downsampling_nearest_neighbor,
548 heif_chroma_downsampling_average,
549 heif_chroma_downsampling_sharp_yuv);
550 upsampling = GENERATE(heif_chroma_upsampling_nearest_neighbor,
551 heif_chroma_upsampling_bilinear);
552 }
553 heif_color_conversion_options options = {
554 .preferred_chroma_downsampling_algorithm = downsampling,
555 .preferred_chroma_upsampling_algorithm = upsampling,
556 .only_use_preferred_chroma_algorithm = only_use_preferred_chroma_algorithm};
557
558 heif_color_conversion_options_ext options_ext = {
559 .alpha_composition_mode = heif_alpha_composition_mode_none
560 };
561
562 ColorState src_state =
563 GENERATE(from_range(GetAllColorStates(GetUnsupportedMatrices())));
564 ColorState dst_state =
565 GENERATE(from_range(GetAllColorStates(GetUnsupportedMatrices())));
566 nclx_profile default_nclx = nclx_profile::defaults();
567
568 if (src_state == dst_state ||
569 NclxMatches(src_state.colorspace, src_state.nclx,
570 dst_state.nclx) ||
571 (src_state.nclx.get_matrix_coefficients() == default_nclx.get_matrix_coefficients() ||
572 dst_state.nclx.get_matrix_coefficients() == default_nclx.get_matrix_coefficients())) {
573 return;
574 }
575
576 std::ostringstream os;
577 os << "from: " << src_state << "\nto: " << dst_state;
578 TestFailingConversion(os.str(), src_state, dst_state, options, options_ext);
579 }
580
581 TEST_CASE("Sharp yuv conversion", "[heif_image]") {
582 heif_color_conversion_options sharp_yuv_options{
583 .preferred_chroma_downsampling_algorithm =
584 heif_chroma_downsampling_sharp_yuv,
585 .preferred_chroma_upsampling_algorithm = heif_chroma_upsampling_bilinear,
586 .only_use_preferred_chroma_algorithm = true};
587
588 heif_color_conversion_options_ext options_ext = {
589 .alpha_composition_mode = heif_alpha_composition_mode_none
590 };
591
592 #ifdef HAVE_LIBSHARPYUV
593 TestConversion("### interleaved RGBA -> YCbCr 420 with sharp yuv",
594 {heif_colorspace_RGB, heif_chroma_interleaved_RGBA, true, 8},
595 {heif_colorspace_YCbCr, heif_chroma_420, true, 8},
596 sharp_yuv_options, options_ext);
597 TestConversion("### interleaved RGB 10bit -> YCbCr 420 10bit with sharp yuv",
598 {heif_colorspace_RGB, heif_chroma_interleaved_RGB, false, 8},
599 {heif_colorspace_YCbCr, heif_chroma_420, false, 8},
600 sharp_yuv_options, options_ext);
601
602 TestConversion("### interleaved RGBA 12bit big endian -> YCbCr 420 12bit with sharp yuv",
603 {heif_colorspace_RGB, heif_chroma_interleaved_RRGGBBAA_BE, true, 12},
604 {heif_colorspace_YCbCr, heif_chroma_420, true, 12},
605 sharp_yuv_options, options_ext);
606 TestConversion("### interleaved RGBA 12bit little endian -> YCbCr 420 12bit with sharp yuv",
607 {heif_colorspace_RGB, heif_chroma_interleaved_RRGGBBAA_LE, true, 12},
608 {heif_colorspace_YCbCr, heif_chroma_420, true, 12},
609 sharp_yuv_options, options_ext);
610 TestConversion("### planar RGB -> YCbCr 420 with sharp yuv",
611 {heif_colorspace_RGB, heif_chroma_444, false, 8},
612 {heif_colorspace_YCbCr, heif_chroma_420, false, 8},
613 sharp_yuv_options, options_ext);
614 TestConversion("### planar RGBA -> YCbCr 420 with sharp yuv",
615 {heif_colorspace_RGB, heif_chroma_444, true, 8},
616 {heif_colorspace_YCbCr, heif_chroma_420, true, 8},
617 sharp_yuv_options, options_ext);
618 TestConversion("### planar RGB 10bit -> YCbCr 420 10bit with sharp yuv",
619 {heif_colorspace_RGB, heif_chroma_444, false, 10},
620 {heif_colorspace_YCbCr, heif_chroma_420, false, 10},
621 sharp_yuv_options, options_ext);
622 TestConversion("### planar RGBA 10bit -> YCbCr 420 10bit with sharp yuv",
623 {heif_colorspace_RGB, heif_chroma_444, true, 10},
624 {heif_colorspace_YCbCr, heif_chroma_420, true, 10},
625 sharp_yuv_options, options_ext);
626 #else
627 // Should fail if libsharpyuv is not compiled in.
628 TestFailingConversion(
629 "### interleaved RGBA -> YCbCr 420 with sharp yuv NOT COMPILED IN",
630 {heif_colorspace_RGB, heif_chroma_interleaved_RGBA, true, 8},
631 {heif_colorspace_YCbCr, heif_chroma_420, false, 8}, sharp_yuv_options, options_ext);
632 WARN("Tests built without sharp yuv");
633 #endif
634
635 TestFailingConversion(
636 "### interleaved RGBA -> YCbCr 422 with sharp yuv (not supported!)",
637 {heif_colorspace_RGB, heif_chroma_interleaved_RGBA, true, 8},
638 {heif_colorspace_YCbCr, heif_chroma_422, false, 8}, sharp_yuv_options, options_ext);
639 }
640
641
642 static void fill_plane(std::shared_ptr<HeifPixelImage>& img, heif_channel channel, int w, int h, const std::vector<uint8_t>& pixels)
643 {
644 auto error = img->add_plane(channel, w, h, 8, nullptr);
645 REQUIRE(!error);
646
647 size_t stride;
648 uint8_t* p = img->get_plane(channel, &stride);
649
650 for (int y = 0; y < h; y++) {
651 for (int x = 0; x < w; x++) {
652 p[y * stride + x] = pixels[y * w + x];
653 }
654 }
655 }
656
657
658 static void assert_plane(std::shared_ptr<HeifPixelImage>& img, heif_channel channel, const std::vector<uint8_t>& pixels)
659 {
660 INFO("channel: " << channel);
661 uint32_t w = img->get_width(channel);
662 uint32_t h = img->get_height(channel);
663
664 size_t stride;
665 uint8_t* p = img->get_plane(channel, &stride);
666
667 for (uint32_t y = 0; y < h; y++) {
668 INFO("row: " << y);
669 for (uint32_t x = 0; x < w; x++) {
670 INFO("column: " << x);
671 REQUIRE((int)p[y * stride + x] == (int)pixels[y * w + x]);
672 }
673 }
674 }
675
676
677 TEST_CASE("Bilinear upsampling", "[heif_image]")
678 {
679 heif_color_conversion_options options = {
680 .preferred_chroma_upsampling_algorithm = heif_chroma_upsampling_bilinear,
681 .only_use_preferred_chroma_algorithm = true};
682
683 std::shared_ptr<HeifPixelImage> img = std::make_shared<HeifPixelImage>();
684 img->create(4, 4, heif_colorspace_YCbCr, heif_chroma_420);
685
686 auto error = img->fill_new_plane(heif_channel_Y, 128, 4,4, 8, nullptr);
687 REQUIRE(!error);
688
689 fill_plane(img, heif_channel_Cb, 2,2,
690 {10, 40,
691 100, 240});
692 fill_plane(img, heif_channel_Cr, 2, 2,
693 {255, 200,
694 50, 0});
695
696 auto conversionResult = convert_colorspace(img, heif_colorspace_YCbCr, heif_chroma_444,
697 nclx_profile::defaults(), 8, options, nullptr, heif_get_disabled_security_limits());
698 REQUIRE(conversionResult);
699 std::shared_ptr<HeifPixelImage> out = *conversionResult;
700
701 assert_plane(out, heif_channel_Cb,
702 {
703 10, 18, 33, 40,
704 33, 47, 76, 90,
705 78, 106, 162, 190,
706 100, 135, 205, 240
707 });
708
709
710 assert_plane(out, heif_channel_Cr,
711 {
712 255, 241, 214, 200,
713 204, 190, 163, 150,
714 101, 88, 63, 50,
715 50, 38, 13, 0
716 });
717 }
718
719 TEST_CASE("RGB 5-6-5 to RGB")
720 {
721 heif_color_conversion_options options = {};
722
723 std::shared_ptr<HeifPixelImage> img = std::make_shared<HeifPixelImage>();
724 const uint32_t width = 3;
725 const uint32_t height = 2;
726 img->create(width, height, heif_colorspace_RGB, heif_chroma_444);
727 Error err;
728 err = img->add_plane(heif_channel_R, width, height, 5, heif_get_disabled_security_limits());
729 REQUIRE(!err);
730 REQUIRE(img->get_bits_per_pixel(heif_channel_R) == 5);
731 err = img->add_plane(heif_channel_G, width, height, 6, heif_get_disabled_security_limits());
732 REQUIRE(!err);
733 REQUIRE(img->get_bits_per_pixel(heif_channel_G) == 6);
734 err = img->add_plane(heif_channel_B, width, height, 5, heif_get_disabled_security_limits());
735 REQUIRE(!err);
736 REQUIRE(img->get_bits_per_pixel(heif_channel_B) == 5);
737
738 uint8_t v = 1;
739 for (heif_channel plane: {heif_channel_R, heif_channel_G, heif_channel_B}) {
740 size_t dst_stride = 0;
741 uint8_t *dst = img->get_plane(plane, &dst_stride);
742 for (uint32_t y = 0; y < height; y++) {
743 for (uint32_t x = 0; x < width; x++) {
744 dst[y * dst_stride + x] = v;
745 v++;
746 }
747 }
748 }
749
750 auto conversionResult = convert_colorspace(img, heif_colorspace_RGB, heif_chroma_444,
751 nclx_profile::defaults(), 8, options, nullptr, heif_get_disabled_security_limits());
752 REQUIRE(conversionResult);
753 std::shared_ptr<HeifPixelImage> out = *conversionResult;
754
755 assert_plane(out, heif_channel_R, {8, 16, 24, 33, 41, 49});
756 assert_plane(out, heif_channel_G, {28, 32, 36, 40, 44, 48});
757 assert_plane(out, heif_channel_B, {107, 115, 123, 132, 140, 148});
758 }