File: gimpimage-colormap.c

package info (click to toggle)
gimp 3.0.4-6.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 210,548 kB
  • sloc: ansic: 842,405; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (534 lines) | stat: -rw-r--r-- 15,331 bytes parent folder | download | duplicates (2)
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
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libgimpcolor/gimpcolor.h"

#include "core-types.h"

#include "config/gimpgeglconfig.h"

#include "gegl/gimp-babl.h"
#include "gegl/gimp-gegl-loops.h"

#include "gimp.h"
#include "gimpcontainer.h"
#include "gimpdatafactory.h"
#include "gimpdrawable.h"
#include "gimpimage.h"
#include "gimpimage-colormap.h"
#include "gimpimage-private.h"
#include "gimpimage-undo.h"
#include "gimpimage-undo-push.h"
#include "gimppalette.h"

#include "gimp-intl.h"


typedef struct
{
  GeglBuffer *buffer;
  const Babl *format;

  /* Shared by jobs. */
  gboolean   *found;
  GRWLock    *lock;
} IndexUsedJobData;


/*  local function prototype  */

static void   gimp_image_colormap_set_palette_entry    (GimpImage        *image,
                                                        GeglColor        *color,
                                                        gint              index);
static void   gimp_image_colormap_thread_is_index_used (IndexUsedJobData *data,
                                                        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->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->palette = GIMP_PALETTE (gimp_palette_new (NULL, palette_name));

  gimp_image_colormap_update_formats (image);

  gimp_palette_set_columns (private->palette, 16);

  gimp_data_set_image (GIMP_DATA (private->palette), image, TRUE, FALSE);

  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 (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 (GIMP_IS_PALETTE (private->palette));

  g_clear_object (&private->palette);

  /* don't touch the image's babl_palettes because we might still have
   * buffers with that palette on the undo stack, and on undoing the
   * image back to indexed, we must have exactly these palettes around
   */
}

void
gimp_image_colormap_update_formats (GimpImage *image)
{
  GimpImagePrivate *private;
  const Babl       *space;
  const Babl       *format;
  gchar            *format_name;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  space = gimp_image_get_layer_space (image);

  format_name = g_strdup_printf ("-gimp-indexed-format-%d",
                                 gimp_image_get_id (image));

  babl_new_palette_with_space (format_name, space,
                               &private->babl_palette_rgb,
                               &private->babl_palette_rgba);

  format = gimp_babl_format (GIMP_RGB, private->precision, FALSE, space),
  g_free (format_name);

  if (private->palette && gimp_palette_get_n_colors (private->palette) > 0)
    {
      guchar *colormap;
      gint    n_colors;

      gimp_palette_restrict_format (private->palette, format, FALSE);

      colormap = _gimp_image_get_colormap (image, &n_colors);
      g_return_if_fail (colormap != NULL);

      babl_palette_set_palette (private->babl_palette_rgb,
                                format, colormap, n_colors);
      babl_palette_set_palette (private->babl_palette_rgba,
                                format, colormap, n_colors);

      g_free (colormap);
    }
}

const Babl *
gimp_image_colormap_get_rgb_format (GimpImage *image)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  return GIMP_IMAGE_GET_PRIVATE (image)->babl_palette_rgb;
}

const Babl *
gimp_image_colormap_get_rgba_format (GimpImage *image)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  return GIMP_IMAGE_GET_PRIVATE (image)->babl_palette_rgba;
}

GimpPalette *
gimp_image_get_colormap_palette (GimpImage *image)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  return GIMP_IMAGE_GET_PRIVATE (image)->palette;
}

void
gimp_image_set_colormap_palette (GimpImage   *image,
                                 GimpPalette *palette,
                                 gboolean     push_undo)
{
  GimpImagePrivate *private;
  GimpPaletteEntry *entry;
  gint              n_colors, i;

  g_return_if_fail (GIMP_IS_IMAGE (image));
  g_return_if_fail (palette != NULL);
  n_colors = gimp_palette_get_n_colors (palette);
  g_return_if_fail (n_colors >= 0 && n_colors <= 256);

  private = GIMP_IMAGE_GET_PRIVATE (image);

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

  if (!private->palette)
    gimp_image_colormap_init (image);

  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 < n_colors; i++)
    {
      GimpPaletteEntry *entry = gimp_palette_get_entry (palette, i);

      gimp_image_colormap_set_palette_entry (image, entry->color, i);
    }

  gimp_data_thaw (GIMP_DATA (private->palette));

  gimp_image_colormap_changed (image, -1);
}

guchar *
_gimp_image_get_colormap (GimpImage *image,
                          gint      *n_colors)
{
  GimpImagePrivate *private;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  private = GIMP_IMAGE_GET_PRIVATE (image);

  if (private->palette == NULL)
    return NULL;

  g_return_val_if_fail (private->palette->format != NULL, NULL);

  return gimp_palette_get_colormap (private->palette, private->palette->format, n_colors);
}

void
_gimp_image_set_colormap (GimpImage    *image,
                          const guchar *colormap,
                          gint          n_colors,
                          gboolean      push_undo)
{
  GimpImagePrivate *private;
  const Babl       *space;
  const Babl       *format;

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

  private = GIMP_IMAGE_GET_PRIVATE (image);

  if (private->palette == NULL)
    gimp_image_colormap_init (image);

  space  = gimp_image_get_layer_space (image);
  format = gimp_babl_format (GIMP_RGB, private->precision, FALSE, space);

  gimp_palette_set_colormap (private->palette, format, colormap, n_colors, push_undo);
  gimp_image_colormap_changed (image, -1);
}

void
gimp_image_unset_colormap (GimpImage *image,
                           gboolean   push_undo)
{
  GimpImagePrivate *private;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  if (push_undo)
    gimp_image_undo_push_image_colormap (image,
                                         C_("undo-type", "Unset Colormap"));

  if (private->palette)
    {
      gimp_image_colormap_dispose (image);
      gimp_image_colormap_free (image);
    }

  gimp_image_colormap_changed (image, -1);
}

gboolean
gimp_image_colormap_is_index_used (GimpImage *image,
                                   gint       color_index)
{
  GList       *layers;
  GList       *iter;
  GThreadPool *pool;
  GRWLock      lock;
  gboolean     found = FALSE;
  gint         num_processors;

  g_rw_lock_init (&lock);
  num_processors = GIMP_GEGL_CONFIG (image->gimp->config)->num_processors;
  layers         = gimp_image_get_layer_list (image);

  pool = g_thread_pool_new_full ((GFunc) gimp_image_colormap_thread_is_index_used,
                                 GINT_TO_POINTER (color_index),
                                 (GDestroyNotify) g_free,
                                 num_processors, TRUE, NULL);
  for (iter = layers; iter; iter = g_list_next (iter))
    {
      IndexUsedJobData *job_data;

      job_data = g_malloc (sizeof (IndexUsedJobData ));
      job_data->buffer = gimp_drawable_get_buffer (iter->data);
      job_data->format = gimp_drawable_get_format_without_alpha (iter->data);
      job_data->found  = &found;
      job_data->lock   = &lock;

      g_thread_pool_push (pool, job_data, NULL);
    }

  g_thread_pool_free (pool, FALSE, TRUE);
  g_rw_lock_clear (&lock);
  g_list_free (layers);

  return found;
}

GeglColor *
gimp_image_get_colormap_entry (GimpImage *image,
                               gint       color_index)
{
  GimpImagePrivate *private;
  GimpPaletteEntry *entry;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_val_if_fail (private->palette != NULL, NULL);
  g_return_val_if_fail (color_index >= 0 &&
                        color_index < gimp_palette_get_n_colors (private->palette),
                        NULL);

  entry = gimp_palette_get_entry (private->palette, color_index);

  g_return_val_if_fail (entry != NULL, NULL);

  return entry->color;
}

void
gimp_image_set_colormap_entry (GimpImage *image,
                               gint       color_index,
                               GeglColor *color,
                               gboolean   push_undo)
{
  GimpImagePrivate *private;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->palette != NULL);
  g_return_if_fail (color_index >= 0 &&
                    color_index < gimp_palette_get_n_colors (private->palette));
  g_return_if_fail (GEGL_IS_COLOR (color));

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

  gimp_image_colormap_set_palette_entry (image, color, color_index);

  gimp_image_colormap_changed (image, color_index);
}

void
gimp_image_add_colormap_entry (GimpImage *image,
                               GeglColor *color)
{
  GimpImagePrivate *private;

  g_return_if_fail (GIMP_IS_IMAGE (image));

  private = GIMP_IMAGE_GET_PRIVATE (image);

  g_return_if_fail (private->palette != NULL);
  g_return_if_fail (gimp_palette_get_n_colors (private->palette) < 256);
  g_return_if_fail (GEGL_IS_COLOR (color));

  gimp_image_undo_push_image_colormap (image,
                                       C_("undo-type", "Add Color to Colormap"));

  gimp_image_colormap_set_palette_entry (image, color,
                                         gimp_palette_get_n_colors (private->palette));

  gimp_image_colormap_changed (image, -1);
}

gboolean
gimp_image_delete_colormap_entry (GimpImage *image,
                                  gint       color_index,
                                  gboolean   push_undo)
{
  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);

  if (! gimp_image_colormap_is_index_used (image, color_index))
    {
      GimpImagePrivate *private;
      GimpPaletteEntry *entry;
      GList            *layers;
      GList            *iter;

      if (push_undo)
        {
          gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_COLORMAP_REMAP,
                                       C_("undo-type", "Delete Colormap entry"));

          gimp_image_undo_push_image_colormap (image, NULL);
        }

      private = GIMP_IMAGE_GET_PRIVATE (image);
      layers  = gimp_image_get_layer_list (image);

      for (iter = layers; iter; iter = g_list_next (iter))
        {
          if (push_undo)
            gimp_image_undo_push_drawable_mod (image, NULL, iter->data, TRUE);

          gimp_gegl_shift_index (gimp_drawable_get_buffer (iter->data), NULL,
                                 gimp_drawable_get_format (iter->data),
                                 color_index, -1);
        }

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

      g_list_free (layers);

      if (push_undo)
        gimp_image_undo_group_end (image);

      gimp_image_colormap_changed (image, -1);

      return TRUE;
    }

  return FALSE;
}


/*  private functions  */

static void
gimp_image_colormap_set_palette_entry (GimpImage *image,
                                       GeglColor *color,
                                       gint       index)
{
  GimpImagePrivate *private   = GIMP_IMAGE_GET_PRIVATE (image);
  GeglColor        *new_color = gegl_color_new ("black");
  const Babl       *space;
  const Babl       *format;
  guint8            rgb[3];
  gchar             name[64];
  gint              i;

  g_return_if_fail (GEGL_IS_COLOR (color));

  /* Adding black entries if needed. */
  for (i = gimp_palette_get_n_colors (private->palette);
       i <= index;
       i++)
    {
      g_snprintf (name, sizeof (name), "#%d", i);
      gimp_palette_add_entry (private->palette, index, name, new_color);
    }

  space  = gimp_image_get_layer_space (image);
  format = gimp_babl_format (GIMP_RGB, private->precision, FALSE, space);

  /* For image colormap, we force the color to be in the target format (which at
   * time of writing is only "R'G'B' u8" for the target space).
   */
  gegl_color_get_pixel (color, format, rgb);
  gegl_color_set_pixel (new_color, format, rgb);
  gimp_palette_set_entry (private->palette, index, name, new_color);
  g_object_unref (new_color);
}

static void
gimp_image_colormap_thread_is_index_used (IndexUsedJobData *data,
                                          gint              index)
{
  g_rw_lock_reader_lock (data->lock);
  if (*data->found)
    {
      g_rw_lock_reader_unlock (data->lock);
      return;
    }
  g_rw_lock_reader_unlock (data->lock);
  if (gimp_gegl_is_index_used (data->buffer, NULL, data->format, index))
    {
      g_rw_lock_writer_lock (data->lock);
      *data->found = TRUE;
      g_rw_lock_writer_unlock (data->lock);
    }
}