Skip to content

Commit

Permalink
public API to read extrinsic camera matrix (#1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Jun 17, 2024
1 parent 3f5fc01 commit 675dee7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ class Box_cmex : public FullBox
bool m_has_pos_y = false;
bool m_has_pos_z = false;
bool m_has_orientation = false;
bool m_has_world_coordinate_system_id;
bool m_has_world_coordinate_system_id = false;

enum Flags
{
Expand Down
89 changes: 89 additions & 0 deletions libheif/heif_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,92 @@ struct heif_error heif_item_add_property_camera_intrinsic_matrix(const struct he

return heif_error_success;
}


struct heif_camera_extrinsic_matrix
{
Box_cmex::ExtrinsicMatrix matrix;
};


struct heif_error heif_item_get_property_camera_extrinsic_matrix(const struct heif_context* context,
heif_item_id itemId,
heif_property_id propertyId,
struct heif_camera_extrinsic_matrix** out_matrix)
{
if (!out_matrix || !context) {
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 cmex = std::dynamic_pointer_cast<Box_cmex>(properties[propertyId - 1]);
if (!cmex) {
return {heif_error_Usage_error, heif_suberror_Invalid_property, "wrong property type"};
}

*out_matrix = new heif_camera_extrinsic_matrix;
(*out_matrix)->matrix = cmex->get_extrinsic_matrix();

return heif_error_success;
}


void heif_camera_extrinsic_matrix_release(struct heif_camera_extrinsic_matrix* matrix)
{
delete matrix;
}


struct heif_error heif_camera_extrinsic_matrix_get_rotation_matrix(const struct heif_camera_extrinsic_matrix* matrix,
double* out_matrix)
{
if (!matrix || !out_matrix) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
}

auto rot_matrix = matrix->matrix.calculate_rotation_matrix();
for (int i = 0; i < 9; i++) {
out_matrix[i] = rot_matrix[i];
}

return heif_error_success;
}


struct heif_error heif_camera_extrinsic_matrix_get_position_vector(const struct heif_camera_extrinsic_matrix* matrix,
int32_t* out_vector)
{
if (!matrix || !out_vector) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
}

out_vector[0] = matrix->matrix.pos_x;
out_vector[1] = matrix->matrix.pos_y;
out_vector[2] = matrix->matrix.pos_z;

return heif_error_success;
}


struct heif_error heif_camera_extrinsic_matrix_get_world_coordinate_system_id(const struct heif_camera_extrinsic_matrix* matrix,
uint32_t* out_wcs_id)
{
if (!matrix || !out_wcs_id) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
}

*out_wcs_id = matrix->matrix.world_coordinate_system_id;

return heif_error_success;
}
27 changes: 26 additions & 1 deletion libheif/heif_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ enum heif_item_property_type
heif_item_property_type_transform_crop = heif_fourcc('c', 'l', 'a', 'p'),
heif_item_property_type_image_size = heif_fourcc('i', 's', 'p', 'e'),
heif_item_property_type_uuid = heif_fourcc('u', 'u', 'i', 'd'),
heif_item_property_type_camera_intrinsic_matrix = heif_fourcc('c', 'm', 'i', 'n')
heif_item_property_type_camera_intrinsic_matrix = heif_fourcc('c', 'm', 'i', 'n'),
heif_item_property_type_camera_extrinsic_matrix = heif_fourcc('c', 'm', 'e', 'x')
};

// Get the heif_property_id for a heif_item_id.
Expand Down Expand Up @@ -216,6 +217,30 @@ struct heif_error heif_item_add_property_camera_intrinsic_matrix(const struct he
const struct heif_camera_intrinsic_matrix* matrix,
heif_property_id* out_propertyId);


LIBHEIF_API
struct heif_error heif_item_get_property_camera_extrinsic_matrix(const struct heif_context* context,
heif_item_id itemId,
heif_property_id propertyId,
struct heif_camera_extrinsic_matrix** out_matrix);

LIBHEIF_API
void heif_camera_extrinsic_matrix_release(struct heif_camera_extrinsic_matrix* matrix);

// `out_matrix` must point to a 9-element matrix, which will be filled in row-major order.
LIBHEIF_API
struct heif_error heif_camera_extrinsic_matrix_get_rotation_matrix(const struct heif_camera_extrinsic_matrix* matrix,
double* out_matrix);

// `out_vector` must point to a 3-element vector, which will be filled with the (X,Y,Z) coordinates (in micrometers).
LIBHEIF_API
struct heif_error heif_camera_extrinsic_matrix_get_position_vector(const struct heif_camera_extrinsic_matrix* matrix,
int32_t* out_vector);

LIBHEIF_API
struct heif_error heif_camera_extrinsic_matrix_get_world_coordinate_system_id(const struct heif_camera_extrinsic_matrix* matrix,
uint32_t* out_wcs_id);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 675dee7

Please sign in to comment.