gimpimage-colormap.c 8.08 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

#include "config.h"

#include <string.h>

22
#include <gegl.h>
23 24 25 26 27

#include "libgimpcolor/gimpcolor.h"

#include "core-types.h"

28 29 30
#include "gimp.h"
#include "gimpcontainer.h"
#include "gimpdatafactory.h"
31 32
#include "gimpimage.h"
#include "gimpimage-colormap.h"
33
#include "gimpimage-private.h"
34
#include "gimpimage-undo-push.h"
35
#include "gimppalette.h"
36

37
#include "gimp-intl.h"
38 39


40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/*  local function prototype  */

void   gimp_image_colormap_set_palette_entry (GimpImage *image,
                                              gint       index);


/*  public functions  */

void
gimp_image_colormap_init (GimpImage *image)
{
  GimpImagePrivate *private;
  GimpContainer    *palettes;
  gchar            *palette_name;
  gchar            *palette_id;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->colormap == NULL);
  g_return_if_fail (private->palette == NULL);

  palette_name = g_strdup_printf (_("Colormap of Image #%d (%s)"),
                                  gimp_image_get_ID (image),
                                  gimp_image_get_display_name (image));
  palette_id = g_strdup_printf ("gimp-indexed-image-palette-%d",
                                gimp_image_get_ID (image));

  private->n_colors = 0;
  private->colormap = g_new0 (guchar, GIMP_IMAGE_COLORMAP_SIZE);
  private->palette  = GIMP_PALETTE (gimp_palette_new (NULL, palette_name));

  gimp_data_make_internal (GIMP_DATA (private->palette), palette_id);

  palettes = gimp_data_factory_get_container (image->gimp->palette_factory);

  gimp_container_add (palettes, GIMP_OBJECT (private->palette));

  g_free (palette_name);
  g_free (palette_id);
}

void
gimp_image_colormap_dispose (GimpImage *image)
{
  GimpImagePrivate *private;
  GimpContainer    *palettes;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->colormap != NULL);
  g_return_if_fail (GIMP_IS_PALETTE (private->palette));

  palettes = gimp_data_factory_get_container (image->gimp->palette_factory);

  gimp_container_remove (palettes, GIMP_OBJECT (private->palette));
}

void
gimp_image_colormap_free (GimpImage *image)
{
  GimpImagePrivate *private;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->colormap != NULL);
  g_return_if_fail (GIMP_IS_PALETTE (private->palette));

  g_free (private->colormap);
  private->colormap = NULL;

  g_object_unref (private->palette);
  private->palette = NULL;
}

120
const guchar *
121
gimp_image_get_colormap (const GimpImage *image)
122
{
123
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
124

125
  return GIMP_IMAGE_GET_PRIVATE (image)->colormap;
126 127 128
}

gint
129
gimp_image_get_colormap_size (const GimpImage *image)
130
{
131
  g_return_val_if_fail (GIMP_IS_IMAGE (image), 0);
132

133
  return GIMP_IMAGE_GET_PRIVATE (image)->n_colors;
134 135 136
}

void
137
gimp_image_set_colormap (GimpImage    *image,
138
                         const guchar *colormap,
Sven Neumann's avatar
Sven Neumann committed
139 140
                         gint          n_colors,
                         gboolean      push_undo)
141
{
142 143
  GimpImagePrivate *private;

144
  g_return_if_fail (GIMP_IS_IMAGE (image));
145
  g_return_if_fail (colormap != NULL || n_colors == 0);
146 147
  g_return_if_fail (n_colors >= 0 && n_colors <= 256);

148 149
  private = GIMP_IMAGE_GET_PRIVATE (image);

150
  if (push_undo)
151
    gimp_image_undo_push_image_colormap (image, C_("undo-type", "Set Colormap"));
152

153 154
  if (private->colormap)
    memset (private->colormap, 0, GIMP_IMAGE_COLORMAP_SIZE);
155

156
  if (colormap)
157
    {
158
      if (! private->colormap)
159 160 161
        {
          gimp_image_colormap_init (image);
        }
162

163
      memcpy (private->colormap, colormap, n_colors * 3);
164
    }
165
  else if (private->colormap)
166
    {
167 168
      gimp_image_colormap_dispose (image);
      gimp_image_colormap_free (image);
169
    }
170

171
  private->n_colors = n_colors;
172

173
  gimp_image_colormap_changed (image, -1);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  if (private->palette)
    {
      GimpPaletteEntry *entry;
      gint              i;

      gimp_data_freeze (GIMP_DATA (private->palette));

      while ((entry = gimp_palette_get_entry (private->palette, 0)))
        gimp_palette_delete_entry (private->palette, entry);

      for (i = 0; i < private->n_colors; i++)
        gimp_image_colormap_set_palette_entry (image, i);

      gimp_data_thaw (GIMP_DATA (private->palette));
    }
190 191 192
}

void
193
gimp_image_get_colormap_entry (GimpImage *image,
194 195 196
                               gint       color_index,
                               GimpRGB   *color)
{
197 198
  GimpImagePrivate *private;

199
  g_return_if_fail (GIMP_IS_IMAGE (image));
200 201 202 203 204

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->colormap != NULL);
  g_return_if_fail (color_index >= 0 && color_index < private->n_colors);
205 206 207
  g_return_if_fail (color != NULL);

  gimp_rgba_set_uchar (color,
208 209 210
                       private->colormap[color_index * 3],
                       private->colormap[color_index * 3 + 1],
                       private->colormap[color_index * 3 + 2],
211 212 213 214
                       OPAQUE_OPACITY);
}

void
215
gimp_image_set_colormap_entry (GimpImage     *image,
216 217 218 219
                               gint           color_index,
                               const GimpRGB *color,
                               gboolean       push_undo)
{
220 221
  GimpImagePrivate *private;

222
  g_return_if_fail (GIMP_IS_IMAGE (image));
223 224 225 226 227

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->colormap != NULL);
  g_return_if_fail (color_index >= 0 && color_index < private->n_colors);
228 229 230
  g_return_if_fail (color != NULL);

  if (push_undo)
231
    gimp_image_undo_push_image_colormap (image,
232
                                         C_("undo-type", "Change Colormap entry"));
233 234

  gimp_rgb_get_uchar (color,
235 236 237
                      &private->colormap[color_index * 3],
                      &private->colormap[color_index * 3 + 1],
                      &private->colormap[color_index * 3 + 2]);
238

239
  gimp_image_colormap_changed (image, color_index);
240 241 242
}

void
243
gimp_image_add_colormap_entry (GimpImage     *image,
244 245
                               const GimpRGB *color)
{
246 247
  GimpImagePrivate *private;

248
  g_return_if_fail (GIMP_IS_IMAGE (image));
249 250 251 252 253

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->colormap != NULL);
  g_return_if_fail (private->n_colors < 256);
254 255
  g_return_if_fail (color != NULL);

256
  gimp_image_undo_push_image_colormap (image,
257
                                       C_("undo-type", "Add Color to Colormap"));
258 259

  gimp_rgb_get_uchar (color,
260 261 262
                      &private->colormap[private->n_colors * 3],
                      &private->colormap[private->n_colors * 3 + 1],
                      &private->colormap[private->n_colors * 3 + 2]);
263

264
  private->n_colors++;
265

266
  gimp_image_colormap_changed (image, -1);
267
}
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292


/*  private functions  */

void
gimp_image_colormap_set_palette_entry (GimpImage *image,
                                       gint       index)
{
  GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
  GimpRGB           color;
  gchar             name[64];

  gimp_rgba_set_uchar (&color,
                       private->colormap[3 * index + 0],
                       private->colormap[3 * index + 1],
                       private->colormap[3 * index + 2],
                       255);

  g_snprintf (name, sizeof (name), "#%d", index);

  if (gimp_palette_get_n_colors (private->palette) < private->n_colors)
    gimp_palette_add_entry (private->palette, index, name, &color);
  else
    gimp_palette_set_entry (private->palette, index, name, &color);
}