Skip to content

Commit

Permalink
implement decoding progress reporting (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 15, 2024
1 parent 8d81f48 commit 009c34e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
21 changes: 21 additions & 0 deletions examples/heif_dec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,24 @@ int decode_image_tiles(heif_image_handle* handle,
return 0;
}

static int max_value_progress = 0;

void start_progress(enum heif_progress_step step, int max_progress, void* progress_user_data)
{
max_value_progress = max_progress;
}

void on_progress(enum heif_progress_step step, int progress, void* progress_user_data)
{
std::cout << "decoding image... " << progress * 100 / max_value_progress << "%\r";
std::cout.flush();
}

void end_progress(enum heif_progress_step step, void* progress_user_data)
{
std::cout << "\n";
}


class LibHeifInitializer {
public:
Expand Down Expand Up @@ -835,6 +853,9 @@ int main(int argc, char** argv)

decode_options->strict_decoding = strict_decoding;
decode_options->decoder_id = decoder_id;
decode_options->start_progress = start_progress;
decode_options->on_progress = on_progress;
decode_options->end_progress = end_progress;

if (chroma_upsampling=="nearest-neighbor") {
decode_options->color_conversion_options.preferred_chroma_upsampling_algorithm = heif_chroma_upsampling_nearest_neighbor;
Expand Down
28 changes: 25 additions & 3 deletions libheif/image-items/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
uint32_t tile_width = 0;
uint32_t tile_height = 0;

if (options.start_progress) {
options.start_progress(heif_progress_step_total, grid.get_rows() * grid.get_columns(), options.progress_user_data);
}
if (options.on_progress) {
options.on_progress(heif_progress_step_total, 0, options.progress_user_data);
}

int progress_counter = 0;

for (uint32_t y = 0; y < grid.get_rows(); y++) {
uint32_t x0 = 0;

Expand Down Expand Up @@ -324,7 +333,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
if (1)
#endif
{
err = decode_and_paste_tile_image(tileID, x0, y0, img, options);
err = decode_and_paste_tile_image(tileID, x0, y0, img, options, progress_counter);
if (err) {
return err;
}
Expand Down Expand Up @@ -364,7 +373,8 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c

errs.push_back(std::async(std::launch::async,
&ImageItem_Grid::decode_and_paste_tile_image, this,
data.tileID, data.x_origin, data.y_origin, std::ref(img), options));
data.tileID, data.x_origin, data.y_origin, std::ref(img), options,
std::ref(progress_counter)));
}

// check for decoding errors in remaining tiles
Expand All @@ -380,12 +390,17 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
}
#endif

if (options.end_progress) {
options.end_progress(heif_progress_step_total, options.progress_user_data);
}

return img;
}

Error ImageItem_Grid::decode_and_paste_tile_image(heif_item_id tileID, uint32_t x0, uint32_t y0,
std::shared_ptr<HeifPixelImage>& inout_image,
const heif_decoding_options& options) const
const heif_decoding_options& options,
int& progress_counter) const
{
std::shared_ptr<HeifPixelImage> tile_img;

Expand Down Expand Up @@ -440,6 +455,13 @@ Error ImageItem_Grid::decode_and_paste_tile_image(heif_item_id tileID, uint32_t

inout_image->copy_image_to(tile_img, x0, y0);

if (options.on_progress) {
static std::mutex progressMutex;
std::lock_guard<std::mutex> lock(progressMutex);

options.on_progress(heif_progress_step_total, ++progress_counter, options.progress_user_data);
}

return Error::Ok;
}

Expand Down
2 changes: 1 addition & 1 deletion libheif/image-items/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class ImageItem_Grid : public ImageItem

Error decode_and_paste_tile_image(heif_item_id tileID, uint32_t x0, uint32_t y0,
std::shared_ptr<HeifPixelImage>& inout_image,
const heif_decoding_options& options) const;
const heif_decoding_options& options, int& progress_counter) const;
};


Expand Down

0 comments on commit 009c34e

Please sign in to comment.