[babl] allow reregistering but check for differences



commit 4006f433f9e0d6b3cb54943adccada464f69595a
Author: Rupert Weber <gimp leguanease org>
Date:   Fri Oct 1 02:18:53 2010 +0200

    allow reregistering but check for differences
    
    babl_model_new() emitted a warning when reregistering a model,
    babl_format_new(), babl_type_new(), and babl_component_new()
    quietly returned a preexisting object.
    
    Changed so that all four functions return the preexisting object
    if the new registration doesn't conflict, otherwise make the
    error fatal.
    
    (Fixed some conflicting and redundant registrations for
    "Y'CbCr u8" along the way)

 babl/babl-component.c   |   31 ++++++++++++++++++++++++++-----
 babl/babl-format.c      |   38 ++++++++++++++++++++++++++++++++++++--
 babl/babl-model.c       |   24 +++++++++++++++++++++++-
 babl/babl-type.c        |   29 +++++++++++++++++++++++------
 babl/base/formats.c     |   20 ++++++++------------
 babl/base/model-ycbcr.c |    1 -
 extensions/gggl-lies.c  |    3 +++
 extensions/gggl.c       |    3 +++
 8 files changed, 122 insertions(+), 27 deletions(-)
---
diff --git a/babl/babl-component.c b/babl/babl-component.c
index 54a06b3..0d03647 100644
--- a/babl/babl-component.c
+++ b/babl/babl-component.c
@@ -44,6 +44,19 @@ component_new (const char *name,
   return babl;
 }
 
+
+static int
+is_component_duplicate (Babl *babl, int luma, int chroma, int alpha)
+{
+  if (babl->component.luma   != luma   ||
+      babl->component.chroma != chroma ||
+      babl->component.alpha  != alpha)
+    return 0;
+
+  return 1;
+}
+
+
 Babl *
 babl_component_new (void *first_arg,
                     ...)
@@ -54,7 +67,8 @@ babl_component_new (void *first_arg,
   int         luma   = 0;
   int         chroma = 0;
   int         alpha  = 0;
-  const char *arg    = (char *) first_arg;
+  const char *name   = first_arg;
+  const char *arg;
 
   va_start (varg, first_arg);
 
@@ -95,22 +109,29 @@ babl_component_new (void *first_arg,
 
       else
         {
-          babl_fatal ("unhandled argument '%s' for format '%s'", arg, first_arg);
+          babl_fatal ("unhandled argument '%s' for component '%s'", arg, name);
         }
     }
 
   va_end (varg);
 
-  babl = babl_db_exist (db, id, first_arg);
+  babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablComponent '%s' with different id!",
+                name);
+
   if (babl)
     {
       /* There is an instance already registered by the required id/name,
-       * returning the preexistent one instead.
+       * returning the preexistent one instead if it doesn't differ.
        */
+      if (!is_component_duplicate (babl, luma, chroma, alpha))
+        babl_fatal ("BablComponent '%s' already registered "
+                    "with different attributes!", name);
       return babl;
     }
 
-  babl = component_new (first_arg, id, luma, chroma, alpha);
+  babl = component_new (name, id, luma, chroma, alpha);
 
   /* Since there is not an already registered instance by the required
    * id/name, inserting newly created class into database.
diff --git a/babl/babl-format.c b/babl/babl-format.c
index 186d15a..3760452 100644
--- a/babl/babl-format.c
+++ b/babl/babl-format.c
@@ -51,7 +51,7 @@ format_new (const char     *name,
 {
   Babl *babl;
 
-  /* i is desintation position */
+  /* i is destination position */
   int i, j, component_found = 0;
   for (i = 0; i < model->components; i++)
     {
@@ -236,6 +236,32 @@ babl_format_n (Babl *btype,
   return babl;
 }
 
+static int
+is_format_duplicate (Babl           *babl,
+                     int             planar,
+                     int             components,
+                     BablModel      *model,
+                     BablComponent **component,
+                     BablSampling  **sampling,
+                     BablType      **type)
+{
+  int i;
+
+  if (babl->format.planar     != planar     ||
+      babl->format.components != components ||
+      babl->format.model      != model)
+    return 0;
+
+  for (i = 0; i < components; i++)
+    {
+      if (babl->format.component[i] != component[i] ||
+          babl->format.sampling[i]  != sampling[i]  ||
+          babl->format.type[i]      != type[i])
+        return 0;
+    }
+  return 1;
+}
+
 Babl *
 babl_format_new (void *first_arg,
                  ...)
@@ -359,11 +385,19 @@ babl_format_new (void *first_arg,
     name = create_name (model, components, component, type);
 
   babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablFormat '%s' with different id!", name);
+
   if (babl)
     {
       /* There is an instance already registered by the required id/name,
-       * returning the preexistent one instead.
+       * returning the preexistent one instead if it doesn't differ.
        */
+      if (!is_format_duplicate (babl, planar, components, model,
+                                component, sampling, type))
+        babl_fatal ("BablFormat '%s' already registered "
+                    "with different content!", name);
+
       babl_free (name);
       return babl;
     }
diff --git a/babl/babl-model.c b/babl/babl-model.c
index 4766b3b..11a3dd1 100644
--- a/babl/babl-model.c
+++ b/babl/babl-model.c
@@ -74,6 +74,24 @@ model_new (const char     *name,
   return babl;
 }
 
+static int
+is_model_duplicate (Babl *babl, int components, BablComponent **component)
+{
+  int   i;
+
+  if (babl->model.components != components)
+    return 0;
+
+  for (i = 0; i < components; i++)
+    {
+      if (babl->model.component[i] != component[i])
+        return 0;
+    }
+
+  return 1;
+}
+
+
 Babl *
 babl_model_new (void *first_argument,
                 ...)
@@ -166,6 +184,8 @@ babl_model_new (void *first_argument,
     name = babl_model_create_name (components, component);
 
   babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablModel '%s' with different id!", name);
 
   if (! babl)
     {
@@ -175,7 +195,9 @@ babl_model_new (void *first_argument,
     }
   else
     {
-      babl_log ("Warning: BablModel '%s' already registered!", name);
+      if (!is_model_duplicate (babl, components, component))
+        babl_fatal ("BablModel '%s' already registered "
+                    "with different components!", name);
     }
 
   babl_free (name);
diff --git a/babl/babl-type.c b/babl/babl-type.c
index 467b33f..1bf7a07 100644
--- a/babl/babl-type.c
+++ b/babl/babl-type.c
@@ -55,6 +55,15 @@ type_new (const char *name,
   return babl;
 }
 
+static int
+is_type_duplicate (Babl *babl, int bits)
+{
+  if (babl->type.bits != bits)
+    return 0;
+
+  return 1;
+}
+
 Babl *
 babl_type_new (void *first_arg,
                ...)
@@ -68,8 +77,8 @@ babl_type_new (void *first_arg,
   long        max        = 255;
   double      min_val    = 0.0;
   double      max_val    = 0.0;
-
-  const char *arg = first_arg;
+  const char *name = first_arg;
+  const char *arg;
 
   va_start (varg, first_arg);
 
@@ -122,22 +131,30 @@ babl_type_new (void *first_arg,
 
       else
         {
-          babl_fatal ("unhandled argument '%s' for format '%s'", arg, first_arg);
+          babl_fatal ("unhandled argument '%s' for format '%s'", arg, name);
         }
     }
 
   va_end (varg);
 
-  babl = babl_db_exist (db, id, first_arg);
+  babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablType '%s' with different id!", name);
+
   if (babl)
     {
       /* There is an instance already registered by the required id/name,
-       * returning the preexistent one instead.
+       * returning the preexistent one instead if it doesn't differ.
        */
+
+      if (!is_type_duplicate (babl, bits))
+        babl_fatal ("BablType '%s' already registered "
+                    "as different type!", name);
+
       return babl;
     }
 
-  babl = type_new (first_arg, id, bits);
+  babl = type_new (name, id, bits);
 
   /* Since there is not an already registered instance by the required
    * id/name, inserting newly created class into database.
diff --git a/babl/base/formats.c b/babl/base/formats.c
index 5e6e0c0..65b3d77 100644
--- a/babl/base/formats.c
+++ b/babl/base/formats.c
@@ -155,13 +155,21 @@ babl_formats_init (void)
     babl_type_from_id (BABL_U8),
     babl_component_from_id (BABL_LUMA),
     NULL);
+
+  /* overriding name, since the generated name would be wrong due
+   * to differing types
+   */
   babl_format_new (
     "name", "Y'CbCr u8",
+    "planar",
     babl_model_from_id (BABL_YCBCR),
     babl_type_from_id (BABL_U8_LUMA),
+    babl_sampling (1, 1),
     babl_component_from_id (BABL_LUMA),
     babl_type_from_id (BABL_U8_CHROMA),
+    babl_sampling (2, 2),
     babl_component_from_id (BABL_CB),
+    babl_sampling (2, 2),
     babl_component_from_id (BABL_CR),
     NULL);
   babl_format_new (
@@ -310,18 +318,6 @@ babl_formats_init (void)
     babl_component_from_id (BABL_LUMINANCE),
     NULL);
 
-  /* overriding name, since the generated name would be wrong due
-   * to differing types
-   */
-  babl_format_new (
-    "name", "Y'CbCr u8",
-    babl_model_from_id (BABL_YCBCR),
-    babl_type_from_id (BABL_U8_LUMA),
-    babl_component_from_id (BABL_LUMA),
-    babl_type_from_id (BABL_U8_CHROMA),
-    babl_component_from_id (BABL_CB),
-    babl_component_from_id (BABL_CR),
-    NULL);
   babl_format_new (
     babl_model_from_id (BABL_YCBCR),
     babl_type_from_id (BABL_FLOAT),
diff --git a/babl/base/model-ycbcr.c b/babl/base/model-ycbcr.c
index c4f6ab9..13f3581 100644
--- a/babl/base/model-ycbcr.c
+++ b/babl/base/model-ycbcr.c
@@ -242,7 +242,6 @@ formats (void)
 {
   babl_format_new (
     "name", "Y'CbCr u8",
-    "id", BABL_YCBCR420,
     "planar",
     babl_model_from_id (BABL_YCBCR),
     babl_type_from_id (BABL_U8_LUMA),
diff --git a/extensions/gggl-lies.c b/extensions/gggl-lies.c
index b93d47c..33d19ef 100644
--- a/extensions/gggl-lies.c
+++ b/extensions/gggl-lies.c
@@ -1926,10 +1926,13 @@ init (void)
     NULL);
   Babl *yuv8 = babl_format_new (
     "name", "Y'CbCr u8",
+    "planar",
     babl_model ("Y'CbCr"),
     babl_type ("u8-luma"),
+    babl_sampling (1, 1),
     babl_component ("Y'"),
     babl_type ("u8-chroma"),
+    babl_sampling (2, 2),
     babl_component ("Cb"),
     babl_component ("Cr"),
     NULL);
diff --git a/extensions/gggl.c b/extensions/gggl.c
index faf28ec..a9d163d 100644
--- a/extensions/gggl.c
+++ b/extensions/gggl.c
@@ -2017,10 +2017,13 @@ init (void)
     NULL);
   Babl *yuv8 = babl_format_new (
     "name", "Y'CbCr u8",
+    "planar",
     babl_model ("Y'CbCr"),
     babl_type ("u8-luma"),
+    babl_sampling (1, 1),
     babl_component ("Y'"),
     babl_type ("u8-chroma"),
+    babl_sampling (2, 2),
     babl_component ("Cb"),
     babl_component ("Cr"),
     NULL);



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