mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
test: add webp mimic tests and benches
This commit is contained in:
1 parent
5654506497
commit
6c8a43390b
2 files changed
+288
-8
No files matched your search
@@ -0,0 +1,113 @@
|
|||||||
|
// Copyright 2024 The Wuffs Authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||||
|
|
||||||
|
#include "webp/decode.h"
|
||||||
|
|
||||||
|
static void //
|
||||||
|
mimiclib_convert_to_y_from_bgra_nonpremul(uint8_t* dst,
|
||||||
|
const uint8_t* src,
|
||||||
|
uint32_t n) {
|
||||||
|
while (n--) {
|
||||||
|
uint32_t s0_nonpremul = wuffs_base__peek_u32le__no_bounds_check(src);
|
||||||
|
uint32_t s0_premul =
|
||||||
|
wuffs_base__color_u32_argb_nonpremul__as__color_u32_argb_premul(
|
||||||
|
s0_nonpremul);
|
||||||
|
dst[0] = wuffs_base__color_u32_argb_premul__as__color_u8_gray(s0_premul);
|
||||||
|
dst += 1;
|
||||||
|
src += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
mimic_webp_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
WebPDecoderConfig config;
|
||||||
|
if (!WebPInitDecoderConfig(&config)) {
|
||||||
|
return "mimic_webp_decode: WebPInitDecoderConfig failed";
|
||||||
|
} else if (WebPGetFeatures(wuffs_base__io_buffer__reader_pointer(src),
|
||||||
|
wuffs_base__io_buffer__reader_length(src),
|
||||||
|
&config.input) != VP8_STATUS_OK) {
|
||||||
|
return "mimic_webp_decode: WebPGetFeatures failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t w = config.input.width;
|
||||||
|
const uint32_t h = config.input.height;
|
||||||
|
if ((w > 0x4000) || (h > 0x4000)) {
|
||||||
|
return "mimic_webp_decode: impossible WebP dimensions";
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (pixfmt.repr) {
|
||||||
|
case WUFFS_BASE__PIXEL_FORMAT__Y:
|
||||||
|
if (((1 * w * h) > wuffs_base__io_buffer__writer_length(dst)) ||
|
||||||
|
((4 * w * h) > g_mimiclib_scratch_slice_u8.len)) {
|
||||||
|
return "mimic_webp_decode: image is too large";
|
||||||
|
}
|
||||||
|
config.output.u.RGBA.rgba = g_mimiclib_scratch_slice_u8.ptr;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
||||||
|
if ((4 * w * h) > wuffs_base__io_buffer__writer_length(dst)) {
|
||||||
|
return "mimic_webp_decode: image is too large";
|
||||||
|
}
|
||||||
|
config.output.u.RGBA.rgba = wuffs_base__io_buffer__writer_pointer(dst);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "mimic_webp_decode: unsupported pixfmt";
|
||||||
|
}
|
||||||
|
|
||||||
|
config.output.colorspace = MODE_BGRA;
|
||||||
|
config.output.width = w;
|
||||||
|
config.output.height = h;
|
||||||
|
config.output.is_external_memory = 1;
|
||||||
|
config.output.u.RGBA.stride = 4 * w;
|
||||||
|
config.output.u.RGBA.size = 4 * w * h;
|
||||||
|
|
||||||
|
VP8StatusCode status =
|
||||||
|
WebPDecode(wuffs_base__io_buffer__reader_pointer(src),
|
||||||
|
wuffs_base__io_buffer__reader_length(src), &config);
|
||||||
|
WebPFreeDecBuffer(&config.output);
|
||||||
|
if (status != VP8_STATUS_OK) {
|
||||||
|
return "mimic_webp_decode: WebPDecode failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t n = 0;
|
||||||
|
switch (pixfmt.repr) {
|
||||||
|
case WUFFS_BASE__PIXEL_FORMAT__Y:
|
||||||
|
mimiclib_convert_to_y_from_bgra_nonpremul(
|
||||||
|
wuffs_base__io_buffer__writer_pointer(dst),
|
||||||
|
g_mimiclib_scratch_slice_u8.ptr, 1 * w * h);
|
||||||
|
n = 1 * w * h;
|
||||||
|
break;
|
||||||
|
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
||||||
|
n = 4 * w * h;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
dst->meta.wi += n;
|
||||||
|
if (n_bytes_out) {
|
||||||
|
*n_bytes_out += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
+175
-8
@@ -33,7 +33,7 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
|
|||||||
"wuffs mimic cflags" to run the mimic benchmarks.
|
"wuffs mimic cflags" to run the mimic benchmarks.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// ¿ wuffs mimic cflags: -DWUFFS_MIMIC
|
// ¿ wuffs mimic cflags: -DWUFFS_MIMIC -lwebp
|
||||||
|
|
||||||
// Wuffs ships as a "single file C library" or "header file library" as per
|
// Wuffs ships as a "single file C library" or "header file library" as per
|
||||||
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
|
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
|
||||||
@@ -61,7 +61,7 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
|
|||||||
#include "../../../release/c/wuffs-unsupported-snapshot.c"
|
#include "../../../release/c/wuffs-unsupported-snapshot.c"
|
||||||
#include "../testlib/testlib.c"
|
#include "../testlib/testlib.c"
|
||||||
#ifdef WUFFS_MIMIC
|
#ifdef WUFFS_MIMIC
|
||||||
// No mimic library.
|
#include "../mimiclib/webp.c"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static wuffs_webp__decoder g_webp_decoder;
|
static wuffs_webp__decoder g_webp_decoder;
|
||||||
@@ -105,19 +105,174 @@ test_wuffs_webp_decode_interface() {
|
|||||||
|
|
||||||
#ifdef WUFFS_MIMIC
|
#ifdef WUFFS_MIMIC
|
||||||
|
|
||||||
// No mimic tests.
|
const char* //
|
||||||
|
do_test_mimic_webp_decode(const char* filename) {
|
||||||
|
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||||
|
.data = g_src_slice_u8,
|
||||||
|
});
|
||||||
|
CHECK_STRING(read_file(&src, filename));
|
||||||
|
|
||||||
|
src.meta.ri = 0;
|
||||||
|
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
|
||||||
|
.data = g_have_slice_u8,
|
||||||
|
});
|
||||||
|
CHECK_STRING(wuffs_webp_decode(
|
||||||
|
NULL, &have, WUFFS_INITIALIZE__DEFAULT_OPTIONS,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, &src));
|
||||||
|
|
||||||
|
src.meta.ri = 0;
|
||||||
|
wuffs_base__io_buffer want = ((wuffs_base__io_buffer){
|
||||||
|
.data = g_want_slice_u8,
|
||||||
|
});
|
||||||
|
CHECK_STRING(mimic_webp_decode(
|
||||||
|
NULL, &want, WUFFS_INITIALIZE__DEFAULT_OPTIONS,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, &src));
|
||||||
|
|
||||||
|
return check_io_buffers_equal("", &have, &want);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
test_mimic_webp_lossless_decode_image_19k_8bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_test_mimic_webp_decode("test/data/bricks-gray.lossless.webp");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
test_mimic_webp_lossless_decode_image_40k_24bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_test_mimic_webp_decode("test/data/hat.lossless.webp");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
test_mimic_webp_lossless_decode_image_77k_8bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_test_mimic_webp_decode("test/data/bricks-dither.lossless.webp");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
test_mimic_webp_lossless_decode_image_552k_32bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_test_mimic_webp_decode(
|
||||||
|
"test/data/hibiscus.primitive.lossless.webp");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
test_mimic_webp_lossless_decode_image_4002k_24bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_test_mimic_webp_decode("test/data/harvesters.lossless.webp");
|
||||||
|
}
|
||||||
|
|
||||||
#endif // WUFFS_MIMIC
|
#endif // WUFFS_MIMIC
|
||||||
|
|
||||||
// ---------------- WebP Benches
|
// ---------------- WebP Benches
|
||||||
|
|
||||||
// No WebP benches.
|
const char* //
|
||||||
|
bench_wuffs_webp_lossless_decode_image_19k_8bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&wuffs_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__Y), NULL, 0,
|
||||||
|
"test/data/bricks-gray.lossless.webp", 0, SIZE_MAX, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_wuffs_webp_lossless_decode_image_40k_24bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&wuffs_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, "test/data/hat.lossless.webp", 0, SIZE_MAX, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_wuffs_webp_lossless_decode_image_77k_8bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&wuffs_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, "test/data/bricks-dither.lossless.webp", 0, SIZE_MAX, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_wuffs_webp_lossless_decode_image_552k_32bpp() {
|
||||||
|
uint32_t q = WUFFS_BASE__QUIRK_IGNORE_CHECKSUM;
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&wuffs_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
&q, 1, "test/data/hibiscus.primitive.lossless.webp", 0, SIZE_MAX, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_wuffs_webp_lossless_decode_image_4002k_24bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&wuffs_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, "test/data/harvesters.lossless.webp", 0, SIZE_MAX, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- Mimic Benches
|
// ---------------- Mimic Benches
|
||||||
|
|
||||||
#ifdef WUFFS_MIMIC
|
#ifdef WUFFS_MIMIC
|
||||||
|
|
||||||
// No mimic benches.
|
const char* //
|
||||||
|
bench_mimic_webp_lossless_decode_image_19k_8bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&mimic_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__Y), NULL, 0,
|
||||||
|
"test/data/bricks-gray.lossless.webp", 0, SIZE_MAX, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_mimic_webp_lossless_decode_image_40k_24bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&mimic_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, "test/data/hat.lossless.webp", 0, SIZE_MAX, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_mimic_webp_lossless_decode_image_77k_8bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&mimic_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, "test/data/bricks-dither.lossless.webp", 0, SIZE_MAX, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_mimic_webp_lossless_decode_image_552k_32bpp() {
|
||||||
|
uint32_t q = WUFFS_BASE__QUIRK_IGNORE_CHECKSUM;
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&mimic_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
&q, 1, "test/data/hibiscus.primitive.lossless.webp", 0, SIZE_MAX, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* //
|
||||||
|
bench_mimic_webp_lossless_decode_image_4002k_24bpp() {
|
||||||
|
CHECK_FOCUS(__func__);
|
||||||
|
return do_bench_image_decode(
|
||||||
|
&mimic_webp_decode,
|
||||||
|
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
|
||||||
|
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
|
||||||
|
NULL, 0, "test/data/harvesters.lossless.webp", 0, SIZE_MAX, 1);
|
||||||
|
}
|
||||||
|
|
||||||
#endif // WUFFS_MIMIC
|
#endif // WUFFS_MIMIC
|
||||||
|
|
||||||
@@ -129,7 +284,11 @@ proc g_tests[] = {
|
|||||||
|
|
||||||
#ifdef WUFFS_MIMIC
|
#ifdef WUFFS_MIMIC
|
||||||
|
|
||||||
// No mimic tests.
|
test_mimic_webp_lossless_decode_image_19k_8bpp,
|
||||||
|
test_mimic_webp_lossless_decode_image_40k_24bpp,
|
||||||
|
test_mimic_webp_lossless_decode_image_77k_8bpp,
|
||||||
|
test_mimic_webp_lossless_decode_image_552k_32bpp,
|
||||||
|
test_mimic_webp_lossless_decode_image_4002k_24bpp,
|
||||||
|
|
||||||
#endif // WUFFS_MIMIC
|
#endif // WUFFS_MIMIC
|
||||||
|
|
||||||
@@ -138,11 +297,19 @@ proc g_tests[] = {
|
|||||||
|
|
||||||
proc g_benches[] = {
|
proc g_benches[] = {
|
||||||
|
|
||||||
// No WebP benches.
|
bench_wuffs_webp_lossless_decode_image_19k_8bpp,
|
||||||
|
bench_wuffs_webp_lossless_decode_image_40k_24bpp,
|
||||||
|
bench_wuffs_webp_lossless_decode_image_77k_8bpp,
|
||||||
|
bench_wuffs_webp_lossless_decode_image_552k_32bpp,
|
||||||
|
bench_wuffs_webp_lossless_decode_image_4002k_24bpp,
|
||||||
|
|
||||||
#ifdef WUFFS_MIMIC
|
#ifdef WUFFS_MIMIC
|
||||||
|
|
||||||
// No mimic benches.
|
bench_mimic_webp_lossless_decode_image_19k_8bpp,
|
||||||
|
bench_mimic_webp_lossless_decode_image_40k_24bpp,
|
||||||
|
bench_mimic_webp_lossless_decode_image_77k_8bpp,
|
||||||
|
bench_mimic_webp_lossless_decode_image_552k_32bpp,
|
||||||
|
bench_mimic_webp_lossless_decode_image_4002k_24bpp,
|
||||||
|
|
||||||
#endif // WUFFS_MIMIC
|
#endif // WUFFS_MIMIC
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user