forked from kornelski/pngquant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
116 lines (88 loc) · 3.04 KB
/
test.c
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
#undef NDEBUG
#include <assert.h>
#include "../lib/libimagequant.h"
#include <stdio.h>
#include <string.h>
static char magic[] = "magic";
static void test_log_callback_function(const liq_attr *at, const char *message, void* user_info) {
assert(user_info == magic);
assert(message);
assert(strlen(message));
}
static int test_abort_callback(float progress_percent, void* user_info) {
assert(user_info == magic);
return 0;
}
static int progress_called = 0;
static int test_continue_callback(float progress_percent, void* user_info) {
assert(user_info == magic);
progress_called++;
return 1;
}
static void test_abort() {
liq_attr *attr = liq_attr_create();
unsigned char dummy[4] = {0};
liq_image *img = liq_image_create_rgba(attr, dummy, 1, 1, 0);
liq_attr_set_progress_callback(attr, test_abort_callback, magic);
liq_result *res = liq_quantize_image(attr, img);
assert(!res);
liq_attr_destroy(attr);
}
static void test_fixed_colors() {
liq_attr *attr = liq_attr_create();
liq_attr_set_progress_callback(attr, test_continue_callback, magic);
liq_set_log_callback(attr, test_log_callback_function, magic);
unsigned char dummy[4] = {0};
liq_image *img = liq_image_create_rgba(attr, dummy, 1, 1, 0);
assert(img);
liq_image_add_fixed_color(img, (liq_color){0,0,0,0});
liq_result *res = liq_quantize_image(attr, img);
assert(res);
assert(progress_called);
const liq_palette *pal = liq_get_palette(res);
assert(pal);
assert(pal->count == 1);
liq_result_destroy(res);
liq_image_destroy(img);
liq_attr_destroy(attr);
}
static void test_fixed_colors_order() {
liq_attr *attr = liq_attr_create();
unsigned char dummy[4] = {0};
liq_image *img = liq_image_create_rgba(attr, dummy, 1, 1, 0);
assert(img);
liq_color colors[17] = {
{0,0,0,0}, {1,1,1,1}, {2,2,2,2}, {3,3,3,3}, {4,4,4,4}, {5,4,4,4},
{6,4,4,4}, {6,7,4,4}, {6,7,8,4}, {6,7,8,9}, {10,7,8,9}, {10,11,8,9},
{10,11,12,9}, {10,11,12,13}, {14,11,12,13}, {14,15,12,13}, {14,15,16,13},
};
for(int i=0; i < 17; i++) {
liq_image_add_fixed_color(img, colors[i]);
}
liq_result *res = liq_quantize_image(attr, img);
assert(res);
const liq_palette *pal = liq_get_palette(res);
assert(pal);
assert(pal->count == 17);
for(int i=0; i < 17; i++) {
assert(pal->entries[i].r == colors[i].r);
assert(pal->entries[i].g == colors[i].g);
assert(pal->entries[i].b == colors[i].b);
assert(pal->entries[i].a == colors[i].a);
}
liq_set_dithering_level(res, 1.0);
char buf[1];
assert(LIQ_OK == liq_write_remapped_image(res, img, buf, 1));
liq_result_set_progress_callback(res, test_abort_callback, magic);
assert(LIQ_ABORTED == liq_write_remapped_image(res, img, buf, 1));
liq_result_destroy(res);
liq_image_destroy(img);
liq_attr_destroy(attr);
}
int main(void) {
test_fixed_colors();
test_fixed_colors_order();
test_abort();
assert(printf("OK\n"));
return 0;
}