gimp-babl.c 8.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimp-babl.c
 * Copyright (C) 2012 Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>

#include "gimp-gegl-types.h"

#include "gimp-babl.h"

#include "gimp-intl.h"


void
gimp_babl_init (void)
{
  babl_format_new ("name", "R' u8",
                   babl_model ("R'G'B'A"),
                   babl_type ("u8"),
                   babl_component ("R'"),
                   NULL);
  babl_format_new ("name", "G' u8",
                   babl_model ("R'G'B'A"),
                   babl_type ("u8"),
                   babl_component ("G'"),
                   NULL);
  babl_format_new ("name", "B' u8",
                   babl_model ("R'G'B'A"),
                   babl_type ("u8"),
                   babl_component ("B'"),
                   NULL);
  babl_format_new ("name", "A u8",
                   babl_model ("R'G'B'A"),
                   babl_type ("u8"),
                   babl_component ("A"),
                   NULL);

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  babl_format_new ("name", "R float",
                   babl_model ("RGBA"),
                   babl_type ("float"),
                   babl_component ("R"),
                   NULL);
  babl_format_new ("name", "G float",
                   babl_model ("RGBA"),
                   babl_type ("float"),
                   babl_component ("G"),
                   NULL);
  babl_format_new ("name", "B float",
                   babl_model ("RGBA"),
                   babl_type ("float"),
                   babl_component ("B"),
                   NULL);
71
  babl_format_new ("name", "A float",
72
                   babl_model ("RGBA"),
73 74 75 76 77
                   babl_type ("float"),
                   babl_component ("A"),
                   NULL);

  babl_format_new ("name", "A double",
78
                   babl_model ("RGBA"),
79 80 81 82 83 84 85 86 87 88 89 90 91
                   babl_type ("double"),
                   babl_component ("A"),
                   NULL);
}

static const struct
{
  const gchar *name;
  const gchar *description;
}
babl_descriptions[] =
{
  { "R'G'B' u8",  N_("RGB") },
92 93
  { "RGB float",  N_("RGB") },

94
  { "R'G'B'A u8", N_("RGB-alpha") },
95 96
  { "RGBA float", N_("RGB-alpha") },

97
  { "Y' u8",      N_("Grayscale") },
98 99
  { "Y float",    N_("Grayscale") },

100
  { "Y'A u8",     N_("Grayscale-alpha") },
101 102
  { "YA float",   N_("Grayscale-alpha") },

103
  { "R' u8",      N_("Red component") },
104 105
  { "R float",    N_("Red component") },

106
  { "G' u8",      N_("Green component") },
107 108
  { "G float",    N_("Green component") },

109
  { "B' u8",      N_("Blue component") },
110 111
  { "B float",    N_("Blue component") },

112 113 114
  { "A u8",       N_("Alpha component") },
  { "A float",    N_("Alpha component") },
  { "A double",   N_("Alpha component") }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
};

static GHashTable *babl_description_hash = NULL;

const gchar *
gimp_babl_get_description (const Babl *babl)
{
  const gchar *description;

  g_return_val_if_fail (babl != NULL, NULL);

  if (G_UNLIKELY (! babl_description_hash))
    {
      gint i;

      babl_description_hash = g_hash_table_new (g_str_hash,
                                                g_str_equal);

      for (i = 0; i < G_N_ELEMENTS (babl_descriptions); i++)
        g_hash_table_insert (babl_description_hash,
                             (gpointer) babl_descriptions[i].name,
                             gettext (babl_descriptions[i].description));
    }

  if (babl_format_is_palette (babl))
    {
      if (babl_format_has_alpha (babl))
        return _("Indexed-alpha");
      else
        return _("Indexed");
    }

  description = g_hash_table_lookup (babl_description_hash,
                                     babl_get_name (babl));

  if (description)
    return description;

  return g_strconcat ("ERROR: unknown Babl format ",
                      babl_get_name (babl), NULL);
}
156 157 158 159 160 161

GimpImageBaseType
gimp_babl_format_get_base_type (const Babl *format)
{
  g_return_val_if_fail (format != NULL, -1);

162 163 164 165 166
  if (format == babl_format ("Y u8")    ||
      format == babl_format ("Y' u8")   ||
      format == babl_format ("Y float") ||
      format == babl_format ("Y'A u8")  ||
      format == babl_format ("YA float"))
167 168 169
    {
      return GIMP_GRAY;
    }
170 171 172 173
  else if (format == babl_format ("R'G'B' u8")  ||
           format == babl_format ("RGB float")  ||
           format == babl_format ("R'G'B'A u8") ||
           format == babl_format ("RGBA float"))
174 175 176 177 178 179 180 181 182 183
    {
      return GIMP_RGB;
    }
  else if (babl_format_is_palette (format))
    {
      return GIMP_INDEXED;
    }

  g_return_val_if_reached (-1);
}
184

185 186 187 188 189 190 191 192 193 194 195
GimpPrecision
gimp_babl_format_get_precision (const Babl *format)
{
  const Babl *type;

  g_return_val_if_fail (format != NULL, -1);

  type = babl_format_get_type (format, 0);

  if (type == babl_type ("u8"))
    return GIMP_PRECISION_U8;
196 197
  else if (type == babl_type ("float"))
    return GIMP_PRECISION_FLOAT;
198 199 200 201

  g_return_val_if_reached (-1);
}

202 203
const Babl *
gimp_babl_format (GimpImageBaseType  base_type,
204
                  GimpPrecision      precision,
205 206 207 208 209
                  gboolean           with_alpha)
{
  switch (base_type)
    {
    case GIMP_RGB:
210 211 212 213 214 215 216 217
      switch (precision)
        {
        case GIMP_PRECISION_U8:
          if (with_alpha)
            return babl_format ("R'G'B'A u8");
          else
            return babl_format ("R'G'B' u8");

218 219 220 221 222 223
        case GIMP_PRECISION_FLOAT:
          if (with_alpha)
            return babl_format ("RGBA float");
          else
            return babl_format ("RGB float");

224 225 226 227
        default:
          break;
        }
      break;
228 229

    case GIMP_GRAY:
230 231 232 233 234 235 236 237
      switch (precision)
        {
        case GIMP_PRECISION_U8:
          if (with_alpha)
            return babl_format ("Y'A u8");
          else
            return babl_format ("Y' u8");

238 239 240 241 242 243
        case GIMP_PRECISION_FLOAT:
          if (with_alpha)
            return babl_format ("YA float");
          else
            return babl_format ("Y float");

244 245 246 247
        default:
          break;
        }
      break;
248 249 250 251 252 253 254 255

    case GIMP_INDEXED:
      /* need to use the image's api for this */
      break;
    }

  g_return_val_if_reached (NULL);
}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

const Babl *
gimp_babl_component_format (GimpImageBaseType base_type,
                            GimpPrecision     precision,
                            gint              index)
{
  switch (base_type)
    {
    case GIMP_RGB:
      switch (precision)
        {
        case GIMP_PRECISION_U8:
          switch (index)
            {
            case 0: return babl_format ("R' u8");
            case 1: return babl_format ("G' u8");
            case 2: return babl_format ("B' u8");
            case 3: return babl_format ("A u8");
            default:
              break;
            }
277 278 279 280 281 282 283 284 285 286 287 288 289
          break;

        case GIMP_PRECISION_FLOAT:
          switch (index)
            {
            case 0: return babl_format ("R float");
            case 1: return babl_format ("G float");
            case 2: return babl_format ("B float");
            case 3: return babl_format ("A float");
            default:
              break;
            }
          break;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

        default:
          break;
        }
      break;

    case GIMP_GRAY:
      switch (precision)
        {
        case GIMP_PRECISION_U8:
          switch (index)
            {
            case 0: return babl_format ("Y' u8");
            case 1: return babl_format ("A u8");
            default:
              break;
            }
307 308 309 310 311 312 313 314 315 316 317
          break;

        case GIMP_PRECISION_FLOAT:
          switch (index)
            {
            case 0: return babl_format ("Y float");
            case 1: return babl_format ("A float");
            default:
              break;
            }
          break;
318 319 320 321 322 323 324 325 326 327 328 329 330

        default:
          break;
        }
      break;

    case GIMP_INDEXED:
      /* need to use the image's api for this */
      break;
    }

  g_return_val_if_reached (NULL);
}