2019-02-09 14:06:26 +02:00
|
|
|
#include "vips.h"
|
2019-05-07 13:23:38 +02:00
|
|
|
#include <string.h>
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2021-04-26 14:27:16 +02:00
|
|
|
#define VIPS_SUPPORT_AVIF_SPEED \
|
|
|
|
(VIPS_MAJOR_VERSION > 8 || \
|
|
|
|
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION > 10) || \
|
|
|
|
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10 && VIPS_MICRO_VERSION >= 2))
|
|
|
|
|
2021-11-22 20:16:28 +02:00
|
|
|
#define VIPS_SUPPORT_AVIF_EFFORT \
|
|
|
|
(VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 12))
|
|
|
|
|
2021-11-22 20:12:59 +02:00
|
|
|
#define VIPS_SUPPORT_GIFSAVE \
|
|
|
|
(VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 12))
|
|
|
|
|
2022-07-18 14:49:40 +02:00
|
|
|
#define VIPS_GIF_RESOLUTION_LIMITED \
|
|
|
|
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION <= 12)
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
int
|
|
|
|
vips_initialize() {
|
|
|
|
return vips_init("imgproxy");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clear_image(VipsImage **in) {
|
|
|
|
if (G_IS_OBJECT(*in)) g_clear_object(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_free_go(void **buf) {
|
|
|
|
g_free(*buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
swap_and_clear(VipsImage **in, VipsImage *out) {
|
|
|
|
clear_image(in);
|
|
|
|
*in = out;
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:49:40 +02:00
|
|
|
int
|
|
|
|
gif_resolution_limit() {
|
|
|
|
#if VIPS_GIF_RESOLUTION_LIMITED
|
|
|
|
// https://github.com/libvips/libvips/blob/v8.12.2/libvips/foreign/cgifsave.c#L437-L442
|
|
|
|
return 2000 * 2000;
|
|
|
|
#else
|
|
|
|
return INT_MAX / 4;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
int
|
|
|
|
vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
|
|
|
|
if (shrink > 1)
|
|
|
|
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
|
|
|
|
|
|
|
|
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_pngload_go(void *buf, size_t len, VipsImage **out) {
|
|
|
|
return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-06-03 12:53:47 +02:00
|
|
|
vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
|
|
|
|
return vips_webpload_buffer(
|
|
|
|
buf, len, out,
|
|
|
|
"access", VIPS_ACCESS_SEQUENTIAL,
|
|
|
|
"scale", scale,
|
|
|
|
"n", pages,
|
|
|
|
NULL
|
|
|
|
);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
|
2021-03-15 13:54:08 +02:00
|
|
|
return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
|
2021-03-15 13:54:08 +02:00
|
|
|
// libvips limits the minimal scale to 0.001, so we have to scale down dpi
|
|
|
|
// for lower scale values
|
|
|
|
double dpi = 72.0;
|
|
|
|
if (scale < 0.001) {
|
|
|
|
dpi *= VIPS_MAX(scale / 0.001, 0.001);
|
|
|
|
scale = 0.001;
|
|
|
|
}
|
2021-02-22 17:57:19 +02:00
|
|
|
|
2021-03-15 13:54:08 +02:00
|
|
|
return vips_svgload_buffer(
|
|
|
|
buf, len, out,
|
|
|
|
"access", VIPS_ACCESS_SEQUENTIAL,
|
|
|
|
"scale", scale,
|
|
|
|
"dpi", dpi,
|
|
|
|
NULL
|
|
|
|
);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 18:07:03 +02:00
|
|
|
int
|
2022-04-14 17:41:00 +02:00
|
|
|
vips_heifload_go(void *buf, size_t len, VipsImage **out, int thumbnail) {
|
|
|
|
return vips_heifload_buffer(
|
|
|
|
buf, len, out,
|
|
|
|
"access", VIPS_ACCESS_SEQUENTIAL,
|
|
|
|
"thumbnail", thumbnail,
|
|
|
|
NULL
|
|
|
|
);
|
2019-06-03 18:07:03 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 13:30:20 +02:00
|
|
|
int
|
2021-11-11 09:43:06 +02:00
|
|
|
vips_tiffload_go(void *buf, size_t len, VipsImage **out) {
|
|
|
|
return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
2019-09-30 13:30:20 +02:00
|
|
|
}
|
2019-09-30 13:36:38 +02:00
|
|
|
|
2019-09-27 13:49:52 +02:00
|
|
|
int
|
2021-11-11 09:43:06 +02:00
|
|
|
vips_black_go(VipsImage **out, int width, int height, int bands) {
|
|
|
|
VipsImage *tmp;
|
|
|
|
|
|
|
|
int res = vips_black(&tmp, width, height, "bands", bands, NULL) ||
|
|
|
|
vips_copy(tmp, out, "interpretation", VIPS_INTERPRETATION_sRGB, NULL);
|
|
|
|
|
|
|
|
clear_image(&tmp);
|
|
|
|
|
|
|
|
return res;
|
2019-09-27 13:49:52 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 16:57:10 +02:00
|
|
|
int
|
2019-09-19 15:44:11 +02:00
|
|
|
vips_get_orientation(VipsImage *image) {
|
|
|
|
int orientation;
|
|
|
|
|
2022-07-21 10:46:45 +02:00
|
|
|
if (
|
2019-10-11 13:19:15 +02:00
|
|
|
vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
|
|
|
|
vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
|
|
|
|
) return orientation;
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2022-07-21 10:46:45 +02:00
|
|
|
return 1;
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 15:04:07 +02:00
|
|
|
int
|
|
|
|
vips_get_palette_bit_depth(VipsImage *image) {
|
|
|
|
int palette_bit_depth;
|
|
|
|
|
|
|
|
if (
|
|
|
|
vips_image_get_typeof(image, "palette-bit-depth") == G_TYPE_INT &&
|
|
|
|
vips_image_get_int(image, "palette-bit-depth", &palette_bit_depth) == 0
|
|
|
|
) return palette_bit_depth;
|
|
|
|
|
2022-07-21 10:46:45 +02:00
|
|
|
return 0;
|
2021-03-31 15:04:07 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
VipsBandFormat
|
|
|
|
vips_band_format(VipsImage *in) {
|
|
|
|
return in->BandFmt;
|
|
|
|
}
|
|
|
|
|
2019-06-03 12:53:47 +02:00
|
|
|
gboolean
|
|
|
|
vips_is_animated(VipsImage * in) {
|
2022-06-10 11:27:15 +02:00
|
|
|
int n_pages;
|
|
|
|
|
|
|
|
return( vips_image_get_typeof(in, "delay") != G_TYPE_INVALID &&
|
|
|
|
vips_image_get_typeof(in, "loop") != G_TYPE_INVALID &&
|
|
|
|
vips_image_get_typeof(in, "page-height") == G_TYPE_INT &&
|
|
|
|
vips_image_get_typeof(in, "n-pages") == G_TYPE_INT &&
|
|
|
|
vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
|
|
|
|
n_pages > 1 );
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:30:55 +02:00
|
|
|
int
|
|
|
|
vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
|
|
|
|
return vips_image_get_array_int(image, name, out, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
|
|
|
|
vips_image_set_array_int(image, name, array, n);
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:04:33 +02:00
|
|
|
int
|
|
|
|
vips_addalpha_go(VipsImage *in, VipsImage **out) {
|
|
|
|
return vips_addalpha(in, out, NULL);
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
int
|
|
|
|
vips_premultiply_go(VipsImage *in, VipsImage **out) {
|
|
|
|
if (!vips_image_hasalpha(in))
|
|
|
|
return vips_copy(in, out, NULL);
|
|
|
|
|
|
|
|
return vips_premultiply(in, out, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
|
|
|
|
if (!vips_image_hasalpha(in))
|
|
|
|
return vips_copy(in, out, NULL);
|
|
|
|
|
|
|
|
return vips_unpremultiply(in, out, NULL);
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
int
|
|
|
|
vips_copy_go(VipsImage *in, VipsImage **out) {
|
|
|
|
return vips_copy(in, out, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
|
|
|
|
return vips_cast(in, out, format, NULL);
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:57:10 +02:00
|
|
|
int
|
|
|
|
vips_rad2float_go(VipsImage *in, VipsImage **out) {
|
2022-07-21 10:46:45 +02:00
|
|
|
return vips_rad2float(in, out, NULL);
|
2019-03-22 16:57:10 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
int
|
2021-03-29 13:13:28 +02:00
|
|
|
vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale) {
|
2021-04-26 13:52:50 +02:00
|
|
|
if (!vips_image_hasalpha(in))
|
|
|
|
return vips_resize(in, out, wscale, "vscale", hscale, NULL);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
VipsBandFormat format = vips_band_format(in);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
VipsImage *base = vips_image_new();
|
2022-07-21 10:46:45 +02:00
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 3);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
int res =
|
|
|
|
vips_premultiply(in, &t[0], NULL) ||
|
|
|
|
vips_resize(t[0], &t[1], wscale, "vscale", hscale, NULL) ||
|
|
|
|
vips_unpremultiply(t[1], &t[2], NULL) ||
|
|
|
|
vips_cast(t[2], out, format, NULL);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
clear_image(&base);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:23:22 +02:00
|
|
|
int
|
|
|
|
vips_pixelate(VipsImage *in, VipsImage **out, int pixels) {
|
|
|
|
VipsImage *base = vips_image_new();
|
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 3);
|
|
|
|
|
|
|
|
int w, h, tw, th;
|
|
|
|
|
|
|
|
w = in->Xsize;
|
|
|
|
h = in->Ysize;
|
|
|
|
|
|
|
|
tw = (int)((double)(w + pixels - 1) / pixels) * pixels;
|
|
|
|
th = (int)((double)(h + pixels - 1) / pixels) * pixels;
|
|
|
|
|
|
|
|
if (tw > w || th > h) {
|
|
|
|
if (vips_embed(in, &t[0], 0, 0, tw, th, "extend", VIPS_EXTEND_COPY, NULL)) {
|
|
|
|
clear_image(&base);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (vips_copy(in, &t[0], NULL)) {
|
|
|
|
clear_image(&base);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
vips_shrink(t[0], &t[1], pixels, pixels, NULL) ||
|
|
|
|
vips_zoom(t[1], &t[2], pixels, pixels, NULL)
|
|
|
|
) {
|
|
|
|
clear_image(&base);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tw > w || th > h) {
|
|
|
|
if (vips_extract_area(t[2], out, 0, 0, w, h, NULL)) {
|
|
|
|
clear_image(&base);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (vips_copy(t[2], out, NULL)) {
|
|
|
|
clear_image(&base);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_image(&base);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-08 12:21:10 +02:00
|
|
|
int
|
|
|
|
vips_icc_is_srgb_iec61966(VipsImage *in) {
|
2021-07-08 12:48:40 +02:00
|
|
|
const void *data;
|
2021-07-08 12:21:10 +02:00
|
|
|
size_t data_len;
|
|
|
|
|
|
|
|
// 1998-12-01
|
|
|
|
static char date[] = { 7, 206, 0, 2, 0, 9 };
|
|
|
|
// 2.1
|
|
|
|
static char version[] = { 2, 16, 0, 0 };
|
|
|
|
|
|
|
|
// The image had no profile and built-in CMYK was imported.
|
|
|
|
// Vips gives us an invalid data pointer when the built-in profile was imported,
|
|
|
|
// so we check this mark before receiving an actual profile.
|
|
|
|
// if (vips_image_get_typeof(in, "icc-cmyk-no-profile"))
|
|
|
|
// return FALSE;
|
|
|
|
|
|
|
|
if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
// Less than header size
|
|
|
|
if (data_len < 128)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
// Predict it is sRGB IEC61966 2.1 by checking some header fields
|
|
|
|
return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
|
|
|
|
(memcmp(data + 52, "sRGB", 4) == 0) && // Device model
|
|
|
|
(memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
|
|
|
|
(memcmp(data + 24, date, 6) == 0) && // Date of creation
|
|
|
|
(memcmp(data + 8, version, 4) == 0)); // Version
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
int
|
2019-05-30 13:26:30 +02:00
|
|
|
vips_has_embedded_icc(VipsImage *in) {
|
|
|
|
return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-11-18 08:22:23 +02:00
|
|
|
vips_icc_import_go(VipsImage *in, VipsImage **out) {
|
2021-12-07 10:21:51 +02:00
|
|
|
return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
|
2021-01-12 16:43:09 +02:00
|
|
|
}
|
2020-11-18 08:22:23 +02:00
|
|
|
|
2021-01-12 16:43:09 +02:00
|
|
|
int
|
|
|
|
vips_icc_export_go(VipsImage *in, VipsImage **out) {
|
2021-12-07 10:21:51 +02:00
|
|
|
return vips_icc_export(in, out, "pcs", VIPS_PCS_LAB, NULL);
|
2021-01-12 16:43:09 +02:00
|
|
|
}
|
2020-11-18 08:22:23 +02:00
|
|
|
|
2021-01-12 16:43:09 +02:00
|
|
|
int
|
|
|
|
vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
|
2021-12-07 10:21:51 +02:00
|
|
|
return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", VIPS_PCS_LAB, NULL);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-11-18 08:22:23 +02:00
|
|
|
vips_icc_transform_go(VipsImage *in, VipsImage **out) {
|
2021-12-07 10:21:51 +02:00
|
|
|
return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
|
2021-01-12 16:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_icc_remove(VipsImage *in, VipsImage **out) {
|
|
|
|
if (vips_copy(in, out, NULL)) return 1;
|
2020-01-30 17:43:08 +02:00
|
|
|
|
|
|
|
vips_image_remove(*out, VIPS_META_ICC_NAME);
|
|
|
|
|
|
|
|
return 0;
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
|
|
|
|
return vips_colourspace(in, out, cs, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
|
|
|
|
return vips_rot(in, out, angle, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
|
|
|
|
return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
|
|
|
|
return vips_smartcrop(in, out, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
|
|
|
|
return vips_gaussblur(in, out, sigma, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
|
|
|
|
return vips_sharpen(in, out, "sigma", sigma, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
|
2021-04-26 13:52:50 +02:00
|
|
|
if (!vips_image_hasalpha(in))
|
|
|
|
return vips_copy(in, out, NULL);
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
|
|
|
|
int res = vips_flatten(in, out, "background", bg, NULL);
|
|
|
|
vips_area_unref((VipsArea *)bg);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
|
|
|
|
return vips_extract_area(in, out, left, top, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:54:50 +02:00
|
|
|
int
|
2020-04-07 14:06:28 +02:00
|
|
|
vips_trim(VipsImage *in, VipsImage **out, double threshold,
|
|
|
|
gboolean smart, double r, double g, double b,
|
|
|
|
gboolean equal_hor, gboolean equal_ver) {
|
2020-01-17 11:54:50 +02:00
|
|
|
|
2022-06-23 13:59:04 +02:00
|
|
|
VipsImage *base = vips_image_new();
|
2022-07-21 10:46:45 +02:00
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
|
2022-06-23 13:59:04 +02:00
|
|
|
|
|
|
|
VipsImage *tmp = in;
|
|
|
|
|
|
|
|
if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
|
|
|
|
if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
|
|
|
|
clear_image(&base);
|
2020-01-17 11:54:50 +02:00
|
|
|
return 1;
|
2022-06-23 13:59:04 +02:00
|
|
|
}
|
|
|
|
tmp = t[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vips_image_hasalpha(tmp)) {
|
|
|
|
if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
|
|
|
|
clear_image(&base);
|
2020-01-17 11:54:50 +02:00
|
|
|
return 1;
|
2022-06-23 13:59:04 +02:00
|
|
|
}
|
|
|
|
tmp = t[1];
|
2020-01-17 11:54:50 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 13:59:04 +02:00
|
|
|
double *bg = NULL;
|
2020-01-17 11:54:50 +02:00
|
|
|
int bgn;
|
2020-04-07 14:06:28 +02:00
|
|
|
VipsArrayDouble *bga;
|
2020-01-17 11:54:50 +02:00
|
|
|
|
2020-04-07 14:06:28 +02:00
|
|
|
if (smart) {
|
|
|
|
if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
|
2022-06-23 13:59:04 +02:00
|
|
|
clear_image(&base);
|
2020-04-07 14:06:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
bga = vips_array_double_new(bg, bgn);
|
|
|
|
} else {
|
|
|
|
bga = vips_array_double_newv(3, r, g, b);
|
2020-01-17 11:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:06:28 +02:00
|
|
|
int left, right, top, bot, width, height, diff;
|
2022-06-23 13:59:04 +02:00
|
|
|
int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
|
2020-01-17 11:54:50 +02:00
|
|
|
|
2022-06-23 13:59:04 +02:00
|
|
|
clear_image(&base);
|
|
|
|
vips_area_unref((VipsArea *)bga);
|
|
|
|
g_free(bg);
|
|
|
|
|
|
|
|
if (res) {
|
2020-01-17 11:54:50 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-07 14:06:28 +02:00
|
|
|
if (equal_hor) {
|
|
|
|
right = in->Xsize - left - width;
|
|
|
|
diff = right - left;
|
|
|
|
if (diff > 0) {
|
|
|
|
width += diff;
|
|
|
|
} else if (diff < 0) {
|
|
|
|
left = right;
|
|
|
|
width -= diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal_ver) {
|
|
|
|
bot = in->Ysize - top - height;
|
|
|
|
diff = bot - top;
|
|
|
|
if (diff > 0) {
|
|
|
|
height += diff;
|
|
|
|
} else if (diff < 0) {
|
|
|
|
top = bot;
|
|
|
|
height -= diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:54:50 +02:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
return vips_copy(in, out, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vips_extract_area(in, out, left, top, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
int
|
|
|
|
vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
|
|
|
|
VipsImage *tmp;
|
|
|
|
|
|
|
|
if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
|
|
|
|
return 1;
|
|
|
|
|
2022-07-21 10:46:45 +02:00
|
|
|
if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
|
2019-02-09 14:06:26 +02:00
|
|
|
clear_image(&tmp);
|
2022-07-21 10:46:45 +02:00
|
|
|
return 1;
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clear_image(&tmp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-04-26 13:52:50 +02:00
|
|
|
vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
|
2021-12-07 10:21:51 +02:00
|
|
|
VipsImage *tmp;
|
2021-04-26 13:52:50 +02:00
|
|
|
|
|
|
|
int ret =
|
2021-12-07 10:21:51 +02:00
|
|
|
vips_ensure_alpha(in, &tmp) ||
|
|
|
|
vips_embed(tmp, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
|
2021-04-26 13:52:50 +02:00
|
|
|
|
2021-12-07 10:21:51 +02:00
|
|
|
clear_image(&tmp);
|
2021-04-26 13:52:50 +02:00
|
|
|
|
2019-06-17 12:13:34 +02:00
|
|
|
return ret;
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-02-21 17:55:20 +02:00
|
|
|
vips_ensure_alpha(VipsImage *in, VipsImage **out) {
|
2021-04-26 13:52:50 +02:00
|
|
|
if (vips_image_hasalpha(in))
|
2019-02-21 17:55:20 +02:00
|
|
|
return vips_copy(in, out, NULL);
|
|
|
|
|
|
|
|
return vips_bandjoin_const1(in, out, 255, NULL);
|
|
|
|
}
|
2019-02-09 14:06:26 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
|
2019-09-11 11:45:11 +02:00
|
|
|
VipsImage *base = vips_image_new();
|
2022-07-21 10:46:45 +02:00
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 6);
|
2021-04-26 13:52:50 +02:00
|
|
|
|
|
|
|
if (vips_ensure_alpha(watermark, &t[0])) {
|
|
|
|
clear_image(&base);
|
2022-07-21 10:46:45 +02:00
|
|
|
return 1;
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2022-07-21 10:46:45 +02:00
|
|
|
if (opacity < 1) {
|
2019-09-11 11:45:11 +02:00
|
|
|
if (
|
2021-04-26 13:52:50 +02:00
|
|
|
vips_extract_band(t[0], &t[1], 0, "n", t[0]->Bands - 1, NULL) ||
|
|
|
|
vips_extract_band(t[0], &t[2], t[0]->Bands - 1, "n", 1, NULL) ||
|
2022-07-21 10:46:45 +02:00
|
|
|
vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
|
2021-04-26 13:52:50 +02:00
|
|
|
vips_bandjoin2(t[1], t[3], &t[4], NULL)
|
2019-09-11 11:45:11 +02:00
|
|
|
) {
|
|
|
|
clear_image(&base);
|
2022-07-21 10:46:45 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-26 13:52:50 +02:00
|
|
|
if (vips_copy(t[0], &t[4], NULL)) {
|
2019-09-11 11:45:11 +02:00
|
|
|
clear_image(&base);
|
|
|
|
return 1;
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:45:11 +02:00
|
|
|
int res =
|
2021-04-26 13:52:50 +02:00
|
|
|
vips_composite2(in, t[4], &t[5], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
|
|
|
|
vips_cast(t[5], out, vips_image_get_format(in), NULL);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2019-09-11 11:45:11 +02:00
|
|
|
clear_image(&base);
|
2019-02-09 14:06:26 +02:00
|
|
|
|
2019-09-11 11:45:11 +02:00
|
|
|
return res;
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
|
|
|
|
return vips_arrayjoin(in, out, n, "across", 1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2022-04-25 12:53:08 +02:00
|
|
|
vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright) {
|
2021-07-20 15:29:18 +02:00
|
|
|
static double default_resolution = 72.0 / 25.4;
|
|
|
|
|
|
|
|
if (vips_copy(
|
|
|
|
in, out,
|
|
|
|
"xres", default_resolution,
|
|
|
|
"yres", default_resolution,
|
|
|
|
NULL
|
|
|
|
)) return 1;
|
2021-01-12 16:43:09 +02:00
|
|
|
|
|
|
|
gchar **fields = vips_image_get_fields(in);
|
|
|
|
|
|
|
|
for (int i = 0; fields[i] != NULL; i++) {
|
|
|
|
gchar *name = fields[i];
|
|
|
|
|
|
|
|
if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
|
2021-03-31 15:04:07 +02:00
|
|
|
if (strcmp(name, "palette-bit-depth") == 0) continue;
|
2021-01-12 16:43:09 +02:00
|
|
|
|
2022-04-25 12:53:08 +02:00
|
|
|
if (keep_exif_copyright) {
|
|
|
|
if (strcmp(name, VIPS_META_EXIF_NAME) == 0) continue;
|
|
|
|
if (strcmp(name, "exif-ifd0-Copyright") == 0) continue;
|
|
|
|
if (strcmp(name, "exif-ifd0-Artist") == 0) continue;
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:43:09 +02:00
|
|
|
vips_image_remove(*out, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(fields);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
|
|
|
|
return vips_jpegsave_buffer(
|
|
|
|
in, buf, len,
|
|
|
|
"Q", quality,
|
|
|
|
"optimize_coding", TRUE,
|
|
|
|
"interlace", interlace,
|
|
|
|
NULL
|
|
|
|
);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-04-05 12:30:16 +02:00
|
|
|
vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
|
2021-03-31 15:04:07 +02:00
|
|
|
int bitdepth;
|
|
|
|
|
|
|
|
if (quantize) {
|
|
|
|
bitdepth = 1;
|
|
|
|
if (colors > 16) bitdepth = 8;
|
|
|
|
else if (colors > 4) bitdepth = 4;
|
|
|
|
else if (colors > 2) bitdepth = 2;
|
|
|
|
} else {
|
|
|
|
bitdepth = vips_get_palette_bit_depth(in);
|
|
|
|
if (bitdepth) {
|
2022-07-12 14:46:35 +02:00
|
|
|
if (bitdepth > 4) bitdepth = 8;
|
|
|
|
else if (bitdepth > 2) bitdepth = 4;
|
2021-03-31 15:04:07 +02:00
|
|
|
quantize = 1;
|
|
|
|
colors = 1 << bitdepth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:54:15 +02:00
|
|
|
if (!quantize)
|
|
|
|
return vips_pngsave_buffer(
|
|
|
|
in, buf, len,
|
|
|
|
"filter", VIPS_FOREIGN_PNG_FILTER_NONE,
|
|
|
|
"interlace", interlace,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
2019-04-05 12:30:16 +02:00
|
|
|
return vips_pngsave_buffer(
|
|
|
|
in, buf, len,
|
|
|
|
"filter", VIPS_FOREIGN_PNG_FILTER_NONE,
|
|
|
|
"interlace", interlace,
|
2021-03-30 17:54:15 +02:00
|
|
|
"palette", quantize,
|
|
|
|
"bitdepth", bitdepth,
|
|
|
|
NULL
|
|
|
|
);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-01-12 16:43:09 +02:00
|
|
|
vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
|
|
|
|
return vips_webpsave_buffer(
|
|
|
|
in, buf, len,
|
|
|
|
"Q", quality,
|
|
|
|
NULL
|
|
|
|
);
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
|
2021-11-22 20:12:59 +02:00
|
|
|
#if VIPS_SUPPORT_GIFSAVE
|
|
|
|
return vips_gifsave_buffer(in, buf, len, NULL);
|
|
|
|
#else
|
|
|
|
vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.12+ reuired)");
|
|
|
|
return 1;
|
|
|
|
#endif
|
2019-02-09 14:06:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 13:49:52 +02:00
|
|
|
int
|
|
|
|
vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
|
|
|
|
return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
|
2019-06-03 18:07:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-16 12:15:01 +02:00
|
|
|
int
|
2021-04-26 14:27:16 +02:00
|
|
|
vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
|
2021-04-26 14:31:57 +02:00
|
|
|
return vips_heifsave_buffer(
|
|
|
|
in, buf, len,
|
|
|
|
"Q", quality,
|
|
|
|
"compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
|
2021-11-22 20:16:28 +02:00
|
|
|
#if VIPS_SUPPORT_AVIF_EFFORT
|
2022-02-07 12:33:09 +02:00
|
|
|
"effort", 9-speed,
|
2021-11-22 20:16:28 +02:00
|
|
|
#elif VIPS_SUPPORT_AVIF_SPEED
|
2021-04-26 14:31:57 +02:00
|
|
|
"speed", speed,
|
2021-04-26 14:27:16 +02:00
|
|
|
#endif
|
2021-04-26 14:31:57 +02:00
|
|
|
NULL);
|
2020-09-16 12:15:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 14:06:26 +02:00
|
|
|
void
|
|
|
|
vips_cleanup() {
|
|
|
|
vips_error_clear();
|
|
|
|
vips_thread_shutdown();
|
|
|
|
}
|