forked from davidbyttow/govips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.c
302 lines (257 loc) · 9.27 KB
/
bridge.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
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
295
296
297
298
299
300
301
302
#include "bridge.h"
int is_16bit(VipsInterpretation interpretation) {
return interpretation == VIPS_INTERPRETATION_RGB16 || interpretation == VIPS_INTERPRETATION_GREY16;
}
int init_image(void *buf, size_t len, int imageType, ImageLoadOptions *o, VipsImage **out) {
int code = 1;
if (imageType == JPEG) {
code = vips_jpegload_buffer(buf, len, out, "access", o->access, NULL);
} else if (imageType == PNG) {
code = vips_pngload_buffer(buf, len, out, "access", o->access, NULL);
} else if (imageType == WEBP) {
code = vips_webpload_buffer(buf, len, out, "access", o->access, NULL);
} else if (imageType == TIFF) {
code = vips_tiffload_buffer(buf, len, out, "access", o->access, NULL);
#if (VIPS_MAJOR_VERSION >= 8)
#if (VIPS_MINOR_VERSION >= 3)
} else if (imageType == GIF) {
code = vips_gifload_buffer(buf, len, out, "access", o->access, NULL);
} else if (imageType == PDF) {
code = vips_pdfload_buffer(buf, len, out, "access", o->access, NULL);
} else if (imageType == SVG) {
code = vips_svgload_buffer(buf, len, out, "access", o->access, NULL);
#endif
} else if (imageType == MAGICK) {
code = vips_magickload_buffer(buf, len, out, "access", o->access, NULL);
#endif
}
return code;
}
unsigned long has_profile_embed(VipsImage *in) {
return vips_image_get_typeof(in, VIPS_META_ICC_NAME);
}
int remove_icc_profile(VipsImage *in) {
return vips_image_remove(in, VIPS_META_ICC_NAME);
}
int load_jpeg_buffer(void *buf, size_t len, VipsImage **out, int shrink) {
if (shrink > 0) {
return vips_jpegload_buffer(buf, len, out, "shrink", shrink, NULL);
} else {
return vips_jpegload_buffer(buf, len, out, NULL);
}
}
int copy_image(VipsImage *in, VipsImage **out) {
return vips_copy(in, out, NULL);
}
int save_jpeg_buffer(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
return vips_jpegsave_buffer(in, buf, len,
"strip", INT_TO_GBOOLEAN(strip),
"Q", quality,
"optimize_coding", TRUE,
"interlace", INT_TO_GBOOLEAN(interlace),
NULL
);
}
int save_png_buffer(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace) {
return vips_pngsave_buffer(in, buf, len,
"strip", INT_TO_GBOOLEAN(strip),
"compression", compression,
"interlace", INT_TO_GBOOLEAN(interlace),
"filter", VIPS_FOREIGN_PNG_FILTER_NONE,
NULL
);
}
int save_webp_buffer(VipsImage *in, void **buf, size_t *len, int strip, int quality, int lossless) {
return vips_webpsave_buffer(in, buf, len,
"strip", INT_TO_GBOOLEAN(strip),
"Q", quality,
"lossless", INT_TO_GBOOLEAN(lossless),
NULL
);
}
int save_tiff_buffer(VipsImage *in, void **buf, size_t *len) {
return vips_tiffsave_buffer(in, buf, len, NULL);
}
int is_colorspace_supported(VipsImage *in) {
return vips_colourspace_issupported(in) ? 1 : 0;
}
int to_colorspace(VipsImage *in, VipsImage **out, VipsInterpretation space) {
return vips_colourspace(in, out, space, NULL);
}
int resize_image(VipsImage *in, VipsImage **out, double scale, double vscale, int kernel) {
if (vscale > 0) {
return vips_resize(in, out, scale, "vscale", vscale, "kernel", kernel, NULL);
}
return vips_resize(in, out, scale, "kernel", kernel, NULL);
}
int rot_image(VipsImage *in, VipsImage **out, VipsAngle angle) {
return vips_rot(in, out, angle, NULL);
}
int flip_image(VipsImage *in, VipsImage **out, int direction) {
return vips_flip(in, out, direction, NULL);
}
int shrink_image(VipsImage *in, VipsImage **out, double xshrink, double yshrink) {
return vips_shrink(in, out, xshrink, yshrink, NULL);
}
int reduce_image(VipsImage *in, VipsImage **out, double xshrink, double yshrink) {
return vips_reduce(in, out, xshrink, yshrink, NULL);
}
int zoom_image(VipsImage *in, VipsImage **out, int xfac, int yfac) {
return vips_zoom(in, out, xfac, yfac, NULL);
}
int text(VipsImage **out, const char *text, const char *font, int width, int height, VipsAlign align, int dpi) {
return vips_text(out, text, "font", font, "width", width, "height", height, "align", align, "dpi", dpi, NULL);
}
int gaussian_blur(VipsImage *in, VipsImage **out, double sigma) {
return vips_gaussblur(in, out, sigma, NULL);
}
int invert_image(VipsImage *in, VipsImage **out) {
return vips_invert(in, out, NULL);
}
int extract_band(VipsImage *in, VipsImage **out, int band, int num) {
if (num > 0) {
return vips_extract_band(in, out, band, "n", num, NULL);
}
return vips_extract_band(in, out, band, NULL);
}
int linear1(VipsImage *in, VipsImage **out, double a, double b) {
return vips_linear1(in, out, a, b, NULL);
}
int embed_image(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend, double r, double g, double b) {
if (extend == VIPS_EXTEND_BACKGROUND) {
double background[3] = {r, g, b};
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
return vips_embed(in, out, left, top, width, height, "extend", extend, "background", vipsBackground, NULL);
}
return vips_embed(in, out, left, top, width, height, "extend", extend, NULL);
}
int composite(VipsImage **in, VipsImage **out, int n, int mode) {
return vips_composite(in, out, n, &mode, NULL);
}
int add(VipsImage *left, VipsImage *right, VipsImage **out) {
return vips_add(left, right, out, NULL);
}
int multiply(VipsImage *left, VipsImage *right, VipsImage **out) {
return vips_multiply(left, right, out, NULL);
}
int extract_image_area(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
return vips_extract_area(in, out, left, top, width, height, NULL);
}
int flatten_image_background(VipsImage *in, VipsImage **out, double r, double g, double b) {
if (is_16bit(in->Type)) {
r = 65535 * r / 255;
g = 65535 * g / 255;
b = 65535 * b / 255;
}
double background[3] = {r, g, b};
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
return vips_flatten(in, out,
"background", vipsBackground,
"max_alpha", is_16bit(in->Type) ? 65535.0 : 255.0,
NULL
);
}
int transform_image(VipsImage *in, VipsImage **out, double a, double b, double c, double d, VipsInterpolate *interpolator) {
return vips_affine(in, out, a, b, c, d, "interpolate", interpolator, NULL);
}
int find_image_loader(int t) {
switch (t) {
case GIF:
return vips_type_find("VipsOperation", "gifload");
case PDF:
return vips_type_find("VipsOperation", "pdfload");
case TIFF:
return vips_type_find("VipsOperation", "tiffload");
case SVG:
return vips_type_find("VipsOperation", "svgload");
case WEBP:
return vips_type_find("VipsOperation", "webpload");
case PNG:
return vips_type_find("VipsOperation", "pngload");
case JPEG:
return vips_type_find("VipsOperation", "jpegload");
case MAGICK:
return vips_type_find("VipsOperation", "magickload");
}
return 0;
}
int find_image_type_saver(int t) {
switch (t) {
case TIFF:
return vips_type_find("VipsOperation", "tiffsave_buffer");
case WEBP:
return vips_type_find("VipsOperation", "webpsave_buffer");
case PNG:
return vips_type_find("VipsOperation", "pngsave_buffer");
case JPEG:
return vips_type_find("VipsOperation", "jpegsave_buffer");
}
return 0;
}
void gobject_set_property(VipsObject *object, const char *name, const GValue *value) {
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
GType type = G_VALUE_TYPE( value );
GParamSpec *pspec;
VipsArgumentClass *argument_class;
VipsArgumentInstance *argument_instance;
if( vips_object_get_argument( object, name,
&pspec, &argument_class, &argument_instance ) ) {
g_warning( "gobject warning: %s", vips_error_buffer() );
vips_error_clear();
return;
}
if( G_IS_PARAM_SPEC_ENUM( pspec ) &&
type == G_TYPE_STRING ) {
GType pspec_type = G_PARAM_SPEC_VALUE_TYPE( pspec );
int enum_value;
GValue value2 = { 0 };
if( (enum_value = vips_enum_from_nick( object_class->nickname,
pspec_type, g_value_get_string( value ) )) < 0 ) {
g_warning( "gobject warning: %s", vips_error_buffer() );
vips_error_clear();
return;
}
g_value_init( &value2, pspec_type );
g_value_set_enum( &value2, enum_value );
g_object_set_property( G_OBJECT( object ), name, &value2 );
g_value_unset( &value2 );
} else {
g_object_set_property( G_OBJECT( object ), name, value );
}
}
int label(VipsImage *in, VipsImage **out, LabelOptions *o) {
double ones[3] = { 1, 1, 1 };
VipsImage *base = vips_image_new();
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
t[0] = in;
if (
vips_text(&t[1], o->Text,
"font", o->Font,
"width", o->Width,
"height", o->Height,
"align", o->Align,
NULL) ||
vips_linear1(t[1], &t[2], o->Opacity, 0.0, NULL) ||
vips_cast(t[2], &t[3], VIPS_FORMAT_UCHAR, NULL) ||
vips_embed(t[3], &t[4], o->OffsetX, o->OffsetY, t[3]->Xsize + o->OffsetX, t[3]->Ysize + o->OffsetY, NULL)
) {
g_object_unref(base);
return 1;
}
if (
vips_black(&t[5], 1, 1, NULL) ||
vips_linear(t[5], &t[6], ones, o->Color, 3, NULL) ||
vips_cast(t[6], &t[7], VIPS_FORMAT_UCHAR, NULL) ||
vips_copy(t[7], &t[8], "interpretation", t[0]->Type, NULL) ||
vips_embed(t[8], &t[9], 0, 0, t[0]->Xsize, t[0]->Ysize, "extend", VIPS_EXTEND_COPY, NULL)
) {
g_object_unref(base);
return 1;
}
if (vips_ifthenelse(t[4], t[9], t[0], out, "blend", TRUE, NULL)) {
g_object_unref(base);
return 1;
}
g_object_unref(base);
return 0;
}