Skip to content

Commit

Permalink
wip: item API suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Nov 4, 2023
1 parent 2b0c2ab commit 164ca61
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 18 deletions.
4 changes: 4 additions & 0 deletions libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,10 @@ struct heif_error heif_context_write(struct heif_context* ctx,
}
}

void heif_context_set_major_brand(struct heif_context* ctx, heif_brand2 major_brand)
{
ctx->context->get_heif_file()->get_ftyp_box()->set_major_brand(major_brand);
}

void heif_context_add_compatible_brand(struct heif_context* ctx,
heif_brand2 compatible_brand)
Expand Down
3 changes: 3 additions & 0 deletions libheif/heif.h
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,9 @@ struct heif_error heif_context_write(struct heif_context*,
struct heif_writer* writer,
void* userdata);

LIBHEIF_API
void heif_context_set_major_brand(struct heif_context* ctx, heif_brand2 major_brand);

// Add a compatible brand that is now added automatically by libheif when encoding images (e.g. some application brands like 'geo1').
LIBHEIF_API
void heif_context_add_compatible_brand(struct heif_context* ctx,
Expand Down
19 changes: 10 additions & 9 deletions libheif/heif_items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@

// ------------------------- reading -------------------------

int heif_context_get_number_of_items(const struct heif_context* ctx)
size_t heif_context_get_number_of_items(const struct heif_context* ctx)
{
return (int) ctx->context->get_heif_file()->get_number_of_items();
return ctx->context->get_heif_file()->get_number_of_items();
}


int heif_context_get_list_of_item_IDs(const struct heif_context* ctx,
size_t heif_context_get_list_of_item_IDs(const struct heif_context* ctx,
heif_item_id* ID_array,
int count)
size_t count)
{
if (!ID_array) {
return 0;
}

auto ids = ctx->context->get_heif_file()->get_item_IDs();
for (int i = 0; i < (int) ids.size(); i++) {
for (size_t i = 0; i < ids.size(); i++) {
if (i == count) {
return count;
}

ID_array[i] = ids[i];
}

return (int) ids.size();
return ids.size();
}


Expand Down Expand Up @@ -209,13 +209,14 @@ void heif_release_item_references(const struct heif_context* ctx, heif_item_id**

struct heif_error heif_context_add_item(struct heif_context* ctx,
const char* item_type,
const void* data, int size,
const void* data, size_t size,
heif_item_id* out_item_id)
{
Result<heif_item_id> result = ctx->context->get_heif_file()->add_infe(item_type, (const uint8_t*) data, size);

if (result && out_item_id) {
*out_item_id = result.value;
ctx->context->get_heif_file()->set_primary_item_id(*out_item_id);
return heif_error_success;
}
else {
Expand All @@ -226,7 +227,7 @@ struct heif_error heif_context_add_item(struct heif_context* ctx,
struct heif_error heif_context_add_mime_item(struct heif_context* ctx,
const char* content_type,
heif_metadata_compression content_encoding,
const void* data, int size,
const void* data, size_t size,
heif_item_id* out_item_id)
{
Result<heif_item_id> result = ctx->context->get_heif_file()->add_infe_mime(content_type, content_encoding, (const uint8_t*) data, size);
Expand All @@ -242,7 +243,7 @@ struct heif_error heif_context_add_mime_item(struct heif_context* ctx,

struct heif_error heif_context_add_uri_item(struct heif_context* ctx,
const char* item_uri_type,
const void* data, int size,
const void* data, size_t size,
heif_item_id* out_item_id)
{
Result<heif_item_id> result = ctx->context->get_heif_file()->add_infe_uri(item_uri_type, (const uint8_t*) data, size);
Expand Down
35 changes: 26 additions & 9 deletions libheif/heif_items.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,32 @@ extern "C" {

// ------------------------- reading -------------------------

/**
* Gets the number of items.
*
* This is not the same as the number of images, since there can be other types of items,
* such as metadata.
*
* @param ctx the file context
* @return the number of items
*/
LIBHEIF_API
int heif_context_get_number_of_items(const struct heif_context* ctx);
size_t heif_context_get_number_of_items(const struct heif_context* ctx);

// Fills in the item IDs into the user-supplied array 'ID_array', preallocated with 'count' entries.
// Function returns the total number of IDs filled into the array, which may be less than 'count'.
/**
* Get the item identifiers.
*
* Fills in the item IDs into the user-supplied array (@code ID_array), preallocated with (@code count) entries.
*
* @param ctx the file context
* @param ID_array the output array.
* @param count the number of items allocated within (@code ID_array).
* @return the total number of IDs filled into the array, which may be less than (@code count).
*/
LIBHEIF_API
int heif_context_get_list_of_item_IDs(const struct heif_context* ctx,
heif_item_id* ID_array,
int count);
size_t heif_context_get_list_of_item_IDs(const struct heif_context* ctx,
heif_item_id* ID_array,
size_t count);

/**
* Gets the item type.
Expand Down Expand Up @@ -114,20 +131,20 @@ void heif_release_item_references(const struct heif_context* ctx, heif_item_id**
LIBHEIF_API
struct heif_error heif_context_add_item(struct heif_context* ctx,
const char* item_type,
const void* data, int size,
const void* data, size_t size,
heif_item_id* out_item_id);

LIBHEIF_API
struct heif_error heif_context_add_mime_item(struct heif_context* ctx,
const char* content_type,
heif_metadata_compression content_encoding,
const void* data, int size,
const void* data, size_t size,
heif_item_id* out_item_id);

LIBHEIF_API
struct heif_error heif_context_add_uri_item(struct heif_context* ctx,
const char* item_uri_type,
const void* data, int size,
const void* data, size_t size,
heif_item_id* out_item_id);

LIBHEIF_API
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ endif()

add_libheif_test(encode)
add_libheif_test(region)
add_libheif_test(items)

if (WITH_UNCOMPRESSED_CODEC)
add_libheif_test(uncompressed_decode)
Expand Down
76 changes: 76 additions & 0 deletions tests/items.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
libheif unit tests
MIT License
Copyright (c) 2023 Brad Hards <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "libheif/heif.h"
#include "libheif/heif_items.h"

#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string.h>

TEST_CASE("add item") {
std::string filename = "simple_item.heif";
std::cout << "filename: " << filename << std::endl;
std::vector<uint8_t> content{0x03, 0x04, 0x02, 0x01, 0xff};
heif_item_id id;
{
heif_context *write_ctx = heif_context_alloc();
heif_context_set_major_brand(write_ctx, heif_brand2_miaf);
heif_context_add_compatible_brand(write_ctx, heif_brand2_miaf);
size_t num_items = heif_context_get_number_of_items(write_ctx);
REQUIRE(num_items == 0);
heif_context_add_item(write_ctx, "unci", content.data(), content.size(), &id);
struct heif_error res = heif_context_write_to_file(write_ctx, filename.c_str());
REQUIRE(res.code == heif_error_Ok);
heif_context_free(write_ctx);
}
{
heif_context *read_ctx = heif_context_alloc();
heif_context_read_from_file(read_ctx, filename.c_str(), NULL);
size_t num_items = heif_context_get_number_of_items(read_ctx);
REQUIRE(num_items == 1);
std::vector<heif_item_id> item_ids(num_items);
size_t actual_count = heif_context_get_list_of_item_IDs(read_ctx, item_ids.data(), item_ids.size());
REQUIRE(actual_count == 1);
REQUIRE(item_ids[0] == id);
uint32_t item_type = heif_context_get_item_type(read_ctx, id);
REQUIRE(item_type == heif_fourcc('u', 'n', 'c', 'i'));
size_t item_data_size;
heif_error res = heif_context_get_item_data(read_ctx, id, NULL, &item_data_size);
REQUIRE(res.code == heif_error_Ok);
REQUIRE(item_data_size == content.size());
uint8_t *item_data;
res = heif_context_get_item_data(read_ctx, id, &item_data, &item_data_size);
REQUIRE(res.code == heif_error_Ok);
REQUIRE(item_data_size == content.size());
REQUIRE(item_data[0] == 0x03);
REQUIRE(item_data[4] == 0xff);
heif_release_item_data(read_ctx, &item_data);
heif_context_free(read_ctx);
}
}

0 comments on commit 164ca61

Please sign in to comment.