Have wuffs_aux::DecodeImage's Done take unique_ptr
diff --git a/internal/cgen/auxiliary/image.cc b/internal/cgen/auxiliary/image.cc
index 3d48ae7..7fae664 100644
--- a/internal/cgen/auxiliary/image.cc
+++ b/internal/cgen/auxiliary/image.cc
@@ -85,9 +85,11 @@
}
void //
-DecodeImageCallbacks::Done(DecodeImageResult& result,
- sync_io::Input& input,
- IOBuffer& buffer) {}
+DecodeImageCallbacks::Done(
+ DecodeImageResult& result,
+ sync_io::Input& input,
+ IOBuffer& buffer,
+ wuffs_base__image_decoder::unique_ptr image_decoder) {}
const char DecodeImage_BufferIsTooShort[] = //
"wuffs_aux::DecodeImage: buffer is too short";
@@ -141,7 +143,8 @@
}
DecodeImageResult //
-DecodeImage0(DecodeImageCallbacks& callbacks,
+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,
@@ -167,7 +170,6 @@
}
wuffs_base__image_config image_config = wuffs_base__null_image_config();
- wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);
uint64_t start_pos = io_buf.reader_position();
bool redirected = false;
int32_t fourcc = 0;
@@ -358,10 +360,11 @@
io_buf = &fallback_io_buf;
}
+ wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);
DecodeImageResult result =
- DecodeImage0(callbacks, input, *io_buf, override_pixel_format_repr,
- pixel_blend, max_incl_dimension);
- callbacks.Done(result, input, *io_buf);
+ DecodeImage0(image_decoder, callbacks, input, *io_buf,
+ override_pixel_format_repr, pixel_blend, max_incl_dimension);
+ callbacks.Done(result, input, *io_buf, std::move(image_decoder));
return result;
}
diff --git a/internal/cgen/auxiliary/image.hh b/internal/cgen/auxiliary/image.hh
index ad3d03d..33de749 100644
--- a/internal/cgen/auxiliary/image.hh
+++ b/internal/cgen/auxiliary/image.hh
@@ -91,12 +91,20 @@
// 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
+ // was successful), or a no-op unique_ptr otherwise. Like any unique_ptr,
+ // ownership moves to the Done implementation.
+ //
// Do not keep a reference to buffer or buffer.data.ptr after Done returns,
// as DecodeImage may then de-allocate the backing array.
//
- // The default Done implementation is a no-op.
+ // The default Done implementation is a no-op, other than running the
+ // image_decoder unique_ptr destructor.
virtual void //
- Done(DecodeImageResult& result, sync_io::Input& input, IOBuffer& buffer);
+ Done(DecodeImageResult& result,
+ sync_io::Input& input,
+ IOBuffer& buffer,
+ wuffs_base__image_decoder::unique_ptr image_decoder);
};
extern const char DecodeImage_BufferIsTooShort[];
diff --git a/internal/cgen/data/data.go b/internal/cgen/data/data.go
index 1c91fcc..eb174aa 100644
--- a/internal/cgen/data/data.go
+++ b/internal/cgen/data/data.go
@@ -736,28 +736,29 @@
const AuxImageCc = "" +
"// ---------------- Auxiliary - Image\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__AUX__IMAGE)\n\n#include <utility>\n\nnamespace wuffs_aux {\n\nDecodeImageResult::DecodeImageResult(MemOwner&& pixbuf_mem_owner0,\n wuffs_base__slice_u8 pixbuf_mem_slice0,\n wuffs_base__pixel_buffer pixbuf0,\n std::string&& error_message0)\n : pixbuf_mem_owner(std::move(pixbuf_mem_owner0)),\n pixbuf_mem_slice(pixbuf_mem_slice0),\n pixbuf(pixbuf0),\n error_message(std::move(error_message0)) {}\n\nDecodeImageResult::DecodeImageResult(std::string&& error_message0)\n : pixbuf_mem_owner(nullptr, &free),\n pixbuf_mem_slice(wuffs_base__empty_slice_u8()),\n pixbuf({0}),\n error_message(std::move(error_message0)) {}\n\nDecodeImageCallbacks::AllocResult::AllocResult(MemOwner&& mem_owner0,\n wuffs_base__slice_u8 mem_slice0)\n : mem_owner(std::move(" +
"mem_owner0)),\n mem_slice(mem_slice0),\n error_message(\"\") {}\n\nDecodeImageCallbacks::AllocResult::AllocResult(std::string&& error_message0)\n : mem_owner(nullptr, &free),\n mem_slice(wuffs_base__empty_slice_u8()),\n error_message(std::move(error_message0)) {}\n\nDecodeImageCallbacks::AllocResult //\nDecodeImageCallbacks::OnImageConfig(\n const wuffs_base__image_config& image_config) {\n uint32_t w = image_config.pixcfg.width();\n uint32_t h = image_config.pixcfg.height();\n if ((w == 0) || (h == 0)) {\n return AllocResult(\"\");\n }\n uint64_t len = image_config.pixcfg.pixbuf_len();\n if ((len == 0) || (SIZE_MAX < len)) {\n return AllocResult(DecodeImage_UnsupportedPixelConfiguration);\n }\n void* ptr = calloc((size_t)len, 1);\n if (!ptr) {\n return AllocResult(DecodeImage_OutOfMemory);\n }\n return AllocResult(MemOwner(ptr, &free),\n wuffs_base__make_slice_u8((uint8_t*)ptr, (size_t)len));\n}\n\nDecodeImageCallbacks::AllocResult //\nDecodeImageCallbacks::AllocWorkbuf(wuff" +
- "s_base__range_ii_u64 len) {\n if (len.max_incl == 0) {\n return DecodeImageCallbacks::AllocResult(\"\");\n } else if (SIZE_MAX < len.max_incl) {\n return DecodeImageCallbacks::AllocResult(DecodeImage_OutOfMemory);\n }\n void* p = calloc((size_t)len.max_incl, 1);\n if (!p) {\n return DecodeImageCallbacks::AllocResult(DecodeImage_OutOfMemory);\n }\n return DecodeImageCallbacks::AllocResult(\n MemOwner(p, &free),\n wuffs_base__make_slice_u8((uint8_t*)p, (size_t)len.max_incl));\n}\n\nvoid //\nDecodeImageCallbacks::Done(DecodeImageResult& result,\n sync_io::Input& input,\n IOBuffer& buffer) {}\n\nconst char DecodeImage_BufferIsTooShort[] = //\n \"wuffs_aux::DecodeImage: buffer is too short\";\nconst char DecodeImage_MaxInclDimensionExceeded[] = //\n \"wuffs_aux::DecodeImage: max_incl_dimension exceeded\";\nconst char DecodeImage_OutOfMemory[] = //\n \"wuffs_aux::DecodeImage: out of memory\";\nconst char DecodeImage_UnexpectedEndOfFile[] = //\n \"wuffs_aux:" +
- ":DecodeImage: unexpected end of file\";\nconst char DecodeImage_UnsupportedImageFormat[] = //\n \"wuffs_aux::DecodeImage: unsupported image format\";\nconst char DecodeImage_UnsupportedPixelBlend[] = //\n \"wuffs_aux::DecodeImage: unsupported pixel blend\";\nconst char DecodeImage_UnsupportedPixelConfiguration[] = //\n \"wuffs_aux::DecodeImage: unsupported pixel configuration\";\nconst char DecodeImage_UnsupportedPixelFormat[] = //\n \"wuffs_aux::DecodeImage: unsupported pixel format\";\n\n" +
+ "s_base__range_ii_u64 len) {\n if (len.max_incl == 0) {\n return DecodeImageCallbacks::AllocResult(\"\");\n } else if (SIZE_MAX < len.max_incl) {\n return DecodeImageCallbacks::AllocResult(DecodeImage_OutOfMemory);\n }\n void* p = calloc((size_t)len.max_incl, 1);\n if (!p) {\n return DecodeImageCallbacks::AllocResult(DecodeImage_OutOfMemory);\n }\n return DecodeImageCallbacks::AllocResult(\n MemOwner(p, &free),\n wuffs_base__make_slice_u8((uint8_t*)p, (size_t)len.max_incl));\n}\n\nvoid //\nDecodeImageCallbacks::Done(\n DecodeImageResult& result,\n sync_io::Input& input,\n IOBuffer& buffer,\n wuffs_base__image_decoder::unique_ptr image_decoder) {}\n\nconst char DecodeImage_BufferIsTooShort[] = //\n \"wuffs_aux::DecodeImage: buffer is too short\";\nconst char DecodeImage_MaxInclDimensionExceeded[] = //\n \"wuffs_aux::DecodeImage: max_incl_dimension exceeded\";\nconst char DecodeImage_OutOfMemory[] = //\n \"wuffs_aux::DecodeImage: out of memory\";\nconst char DecodeImage_UnexpectedEndOfFile[] = //" +
+ "\n \"wuffs_aux::DecodeImage: unexpected end of file\";\nconst char DecodeImage_UnsupportedImageFormat[] = //\n \"wuffs_aux::DecodeImage: unsupported image format\";\nconst char DecodeImage_UnsupportedPixelBlend[] = //\n \"wuffs_aux::DecodeImage: unsupported pixel blend\";\nconst char DecodeImage_UnsupportedPixelConfiguration[] = //\n \"wuffs_aux::DecodeImage: unsupported pixel configuration\";\nconst char DecodeImage_UnsupportedPixelFormat[] = //\n \"wuffs_aux::DecodeImage: unsupported pixel format\";\n\n" +
"" +
- "// --------\n\nnamespace {\n\nstd::string //\nDecodeImageAdvanceIOBuf(sync_io::Input& input,\n wuffs_base__io_buffer& io_buf,\n bool compactable,\n uint64_t min_excl_pos,\n uint64_t pos) {\n if ((pos <= min_excl_pos) || (pos < io_buf.reader_position())) {\n // Redirects must go forward.\n return DecodeImage_UnsupportedImageFormat;\n }\n while (true) {\n uint64_t relative_pos = pos - io_buf.reader_position();\n if (relative_pos <= io_buf.reader_length()) {\n io_buf.meta.ri += (size_t)relative_pos;\n break;\n } else if (io_buf.meta.closed) {\n return DecodeImage_UnexpectedEndOfFile;\n }\n io_buf.meta.ri = io_buf.meta.wi;\n if (compactable) {\n io_buf.compact();\n }\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return std::move(error_message);\n }\n }\n return \"\";\n}\n\nDecodeImageResult //\nDecodeImage0(DecodeImageCallbacks& callbacks,\n " +
- " sync_io::Input& input,\n wuffs_base__io_buffer& io_buf,\n uint32_t override_pixel_format_repr,\n wuffs_base__pixel_blend pixel_blend,\n uint32_t max_incl_dimension) {\n // Check args.\n switch (override_pixel_format_repr) {\n case 0:\n case WUFFS_BASE__PIXEL_FORMAT__BGR_565:\n case WUFFS_BASE__PIXEL_FORMAT__BGR:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n break;\n default:\n return DecodeImageResult(DecodeImage_UnsupportedPixelFormat);\n }\n switch (pixel_blend) {\n case WUFFS_BASE__PIXEL_BLEND__SRC:\n case WUFFS_BASE__PIXEL_BLEND__SRC_OVER:\n break;\n default:\n return DecodeImageResult(DecodeImage_UnsupportedPixelBlend);\n }\n\n wuffs_base__image_config image_config = wuffs_base__null_image_config();\n wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);\n uint64_t start_pos = io_buf.reader_position();\n bool redirected = false;\n int32_t fourcc = 0;\nredirect:\n " +
- "do {\n // Determine the image format.\n if (!redirected) {\n while (true) {\n fourcc = wuffs_base__magic_number_guess_fourcc(io_buf.reader_slice());\n if (fourcc > 0) {\n break;\n } else if ((fourcc == 0) && (io_buf.reader_length() >= 64)) {\n break;\n } else if (io_buf.meta.closed || (io_buf.writer_length() == 0)) {\n fourcc = 0;\n break;\n }\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n }\n }\n } else {\n wuffs_base__io_buffer empty = wuffs_base__empty_io_buffer();\n wuffs_base__more_information minfo = wuffs_base__empty_more_information();\n wuffs_base__status tmm_status =\n image_decoder->tell_me_more(&empty, &minfo, &io_buf);\n if (minfo.flavor != WUFFS_BASE__MORE_INFORMATION__FLAVOR__IO_REDIRECT) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n uint6" +
- "4_t pos = minfo.io_redirect__range().min_incl;\n std::string error_message = DecodeImageAdvanceIOBuf(\n input, io_buf, !input.BringsItsOwnIOBuffer(), start_pos, pos);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n }\n fourcc = (int32_t)(minfo.io_redirect__fourcc());\n if (fourcc == 0) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n image_decoder.reset();\n }\n\n // Make the image decoder.\n image_decoder = callbacks.OnImageFormat(\n (uint32_t)fourcc,\n fourcc ? wuffs_base__empty_slice_u8() : io_buf.reader_slice());\n if (!image_decoder) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n\n // Decode the image config.\n while (true) {\n wuffs_base__status id_dic_status =\n image_decoder->decode_image_config(&image_config, &io_buf);\n if (id_dic_status.repr == NULL) {\n break;\n } else if (id_dic_status.repr == wuffs_base__note" +
- "__i_o_redirect) {\n if (redirected) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n redirected = true;\n goto redirect;\n } else if (id_dic_status.repr != wuffs_base__suspension__short_read) {\n return DecodeImageResult(id_dic_status.message());\n } else if (io_buf.meta.closed) {\n return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);\n } else {\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n }\n }\n }\n } while (false);\n\n // Apply the override pixel format.\n uint32_t w = image_config.pixcfg.width();\n uint32_t h = image_config.pixcfg.height();\n if ((w > max_incl_dimension) || (h > max_incl_dimension)) {\n return DecodeImageResult(DecodeImage_MaxInclDimensionExceeded);\n }\n if (override_pixel_format_repr != 0) {\n image_config.pixcfg.set(override_pixel_format_repr,\n WU" +
- "FFS_BASE__PIXEL_SUBSAMPLING__NONE, w, h);\n }\n\n // Allocate the pixel buffer.\n size_t pixbuf_len_min_incl = 0;\n if ((w > 0) && (h > 0)) {\n pixbuf_len_min_incl = image_config.pixcfg.pixbuf_len();\n if (pixbuf_len_min_incl == 0) {\n return DecodeImageResult(DecodeImage_UnsupportedPixelFormat);\n }\n }\n DecodeImageCallbacks::AllocResult oic_result =\n callbacks.OnImageConfig(image_config);\n if (!oic_result.error_message.empty()) {\n return DecodeImageResult(std::move(oic_result.error_message));\n } else if (oic_result.mem_slice.len < pixbuf_len_min_incl) {\n return DecodeImageResult(DecodeImage_BufferIsTooShort);\n }\n wuffs_base__pixel_buffer pixel_buffer;\n wuffs_base__status pb_sfs_status =\n pixel_buffer.set_from_slice(&image_config.pixcfg, oic_result.mem_slice);\n if (!pb_sfs_status.is_ok()) {\n return DecodeImageResult(pb_sfs_status.message());\n }\n\n // Allocate the work buffer.\n wuffs_base__range_ii_u64 workbuf_len = image_decoder->workbuf_len();\n DecodeImageCallbacks::All" +
- "ocResult alloc_workbuf_result =\n callbacks.AllocWorkbuf(workbuf_len);\n if (!alloc_workbuf_result.error_message.empty()) {\n return DecodeImageResult(std::move(alloc_workbuf_result.error_message));\n } else if (alloc_workbuf_result.mem_slice.len < workbuf_len.min_incl) {\n return DecodeImageResult(DecodeImage_BufferIsTooShort);\n }\n\n // Decode the frame config.\n wuffs_base__frame_config frame_config = wuffs_base__null_frame_config();\n while (true) {\n wuffs_base__status id_dfc_status =\n image_decoder->decode_frame_config(&frame_config, &io_buf);\n if (id_dfc_status.repr == NULL) {\n break;\n } else if (id_dfc_status.repr != wuffs_base__suspension__short_read) {\n return DecodeImageResult(id_dfc_status.message());\n } else if (io_buf.meta.closed) {\n return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);\n } else {\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n" +
- " }\n }\n }\n\n // Decode the frame (the pixels).\n //\n // From here on, always returns the pixel_buffer. If we get this far, we can\n // still display a partial image, even if we encounter an error.\n std::string message(\"\");\n if ((pixel_blend == WUFFS_BASE__PIXEL_BLEND__SRC_OVER) &&\n frame_config.overwrite_instead_of_blend()) {\n pixel_blend = WUFFS_BASE__PIXEL_BLEND__SRC;\n }\n while (true) {\n wuffs_base__status id_df_status =\n image_decoder->decode_frame(&pixel_buffer, &io_buf, pixel_blend,\n alloc_workbuf_result.mem_slice, NULL);\n if (id_df_status.repr == NULL) {\n break;\n } else if (id_df_status.repr != wuffs_base__suspension__short_read) {\n message = id_df_status.message();\n break;\n } else if (io_buf.meta.closed) {\n message = DecodeImage_UnexpectedEndOfFile;\n break;\n } else {\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n message = std::move(error_message);" +
- "\n break;\n }\n }\n }\n return DecodeImageResult(std::move(oic_result.mem_owner),\n oic_result.mem_slice, pixel_buffer,\n std::move(message));\n}\n\n} // namespace\n\nDecodeImageResult //\nDecodeImage(DecodeImageCallbacks& callbacks,\n sync_io::Input& input,\n uint32_t override_pixel_format_repr,\n wuffs_base__pixel_blend pixel_blend,\n uint32_t max_incl_dimension) {\n wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();\n wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();\n std::unique_ptr<uint8_t[]> fallback_io_array(nullptr);\n if (!io_buf) {\n fallback_io_array = std::unique_ptr<uint8_t[]>(new uint8_t[32768]);\n fallback_io_buf =\n wuffs_base__ptr_u8__writer(fallback_io_array.get(), 32768);\n io_buf = &fallback_io_buf;\n }\n\n DecodeImageResult result =\n DecodeImage0(callbacks, input, *io_buf, override_pixel_format_repr,\n pixel_blend, max_incl_dim" +
- "ension);\n callbacks.Done(result, input, *io_buf);\n return result;\n}\n\n} // namespace wuffs_aux\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__AUX__IMAGE)\n" +
+ "// --------\n\nnamespace {\n\nstd::string //\nDecodeImageAdvanceIOBuf(sync_io::Input& input,\n wuffs_base__io_buffer& io_buf,\n bool compactable,\n uint64_t min_excl_pos,\n uint64_t pos) {\n if ((pos <= min_excl_pos) || (pos < io_buf.reader_position())) {\n // Redirects must go forward.\n return DecodeImage_UnsupportedImageFormat;\n }\n while (true) {\n uint64_t relative_pos = pos - io_buf.reader_position();\n if (relative_pos <= io_buf.reader_length()) {\n io_buf.meta.ri += (size_t)relative_pos;\n break;\n } else if (io_buf.meta.closed) {\n return DecodeImage_UnexpectedEndOfFile;\n }\n io_buf.meta.ri = io_buf.meta.wi;\n if (compactable) {\n io_buf.compact();\n }\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return std::move(error_message);\n }\n }\n return \"\";\n}\n\nDecodeImageResult //\nDecodeImage0(wuffs_base__image_decoder::unique_ptr& image_" +
+ "decoder,\n DecodeImageCallbacks& callbacks,\n sync_io::Input& input,\n wuffs_base__io_buffer& io_buf,\n uint32_t override_pixel_format_repr,\n wuffs_base__pixel_blend pixel_blend,\n uint32_t max_incl_dimension) {\n // Check args.\n switch (override_pixel_format_repr) {\n case 0:\n case WUFFS_BASE__PIXEL_FORMAT__BGR_565:\n case WUFFS_BASE__PIXEL_FORMAT__BGR:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n break;\n default:\n return DecodeImageResult(DecodeImage_UnsupportedPixelFormat);\n }\n switch (pixel_blend) {\n case WUFFS_BASE__PIXEL_BLEND__SRC:\n case WUFFS_BASE__PIXEL_BLEND__SRC_OVER:\n break;\n default:\n return DecodeImageResult(DecodeImage_UnsupportedPixelBlend);\n }\n\n wuffs_base__image_config image_config = wuffs_base__null_image_config();\n uint64_t start_pos = io_buf.reader_position();\n bool redirected = false;\n int32_t fourcc = 0;\nredirect:\n do {" +
+ "\n // Determine the image format.\n if (!redirected) {\n while (true) {\n fourcc = wuffs_base__magic_number_guess_fourcc(io_buf.reader_slice());\n if (fourcc > 0) {\n break;\n } else if ((fourcc == 0) && (io_buf.reader_length() >= 64)) {\n break;\n } else if (io_buf.meta.closed || (io_buf.writer_length() == 0)) {\n fourcc = 0;\n break;\n }\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n }\n }\n } else {\n wuffs_base__io_buffer empty = wuffs_base__empty_io_buffer();\n wuffs_base__more_information minfo = wuffs_base__empty_more_information();\n wuffs_base__status tmm_status =\n image_decoder->tell_me_more(&empty, &minfo, &io_buf);\n if (minfo.flavor != WUFFS_BASE__MORE_INFORMATION__FLAVOR__IO_REDIRECT) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n uint64_t " +
+ "pos = minfo.io_redirect__range().min_incl;\n std::string error_message = DecodeImageAdvanceIOBuf(\n input, io_buf, !input.BringsItsOwnIOBuffer(), start_pos, pos);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n }\n fourcc = (int32_t)(minfo.io_redirect__fourcc());\n if (fourcc == 0) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n image_decoder.reset();\n }\n\n // Make the image decoder.\n image_decoder = callbacks.OnImageFormat(\n (uint32_t)fourcc,\n fourcc ? wuffs_base__empty_slice_u8() : io_buf.reader_slice());\n if (!image_decoder) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n\n // Decode the image config.\n while (true) {\n wuffs_base__status id_dic_status =\n image_decoder->decode_image_config(&image_config, &io_buf);\n if (id_dic_status.repr == NULL) {\n break;\n } else if (id_dic_status.repr == wuffs_base__note__i_" +
+ "o_redirect) {\n if (redirected) {\n return DecodeImageResult(DecodeImage_UnsupportedImageFormat);\n }\n redirected = true;\n goto redirect;\n } else if (id_dic_status.repr != wuffs_base__suspension__short_read) {\n return DecodeImageResult(id_dic_status.message());\n } else if (io_buf.meta.closed) {\n return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);\n } else {\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n }\n }\n }\n } while (false);\n\n // Apply the override pixel format.\n uint32_t w = image_config.pixcfg.width();\n uint32_t h = image_config.pixcfg.height();\n if ((w > max_incl_dimension) || (h > max_incl_dimension)) {\n return DecodeImageResult(DecodeImage_MaxInclDimensionExceeded);\n }\n if (override_pixel_format_repr != 0) {\n image_config.pixcfg.set(override_pixel_format_repr,\n WUFFS_" +
+ "BASE__PIXEL_SUBSAMPLING__NONE, w, h);\n }\n\n // Allocate the pixel buffer.\n size_t pixbuf_len_min_incl = 0;\n if ((w > 0) && (h > 0)) {\n pixbuf_len_min_incl = image_config.pixcfg.pixbuf_len();\n if (pixbuf_len_min_incl == 0) {\n return DecodeImageResult(DecodeImage_UnsupportedPixelFormat);\n }\n }\n DecodeImageCallbacks::AllocResult oic_result =\n callbacks.OnImageConfig(image_config);\n if (!oic_result.error_message.empty()) {\n return DecodeImageResult(std::move(oic_result.error_message));\n } else if (oic_result.mem_slice.len < pixbuf_len_min_incl) {\n return DecodeImageResult(DecodeImage_BufferIsTooShort);\n }\n wuffs_base__pixel_buffer pixel_buffer;\n wuffs_base__status pb_sfs_status =\n pixel_buffer.set_from_slice(&image_config.pixcfg, oic_result.mem_slice);\n if (!pb_sfs_status.is_ok()) {\n return DecodeImageResult(pb_sfs_status.message());\n }\n\n // Allocate the work buffer.\n wuffs_base__range_ii_u64 workbuf_len = image_decoder->workbuf_len();\n DecodeImageCallbacks::AllocRe" +
+ "sult alloc_workbuf_result =\n callbacks.AllocWorkbuf(workbuf_len);\n if (!alloc_workbuf_result.error_message.empty()) {\n return DecodeImageResult(std::move(alloc_workbuf_result.error_message));\n } else if (alloc_workbuf_result.mem_slice.len < workbuf_len.min_incl) {\n return DecodeImageResult(DecodeImage_BufferIsTooShort);\n }\n\n // Decode the frame config.\n wuffs_base__frame_config frame_config = wuffs_base__null_frame_config();\n while (true) {\n wuffs_base__status id_dfc_status =\n image_decoder->decode_frame_config(&frame_config, &io_buf);\n if (id_dfc_status.repr == NULL) {\n break;\n } else if (id_dfc_status.repr != wuffs_base__suspension__short_read) {\n return DecodeImageResult(id_dfc_status.message());\n } else if (io_buf.meta.closed) {\n return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);\n } else {\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n return DecodeImageResult(std::move(error_message));\n " +
+ " }\n }\n }\n\n // Decode the frame (the pixels).\n //\n // From here on, always returns the pixel_buffer. If we get this far, we can\n // still display a partial image, even if we encounter an error.\n std::string message(\"\");\n if ((pixel_blend == WUFFS_BASE__PIXEL_BLEND__SRC_OVER) &&\n frame_config.overwrite_instead_of_blend()) {\n pixel_blend = WUFFS_BASE__PIXEL_BLEND__SRC;\n }\n while (true) {\n wuffs_base__status id_df_status =\n image_decoder->decode_frame(&pixel_buffer, &io_buf, pixel_blend,\n alloc_workbuf_result.mem_slice, NULL);\n if (id_df_status.repr == NULL) {\n break;\n } else if (id_df_status.repr != wuffs_base__suspension__short_read) {\n message = id_df_status.message();\n break;\n } else if (io_buf.meta.closed) {\n message = DecodeImage_UnexpectedEndOfFile;\n break;\n } else {\n std::string error_message = input.CopyIn(&io_buf);\n if (!error_message.empty()) {\n message = std::move(error_message);\n " +
+ " break;\n }\n }\n }\n return DecodeImageResult(std::move(oic_result.mem_owner),\n oic_result.mem_slice, pixel_buffer,\n std::move(message));\n}\n\n} // namespace\n\nDecodeImageResult //\nDecodeImage(DecodeImageCallbacks& callbacks,\n sync_io::Input& input,\n uint32_t override_pixel_format_repr,\n wuffs_base__pixel_blend pixel_blend,\n uint32_t max_incl_dimension) {\n wuffs_base__io_buffer* io_buf = input.BringsItsOwnIOBuffer();\n wuffs_base__io_buffer fallback_io_buf = wuffs_base__empty_io_buffer();\n std::unique_ptr<uint8_t[]> fallback_io_array(nullptr);\n if (!io_buf) {\n fallback_io_array = std::unique_ptr<uint8_t[]>(new uint8_t[32768]);\n fallback_io_buf =\n wuffs_base__ptr_u8__writer(fallback_io_array.get(), 32768);\n io_buf = &fallback_io_buf;\n }\n\n wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);\n DecodeImageResult result =\n DecodeImage0(image_decoder, callbacks, input," +
+ " *io_buf,\n override_pixel_format_repr, pixel_blend, max_incl_dimension);\n callbacks.Done(result, input, *io_buf, std::move(image_decoder));\n return result;\n}\n\n} // namespace wuffs_aux\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__AUX__IMAGE)\n" +
""
const AuxImageHh = "" +
"// ---------------- Auxiliary - Image\n\nnamespace wuffs_aux {\n\nstruct DecodeImageResult {\n DecodeImageResult(MemOwner&& pixbuf_mem_owner0,\n wuffs_base__slice_u8 pixbuf_mem_slice0,\n wuffs_base__pixel_buffer pixbuf0,\n std::string&& error_message0);\n DecodeImageResult(std::string&& error_message0);\n\n MemOwner pixbuf_mem_owner;\n wuffs_base__slice_u8 pixbuf_mem_slice;\n wuffs_base__pixel_buffer pixbuf;\n std::string error_message;\n};\n\n// DecodeImageCallbacks are the callbacks given to DecodeImage. They are always\n// called in this order:\n// 1. OnImageFormat\n// 2. OnImageConfig\n// 3. AllocWorkbuf\n// 4. Done\n//\n// It may return early - the third callback might not be invoked if the second\n// one fails (returns a non-empty error message) - but the final callback\n// (Done) is always invoked.\nclass DecodeImageCallbacks {\n public:\n // AllocResult holds a memory allocation (the result of malloc or new, a\n // statically allocated pointer, etc), or an error " +
"message. The memory is\n // de-allocated when mem_owner goes out of scope and is destroyed.\n struct AllocResult {\n AllocResult(MemOwner&& mem_owner0, wuffs_base__slice_u8 mem_slice0);\n AllocResult(std::string&& error_message0);\n\n MemOwner mem_owner;\n wuffs_base__slice_u8 mem_slice;\n std::string error_message;\n };\n\n // OnImageFormat returns the image decoder for the input data's file format.\n // Returning a nullptr means failure (DecodeImage_UnsupportedImageFormat).\n //\n // Common formats will have a non-zero FourCC value, such as\n // WUFFS_BASE__FOURCC__JPEG. A zero FourCC code means that the caller is\n // responsible for examining the opening bytes (a prefix) of the input data.\n // OnImageFormat implementations should not modify those bytes.\n //\n // OnImageFormat might be called more than once, since some image file\n // formats can wrap others. For example, a nominal BMP file can actually\n // contain a JPEG or a PNG.\n //\n // There is no default implementation, as modular Wuffs b" +
"uilds (those that\n // define WUFFS_CONFIG__MODULES) may enable or disable particular codecs.\n virtual wuffs_base__image_decoder::unique_ptr //\n OnImageFormat(uint32_t fourcc, wuffs_base__slice_u8 prefix) = 0;\n\n // OnImageConfig allocates the pixel buffer.\n //\n // The default OnImageConfig implementation allocates zeroed memory.\n virtual AllocResult //\n OnImageConfig(const wuffs_base__image_config& image_config);\n\n // AllocWorkbuf allocates the work buffer. The allocated buffer's length\n // should be at least len.min_incl, but larger allocations (up to\n // len.max_incl) may have better performance (by using more memory).\n //\n // The default AllocWorkbuf implementation allocates len.max_incl bytes of\n // zeroed memory.\n virtual AllocResult //\n AllocWorkbuf(wuffs_base__range_ii_u64 len);\n\n // Done is always the last Callback method called by DecodeImage, whether or\n // not parsing the input encountered an error. Even when successful, trailing\n // data may remain in input and buffer.\n //\n " +
- "// Do not keep a reference to buffer or buffer.data.ptr after Done returns,\n // as DecodeImage may then de-allocate the backing array.\n //\n // The default Done implementation is a no-op.\n virtual void //\n Done(DecodeImageResult& result, sync_io::Input& input, IOBuffer& buffer);\n};\n\nextern const char DecodeImage_BufferIsTooShort[];\nextern const char DecodeImage_MaxInclDimensionExceeded[];\nextern const char DecodeImage_OutOfMemory[];\nextern const char DecodeImage_UnexpectedEndOfFile[];\nextern const char DecodeImage_UnsupportedImageFormat[];\nextern const char DecodeImage_UnsupportedPixelBlend[];\nextern const char DecodeImage_UnsupportedPixelConfiguration[];\nextern const char DecodeImage_UnsupportedPixelFormat[];\n\n// DecodeImage decodes the image data in input. A variety of image file formats\n// can be decoded, depending on what callbacks.OnImageFormat returns. For\n// animated formats, only the first frame is returned.\n//\n// - On total success, the returned error_message is empty.\n// - On partial success " +
- "(e.g. the input file was truncated but we are still\n// able to decode some pixels), error_message is non-empty but the returned\n// pixbuf.pixcfg.is_valid() is true.\n// - On failure, the error_message is non_empty and pixbuf.pixcfg.is_valid()\n// is false.\n//\n// The callbacks allocate the pixel buffer memory and work buffer memory. On\n// success, pixel buffer memory ownership is passed to the DecodeImage caller\n// as the returned pixbuf_mem_owner. Regardless of success or failure, the work\n// buffer memory is deleted.\n//\n// If override_pixel_format_repr is zero then the pixel buffer will have the\n// image file's natural pixel format. For example, GIF images' natural pixel\n// format is an indexed one.\n//\n// If override_pixel_format_repr is non-zero (and one of the constants listed\n// below) then the image will be decoded to that particular pixel format:\n// - WUFFS_BASE__PIXEL_FORMAT__BGR_565\n// - WUFFS_BASE__PIXEL_FORMAT__BGR\n// - WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL\n// - WUFFS_BASE__PIXEL_FORM" +
- "AT__BGRA_PREMUL\n//\n// The pixel_blend (one of the constants listed below) determines how to\n// composite the decoded image over the pixel buffer's original pixels (as\n// returned by callbacks.OnImageConfig):\n// - WUFFS_BASE__PIXEL_BLEND__SRC\n// - WUFFS_BASE__PIXEL_BLEND__SRC_OVER\n//\n// Decoding fails (with DecodeImage_MaxInclDimensionExceeded) if the image's\n// width or height is greater than max_incl_dimension.\nDecodeImageResult //\nDecodeImage(DecodeImageCallbacks& callbacks,\n sync_io::Input& input,\n uint32_t override_pixel_format_repr,\n wuffs_base__pixel_blend pixel_blend = WUFFS_BASE__PIXEL_BLEND__SRC,\n uint32_t max_incl_dimension = 1048575); // 0x000F_FFFF\n\n} // namespace wuffs_aux\n" +
+ "// The image_decoder is the one returned by OnImageFormat (if OnImageFormat\n // was successful), or a no-op unique_ptr otherwise. Like any unique_ptr,\n // ownership moves to the Done implementation.\n //\n // Do not keep a reference to buffer or buffer.data.ptr after Done returns,\n // as DecodeImage may then de-allocate the backing array.\n //\n // The default Done implementation is a no-op, other than running the\n // image_decoder unique_ptr destructor.\n virtual void //\n Done(DecodeImageResult& result,\n sync_io::Input& input,\n IOBuffer& buffer,\n wuffs_base__image_decoder::unique_ptr image_decoder);\n};\n\nextern const char DecodeImage_BufferIsTooShort[];\nextern const char DecodeImage_MaxInclDimensionExceeded[];\nextern const char DecodeImage_OutOfMemory[];\nextern const char DecodeImage_UnexpectedEndOfFile[];\nextern const char DecodeImage_UnsupportedImageFormat[];\nextern const char DecodeImage_UnsupportedPixelBlend[];\nextern const char DecodeImage_UnsupportedPixelConfiguration[];\nextern c" +
+ "onst char DecodeImage_UnsupportedPixelFormat[];\n\n// DecodeImage decodes the image data in input. A variety of image file formats\n// can be decoded, depending on what callbacks.OnImageFormat returns. For\n// animated formats, only the first frame is returned.\n//\n// - On total success, the returned error_message is empty.\n// - On partial success (e.g. the input file was truncated but we are still\n// able to decode some pixels), error_message is non-empty but the returned\n// pixbuf.pixcfg.is_valid() is true.\n// - On failure, the error_message is non_empty and pixbuf.pixcfg.is_valid()\n// is false.\n//\n// The callbacks allocate the pixel buffer memory and work buffer memory. On\n// success, pixel buffer memory ownership is passed to the DecodeImage caller\n// as the returned pixbuf_mem_owner. Regardless of success or failure, the work\n// buffer memory is deleted.\n//\n// If override_pixel_format_repr is zero then the pixel buffer will have the\n// image file's natural pixel format. For example, GIF images' na" +
+ "tural pixel\n// format is an indexed one.\n//\n// If override_pixel_format_repr is non-zero (and one of the constants listed\n// below) then the image will be decoded to that particular pixel format:\n// - WUFFS_BASE__PIXEL_FORMAT__BGR_565\n// - WUFFS_BASE__PIXEL_FORMAT__BGR\n// - WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL\n// - WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL\n//\n// The pixel_blend (one of the constants listed below) determines how to\n// composite the decoded image over the pixel buffer's original pixels (as\n// returned by callbacks.OnImageConfig):\n// - WUFFS_BASE__PIXEL_BLEND__SRC\n// - WUFFS_BASE__PIXEL_BLEND__SRC_OVER\n//\n// Decoding fails (with DecodeImage_MaxInclDimensionExceeded) if the image's\n// width or height is greater than max_incl_dimension.\nDecodeImageResult //\nDecodeImage(DecodeImageCallbacks& callbacks,\n sync_io::Input& input,\n uint32_t override_pixel_format_repr,\n wuffs_base__pixel_blend pixel_blend = WUFFS_BASE__PIXEL_BLEND__SRC,\n uint32_t max_inc" +
+ "l_dimension = 1048575); // 0x000F_FFFF\n\n} // namespace wuffs_aux\n" +
""
const AuxJsonCc = "" +
diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c
index d7e2bda..36e6d164 100644
--- a/release/c/wuffs-unsupported-snapshot.c
+++ b/release/c/wuffs-unsupported-snapshot.c
@@ -9444,12 +9444,20 @@
// 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
+ // was successful), or a no-op unique_ptr otherwise. Like any unique_ptr,
+ // ownership moves to the Done implementation.
+ //
// Do not keep a reference to buffer or buffer.data.ptr after Done returns,
// as DecodeImage may then de-allocate the backing array.
//
- // The default Done implementation is a no-op.
+ // The default Done implementation is a no-op, other than running the
+ // image_decoder unique_ptr destructor.
virtual void //
- Done(DecodeImageResult& result, sync_io::Input& input, IOBuffer& buffer);
+ Done(DecodeImageResult& result,
+ sync_io::Input& input,
+ IOBuffer& buffer,
+ wuffs_base__image_decoder::unique_ptr image_decoder);
};
extern const char DecodeImage_BufferIsTooShort[];
@@ -36734,9 +36742,11 @@
}
void //
-DecodeImageCallbacks::Done(DecodeImageResult& result,
- sync_io::Input& input,
- IOBuffer& buffer) {}
+DecodeImageCallbacks::Done(
+ DecodeImageResult& result,
+ sync_io::Input& input,
+ IOBuffer& buffer,
+ wuffs_base__image_decoder::unique_ptr image_decoder) {}
const char DecodeImage_BufferIsTooShort[] = //
"wuffs_aux::DecodeImage: buffer is too short";
@@ -36790,7 +36800,8 @@
}
DecodeImageResult //
-DecodeImage0(DecodeImageCallbacks& callbacks,
+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,
@@ -36816,7 +36827,6 @@
}
wuffs_base__image_config image_config = wuffs_base__null_image_config();
- wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);
uint64_t start_pos = io_buf.reader_position();
bool redirected = false;
int32_t fourcc = 0;
@@ -37007,10 +37017,11 @@
io_buf = &fallback_io_buf;
}
+ wuffs_base__image_decoder::unique_ptr image_decoder(nullptr, &free);
DecodeImageResult result =
- DecodeImage0(callbacks, input, *io_buf, override_pixel_format_repr,
- pixel_blend, max_incl_dimension);
- callbacks.Done(result, input, *io_buf);
+ DecodeImage0(image_decoder, callbacks, input, *io_buf,
+ override_pixel_format_repr, pixel_blend, max_incl_dimension);
+ callbacks.Done(result, input, *io_buf, std::move(image_decoder));
return result;
}