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
|
/*
* The Python Imaging Library
* $Id$
*
* quantization using libimagequant, a part of pngquant.
*
* Copyright (c) 2016 Marcin Kurczewski <rr-@sakuya.pl>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "QuantPngQuant.h"
#ifdef HAVE_LIBIMAGEQUANT
#error #include "libimagequant.h"
int
quantize_pngquant(
Pixel *pixelData,
int width,
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;
/* 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(width * height);
if (!charMatrix) { goto err; }
charMatrixRows = malloc(height * sizeof(unsigned char*));
if (!charMatrixRows) { goto err; }
for (y = 0; y < height; y++) {
charMatrixRows[y] = &charMatrix[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) * width * height);
if (!*quantizedPixels) { goto err; }
for (i = 0; i < width * height; 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;
}
#endif
|