mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Wuffs' std/png and std/zlib implementations have not been optimized yet, but even at this early stage, they are roughly on par with their "mimics": equivalent production-quality C libraries (libpng and zlib). On a mid-range x86_64 laptop: wuffs_png_decode_19k_8bpp/clang9 95.8MB/s ± 0% wuffs_png_decode_40k_24bpp/clang9 100MB/s ± 1% wuffs_png_decode_77k_8bpp/clang9 359MB/s ± 0% wuffs_png_decode_552k_32bpp/clang9 119MB/s ± 0% wuffs_png_decode_4002k_24bpp/clang9 101MB/s ± 0% wuffs_png_decode_19k_8bpp/gcc10 97.2MB/s ± 0% wuffs_png_decode_40k_24bpp/gcc10 111MB/s ± 0% wuffs_png_decode_77k_8bpp/gcc10 316MB/s ± 0% wuffs_png_decode_552k_32bpp/gcc10 132MB/s ± 0% wuffs_png_decode_4002k_24bpp/gcc10 116MB/s ± 0% mimic_png_decode_19k_8bpp 58.1MB/s ± 0% mimic_png_decode_40k_24bpp 73.3MB/s ± 0% mimic_png_decode_77k_8bpp 177MB/s ± 0% mimic_png_decode_552k_32bpp 147MB/s ± 0% mimic_png_decode_4002k_24bpp 104MB/s ± 0% wuffs_zlib_decode_10k/clang9 126MB/s ± 0% wuffs_zlib_decode_100k/clang9 136MB/s ± 0% wuffs_zlib_decode_10k/gcc10 131MB/s ± 0% wuffs_zlib_decode_100k/gcc10 159MB/s ± 0% mimic_zlib_decode_10k 133MB/s ± 0% mimic_zlib_decode_100k 160MB/s ± 0%
77 lines
2.2 KiB
C
77 lines
2.2 KiB
C
// Copyright 2020 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.
|
|
|
|
#include "png.h"
|
|
|
|
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,
|
|
wuffs_base__io_buffer* src) {
|
|
static uint8_t dst_fallback_array_u8[64 * 1024 * 1024];
|
|
wuffs_base__io_buffer dst_fallback = ((wuffs_base__io_buffer){
|
|
.data = ((wuffs_base__slice_u8){
|
|
.ptr = dst_fallback_array_u8,
|
|
.len = 64 * 1024 * 1024,
|
|
}),
|
|
});
|
|
if (!dst) {
|
|
dst = &dst_fallback;
|
|
}
|
|
|
|
const char* ret = NULL;
|
|
|
|
png_image pi;
|
|
memset(&pi, 0, (sizeof pi));
|
|
pi.version = PNG_IMAGE_VERSION;
|
|
|
|
if (!png_image_begin_read_from_memory(&pi, src->data.ptr + src->meta.ri,
|
|
src->meta.wi - src->meta.ri)) {
|
|
ret = "mimic_png_decode: png_image_begin_read_from_memory failed";
|
|
goto cleanup0;
|
|
}
|
|
|
|
switch (pixfmt.repr) {
|
|
case WUFFS_BASE__PIXEL_FORMAT__Y:
|
|
pi.format = PNG_FORMAT_GRAY;
|
|
break;
|
|
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
|
pi.format = PNG_FORMAT_BGRA;
|
|
break;
|
|
default:
|
|
ret = "mimic_png_decode: unsupported pixfmt";
|
|
goto cleanup0;
|
|
}
|
|
|
|
size_t n = PNG_IMAGE_SIZE(pi);
|
|
if (n > wuffs_base__io_buffer__writer_length(dst)) {
|
|
ret = "mimic_png_decode: image is too large";
|
|
goto cleanup0;
|
|
}
|
|
if (!png_image_finish_read(
|
|
&pi, NULL, wuffs_base__io_buffer__writer_pointer(dst), 0, NULL)) {
|
|
ret = "mimic_png_decode: png_image_finish_read failed";
|
|
goto cleanup0;
|
|
}
|
|
dst->meta.wi += n;
|
|
if (n_bytes_out) {
|
|
*n_bytes_out += n;
|
|
}
|
|
|
|
cleanup0:;
|
|
png_image_free(&pi);
|
|
return ret;
|
|
}
|