Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/image-utils/ports/libheif.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def create(final):
# enable virtual-function-elimination, which also requires lto=full.
#
# `heif_context_read_from_memory_without_copy`
# -> `HeifContext::read` -> `HeifContext::interpret_heif_file`
# -> `HeifContext::interpret_heif_file_images`
# -> `HeifContext::read`
# -> `HeifContext::interpret_heif_file`
# -> `HeifContext::interpret_heif_file_images`
# -> `ImageItem::alloc_for_infe_box`
#
Expand Down
94 changes: 52 additions & 42 deletions packages/image-utils/src/codecs/heif.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
#include "heif.h"

#include <cstdlib>
#include <cstring>

#include <iterator>
#include <optional>
#include <string>

#include <emscripten/val.h>
#include <libheif/heif_context.h>
#include <libheif/heif_image_handle.h>
#include <libheif/heif_metadata.h>

#include "../constants.hpp"
#include "heif.h"
#include "../common.h"
#include "../constants.h"

using namespace emscripten;
// Read the first four bytes as big-endian uint32_t
constexpr uint32_t read_be32(const uint8_t *data) noexcept {
return (static_cast<uint32_t>(data[0]) << 24) |
(static_cast<uint32_t>(data[1]) << 16) |
(static_cast<uint32_t>(data[2]) << 8) | static_cast<uint32_t>(data[3]);
}

std::optional<Uint8Array> heif_get_exif_data(const std::string heif_data) {
std::optional<Uint8Array>
heif_get_exif_data(const std::string heif_data) noexcept {
struct heif_context *ctx = heif_context_alloc();

struct heif_error read_err = heif_context_read_from_memory_without_copy(
Expand All @@ -29,48 +45,42 @@ std::optional<Uint8Array> heif_get_exif_data(const std::string heif_data) {
std::optional<Uint8Array> output = std::nullopt;

if (n == 1) {
size_t exif_size =
size_t heif_exif_size =
heif_image_handle_get_metadata_size(image_handle, exif_item_id);
uint8_t *exif_data = static_cast<uint8_t *>(malloc(exif_size));

if (exif_data != nullptr) {
struct heif_error metadata_err =
heif_image_handle_get_metadata(image_handle, exif_item_id, exif_data);

if (metadata_err.code == heif_error_Ok && exif_size >= 4) {
// The first four bytes are the offset to the start of the TIFF header
// of the Exif data
exif_size -= 4;
memmove(exif_data, exif_data + 4, exif_size);

const bool has_exif_header =
exif_size >= std::size(constants::ExifHeader) &&
std::equal(std::begin(constants::ExifHeader),
std::end(constants::ExifHeader), exif_data);

// From the very small sample size I've seen, it seems that HEIC files
// begin with the Exif header and AVIF files do not
if (has_exif_header) {
output = Uint8Array(val(typed_memory_view(exif_size, exif_data)));
} else {
auto *exif_data_with_header = static_cast<uint8_t *>(
malloc(std::size(constants::ExifHeader) + exif_size));

if (exif_data_with_header != nullptr) {
memcpy(exif_data_with_header, constants::ExifHeader,
std::size(constants::ExifHeader));
memcpy(exif_data_with_header + std::size(constants::ExifHeader),
exif_data, exif_size);

output = Uint8Array(val(
typed_memory_view(std::size(constants::ExifHeader) + exif_size,
exif_data_with_header)));
uint8_t *heif_exif_data =
static_cast<uint8_t *>(std::malloc(heif_exif_size));

if (heif_exif_data != nullptr) {
struct heif_error metadata_err = heif_image_handle_get_metadata(
image_handle, exif_item_id, heif_exif_data);

if (metadata_err.code == heif_error_Ok && heif_exif_size >= 4) {
// If tiff_offset is 10, then there is an Exif header (Exif\0\0). If
// tiff_offset is 4, then it is followed directly by the TIFF header
// (MM/II). It seems that based on my experiences, HEIC will have the
// header while AVIF will not. Since we always want the Exif header, use
// this offset to normalize the output
// https://github.com/Exiv2/exiv2/issues/2162#issuecomment-1079000778
// https://github.com/strukturag/libheif/blob/eda8f2dc3670d7c07cee3769b247476f86229c24/libheif/api/libheif/heif_metadata.h#L80-L81
const uint64_t tiff_offset = read_be32(heif_exif_data) + 4ULL;

if (tiff_offset < heif_exif_size) {
const size_t exif_size = std::size(constants::kExifHeader) +
(heif_exif_size - tiff_offset);
auto *exif_data = static_cast<uint8_t *>(std::malloc(exif_size));

if (exif_data != nullptr) {
std::memcpy(exif_data, constants::kExifHeader,
std::size(constants::kExifHeader));
std::memcpy(exif_data + std::size(constants::kExifHeader),
heif_exif_data + tiff_offset,
heif_exif_size - tiff_offset);
output = Uint8Array(emscripten::val(
emscripten::typed_memory_view(exif_size, exif_data)));
}
free(exif_data);
}
} else {
free(exif_data);
}
std::free(heif_exif_data);
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/image-utils/src/codecs/heif.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <optional>
#include <string>

#include "../common.h"
#include <emscripten/val.h>

std::optional<Uint8Array> heif_get_exif_data(const std::string heif_data);
std::optional<Uint8Array>
heif_get_exif_data(const std::string heif_data) noexcept;
77 changes: 47 additions & 30 deletions packages/image-utils/src/codecs/jpeg.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
#include <stdio.h>
#include "jpeg.h"

#include <jpeglib.h>
#include <cstring>

#include "../constants.hpp"
#include "jpeg.h"
#include <iterator>
#include <stdexcept>
#include <string>

using namespace emscripten;
#include <emscripten/val.h>
#include <jpeglib.h>

// "JFIF\0"
constexpr unsigned char JfifHeader[5] = {0x4A, 0x46, 0x49, 0x46, 0x00};
// "Adobe"
constexpr unsigned char AdobeHeader[5] = {0x41, 0x64, 0x6F, 0x62, 0x65};
#include "../common.h"
#include "../constants.h"

constexpr int JPEG_APP1 = JPEG_APP0 + 1;
constexpr int JPEG_APP14 = JPEG_APP0 + 14;
constexpr uint8_t kJfifHeader[5] = {'J', 'F', 'I', 'F', '\0'};
constexpr uint8_t kAdobeHeader[5] = {'A', 'd', 'o', 'b', 'e'};

constexpr int kApp1Marker = JPEG_APP0 + 1;
constexpr int kApp14Marker = JPEG_APP0 + 14;

// Does not include the two bytes needed for the data size
// https://github.com/winlibs/libjpeg/blob/66ef88ad97f3c47eb1c7b85f2637ff1073ebce9b/src/jcicc.c#L37
constexpr int MAX_BYTES_IN_MARKER = 0xFFFF - 2;
constexpr int kMaxBytesInMarker = 0xFFFF - 2;

/**
* @see {@link
* https://github.com/winlibs/libjpeg/blob/66ef88ad97f3c47eb1c7b85f2637ff1073ebce9b/src/jpegtran.c#L489}
*/
Uint8Array jpeg_set_exif_data(const std::string jpeg_data,
const std::string exif_data) {
if (exif_data.size() > MAX_BYTES_IN_MARKER) {
if (exif_data.size() > kMaxBytesInMarker) {
throw std::invalid_argument("Exif data is too big!");
}

Expand All @@ -38,38 +41,44 @@ Uint8Array jpeg_set_exif_data(const std::string jpeg_data,
jpeg_create_decompress(&srcinfo);
jpeg_create_compress(&dstinfo);

unsigned char *out_buffer;
size_t out_size;
unsigned char *output;
size_t output_size;
jpeg_mem_src(&srcinfo, reinterpret_cast<const JOCTET *>(jpeg_data.data()),
jpeg_data.size());
jpeg_mem_dest(&dstinfo, &out_buffer, &out_size);
jpeg_mem_dest(&dstinfo, &output, &output_size);

// Save all APPn markers
for (int index = 0; index < 16; index++) {
jpeg_save_markers(&srcinfo, JPEG_APP0 + index, 0xFFFF);
jpeg_save_markers(&srcinfo, JPEG_APP0 + index, /* length_limit */ 0xFFFF);
}

const int header_result = jpeg_read_header(&srcinfo,
/* require_image */ boolean::TRUE);

// Since require_image is true, header_result can only be
// JPEG_SUSPENDED/JPEG_HEADER_OK
if (header_result != JPEG_HEADER_OK) {
jpeg_destroy_compress(&dstinfo);
jpeg_destroy_decompress(&srcinfo);
throw std::invalid_argument("Invalid JPEG data");
}

// Get raw DCT coefficients for lossless encoding
// https://github.com/winlibs/libjpeg/blob/66ef88ad97f3c47eb1c7b85f2637ff1073ebce9b/src/jpeglib.h#L1131-L1132
jvirt_barray_ptr *src_coefficients = jpeg_read_coefficients(&srcinfo);

jpeg_copy_critical_parameters(&srcinfo, &dstinfo);

jpeg_write_coefficients(&dstinfo, src_coefficients);

bool is_exif_marker_found = false;
// Update marker in srcinfo with our Exif data
for (jpeg_saved_marker_ptr marker = srcinfo.marker_list; marker != NULL;
marker = marker->next) {
if (marker->marker == JPEG_APP1 &&
marker->data_length >= std::size(constants::ExifHeader) &&
memcmp(marker->data, constants::ExifHeader,
std::size(constants::ExifHeader)) == 0) {
if (marker->marker == kApp1Marker &&
marker->data_length >= std::size(constants::kExifHeader) &&
std::equal(std::begin(constants::kExifHeader),
std::end(constants::kExifHeader), marker->data)) {
is_exif_marker_found = true;
marker->data = reinterpret_cast<unsigned char *>(
const_cast<char *>(exif_data.data()));
Expand All @@ -80,26 +89,33 @@ Uint8Array jpeg_set_exif_data(const std::string jpeg_data,

for (jpeg_saved_marker_ptr marker = srcinfo.marker_list; marker != nullptr;
marker = marker->next) {
// Only write JFIF marker when dstinfo.write_JFIF_header (false for non-JFIF
// colorspaces)
if (dstinfo.write_JFIF_header && marker->marker == JPEG_APP0 &&
marker->data_length >= std::size(JfifHeader) &&
memcmp(marker->data, JfifHeader, std::size(JfifHeader)) == 0)
marker->data_length >= std::size(kJfifHeader) &&
std::equal(std::begin(kJfifHeader), std::end(kJfifHeader),
marker->data)) {
continue;
if (dstinfo.write_Adobe_marker && marker->marker == JPEG_APP14 &&
marker->data_length >= std::size(AdobeHeader) &&
memcmp(marker->data, AdobeHeader, std::size(AdobeHeader)) == 0)
}
// Only write Adobe marker when dstinfo.write_Adobe_marker
if (dstinfo.write_Adobe_marker && marker->marker == kApp14Marker &&
marker->data_length >= std::size(kAdobeHeader) &&
std::equal(std::begin(kAdobeHeader), std::end(kAdobeHeader),
marker->data)) {
continue;
}

jpeg_write_marker(&dstinfo, marker->marker, marker->data,
marker->data_length);
}

// Appending to the end of the JPEG markers if there is no original Exif
// marker since that is how libexif's exif CLI handles adding Exif data
// If there is no Exif marker in jpeg_data, append one to the end. This is how
// libexif's CLI tool exif handles adding Exif data
//
// https://github.com/libexif/exif/blob/9af1f54d5879e72d0b2bf78f65162db84ebcba26/libjpeg/jpeg-data.c#L493
if (!is_exif_marker_found) {
jpeg_write_marker(
&dstinfo, JPEG_APP1,
&dstinfo, kApp1Marker,
reinterpret_cast<unsigned char *>(const_cast<char *>(exif_data.data())),
exif_data.size());
}
Expand All @@ -110,5 +126,6 @@ Uint8Array jpeg_set_exif_data(const std::string jpeg_data,
jpeg_finish_decompress(&srcinfo);
jpeg_destroy_decompress(&srcinfo);

return Uint8Array(val(typed_memory_view(out_size, out_buffer)));
return Uint8Array(
emscripten::val(emscripten::typed_memory_view(output_size, output)));
}
4 changes: 3 additions & 1 deletion packages/image-utils/src/codecs/jpeg.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <optional>
#include <string>

#include "../common.h"
#include <emscripten/val.h>

Uint8Array jpeg_set_exif_data(const std::string jpeg_data,
const std::string exif_data);
Loading
Loading