[babl/wip/pippin/icc-cluts: 3/4] use the same lut impl for all lut purposes



commit dc0033d1fcdb45c9ddd1317b302a542d8688df09
Author: Øyvind Kolås <pippin gimp org>
Date:   Tue Sep 5 19:38:42 2017 +0200

    use the same lut impl for all lut purposes

 babl/babl-fish-path.c      |    3 +-
 babl/babl-fish-reference.c |    7 ++---
 babl/babl-icc.c            |   11 +++++---
 babl/babl-space.c          |   12 ++++-----
 babl/babl-space.h          |   53 ++++++++++++++++++++++---------------------
 babl/babl-trc.c            |   42 +---------------------------------
 tools/babl-icc-dump.c      |   53 +++++++++++++++++++++++++++++++++++++++++++-
 7 files changed, 97 insertions(+), 84 deletions(-)
---
diff --git a/babl/babl-fish-path.c b/babl/babl-fish-path.c
index 2a71464..94b61ba 100644
--- a/babl/babl-fish-path.c
+++ b/babl/babl-fish-path.c
@@ -235,8 +235,7 @@ get_conversion_path (PathContext *pc,
 
           if ((path_cost < ref_cost) && /* do not use paths that took longer to compute than reference */
               (path_cost < pc->fish_path->fish_path.cost) && // best thus far
-              (path_error <= legal_error )               // within tolerance
-              )
+              (path_error <= legal_error ))               // within tolerance
             {
               /* We have found the best path so far,
                * let's copy it into our new fish */
diff --git a/babl/babl-fish-reference.c b/babl/babl-fish-reference.c
index 3e3b24e..6a9b996 100644
--- a/babl/babl-fish-reference.c
+++ b/babl/babl-fish-reference.c
@@ -493,10 +493,9 @@ babl_fish_reference_process (const Babl *babl,
     else babl_fatal ("oops");
   }
 
-  if (source_space != destination_space || 1)
+  if (source_space != destination_space)
   {
-    // if neither space has luts we use the matrix
-    if (destination_space->b2a0 || source_space->a2b0)
+    if (destination_space->a2b0 || source_space->b2a0)
     {
       int i;
 
@@ -505,7 +504,7 @@ babl_fish_reference_process (const Babl *babl,
       {
         double  xyz[4];
         static int counter = 0;
-  if (counter ++ % 100000 == 0)
+        if (counter ++ % 1000000 == 0)
           fprintf (stderr, "!");
 
         babl_space_to_xyz ((void*)source_space, rgba, xyz);
diff --git a/babl/babl-icc.c b/babl/babl-icc.c
index 51e4d65..343fa8a 100644
--- a/babl/babl-icc.c
+++ b/babl/babl-icc.c
@@ -708,15 +708,11 @@ static ICCv2CLUT *load_mft2 (ICC *state, int offset, int size)
   clut->in_table_size = icc_read (u16, offset + 48);
   clut->out_table_size = icc_read (u16, offset + 50);
 
-
   for (i = 0; i < 9; i++)
     clut->matrix[i] = icc_read (s15f16, offset + 12 + i * 4);
 
-  clut->clut = babl_calloc (sizeof (float), 3 * clut->clut_size[0] * clut->clut_size[1] * 
(clut->clut_size[2]+1));
   for (c = 0; c < 3; c++)
     clut->in_table[c] = babl_calloc (sizeof (float), clut->in_table_size);
-  for (c = 0; c < 3; c++)
-    clut->out_table[c] = babl_calloc (sizeof (float), clut->out_table_size);
 
   o = 52;
   for (c = 0; c < 3; c++)
@@ -725,11 +721,18 @@ static ICCv2CLUT *load_mft2 (ICC *state, int offset, int size)
       clut->in_table[c][i] = icc_read (u16, offset + o) / 65535.0;
       o+=2;
     }
+
+  clut->clut = babl_calloc (sizeof (float), 3 * clut->clut_size[0] * clut->clut_size[1] * 
(clut->clut_size[2]+1));
+
   for (i = 0; i < 3 * clut->clut_size[0] * clut->clut_size[1] * clut->clut_size[2]; i++)
     {
       clut->clut[i] = icc_read (u16, offset + o) / 65535.0;
       o+=2;
     }
+
+  for (c = 0; c < 3; c++)
+    clut->out_table[c] = babl_calloc (sizeof (float), clut->out_table_size);
+
   for (c = 0; c < 3; c++)
     for (i = 0; i < clut->out_table_size; i++)
     {
diff --git a/babl/babl-space.c b/babl/babl-space.c
index d4936be..ce2100b 100644
--- a/babl/babl-space.c
+++ b/babl/babl-space.c
@@ -845,10 +845,8 @@ universal_generic_rgba_converter (const Babl *conversion,unsigned char *src_char
   for (i = 0; i < samples; i++)
   {
      double rgba[4] = {rgba_in[0], rgba_in[1], rgba_in[2]};
-     babl_space_to_xyz (source_space,
-                        rgba, rgba);
-     babl_space_from_xyz (destination_space,
-                          rgba, rgba);
+     _babl_space_to_xyz (source_space, rgba, rgba);
+     _babl_space_from_xyz (destination_space, rgba, rgba);
      rgba_out[0] = rgba[0];
      rgba_out[1] = rgba[1];
      rgba_out[2] = rgba[2];
@@ -876,8 +874,8 @@ universal_generic_rgb_converter (const Babl *conversion,unsigned char *src_char,
   for (i = 0; i < samples; i++)
   {
      double rgba[4] = {rgba_in[0], rgba_in[1], rgba_in[2]};
-     babl_space_to_xyz (source_space, rgba, rgba);
-     babl_space_from_xyz (destination_space, rgba, rgba);
+     _babl_space_to_xyz (source_space, rgba, rgba);
+     _babl_space_from_xyz (destination_space, rgba, rgba);
      rgba_out[0] = rgba[0];
      rgba_out[1] = rgba[1];
      rgba_out[2] = rgba[2];
@@ -900,7 +898,7 @@ add_rgb_adapter (Babl *babl,
 {
   Babl *space = data;
 
-  if (space->space.a2b0 || babl->space.a2b0)
+  if ((space->space.a2b0 || babl->space.a2b0) && 0)
   {
     prep_conversion(babl_conversion_new(babl_format_with_space("RGBA float", (void*)space),
                     babl_format_with_space("RGBA float", babl),
diff --git a/babl/babl-space.h b/babl/babl-space.h
index 4b4ae38..7cb57bd 100644
--- a/babl/babl-space.h
+++ b/babl/babl-space.h
@@ -79,23 +79,40 @@ typedef struct
 
 } BablSpace;
 
+static inline int lut_init (int lut_size, float value, float *rdiff)
+{
+  float fentry = value * (lut_size-1);
+  int entry    = fentry;
+  float diff   = fentry - entry;
+
+  if (entry >= lut_size - 1) {
+    entry = lut_size - 1;
+    diff = 0.0;
+  }
+  else
+  {
+    if (entry < 0)
+    {
+      entry = 0;
+      diff = 0.0;
+    }
+  }
+  *rdiff = diff;
+  return entry;
+}
 
 static inline float do_lut (float *lut, int lut_size, float value)
 {
   int entry;
   float diff;
-  entry = value * (lut_size-1);
-  diff = ( (value * (lut_size-1)) - entry);
-  if (entry >= lut_size) entry = lut_size - 1;
-  else if (entry < 0) entry = 0;
-
-  if (diff > 0.0 && entry < lut_size - 1)
+  entry = lut_init (lut_size, value, &diff);
+  if (diff <= 0.0)
   {
-    return lut[entry] * (1.0 - diff) + lut[entry+1] * diff;
+    return lut[entry];
   }
   else
   {
-    return lut[entry];
+    return lut[entry] * (1.0 - diff) + lut[entry+1] * diff;
   }
 }
 
@@ -116,19 +133,7 @@ static inline void clut_interpol (ICCv2CLUT *clut, const double *ind, double *ou
 
   for (c = 0; c < 3; c++)
   {
-     entry[c] = val[c] * (dim[c]-1);
-     diff[c] = ((val[c] * (dim[c]-1)) - entry[c]);
-
-     if (entry[c] >= dim[c] - 1)
-     {
-       entry[c] = dim[c] - 1;
-       diff[c] = 0.0;
-     }
-     else if (entry[c] <= 0.0001)
-     {
-       entry[c] = 0;
-       diff[c] = 0.0;
-     }
+     entry[c] = lut_init (dim[c], val[c], &diff[c]);
   }
 
   // needs rework for non-same sized acceses
@@ -153,14 +158,10 @@ static inline void clut_interpol (ICCv2CLUT *clut, const double *ind, double *ou
 #else
      val[c] = clut->clut[IDX(entry[0], entry[1], entry[2],c)];
 #endif
+    outd[c] = do_lut (clut->out_table[c], clut->out_table_size, val[c]);
   }
-  for (c = 0; c < 3; c ++)
-    val[c] = do_lut (clut->out_table[c], clut->out_table_size, val[c]);
-  for (c = 0; c < 3; c ++)
-     outd[c] = val[c];
 }
 
-
 static inline void babl_space_to_xyzf (const Babl *space, const float *rgb, float *xyz)
 {
   BablSpace *space_ = (void*)space;
diff --git a/babl/babl-trc.c b/babl/babl-trc.c
index 7524ef8..b6cd249 100644
--- a/babl/babl-trc.c
+++ b/babl/babl-trc.c
@@ -38,51 +38,13 @@ static inline float _babl_trc_linear (const Babl *trc_, float value)
 static inline float babl_trc_lut_from_linear (const Babl *trc_, float x)
 {
   BablTRC *trc = (void*)trc_;
-  int entry;
-  float ret, diff;
-
-  entry = x * (trc->lut_size-1);
-  diff =  ( (x * (trc->lut_size-1)) - entry);
-
-  if (entry >= trc->lut_size -1)
-  {
-    entry = trc->lut_size - 1;
-    diff = 0.0;
-  }
-  else if (entry < 0) entry = 0;
-
-  if (diff > 0.0)
-  {
-    ret = trc->inv_lut[entry] * (1.0 - diff) + trc->inv_lut[entry+1] * diff;
-  }
-  else
-  {
-    ret = trc->inv_lut[entry];
-  }
-  return ret;
+  return do_lut (trc->inv_lut, trc->lut_size, x);
 }
 
 static inline float babl_trc_lut_to_linear (const Babl *trc_, float x)
 {
   BablTRC *trc = (void*)trc_;
-  int entry;
-  float ret, diff;
-
-  entry = x * (trc->lut_size-1);
-  diff =  ( (x * (trc->lut_size-1)) - entry);
-
-  if (entry >= trc->lut_size) entry = trc->lut_size - 1;
-  else if (entry < 0) entry = 0;
-
-  if (diff > 0.0 && entry < trc->lut_size - 1)
-  {
-    ret = trc->lut[entry] * (1.0 - diff) + trc->lut[entry+1] * diff;
-  }
-  else
-  {
-    ret = trc->lut[entry];
-  }
-  return ret;
+  return do_lut (trc->lut, trc->lut_size, x);
 }
 
 static inline float _babl_trc_gamma_to_linear (const Babl *trc_, float value)
diff --git a/tools/babl-icc-dump.c b/tools/babl-icc-dump.c
index 4b549ef..0f1f38a 100644
--- a/tools/babl-icc-dump.c
+++ b/tools/babl-icc-dump.c
@@ -295,7 +295,7 @@ static int load_icc_from_memory (const char *icc, long length, char **error)
   load_sign (icc, length, 20, pcs_space);
   load_sign (icc, length, 12, profile_class);
 
-  if (strcmp (profile_class, "mntr"))
+  if (0 && strcmp (profile_class, "mntr"))
   {
     *error = "not a monitor-class profile";
     return -1;
@@ -328,6 +328,57 @@ static int load_icc_from_memory (const char *icc, long length, char **error)
      icc_tag (icc, length, "cprt", &offset, &element_size);
      fprintf (stdout, "copyright: %s\n", icc + offset + 8);
   }
+  {
+     int offset, element_size;
+     icc_tag (icc, length, "A2B0", &offset, &element_size);
+     fprintf (stdout, "A2B0: %s\n", icc + offset);
+
+     if (!strcmp (icc + offset, "mft2"))
+     {
+       int input_channels = load_u8 (icc, length, offset + 8);
+       int output_channels = load_u8 (icc, length, offset + 9);
+       int grid_points = load_u8 (icc, length, offset + 10);
+
+       // 12 , 16, 20, .. 44 is matrix
+
+       int input_table_entries = load_u16 (icc, length, offset + 48);
+       int output_table_entries = load_u16 (icc, length, offset + 50);
+
+       fprintf (stdout, "in: %i out: %i grid-points:%i\n", input_channels, output_channels, grid_points);
+
+       fprintf (stdout, "%i %i\n", input_table_entries, output_table_entries);
+
+     }
+     else
+     {
+
+     int input_channels = load_u8 (icc, length, offset + 8);
+     int output_channels = load_u8 (icc, length, offset + 9);
+     int b_offset = load_u32 (icc, length, offset + 12);
+     int mat_offset = load_u32 (icc, length, offset + 16);
+     int m_offset = load_u32 (icc, length, offset + 20);
+     int clut_offset = load_u32 (icc, length, offset + 24);
+     int a_offset = load_u32 (icc, length, offset + 28);
+
+     fprintf (stdout, "in: %i out: %i\n", input_channels, output_channels);
+     fprintf (stdout, " %i %i %i %i %i %i %i \n",
+     input_channels, output_channels, b_offset, mat_offset, m_offset, clut_offset, a_offset);
+     int clut_precision = load_u8 (icc, length, offset + clut_offset + 16);
+
+     fprintf (stdout, "precision: %i\n", clut_precision);
+     {
+        int i;
+        for (i = 0; i < input_channels; i++)
+          fprintf (stdout, "%i:%i\n", i, load_u8 (icc, length, offset + clut_offset +i));
+     }
+     }
+
+  }
+  {
+     int offset, element_size;
+     icc_tag (icc, length, "B2A0", &offset, &element_size);
+     fprintf (stdout, "B2A0: %s\n", icc + offset);
+  }
 
 #if 1
   fprintf (stdout, "icc version: %i.%i.%i\n", profile_version_major, profile_version_minor, 
profile_version_micro);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]