Skip to content

Commit 9ab6f71

Browse files
committed
move tile encoding function to heif_tiling.h
1 parent 4bf7fc8 commit 9ab6f71

File tree

5 files changed

+298
-279
lines changed

5 files changed

+298
-279
lines changed

libheif/api/libheif/heif.cc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,100 @@
2020

2121
#include "heif.h"
2222
#include "heif_experimental.h"
23+
#include "error.h"
24+
#include "context.h"
25+
#include "api_structs.h"
26+
#include "file.h"
27+
28+
#include <vector>
29+
30+
31+
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES
32+
33+
static struct heif_error error_null_parameter = {heif_error_Usage_error,
34+
heif_suberror_Null_pointer_argument,
35+
"NULL passed"};
36+
37+
38+
struct heif_error heif_context_add_pyramid_entity_group(struct heif_context* ctx,
39+
const heif_item_id* layer_item_ids,
40+
size_t num_layers,
41+
/*
42+
uint16_t tile_width,
43+
uint16_t tile_height,
44+
uint32_t num_layers,
45+
const heif_pyramid_layer_info* in_layers,
46+
*/
47+
heif_item_id* out_group_id)
48+
{
49+
if (!layer_item_ids) {
50+
return error_null_parameter;
51+
}
52+
53+
if (num_layers == 0) {
54+
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "Number of layers cannot be 0."};
55+
}
56+
57+
std::vector<heif_item_id> layers(num_layers);
58+
for (size_t i = 0; i < num_layers; i++) {
59+
layers[i] = layer_item_ids[i];
60+
}
61+
62+
Result<heif_item_id> result = ctx->context->add_pyramid_group(layers);
63+
64+
if (result) {
65+
if (out_group_id) {
66+
*out_group_id = result.value;
67+
}
68+
return heif_error_success;
69+
}
70+
else {
71+
return result.error.error_struct(ctx->context.get());
72+
}
73+
}
74+
75+
76+
struct heif_pyramid_layer_info* heif_context_get_pyramid_entity_group_info(struct heif_context* ctx, heif_entity_group_id id, int* out_num_layers)
77+
{
78+
if (!out_num_layers) {
79+
return nullptr;
80+
}
81+
82+
std::shared_ptr<Box_EntityToGroup> groupBox = ctx->context->get_heif_file()->get_entity_group(id);
83+
if (!groupBox) {
84+
return nullptr;
85+
}
86+
87+
const auto pymdBox = std::dynamic_pointer_cast<Box_pymd>(groupBox);
88+
if (!pymdBox) {
89+
return nullptr;
90+
}
91+
92+
const std::vector<Box_pymd::LayerInfo> pymd_layers = pymdBox->get_layers();
93+
if (pymd_layers.empty()) {
94+
return nullptr;
95+
}
96+
97+
auto items = pymdBox->get_item_ids();
98+
assert(items.size() == pymd_layers.size());
99+
100+
auto* layerInfo = new heif_pyramid_layer_info[pymd_layers.size()];
101+
for (size_t i=0; i<pymd_layers.size(); i++) {
102+
layerInfo[i].layer_image_id = items[i];
103+
layerInfo[i].layer_binning = pymd_layers[i].layer_binning;
104+
layerInfo[i].tile_rows_in_layer = pymd_layers[i].tiles_in_layer_row_minus1 + 1;
105+
layerInfo[i].tile_columns_in_layer = pymd_layers[i].tiles_in_layer_column_minus1 + 1;
106+
}
107+
108+
*out_num_layers = static_cast<int>(pymd_layers.size());
109+
110+
return layerInfo;
111+
}
112+
23113

24114
void heif_pyramid_layer_info_release(struct heif_pyramid_layer_info* infos)
25115
{
26116
delete[] infos;
27117
}
118+
119+
#endif

libheif/api/libheif/heif_encoding.cc

Lines changed: 0 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -743,115 +743,6 @@ struct heif_error heif_context_encode_image(struct heif_context* ctx,
743743
}
744744

745745

746-
struct heif_error heif_context_encode_grid(struct heif_context* ctx,
747-
struct heif_image** tiles,
748-
uint16_t columns,
749-
uint16_t rows,
750-
struct heif_encoder* encoder,
751-
const struct heif_encoding_options* input_options,
752-
struct heif_image_handle** out_image_handle)
753-
{
754-
if (!encoder || !tiles) {
755-
return Error(heif_error_Usage_error,
756-
heif_suberror_Null_pointer_argument).error_struct(ctx->context.get());
757-
}
758-
else if (rows == 0 || columns == 0) {
759-
return Error(heif_error_Usage_error,
760-
heif_suberror_Invalid_parameter_value).error_struct(ctx->context.get());
761-
}
762-
763-
// TODO: Don't repeat this code from heif_context_encode_image()
764-
heif_encoding_options options;
765-
heif_color_profile_nclx nclx;
766-
set_default_encoding_options(options);
767-
if (input_options) {
768-
heif_encoding_options_copy(&options, input_options);
769-
770-
if (options.output_nclx_profile == nullptr) {
771-
auto input_nclx = tiles[0]->image->get_color_profile_nclx();
772-
if (input_nclx) {
773-
options.output_nclx_profile = &nclx;
774-
nclx.version = 1;
775-
nclx.color_primaries = (enum heif_color_primaries) input_nclx->get_colour_primaries();
776-
nclx.transfer_characteristics = (enum heif_transfer_characteristics) input_nclx->get_transfer_characteristics();
777-
nclx.matrix_coefficients = (enum heif_matrix_coefficients) input_nclx->get_matrix_coefficients();
778-
nclx.full_range_flag = input_nclx->get_full_range_flag();
779-
}
780-
}
781-
}
782-
783-
// Convert heif_images to a vector of HeifPixelImages
784-
std::vector<std::shared_ptr<HeifPixelImage>> pixel_tiles;
785-
for (int i=0; i<rows*columns; i++) {
786-
pixel_tiles.push_back(tiles[i]->image);
787-
}
788-
789-
// Encode Grid
790-
std::shared_ptr<ImageItem> out_grid;
791-
auto addGridResult = ImageItem_Grid::add_and_encode_full_grid(ctx->context.get(),
792-
pixel_tiles,
793-
rows, columns,
794-
encoder,
795-
options);
796-
if (addGridResult.error) {
797-
return addGridResult.error.error_struct(ctx->context.get());
798-
}
799-
800-
out_grid = addGridResult.value;
801-
802-
// Mark as primary image
803-
if (ctx->context->is_primary_image_set() == false) {
804-
ctx->context->set_primary_image(out_grid);
805-
}
806-
807-
if (out_image_handle) {
808-
*out_image_handle = new heif_image_handle;
809-
(*out_image_handle)->image = std::move(out_grid);
810-
(*out_image_handle)->context = ctx->context;
811-
}
812-
813-
return heif_error_success;
814-
}
815-
816-
817-
struct heif_error heif_context_add_grid_image(struct heif_context* ctx,
818-
uint32_t image_width,
819-
uint32_t image_height,
820-
uint32_t tile_columns,
821-
uint32_t tile_rows,
822-
const struct heif_encoding_options* encoding_options,
823-
struct heif_image_handle** out_grid_image_handle)
824-
{
825-
if (tile_rows == 0 || tile_columns == 0) {
826-
return Error(heif_error_Usage_error,
827-
heif_suberror_Invalid_parameter_value).error_struct(ctx->context.get());
828-
}
829-
else if (tile_rows > 0xFFFF || tile_columns > 0xFFFF) {
830-
return heif_error{heif_error_Usage_error,
831-
heif_suberror_Invalid_image_size,
832-
"Number of tile rows/columns may not exceed 65535"};
833-
}
834-
835-
auto generateGridItemResult = ImageItem_Grid::add_new_grid_item(ctx->context.get(),
836-
image_width,
837-
image_height,
838-
static_cast<uint16_t>(tile_rows),
839-
static_cast<uint16_t>(tile_columns),
840-
encoding_options);
841-
if (generateGridItemResult.error) {
842-
return generateGridItemResult.error.error_struct(ctx->context.get());
843-
}
844-
845-
if (out_grid_image_handle) {
846-
*out_grid_image_handle = new heif_image_handle;
847-
(*out_grid_image_handle)->image = generateGridItemResult.value;
848-
(*out_grid_image_handle)->context = ctx->context;
849-
}
850-
851-
return heif_error_success;
852-
}
853-
854-
855746
struct heif_error heif_context_add_overlay_image(struct heif_context* ctx,
856747
uint32_t image_width,
857748
uint32_t image_height,
@@ -906,63 +797,6 @@ struct heif_error heif_context_add_overlay_image(struct heif_context* ctx,
906797
}
907798

908799

909-
struct heif_error heif_context_add_tiled_image(struct heif_context* ctx,
910-
const struct heif_tiled_image_parameters* parameters,
911-
const struct heif_encoding_options* options, // TODO: do we need this?
912-
const struct heif_encoder* encoder,
913-
struct heif_image_handle** out_grid_image_handle)
914-
{
915-
if (out_grid_image_handle) {
916-
*out_grid_image_handle = nullptr;
917-
}
918-
919-
Result<std::shared_ptr<ImageItem_Tiled>> gridImageResult;
920-
gridImageResult = ImageItem_Tiled::add_new_tiled_item(ctx->context.get(), parameters, encoder);
921-
922-
if (gridImageResult.error != Error::Ok) {
923-
return gridImageResult.error.error_struct(ctx->context.get());
924-
}
925-
926-
if (out_grid_image_handle) {
927-
*out_grid_image_handle = new heif_image_handle;
928-
(*out_grid_image_handle)->image = gridImageResult.value;
929-
(*out_grid_image_handle)->context = ctx->context;
930-
}
931-
932-
return heif_error_success;
933-
}
934-
935-
936-
struct heif_error heif_context_add_image_tile(struct heif_context* ctx,
937-
struct heif_image_handle* tiled_image,
938-
uint32_t tile_x, uint32_t tile_y,
939-
const struct heif_image* image,
940-
struct heif_encoder* encoder)
941-
{
942-
if (auto tili_image = std::dynamic_pointer_cast<ImageItem_Tiled>(tiled_image->image)) {
943-
Error err = tili_image->add_image_tile(tile_x, tile_y, image->image, encoder);
944-
return err.error_struct(ctx->context.get());
945-
}
946-
#if WITH_UNCOMPRESSED_CODEC
947-
else if (auto unci = std::dynamic_pointer_cast<ImageItem_uncompressed>(tiled_image->image)) {
948-
Error err = unci->add_image_tile(tile_x, tile_y, image->image);
949-
return err.error_struct(ctx->context.get());
950-
}
951-
#endif
952-
else if (auto grid_item = std::dynamic_pointer_cast<ImageItem_Grid>(tiled_image->image)) {
953-
Error err = grid_item->add_image_tile(tile_x, tile_y, image->image, encoder);
954-
return err.error_struct(ctx->context.get());
955-
}
956-
else {
957-
return {
958-
heif_error_Usage_error,
959-
heif_suberror_Unspecified,
960-
"Cannot add tile to a non-tiled image"
961-
};
962-
}
963-
}
964-
965-
966800
struct heif_error heif_context_add_unci_image(struct heif_context* ctx,
967801
const struct heif_unci_image_parameters* parameters,
968802
const struct heif_encoding_options* encoding_options,

libheif/api/libheif/heif_encoding.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -350,43 +350,6 @@ struct heif_error heif_context_encode_image(struct heif_context*,
350350
const struct heif_encoding_options* options,
351351
struct heif_image_handle** out_image_handle);
352352

353-
/**
354-
* @brief Encodes an array of images into a grid.
355-
*
356-
* @param ctx The file context
357-
* @param tiles User allocated array of images that will form the grid.
358-
* @param rows The number of rows in the grid.
359-
* @param columns The number of columns in the grid.
360-
* @param encoder Defines the encoder to use. See heif_context_get_encoder_for_format()
361-
* @param input_options Optional, may be nullptr.
362-
* @param out_image_handle Returns a handle to the grid. The caller is responsible for freeing it.
363-
* @return Returns an error if ctx, tiles, or encoder is nullptr. If rows or columns is 0.
364-
*/
365-
LIBHEIF_API
366-
struct heif_error heif_context_encode_grid(struct heif_context* ctx,
367-
struct heif_image** tiles,
368-
uint16_t rows,
369-
uint16_t columns,
370-
struct heif_encoder* encoder,
371-
const struct heif_encoding_options* input_options,
372-
struct heif_image_handle** out_image_handle);
373-
374-
LIBHEIF_API
375-
struct heif_error heif_context_add_grid_image(struct heif_context* ctx,
376-
uint32_t image_width,
377-
uint32_t image_height,
378-
uint32_t tile_columns,
379-
uint32_t tile_rows,
380-
const struct heif_encoding_options* encoding_options,
381-
struct heif_image_handle** out_grid_image_handle);
382-
383-
LIBHEIF_API
384-
struct heif_error heif_context_add_image_tile(struct heif_context* ctx,
385-
struct heif_image_handle* tiled_image,
386-
uint32_t tile_x, uint32_t tile_y,
387-
const struct heif_image* image,
388-
struct heif_encoder* encoder);
389-
390353
// offsets[] should either be NULL (all offsets==0) or an array of size 2*nImages with x;y offset pairs.
391354
// If background_rgba is NULL, the background is transparent.
392355
LIBHEIF_API

0 commit comments

Comments
 (0)