2015-03-30 04:55:04 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/vips7compat.h>
|
|
|
|
|
2015-04-04 22:12:45 +08:00
|
|
|
enum types {
|
|
|
|
UNKNOWN = 0,
|
|
|
|
JPEG,
|
|
|
|
WEBP,
|
|
|
|
PNG,
|
|
|
|
TIFF,
|
|
|
|
MAGICK
|
|
|
|
};
|
|
|
|
|
2015-04-11 08:49:11 +08:00
|
|
|
typedef struct {
|
|
|
|
char *Text;
|
|
|
|
char *Font;
|
|
|
|
} watermarkTextOptions;
|
|
|
|
|
|
|
|
typedef struct {
|
2015-04-11 08:56:59 +08:00
|
|
|
int Width;
|
|
|
|
int DPI;
|
|
|
|
int Margin;
|
|
|
|
int NoReplicate;
|
|
|
|
float Opacity;
|
2015-04-11 08:49:11 +08:00
|
|
|
double Background[3];
|
|
|
|
} watermarkOptions;
|
|
|
|
|
2015-03-30 04:55:04 +08:00
|
|
|
int
|
|
|
|
vips_affine_interpolator(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
|
|
|
|
vips_jpegload_buffer_shrink(void *buf, size_t len, VipsImage **out, int shrink)
|
|
|
|
{
|
|
|
|
return vips_jpegload_buffer(buf, len, out, "shrink", shrink, NULL);
|
|
|
|
};
|
|
|
|
|
2015-03-30 08:41:16 +08:00
|
|
|
int
|
2015-04-06 19:25:01 +08:00
|
|
|
vips_flip_bridge(VipsImage *in, VipsImage **out, int direction)
|
2015-03-30 08:41:16 +08:00
|
|
|
{
|
2015-04-06 08:04:10 +08:00
|
|
|
return vips_flip(in, out, direction, NULL);
|
2015-03-30 08:41:16 +08:00
|
|
|
};
|
|
|
|
|
2015-03-30 04:55:04 +08:00
|
|
|
int
|
2015-04-08 07:05:28 +08:00
|
|
|
vips_shrink_bridge(VipsImage *in, VipsImage **out, double xshrink, double yshrink)
|
2015-03-30 04:55:04 +08:00
|
|
|
{
|
|
|
|
return vips_shrink(in, out, xshrink, yshrink, NULL);
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_rotate(VipsImage *in, VipsImage **buf, int angle)
|
|
|
|
{
|
|
|
|
int rotate = VIPS_ANGLE_D0;
|
|
|
|
|
|
|
|
if (angle == 90) {
|
2015-03-30 08:43:24 +08:00
|
|
|
rotate = VIPS_ANGLE_D90;
|
|
|
|
} else if (angle == 180) {
|
|
|
|
rotate = VIPS_ANGLE_D180;
|
|
|
|
} else if (angle == 270) {
|
|
|
|
rotate = VIPS_ANGLE_D270;
|
|
|
|
}
|
2015-03-30 04:55:04 +08:00
|
|
|
|
|
|
|
return vips_rot(in, buf, rotate, NULL);
|
2015-03-30 08:41:16 +08:00
|
|
|
};
|
2015-03-30 04:55:04 +08:00
|
|
|
|
2015-04-06 04:01:48 +08:00
|
|
|
int
|
|
|
|
vips_exif_orientation(VipsImage *image) {
|
2015-04-06 23:06:44 +08:00
|
|
|
int orientation = 0;
|
|
|
|
const char **exif;
|
|
|
|
if (
|
|
|
|
vips_image_get_typeof(image, "exif-ifd0-Orientation") != 0 &&
|
|
|
|
!vips_image_get_string(image, "exif-ifd0-Orientation", exif)
|
|
|
|
) {
|
|
|
|
orientation = atoi(exif[0]);
|
|
|
|
}
|
|
|
|
return orientation;
|
2015-04-06 04:01:48 +08:00
|
|
|
};
|
|
|
|
|
2015-03-30 04:55:04 +08:00
|
|
|
int
|
2015-04-06 19:25:01 +08:00
|
|
|
has_profile_embed(VipsImage *image) {
|
|
|
|
return (vips_image_get_typeof(image, VIPS_META_ICC_NAME) > 0) ? 1 : 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
has_alpha_channel(VipsImage *image) {
|
|
|
|
return (
|
2015-04-06 23:06:44 +08:00
|
|
|
(image->Bands == 2 && image->Type == VIPS_INTERPRETATION_B_W) ||
|
|
|
|
(image->Bands == 4 && image->Type != VIPS_INTERPRETATION_CMYK) ||
|
|
|
|
(image->Bands == 5 && image->Type == VIPS_INTERPRETATION_CMYK)
|
|
|
|
) ? 1 : 0;
|
2015-04-06 19:25:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
interpolator_window_size(char const *name) {
|
2015-04-06 23:06:44 +08:00
|
|
|
VipsInterpolate *interpolator = vips_interpolate_new(name);
|
|
|
|
int window_size = vips_interpolate_get_window_size(interpolator);
|
|
|
|
g_object_unref(interpolator);
|
|
|
|
return window_size;
|
2015-04-06 19:25:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *
|
|
|
|
vips_enum_nick_bridge(VipsImage *image) {
|
|
|
|
return vips_enum_nick(VIPS_TYPE_INTERPRETATION, image->Type);
|
|
|
|
};
|
|
|
|
|
2015-04-09 07:44:54 +08:00
|
|
|
int
|
2015-04-09 08:08:07 +08:00
|
|
|
vips_zoom_bridge(VipsImage *in, VipsImage **out, int xfac, int yfac)
|
2015-04-09 07:44:54 +08:00
|
|
|
{
|
2015-04-09 08:08:07 +08:00
|
|
|
return vips_zoom(in, out, xfac, yfac, NULL);
|
2015-04-09 07:44:54 +08:00
|
|
|
};
|
|
|
|
|
2015-03-30 04:55:04 +08:00
|
|
|
int
|
2015-04-11 08:49:11 +08:00
|
|
|
vips_embed_bridge(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend)
|
2015-03-30 04:55:04 +08:00
|
|
|
{
|
2015-04-11 08:49:11 +08:00
|
|
|
return vips_embed(in, out, left, top, width, height, "extend", extend, NULL);
|
2015-03-30 04:55:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2015-04-06 19:25:01 +08:00
|
|
|
vips_extract_area_bridge(VipsImage *in, VipsImage **out, int left, int top, int width, int height)
|
2015-03-30 04:55:04 +08:00
|
|
|
{
|
|
|
|
return vips_extract_area(in, out, left, top, width, height, NULL);
|
2015-03-30 08:41:16 +08:00
|
|
|
};
|
2015-03-30 04:55:04 +08:00
|
|
|
|
|
|
|
int
|
2015-04-06 19:25:01 +08:00
|
|
|
vips_jpegsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace)
|
2015-03-30 04:55:04 +08:00
|
|
|
{
|
|
|
|
return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
|
2015-03-30 08:41:16 +08:00
|
|
|
};
|
2015-04-06 05:39:15 +08:00
|
|
|
|
|
|
|
int
|
2015-04-06 19:25:01 +08:00
|
|
|
vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace)
|
2015-04-06 05:39:15 +08:00
|
|
|
{
|
|
|
|
#if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42))
|
2015-04-06 23:06:44 +08:00
|
|
|
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression,
|
|
|
|
"interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
|
2015-04-06 05:39:15 +08:00
|
|
|
#else
|
2015-04-08 02:34:22 +08:00
|
|
|
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression,
|
2015-04-06 05:39:15 +08:00
|
|
|
"interlace", interlace, NULL);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2015-04-06 19:25:01 +08:00
|
|
|
vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace)
|
2015-04-06 05:39:15 +08:00
|
|
|
{
|
|
|
|
return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_init_image(void *buf, size_t len, int imageType, VipsImage **out) {
|
2015-04-06 23:06:44 +08:00
|
|
|
int code = 1;
|
|
|
|
|
|
|
|
if (imageType == JPEG) {
|
|
|
|
code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
|
|
|
} else if (imageType == PNG) {
|
|
|
|
code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
|
|
|
} else if (imageType == WEBP) {
|
|
|
|
code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
|
|
|
} else if (imageType == TIFF) {
|
|
|
|
code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
2015-04-08 05:02:31 +08:00
|
|
|
#if (VIPS_MAJOR_VERSION >= 8)
|
2015-04-06 23:06:44 +08:00
|
|
|
} else if (imageType == MAGICK) {
|
|
|
|
code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
2015-04-08 05:02:31 +08:00
|
|
|
#endif
|
2015-04-06 23:06:44 +08:00
|
|
|
}
|
2015-04-06 05:39:15 +08:00
|
|
|
|
2015-04-06 23:06:44 +08:00
|
|
|
return code;
|
2015-04-06 05:39:15 +08:00
|
|
|
};
|
2015-04-11 08:49:11 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
vips_watermark(VipsImage *in, VipsImage **out, watermarkTextOptions *to, watermarkOptions *o)
|
|
|
|
{
|
2015-04-11 09:12:49 +08:00
|
|
|
double ones[3] = { 1, 1, 1 };
|
|
|
|
|
|
|
|
VipsImage *base = vips_image_new();
|
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 12);
|
|
|
|
t[0] = in;
|
|
|
|
|
|
|
|
// Make the mask.
|
|
|
|
if (
|
|
|
|
vips_text(&t[1], to->Text,
|
|
|
|
"width", o->Width,
|
|
|
|
"dpi", o->DPI,
|
|
|
|
"font", to->Font,
|
|
|
|
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], 100, 100, t[3]->Xsize + o->Margin, t[3]->Ysize + o->Margin, NULL)
|
|
|
|
) {
|
|
|
|
g_object_unref(base);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replicate if necessary
|
|
|
|
if (o->NoReplicate != 1 && (
|
|
|
|
vips_replicate(t[4], &t[5],
|
|
|
|
1 + t[0]->Xsize / t[4]->Xsize,
|
|
|
|
1 + t[0]->Ysize / t[4]->Ysize, NULL) ||
|
|
|
|
vips_crop(t[5], &t[6], 0, 0, t[0]->Xsize, t[0]->Ysize, NULL))
|
|
|
|
) {
|
|
|
|
g_object_unref(base);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the constant image to paint the text with.
|
|
|
|
if (
|
|
|
|
vips_black(&t[7], 1, 1, NULL) ||
|
|
|
|
vips_linear( t[7], &t[8], ones, o->Background, 3, NULL) ||
|
|
|
|
vips_cast(t[8], &t[9], VIPS_FORMAT_UCHAR, NULL) ||
|
|
|
|
vips_copy(t[9], &t[10], "interpretation", t[0]->Type, NULL) ||
|
|
|
|
vips_embed(t[10], &t[11], 0, 0, t[0]->Xsize, t[0]->Ysize, "extend", VIPS_EXTEND_COPY, NULL)
|
|
|
|
) {
|
|
|
|
g_object_unref(base);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blend the mask and text and write to output.
|
|
|
|
if (vips_ifthenelse(t[6], t[11], t[0], out, "blend", TRUE, NULL)) {
|
|
|
|
g_object_unref(base);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref(base);
|
|
|
|
return (0);
|
2015-04-11 08:49:11 +08:00
|
|
|
};
|