|
@@ -380,37 +380,50 @@ int
|
|
|
vips_trim(VipsImage *in, VipsImage **out, double threshold,
|
|
|
gboolean smart, double r, double g, double b,
|
|
|
gboolean equal_hor, gboolean equal_ver) {
|
|
|
- VipsImage *tmp;
|
|
|
|
|
|
- if (vips_image_hasalpha(in)) {
|
|
|
- if (vips_flatten_go(in, &tmp, 255.0, 0, 255.0))
|
|
|
+ VipsImage *base = vips_image_new();
|
|
|
+ VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
|
|
|
+
|
|
|
+ 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);
|
|
|
return 1;
|
|
|
- } else {
|
|
|
- if (vips_copy(in, &tmp, NULL))
|
|
|
+ }
|
|
|
+ tmp = t[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (vips_image_hasalpha(tmp)) {
|
|
|
+ if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
|
|
|
+ clear_image(&base);
|
|
|
return 1;
|
|
|
+ }
|
|
|
+ tmp = t[1];
|
|
|
}
|
|
|
|
|
|
- double *bg;
|
|
|
+ double *bg = NULL;
|
|
|
int bgn;
|
|
|
VipsArrayDouble *bga;
|
|
|
|
|
|
if (smart) {
|
|
|
if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
|
|
|
- clear_image(&tmp);
|
|
|
+ clear_image(&base);
|
|
|
return 1;
|
|
|
}
|
|
|
bga = vips_array_double_new(bg, bgn);
|
|
|
} else {
|
|
|
bga = vips_array_double_newv(3, r, g, b);
|
|
|
- bg = 0;
|
|
|
}
|
|
|
|
|
|
int left, right, top, bot, width, height, diff;
|
|
|
+ int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
|
|
|
|
|
|
- if (vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL)) {
|
|
|
- clear_image(&tmp);
|
|
|
- vips_area_unref((VipsArea *)bga);
|
|
|
- g_free(bg);
|
|
|
+ clear_image(&base);
|
|
|
+ vips_area_unref((VipsArea *)bga);
|
|
|
+ g_free(bg);
|
|
|
+
|
|
|
+ if (res) {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
@@ -436,10 +449,6 @@ vips_trim(VipsImage *in, VipsImage **out, double threshold,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- clear_image(&tmp);
|
|
|
- vips_area_unref((VipsArea *)bga);
|
|
|
- g_free(bg);
|
|
|
-
|
|
|
if (width == 0 || height == 0) {
|
|
|
return vips_copy(in, out, NULL);
|
|
|
}
|