Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience bindings for javascript to check filetype and to get primary image handle #1218

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

EzraH442
Copy link
Contributor

@EzraH442 EzraH442 commented Jul 3, 2024

Currently if you want to check the filetype of some buffer in javascript you need to do something like:

const dummy_buffer = new Uint8Array([]) // pretend this has the image data

const ptr = Module._malloc(12);
if (!ptr) {
  //...
}
Module.HEAPU8.set(dummy_buffer.subarray(0, 12), ptr);
const res = Module._heif_check_filetype(ptr, 12);
Module._free(ptr);
if (res === 1) {
 // valid
}
// handle other cases

This is not only alot of manual memory management on the javascript side but also requires defining a copy of the heif_filetype_result enum on the javascript side to avoid using hardcoded numbers since the enum is not exported by the emscripten bindings.

With new bindings it would be simplified to:

const res = Module.heif_js_check_filetype(dummy_buffer)
if (res.value === Module.heif_filetype_result.heif_filetype_yes_supported.value) {
 // valid
}
// handle other cases

Additionally, (as far I know) the only way to get a proper javascript handle to the primary image is to first use heif_js_context_get_list_of_top_level_image_IDs to get all the image ids, then use heif_context_get_image_handle on each id, then finally use heif_image_handle_is_primary_image on each handle until the primary image handle is found. A special js binding like heif_js_context_get_primary_image_handle would be very convenient to avoid having to do this, especially since the function heif_context_get_primary_image_handle already exists.

@farindk farindk merged commit e8b33b2 into strukturag:master Jul 3, 2024
35 checks passed
@farindk
Copy link
Contributor

farindk commented Jul 3, 2024

Thank you. This is great.

I'm not an active user of the JS-binding myself and have only limited JS knowledge, thus I cannot really maintain this.
If you are interested in helping with the JS part, let me know.

@EzraH442
Copy link
Contributor Author

EzraH442 commented Jul 3, 2024

Thank you for replying so quickly. I would definitely be interested in help maintain the JS part. Some of the first things I'd like to do are update the build-emscripten.sh script to be compatible with the latest version of the emscripten sdk, add an option to the build script to use the --emit-tsd flag to generate typescript types, and add more JS convenience bindings for thumbnail images. Is there anything else that I should look into doing or anything I should avoid?

@farindk
Copy link
Contributor

farindk commented Jul 3, 2024

Great, that all makes sense. In fact, the JS binding was mostly written in one of the first versions and has never been updated much. Thus, a lot of the API is missing. Many things that were recently added and cover more the low-level access to the files are probably not that important, but I'm soon going to add support for extracting single tiles of the image (for just decoding a ROI), and for access to lower-resolution images in a multi-resolution pyramid. That could be very helpful for interactively browsing of large images. Especially if we could add a way to browse large files without first downloading the whole file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants