Blob Blame History Raw
From ccd21419317edb7709b95f95bb7cc742ba8dea32 Mon Sep 17 00:00:00 2001
From: Robin Watts <robin.watts@artifex.com>
Date: Tue, 31 Dec 2013 15:19:54 +0000
Subject: [PATCH] Import LCMS 2.6 Release version

Update gs calling code to cope, including simple mutex
handlers. Also incorporates our optimisations etc.
---
 base/gsicc_lcms2.c | 109 ++++++++++++++++++++++++++++++++++++++++++-----------
 base/lib.mak       |   3 +-
 2 files changed, 90 insertions(+), 22 deletions(-)

diff --git a/base/gsicc_lcms2.c b/base/gsicc_lcms2.c
index 9a013be..ff6097c 100644
--- a/base/gsicc_lcms2.c
+++ b/base/gsicc_lcms2.c
@@ -22,6 +22,12 @@
 #include "gslibctx.h"
 #include "gserrors.h"
 
+#define USE_LCMS2_LOCKING
+
+#ifdef USE_LCMS2_LOCKING
+#include "gxsync.h"
+#endif
+
 #define DUMP_CMS_BUFFER 0
 #define DEBUG_LCMS_MEM 0
 #define LCMS_BYTES_MASK 0x7
@@ -43,7 +49,7 @@ static
 void *gs_lcms2_malloc(cmsContext id, unsigned int size)
 {
     void *ptr;
-    gs_memory_t *mem = (gs_memory_t *)id;
+    gs_memory_t *mem = (gs_memory_t *)cmsGetContextUserData(id);
 
     ptr = gs_alloc_bytes(mem, size, "lcms");
 #if DEBUG_LCMS_MEM
@@ -55,7 +61,7 @@ void *gs_lcms2_malloc(cmsContext id, unsigned int size)
 static
 void gs_lcms2_free(cmsContext id, void *ptr)
 {
-    gs_memory_t *mem = (gs_memory_t *)id;
+    gs_memory_t *mem = (gs_memory_t *)cmsGetContextUserData(id);
     if (ptr != NULL) {
 #if DEBUG_LCMS_MEM
         gs_warn1("lcms free at 0x%x",ptr);
@@ -67,7 +73,7 @@ void gs_lcms2_free(cmsContext id, void *ptr)
 static
 void *gs_lcms2_realloc(cmsContext id, void *ptr, unsigned int size)
 {
-    gs_memory_t *mem = (gs_memory_t *)id;
+    gs_memory_t *mem = (gs_memory_t *)cmsGetContextUserData(id);
     void *ptr2;
 
     if (ptr == 0)
@@ -97,9 +103,53 @@ static cmsPluginMemHandler gs_cms_memhandler =
     gs_lcms2_realloc,
     NULL,
     NULL,
-    NULL
+    NULL,
 };
 
+#ifdef USE_LCMS2_LOCKING
+
+static
+void *gs_lcms2_createMutex(cmsContext id)
+{
+    gs_memory_t *mem = (gs_memory_t *)cmsGetContextUserData(id);
+
+    return gx_monitor_alloc(mem);
+}
+
+static
+void gs_lcms2_destroyMutex(cmsContext id, void* mtx)
+{
+    gx_monitor_free((gx_monitor_t *)mtx);
+}
+
+static
+cmsBool gs_lcms2_lockMutex(cmsContext id, void* mtx)
+{
+    return !gx_monitor_enter((gx_monitor_t *)mtx);
+}
+
+static
+void gs_lcms2_unlockMutex(cmsContext id, void* mtx)
+{
+    gx_monitor_leave((gx_monitor_t *)mtx);
+}
+
+static cmsPluginMutex gs_cms_mutexhandler =
+{
+    {
+        cmsPluginMagicNumber,
+        2060,
+        cmsPluginMutexSig,
+        NULL
+    },
+    gs_lcms2_createMutex,
+    gs_lcms2_destroyMutex,
+    gs_lcms2_lockMutex,
+    gs_lcms2_unlockMutex
+};
+
+#endif
+
 /* Get the number of channels for the profile.
   Input count */
 int
@@ -194,15 +244,19 @@ gcmmhprofile_t
 gscms_get_profile_handle_mem(gs_memory_t *mem, unsigned char *buffer, 
                              unsigned int input_size)
 {
-    cmsSetLogErrorHandler(gscms_error);
-    return(cmsOpenProfileFromMemTHR((cmsContext)mem,buffer,input_size));
+    cmsContext ctx = gs_lib_ctx_get_cms_context(mem);
+
+    cmsSetLogErrorHandlerTHR(ctx, gscms_error);
+    return(cmsOpenProfileFromMemTHR(ctx,buffer,input_size));
 }
 
 /* Get ICC Profile handle from file ptr */
 gcmmhprofile_t
 gscms_get_profile_handle_file(gs_memory_t *mem,const char *filename)
 {
-    return(cmsOpenProfileFromFileTHR((cmsContext)mem, filename, "r"));
+    cmsContext ctx = gs_lib_ctx_get_cms_context(mem);
+
+    return(cmsOpenProfileFromFileTHR(ctx, filename, "r"));
 }
 
 /* Transform an entire buffer */
@@ -347,6 +401,7 @@ gscms_get_link(gcmmhprofile_t  lcms_srchandle,
     int src_nChannels,des_nChannels;
     int lcms_src_color_space, lcms_des_color_space;
     unsigned int flag;
+    cmsContext ctx = gs_lib_ctx_get_cms_context(memory);
 
     /* Check for case of request for a transfrom from a device link profile
        in that case, the destination profile is NULL */
@@ -416,7 +471,7 @@ gscms_get_link(gcmmhprofile_t  lcms_srchandle,
         }
     }
     /* Create the link */
-    return cmsCreateTransformTHR((cmsContext)memory,
+    return cmsCreateTransformTHR(ctx,
                lcms_srchandle, src_data_type,
                lcms_deshandle, des_data_type,
                rendering_params->rendering_intent,flag);
@@ -443,6 +498,7 @@ gscms_get_link_proof_devlink(gcmmhprofile_t lcms_srchandle,
     cmsHPROFILE hProfiles[5]; 
     int nProfiles = 0;
     unsigned int flag;
+    cmsContext ctx = gs_lib_ctx_get_cms_context(memory);
 
     /* Check if the rendering intent is something other than relative colorimetric
        and  if we have a proofing profile.  In this case we need to create the 
@@ -505,7 +561,7 @@ gscms_get_link_proof_devlink(gcmmhprofile_t lcms_srchandle,
             flag = (flag | cmsFLAGS_BLACKPOINTCOMPENSATION);
         }
         /* Use relative colorimetric here */
-        temptransform = cmsCreateMultiprofileTransformTHR((cmsContext)memory,
+        temptransform = cmsCreateMultiprofileTransformTHR(ctx,
                     hProfiles, nProfiles, src_data_type,
                     des_data_type, gsRELATIVECOLORIMETRIC, flag);
         cmsCloseProfile(src_to_proof);
@@ -558,7 +614,7 @@ gscms_get_link_proof_devlink(gcmmhprofile_t lcms_srchandle,
             || rendering_params->black_point_comp == gsBLACKPTCOMP_ON_OR) {
             flag = (flag | cmsFLAGS_BLACKPOINTCOMPENSATION);
         }
-        return cmsCreateMultiprofileTransformTHR((cmsContext)memory,
+        return cmsCreateMultiprofileTransformTHR(ctx,
                     hProfiles, nProfiles, src_data_type,
                     des_data_type, rendering_params->rendering_intent, flag);
     }
@@ -568,15 +624,20 @@ gscms_get_link_proof_devlink(gcmmhprofile_t lcms_srchandle,
 int
 gscms_create(gs_memory_t *memory)
 {
+    cmsContext ctx;
+
     /* Set our own error handling function */
-    cmsSetLogErrorHandler(gscms_error);
-    cmsPluginTHR(memory, &gs_cms_memhandler);
-    /* If we had created any persitent state that we needed access to in the
-     * other functions, we should store that by calling:
-     *   gs_lib_ctx_set_cms_context(memory, state);
-     * We can then retrieve it anywhere else by calling:
-     *   gs_lib_ctx_get_cms_context(memory);
-     * LCMS currently uses no such state. */
+    ctx = cmsCreateContext((void *)&gs_cms_memhandler, memory);
+    if (ctx == NULL)
+        return gs_error_VMerror;
+
+#ifdef USE_LCMS2_LOCKING
+    cmsPluginTHR(ctx, (void *)&gs_cms_mutexhandler);
+#endif
+
+    cmsSetLogErrorHandlerTHR(ctx, gscms_error);
+    gs_lib_ctx_set_cms_context(memory, ctx);
+
     return 0;
 }
 
@@ -584,7 +645,12 @@ gscms_create(gs_memory_t *memory)
 void
 gscms_destroy(gs_memory_t *memory)
 {
-    /* Nothing to do here for lcms */
+    cmsContext ctx = gs_lib_ctx_get_cms_context(memory);
+    if (ctx == NULL)
+        return;
+
+    cmsDeleteContext(ctx);
+    gs_lib_ctx_set_cms_context(memory, NULL);
 }
 
 /* Have the CMS release the link */
@@ -662,6 +728,7 @@ gscms_get_name2device_link(gsicc_link_t *icclink,
     cmsUInt32Number dwOutputFormat;
     cmsUInt32Number lcms_proof_flag;
     int number_colors;
+    cmsContext ctx = gs_lib_ctx_get_cms_context(memory);
 
     /* NOTE:  We need to add a test here to check that we even HAVE
     device values in here and NOT just CIELAB values */
@@ -672,7 +739,7 @@ gscms_get_name2device_link(gsicc_link_t *icclink,
     }
     /* Create the transform */
     /* ToDo:  Adjust rendering intent */
-    hTransform = cmsCreateProofingTransformTHR(memory,
+    hTransform = cmsCreateProofingTransformTHR(ctx,
                                             lcms_srchandle, TYPE_NAMED_COLOR_INDEX,
                                             lcms_deshandle, TYPE_CMYK_8,
                                             lcms_proofhandle,INTENT_PERCEPTUAL,
@@ -692,4 +759,4 @@ gscms_get_name2device_link(gsicc_link_t *icclink,
     if(lcms_deshandle) cmsCloseProfile(lcms_deshandle);
     if(lcms_proofhandle) cmsCloseProfile(lcms_proofhandle);
     return;
-}
\ No newline at end of file
+}
diff --git a/base/lib.mak b/base/lib.mak
index e9cb290..10d0d0d 100644
--- a/base/lib.mak
+++ b/base/lib.mak
@@ -2862,7 +2862,8 @@ $(GLOBJ)gsicc_lcms2_0.$(OBJ) : $(GLSRC)gsicc_lcms2.c\
  $(gsicc_cms_h) $(lcms2_h) $(gslibctx_h) $(lcms2_plugin_h) $(gserrors_h)
 	$(GLLCMS2CC) $(GLO_)gsicc_lcms2_0.$(OBJ) $(C_) $(GLSRC)gsicc_lcms2.c
 
-$(GLOBJ)gsicc_lcms2.$(OBJ) : $(GLOBJ)gsicc_lcms2_$(SHARE_LCMS).$(OBJ)
+$(GLOBJ)gsicc_lcms2.$(OBJ) : $(GLOBJ)gsicc_lcms2_$(SHARE_LCMS).$(OBJ) $(gp_h) \
+ $(gxsync_h)
 	$(CP_) $(GLOBJ)gsicc_lcms2_$(SHARE_LCMS).$(OBJ) $(GLOBJ)gsicc_lcms2.$(OBJ)
 
 # Note that gsicc_create requires compile with lcms to obtain icc34.h
-- 
2.5.5