mimiclib: allow jpeg decoding via stb_image

This commit is contained in:
Nigel Tao committed 2023-06-12 17:24:49 +10:00
1 parent 1f0f323414
commit dfab0d2b3d
4 files changed
+132 -80

No files matched your search

+24
View File
@@ -12,6 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------
// Uncomment one of these #define lines to test and bench alternative mimic
// libraries (stb_image) instead of libjpeg.
//
// These are collectively referred to as
// WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBJPEG.
//
// #define WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBJPEG 1
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBJPEG
#if defined(WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBJPEG)
#define WUFFS_MIMICLIB_JPEG_DOES_NOT_EXACTLY_MATCH_LIBJPEG 1
#define mimic_jpeg_decode mimic_stb_decode
#include "./stb.c"
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBJPEG
#else
#include "jpeglib.h"
const char* //
@@ -79,3 +100,6 @@ cleanup0:;
jpeg_destroy_decompress(&cinfo);
return ret;
}
#endif
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBJPEG
+2 -80
View File
@@ -228,11 +228,6 @@ cleanup0:;
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#elif defined(WUFFS_MIMICLIB_USE_STB_IMAGE_INSTEAD_OF_LIBPNG)
// We #include a foo.cpp file, not a foo.h file, as stb_image is a "single file
// C library".
#define STB_IMAGE_IMPLEMENTATION
#include "/path/to/your/copy/of/github.com/nothings/stb/stb_image.h"
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_SUPPORT_QUIRK_IGNORE_CHECKSUM macro. The
// stb_image library always ignores checksums.
@@ -242,81 +237,8 @@ cleanup0:;
// We deliberately do not define the
// WUFFS_MIMICLIB_PNG_DOES_NOT_VERIFY_FINAL_IDAT_CHECKSUMS macro.
const char* //
mimic_png_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
uint32_t* quirks_ptr,
size_t quirks_len,
wuffs_base__io_buffer* src) {
wuffs_base__io_buffer dst_fallback =
wuffs_base__slice_u8__writer(g_mimiclib_scratch_slice_u8);
if (!dst) {
dst = &dst_fallback;
}
uint64_t n = 0;
bool swap_bgra_rgba = false;
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
n = 1;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
n = 4;
// stb_image doesn't do BGRA8. RGBA8 is the closest approximation. We'll
// fix it up later.
swap_bgra_rgba = true;
break;
default:
return "mimic_png_decode: unsupported pixfmt";
}
int width = 0;
int height = 0;
int channels_in_file = 0;
unsigned char* output =
stbi_load_from_memory(wuffs_base__io_buffer__reader_pointer(src), //
wuffs_base__io_buffer__reader_length(src), //
&width, &height, &channels_in_file, n);
if (!output) {
return "mimic_png_decode: could not load image";
}
const char* ret = NULL;
if ((width > 0xFFFF) || (height > 0xFFFF)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
n *= ((uint64_t)width) * ((uint64_t)height);
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
// Copy from the mimic library's output buffer to Wuffs' dst buffer.
uint8_t* dst_ptr = wuffs_base__io_buffer__writer_pointer(dst);
memcpy(dst_ptr, output, n);
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
// Fix up BGRA8 vs RGBA8.
if (swap_bgra_rgba) {
for (; n >= 4; n -= 4) {
uint8_t swap = dst_ptr[0];
dst_ptr[0] = dst_ptr[2];
dst_ptr[2] = swap;
dst_ptr += 4;
}
}
cleanup0:;
stbi_image_free(output);
return ret;
}
#define mimic_png_decode mimic_stb_decode
#include "./stb.c"
// -------------------------------- WUFFS_MIMICLIB_USE_XXX_INSTEAD_OF_LIBPNG
#else
+98
View File
@@ -0,0 +1,98 @@
// Copyright 2023 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// We #include a foo.cpp file, not a foo.h file, as stb_image is a "single file
// C library".
//
// Consider also #define'ing STBI_NO_SIMD.
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "/path/to/your/copy/of/github.com/nothings/stb/stb_image.h"
const char* //
mimic_stb_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
uint32_t* quirks_ptr,
size_t quirks_len,
wuffs_base__io_buffer* src) {
wuffs_base__io_buffer dst_fallback =
wuffs_base__slice_u8__writer(g_mimiclib_scratch_slice_u8);
if (!dst) {
dst = &dst_fallback;
}
uint64_t n = 0;
bool swap_bgra_rgba = false;
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
n = 1;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
n = 4;
// stb_image doesn't do BGRA8. RGBA8 is the closest approximation. We'll
// fix it up later.
swap_bgra_rgba = true;
break;
default:
return "mimic_stb_decode: unsupported pixfmt";
}
int width = 0;
int height = 0;
int channels_in_file = 0;
unsigned char* output =
stbi_load_from_memory(wuffs_base__io_buffer__reader_pointer(src), //
wuffs_base__io_buffer__reader_length(src), //
&width, &height, &channels_in_file, n);
if (!output) {
return "mimic_stb_decode: could not load image";
}
const char* ret = NULL;
if ((width > 0xFFFF) || (height > 0xFFFF)) {
ret = "mimic_stb_decode: image is too large";
goto cleanup0;
}
n *= ((uint64_t)width) * ((uint64_t)height);
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_stb_decode: image is too large";
goto cleanup0;
}
// Copy from the mimic library's output buffer to Wuffs' dst buffer.
uint8_t* dst_ptr = wuffs_base__io_buffer__writer_pointer(dst);
memcpy(dst_ptr, output, n);
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
// Fix up BGRA8 vs RGBA8.
if (swap_bgra_rgba) {
for (; n >= 4; n -= 4) {
uint8_t swap = dst_ptr[0];
dst_ptr[0] = dst_ptr[2];
dst_ptr[2] = swap;
dst_ptr += 4;
}
}
cleanup0:;
stbi_image_free(output);
return ret;
}
+8
View File
@@ -768,7 +768,15 @@ do_test_mimic_jpeg_decode(const char* filename) {
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
NULL, 0, &src));
#ifdef WUFFS_MIMICLIB_JPEG_DOES_NOT_EXACTLY_MATCH_LIBJPEG
if (have.meta.wi != want.meta.wi) {
RETURN_FAIL("decoded image size (in bytes): have %zu, want %zu.\n",
have.meta.wi, want.meta.wi);
}
return NULL;
#else
return check_io_buffers_equal("", &have, &want);
#endif // WUFFS_MIMICLIB_JPEG_DOES_NOT_EXACTLY_MATCH_LIBJPEG
}
const char* //