From 7afd02e7e03675b6c2b6e9309eb4859b57c49b67 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 16 May 2021 09:39:39 -0700 Subject: [PATCH] Tools: Make ZimTool able to use ZSTD/specify level. There's decompression speed tradeoffs at some levels. --- Common/Data/Format/ZIMSave.cpp | 10 +++++----- Common/Data/Format/ZIMSave.h | 2 +- ext/native/tools/zimtool.cpp | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Common/Data/Format/ZIMSave.cpp b/Common/Data/Format/ZIMSave.cpp index 3d61ace397..b18fcf682a 100644 --- a/Common/Data/Format/ZIMSave.cpp +++ b/Common/Data/Format/ZIMSave.cpp @@ -24,7 +24,7 @@ static unsigned int log2i(unsigned int val) { } -int ezcompress(unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, long nSrcLen) { +int ezcompress(unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, long nSrcLen, int compressLevel) { z_stream stream; int err; @@ -43,7 +43,7 @@ int ezcompress(unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, stream.zfree = (free_func)0; stream.opaque = (voidpf)0; - err = deflateInit(&stream, Z_DEFAULT_COMPRESSION); + err = deflateInit(&stream, compressLevel); if (err != Z_OK) return err; nExtraChunks = 0; do { @@ -179,7 +179,7 @@ uint8_t *DownsampleBy2(const uint8_t *image, int width, int height, int pitch) { return out; } -void SaveZIM(FILE *f, int width, int height, int pitch, int flags, const uint8_t *image_data) { +void SaveZIM(FILE *f, int width, int height, int pitch, int flags, const uint8_t *image_data, int compressLevel) { fwrite(magic, 1, 4, f); fwrite(&width, 1, 4, f); fwrite(&height, 1, 4, f); @@ -196,7 +196,7 @@ void SaveZIM(FILE *f, int width, int height, int pitch, int flags, const uint8_t if (flags & ZIM_ZLIB_COMPRESSED) { long dest_len = data_size * 2; uint8_t *dest = new uint8_t[dest_len]; - if (Z_OK == ezcompress(dest, &dest_len, data, data_size)) { + if (Z_OK == ezcompress(dest, &dest_len, data, data_size, compressLevel == 0 ? Z_DEFAULT_COMPRESSION : compressLevel)) { fwrite(dest, 1, dest_len, f); } else { ERROR_LOG(IO, "Zlib compression failed.\n"); @@ -205,7 +205,7 @@ void SaveZIM(FILE *f, int width, int height, int pitch, int flags, const uint8_t } else if (flags & ZIM_ZSTD_COMPRESSED) { size_t dest_len = ZSTD_compressBound(data_size); uint8_t *dest = new uint8_t[dest_len]; - dest_len = ZSTD_compress(dest, dest_len, data, data_size, 22); + dest_len = ZSTD_compress(dest, dest_len, data, data_size, compressLevel == 0 ? 22 : compressLevel); if (!ZSTD_isError(dest_len)) { fwrite(dest, 1, dest_len, f); } else { diff --git a/Common/Data/Format/ZIMSave.h b/Common/Data/Format/ZIMSave.h index 90158d3d55..d22fd0490c 100644 --- a/Common/Data/Format/ZIMSave.h +++ b/Common/Data/Format/ZIMSave.h @@ -11,4 +11,4 @@ // * Generate mipmaps if requested // * Convert images to the requested format // Input image is always 8888 RGBA. SaveZIM takes care of downsampling and mipmap generation. -void SaveZIM(FILE *f, int width, int height, int pitch, int format, const uint8_t *image); +void SaveZIM(FILE *f, int width, int height, int pitch, int format, const uint8_t *image, int compressLevel = 0); diff --git a/ext/native/tools/zimtool.cpp b/ext/native/tools/zimtool.cpp index 88f9e9da16..b9a24cb30f 100644 --- a/ext/native/tools/zimtool.cpp +++ b/ext/native/tools/zimtool.cpp @@ -14,7 +14,7 @@ const char *format_strings[4] = { "8888", "4444", "565", "ETC1" }; int formats[3] = { ZIM_RGBA8888, ZIM_RGBA4444, ZIM_RGB565 }; void printusage() { - fprintf(stderr, "Usage: zimtool infile.png outfile.zim [-f=FORMAT] [-m] [-g]\n"); + fprintf(stderr, "Usage: zimtool infile.png outfile.zim [-f=FORMAT] [-m] [-g] [-z[LEVEL]]\n"); fprintf(stderr, "Formats: 8888 4444 565 ETC1\n"); } @@ -40,6 +40,7 @@ int main(int argc, char **argv) { } int flags = 0; + int level = 0; bool format_set = false; for (int i = 3; i < argc; i++) { if (argv[i][0] != '-') { @@ -68,6 +69,16 @@ int main(int argc, char **argv) { } } break; + case 'z': + flags |= ZIM_ZSTD_COMPRESSED; + if (argv[i][2] != '\0') { + int pos = 2; + while (argv[i][pos] >= '0' && argv[i][pos] <= '9') { + level = level * 10 + argv[i][pos] - '0'; + pos++; + } + } + break; } } // TODO: make setting? @@ -87,7 +98,7 @@ int main(int argc, char **argv) { } FILE *f = fopen(FLAGS_outfile, "wb"); - SaveZIM(f, width, height, width * 4, flags, image_data); + SaveZIM(f, width, height, width * 4, flags, image_data, level); fclose(f); int in_file_size = filesize(FLAGS_infile); int out_file_size = filesize(FLAGS_outfile);