Browse Source

Fix trimming of CMYK images

DarthSim 3 years ago
parent
commit
7a5074187e
4 changed files with 38 additions and 17 deletions
  1. 4 1
      CHANGELOG.md
  2. 4 0
      processing/import_color_profile.go
  3. 5 0
      processing/trim.go
  4. 25 16
      vips/vips.c

+ 4 - 1
CHANGELOG.md

@@ -1,9 +1,12 @@
 # Changelog
 
 ## [Unreleased]
-## Add
+### Add
 - Add support of 16-bit BMP.
 
+### Fix
+- Fix trimming of CMYK images.
+
 ## [3.6.0] - 2022-06-13
 ### Add
 - Add `IMGPROXY_RETURN_ATTACHMENT` config and [return_attachment](https://docs.imgproxy.net/generating_the_url?return-attachment) processing option.

+ 4 - 0
processing/import_color_profile.go

@@ -8,6 +8,10 @@ import (
 )
 
 func importColorProfile(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
+	if pctx.iccImported {
+		return nil
+	}
+
 	if err := img.Rad2Float(); err != nil {
 		return err
 	}

+ 5 - 0
processing/trim.go

@@ -11,6 +11,11 @@ func trim(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions,
 		return nil
 	}
 
+	// We need to import color profile before trim
+	if err := importColorProfile(pctx, img, po, imgdata); err != nil {
+		return err
+	}
+
 	if err := img.Trim(po.Trim.Threshold, po.Trim.Smart, po.Trim.Color, po.Trim.EqualHor, po.Trim.EqualVer); err != nil {
 		return err
 	}

+ 25 - 16
vips/vips.c

@@ -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);
   }