Skip to content

Commit

Permalink
move encode_grid() to ImageItem_Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 14, 2024
1 parent 1236f2f commit ed36bc0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 79 deletions.
15 changes: 7 additions & 8 deletions libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3427,15 +3427,14 @@ struct heif_error heif_context_encode_grid(struct heif_context* ctx,
}

// Encode Grid
Error error;
std::shared_ptr<ImageItem> out_grid;
error = ctx->context->encode_grid(pixel_tiles,
rows, columns,
encoder,
options,
out_grid);
if (error != Error::Ok) {
return error.error_struct(ctx->context.get());
auto addGridResult = ImageItem_Grid::add_and_encode_full_grid(ctx->context.get(),
pixel_tiles,
rows, columns,
encoder,
options);
if (addGridResult.error) {
return addGridResult.error.error_struct(ctx->context.get());
}

// Mark as primary image
Expand Down
57 changes: 0 additions & 57 deletions libheif/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1243,63 +1243,6 @@ Error HeifContext::encode_image(const std::shared_ptr<HeifPixelImage>& pixel_ima
return error;
}

Error HeifContext::encode_grid(const std::vector<std::shared_ptr<HeifPixelImage>>& tiles,
uint16_t rows,
uint16_t columns,
struct heif_encoder* encoder,
const struct heif_encoding_options& options,
std::shared_ptr<ImageItem>& out_grid_image)
{
// Create ImageGrid
ImageGrid grid;
grid.set_num_tiles(columns, rows);
int tile_width = tiles[0]->get_width(heif_channel_interleaved);
int tile_height = tiles[0]->get_height(heif_channel_interleaved);
grid.set_output_size(tile_width * columns, tile_height * rows);
std::vector<uint8_t> grid_data = grid.write();

// Encode Tiles
Error error;
std::vector<heif_item_id> tile_ids;
for (int i=0; i<rows*columns; i++) {
std::shared_ptr<ImageItem> out_tile;
error = encode_image(tiles[i],
encoder,
options,
heif_image_input_class_normal,
out_tile);
heif_item_id tile_id = out_tile->get_id();
m_heif_file->get_infe_box(tile_id)->set_hidden_item(true); // only show the full grid
tile_ids.push_back(out_tile->get_id());
}

// Create Grid Item
heif_item_id grid_id = m_heif_file->add_new_image(fourcc("grid"));
out_grid_image = std::make_shared<ImageItem>(this, grid_id);
m_all_images.insert(std::make_pair(grid_id, out_grid_image));
const int construction_method = 1; // 0=mdat 1=idat
m_heif_file->append_iloc_data(grid_id, grid_data, construction_method);

// Connect tiles to grid
m_heif_file->add_iref_reference(grid_id, fourcc("dimg"), tile_ids);

// Add ISPE property
int image_width = tile_width * columns;
int image_height = tile_height * rows;
m_heif_file->add_ispe_property(grid_id, image_width, image_height, false);

// Add PIXI property (copy from first tile)
auto pixi = m_heif_file->get_property<Box_pixi>(tile_ids[0]);
m_heif_file->add_property(grid_id, pixi, true);

// Set Brands
m_heif_file->set_brand(encoder->plugin->compression_format,
out_grid_image->is_miaf_compatible());

return error;
}


Error HeifContext::add_tiled_image_tile(heif_item_id tild_id, uint32_t tile_x, uint32_t tile_y,
const std::shared_ptr<HeifPixelImage>& image,
struct heif_encoder* encoder)
Expand Down
14 changes: 0 additions & 14 deletions libheif/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,13 @@

#include "region.h"

class HeifContext;

class HeifFile;

class HeifPixelImage;

class StreamWriter;

class ImageOverlay;

class ImageItem;
class ImageItem_Overlay;
class ImageItem_Tiled;
class ImageItem_uncompressed;


// This is a higher-level view than HeifFile.
Expand Down Expand Up @@ -135,13 +128,6 @@ class HeifContext : public ErrorBuffer
enum heif_image_input_class input_class,
std::shared_ptr<ImageItem>& out_image);

Error encode_grid(const std::vector<std::shared_ptr<HeifPixelImage>>& tiles,
uint16_t rows,
uint16_t columns,
struct heif_encoder* encoder,
const struct heif_encoding_options& options,
std::shared_ptr<ImageItem>& out_image);

Error add_tiled_image_tile(heif_item_id tili_id, uint32_t tile_x, uint32_t tile_y,
const std::shared_ptr<HeifPixelImage>& image,
struct heif_encoder* encoder);
Expand Down
62 changes: 62 additions & 0 deletions libheif/image-items/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <future>
#include <set>
#include <algorithm>
#include <libheif/api_structs.h>


Error ImageGrid::parse(const std::vector<uint8_t>& data)
Expand Down Expand Up @@ -642,3 +643,64 @@ Error ImageItem_Grid::add_image_tile(heif_item_id grid_id, uint32_t tile_x, uint

return Error::Ok;
}


Result<std::shared_ptr<ImageItem_Grid>> ImageItem_Grid::add_and_encode_full_grid(HeifContext* ctx,
const std::vector<std::shared_ptr<HeifPixelImage>>& tiles,
uint16_t rows,
uint16_t columns,
struct heif_encoder* encoder,
const struct heif_encoding_options& options)
{
std::shared_ptr<ImageItem_Grid> griditem;

// Create ImageGrid
ImageGrid grid;
grid.set_num_tiles(columns, rows);
uint32_t tile_width = tiles[0]->get_width(heif_channel_interleaved);
uint32_t tile_height = tiles[0]->get_height(heif_channel_interleaved);
grid.set_output_size(tile_width * columns, tile_height * rows);
std::vector<uint8_t> grid_data = grid.write();

auto file = ctx->get_heif_file();

// Encode Tiles
Error error;
std::vector<heif_item_id> tile_ids;
for (int i=0; i<rows*columns; i++) {
std::shared_ptr<ImageItem> out_tile;
error = ctx->encode_image(tiles[i],
encoder,
options,
heif_image_input_class_normal,
out_tile);
heif_item_id tile_id = out_tile->get_id();
file->get_infe_box(tile_id)->set_hidden_item(true); // only show the full grid
tile_ids.push_back(out_tile->get_id());
}

// Create Grid Item
heif_item_id grid_id = file->add_new_image(fourcc("grid"));
griditem = std::make_shared<ImageItem_Grid>(ctx, grid_id);
ctx->insert_new_image(grid_id, griditem);
const int construction_method = 1; // 0=mdat 1=idat
file->append_iloc_data(grid_id, grid_data, construction_method);

// Connect tiles to grid
file->add_iref_reference(grid_id, fourcc("dimg"), tile_ids);

// Add ISPE property
uint32_t image_width = tile_width * columns;
uint32_t image_height = tile_height * rows;
file->add_ispe_property(grid_id, image_width, image_height, false);

// Add PIXI property (copy from first tile)
auto pixi = file->get_property<Box_pixi>(tile_ids[0]);
file->add_property(grid_id, pixi, true);

// Set Brands
file->set_brand(encoder->plugin->compression_format,
griditem->is_miaf_compatible());

return error;
}
8 changes: 8 additions & 0 deletions libheif/image-items/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ class ImageItem_Grid : public ImageItem
const std::shared_ptr<HeifPixelImage>& image,
struct heif_encoder* encoder);

static Result<std::shared_ptr<ImageItem_Grid>> add_and_encode_full_grid(HeifContext* ctx,
const std::vector<std::shared_ptr<HeifPixelImage>>& tiles,
uint16_t rows,
uint16_t columns,
struct heif_encoder* encoder,
const struct heif_encoding_options& options);


// TODO: nclx depends on contained format
// const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }

Expand Down

0 comments on commit ed36bc0

Please sign in to comment.