Skip to content

Commit

Permalink
move functions related to item properties to separate header file
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 12, 2023
1 parent 03369b9 commit 25d6ae9
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 378 deletions.
1 change: 1 addition & 0 deletions examples/heif_enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <string>

#include <libheif/heif.h>
#include <libheif/heif_properties.h>

#if HAVE_LIBJPEG
#include "decoder_jpeg.h"
Expand Down
1 change: 1 addition & 0 deletions examples/heif_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <libheif/heif.h>
#include <libheif/heif_regions.h>
#include <libheif/heif_properties.h>

#include <fstream>
#include <iostream>
Expand Down
3 changes: 3 additions & 0 deletions libheif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(libheif_headers
heif.h
heif_cxx.h
heif_plugin.h
heif_properties.h
heif_regions.h
${CMAKE_CURRENT_BINARY_DIR}/heif_version.h)

Expand Down Expand Up @@ -46,6 +47,8 @@ set(libheif_sources
region.h
heif_regions.h
heif_regions.cc
heif_properties.h
heif_properties.cc
color-conversion/colorconversion.cc
color-conversion/colorconversion.h
color-conversion/rgb2yuv.cc
Expand Down
1 change: 1 addition & 0 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <cstdint>
#include "libheif/common_utils.h"
#include "libheif/heif_properties.h"
#include <cinttypes>
#include <cstddef>

Expand Down
1 change: 1 addition & 0 deletions libheif/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "file.h"
#include "libheif/box.h"
#include "libheif/heif.h"
#include "libheif/heif_properties.h"
#include "libheif/jpeg2000.h"
#include "libheif/jpeg.h"
#include "libheif/vvc.h"
Expand Down
273 changes: 0 additions & 273 deletions libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1846,279 +1846,6 @@ void heif_nclx_color_profile_free(struct heif_color_profile_nclx* nclx_profile)
color_profile_nclx::free_nclx_color_profile(nclx_profile);
}


int heif_item_get_properties_of_type(const struct heif_context* context,
heif_item_id id,
heif_item_property_type type,
heif_property_id* out_list,
int count)
{
auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(id, properties);
if (err) {
// We do not pass the error, because a missing ipco should have been detected already when reading the file.
return 0;
}

int out_idx = 0;
int property_id = 1;

for (const auto& property : properties) {
bool match;
if (type == heif_item_property_type_invalid) {
match = true;
}
else {
match = (property->get_short_type() == type);
}

if (match) {
if (out_list && out_idx < count) {
out_list[out_idx] = property_id;
out_idx++;
}
else if (!out_list) {
out_idx++;
}
}

property_id++;
}

return out_idx;
}


int heif_item_get_transformation_properties(const struct heif_context* context,
heif_item_id id,
heif_property_id* out_list,
int count)
{
auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(id, properties);
if (err) {
// We do not pass the error, because a missing ipco should have been detected already when reading the file.
return 0;
}

int out_idx = 0;
int property_id = 1;

for (const auto& property : properties) {
bool match = (property->get_short_type() == fourcc("imir") ||
property->get_short_type() == fourcc("irot") ||
property->get_short_type() == fourcc("clap"));

if (match) {
if (out_list && out_idx < count) {
out_list[out_idx] = property_id;
out_idx++;
}
else if (!out_list) {
out_idx++;
}
}

property_id++;
}

return out_idx;
}

enum heif_item_property_type heif_item_get_property_type(const struct heif_context* context,
heif_item_id id,
heif_property_id propertyId)
{
auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(id, properties);
if (err) {
// We do not pass the error, because a missing ipco should have been detected already when reading the file.
return heif_item_property_type_invalid;
}

if (propertyId - 1 < 0 || propertyId - 1 >= properties.size()) {
return heif_item_property_type_invalid;
}

auto property = properties[propertyId - 1];
return (enum heif_item_property_type) property->get_short_type();
}


static char* create_c_string_copy(const std::string s)
{
char* copy = new char[s.length() + 1];
strcpy(copy, s.data());
return copy;
}


struct heif_error heif_item_get_property_user_description(const struct heif_context* context,
heif_item_id itemId,
heif_property_id propertyId,
struct heif_property_user_description** out)
{
if (!out) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
}

auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(itemId, properties);
if (err) {
return err.error_struct(context->context.get());
}

if (propertyId - 1 < 0 || propertyId - 1 >= properties.size()) {
return {heif_error_Usage_error, heif_suberror_Invalid_property, "property index out of range"};
}

auto udes = std::dynamic_pointer_cast<Box_udes>(properties[propertyId - 1]);
if (!udes) {
return {heif_error_Usage_error, heif_suberror_Invalid_property, "wrong property type"};
}

auto* udes_c = new heif_property_user_description();
udes_c->version = 1;
udes_c->lang = create_c_string_copy(udes->get_lang());
udes_c->name = create_c_string_copy(udes->get_name());
udes_c->description = create_c_string_copy(udes->get_description());
udes_c->tags = create_c_string_copy(udes->get_tags());

*out = udes_c;

return heif_error_success;
}


LIBHEIF_API
struct heif_error heif_item_add_property_user_description(const struct heif_context* context,
heif_item_id itemId,
const struct heif_property_user_description* description,
heif_property_id* out_propertyId)
{
if (!context || !description) {
return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"};
}

auto udes = std::make_shared<Box_udes>();
udes->set_lang(description->lang ? description->lang : "");
udes->set_name(description->name ? description->name : "");
udes->set_description(description->description ? description->description : "");
udes->set_tags(description->tags ? description->tags : "");

heif_property_id id = context->context->add_property(itemId, udes, false);

if (out_propertyId) {
*out_propertyId = id;
}

return heif_error_success;
}


enum heif_transform_mirror_direction heif_item_get_property_transform_mirror(const struct heif_context* context,
heif_item_id itemId,
heif_property_id propertyId)
{
auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(itemId, properties);
if (err) {
return heif_transform_mirror_direction_invalid;
}

if (propertyId - 1 < 0 || propertyId - 1 >= properties.size()) {
return heif_transform_mirror_direction_invalid;
}

auto imir = std::dynamic_pointer_cast<Box_imir>(properties[propertyId - 1]);
if (!imir) {
return heif_transform_mirror_direction_invalid;
}

return imir->get_mirror_direction();
}


int heif_item_get_property_transform_rotation_ccw(const struct heif_context* context,
heif_item_id itemId,
heif_property_id propertyId)
{
auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(itemId, properties);
if (err) {
return -1;
}

if (propertyId - 1 < 0 || propertyId - 1 >= properties.size()) {
return -1;
}

auto irot = std::dynamic_pointer_cast<Box_irot>(properties[propertyId - 1]);
if (!irot) {
return -1;
}

return irot->get_rotation();
}


void heif_item_get_property_transform_crop_borders(const struct heif_context* context,
heif_item_id itemId,
heif_property_id propertyId,
int image_width, int image_height,
int* left, int* top, int* right, int* bottom)
{
auto file = context->context->get_heif_file();

std::vector<std::shared_ptr<Box>> properties;
Error err = file->get_properties(itemId, properties);
if (err) {
return;
}

if (propertyId - 1 < 0 || propertyId - 1 >= properties.size()) {
return;
}

auto clap = std::dynamic_pointer_cast<Box_clap>(properties[propertyId - 1]);
if (!clap) {
return;
}

if (left) *left = clap->left_rounded(image_width);
if (right) *right = image_width - 1 - clap->right_rounded(image_width);
if (top) *top = clap->top_rounded(image_height);
if (bottom) *bottom = image_height - 1 - clap->bottom_rounded(image_height);
}


void heif_property_user_description_release(struct heif_property_user_description* udes)
{
if (udes == nullptr) {
return;
}

delete[] udes->lang;
delete[] udes->name;
delete[] udes->description;
delete[] udes->tags;

delete udes;
}


// DEPRECATED
struct heif_error heif_register_decoder(heif_context* heif, const heif_decoder_plugin* decoder_plugin)
{
Expand Down
Loading

0 comments on commit 25d6ae9

Please sign in to comment.