-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathQuantPngQuant.c
More file actions
148 lines (133 loc) · 3.44 KB
/
QuantPngQuant.c
File metadata and controls
148 lines (133 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* The Python Imaging Library
* $Id$
*
* quantization using libimagequant, a part of pngquant.
*
* Copyright (c) 2016 Marcin Kurczewski <rr-@sakuya.pl>
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "QuantPngQuant.h"
#ifdef HAVE_LIBIMAGEQUANT
#include "libimagequant.h"
int
quantize_pngquant(
Pixel *pixelData,
unsigned int width,
unsigned int height,
uint32_t quantPixels,
Pixel **palette,
uint32_t *paletteLength,
uint32_t **quantizedPixels,
int withAlpha
) {
int result = 0;
liq_image *image = NULL;
liq_attr *attr = NULL;
liq_result *remap = NULL;
unsigned char *charMatrix = NULL;
unsigned char **charMatrixRows = NULL;
unsigned int i, y;
*palette = NULL;
*paletteLength = 0;
*quantizedPixels = NULL;
/* Check for integer overflow in width * height to prevent
* undersized allocations leading to heap buffer overflow. */
if (height != 0 && (size_t)width > SIZE_MAX / (size_t)height) {
goto err;
}
size_t total_pixels = (size_t)width * (size_t)height;
/* configure pngquant */
attr = liq_attr_create();
if (!attr) {
goto err;
}
if (quantPixels) {
liq_set_max_colors(attr, quantPixels);
}
/* prepare input image */
image = liq_image_create_rgba(attr, pixelData, width, height, 0.45455 /* gamma */);
if (!image) {
goto err;
}
/* quantize the image */
remap = liq_quantize_image(attr, image);
if (!remap) {
goto err;
}
liq_set_output_gamma(remap, 0.45455);
liq_set_dithering_level(remap, 1);
/* write output palette */
const liq_palette *l_palette = liq_get_palette(remap);
*paletteLength = l_palette->count;
*palette = malloc(sizeof(Pixel) * l_palette->count);
if (!*palette) {
goto err;
}
for (i = 0; i < l_palette->count; i++) {
(*palette)[i].c.b = l_palette->entries[i].b;
(*palette)[i].c.g = l_palette->entries[i].g;
(*palette)[i].c.r = l_palette->entries[i].r;
(*palette)[i].c.a = l_palette->entries[i].a;
}
/* write output pixels (pngquant uses char array) */
charMatrix = malloc(total_pixels);
if (!charMatrix) {
goto err;
}
charMatrixRows = malloc(height * sizeof(unsigned char *));
if (!charMatrixRows) {
goto err;
}
for (y = 0; y < height; y++) {
charMatrixRows[y] = &charMatrix[(size_t)y * width];
}
if (LIQ_OK != liq_write_remapped_image_rows(remap, image, charMatrixRows)) {
goto err;
}
/* transcribe output pixels (pillow uses uint32_t array) */
*quantizedPixels = malloc(sizeof(uint32_t) * total_pixels);
if (!*quantizedPixels) {
goto err;
}
for (i = 0; i < total_pixels; i++) {
(*quantizedPixels)[i] = charMatrix[i];
}
result = 1;
err:
if (attr) {
liq_attr_destroy(attr);
}
if (image) {
liq_image_destroy(image);
}
if (remap) {
liq_result_destroy(remap);
}
free(charMatrix);
free(charMatrixRows);
if (!result) {
free(*quantizedPixels);
free(*palette);
}
return result;
}
const char *
ImagingImageQuantVersion(void) {
static char version[20];
int number = liq_version();
snprintf(
version,
sizeof(version),
"%d.%d.%d",
number / 10000,
(number / 100) % 100,
number % 100
);
return version;
}
#endif