mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Add DecodeImageCallbacks::SelectPixfmt
This commit is contained in:
1 parent
441e96a8d4
commit
5767f030a3
5 files changed
+175
-155
No files matched your search
@@ -9469,14 +9469,14 @@ struct DecodeImageResult {
|
||||
|
||||
// DecodeImageCallbacks are the callbacks given to DecodeImage. They are always
|
||||
// called in this order:
|
||||
// 1. OnImageFormat
|
||||
// 2. OnImageConfig
|
||||
// 3. AllocWorkbuf
|
||||
// 4. Done
|
||||
// 1. SelectDecoder
|
||||
// 2. SelectPixfmt
|
||||
// 3. AllocPixbuf
|
||||
// 4. AllocWorkbuf
|
||||
// 5. Done
|
||||
//
|
||||
// It may return early - the third callback might not be invoked if the second
|
||||
// one fails (returns a non-empty error message) - but the final callback
|
||||
// (Done) is always invoked.
|
||||
// one fails - but the final callback (Done) is always invoked.
|
||||
class DecodeImageCallbacks {
|
||||
public:
|
||||
// AllocResult holds a memory allocation (the result of malloc or new, a
|
||||
@@ -9491,19 +9491,19 @@ class DecodeImageCallbacks {
|
||||
std::string error_message;
|
||||
};
|
||||
|
||||
// OnImageFormat returns the image decoder for the input data's file format.
|
||||
// SelectDecoder returns the image decoder for the input data's file format.
|
||||
// Returning a nullptr means failure (DecodeImage_UnsupportedImageFormat).
|
||||
//
|
||||
// Common formats will have a FourCC value in the range [1 ..= 0x7FFF_FFFF],
|
||||
// such as WUFFS_BASE__FOURCC__JPEG. A zero FourCC value means that the
|
||||
// caller is responsible for examining the opening bytes (a prefix) of the
|
||||
// input data. OnImageFormat implementations should not modify those bytes.
|
||||
// input data. SelectDecoder implementations should not modify those bytes.
|
||||
//
|
||||
// OnImageFormat might be called more than once, since some image file
|
||||
// SelectDecoder might be called more than once, since some image file
|
||||
// formats can wrap others. For example, a nominal BMP file can actually
|
||||
// contain a JPEG or a PNG.
|
||||
//
|
||||
// The default OnImageFormat accepts the FOURCC codes listed below. For
|
||||
// The default SelectDecoder accepts the FOURCC codes listed below. For
|
||||
// modular builds (i.e. when #define'ing WUFFS_CONFIG__MODULES), acceptance
|
||||
// of the ETC file format is optional (for each value of ETC) and depends on
|
||||
// the corresponding module to be enabled at compile time (i.e. #define'ing
|
||||
@@ -9514,20 +9514,38 @@ class DecodeImageCallbacks {
|
||||
// - WUFFS_BASE__FOURCC__PNG
|
||||
// - WUFFS_BASE__FOURCC__WBMP
|
||||
virtual wuffs_base__image_decoder::unique_ptr //
|
||||
OnImageFormat(uint32_t fourcc, wuffs_base__slice_u8 prefix);
|
||||
SelectDecoder(uint32_t fourcc, wuffs_base__slice_u8 prefix);
|
||||
|
||||
// OnImageConfig allocates the pixel buffer.
|
||||
// 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
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGR
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL
|
||||
// or return image_config.pixcfg.pixel_format(). The latter means to use the
|
||||
// image file's natural pixel format. For example, GIF images' natural pixel
|
||||
// format is an indexed one.
|
||||
//
|
||||
// Returning otherwise means failure (DecodeImage_UnsupportedPixelFormat).
|
||||
//
|
||||
// The default SelectPixfmt implementation returns
|
||||
// wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL)
|
||||
// which is 4 bytes per pixel (8 bits per channel × 4 channels).
|
||||
virtual wuffs_base__pixel_format //
|
||||
SelectPixfmt(const wuffs_base__image_config& image_config);
|
||||
|
||||
// AllocPixbuf allocates the pixel buffer.
|
||||
//
|
||||
// allow_uninitialized_memory will be true if a valid background_color was
|
||||
// passed to DecodeImage, since the pixel buffer's contents will be
|
||||
// overwritten with that color after OnImageConfig returns.
|
||||
// overwritten with that color after AllocPixbuf returns.
|
||||
//
|
||||
// The default OnImageConfig implementation allocates either uninitialized or
|
||||
// The default AllocPixbuf implementation allocates either uninitialized or
|
||||
// zeroed memory. Zeroed memory typically corresponds to filling with opaque
|
||||
// black or transparent black, depending on the pixel format.
|
||||
virtual AllocResult //
|
||||
OnImageConfig(const wuffs_base__image_config& image_config,
|
||||
bool allow_uninitialized_memory);
|
||||
AllocPixbuf(const wuffs_base__image_config& image_config,
|
||||
bool allow_uninitialized_memory);
|
||||
|
||||
// AllocWorkbuf allocates the work buffer. The allocated buffer's length
|
||||
// should be at least len_range.min_incl, but larger allocations (up to
|
||||
@@ -9543,7 +9561,7 @@ class DecodeImageCallbacks {
|
||||
// not parsing the input encountered an error. Even when successful, trailing
|
||||
// data may remain in input and buffer.
|
||||
//
|
||||
// The image_decoder is the one returned by OnImageFormat (if OnImageFormat
|
||||
// The image_decoder is the one returned by SelectDecoder (if SelectDecoder
|
||||
// was successful), or a no-op unique_ptr otherwise. Like any unique_ptr,
|
||||
// ownership moves to the Done implementation.
|
||||
//
|
||||
@@ -9569,7 +9587,7 @@ extern const char DecodeImage_UnsupportedPixelConfiguration[];
|
||||
extern const char DecodeImage_UnsupportedPixelFormat[];
|
||||
|
||||
// DecodeImage decodes the image data in input. A variety of image file formats
|
||||
// can be decoded, depending on what callbacks.OnImageFormat returns.
|
||||
// can be decoded, depending on what callbacks.SelectDecoder returns.
|
||||
//
|
||||
// For animated formats, only the first frame is returned, since the API is
|
||||
// simpler for synchronous I/O and having DecodeImage only return when
|
||||
@@ -9594,25 +9612,14 @@ extern const char DecodeImage_UnsupportedPixelFormat[];
|
||||
// as the returned pixbuf_mem_owner. Regardless of success or failure, the work
|
||||
// buffer memory is deleted.
|
||||
//
|
||||
// If override_pixel_format_repr is zero then the pixel buffer will have the
|
||||
// image file's natural pixel format. For example, GIF images' natural pixel
|
||||
// format is an indexed one.
|
||||
//
|
||||
// If override_pixel_format_repr is non-zero (and one of the constants listed
|
||||
// below) then the image will be decoded to that particular pixel format:
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGR_565
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGR
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
// - WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL
|
||||
//
|
||||
// The pixel_blend (one of the constants listed below) determines how to
|
||||
// composite the decoded image over the pixel buffer's original pixels (as
|
||||
// returned by callbacks.OnImageConfig):
|
||||
// returned by callbacks.AllocPixbuf):
|
||||
// - WUFFS_BASE__PIXEL_BLEND__SRC
|
||||
// - WUFFS_BASE__PIXEL_BLEND__SRC_OVER
|
||||
//
|
||||
// The background_color is used to fill the pixel buffer after
|
||||
// callbacks.OnImageConfig returns, if it is valid in the
|
||||
// callbacks.AllocPixbuf returns, if it is valid in the
|
||||
// wuffs_base__color_u32_argb_premul__is_valid sense. The default value,
|
||||
// 0x0000_0001, is not valid since its Blue channel value (0x01) is greater
|
||||
// than its Alpha channel value (0x00). A valid background_color will typically
|
||||
@@ -9625,7 +9632,6 @@ extern const char DecodeImage_UnsupportedPixelFormat[];
|
||||
DecodeImageResult //
|
||||
DecodeImage(DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
uint32_t override_pixel_format_repr,
|
||||
wuffs_base__pixel_blend pixel_blend = WUFFS_BASE__PIXEL_BLEND__SRC,
|
||||
wuffs_base__color_u32_argb_premul background_color = 1, // Invalid.
|
||||
uint32_t max_incl_dimension = 1048575); // 0x000F_FFFF
|
||||
@@ -36910,7 +36916,7 @@ DecodeImageCallbacks::AllocResult::AllocResult(std::string&& error_message0)
|
||||
error_message(std::move(error_message0)) {}
|
||||
|
||||
wuffs_base__image_decoder::unique_ptr //
|
||||
DecodeImageCallbacks::OnImageFormat(uint32_t fourcc,
|
||||
DecodeImageCallbacks::SelectDecoder(uint32_t fourcc,
|
||||
wuffs_base__slice_u8 prefix) {
|
||||
switch (fourcc) {
|
||||
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BMP)
|
||||
@@ -36946,10 +36952,16 @@ DecodeImageCallbacks::OnImageFormat(uint32_t fourcc,
|
||||
return wuffs_base__image_decoder::unique_ptr(nullptr, &free);
|
||||
}
|
||||
|
||||
wuffs_base__pixel_format //
|
||||
DecodeImageCallbacks::SelectPixfmt(
|
||||
const wuffs_base__image_config& image_config) {
|
||||
return wuffs_base__make_pixel_format(
|
||||
WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL);
|
||||
}
|
||||
|
||||
DecodeImageCallbacks::AllocResult //
|
||||
DecodeImageCallbacks::OnImageConfig(
|
||||
const wuffs_base__image_config& image_config,
|
||||
bool allow_uninitialized_memory) {
|
||||
DecodeImageCallbacks::AllocPixbuf(const wuffs_base__image_config& image_config,
|
||||
bool allow_uninitialized_memory) {
|
||||
uint32_t w = image_config.pixcfg.width();
|
||||
uint32_t h = image_config.pixcfg.height();
|
||||
if ((w == 0) || (h == 0)) {
|
||||
@@ -37047,21 +37059,10 @@ DecodeImage0(wuffs_base__image_decoder::unique_ptr& image_decoder,
|
||||
DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
wuffs_base__io_buffer& io_buf,
|
||||
uint32_t override_pixel_format_repr,
|
||||
wuffs_base__pixel_blend pixel_blend,
|
||||
wuffs_base__color_u32_argb_premul background_color,
|
||||
uint32_t max_incl_dimension) {
|
||||
// Check args.
|
||||
switch (override_pixel_format_repr) {
|
||||
case 0:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGR_565:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGR:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
|
||||
break;
|
||||
default:
|
||||
return DecodeImageResult(DecodeImage_UnsupportedPixelFormat);
|
||||
}
|
||||
switch (pixel_blend) {
|
||||
case WUFFS_BASE__PIXEL_BLEND__SRC:
|
||||
case WUFFS_BASE__PIXEL_BLEND__SRC_OVER:
|
||||
@@ -37117,8 +37118,8 @@ redirect:
|
||||
image_decoder.reset();
|
||||
}
|
||||
|
||||
// Make the image decoder.
|
||||
image_decoder = callbacks.OnImageFormat(
|
||||
// Select the image decoder.
|
||||
image_decoder = callbacks.SelectDecoder(
|
||||
(uint32_t)fourcc,
|
||||
fourcc ? wuffs_base__empty_slice_u8() : io_buf.reader_slice());
|
||||
if (!image_decoder) {
|
||||
@@ -37150,14 +37151,24 @@ redirect:
|
||||
}
|
||||
} while (false);
|
||||
|
||||
// Apply the override pixel format.
|
||||
// Select the pixel format.
|
||||
uint32_t w = image_config.pixcfg.width();
|
||||
uint32_t h = image_config.pixcfg.height();
|
||||
if ((w > max_incl_dimension) || (h > max_incl_dimension)) {
|
||||
return DecodeImageResult(DecodeImage_MaxInclDimensionExceeded);
|
||||
}
|
||||
if (override_pixel_format_repr != 0) {
|
||||
image_config.pixcfg.set(override_pixel_format_repr,
|
||||
wuffs_base__pixel_format pixel_format = callbacks.SelectPixfmt(image_config);
|
||||
if (pixel_format.repr != image_config.pixcfg.pixel_format().repr) {
|
||||
switch (pixel_format.repr) {
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGR_565:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGR:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
|
||||
break;
|
||||
default:
|
||||
return DecodeImageResult(DecodeImage_UnsupportedPixelFormat);
|
||||
}
|
||||
image_config.pixcfg.set(pixel_format.repr,
|
||||
WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, w, h);
|
||||
}
|
||||
|
||||
@@ -37171,16 +37182,16 @@ redirect:
|
||||
}
|
||||
bool valid_background_color =
|
||||
wuffs_base__color_u32_argb_premul__is_valid(background_color);
|
||||
DecodeImageCallbacks::AllocResult oic_result =
|
||||
callbacks.OnImageConfig(image_config, valid_background_color);
|
||||
if (!oic_result.error_message.empty()) {
|
||||
return DecodeImageResult(std::move(oic_result.error_message));
|
||||
} else if (oic_result.mem_slice.len < pixbuf_len_min_incl) {
|
||||
DecodeImageCallbacks::AllocResult alloc_pixbuf_result =
|
||||
callbacks.AllocPixbuf(image_config, valid_background_color);
|
||||
if (!alloc_pixbuf_result.error_message.empty()) {
|
||||
return DecodeImageResult(std::move(alloc_pixbuf_result.error_message));
|
||||
} else if (alloc_pixbuf_result.mem_slice.len < pixbuf_len_min_incl) {
|
||||
return DecodeImageResult(DecodeImage_BufferIsTooShort);
|
||||
}
|
||||
wuffs_base__pixel_buffer pixel_buffer;
|
||||
wuffs_base__status pb_sfs_status =
|
||||
pixel_buffer.set_from_slice(&image_config.pixcfg, oic_result.mem_slice);
|
||||
wuffs_base__status pb_sfs_status = pixel_buffer.set_from_slice(
|
||||
&image_config.pixcfg, alloc_pixbuf_result.mem_slice);
|
||||
if (!pb_sfs_status.is_ok()) {
|
||||
return DecodeImageResult(pb_sfs_status.message());
|
||||
}
|
||||
@@ -37251,8 +37262,8 @@ redirect:
|
||||
}
|
||||
}
|
||||
}
|
||||
return DecodeImageResult(std::move(oic_result.mem_owner),
|
||||
oic_result.mem_slice, pixel_buffer,
|
||||
return DecodeImageResult(std::move(alloc_pixbuf_result.mem_owner),
|
||||
alloc_pixbuf_result.mem_slice, pixel_buffer,
|
||||
std::move(message));
|
||||
}
|
||||
|
||||
@@ -37261,7 +37272,6 @@ redirect:
|
||||
DecodeImageResult //
|
||||
DecodeImage(DecodeImageCallbacks& callbacks,
|
||||
sync_io::Input& input,
|
||||
uint32_t override_pixel_format_repr,
|
||||
wuffs_base__pixel_blend pixel_blend,
|
||||
wuffs_base__color_u32_argb_premul background_color,
|
||||
uint32_t max_incl_dimension) {
|
||||
@@ -37276,9 +37286,9 @@ DecodeImage(DecodeImageCallbacks& callbacks,
|
||||
}
|
||||
|
||||
wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);
|
||||
DecodeImageResult result = DecodeImage0(
|
||||
image_decoder, callbacks, input, *io_buf, override_pixel_format_repr,
|
||||
pixel_blend, background_color, max_incl_dimension);
|
||||
DecodeImageResult result =
|
||||
DecodeImage0(image_decoder, callbacks, input, *io_buf, pixel_blend,
|
||||
background_color, max_incl_dimension);
|
||||
callbacks.Done(result, input, *io_buf, std::move(image_decoder));
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user