Update of /cvsroot/ufraw/ufraw
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv4408

Modified Files:
        configure.ac dcraw.cc ufraw_developer.c ufraw_writer.c 
Log Message:
Use lcms 2.x if found. Based on patch by Nils Philippsen.

Index: dcraw.cc
===================================================================
RCS file: /cvsroot/ufraw/ufraw/dcraw.cc,v
retrieving revision 1.298
retrieving revision 1.299
diff -u -d -r1.298 -r1.299
--- dcraw.cc    28 Sep 2013 02:15:16 -0000      1.298
+++ dcraw.cc    5 Oct 2013 16:00:10 -0000       1.299
@@ -85,7 +85,11 @@
 }
 #endif                         /* and Adobe Lossy DNGs */
 #ifndef NO_LCMS
-#include <lcms.h>              /* Support color profiles */
+#ifdef HAVE_LCMS2
+#include <lcms2.h>             /* Support color profiles */
+#else
+#include <lcms.h>
+#endif
 #endif
 #ifndef DCRAW_NOMAIN
 #ifdef LOCALEDIR
@@ -8759,6 +8763,35 @@
 }
 
 #ifndef NO_LCMS
+#ifdef DCRAW_NOMAIN
+extern "C" { char *ufraw_message(int code, const char *format, ...); }
+#define UFRAW_ERROR 100
+#endif
+#ifdef HAVE_LCMS2
+static void dcraw_lcms_message (cmsContext ContextID,
+                                cmsUInt32Number ErrorCode,
+                                const char *ErrorText)
+{
+    (void) ContextID;
+#else
+static int dcraw_lcms_message (int ErrorCode, const char *ErrorText)
+{
+#endif
+    /* Possible ErrorCode:
+     * see cmsERROR_* in <lcms2.h> or LCMS_ERRC_* in <lcms.h>. */
+    (void) ErrorCode;
+
+#ifdef DCRAW_NOMAIN
+    ufraw_message (UFRAW_ERROR, "%s", ErrorText);
+#else
+    fprintf (stderr, "%s", ErrorText);
+#endif
+
+#ifdef HAVE_LCMS1
+    return 1; /* Tell lcms that we handled the error */
+#endif
+}
+
 void CLASS apply_profile (const char *input, const char *output)
 {
   char *prof;
@@ -8767,7 +8800,11 @@
   FILE *fp;
   unsigned size;
 
-  cmsErrorAction (LCMS_ERROR_SHOW);
+#ifdef HAVE_LCMS2
+  cmsSetLogErrorHandler (dcraw_lcms_message);
+#else
+  cmsSetErrorHandler (dcraw_lcms_message);
+#endif
   if (strcmp (input, "embed"))
     hInProfile = cmsOpenProfileFromFile (input, "r");
   else if (profile_length) {
@@ -8806,7 +8843,7 @@
 quit:
   cmsCloseProfile (hInProfile);
 }
-#endif
+#endif /* !NO_LCMS */
 
 void CLASS convert_to_rgb()
 {

Index: ufraw_writer.c
===================================================================
RCS file: /cvsroot/ufraw/ufraw/ufraw_writer.c,v
retrieving revision 1.90
retrieving revision 1.91
diff -u -d -r1.90 -r1.91
--- ufraw_writer.c      19 Apr 2013 03:00:09 -0000      1.90
+++ ufraw_writer.c      5 Oct 2013 16:00:10 -0000       1.91
@@ -14,7 +14,23 @@
 #include <glib/gi18n.h>
 #include <errno.h>     /* for errno */
 #include <string.h>
+#ifdef HAVE_LCMS2
+#include <lcms2.h>
+#else
 #include <lcms.h>
+typedef DWORD cmsUInt32Number;
+static LCMSBOOL cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr,
+                                    cmsUInt32Number *BytesNeeded)
+{
+    size_t _BytesNeeded;
+    LCMSBOOL retval;
+
+    retval = _cmsSaveProfileToMem(hProfile, MemPtr, &_BytesNeeded);
+    if (BytesNeeded)
+        *(BytesNeeded) = (cmsUInt32Number) _BytesNeeded;
+    return retval;
+}
+#endif
 #ifdef HAVE_LIBTIFF
 #include <tiffio.h>
 #endif
@@ -405,11 +421,11 @@
             }
         } else if (uf->conf->profileIndex[out_profile] == 1) { // Embed sRGB.
             cmsHPROFILE hOutProfile = cmsCreate_sRGBProfile();
-            gsize len = 0;
-            _cmsSaveProfileToMem(hOutProfile, 0, &len); // Calculate len.
+            cmsUInt32Number len = 0;
+            cmsSaveProfileToMem(hOutProfile, 0, &len); // Calculate len.
             if (len > 0) {
                 unsigned char buf[len];
-                _cmsSaveProfileToMem(hOutProfile, buf, &len);
+                cmsSaveProfileToMem(hOutProfile, buf, &len);
                 TIFFSetField(out, TIFFTAG_ICCPROFILE, len, buf);
             } else {
                 ufraw_set_warning(uf,
@@ -478,11 +494,11 @@
             }
         } else if (uf->conf->profileIndex[out_profile] == 1) { // Embed sRGB.
             cmsHPROFILE hOutProfile = cmsCreate_sRGBProfile();
-            gsize len = 0;
-            _cmsSaveProfileToMem(hOutProfile, 0, &len); // Calculate len.
+            cmsUInt32Number len = 0;
+            cmsSaveProfileToMem(hOutProfile, 0, &len); // Calculate len.
             if (len > 0) {
                 unsigned char buf[len];
-                _cmsSaveProfileToMem(hOutProfile, buf, &len);
+                cmsSaveProfileToMem(hOutProfile, buf, &len);
                 write_icc_profile(&cinfo, buf, len);
             } else {
                 ufraw_set_warning(uf,
@@ -570,11 +586,11 @@
                 }
             } else if (uf->conf->profileIndex[out_profile] == 1) { // Embed 
sRGB.
                 cmsHPROFILE hOutProfile = cmsCreate_sRGBProfile();
-                gsize len = 0;
-                _cmsSaveProfileToMem(hOutProfile, 0, &len); // Calculate len.
+                cmsUInt32Number len = 0;
+                cmsSaveProfileToMem(hOutProfile, 0, &len); // Calculate len.
                 if (len > 0) {
                     char buf[len];
-                    _cmsSaveProfileToMem(hOutProfile, buf, &len);
+                    cmsSaveProfileToMem(hOutProfile, buf, &len);
                     png_set_iCCP(png, info,
                                  uf->conf->profile[out_profile]
                                  [uf->conf->profileIndex[out_profile]].name,

Index: ufraw_developer.c
===================================================================
RCS file: /cvsroot/ufraw/ufraw/ufraw_developer.c,v
retrieving revision 1.89
retrieving revision 1.90
diff -u -d -r1.89 -r1.90
--- ufraw_developer.c   1 Jan 2013 04:00:17 -0000       1.89
+++ ufraw_developer.c   5 Oct 2013 16:00:10 -0000       1.90
@@ -15,17 +15,34 @@
 #include <omp.h>
 #endif
 #include <math.h>
+#include <string.h>
+#ifdef HAVE_LCMS2
+#include <lcms2.h>
+#include <lcms2_plugin.h>
+#else
 #include <lcms.h>
+#define cmsCreateLab2Profile(x) cmsCreateLabProfile(x)
+typedef GAMMATABLE cmsToneCurve;
+typedef WORD cmsUInt16Number;
+#endif
 
+#ifdef HAVE_LCMS2
+static void lcms_message(cmsContext ContextID,
+                         cmsUInt32Number ErrorCode,
+                         const char *ErrorText)
+{
+    (void) ContextID;
+#else
 static int lcms_message(int ErrorCode, const char *ErrorText)
 {
+#endif
     /* Possible ErrorCode:
-     * LCMS_ERRC_WARNING        0x1000
-     * LCMS_ERRC_RECOVERABLE    0x2000
-     * LCMS_ERRC_ABORTED        0x3000 */
-    (void)ErrorCode;
+     * see cmsERROR_* in <lcms2.h> or LCMS_ERRC_* in <lcms.h>. */
+    (void) ErrorCode;
     ufraw_message(UFRAW_ERROR, "%s", ErrorText);
+#ifdef HAVE_LCMS1
     return 1; /* Tell lcms that we handled the error */
+#endif
 }
 
 developer_data *developer_init()
@@ -48,9 +65,14 @@
     memset(&d->luminosityCurveData, 0, sizeof(d->luminosityCurveData));
     d->luminosityCurveData.m_gamma = -1.0;
     d->luminosityProfile = NULL;
-    LPGAMMATABLE *TransferFunction = (LPGAMMATABLE *)d->TransferFunction;
+    cmsToneCurve **TransferFunction = (cmsToneCurve **)d->TransferFunction;
+#ifdef HAVE_LCMS2
+    TransferFunction[0] = cmsBuildGamma(NULL, 1.0);
+    TransferFunction[1] = TransferFunction[2] = cmsBuildGamma(NULL, 1.0);
+#else
     TransferFunction[0] = cmsAllocGamma(0x100);
     TransferFunction[1] = TransferFunction[2] = cmsBuildGamma(0x100, 1.0);
+#endif
     d->saturationProfile = NULL;
     d->adjustmentProfile = NULL;
     d->intent[out_profile] = -1;
@@ -66,7 +88,11 @@
         d->lightnessAdjustment[i].hue = 0.0;
         d->lightnessAdjustment[i].hueWidth = 0.0;
     }
+#ifdef HAVE_LCMS2
+    cmsSetLogErrorHandler(lcms_message);
+#else
     cmsSetErrorHandler(lcms_message);
+#endif
     return d;
 }
 
@@ -77,8 +103,13 @@
     for (i = 0; i < profile_types; i++)
         if (d->profile[i] != NULL) cmsCloseProfile(d->profile[i]);
     cmsCloseProfile(d->luminosityProfile);
+#ifdef HAVE_LCMS2
+    cmsFreeToneCurve(d->TransferFunction[0]);
+    cmsFreeToneCurve(d->TransferFunction[1]);
+#else
     cmsFreeGamma(d->TransferFunction[0]);
     cmsFreeGamma(d->TransferFunction[1]);
+#endif
     cmsCloseProfile(d->saturationProfile);
     cmsCloseProfile(d->adjustmentProfile);
     if (d->colorTransform != NULL)
@@ -92,6 +123,40 @@
 
 static const char *embedded_display_profile = "embedded display profile";
 
+#ifdef HAVE_LCMS2
+/*
+ * Emulates cmsTakeProductName() from lcms 1.x.
+ *
+ * This is tailored for use with statically allocated strings and not
+ * thread-safe.
+ */
+const char *cmsTakeProductName(cmsHPROFILE profile)
+{
+    static char name[max_name * 2 + 4];
+    char manufacturer[max_name], model[max_name];
+
+    name[0] = manufacturer[0] = model[0] = '\0';
+
+    cmsGetProfileInfoASCII(profile, cmsInfoManufacturer,
+                           "en", "US", manufacturer, max_name);
+    cmsGetProfileInfoASCII(profile, cmsInfoModel,
+                           "en", "US", model, max_name);
+
+    if (!manufacturer[0] && !model[0]) {
+        cmsGetProfileInfoASCII(profile, cmsInfoDescription,
+                               "en", "US", name, max_name * 2 + 4);
+    } else {
+        if (!manufacturer[0] || (strncmp(model, manufacturer, 8) == 0) ||
+                strlen(model) > 30)
+            strcpy(name, model);
+        else
+            sprintf(name, "%s - %s", model, manufacturer);
+    }
+
+    return name;
+}
+#endif
+
 /* Update the profile in the developer
  * and init values in the profile if needed */
 void developer_profile(developer_data *d, int type, profile_data *p)
@@ -185,7 +250,14 @@
 static const double max_luminance = 100.0;
 static const double max_colorfulness = 181.019336; /* sqrt(128*128+128*128) */
 
-static int contrast_saturation_sampler(WORD In[], WORD Out[], LPVOID Cargo)
+#ifdef HAVE_LCMS2
+static cmsInt32Number contrast_saturation_sampler(const cmsUInt16Number In[],
+        cmsUInt16Number Out[],
+        void *Cargo)
+#else
+static int contrast_saturation_sampler(cmsUInt16Number In[],
+                                       cmsUInt16Number Out[], void *Cargo)
+#endif
 {
     cmsCIELab Lab;
     cmsCIELCh LCh;
@@ -207,9 +279,56 @@
         double saturation)
 {
     cmsHPROFILE hICC;
-    LPLUT Lut;
     struct contrast_saturation cs = { contrast, saturation };
 
+#ifdef HAVE_LCMS2
+    cmsPipeline* Pipeline = NULL;
+    cmsStage* CLUT = NULL;
+    cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
+    int i;
+
+    hICC = cmsCreateProfilePlaceholder(NULL);
+    if (hICC == NULL) return NULL; // can't allocate
+
+    cmsSetDeviceClass(hICC, cmsSigAbstractClass);
+    cmsSetColorSpace(hICC, cmsSigLabData);
+    cmsSetPCS(hICC, cmsSigLabData);
+    cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+    // Creates a pipeline with 3D grid only
+    Pipeline = cmsPipelineAlloc(NULL, 3, 3);
+    if (!Pipeline) goto error_out;
+
+    for (i = 0; i < MAX_INPUT_DIMENSIONS; i++) Dimensions[i] = 11;
+    CLUT = cmsStageAllocCLut16bitGranular(NULL, Dimensions, 3, 3, NULL);
+    if (!CLUT ||
+            !cmsStageSampleCLut16bit(CLUT, contrast_saturation_sampler, &cs, 
0))
+        goto error_out;
+
+#if LCMS_VERSION >= 2050
+    if (!cmsPipelineInsertStage(Pipeline, cmsAT_END, CLUT))
+        goto error_out;
+#else
+    cmsPipelineInsertStage(Pipeline, cmsAT_END, CLUT);
+#endif
+
+    // Create tags
+    cmsWriteTag(hICC, cmsSigMediaWhitePointTag, (void *) cmsD50_XYZ());
+    cmsWriteTag(hICC, cmsSigAToB0Tag, (void *) Pipeline);
+
+    // Pipeline is already on virtual profile
+    cmsPipelineFree(Pipeline);
+
+    return hICC;
+
+error_out:
+    if (CLUT) cmsStageFree(CLUT);
+    if (Pipeline) cmsPipelineFree(Pipeline);
+    if (hICC) cmsCloseProfile(hICC);
+    return NULL;
+#else /* HAVE_LCMS1 */
+    LPLUT Lut;
+
     hICC = _cmsCreateProfilePlaceholder();
     if (hICC == NULL) return NULL; // can't allocate
 
@@ -228,14 +347,23 @@
         return NULL;
     }
     // Create tags
-    cmsAddTag(hICC, icSigMediaWhitePointTag, (LPVOID) cmsD50_XYZ());
-    cmsAddTag(hICC, icSigAToB0Tag, (LPVOID) Lut);
+    cmsAddTag(hICC, icSigMediaWhitePointTag, (void *) cmsD50_XYZ());
+    cmsAddTag(hICC, icSigAToB0Tag, (void *) Lut);
     // LUT is already on virtual profile
     cmsFreeLUT(Lut);
+
     return hICC;
+#endif /* HAVE_LCMS1 */
 }
 
-static int luminance_adjustment_sampler(WORD In[], WORD Out[], LPVOID Cargo)
+#ifdef HAVE_LCMS2
+static cmsInt32Number luminance_adjustment_sampler(const cmsUInt16Number In[],
+        cmsUInt16Number Out[],
+        void *Cargo)
+#else
+static int luminance_adjustment_sampler(cmsUInt16Number In[],
+                                        cmsUInt16Number Out[], void *Cargo)
+#endif
 {
     cmsCIELab Lab;
     cmsCIELCh LCh;
@@ -275,6 +403,55 @@
 static cmsHPROFILE create_adjustment_profile(const developer_data *d)
 {
     cmsHPROFILE hICC;
+
+#ifdef HAVE_LCMS2
+    cmsPipeline* Pipeline = NULL;
+    cmsStage* CLUT = NULL;
+    cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
+    int i;
+
+    hICC = cmsCreateProfilePlaceholder(NULL);
+    if (hICC == NULL) return NULL; // can't allocate
+
+    cmsSetDeviceClass(hICC, cmsSigAbstractClass);
+    cmsSetColorSpace(hICC, cmsSigLabData);
+    cmsSetPCS(hICC, cmsSigLabData);
+    cmsSetHeaderRenderingIntent(hICC, INTENT_PERCEPTUAL);
+
+    // Creates a pipeline with 3D grid only
+    Pipeline = cmsPipelineAlloc(NULL, 3, 3);
+    if (!Pipeline) goto error_out;
+
+    for (i = 0; i < MAX_INPUT_DIMENSIONS; i++) Dimensions[i] = 33;
+    CLUT = cmsStageAllocCLut16bitGranular(NULL, Dimensions, 3, 3, NULL);
+
+    if (!CLUT ||
+            !cmsStageSampleCLut16bit(CLUT, luminance_adjustment_sampler,
+                                     (void*)d, 0))
+        goto error_out;
+
+#if LCMS_VERSION >= 2050
+    if (!cmsPipelineInsertStage(Pipeline, cmsAT_END, CLUT))
+        goto error_out;
+#else
+    cmsPipelineInsertStage(Pipeline, cmsAT_END, CLUT);
+#endif
+
+    // Create tags
+    cmsWriteTag(hICC, cmsSigMediaWhitePointTag, (void *) cmsD50_XYZ());
+    cmsWriteTag(hICC, cmsSigAToB0Tag, (void *) Pipeline);
+
+    // Pipeline is already on virtual profile
+    cmsPipelineFree(Pipeline);
+
+    return hICC;
+
+error_out:
+    if (CLUT) cmsStageFree(CLUT);
+    if (Pipeline) cmsPipelineFree(Pipeline);
+    if (hICC) cmsCloseProfile(hICC);
+    return NULL;
+#else /* HAVE_LCMS1 */
     LPLUT Lut;
 
     hICC = _cmsCreateProfilePlaceholder();
@@ -295,11 +472,12 @@
         return NULL;
     }
     // Create tags
-    cmsAddTag(hICC, icSigMediaWhitePointTag, (LPVOID) cmsD50_XYZ());
-    cmsAddTag(hICC, icSigAToB0Tag, (LPVOID) Lut);
+    cmsAddTag(hICC, icSigMediaWhitePointTag, (void *) cmsD50_XYZ());
+    cmsAddTag(hICC, icSigAToB0Tag, (void *) Lut);
     // LUT is already on virtual profile
     cmsFreeLUT(Lut);
     return hICC;
+#endif /* HAVE_LCMS1 */
 }
 
 /* Find a for which (1-exp(-a x)/(1-exp(-a)) has derivative b at x=0 */
@@ -384,7 +562,7 @@
     }
 
     if (d->rgbtolabTransform == NULL) {
-        cmsHPROFILE labProfile = cmsCreateLabProfile(cmsD50_xyY());
+        cmsHPROFILE labProfile = cmsCreateLab2Profile(cmsD50_xyY());
         d->rgbtolabTransform = cmsCreateTransform(d->profile[in_profile],
                                TYPE_RGB_16, labProfile,
                                TYPE_Lab_16, INTENT_ABSOLUTE_COLORIMETRIC, 0);
@@ -559,6 +737,19 @@
                 ufraw_message(UFRAW_REPORT, NULL);
                 d->luminosityProfile = NULL;
             } else {
+#ifdef HAVE_LCMS2
+                cmsToneCurve **TransferFunction =
+                    (cmsToneCurve **)d->TransferFunction;
+                cmsFloat32Number values[0x100];
+                cmsFreeToneCurve(TransferFunction[0]);
+                for (i = 0; i < 0x100; i++)
+                    values[i] = (cmsFloat32Number) cs->m_Samples[i];
+                TransferFunction[0] =
+                    cmsBuildTabulatedToneCurveFloat(NULL, 0x100, values);
+                d->luminosityProfile = cmsCreateLinearizationDeviceLink(
+                                           cmsSigLabData, TransferFunction);
+                cmsSetDeviceClass(d->luminosityProfile, cmsSigAbstractClass);
+#else
                 LPGAMMATABLE *TransferFunction =
                     (LPGAMMATABLE *)d->TransferFunction;
                 for (i = 0; i < 0x100; i++)
@@ -566,6 +757,7 @@
                 d->luminosityProfile = cmsCreateLinearizationDeviceLink(
                                            icSigLabData, TransferFunction);
                 cmsSetDeviceClass(d->luminosityProfile, icSigAbstractClass);
+#endif
             }
             CurveSampleFree(cs);
         }

Index: configure.ac
===================================================================
RCS file: /cvsroot/ufraw/ufraw/configure.ac,v
retrieving revision 1.177
retrieving revision 1.178
diff -u -d -r1.177 -r1.178
--- configure.ac        1 Jun 2013 18:30:12 -0000       1.177
+++ configure.ac        5 Oct 2013 16:00:10 -0000       1.178
@@ -69,7 +69,22 @@
   [ with_prefix=NONE ] )
 
 PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.12 gthread-2.0)
-PKG_CHECK_MODULES(LCMS, lcms >= 1.14)
+
+PKG_CHECK_MODULES(LCMS2, lcms2,
+  [have_lcms="yes (lcms 2.x)"
+   AC_DEFINE(HAVE_LCMS2, 1, have the lcms 2.x library)
+   LCMS_CFLAGS="$LCMS2_CFLAGS"
+   LCMS_LIBS="$LCMS2_LIBS"],
+  [have_lcms="no (lcms 2.x not found)"])
+
+if test "x$have_lcms" != "xyes (lcms 2.x)"; then
+  PKG_CHECK_MODULES(LCMS1, lcms >= 1.14,
+    [have_lcms="yes (lcms 1.x)"
+     AC_DEFINE(HAVE_LCMS1, 1, have the lcms 1.x library)
+     LCMS_CFLAGS="$LCMS1_CFLAGS"
+     LCMS_LIBS="$LCMS1_LIBS"],
+    [AC_MSG_ERROR(can not build UFRaw without lcms)])
+fi
 
 AC_ARG_WITH([gtk],
   [AS_HELP_STRING([--with-gtk],
@@ -365,6 +380,7 @@
 AC_OUTPUT
 
 AC_MSG_NOTICE(====================== summary =====================)
+AC_MSG_NOTICE(Color management support: $have_lcms)
 AC_MSG_NOTICE(build GTK GUI: $have_gtk)
 AC_MSG_NOTICE(build GIMP plug-in: $have_gimp)
 AC_MSG_NOTICE(EXIF support using exiv2: $have_exiv2)


------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134791&iu=/4140/ostg.clktrk
_______________________________________________
ufraw-cvs mailing list
ufraw-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ufraw-cvs

Reply via email to