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 experimental gain map support #1121

Open
wants to merge 7 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
3 changes: 3 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"WITH_OpenJPEG_ENCODER_PLUGIN" : "OFF",
"WITH_FFMPEG_DECODER" : "ON",
"WITH_FFMPEG_DECODER_PLUGIN" : "OFF",
"WITH_EXPERIMENTAL_GAIN_MAP" : "OFF",

"WITH_REDUCED_VISIBILITY" : "OFF",
"WITH_DEFLATE_HEADER_COMPRESSION" : "ON",
Expand Down Expand Up @@ -88,6 +89,7 @@
"WITH_OpenJPEG_ENCODER_PLUGIN" : "ON",
"WITH_FFMPEG_DECODER" : "ON",
"WITH_FFMPEG_DECODER_PLUGIN" : "ON",
"WITH_EXPERIMENTAL_GAIN_MAP" : "OFF",

"WITH_REDUCED_VISIBILITY" : "ON",
"WITH_DEFLATE_HEADER_COMPRESSION" : "ON",
Expand Down Expand Up @@ -120,6 +122,7 @@
"WITH_OpenJPEG_DECODER" : "OFF",
"WITH_OpenJPEG_ENCODER" : "OFF",
"WITH_FFMPEG_DECODER" : "OFF",
"WITH_EXPERIMENTAL_GAIN_MAP" : "OFF",

"WITH_REDUCED_VISIBILITY" : "ON",
"WITH_DEFLATE_HEADER_COMPRESSION" : "OFF",
Expand Down
29 changes: 28 additions & 1 deletion examples/heif_enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#include "benchmark.h"
#include "libheif/exif.h"
#include "common.h"
#if WITH_EXPERIMENTAL_GAIN_MAP
#include "libheif/heif_gain_map.h"
#endif

int master_alpha = 1;
int thumb_alpha = 1;
Expand Down Expand Up @@ -724,10 +727,18 @@ int main(int argc, char** argv)
struct heif_error error;

std::shared_ptr<heif_image> primary_image;
struct heif_image_handle* handle;

for (; optind < argc; optind++) {
std::string input_filename = argv[optind];

#if WITH_EXPERIMENTAL_GAIN_MAP
bool is_gain_map = false;
if (input_filename.find("gain_map") != std::string::npos) {
is_gain_map = true;
}
#endif

if (output_filename.empty()) {
std::string filename_without_suffix;
std::string::size_type dot_position = input_filename.find_last_of('.');
Expand Down Expand Up @@ -865,12 +876,28 @@ int main(int argc, char** argv)
}


struct heif_image_handle* handle;
error = heif_context_encode_image(context.get(),
image.get(),
encoder,
options,
&handle);

#if WITH_EXPERIMENTAL_GAIN_MAP
if (is_gain_map) {
struct heif_image_handle* gain_map_image_handle;
heif_gain_map_metadata gmm;

error = heif_context_encode_gain_map_image(context.get(),
image.get(),
handle,
encoder,
nullptr,
&gmm,
&gain_map_image_handle);
heif_image_handle_release(gain_map_image_handle);
}
#endif

if (error.code != 0) {
heif_encoding_options_free(options);
std::cerr << "Could not encode HEIF/AVIF file: " << error.message << "\n";
Expand Down
20 changes: 20 additions & 0 deletions examples/heif_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
#include <assert.h>
#include <stdio.h>
#include "common.h"
#if WITH_EXPERIMENTAL_GAIN_MAP
#include "libheif/heif_gain_map.h"
#endif


/*
Expand Down Expand Up @@ -687,6 +690,23 @@ int main(int argc, char** argv)
}
}

#if WITH_EXPERIMENTAL_GAIN_MAP
// gain map pixels
heif_image_handle* gain_map_image_handle;
heif_image* gain_map_image;
err = heif_context_get_gain_map_image_handle(ctx.get(), &gain_map_image_handle);
if (err.code != heif_error_Ok) {
goto __exit;
}
heif_decode_image(gain_map_image_handle, &gain_map_image, heif_colorspace_undefined, heif_chroma_undefined, nullptr);

// gain map metadata
heif_gain_map_metadata gmm;
heif_image_get_gain_map_metadata(ctx.get(), &gmm);
gmm.dump();
__exit:
#endif

heif_image_release(image);

heif_image_handle_release(handle);
Expand Down
7 changes: 7 additions & 0 deletions libheif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ if (WITH_UNCOMPRESSED_CODEC)
uncompressed_image.cc)
endif ()

if (WITH_EXPERIMENTAL_GAIN_MAP)
target_compile_definitions(heif PUBLIC WITH_EXPERIMENTAL_GAIN_MAP=1)
target_sources(heif PRIVATE
heif_gain_map.h
heif_gain_map.cc)
endif ()

write_basic_package_version_file(${PROJECT_NAME}-config-version.cmake COMPATIBILITY ExactVersion)

install(TARGETS heif EXPORT ${PROJECT_NAME}-config
Expand Down
24 changes: 24 additions & 0 deletions libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,30 @@ Error Box_mdcv::write(StreamWriter& writer) const
return Error::Ok;
}

#if WITH_EXPERIMENTAL_GAIN_MAP
void Box_altr::add_item_id(heif_item_id id) {
m_item_IDs.push_back(id);
}

Error Box_altr::write(StreamWriter& writer) const
{
size_t box_start = reserve_box_header_space(writer);

writer.write32(1); // unsigned int(32) group_id;
writer.write32((uint32_t) m_item_IDs.size()); // unsigned int(32) num_entities_in_group;
for (uint32_t i = 0; i < m_item_IDs.size(); i++) {
writer.write32((uint32_t) m_item_IDs[i]); // unsigned int(32) entity_id;
}
prepend_header(writer, box_start);

return Error::Ok;
}

Error Box_altr::parse(BitstreamRange& range)
{
return range.get_error();
}
#endif

Error Box_ipco::get_properties_for_item_ID(uint32_t itemID,
const std::shared_ptr<class Box_ipma>& ipma,
Expand Down
31 changes: 31 additions & 0 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ class Box_infe : public FullBox

const std::string& get_item_type() const { return m_item_type; }

#if WITH_EXPERIMENTAL_GAIN_MAP
const std::string& get_item_name() const { return m_item_name; }
#endif

void set_item_type(const std::string& type) { m_item_type = type; }

void set_item_name(const std::string& name) { m_item_name = name; }
Expand Down Expand Up @@ -812,6 +816,13 @@ class Box_idat : public Box
class Box_grpl : public Box
{
public:
#if WITH_EXPERIMENTAL_GAIN_MAP
Box_grpl()
{
set_short_type(fourcc("grpl"));
}
#endif

std::string dump(Indent&) const override;

protected:
Expand Down Expand Up @@ -965,6 +976,26 @@ class Box_mdcv : public Box
Error parse(BitstreamRange& range) override;
};

#if WITH_EXPERIMENTAL_GAIN_MAP
class Box_altr : public Box
{
public:
Box_altr()
{
set_short_type(fourcc("altr"));
}

void add_item_id(heif_item_id id);

Error write(StreamWriter& writer) const override;

protected:
Error parse(BitstreamRange& range) override;

private:
std::vector<heif_item_id> m_item_IDs;
};
#endif

/**
* User Description property.
Expand Down
Loading
Loading