Skip to content

Commit

Permalink
allow user to cancel decoding process (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 15, 2024
1 parent 009c34e commit 820efc6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions go/heif/heif.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ const (
ErrorColorProfileDoesNotExist = C.heif_error_Color_profile_does_not_exist

ErrorPluginLoadingError = C.heif_error_Plugin_loading_error

ErrorCanceled = C.heif_error_Canceled
)

type ErrorSubcode C.enum_heif_suberror_code
Expand Down
9 changes: 8 additions & 1 deletion libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ struct heif_error heif_image_handle_get_depth_image_handle(const struct heif_ima

void fill_default_decoding_options(heif_decoding_options& options)
{
options.version = 5;
options.version = 6;

options.ignore_transformations = false;

Expand All @@ -1299,6 +1299,10 @@ void fill_default_decoding_options(heif_decoding_options& options)
options.color_conversion_options.preferred_chroma_downsampling_algorithm = heif_chroma_downsampling_average;
options.color_conversion_options.preferred_chroma_upsampling_algorithm = heif_chroma_upsampling_bilinear;
options.color_conversion_options.only_use_preferred_chroma_algorithm = false;

// version 6

options.cancel_decoding = nullptr;
}


Expand All @@ -1310,6 +1314,9 @@ static heif_decoding_options normalize_options(const heif_decoding_options* inpu

if (input_options) {
switch (input_options->version) {
case 6:
options.cancel_decoding = input_options->cancel_decoding;
// fallthrough
case 5:
options.color_conversion_options = input_options->color_conversion_options;
// fallthrough
Expand Down
12 changes: 9 additions & 3 deletions libheif/api/libheif/heif.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
// 1.15 4 5 1 1 1 1
// 1.16 5 6 1 1 1 1
// 1.18 5 7 1 1 1 1
// 1.19 5 7 2 1 1 1
// 1.19 6 7 2 1 1 1

#if defined(_MSC_VER) && !defined(LIBHEIF_STATIC_BUILD)
#ifdef LIBHEIF_EXPORTS
Expand Down Expand Up @@ -135,7 +135,10 @@ enum heif_error_code
heif_error_Color_profile_does_not_exist = 10,

// Error loading a dynamic plugin
heif_error_Plugin_loading_error = 11
heif_error_Plugin_loading_error = 11,

// Operation has been canceled
heif_error_Canceled = 12
};


Expand Down Expand Up @@ -1740,10 +1743,13 @@ struct heif_decoding_options
// The priority is defined in the plugin.
const char* decoder_id;


// version 5 options

struct heif_color_conversion_options color_conversion_options;

// version 6 options

int (* cancel_decoding)(void* progress_user_data);
};


Expand Down
3 changes: 2 additions & 1 deletion libheif/api/libheif/heif_emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ EMSCRIPTEN_BINDINGS(libheif) {
.value("heif_error_Decoder_plugin_error", heif_error_Decoder_plugin_error)
.value("heif_error_Encoder_plugin_error", heif_error_Encoder_plugin_error)
.value("heif_error_Encoding_error", heif_error_Encoding_error)
.value("heif_error_Color_profile_does_not_exist", heif_error_Color_profile_does_not_exist);
.value("heif_error_Color_profile_does_not_exist", heif_error_Color_profile_does_not_exist)
.value("heif_error_Canceled", heif_error_Canceled);
emscripten::enum_<heif_suberror_code>("heif_suberror_code")
.value("heif_suberror_Unspecified", heif_suberror_Unspecified)
.value("heif_suberror_Cannot_write_output_data", heif_suberror_Cannot_write_output_data)
Expand Down
2 changes: 2 additions & 0 deletions libheif/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const char* Error::get_error_string(heif_error_code err)
return "Color profile does not exist";
case heif_error_Plugin_loading_error:
return "Error while loading plugin";
case heif_error_Canceled:
return "Canceled by user";
}

assert(false);
Expand Down
24 changes: 21 additions & 3 deletions libheif/image-items/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,12 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
}

int progress_counter = 0;
bool cancelled = false;

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

for (uint32_t x = 0; x < grid.get_columns(); x++) {
for (uint32_t x = 0; x < grid.get_columns() && !cancelled; x++) {

heif_item_id tileID = image_references[reference_idx];

Expand Down Expand Up @@ -333,6 +334,12 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
if (1)
#endif
{
if (options.cancel_decoding) {
if (options.cancel_decoding(options.progress_user_data)) {
cancelled = true;
}
}

err = decode_and_paste_tile_image(tileID, x0, y0, img, options, progress_counter);
if (err) {
return err;
Expand All @@ -352,7 +359,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
// Process all tiles in a set of background threads.
// Do not start more than the maximum number of threads.

while (!tiles.empty()) {
while (!tiles.empty() && !cancelled) {

// If maximum number of threads running, wait until first thread finishes

Expand All @@ -366,6 +373,13 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
}


if (options.cancel_decoding) {
if (options.cancel_decoding(options.progress_user_data)) {
cancelled = true;
}
}


// Start a new decoding thread

tile_data data = tiles.front();
Expand Down Expand Up @@ -394,6 +408,10 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
options.end_progress(heif_progress_step_total, options.progress_user_data);
}

if (cancelled) {
return Error{heif_error_Canceled, heif_suberror_Unspecified, "Decoding the image was canceled"};
}

return img;
}

Expand Down

0 comments on commit 820efc6

Please sign in to comment.