diff --git a/src/compress.c b/src/compress.c index f2c579e..1375722 100644 --- a/src/compress.c +++ b/src/compress.c @@ -36,7 +36,7 @@ #include -static int compress_zx7(uint8_t *data, size_t *size) +static uint8_t *compress_zx7(uint8_t *data, size_t *size) { zx7_Optimal *opt; uint8_t *compressed_data; @@ -45,14 +45,14 @@ static int compress_zx7(uint8_t *data, size_t *size) if (size == NULL || data == NULL) { - return -1; + return NULL; } opt = zx7_optimize(data, *size, 0); if (opt == NULL) { LOG_ERROR("Could not optimize zx7.\n"); - return -1; + return NULL; } compressed_data = zx7_compress(opt, data, *size, 0, &new_size, &delta); @@ -60,15 +60,14 @@ static int compress_zx7(uint8_t *data, size_t *size) if (compressed_data == NULL) { LOG_ERROR("Out of memory.\n"); - return -1; + return NULL; } - memcpy(data, compressed_data, new_size); - *size = new_size; + LOG_DEBUG("Compressed size: %u -> %u\n", *size, new_size); - free(compressed_data); + *size = new_size; - return 0; + return compressed_data; } static void compress_zx0_progress(void) @@ -76,7 +75,7 @@ static void compress_zx0_progress(void) LOG_PRINT("."); } -static int compress_zx0(uint8_t *data, size_t *size) +static uint8_t *compress_zx0(uint8_t *data, size_t *size) { zx0_BLOCK *optimal; uint8_t *compressed_data; @@ -85,7 +84,7 @@ static int compress_zx0(uint8_t *data, size_t *size) if (size == NULL || data == NULL) { - return -1; + return NULL; } LOG_PRINT("[info] Compressing ["); @@ -95,7 +94,7 @@ static int compress_zx0(uint8_t *data, size_t *size) { LOG_ERROR("Out of memory]\n"); zx0_free(); - return -1; + return NULL; } compressed_data = zx0_compress(optimal, data, *size, 0, 0, 1, &new_size, &delta); @@ -106,25 +105,22 @@ static int compress_zx0(uint8_t *data, size_t *size) { LOG_ERROR("Out of memory.\n"); zx0_free(); - return -1; + return NULL; } - memcpy(data, compressed_data, new_size); + LOG_DEBUG("Compressed size: %u -> %u\n", *size, new_size); + *size = new_size; - free(compressed_data); zx0_free(); - return 0; + return compressed_data; } -int compress_array(uint8_t *data, size_t *size, compress_mode_t mode) +uint8_t *compress_array(uint8_t *data, size_t *size, compress_mode_t mode) { switch (mode) { - case COMPRESS_NONE: - return 0; - case COMPRESS_ZX7: return compress_zx7(data, size); @@ -132,8 +128,6 @@ int compress_array(uint8_t *data, size_t *size, compress_mode_t mode) return compress_zx0(data, size); default: - break; + return NULL; } - - return -1; } diff --git a/src/compress.h b/src/compress.h index c5314b0..dca9598 100644 --- a/src/compress.h +++ b/src/compress.h @@ -45,7 +45,7 @@ typedef enum COMPRESS_ZX0, } compress_mode_t; -int compress_array(uint8_t *data, size_t *size, compress_mode_t mode); +uint8_t *compress_array(uint8_t *data, size_t *size, compress_mode_t mode); #ifdef __cplusplus } diff --git a/src/image.c b/src/image.c index 7965447..2e446a3 100644 --- a/src/image.c +++ b/src/image.c @@ -447,14 +447,21 @@ int image_remove_omits(struct image *image, const uint8_t *omit_indices, uint32_ int image_compress(struct image *image, compress_mode_t mode) { - size_t size = image->data_size; - - if (compress_array(image->data, &size, mode)) + if (mode != COMPRESS_NONE) { - return -1; - } + size_t size = image->data_size; + void *original_data = image->data; - image->data_size = size; + image->data = compress_array(original_data, &size, mode); + free(original_data); + + if (image->data == NULL) + { + return -1; + } + + image->data_size = size; + } return 0; } diff --git a/src/palette.c b/src/palette.c index 5c279c0..44c8fc9 100644 --- a/src/palette.c +++ b/src/palette.c @@ -43,9 +43,6 @@ #include #include -/* one mebibyte */ -#define MIB_1 1048576 - /* maximum number of colors that can be quantized */ #define MAX_NR_COLORS 536870912 @@ -388,7 +385,8 @@ int palette_generate_with_images(struct palette *palette) } } - colors = NULL; + size_t nr_colors_alloc = 8192; + colors = memory_realloc_array(NULL, nr_colors_alloc, 4); nr_colors = 0; /* quantize the images into a palette */ @@ -433,9 +431,17 @@ int palette_generate_with_images(struct palette *palette) return -1; } - if (nr_colors % MIB_1 == 0) + if (nr_colors == nr_colors_alloc) { - colors = memory_realloc_array(colors, MIB_1, 4); + /* multiple by 1.5 for best performance */ + nr_colors_alloc *= 3; + nr_colors_alloc /= 2; + + LOG_DEBUG("%u colors, allocating %u more\n", + nr_colors, + nr_colors_alloc); + + colors = memory_realloc_array(colors, nr_colors_alloc, 4); if (colors == NULL) { return -1; @@ -455,6 +461,8 @@ int palette_generate_with_images(struct palette *palette) } } + LOG_DEBUG("%u colors in palette before quantization\n", nr_colors); + if (nr_colors > 0) { liq_result *liqresult = NULL; @@ -578,20 +586,23 @@ int palette_generate_with_images(struct palette *palette) { uint32_t j; - for (j = 0; j < nr_max_entries; ++j) + if (nr_max_entries) { - if (!palette->entries[j].valid) + for (j = 0; j < nr_max_entries; ++j) { - break; + if (!palette->entries[j].valid) + { + break; + } } - } - if (j == nr_max_entries) - { - LOG_ERROR("Could not find a valid fixed entry location.\n"); - liq_histogram_destroy(hist); - liq_attr_destroy(attr); - return -1; + if (j == nr_max_entries) + { + LOG_ERROR("Could not find a valid fixed entry location.\n"); + liq_histogram_destroy(hist); + liq_attr_destroy(attr); + return -1; + } } palette->entries[j] = palette->entries[fixed_entry->index];