-
Notifications
You must be signed in to change notification settings - Fork 1
/
globals.h
294 lines (266 loc) · 8.58 KB
/
globals.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#pragma once
#include "structs.h"
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define PCONVERT_VERSION "0.4.6"
#define PCONVERT_COMPILATION_DATE __DATE__
#define PCONVERT_COMPILATION_TIME __TIME__
#ifdef _MSC_VER
#define PCONVERT_PLATFORM_MSC true
#define PCONVERT_COMPILER "msvc"
#define PCONVERT_COMPILER_VERSION _MSC_VER
#define PCONVERT_COMPILER_VERSION_STRING TOSTRING(_MSC_VER)
#endif
#ifdef __GNUC__
#define PCONVERT_PLATFORM_GCC true
#define PCONVERT_COMPILER "gcc"
#define PCONVERT_COMPILER_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#define PCONVERT_COMPILER_VERSION_STRING __VERSION__
#endif
#define PCONVERT_PLATFORM_CPU_BITS sizeof(void *) * 8
#ifdef PCONVERT_OPENCL
#define PCONVERT_OPENCL_V 1
#define PCONVERT_OPENCL_S " opencl"
#else
#define PCONVERT_OPENCL_V 0
#define PCONVERT_OPENCL_S ""
#endif
#ifdef PCONVERT_EXTENSION
#define PCONVERT_EXTENSION_V 1
#define PCONVERT_EXTENSION_S " python"
#else
#define PCONVERT_EXTENSION_V 0
#define PCONVERT_EXTENSION_S ""
#endif
#define PCONVERT_FEATURES "cpu" PCONVERT_OPENCL_S PCONVERT_EXTENSION_S
#define PCONVERT_ALGORITHMS "multiplicative", "source_over", "destination_over",\
"mask_top", "first_top", "first_bottom", "disjoint_over", "disjoint_under", "disjoint_debug"
#define TRUE 1
#define FALSE 0
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define ROUND(x) floor(x + 0.5)
#define ERROR_T int
#define ERROR -1
#define NO_ERROR 0
#define RAISE return ERROR
#define RAISE_S(...) abort_(__VA_ARGS__); return ERROR
#define RAISE_M(message) set_last_error(message); return ERROR
#define RAISE_F(message, ...) set_last_error_f(message, __VA_ARGS__); return ERROR
#define EXCEPT_P(input) if(IS_ERROR(input)) {\
print_(last_error == NULL ? "Unknown error" : last_error);\
} while(FALSE)
#define EXCEPT_S(input) if(IS_ERROR(input)) {\
print_(last_error == NULL ? "Unknown error" : last_error);\
return ERROR;\
} while(FALSE)
#define EXCEPT_M(input, ...) if(IS_ERROR(input)) {\
__VA_ARGS__;\
print_(last_error == NULL ? "Unknown error" : last_error);\
return ERROR;\
} while(FALSE)
#define NORMAL return NO_ERROR
#define IS_ERROR(input) input != NO_ERROR
#define VALIDATE(input) if(IS_ERROR(input)) { RAISE; } while(FALSE)
#define VALIDATE_R(input, ...) if(IS_ERROR(input)) { return __VA_ARGS__; } while(FALSE)
#define VALIDATE_A(input, ...) if(IS_ERROR(input)) { __VA_ARGS__; } while(FALSE)
#define VALIDATE_PY(input, ...) if(IS_ERROR(input)) {\
__VA_ARGS__;\
PyErr_SetString(PyExc_TypeError, last_error == NULL ? "Unknown error" : last_error);\
return NULL;\
} while(FALSE)
#define BENCHMARK(benchmark, start, target, ...)\
start = (float) clock() / CLOCKS_PER_SEC;\
__VA_ARGS__;\
if (benchmark != NULL) { target += ((float) clock() / CLOCKS_PER_SEC) - start; }\
while(FALSE)
#define Z_NO_COMPRESSION 0
#define Z_BEST_SPEED 1
#define Z_BEST_COMPRESSION 9
#define Z_DEFAULT_COMPRESSION (-1)
#define MAX_ERROR_L 1024
EXTERNAL_PREFIX char *last_error;
EXTERNAL_PREFIX char last_error_b[MAX_ERROR_L];
typedef struct pcv_image {
int width;
int height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
png_bytep *rows;
} pcv_image_t;
typedef struct benchmark {
float blend_time;
float read_png_time;
float write_png_time;
} benchmark_t;
typedef void (blend_algorithm) (
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void print_(const char *s, ...);
void abort_(const char *s, ...);
void set_last_error_f(char *message, ...);
ERROR_T libpng_version(char *buffer);
ERROR_T read_png(char *file_name, char demultiply, struct pcv_image *image);
ERROR_T write_png(struct pcv_image *image, char multiply, char *file_name);
ERROR_T write_png_extra(
struct pcv_image *image,
char multiply,
char *file_name,
int compression,
int filter
);
ERROR_T demultiply_image(struct pcv_image *image);
ERROR_T multiply_image(struct pcv_image *image);
ERROR_T process_image(struct pcv_image *image);
ERROR_T blend_images(
struct pcv_image *bottom,
struct pcv_image *top,
char *algorithm,
params *params
);
ERROR_T blend_images_i(
struct pcv_image *bottom,
struct pcv_image *top,
char *algorithm,
params *params
);
ERROR_T blend_images_source_over_fast(
struct pcv_image *bottom,
struct pcv_image *top,
char *algorithm,
params *params
);
ERROR_T blend_images_destination_over_fast(
struct pcv_image *bottom,
struct pcv_image *top,
char *algorithm,
params *params
);
ERROR_T blend_images_debug(
struct pcv_image *bottom,
struct pcv_image *top,
char *algorithm,
params *params,
char *file_path
);
ERROR_T blend_images_extra(
struct pcv_image *bottom,
struct pcv_image *top,
char *algorithm,
params *params,
char use_opencl
);
ERROR_T release_image(struct pcv_image *image);
ERROR_T release_image_s(struct pcv_image *image, char destroy_struct);
ERROR_T copy_image(struct pcv_image *origin, struct pcv_image *target);
ERROR_T duplicate_image(struct pcv_image *origin, struct pcv_image *target);
ERROR_T compose_images(
char *base_path,
char *algorithm,
params *params,
char *background,
struct benchmark *benchmark
);
ERROR_T compose_images_extra(
char *base_path,
char *algorithm,
params *params,
char *background,
int compression,
int filter,
char use_opencl,
struct benchmark *benchmark
);
char *join_path(char *base, char *extra, char *result);
blend_algorithm *get_blend_algorithm(char *algorithm);
char is_multiplied(char *algorithm);
void blend_alpha(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_multiplicative(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_source_over(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_destination_over(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_mask_top(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_first_top(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_first_bottom(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_disjoint_under(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_disjoint_over(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
void blend_disjoint_debug(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
);
static FINLINE void blend_source_over_i(
params *params,
png_byte *result,
png_byte rb, png_byte gb, png_byte bb, png_byte ab,
png_byte rt, png_byte gt, png_byte bt, png_byte at
) {
png_byte r, g, b, a;
float abf = 1.0f * (ab / 255.0f);
float atf = 1.0f * (at / 255.0f);
float af = abf + atf * (1.0f - abf);
r = af == 0.0f ? 0 : (png_byte) ((rb * abf + rt * atf * (1.0f - abf)) / af);
g = af == 0.0f ? 0 : (png_byte) ((gb * abf + gt * atf * (1.0f - abf)) / af);
b = af == 0.0f ? 0 : (png_byte) ((bb * abf + bt * atf * (1.0f - abf)) / af);
a = MAX(0, MIN(255, (png_byte) (af * 255.0f)));
r = MAX(0, MIN(255, r));
g = MAX(0, MIN(255, g));
b = MAX(0, MIN(255, b));
*result = r;
*(result + 1) = g;
*(result + 2) = b;
*(result + 3) = a;
}
static FINLINE void set_last_error(char *message, ...) {
last_error = message;
}