Blame SOURCES/ghostscript-import-lcms2-2.6-changes.patch

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