Skip to content

Add heif_items API to emscripten #1462

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

build
_build
buildjs
third-party/SVT-AV1/
third-party/dav1d/
third-party/libwebp/
Expand Down
139 changes: 118 additions & 21 deletions libheif/api/libheif/heif_emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include <cassert>

#include "heif.h"
#include "heif_items.h"

static std::string _heif_get_version()
static std::string heif_js_get_version()
{
return heif_get_version();
}

static struct heif_error _heif_context_read_from_memory(
static struct heif_error heif_js_context_read_from_memory(
struct heif_context* context, const std::string& data)
{
return heif_context_read_from_memory(context, data.data(), data.size(), nullptr);
Expand Down Expand Up @@ -78,7 +79,7 @@ static emscripten::val heif_js_context_get_list_of_top_level_image_IDs(
return result;
}

heif_item_id* ids = (heif_item_id*) malloc(count * sizeof(heif_item_id));
heif_item_id* ids = (heif_item_id*) alloca(count * sizeof(heif_item_id));
if (!ids) {
struct heif_error err;
err.code = heif_error_Memory_allocation_error;
Expand All @@ -95,10 +96,98 @@ static emscripten::val heif_js_context_get_list_of_top_level_image_IDs(
for (int i = 0; i < received; i++) {
result.set(i, ids[i]);
}
free(ids);
return result;
}


static emscripten::val heif_js_context_get_list_of_item_IDs(
struct heif_context* context)
{
emscripten::val result = emscripten::val::array();
if (!context) {
return result;
}

int count = heif_context_get_number_of_items(context);
if (count <= 0) {
return result;
}

heif_item_id* ids = (heif_item_id*) alloca(count * sizeof(heif_item_id));
if (!ids) {
struct heif_error err;
err.code = heif_error_Memory_allocation_error;
err.subcode = heif_suberror_Security_limit_exceeded;
return emscripten::val(err);
}

int num_ids_received = heif_context_get_list_of_item_IDs(context, ids, count);

for (int i = 0; i < num_ids_received; i++) {
result.set(i, ids[i]);
}

return result;
}


static emscripten::val heif_js_item_get_item_type(
const struct heif_context* ctx, heif_item_id id)
{
uint32_t type = heif_item_get_item_type(ctx, id);
std::string type_string = fourcc_to_string(type);
return emscripten::val(type_string);
}


static emscripten::val heif_js_item_get_mime_item_content_type(
const struct heif_context* ctx, heif_item_id id)
{
std::string content_type = "";
const char* cstring = heif_item_get_mime_item_content_type(ctx, id);
if (cstring) {
content_type = cstring;
}
return emscripten::val(content_type);
}


static emscripten::val heif_js_item_get_mime_item_content_encoding(
const struct heif_context* ctx, heif_item_id id)
{
std::string content_encoding = "";
const char* cstring = heif_item_get_mime_item_content_encoding(ctx, id);
if (cstring) {
content_encoding = cstring;
}
return emscripten::val(content_encoding);
}


static emscripten::val heif_js_item_get_uri_item_uri_type(
const struct heif_context* ctx, heif_item_id id)
{
std::string uri_type = "";
const char* cstring = heif_item_get_uri_item_uri_type(ctx, id);
if (cstring) {
uri_type = cstring;
}
return emscripten::val(uri_type);
}


static emscripten::val heif_js_item_get_item_name(
const struct heif_context* ctx, heif_item_id id)
{
std::string item_name = "";
const char* cstring = heif_item_get_item_name(ctx, id);
if (cstring) {
item_name = cstring;
}
return emscripten::val(item_name);
}


#if 0
static void strided_copy(void* dest, const void* src, int width, int height,
int stride)
Expand Down Expand Up @@ -288,33 +377,41 @@ static emscripten::val heif_js_decode_image2(struct heif_image_handle* handle,
emscripten::function(#name, &name, emscripten::allow_raw_pointers())

EMSCRIPTEN_BINDINGS(libheif) {
emscripten::function("heif_get_version", &_heif_get_version,
emscripten::allow_raw_pointers());

// heif.h
emscripten::function("heif_get_version", &heif_js_get_version, emscripten::allow_raw_pointers());
emscripten::function("heif_context_read_from_memory", &heif_js_context_read_from_memory, emscripten::allow_raw_pointers());
emscripten::function("heif_check_filetype", &heif_js_check_filetype, emscripten::allow_raw_pointers());
emscripten::function("heif_context_get_list_of_top_level_image_IDs", &heif_js_context_get_list_of_top_level_image_IDs, emscripten::allow_raw_pointers());
emscripten::function("heif_context_get_image_handle", &heif_js_context_get_image_handle, emscripten::allow_raw_pointers());
emscripten::function("heif_context_get_primary_image_handle", &heif_js_context_get_primary_image_handle, emscripten::allow_raw_pointers());
emscripten::function("heif_js_decode_image2", &heif_js_decode_image2, emscripten::allow_raw_pointers());
EXPORT_HEIF_FUNCTION(heif_get_version_number);

EXPORT_HEIF_FUNCTION(heif_context_alloc);
EXPORT_HEIF_FUNCTION(heif_context_free);
emscripten::function("heif_context_read_from_memory",
&_heif_context_read_from_memory, emscripten::allow_raw_pointers());
emscripten::function("heif_js_check_filetype",
&heif_js_check_filetype, emscripten::allow_raw_pointers());
EXPORT_HEIF_FUNCTION(heif_context_get_number_of_top_level_images);
emscripten::function("heif_js_context_get_list_of_top_level_image_IDs",
&heif_js_context_get_list_of_top_level_image_IDs, emscripten::allow_raw_pointers());
emscripten::function("heif_js_context_get_image_handle",
&heif_js_context_get_image_handle, emscripten::allow_raw_pointers());
emscripten::function("heif_js_context_get_primary_image_handle",
&heif_js_context_get_primary_image_handle, emscripten::allow_raw_pointers());
//emscripten::function("heif_js_decode_image",
//&heif_js_decode_image, emscripten::allow_raw_pointers());
emscripten::function("heif_js_decode_image2",
&heif_js_decode_image2, emscripten::allow_raw_pointers());
EXPORT_HEIF_FUNCTION(heif_image_handle_release);
EXPORT_HEIF_FUNCTION(heif_image_handle_get_width);
EXPORT_HEIF_FUNCTION(heif_image_handle_get_height);
EXPORT_HEIF_FUNCTION(heif_image_handle_is_primary_image);
EXPORT_HEIF_FUNCTION(heif_image_release);

// heif_items.h
emscripten::function("heif_context_get_list_of_item_IDs", &heif_js_context_get_list_of_item_IDs, emscripten::allow_raw_pointers());
emscripten::function("heif_item_get_item_type", heif_js_item_get_item_type, emscripten::allow_raw_pointers());
emscripten::function("heif_item_get_mime_item_content_type", heif_js_item_get_mime_item_content_type, emscripten::allow_raw_pointers());
emscripten::function("heif_item_get_mime_item_content_encoding", heif_js_item_get_mime_item_content_encoding, emscripten::allow_raw_pointers());
emscripten::function("heif_item_get_uri_item_uri_type", heif_js_item_get_uri_item_uri_type, emscripten::allow_raw_pointers());
emscripten::function("heif_item_get_item_name", heif_js_item_get_item_name, emscripten::allow_raw_pointers());
EXPORT_HEIF_FUNCTION(heif_context_get_number_of_items);
EXPORT_HEIF_FUNCTION(heif_item_is_item_hidden);

// DEPRECATED, use functions without the 'js' prefix.
emscripten::function("heif_js_check_filetype", &heif_js_check_filetype, emscripten::allow_raw_pointers());
emscripten::function("heif_js_context_get_list_of_top_level_image_IDs", &heif_js_context_get_list_of_top_level_image_IDs, emscripten::allow_raw_pointers());
emscripten::function("heif_js_context_get_image_handle", &heif_js_context_get_image_handle, emscripten::allow_raw_pointers());
emscripten::function("heif_js_context_get_primary_image_handle", &heif_js_context_get_primary_image_handle, emscripten::allow_raw_pointers());

emscripten::enum_<heif_error_code>("heif_error_code")
.value("heif_error_Ok", heif_error_Ok)
.value("heif_error_Input_does_not_exist", heif_error_Input_does_not_exist)
Expand Down
Loading