mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 00:47:28 +01:00
1 parent
d259f7a579
commit
b003f9fe65
6 files changed
+308
-13
No files matched your search
@@ -20,6 +20,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wuffs_aux {
|
||||
|
||||
|
||||
@@ -93,6 +93,12 @@ DecodeImageCallbacks::SelectDecoder(uint32_t fourcc,
|
||||
return wuffs_base__image_decoder::unique_ptr(nullptr, &free);
|
||||
}
|
||||
|
||||
std::string //
|
||||
DecodeImageCallbacks::HandleMetadata(const wuffs_base__more_information& minfo,
|
||||
std::vector<uint8_t>&& raw) {
|
||||
return "";
|
||||
}
|
||||
|
||||
wuffs_base__pixel_format //
|
||||
DecodeImageCallbacks::SelectPixfmt(
|
||||
const wuffs_base__image_config& image_config) {
|
||||
@@ -181,6 +187,13 @@ DecodeImageArgQuirks::DefaultValue() {
|
||||
return DecodeImageArgQuirks(wuffs_base__empty_slice_u32());
|
||||
}
|
||||
|
||||
DecodeImageArgFlags::DecodeImageArgFlags(uint64_t repr0) : repr(repr0) {}
|
||||
|
||||
DecodeImageArgFlags //
|
||||
DecodeImageArgFlags::DefaultValue() {
|
||||
return DecodeImageArgFlags(0);
|
||||
}
|
||||
|
||||
DecodeImageArgPixelBlend::DecodeImageArgPixelBlend(
|
||||
wuffs_base__pixel_blend repr0)
|
||||
: repr(repr0) {}
|
||||
@@ -241,12 +254,29 @@ DecodeImageAdvanceIOBuf(sync_io::Input& input,
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string //
|
||||
DecodeImageHandleMetadata(wuffs_base__image_decoder::unique_ptr& image_decoder,
|
||||
DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
wuffs_base__io_buffer& io_buf) {
|
||||
std::vector<uint8_t> raw;
|
||||
wuffs_base__io_buffer empty = wuffs_base__empty_io_buffer();
|
||||
wuffs_base__more_information minfo = wuffs_base__empty_more_information();
|
||||
wuffs_base__status status =
|
||||
image_decoder->tell_me_more(&empty, &minfo, &io_buf);
|
||||
if (status.repr != NULL) {
|
||||
return status.message();
|
||||
}
|
||||
return callbacks.HandleMetadata(minfo, std::move(raw));
|
||||
}
|
||||
|
||||
DecodeImageResult //
|
||||
DecodeImage0(wuffs_base__image_decoder::unique_ptr& image_decoder,
|
||||
DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
wuffs_base__io_buffer& io_buf,
|
||||
wuffs_base__slice_u32 quirks,
|
||||
uint64_t flags,
|
||||
wuffs_base__pixel_blend pixel_blend,
|
||||
wuffs_base__color_u32_argb_premul background_color,
|
||||
uint32_t max_incl_dimension) {
|
||||
@@ -319,6 +349,19 @@ redirect:
|
||||
image_decoder->set_quirk_enabled(quirks.ptr[i], true);
|
||||
}
|
||||
|
||||
// Apply flags.
|
||||
if (flags != 0) {
|
||||
if (flags & DecodeImageArgFlags::REPORT_METADATA_CHRM) {
|
||||
image_decoder->set_report_metadata(WUFFS_BASE__FOURCC__CHRM, true);
|
||||
}
|
||||
if (flags & DecodeImageArgFlags::REPORT_METADATA_GAMA) {
|
||||
image_decoder->set_report_metadata(WUFFS_BASE__FOURCC__GAMA, true);
|
||||
}
|
||||
if (flags & DecodeImageArgFlags::REPORT_METADATA_SRGB) {
|
||||
image_decoder->set_report_metadata(WUFFS_BASE__FOURCC__SRGB, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Decode the image config.
|
||||
while (true) {
|
||||
wuffs_base__status id_dic_status =
|
||||
@@ -331,6 +374,12 @@ redirect:
|
||||
}
|
||||
redirected = true;
|
||||
goto redirect;
|
||||
} else if (id_dic_status.repr == wuffs_base__note__metadata_reported) {
|
||||
std::string error_message =
|
||||
DecodeImageHandleMetadata(image_decoder, callbacks, input, io_buf);
|
||||
if (!error_message.empty()) {
|
||||
return DecodeImageResult(std::move(error_message));
|
||||
}
|
||||
} else if (id_dic_status.repr != wuffs_base__suspension__short_read) {
|
||||
return DecodeImageResult(id_dic_status.message());
|
||||
} else if (io_buf.meta.closed) {
|
||||
@@ -454,6 +503,7 @@ DecodeImageResult //
|
||||
DecodeImage(DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
DecodeImageArgQuirks quirks,
|
||||
DecodeImageArgFlags flags,
|
||||
DecodeImageArgPixelBlend pixel_blend,
|
||||
DecodeImageArgBackgroundColor background_color,
|
||||
DecodeImageArgMaxInclDimension max_incl_dimension) {
|
||||
@@ -469,8 +519,8 @@ DecodeImage(DecodeImageCallbacks& callbacks,
|
||||
|
||||
wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);
|
||||
DecodeImageResult result = DecodeImage0(
|
||||
image_decoder, callbacks, input, *io_buf, quirks.repr, pixel_blend.repr,
|
||||
background_color.repr, max_incl_dimension.repr);
|
||||
image_decoder, callbacks, input, *io_buf, quirks.repr, flags.repr,
|
||||
pixel_blend.repr, background_color.repr, max_incl_dimension.repr);
|
||||
callbacks.Done(result, input, *io_buf, std::move(image_decoder));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -30,10 +30,11 @@ struct DecodeImageResult {
|
||||
// DecodeImageCallbacks are the callbacks given to DecodeImage. They are always
|
||||
// called in this order:
|
||||
// 1. SelectDecoder
|
||||
// 2. SelectPixfmt
|
||||
// 3. AllocPixbuf
|
||||
// 4. AllocWorkbuf
|
||||
// 5. Done
|
||||
// 2. HandleMetadata
|
||||
// 3. SelectPixfmt
|
||||
// 4. AllocPixbuf
|
||||
// 5. AllocWorkbuf
|
||||
// 6. Done
|
||||
//
|
||||
// It may return early - the third callback might not be invoked if the second
|
||||
// one fails - but the final callback (Done) is always invoked.
|
||||
@@ -90,6 +91,21 @@ class DecodeImageCallbacks {
|
||||
virtual wuffs_base__image_decoder::unique_ptr //
|
||||
SelectDecoder(uint32_t fourcc, wuffs_base__slice_u8 prefix);
|
||||
|
||||
// HandleMetadata acknowledges image metadata. minfo.flavor will be one of:
|
||||
// - WUFFS_BASE__MORE_INFORMATION__FLAVOR__METADATA_RAW
|
||||
// - WUFFS_BASE__MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
// If it is ETC__METADATA_RAW then raw contains the metadata bytes.
|
||||
//
|
||||
// minfo.metadata__fourcc() will typically match one of the
|
||||
// DecodeImageArgFlags bits. For example, if (REPORT_METADATA_CHRM |
|
||||
// REPORT_METADATA_GAMA) was passed to DecodeImage then the metadata FourCC
|
||||
// will be either WUFFS_BASE__FOURCC__CHRM or WUFFS_BASE__FOURCC__GAMA.
|
||||
//
|
||||
// It returns an error message, or an empty string on success.
|
||||
virtual std::string //
|
||||
HandleMetadata(const wuffs_base__more_information& minfo,
|
||||
std::vector<uint8_t>&& raw);
|
||||
|
||||
// SelectPixfmt returns the destination pixel format for AllocPixbuf. It
|
||||
// should return wuffs_base__make_pixel_format(etc) called with one of:
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGR_565
|
||||
@@ -183,6 +199,42 @@ struct DecodeImageArgQuirks {
|
||||
wuffs_base__slice_u32 repr;
|
||||
};
|
||||
|
||||
// DecodeImageArgFlags wraps an optional argument to DecodeImage.
|
||||
struct DecodeImageArgFlags {
|
||||
explicit DecodeImageArgFlags(uint64_t repr0);
|
||||
|
||||
// DefaultValue returns 0.
|
||||
static DecodeImageArgFlags DefaultValue();
|
||||
|
||||
// TODO: support all of the REPORT_METADATA_ETC flags, not just CHRM, GAMA
|
||||
// and SRGB.
|
||||
|
||||
// Background Color.
|
||||
static constexpr uint64_t REPORT_METADATA_BGCL = 0x0001;
|
||||
// Primary Chromaticities and White Point.
|
||||
static constexpr uint64_t REPORT_METADATA_CHRM = 0x0002;
|
||||
// Exchangeable Image File Format.
|
||||
static constexpr uint64_t REPORT_METADATA_EXIF = 0x0004;
|
||||
// Gamma Correction.
|
||||
static constexpr uint64_t REPORT_METADATA_GAMA = 0x0008;
|
||||
// International Color Consortium Profile.
|
||||
static constexpr uint64_t REPORT_METADATA_ICCP = 0x0010;
|
||||
// Modification Time.
|
||||
static constexpr uint64_t REPORT_METADATA_MTIM = 0x0020;
|
||||
// Offset (2-Dimensional).
|
||||
static constexpr uint64_t REPORT_METADATA_OFS2 = 0x0040;
|
||||
// Physical Dimensions.
|
||||
static constexpr uint64_t REPORT_METADATA_PHYD = 0x0080;
|
||||
// Standard Red Green Blue (Rendering Intent).
|
||||
static constexpr uint64_t REPORT_METADATA_SRGB = 0x0100;
|
||||
// Text.
|
||||
static constexpr uint64_t REPORT_METADATA_TEXT = 0x0200;
|
||||
// Extensible Metadata Platform.
|
||||
static constexpr uint64_t REPORT_METADATA_XMP = 0x0400;
|
||||
|
||||
uint64_t repr;
|
||||
};
|
||||
|
||||
// DecodeImageArgPixelBlend wraps an optional argument to DecodeImage.
|
||||
struct DecodeImageArgPixelBlend {
|
||||
explicit DecodeImageArgPixelBlend(wuffs_base__pixel_blend repr0);
|
||||
@@ -261,6 +313,7 @@ DecodeImageResult //
|
||||
DecodeImage(DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
DecodeImageArgQuirks quirks = DecodeImageArgQuirks::DefaultValue(),
|
||||
DecodeImageArgFlags flags = DecodeImageArgFlags::DefaultValue(),
|
||||
DecodeImageArgPixelBlend pixel_blend =
|
||||
DecodeImageArgPixelBlend::DefaultValue(),
|
||||
DecodeImageArgBackgroundColor background_color =
|
||||
|
||||
Reference in new issue
Block a user