From 086774b0650be8f9cbeca3efcd026515e71458da Mon Sep 17 00:00:00 2001
From: Yavor Doganov <yavor@gnu.org>
Date: Sat, 24 Feb 2018 08:49:21 +0200
Subject: [PATCH] Switch to lcms2.

---
 ChangeLog                                  | 27 +++++++++++++
 README                                     |  2 +-
 Source/GNUmakefile                         |  2 +-
 Source/OpalGraphics/GNUmakefile            |  2 +-
 Source/OpalGraphics/OPColorSpaceLCMS.h     |  2 +-
 Source/OpalGraphics/OPColorSpaceLCMS.m     | 63 ++++++++++++++++++------------
 Source/OpalGraphics/OPColorTransformLCMS.h |  2 +-
 Source/OpalGraphics/OPColorTransformLCMS.m |  8 ++--
 Source/OpalGraphics/OPImageConversion.m    |  1 -
 Source/OpalText/GNUmakefile                |  1 -
 10 files changed, 74 insertions(+), 36 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bd5a46f..f157acc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,30 @@
+2018-02-24  Yavor Doganov  <yavor@gnu.org>
+
+	* Source/OpalGraphics/OPColorSpaceLCMS.m: Don't include lcms
+	header, already included from OPColorSpaceLCMS.h.
+	(name): Get profile with cmsGetProfileInfoASCII.
+	(initWithCalibratedGrayWithWhitePoint:blackPoint:gamma:):
+	(initWithCalibratedRGBWithWhitePointCIExy:redCIExy:greenCIExy:blueCIExy:gamma:):
+	(initWithCalibratedRGBWithWhitePoint:blackPoint:gamma:matrix:):
+	Replace LPGAMMMATABLE with cmsToneCurve*.
+	(initWithICCProfile:): Replce LPVOID with const void*.
+	(CGColorSpaceModelForSignature): Change function parameter type to
+	cmsColorSpaceSignature.  Update switch statement.
+	(numberOfComponents): Replace _cmsChannelsOf with cmsChannelsOf.
+	* Source/OpalGraphics/OPColorTransformLCMS.m: Don't include lcms
+	header, already included from OPColorTransformLCMS.h.
+	(LcmsFormatForOPImageFormat): Replace DWORD with cmsUInt32Number,
+	including the return type.
+	* Source/OpalGraphics/OPImageConversion.m: Don't include lcms
+	header, unnecessary.
+	* Source/OpalGraphics/OPColorSpaceLCMS.h: Include <lcms2.h>.
+	* Source/OpalGraphics/OPColorTransformLCMS.h: Likewise.
+	* Source/GNUmakefile (LIBRARIES_DEPEND_UPON): Detect lcms2.
+	* Source/OpalGraphics/GNUmakefile (ADDITIONAL_CPPFLAGS): Likewise.
+	* Source/OpalText/GNUmakefile (ADDITIONAL_CPPFLAGS): Remove lcms
+	detection, not necessary.
+	* README: Require lcms version 2.
+
 2017-08-04  Daniel Ferreira <dtf@stanford.edu>
 
 	* Headers/ImageIO/ImageIO.h
diff --git a/README b/README
index 9bc75d2..6a83aa8 100644
--- a/README
+++ b/README
@@ -39,7 +39,7 @@ Requirements
 - libjpeg
 - libpng
 - libtiff
-- lcms (version 1)
+- lcms (version 2)
 
 and on non-Windows platforms:
 - freetype
diff --git a/Source/GNUmakefile b/Source/GNUmakefile
index 0769b1d..4251dd9 100644
--- a/Source/GNUmakefile
+++ b/Source/GNUmakefile
@@ -28,7 +28,7 @@ else
 LIBRARIES_DEPEND_UPON += -lgdi32 -lcairo
 endif
 
-LIBRARIES_DEPEND_UPON += $(shell pkg-config --libs lcms)
+LIBRARIES_DEPEND_UPON += $(shell pkg-config --libs lcms2)
 LIBRARIES_DEPEND_UPON += $(shell pkg-config --libs libpng)
 LIBRARIES_DEPEND_UPON += -ljpeg -ltiff
 
diff --git a/Source/OpalGraphics/GNUmakefile b/Source/OpalGraphics/GNUmakefile
index d65bf3a..8aced15 100644
--- a/Source/OpalGraphics/GNUmakefile
+++ b/Source/OpalGraphics/GNUmakefile
@@ -49,7 +49,7 @@ $(SUBPROJECT_NAME)_HEADER_FILES = \
 
 ADDITIONAL_OBJCFLAGS += -Wall -g -O0 -std=gnu99 -DINTERNAL_BUILD_OBJC
 ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags cairo)
-ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags lcms)
+ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags lcms2)
 
 ifneq ($(GNUSTEP_TARGET_OS), mingw32)
 ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags freetype2)
diff --git a/Source/OpalGraphics/OPColorSpaceLCMS.h b/Source/OpalGraphics/OPColorSpaceLCMS.h
index f8f5e83..4870638 100644
--- a/Source/OpalGraphics/OPColorSpaceLCMS.h
+++ b/Source/OpalGraphics/OPColorSpaceLCMS.h
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
 
-#include <lcms.h>
+#include <lcms2.h>
 
 #include "CoreGraphics/CGColorSpace.h"
 
diff --git a/Source/OpalGraphics/OPColorSpaceLCMS.m b/Source/OpalGraphics/OPColorSpaceLCMS.m
index 181acd8..06eb137 100644
--- a/Source/OpalGraphics/OPColorSpaceLCMS.m
+++ b/Source/OpalGraphics/OPColorSpaceLCMS.m
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
 
-#include <lcms.h>
-
 #include "CoreGraphics/CGColorSpace.h"
 
 #import "OPColorSpaceLCMS.h"
@@ -164,7 +162,24 @@ static OPColorSpaceLCMS *colorSpaceGenericGrayGamma2_2;
 
 - (NSString*)name
 {
-  return [NSString stringWithUTF8String: cmsTakeProductName(self->profile)];
+  cmsUInt32Number size;
+  char *buf = NULL;
+  NSString *name = nil;
+
+  size = cmsGetProfileInfoASCII(self->profile, cmsInfoModel,
+                                "en", "US", NULL, 0);
+  if (size > 0)
+    {
+      buf = (char *)malloc(size + 1);
+      size = cmsGetProfileInfoASCII(self->profile, cmsInfoModel,
+                                    "en", "US", buf, size);
+      if (size > 0)
+        name = [NSString stringWithUTF8String: buf];
+
+      free(buf);
+    }
+
+  return name;
 }
 
 static inline cmsCIExyY CIExyzToCIExyY(const CGFloat point[3])
@@ -190,10 +205,10 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 
   // NOTE: we ignore the black point; LCMS computes it on its own
 
-  LPGAMMATABLE table = cmsBuildGamma(256, gamma);
+  cmsToneCurve *table = cmsBuildGamma(NULL, gamma);
   cmsCIExyY whiteCIExyY = CIEXYZToCIExyY(whiteCIEXYZ);
   self->profile = cmsCreateGrayProfile(&whiteCIExyY, table);
-  cmsFreeGamma(table);
+  cmsFreeToneCurve(table);
 
   return self;
 }
@@ -205,9 +220,9 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 {
   self = [super init];
 
-  LPGAMMATABLE tables[3] = {cmsBuildGamma(256, gamma),
-                            cmsBuildGamma(256, gamma),
-                            cmsBuildGamma(256, gamma)};
+  cmsToneCurve *tables[3] = {cmsBuildGamma(NULL, gamma),
+                             cmsBuildGamma(NULL, gamma),
+                             cmsBuildGamma(NULL, gamma)};
   cmsCIExyY whitePoint = {white[0], white[1], 1.0};
   cmsCIExyYTRIPLE primaries = {
     {red[0], red[1], 1.0},
@@ -217,9 +232,9 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 
   self->profile = cmsCreateRGBProfile(&whitePoint, &primaries, tables);
 
-  cmsFreeGamma(tables[0]);
-  cmsFreeGamma(tables[1]);
-  cmsFreeGamma(tables[2]);
+  cmsFreeToneCurve(tables[0]);
+  cmsFreeToneCurve(tables[1]);
+  cmsFreeToneCurve(tables[2]);
   
   return self;
 }
@@ -232,9 +247,9 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 
   // NOTE: we ignore the black point; LCMS computes it on its own
 
-  LPGAMMATABLE tables[3] = {cmsBuildGamma(256, gamma[0]),
-                            cmsBuildGamma(256, gamma[1]),
-                            cmsBuildGamma(256, gamma[2])};
+  cmsToneCurve *tables[3] = {cmsBuildGamma(NULL, gamma[0]),
+                             cmsBuildGamma(NULL, gamma[1]),
+                             cmsBuildGamma(NULL, gamma[2])};
 
   // FIXME: I'm not 100% sure this is the correct interpretation of matrix
 
@@ -250,9 +265,9 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 
   self->profile = cmsCreateRGBProfile(&whitePointCIExyY, &primaries, tables);
 
-  cmsFreeGamma(tables[0]);
-  cmsFreeGamma(tables[1]);
-  cmsFreeGamma(tables[2]);
+  cmsFreeToneCurve(tables[0]);
+  cmsFreeToneCurve(tables[1]);
+  cmsFreeToneCurve(tables[2]);
   
   return self;
 }
@@ -274,7 +289,7 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 {
   self = [super init];
   self->data = [profileData retain];
-	self->profile = cmsOpenProfileFromMem((LPVOID)[profileData bytes], [profileData length]);
+	self->profile = cmsOpenProfileFromMem((const void*)[profileData bytes], [profileData length]);
   return self;
 }
 
@@ -284,17 +299,17 @@ static inline cmsCIExyY CIEXYZToCIExyY(const CGFloat point[3])
 	return nil;
 }
 
-static CGColorSpaceModel CGColorSpaceModelForSignature(icColorSpaceSignature sig)
+static CGColorSpaceModel CGColorSpaceModelForSignature(cmsColorSpaceSignature sig)
 {
   switch (sig)
   {
-    case icSigGrayData:
+    case cmsSigGrayData:
       return kCGColorSpaceModelMonochrome;
-    case icSigRgbData:
+    case cmsSigRgbData:
       return kCGColorSpaceModelRGB;
-    case icSigCmykData:
+    case cmsSigCmykData:
       return kCGColorSpaceModelCMYK;
-    case icSigLabData:
+    case cmsSigLabData:
       return kCGColorSpaceModelLab;
     default:
       return kCGColorSpaceModelUnknown;
@@ -308,7 +323,7 @@ static CGColorSpaceModel CGColorSpaceModelForSignature(icColorSpaceSignature sig
 }
 - (size_t) numberOfComponents
 {
-  return _cmsChannelsOf(cmsGetColorSpace(profile));
+  return cmsChannelsOf(cmsGetColorSpace(profile));
 }
 
 - (id<OPColorTransform>) colorTransformTo: (id<CGColorSpace>)aColorSpace
diff --git a/Source/OpalGraphics/OPColorTransformLCMS.h b/Source/OpalGraphics/OPColorTransformLCMS.h
index b8cf5e0..a9abd7d 100644
--- a/Source/OpalGraphics/OPColorTransformLCMS.h
+++ b/Source/OpalGraphics/OPColorTransformLCMS.h
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
 
-#include <lcms.h>
+#include <lcms2.h>
 
 #include "CoreGraphics/CGColorSpace.h"
 
diff --git a/Source/OpalGraphics/OPColorTransformLCMS.m b/Source/OpalGraphics/OPColorTransformLCMS.m
index 18ace4c..f55e936 100644
--- a/Source/OpalGraphics/OPColorTransformLCMS.m
+++ b/Source/OpalGraphics/OPColorTransformLCMS.m
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    */
 
-#include <lcms.h>
-
 #include "CoreGraphics/CGColorSpace.h"
 
 #import "CGColorSpace-private.h"
@@ -72,9 +70,9 @@ static int LcmsPixelTypeForCGColorSpaceModel(CGColorSpaceModel model)
   }
 }
  
-static DWORD LcmsFormatForOPImageFormat(OPImageFormat opalFormat, CGColorSpaceRef colorSpace)
+static cmsUInt32Number LcmsFormatForOPImageFormat(OPImageFormat opalFormat, CGColorSpaceRef colorSpace)
 {    
-  DWORD cmsFormat = 0;
+  cmsUInt32Number cmsFormat = 0;
   
   switch (opalFormat.compFormat)
   {
@@ -92,7 +90,7 @@ static DWORD LcmsFormatForOPImageFormat(OPImageFormat opalFormat, CGColorSpaceRe
 			break;
   }
   
-  cmsFormat |= CHANNELS_SH((DWORD)opalFormat.colorComponents);
+  cmsFormat |= CHANNELS_SH((cmsUInt32Number)opalFormat.colorComponents);
  
   if (opalFormat.hasAlpha)
   {
diff --git a/Source/OpalGraphics/OPImageConversion.m b/Source/OpalGraphics/OPImageConversion.m
index d804fc9..5aacc66 100644
--- a/Source/OpalGraphics/OPImageConversion.m
+++ b/Source/OpalGraphics/OPImageConversion.m
@@ -23,7 +23,6 @@
    */
 
 
-#include <lcms.h>
 #import <Foundation/NSString.h>
 
 #include "CoreGraphics/CGColorSpace.h"
diff --git a/Source/OpalText/GNUmakefile b/Source/OpalText/GNUmakefile
index dc26ef6..6c1721a 100644
--- a/Source/OpalText/GNUmakefile
+++ b/Source/OpalText/GNUmakefile
@@ -34,7 +34,6 @@ $(SUBPROJECT_NAME)_HEADER_FILES = \
 
 ADDITIONAL_OBJCFLAGS += -Wall -g -O0 -std=gnu99 -DINTERNAL_BUILD_OBJC
 ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags cairo)
-ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags lcms)
 
 ifneq ($(GNUSTEP_TARGET_OS), mingw32)
 ADDITIONAL_CPPFLAGS += $(shell pkg-config --cflags freetype2)
-- 
2.16.1

