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

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