Browse Source

Better work with ICC profiles; Usage of vips 8.8 bbuilt-in cmyk profile

DarthSim 6 years ago
parent
commit
eb60f0567c
3 changed files with 51 additions and 15 deletions
  1. 32 7
      process.go
  2. 17 7
      vips.c
  3. 2 1
      vips.h

+ 32 - 7
process.go

@@ -873,19 +873,44 @@ func vipsSharpen(img **C.VipsImage, sigma float32) error {
 func vipsImportColourProfile(img **C.VipsImage, evenSRGB bool) error {
 func vipsImportColourProfile(img **C.VipsImage, evenSRGB bool) error {
 	var tmp *C.VipsImage
 	var tmp *C.VipsImage
 
 
-	if C.vips_need_icc_import(*img) > 0 && (evenSRGB || C.vips_icc_is_srgb_iec61966(*img) == 0) {
-		profile, err := cmykProfilePath()
-		if err != nil {
-			return err
+	if (*img).Coding != C.VIPS_CODING_NONE {
+		return nil
+	}
+
+	if (*img).BandFmt != C.VIPS_FORMAT_UCHAR && (*img).BandFmt != C.VIPS_FORMAT_USHORT {
+		return nil
+	}
+
+	profile := (*C.char)(nil)
+
+	if (*img).Type == C.VIPS_INTERPRETATION_sRGB {
+		// No embedded profile for sRGB, ignore
+		if C.vips_has_embedded_icc(*img) == 0 {
+			return nil
 		}
 		}
 
 
-		if C.vips_icc_import_go(*img, &tmp, cachedCString(profile)) == 0 {
-			C.swap_and_clear(img, tmp)
+		// Don't import sRGB IEC61966 2.1 unless evenSRGB
+		if !evenSRGB && C.vips_icc_is_srgb_iec61966(*img) != 0 {
+			return nil
+		}
+	} else if (*img).Type == C.VIPS_INTERPRETATION_CMYK && C.vips_has_embedded_icc(*img) == 0 {
+		if C.vips_support_builtin_icc() != 0 {
+			profile = cachedCString("cmyk")
 		} else {
 		} else {
-			logWarning("Can't import ICC profile: %s", vipsError())
+			p, err := cmykProfilePath()
+			if err != nil {
+				return err
+			}
+			profile = cachedCString(p)
 		}
 		}
 	}
 	}
 
 
+	if C.vips_icc_import_go(*img, &tmp, profile) == 0 {
+		C.swap_and_clear(img, tmp)
+	} else {
+		logWarning("Can't import ICC profile: %s", vipsError())
+	}
+
 	return nil
 	return nil
 }
 }
 
 

+ 17 - 7
vips.c

@@ -19,8 +19,17 @@
 #define VIPS_SUPPORT_PNG_QUANTIZATION \
 #define VIPS_SUPPORT_PNG_QUANTIZATION \
   (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
   (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
 
 
+#define VIPS_SUPPORT_BUILTIN_ICC \
+  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
+
 #define EXIF_ORIENTATION "exif-ifd0-Orientation"
 #define EXIF_ORIENTATION "exif-ifd0-Orientation"
 
 
+#if (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
+  #define VIPS_BLOB_DATA_TYPE const void *
+#else
+  #define VIPS_BLOB_DATA_TYPE void *
+#endif
+
 int
 int
 vips_initialize() {
 vips_initialize() {
   return vips_init("imgproxy");
   return vips_init("imgproxy");
@@ -218,7 +227,7 @@ vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
 
 
 int
 int
 vips_icc_is_srgb_iec61966(VipsImage *in) {
 vips_icc_is_srgb_iec61966(VipsImage *in) {
-  void *data;
+  VIPS_BLOB_DATA_TYPE data;
   size_t data_len;
   size_t data_len;
 
 
   // 1998-12-01
   // 1998-12-01
@@ -242,12 +251,13 @@ vips_icc_is_srgb_iec61966(VipsImage *in) {
 }
 }
 
 
 int
 int
-vips_need_icc_import(VipsImage *in) {
-  return (vips_image_get_typeof(in, VIPS_META_ICC_NAME) ||
-    in->Type == VIPS_INTERPRETATION_CMYK) &&
-		in->Coding == VIPS_CODING_NONE &&
-		(in->BandFmt == VIPS_FORMAT_UCHAR ||
-		 in->BandFmt == VIPS_FORMAT_USHORT);
+vips_has_embedded_icc(VipsImage *in) {
+  return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
+}
+
+int
+vips_support_builtin_icc() {
+  return VIPS_SUPPORT_BUILTIN_ICC;
 }
 }
 
 
 int
 int

+ 2 - 1
vips.h

@@ -48,7 +48,8 @@ int vips_resize_go(VipsImage *in, VipsImage **out, double scale);
 int vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale);
 int vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale);
 
 
 int vips_icc_is_srgb_iec61966(VipsImage *in);
 int vips_icc_is_srgb_iec61966(VipsImage *in);
-int vips_need_icc_import(VipsImage *in);
+int vips_has_embedded_icc(VipsImage *in);
+int vips_support_builtin_icc();
 int vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile);
 int vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile);
 int vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs);
 int vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs);