| 1 |
/* |
| 2 |
libheif integration tests for regions |
| 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 "api_structs.h" |
| 29 |
#include "libheif/heif.h" |
| 30 |
#include "test-config.h" |
| 31 |
#include "test_utils.h" |
| 32 |
#include <cstddef> |
| 33 |
#include <cstdint> |
| 34 |
#include <cstring> |
| 35 |
|
| 36 |
|
| 37 |
TEST_CASE("no regions") { |
| 38 |
// skip test if we do not have the uncompressed codec |
| 39 |
if (!heif_have_decoder_for_format(heif_compression_uncompressed)) { |
| 40 |
SKIP("Skipping test because uncompressed codec is not compiled."); |
| 41 |
} |
| 42 |
|
| 43 |
auto context = get_context_for_test_file("uncompressed_comp_RGB.heif"); |
| 44 |
heif_image_handle *handle = get_primary_image_handle(context); |
| 45 |
int num_region_items = heif_image_handle_get_number_of_region_items(handle); |
| 46 |
REQUIRE(num_region_items == 0); |
| 47 |
heif_image_handle_release(handle); |
| 48 |
heif_context_free(context); |
| 49 |
} |
| 50 |
|
| 51 |
TEST_CASE("create regions") { |
| 52 |
struct heif_error err; |
| 53 |
|
| 54 |
heif_image* img; |
| 55 |
uint32_t input_width = 1280; |
| 56 |
uint32_t input_height = 1024; |
| 57 |
heif_image_create(input_width,input_height, heif_colorspace_YCbCr, heif_chroma_420, &img); |
| 58 |
fill_new_plane(img, heif_channel_Y, input_width, input_height); |
| 59 |
fill_new_plane(img, heif_channel_Cb, (input_width+1)/2, (input_height+1)/2); |
| 60 |
fill_new_plane(img, heif_channel_Cr, (input_width+1)/2, (input_height+1)/2); |
| 61 |
|
| 62 |
heif_context* ctx = heif_context_alloc(); |
| 63 |
heif_encoder* enc; |
| 64 |
err = heif_context_get_encoder_for_format(ctx, heif_compression_AV1, &enc); |
| 65 |
REQUIRE(err.code == heif_error_Ok); |
| 66 |
|
| 67 |
struct heif_encoding_options* options; |
| 68 |
options = heif_encoding_options_alloc(); |
| 69 |
options->macOS_compatibility_workaround = false; |
| 70 |
options->macOS_compatibility_workaround_no_nclx_profile = false; |
| 71 |
options->image_orientation = heif_orientation_normal; |
| 72 |
|
| 73 |
heif_image_handle* handle; |
| 74 |
err = heif_context_encode_image(ctx, img, enc, options, &handle); |
| 75 |
REQUIRE(err.code == heif_error_Ok); |
| 76 |
|
| 77 |
struct heif_region_item* region_item1; |
| 78 |
err = heif_image_handle_add_region_item(handle, input_width, input_height, ®ion_item1); |
| 79 |
REQUIRE(err.code == heif_error_Ok); |
| 80 |
struct heif_region *regionA; |
| 81 |
err = heif_region_item_add_region_point(region_item1, 100, 200, ®ionA); |
| 82 |
REQUIRE(err.code == heif_error_Ok); |
| 83 |
struct heif_region *regionB; |
| 84 |
err = heif_region_item_add_region_rectangle(region_item1, 150, 250, 30, 50, ®ionB); |
| 85 |
REQUIRE(err.code == heif_error_Ok); |
| 86 |
int32_t polylinePoints[6] = {10, 20, 15, 20, 15, 50}; |
| 87 |
int nPolylinePoints = (sizeof(polylinePoints)/sizeof(polylinePoints[0])) / 2; |
| 88 |
err = heif_region_item_add_region_polyline(region_item1, polylinePoints, nPolylinePoints, NULL); |
| 89 |
REQUIRE(err.code == heif_error_Ok); |
| 90 |
|
| 91 |
struct heif_region_item* region_item2; |
| 92 |
err = heif_image_handle_add_region_item(handle, input_width, input_height, ®ion_item2); |
| 93 |
REQUIRE(err.code == heif_error_Ok); |
| 94 |
struct heif_region *regionC; |
| 95 |
err = heif_region_item_add_region_ellipse(region_item2, 350, 450, 60, 80, ®ionC); |
| 96 |
REQUIRE(err.code == heif_error_Ok); |
| 97 |
err = heif_region_item_add_region_point(region_item2, 360, 460, NULL); |
| 98 |
REQUIRE(err.code == heif_error_Ok); |
| 99 |
err = heif_region_item_add_region_rectangle(region_item2, 370, 420, 10, 16, NULL); |
| 100 |
REQUIRE(err.code == heif_error_Ok); |
| 101 |
int32_t polygonPoints[6] = {100, 120, 115, 120, 125, 150}; |
| 102 |
int nPolygonPoints = (sizeof(polygonPoints)/sizeof(polygonPoints[0])) / 2; |
| 103 |
err = heif_region_item_add_region_polygon(region_item2, polygonPoints, nPolygonPoints, NULL); |
| 104 |
REQUIRE(err.code == heif_error_Ok); |
| 105 |
|
| 106 |
err = heif_context_write_to_file(ctx, "regions.heif"); |
| 107 |
REQUIRE(err.code == heif_error_Ok); |
| 108 |
|
| 109 |
heif_region_release(regionA); |
| 110 |
heif_region_release(regionB); |
| 111 |
heif_region_release(regionC); |
| 112 |
heif_region_item_release(region_item1); |
| 113 |
heif_region_item_release(region_item2); |
| 114 |
|
| 115 |
heif_image_handle_release(handle); |
| 116 |
heif_encoder_release(enc); |
| 117 |
heif_encoding_options_free(options); |
| 118 |
heif_context_free(ctx); |
| 119 |
heif_image_release(img); |
| 120 |
|
| 121 |
heif_context *readbackCtx = get_context_for_local_file("regions.heif"); |
| 122 |
heif_image_handle *readbackHandle = get_primary_image_handle(readbackCtx); |
| 123 |
int num_region_items = heif_image_handle_get_number_of_region_items(readbackHandle); |
| 124 |
REQUIRE(num_region_items == 2); |
| 125 |
std::vector<heif_item_id> region_item_ids_minus(1); |
| 126 |
int num_returned = heif_image_handle_get_list_of_region_item_ids(readbackHandle, region_item_ids_minus.data(), 1); |
| 127 |
REQUIRE(num_returned == 1); |
| 128 |
std::vector<heif_item_id> region_item_ids_extra(3); |
| 129 |
num_returned = heif_image_handle_get_list_of_region_item_ids(readbackHandle, region_item_ids_extra.data(), 3); |
| 130 |
REQUIRE(num_returned == num_region_items); |
| 131 |
std::vector<heif_item_id> region_item_ids(num_region_items); |
| 132 |
num_returned = heif_image_handle_get_list_of_region_item_ids(readbackHandle, region_item_ids.data(), num_region_items); |
| 133 |
REQUIRE(num_returned == 2); |
| 134 |
heif_region_item* in_region_1; |
| 135 |
err = heif_context_get_region_item(readbackCtx, region_item_ids[0], &in_region_1); |
| 136 |
heif_item_id id1 = heif_region_item_get_id(in_region_1); |
| 137 |
REQUIRE(id1 == region_item_ids[0]); |
| 138 |
uint32_t width1; |
| 139 |
uint32_t height1; |
| 140 |
heif_region_item_get_reference_size(in_region_1, &width1, &height1); |
| 141 |
REQUIRE(width1 == input_width); |
| 142 |
REQUIRE(height1 == input_height); |
| 143 |
int num_regions_region_item1 = heif_region_item_get_number_of_regions(in_region_1); |
| 144 |
REQUIRE(num_regions_region_item1 == 3); |
| 145 |
std::vector<heif_region*> out_regions_1(num_regions_region_item1); |
| 146 |
int num_regions1 = heif_region_item_get_list_of_regions(in_region_1, out_regions_1.data(), (int)(out_regions_1.size())); |
| 147 |
REQUIRE(num_regions1 == num_regions_region_item1); |
| 148 |
REQUIRE(heif_region_get_type(out_regions_1[0]) == heif_region_type_point); |
| 149 |
int32_t x, y; |
| 150 |
err = heif_region_get_point(out_regions_1[0], &x, &y); |
| 151 |
REQUIRE (err.code == heif_error_Ok); |
| 152 |
REQUIRE(x == 100); |
| 153 |
REQUIRE(y == 200); |
| 154 |
REQUIRE(heif_region_get_type(out_regions_1[1]) == heif_region_type_rectangle); |
| 155 |
uint32_t width, height; |
| 156 |
err = heif_region_get_rectangle(out_regions_1[1], &x, &y, &width, &height); |
| 157 |
REQUIRE (err.code == heif_error_Ok); |
| 158 |
REQUIRE(x == 150); |
| 159 |
REQUIRE(y == 250); |
| 160 |
REQUIRE(width == 30); |
| 161 |
REQUIRE(height == 50); |
| 162 |
REQUIRE(heif_region_get_type(out_regions_1[2]) == heif_region_type_polyline); |
| 163 |
int num_polyline_points_out = heif_region_get_polyline_num_points(out_regions_1[2]); |
| 164 |
REQUIRE(num_polyline_points_out == 3); |
| 165 |
std::vector<int32_t> polyline(num_polyline_points_out * 2); |
| 166 |
err = heif_region_get_polyline_points(out_regions_1[2], polyline.data()); |
| 167 |
REQUIRE(err.code == heif_error_Ok); |
| 168 |
REQUIRE(polyline[0] == 10); |
| 169 |
REQUIRE(polyline[1] == 20); |
| 170 |
REQUIRE(polyline[2] == 15); |
| 171 |
REQUIRE(polyline[3] == 20); |
| 172 |
REQUIRE(polyline[4] == 15); |
| 173 |
REQUIRE(polyline[5] == 50); |
| 174 |
heif_region_release_many(out_regions_1.data(), (int)(out_regions_1.size())); |
| 175 |
|
| 176 |
heif_region_item* in_region_2; |
| 177 |
err = heif_context_get_region_item(readbackCtx, region_item_ids[1], &in_region_2); |
| 178 |
heif_item_id id2 = heif_region_item_get_id(in_region_2); |
| 179 |
REQUIRE(id2 == region_item_ids[1]); |
| 180 |
uint32_t width2; |
| 181 |
uint32_t height2; |
| 182 |
heif_region_item_get_reference_size(in_region_2, &width2, &height2); |
| 183 |
REQUIRE(width2 == input_width); |
| 184 |
REQUIRE(height2 == input_height); |
| 185 |
int num_regions_region_item2 = heif_region_item_get_number_of_regions(in_region_2); |
| 186 |
REQUIRE(num_regions_region_item2 == 4); |
| 187 |
std::vector<heif_region*> out_regions_2(num_regions_region_item2); |
| 188 |
int num_regions2 = heif_region_item_get_list_of_regions(in_region_2, out_regions_2.data(), (int)(out_regions_2.size())); |
| 189 |
REQUIRE(num_regions2 == num_regions_region_item2); |
| 190 |
REQUIRE(heif_region_get_type(out_regions_2[0]) == heif_region_type_ellipse); |
| 191 |
uint32_t radius_x, radius_y; |
| 192 |
err = heif_region_get_ellipse(out_regions_2[0], &x, &y, &radius_x, &radius_y); |
| 193 |
REQUIRE (err.code == heif_error_Ok); |
| 194 |
REQUIRE(x == 350); |
| 195 |
REQUIRE(y == 450); |
| 196 |
REQUIRE(radius_x == 60); |
| 197 |
REQUIRE(radius_y == 80); |
| 198 |
heif_region_release(out_regions_2[0]); |
| 199 |
REQUIRE(heif_region_get_type(out_regions_2[1]) == heif_region_type_point); |
| 200 |
err = heif_region_get_point(out_regions_2[1], &x, &y); |
| 201 |
REQUIRE (err.code == heif_error_Ok); |
| 202 |
REQUIRE(x == 360); |
| 203 |
REQUIRE(y == 460); |
| 204 |
heif_region_release(out_regions_2[1]); |
| 205 |
REQUIRE(heif_region_get_type(out_regions_2[2]) == heif_region_type_rectangle); |
| 206 |
err = heif_region_get_rectangle(out_regions_2[2], &x, &y, &width, &height); |
| 207 |
REQUIRE (err.code == heif_error_Ok); |
| 208 |
REQUIRE(x == 370); |
| 209 |
REQUIRE(y == 420); |
| 210 |
REQUIRE(width == 10); |
| 211 |
REQUIRE(height == 16); |
| 212 |
heif_region_release(out_regions_2[2]); |
| 213 |
REQUIRE(heif_region_get_type(out_regions_2[3]) == heif_region_type_polygon); |
| 214 |
int num_polygon_points_out = heif_region_get_polygon_num_points(out_regions_2[3]); |
| 215 |
REQUIRE(num_polygon_points_out == 3); |
| 216 |
std::vector<int32_t> polygon(num_polygon_points_out * 2); |
| 217 |
err = heif_region_get_polygon_points(out_regions_2[3], polygon.data()); |
| 218 |
REQUIRE(err.code == heif_error_Ok); |
| 219 |
REQUIRE(polygon[0] == 100); |
| 220 |
REQUIRE(polygon[1] == 120); |
| 221 |
REQUIRE(polygon[2] == 115); |
| 222 |
REQUIRE(polygon[3] == 120); |
| 223 |
REQUIRE(polygon[4] == 125); |
| 224 |
REQUIRE(polygon[5] == 150); |
| 225 |
heif_region_release(out_regions_2[3]); |
| 226 |
|
| 227 |
heif_region_item_release(in_region_1); |
| 228 |
heif_region_item_release(in_region_2); |
| 229 |
heif_image_handle_release(readbackHandle); |
| 230 |
heif_context_free(readbackCtx); |
| 231 |
} |
| 232 |
|
| 233 |
TEST_CASE("create mask region") { |
| 234 |
struct heif_error err; |
| 235 |
|
| 236 |
heif_image* img; |
| 237 |
uint32_t input_width = 1280; |
| 238 |
uint32_t input_height = 1024; |
| 239 |
heif_image_create(input_width, input_height, heif_colorspace_YCbCr, |
| 240 |
heif_chroma_420, &img); |
| 241 |
fill_new_plane(img, heif_channel_Y, input_width, input_height); |
| 242 |
fill_new_plane(img, heif_channel_Cb, (input_width + 1) / 2, |
| 243 |
(input_height + 1) / 2); |
| 244 |
fill_new_plane(img, heif_channel_Cr, (input_width + 1) / 2, |
| 245 |
(input_height + 1) / 2); |
| 246 |
|
| 247 |
heif_context *ctx = heif_context_alloc(); |
| 248 |
heif_encoder *enc; |
| 249 |
err = heif_context_get_encoder_for_format(ctx, heif_compression_AV1, &enc); |
| 250 |
REQUIRE(err.code == heif_error_Ok); |
| 251 |
|
| 252 |
struct heif_encoding_options *options; |
| 253 |
options = heif_encoding_options_alloc(); |
| 254 |
options->macOS_compatibility_workaround = false; |
| 255 |
options->macOS_compatibility_workaround_no_nclx_profile = false; |
| 256 |
options->image_orientation = heif_orientation_normal; |
| 257 |
|
| 258 |
heif_image_handle *handle; |
| 259 |
err = heif_context_encode_image(ctx, img, enc, options, &handle); |
| 260 |
REQUIRE(err.code == heif_error_Ok); |
| 261 |
|
| 262 |
heif_image *mask; |
| 263 |
heif_image_create(128, 64, heif_colorspace_monochrome, |
| 264 |
heif_chroma_monochrome, &mask); |
| 265 |
fill_new_plane(mask, heif_channel_Y, 128, 64); |
| 266 |
int mask_stride; |
| 267 |
uint8_t* p = heif_image_get_plane(mask, heif_channel_Y, &mask_stride); |
| 268 |
p[0] = 0xff; |
| 269 |
p[127] = 0x00; |
| 270 |
p[128*64 - 1] = 0xfe; |
| 271 |
|
| 272 |
heif_encoder *mask_enc; |
| 273 |
err = heif_context_get_encoder_for_format(ctx, heif_compression_mask, |
| 274 |
&mask_enc); |
| 275 |
REQUIRE(err.code == heif_error_Ok); |
| 276 |
heif_image_handle *mask_handle; |
| 277 |
err = heif_context_encode_image(ctx, mask, mask_enc, options, &mask_handle); |
| 278 |
REQUIRE(err.code == heif_error_Ok); |
| 279 |
|
| 280 |
struct heif_region_item *region_item1; |
| 281 |
err = heif_image_handle_add_region_item(handle, input_width, input_height, |
| 282 |
®ion_item1); |
| 283 |
REQUIRE(err.code == heif_error_Ok); |
| 284 |
|
| 285 |
err = heif_region_item_add_region_referenced_mask( |
| 286 |
region_item1, 200, 140, 128, 64, |
| 287 |
heif_image_handle_get_item_id(mask_handle), NULL); |
| 288 |
REQUIRE(err.code == heif_error_Ok); |
| 289 |
// TODO: maybe another mask or two? |
| 290 |
|
| 291 |
err = heif_context_write_to_file(ctx, "regions_mask.heif"); |
| 292 |
REQUIRE(err.code == heif_error_Ok); |
| 293 |
|
| 294 |
heif_region_item_release(region_item1); |
| 295 |
|
| 296 |
heif_image_handle_release(handle); |
| 297 |
heif_encoder_release(enc); |
| 298 |
heif_image_handle_release(mask_handle); |
| 299 |
heif_encoder_release(mask_enc); |
| 300 |
heif_encoding_options_free(options); |
| 301 |
heif_context_free(ctx); |
| 302 |
heif_image_release(img); |
| 303 |
heif_image_release(mask); |
| 304 |
|
| 305 |
heif_context *readbackCtx = get_context_for_local_file("regions_mask.heif"); |
| 306 |
heif_image_handle *readbackHandle = get_primary_image_handle(readbackCtx); |
| 307 |
int num_region_items = heif_image_handle_get_number_of_region_items(readbackHandle); |
| 308 |
REQUIRE(num_region_items == 1); |
| 309 |
std::vector<heif_item_id> region_item_ids(num_region_items); |
| 310 |
int num_returned = heif_image_handle_get_list_of_region_item_ids(readbackHandle, region_item_ids.data(), num_region_items); |
| 311 |
REQUIRE(num_returned == 1); |
| 312 |
heif_region_item* in_region_item; |
| 313 |
err = heif_context_get_region_item(readbackCtx, region_item_ids[0], &in_region_item); |
| 314 |
heif_item_id id1 = heif_region_item_get_id(in_region_item); |
| 315 |
REQUIRE(id1 == region_item_ids[0]); |
| 316 |
int num_regions = heif_region_item_get_number_of_regions(in_region_item); |
| 317 |
REQUIRE(num_regions == 1); |
| 318 |
std::vector<heif_region*> regions(num_regions); |
| 319 |
int num_regions_returned = heif_region_item_get_list_of_regions(in_region_item, regions.data(), (int)(regions.size())); |
| 320 |
REQUIRE(num_regions_returned == num_regions); |
| 321 |
REQUIRE(heif_region_get_type(regions[0]) == heif_region_type_referenced_mask); |
| 322 |
int32_t x, y; |
| 323 |
uint32_t width, height; |
| 324 |
heif_item_id referenced_item_id; |
| 325 |
err = heif_region_get_referenced_mask_ID(regions[0], &x, &y, &width, &height, &referenced_item_id); |
| 326 |
REQUIRE (err.code == heif_error_Ok); |
| 327 |
REQUIRE(x == 200); |
| 328 |
REQUIRE(y == 140); |
| 329 |
REQUIRE(width == 128); |
| 330 |
REQUIRE(height == 64); |
| 331 |
// This is kind of an implementation detail, but checks the iref is in the right direction |
| 332 |
REQUIRE(referenced_item_id == 2); |
| 333 |
heif_image_handle* mski_handle_in; |
| 334 |
err = heif_context_get_image_handle(readbackCtx, referenced_item_id, &mski_handle_in); |
| 335 |
REQUIRE(err.code == heif_error_Ok); |
| 336 |
heif_image* mski_in; |
| 337 |
err = heif_decode_image(mski_handle_in, &mski_in, heif_colorspace_monochrome, heif_chroma_monochrome, NULL); |
| 338 |
REQUIRE(err.code == heif_error_Ok); |
| 339 |
int mski_in_width = heif_image_get_width(mski_in, heif_channel_Y); |
| 340 |
int mski_in_height = heif_image_get_height(mski_in, heif_channel_Y); |
| 341 |
REQUIRE(mski_in_width == 128); |
| 342 |
REQUIRE(mski_in_height == 64); |
| 343 |
int mask_stride_in; |
| 344 |
uint8_t* plane_in = heif_image_get_plane(mski_in, heif_channel_Y, &mask_stride_in); |
| 345 |
REQUIRE(plane_in[0] == 0xff); |
| 346 |
REQUIRE(plane_in[mski_in_width - 1] == 0x00); |
| 347 |
REQUIRE(plane_in[mski_in_width*mski_in_height - 1] == 0xfe); |
| 348 |
|
| 349 |
heif_region_release(regions[0]); |
| 350 |
heif_image_release(mski_in); |
| 351 |
heif_image_handle_release(mski_handle_in); |
| 352 |
heif_region_item_release(in_region_item); |
| 353 |
heif_image_handle_release(readbackHandle); |
| 354 |
heif_context_free(readbackCtx); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
TEST_CASE("create inline mask region from data") { |
| 359 |
struct heif_error err; |
| 360 |
|
| 361 |
heif_image* img; |
| 362 |
uint32_t input_width = 1280; |
| 363 |
uint32_t input_height = 1024; |
| 364 |
heif_image_create(input_width, input_height, heif_colorspace_YCbCr, |
| 365 |
heif_chroma_420, &img); |
| 366 |
fill_new_plane(img, heif_channel_Y, input_width, input_height); |
| 367 |
fill_new_plane(img, heif_channel_Cb, (input_width + 1) / 2, |
| 368 |
(input_height + 1) / 2); |
| 369 |
fill_new_plane(img, heif_channel_Cr, (input_width + 1) / 2, |
| 370 |
(input_height + 1) / 2); |
| 371 |
|
| 372 |
heif_context *ctx = heif_context_alloc(); |
| 373 |
heif_encoder *enc; |
| 374 |
err = heif_context_get_encoder_for_format(ctx, heif_compression_AV1, &enc); |
| 375 |
REQUIRE(err.code == heif_error_Ok); |
| 376 |
|
| 377 |
struct heif_encoding_options *options; |
| 378 |
options = heif_encoding_options_alloc(); |
| 379 |
options->macOS_compatibility_workaround = false; |
| 380 |
options->macOS_compatibility_workaround_no_nclx_profile = false; |
| 381 |
options->image_orientation = heif_orientation_normal; |
| 382 |
|
| 383 |
heif_image_handle *handle; |
| 384 |
err = heif_context_encode_image(ctx, img, enc, options, &handle); |
| 385 |
REQUIRE(err.code == heif_error_Ok); |
| 386 |
|
| 387 |
struct heif_region_item *region_item1; |
| 388 |
err = heif_image_handle_add_region_item(handle, input_width, input_height, |
| 389 |
®ion_item1); |
| 390 |
REQUIRE(err.code == heif_error_Ok); |
| 391 |
|
| 392 |
std::vector<uint8_t> mask_data((64 * 3) / 8); |
| 393 |
mask_data[0] = 0x80; |
| 394 |
mask_data[2] = 0x7f; |
| 395 |
mask_data[10] = 0x3e; |
| 396 |
mask_data[18] = 0x1d; |
| 397 |
mask_data[23] = 0x01; |
| 398 |
err = heif_region_item_add_region_inline_mask_data(region_item1, 20, 50, 64, 3, mask_data.data(), mask_data.size(), NULL); |
| 399 |
REQUIRE(err.code == heif_error_Ok); |
| 400 |
|
| 401 |
err = heif_context_write_to_file(ctx, "regions_mask_inline_data.heif"); |
| 402 |
REQUIRE(err.code == heif_error_Ok); |
| 403 |
|
| 404 |
heif_region_item_release(region_item1); |
| 405 |
|
| 406 |
heif_image_handle_release(handle); |
| 407 |
heif_encoder_release(enc); |
| 408 |
heif_encoding_options_free(options); |
| 409 |
heif_context_free(ctx); |
| 410 |
heif_image_release(img);; |
| 411 |
|
| 412 |
heif_context *readbackCtx = get_context_for_local_file("regions_mask_inline_data.heif"); |
| 413 |
heif_image_handle *readbackHandle = get_primary_image_handle(readbackCtx); |
| 414 |
int num_region_items = heif_image_handle_get_number_of_region_items(readbackHandle); |
| 415 |
REQUIRE(num_region_items == 1); |
| 416 |
std::vector<heif_item_id> region_item_ids(num_region_items); |
| 417 |
int num_returned = heif_image_handle_get_list_of_region_item_ids(readbackHandle, region_item_ids.data(), num_region_items); |
| 418 |
REQUIRE(num_returned == 1); |
| 419 |
heif_region_item* in_region_item; |
| 420 |
err = heif_context_get_region_item(readbackCtx, region_item_ids[0], &in_region_item); |
| 421 |
heif_item_id id1 = heif_region_item_get_id(in_region_item); |
| 422 |
REQUIRE(id1 == region_item_ids[0]); |
| 423 |
int num_regions = heif_region_item_get_number_of_regions(in_region_item); |
| 424 |
REQUIRE(num_regions == 1); |
| 425 |
std::vector<heif_region*> regions(num_regions); |
| 426 |
int num_regions_returned = heif_region_item_get_list_of_regions(in_region_item, regions.data(), (int)(regions.size())); |
| 427 |
REQUIRE(num_regions_returned == num_regions); |
| 428 |
REQUIRE(heif_region_get_type(regions[0]) == heif_region_type_inline_mask); |
| 429 |
size_t data_len = heif_region_get_inline_mask_data_len(regions[0]); |
| 430 |
int32_t x, y; |
| 431 |
uint32_t width, height; |
| 432 |
std::vector<uint8_t> mask_data_in(data_len); |
| 433 |
err = heif_region_get_inline_mask_data(regions[0], &x, &y, &width, &height, mask_data_in.data()); |
| 434 |
REQUIRE (err.code == heif_error_Ok); |
| 435 |
REQUIRE(x == 20); |
| 436 |
REQUIRE(y == 50); |
| 437 |
REQUIRE(width == 64); |
| 438 |
REQUIRE(height == 3); |
| 439 |
REQUIRE(mask_data_in[0] == 0x80); |
| 440 |
REQUIRE(mask_data_in[1] == 0x00); |
| 441 |
REQUIRE(mask_data_in[2] == 0x7f); |
| 442 |
REQUIRE(mask_data_in[10] == 0x3e); |
| 443 |
REQUIRE(mask_data_in[18] == 0x1d); |
| 444 |
REQUIRE(mask_data_in[23] == 0x01); |
| 445 |
heif_region_release(regions[0]); |
| 446 |
|
| 447 |
heif_region_item_release(in_region_item); |
| 448 |
heif_image_handle_release(readbackHandle); |
| 449 |
heif_context_free(readbackCtx); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
TEST_CASE("create inline mask region from image") { |
| 454 |
struct heif_error err; |
| 455 |
|
| 456 |
heif_image* img; |
| 457 |
uint32_t input_width = 1280; |
| 458 |
uint32_t input_height = 1024; |
| 459 |
heif_image_create(input_width, input_height, heif_colorspace_YCbCr, |
| 460 |
heif_chroma_420, &img); |
| 461 |
fill_new_plane(img, heif_channel_Y, input_width, input_height); |
| 462 |
fill_new_plane(img, heif_channel_Cb, (input_width + 1) / 2, |
| 463 |
(input_height + 1) / 2); |
| 464 |
fill_new_plane(img, heif_channel_Cr, (input_width + 1) / 2, |
| 465 |
(input_height + 1) / 2); |
| 466 |
|
| 467 |
heif_context *ctx = heif_context_alloc(); |
| 468 |
heif_encoder *enc; |
| 469 |
err = heif_context_get_encoder_for_format(ctx, heif_compression_AV1, &enc); |
| 470 |
REQUIRE(err.code == heif_error_Ok); |
| 471 |
|
| 472 |
struct heif_encoding_options *options; |
| 473 |
options = heif_encoding_options_alloc(); |
| 474 |
options->macOS_compatibility_workaround = false; |
| 475 |
options->macOS_compatibility_workaround_no_nclx_profile = false; |
| 476 |
options->image_orientation = heif_orientation_normal; |
| 477 |
|
| 478 |
heif_image_handle *handle; |
| 479 |
err = heif_context_encode_image(ctx, img, enc, options, &handle); |
| 480 |
REQUIRE(err.code == heif_error_Ok); |
| 481 |
|
| 482 |
struct heif_region_item *region_item1; |
| 483 |
err = heif_image_handle_add_region_item(handle, input_width, input_height, |
| 484 |
®ion_item1); |
| 485 |
REQUIRE(err.code == heif_error_Ok); |
| 486 |
|
| 487 |
heif_image* mask_image; |
| 488 |
heif_image_create(64, 3, heif_colorspace_monochrome, |
| 489 |
heif_chroma_monochrome, &mask_image); |
| 490 |
err = heif_image_add_plane(mask_image, heif_channel_Y, 64, 3, 8); |
| 491 |
REQUIRE(err.code == heif_error_Ok); |
| 492 |
int stride; |
| 493 |
uint8_t* p = heif_image_get_plane(mask_image, heif_channel_Y, &stride); |
| 494 |
memset(p, 0, 3 * stride); |
| 495 |
p[0] = 0x82; |
| 496 |
p[17] = 0x81; |
| 497 |
p[18] = 0x81; |
| 498 |
p[19] = 0x81; |
| 499 |
p[20] = 0x81; |
| 500 |
p[21] = 0xff; |
| 501 |
p[22] = 0xff; |
| 502 |
p[23] = 0xff; |
| 503 |
p[stride + 18] = 0x86; |
| 504 |
p[stride + 19] = 0x86; |
| 505 |
p[stride + 20] = 0x87; |
| 506 |
p[stride + 21] = 0x87; |
| 507 |
p[stride + 22] = 0x87; |
| 508 |
p[2*stride + 19] = 0x81; |
| 509 |
p[2*stride + 20] = 0x81; |
| 510 |
p[2*stride + 21] = 0x81; |
| 511 |
p[2*stride + 23] = 0x81; |
| 512 |
p[2*stride + 63] = 0x81; |
| 513 |
err = heif_region_item_add_region_inline_mask(region_item1, 20, 50, 64, 3, mask_image, NULL); |
| 514 |
REQUIRE(err.code == heif_error_Ok); |
| 515 |
|
| 516 |
err = heif_context_write_to_file(ctx, "regions_mask_inline_image.heif"); |
| 517 |
REQUIRE(err.code == heif_error_Ok); |
| 518 |
|
| 519 |
heif_image_release(mask_image); |
| 520 |
heif_region_item_release(region_item1); |
| 521 |
|
| 522 |
heif_image_handle_release(handle); |
| 523 |
heif_encoder_release(enc); |
| 524 |
heif_encoding_options_free(options); |
| 525 |
heif_context_free(ctx); |
| 526 |
heif_image_release(img);; |
| 527 |
|
| 528 |
heif_context *readbackCtx = get_context_for_local_file("regions_mask_inline_image.heif"); |
| 529 |
heif_image_handle *readbackHandle = get_primary_image_handle(readbackCtx); |
| 530 |
int num_region_items = heif_image_handle_get_number_of_region_items(readbackHandle); |
| 531 |
REQUIRE(num_region_items == 1); |
| 532 |
std::vector<heif_item_id> region_item_ids(num_region_items); |
| 533 |
int num_returned = heif_image_handle_get_list_of_region_item_ids(readbackHandle, region_item_ids.data(), num_region_items); |
| 534 |
REQUIRE(num_returned == 1); |
| 535 |
heif_region_item* in_region_item; |
| 536 |
err = heif_context_get_region_item(readbackCtx, region_item_ids[0], &in_region_item); |
| 537 |
heif_item_id id1 = heif_region_item_get_id(in_region_item); |
| 538 |
REQUIRE(id1 == region_item_ids[0]); |
| 539 |
int num_regions = heif_region_item_get_number_of_regions(in_region_item); |
| 540 |
REQUIRE(num_regions == 1); |
| 541 |
std::vector<heif_region*> regions(num_regions); |
| 542 |
int num_regions_returned = heif_region_item_get_list_of_regions(in_region_item, regions.data(), (int)(regions.size())); |
| 543 |
REQUIRE(num_regions_returned == num_regions); |
| 544 |
REQUIRE(heif_region_get_type(regions[0]) == heif_region_type_inline_mask); |
| 545 |
heif_image * mask_image_in; |
| 546 |
int32_t x, y; |
| 547 |
uint32_t width, height; |
| 548 |
err = heif_region_get_mask_image(regions[0], &x, &y, &width, &height, &mask_image_in); |
| 549 |
REQUIRE (err.code == heif_error_Ok); |
| 550 |
REQUIRE(x == 20); |
| 551 |
REQUIRE(y == 50); |
| 552 |
REQUIRE(width == 64); |
| 553 |
REQUIRE(height == 3); |
| 554 |
uint8_t* p_in = heif_image_get_plane(mask_image_in, heif_channel_Y, &stride); |
| 555 |
REQUIRE(p_in[0] == 0xff); |
| 556 |
REQUIRE(p_in[1] == 0x00); |
| 557 |
REQUIRE(p_in[8] == 0x00); |
| 558 |
REQUIRE(p_in[16] == 0x00); |
| 559 |
REQUIRE(p_in[17] == 0xff); |
| 560 |
REQUIRE(p_in[18] == 0xff); |
| 561 |
REQUIRE(p_in[19] == 0xff); |
| 562 |
REQUIRE(p_in[20] == 0xff); |
| 563 |
REQUIRE(p_in[21] == 0xff); |
| 564 |
REQUIRE(p_in[22] == 0xff); |
| 565 |
REQUIRE(p_in[23] == 0xff); |
| 566 |
REQUIRE(p_in[24] == 0x00); |
| 567 |
REQUIRE(p_in[81] == 0x00); |
| 568 |
REQUIRE(p_in[82] == 0xff); |
| 569 |
REQUIRE(p_in[83] == 0xff); |
| 570 |
REQUIRE(p_in[84] == 0xff); |
| 571 |
REQUIRE(p_in[85] == 0xff); |
| 572 |
REQUIRE(p_in[86] == 0xff); |
| 573 |
REQUIRE(p_in[87] == 0x00); |
| 574 |
REQUIRE(p_in[146] == 0x00); |
| 575 |
REQUIRE(p_in[147] == 0xff); |
| 576 |
REQUIRE(p_in[148] == 0xff); |
| 577 |
REQUIRE(p_in[149] == 0xff); |
| 578 |
REQUIRE(p_in[150] == 0x00); |
| 579 |
REQUIRE(p_in[151] == 0xff); |
| 580 |
REQUIRE(p_in[190] == 0x00); |
| 581 |
REQUIRE(p_in[191] == 0xff); |
| 582 |
heif_image_release(mask_image_in); |
| 583 |
heif_region_release(regions[0]); |
| 584 |
|
| 585 |
heif_region_item_release(in_region_item); |
| 586 |
heif_image_handle_release(readbackHandle); |
| 587 |
heif_context_free(readbackCtx); |
| 588 |
} |