Debian Codegraph @

tests/text.cc

raw
1 /*
2 libheif integration tests for text items
3
4 MIT License
5
6 Copyright (c) 2025 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 #include <libheif/heif_items.h>
36 #include <libheif/heif_library.h>
37
38
39 TEST_CASE("no text") {
40 // skip test if we do not have the uncompressed codec
41 if (!heif_have_decoder_for_format(heif_compression_uncompressed)) {
42 SKIP("Skipping test because uncompressed codec is not compiled.");
43 }
44
45 auto context = get_context_for_test_file("uncompressed_comp_RGB.heif");
46 heif_image_handle *handle = get_primary_image_handle(context);
47 int num_text_items = heif_image_handle_get_number_of_text_items(handle);
48 REQUIRE(num_text_items == 0);
49 heif_image_handle_release(handle);
50 heif_context_free(context);
51 }
52
53 TEST_CASE("create text item") {
54 // skip test if we do not have the uncompressed codec
55 if (!heif_have_decoder_for_format(heif_compression_uncompressed)) {
56 SKIP("Skipping test because uncompressed codec is not compiled.");
57 }
58 struct heif_error err;
59
60 heif_image* img;
61 uint32_t input_width = 1280;
62 uint32_t input_height = 1024;
63 heif_image_create(input_width,input_height, heif_colorspace_YCbCr, heif_chroma_420, &img);
64 fill_new_plane(img, heif_channel_Y, input_width, input_height);
65 fill_new_plane(img, heif_channel_Cb, (input_width+1)/2, (input_height+1)/2);
66 fill_new_plane(img, heif_channel_Cr, (input_width+1)/2, (input_height+1)/2);
67
68 heif_context* ctx = heif_context_alloc();
69 heif_encoder* enc;
70 err = heif_context_get_encoder_for_format(ctx, heif_compression_uncompressed, &enc);
71 REQUIRE(err.code == heif_error_Ok);
72
73 struct heif_encoding_options* options;
74 options = heif_encoding_options_alloc();
75 options->macOS_compatibility_workaround = false;
76 options->macOS_compatibility_workaround_no_nclx_profile = false;
77 options->image_orientation = heif_orientation_normal;
78
79 heif_image_handle* handle;
80 err = heif_context_encode_image(ctx, img, enc, options, &handle);
81 REQUIRE(err.code == heif_error_Ok);
82
83 struct heif_text_item* text_item1;
84 std::string text_body1("first string");
85 err = heif_image_handle_add_text_item(handle, "text/plain", text_body1.c_str(), &text_item1);
86 REQUIRE(err.code == heif_error_Ok);
87 err = heif_text_item_set_extended_language(text_item1, "en-AU", NULL);
88 REQUIRE(err.code == heif_error_Ok);
89
90 struct heif_text_item* text_item2;
91 std::string text_body2("a second string");
92 err = heif_image_handle_add_text_item(handle, "text/plain", text_body2.c_str(), &text_item2);
93 REQUIRE(err.code == heif_error_Ok);
94 heif_property_id elng_prop_id;
95 err = heif_text_item_set_extended_language(text_item2, "en-UK", &elng_prop_id);
96 REQUIRE(err.code == heif_error_Ok);
97 REQUIRE(elng_prop_id != 0);
98
99 err = heif_context_write_to_file(ctx, "text.heif");
100 REQUIRE(err.code == heif_error_Ok);
101
102 heif_text_item_release(text_item1);
103 heif_text_item_release(text_item2);
104
105 heif_image_handle_release(handle);
106 heif_encoder_release(enc);
107 heif_encoding_options_free(options);
108 heif_context_free(ctx);
109 heif_image_release(img);
110
111 heif_context *readbackCtx = get_context_for_local_file("text.heif");
112 heif_image_handle *readbackHandle = get_primary_image_handle(readbackCtx);
113 int num_text_items = heif_image_handle_get_number_of_text_items(readbackHandle);
114 REQUIRE(num_text_items == 2);
115
116 std::vector<heif_item_id> text_item_ids_minus(1);
117 int num_returned = heif_image_handle_get_list_of_text_item_ids(readbackHandle, text_item_ids_minus.data(), 1);
118 REQUIRE(num_returned == 1);
119
120 std::vector<heif_item_id> text_item_ids_extra(3);
121 num_returned = heif_image_handle_get_list_of_text_item_ids(readbackHandle, text_item_ids_extra.data(), 3);
122 REQUIRE(num_returned == num_text_items);
123
124 std::vector<heif_item_id> text_item_ids(num_text_items);
125 num_returned = heif_image_handle_get_list_of_text_item_ids(readbackHandle, text_item_ids.data(), num_text_items);
126 REQUIRE(num_returned == 2);
127
128 heif_text_item* text0;
129 err = heif_context_get_text_item(readbackCtx, text_item_ids[0], &text0);
130 heif_item_id id0 = heif_text_item_get_id(text0);
131 REQUIRE(id0 == text_item_ids[0]);
132
133 REQUIRE(heif_item_get_item_type(readbackCtx, text_item_ids[0]) == fourcc("mime"));
134 const char* content_type0 = heif_item_get_mime_item_content_type(readbackCtx, text_item_ids[0]);
135 REQUIRE(std::string(content_type0) == "text/plain");
136 const char* body0 = heif_text_item_get_content(text0);
137 REQUIRE(std::string(body0) == text_body1);
138 heif_string_release(body0);
139 char * elng0;
140 err = heif_item_get_property_extended_language(readbackCtx, id0, &elng0);
141 REQUIRE(err.code == heif_error_Ok);
142 REQUIRE(strcmp(elng0, "en-AU") == 0);
143 heif_string_release(elng0);
144
145 heif_text_item* text1;
146 err = heif_context_get_text_item(readbackCtx, text_item_ids[1], &text1);
147 heif_item_id id1 = heif_text_item_get_id(text1);
148 REQUIRE(id1 == text_item_ids[1]);
149
150 REQUIRE(heif_item_get_item_type(readbackCtx, text_item_ids[1]) == fourcc("mime"));
151 const char* content_type1 = heif_item_get_mime_item_content_type(readbackCtx, text_item_ids[0]);
152 REQUIRE(std::string(content_type1) == "text/plain");
153 const char* body1 = heif_text_item_get_content(text1);
154 REQUIRE(std::string(body1) == text_body2);
155 heif_string_release(body1);
156 char * elng1;
157 err = heif_item_get_property_extended_language(readbackCtx, id1, &elng1);
158 REQUIRE(err.code == heif_error_Ok);
159 REQUIRE(strcmp(elng1, "en-UK") == 0);
160 heif_string_release(elng1);
161
162 heif_text_item_release(text0);
163 heif_text_item_release(text1);
164 heif_image_handle_release(readbackHandle);
165 heif_context_free(readbackCtx);
166 }