Debian Codegraph @

tests/encode.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 "catch_amalgamated.hpp"
28 #include "libheif/heif.h"
29 #include "pixelimage.h"
30 #include "api_structs.h"
31 #include "test_utils.h"
32
33 #include <string.h>
34
35
36 heif_image* createImage_RRGGBB_BE() {
37 heif_image* image;
38 heif_error err;
39 err = heif_image_create(256,256,
40 heif_colorspace_RGB,
41 heif_chroma_interleaved_RRGGBB_BE,
42 &image);
43 if (err.code) {
44 return nullptr;
45 }
46
47 err = heif_image_add_plane(image,
48 heif_channel_interleaved,
49 256,256, 10);
50 if (err.code) {
51 heif_image_release(image);
52 return nullptr;
53 }
54
55 return image;
56 }
57
58
59 heif_error encode_image(heif_image* img) {
60 heif_context* ctx = heif_context_alloc();
61
62 heif_encoder* enc = nullptr;
63 heif_error err { heif_error_Ok };
64
65 enc = get_encoder_or_skip_test(heif_compression_HEVC);
66
67 heif_image_handle* hdl;
68 err = heif_context_encode_image(ctx,
69 img,
70 enc,
71 nullptr,
72 &hdl);
73 if (err.code) {
74 heif_encoder_release(enc);
75 heif_context_free(ctx);
76 return err;
77 }
78
79 heif_encoder_release(enc);
80 heif_image_handle_release(hdl);
81 heif_context_free(ctx);
82 return err;
83 }
84
85
86 #if 0
87 TEST_CASE( "Create images", "[heif_image]" ) {
88 auto img = createImage_RRGGBB_BE();
89 REQUIRE( img != nullptr );
90
91 heif_image_release(img);
92 }
93
94
95
96 TEST_CASE( "Encode HDR", "[heif_encoder]" ) {
97 auto img = createImage_RRGGBB_BE();
98 REQUIRE( img != nullptr );
99
100 REQUIRE( encode_image(img).code == heif_error_Ok );
101
102 heif_image_release(img);
103 }
104 #endif
105
106 static void test_ispe_size(heif_compression_format compression,
107 heif_orientation orientation,
108 int input_width, int input_height,
109 int expected_minimum_ispe_width, int expected_minimum_ispe_height)
110 {
111 heif_image* img;
112 heif_image_create(input_width,input_height, heif_colorspace_YCbCr, heif_chroma_420, &img);
113 fill_new_plane(img, heif_channel_Y, input_width, input_height);
114 fill_new_plane(img, heif_channel_Cb, (input_width+1)/2, (input_height+1)/2);
115 fill_new_plane(img, heif_channel_Cr, (input_width+1)/2, (input_height+1)/2);
116
117 heif_context* ctx = heif_context_alloc();
118 heif_encoder* enc = get_encoder_or_skip_test(compression);
119
120 heif_encoding_options* options;
121 options = heif_encoding_options_alloc();
122 options->macOS_compatibility_workaround = false;
123 options->macOS_compatibility_workaround_no_nclx_profile = false;
124 options->image_orientation = orientation;
125
126 heif_image_handle* handle;
127 heif_error err = heif_context_encode_image(ctx, img, enc, options, &handle);
128 UNSCOPED_INFO("heif_context_encode_image: " << err.message);
129 REQUIRE(err.code == heif_error_Ok);
130
131 int ispe_width = heif_image_handle_get_ispe_width(handle);
132 int ispe_height = heif_image_handle_get_ispe_height(handle);
133
134 REQUIRE(ispe_width >= expected_minimum_ispe_width);
135 REQUIRE(ispe_height >= expected_minimum_ispe_height);
136
137 heif_image_handle_release(handle);
138 heif_encoder_release(enc);
139 heif_encoding_options_free(options);
140 heif_context_free(ctx);
141 heif_image_release(img);
142 }
143
144
145 TEST_CASE( "ispe odd size", "[heif_context]" ) {
146
147 // HEVC encoders typically encode with even dimensions only
148 test_ispe_size(heif_compression_HEVC, heif_orientation_normal, 121,99, 122,100);
149 test_ispe_size(heif_compression_HEVC, heif_orientation_rotate_180, 121,99, 122,100);
150 test_ispe_size(heif_compression_HEVC, heif_orientation_rotate_90_cw, 121,99, 122,100);
151 test_ispe_size(heif_compression_HEVC, heif_orientation_rotate_90_cw, 120,100, 120,100);
152
153 // AVIF encoders typically encode with odd dimensions
154 test_ispe_size(heif_compression_AV1, heif_orientation_normal, 121,99, 121,99);
155 test_ispe_size(heif_compression_AV1, heif_orientation_rotate_180, 121,99, 121,99);
156 test_ispe_size(heif_compression_AV1, heif_orientation_rotate_90_cw, 121,99, 121,99);
157 test_ispe_size(heif_compression_AV1, heif_orientation_rotate_90_cw, 120,100, 120,100);
158 }