Tools: Make ZimTool able to use ZSTD/specify level.

There's decompression speed tradeoffs at some levels.
This commit is contained in:
Unknown W. Brackets 2021-05-16 09:39:39 -07:00
parent a0f79ed624
commit 7afd02e7e0
3 changed files with 19 additions and 8 deletions

View file

@ -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 {

View file

@ -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);

View file

@ -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);