Skip to content

Commit

Permalink
Tools: Make ZimTool able to use ZSTD/specify level.
Browse files Browse the repository at this point in the history
There's decompression speed tradeoffs at some levels.
  • Loading branch information
unknownbrackets committed May 16, 2021
1 parent a0f79ed commit 7afd02e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Common/Data/Format/ZIMSave.cpp
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Common/Data/Format/ZIMSave.h
Expand Up @@ -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);
15 changes: 13 additions & 2 deletions ext/native/tools/zimtool.cpp
Expand Up @@ -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");
}

Expand All @@ -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] != '-') {
Expand Down Expand Up @@ -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?
Expand All @@ -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);
Expand Down

0 comments on commit 7afd02e

Please sign in to comment.