gimppalette-import.c 16.9 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 <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <cairo.h>
21
#define GEGL_ITERATOR2_API
22
#include <gegl.h>
23
#include <gdk-pixbuf/gdk-pixbuf.h>
24

25
#include "libgimpbase/gimpbase.h"
26 27
#include "libgimpcolor/gimpcolor.h"

28
#include "core-types.h"
29

30
#include "gimpchannel.h"
31
#include "gimpcontainer.h"
32
#include "gimpcontext.h"
33 34
#include "gimpgradient.h"
#include "gimpimage.h"
35
#include "gimpimage-colormap.h"
36
#include "gimppalette.h"
37
#include "gimppalette-import.h"
38
#include "gimppalette-load.h"
39
#include "gimppickable.h"
40

41
#include "gimp-intl.h"
42 43 44 45 46 47 48 49


#define MAX_IMAGE_COLORS (10000 * 2)


/*  create a palette from a gradient  ****************************************/

GimpPalette *
50 51 52 53 54 55
gimp_palette_import_from_gradient (GimpGradient                *gradient,
                                   GimpContext                 *context,
                                   gboolean                     reverse,
                                   GimpGradientBlendColorSpace  blend_color_space,
                                   const gchar                 *palette_name,
                                   gint                         n_colors)
56
{
57 58 59 60 61
  GimpPalette         *palette;
  GimpGradientSegment *seg = NULL;
  gdouble              dx, cur_x;
  GimpRGB              color;
  gint                 i;
62 63

  g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), NULL);
64
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
65
  g_return_val_if_fail (palette_name != NULL, NULL);
66
  g_return_val_if_fail (n_colors > 1, NULL);
67

68
  palette = GIMP_PALETTE (gimp_palette_new (context, palette_name));
69

70
  dx = 1.0 / (n_colors - 1);
71

72
  for (i = 0, cur_x = 0; i < n_colors; i++, cur_x += dx)
73
    {
74
      seg = gimp_gradient_get_color_at (gradient, context,
75 76
                                        seg, cur_x, reverse, blend_color_space,
                                        &color);
77
      gimp_palette_add_entry (palette, -1, NULL, &color);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    }

  return palette;
}


/*  create a palette from a non-indexed image  *******************************/

typedef struct _ImgColors ImgColors;

struct _ImgColors
{
  guint  count;
  guint  r_adj;
  guint  g_adj;
  guint  b_adj;
  guchar r;
  guchar g;
  guchar b;
};

static gint count_color_entries = 0;

static GHashTable *
102
gimp_palette_import_store_colors (GHashTable *table,
103 104 105
                                  guchar     *colors,
                                  guchar     *colors_real,
                                  gint        n_colors)
106 107 108 109 110
{
  gpointer   found_color = NULL;
  ImgColors *new_color;
  guint      key_colors = colors[0] * 256 * 256 + colors[1] * 256 + colors[2];

111
  if (table == NULL)
112
    {
113
      table = g_hash_table_new (g_direct_hash, g_direct_equal);
114 115 116 117
      count_color_entries = 0;
    }
  else
    {
118
      found_color = g_hash_table_lookup (table, GUINT_TO_POINTER (key_colors));
119 120 121 122 123
    }

  if (found_color == NULL)
    {
      if (count_color_entries > MAX_IMAGE_COLORS)
124 125 126 127
        {
          /* Don't add any more new ones */
          return table;
        }
128 129 130

      count_color_entries++;

131
      new_color = g_slice_new (ImgColors);
132 133 134 135 136 137 138 139 140

      new_color->count = 1;
      new_color->r_adj = 0;
      new_color->g_adj = 0;
      new_color->b_adj = 0;
      new_color->r     = colors[0];
      new_color->g     = colors[1];
      new_color->b     = colors[2];

141
      g_hash_table_insert (table, GUINT_TO_POINTER (key_colors), new_color);
142 143 144
    }
  else
    {
145
      new_color = found_color;
146 147

      if (new_color->count < (G_MAXINT - 1))
148
        new_color->count++;
149 150 151 152 153 154 155 156

      /* Now do the adjustments ...*/
      new_color->r_adj += (colors_real[0] - colors[0]);
      new_color->g_adj += (colors_real[1] - colors[1]);
      new_color->b_adj += (colors_real[2] - colors[2]);

      /* Boundary conditions */
      if(new_color->r_adj > (G_MAXINT - 255))
157
        new_color->r_adj /= new_color->count;
158 159

      if(new_color->g_adj > (G_MAXINT - 255))
160
        new_color->g_adj /= new_color->count;
161 162

      if(new_color->b_adj > (G_MAXINT - 255))
163
        new_color->b_adj /= new_color->count;
164 165
    }

166
  return table;
167 168 169
}

static void
170 171 172
gimp_palette_import_create_list (gpointer key,
                                 gpointer value,
                                 gpointer user_data)
173
{
174 175
  GSList    **list      = user_data;
  ImgColors  *color_tab = value;
176

177
  *list = g_slist_prepend (*list, color_tab);
178 179 180 181
}

static gint
gimp_palette_import_sort_colors (gconstpointer a,
182
                                 gconstpointer b)
183
{
184 185
  const ImgColors *s1 = a;
  const ImgColors *s2 = b;
186 187 188 189 190 191 192 193 194 195 196

  if(s1->count > s2->count)
    return -1;
  if(s1->count < s2->count)
    return 1;

  return 0;
}

static void
gimp_palette_import_create_image_palette (gpointer data,
197
                                          gpointer user_data)
198
{
199
  GimpPalette *palette   = user_data;
Manish Singh's avatar
Manish Singh committed
200
  ImgColors   *color_tab = data;
201 202 203 204
  gint         n_colors;
  gchar       *lab;
  GimpRGB      color;

205
  n_colors = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (palette),
206
                                                 "import-n-colors"));
207

208
  if (gimp_palette_get_n_colors (palette) >= n_colors)
209 210
    return;

211 212 213 214 215
  /* TRANSLATORS: the "%s" is an item title and "%u" is the number of
     occurrences for this item. */
  lab = g_strdup_printf (_("%s (occurs %u)"),
                         _("Untitled"),
                         color_tab->count);
216 217 218 219

  /* Adjust the colors to the mean of the the sample */
  gimp_rgba_set_uchar
    (&color,
220 221
     (guchar) color_tab->r + (color_tab->r_adj / color_tab->count),
     (guchar) color_tab->g + (color_tab->g_adj / color_tab->count),
222 223 224
     (guchar) color_tab->b + (color_tab->b_adj / color_tab->count),
     255);

225
  gimp_palette_add_entry (palette, -1, lab, &color);
226 227 228 229 230

  g_free (lab);
}

static GimpPalette *
231 232
gimp_palette_import_make_palette (GHashTable  *table,
                                  const gchar *palette_name,
233
                                  GimpContext *context,
234
                                  gint         n_colors)
235
{
236 237
  GimpPalette *palette;
  GSList      *list = NULL;
238
  GSList      *iter;
239

240
  palette = GIMP_PALETTE (gimp_palette_new (context, palette_name));
241

242 243
  if (! table)
    return palette;
244

245 246
  g_hash_table_foreach (table, gimp_palette_import_create_list, &list);
  list = g_slist_sort (list, gimp_palette_import_sort_colors);
247

248
  g_object_set_data (G_OBJECT (palette), "import-n-colors",
249
                     GINT_TO_POINTER (n_colors));
250

251
  g_slist_foreach (list, gimp_palette_import_create_image_palette, palette);
252

253
  g_object_set_data (G_OBJECT (palette), "import-n-colors", NULL);
254 255 256 257 258

  /*  Free up used memory
   *  Note the same structure is on both the hash list and the sorted
   *  list. So only delete it once.
   */
259
  g_hash_table_destroy (table);
260

261 262
  for (iter = list; iter; iter = iter->next)
    g_slice_free (ImgColors, iter->data);
263

264
  g_slist_free (list);
265 266 267 268

  return palette;
}

269 270
static GHashTable *
gimp_palette_import_extract (GimpImage     *image,
271 272 273 274
                             GimpPickable  *pickable,
                             gint           pickable_off_x,
                             gint           pickable_off_y,
                             gboolean       selection_only,
275 276 277 278 279 280
                             gint           x,
                             gint           y,
                             gint           width,
                             gint           height,
                             gint           n_colors,
                             gint           threshold)
281
{
282 283 284 285 286 287 288 289 290 291
  GeglBuffer         *buffer;
  GeglBufferIterator *iter;
  GeglRectangle      *mask_roi = NULL;
  GeglRectangle       rect     = { x, y, width, height };
  GHashTable         *colors   = NULL;
  const Babl         *format;
  gint                bpp;
  gint                mask_bpp = 0;

  buffer = gimp_pickable_get_buffer (pickable);
292
  format = babl_format ("R'G'B'A u8");
293

294
  iter = gegl_buffer_iterator_new (buffer, &rect, 0, format,
295
                                   GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
296
  bpp = babl_format_get_bytes_per_pixel (format);
297

298 299 300
  if (selection_only &&
      ! gimp_channel_is_empty (gimp_image_get_mask (image)))
    {
301 302
      GimpDrawable *mask = GIMP_DRAWABLE (gimp_image_get_mask (image));

303 304 305
      rect.x = x + pickable_off_x;
      rect.y = y + pickable_off_y;

306 307
      buffer = gimp_drawable_get_buffer (mask);
      format = babl_format ("Y u8");
308

309
      gegl_buffer_iterator_add (iter, buffer, &rect, 0, format,
310
                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
311
      mask_roi = &iter->items[1].roi;
312
      mask_bpp = babl_format_get_bytes_per_pixel (format);
313 314
    }

315
  while (gegl_buffer_iterator_next (iter))
316
    {
317
      const guchar *data      = iter->items[0].data;
318
      const guchar *mask_data = NULL;
319
      gint          length    = iter->length;
320

321
      if (mask_roi)
322
        mask_data = iter->items[1].data;
323

324
      while (length--)
325
        {
326 327
          /*  ignore unselected, and completely transparent pixels  */
          if ((! mask_data || *mask_data) && data[ALPHA])
328
            {
329 330
              guchar rgba[MAX_CHANNELS]     = { 0, };
              guchar rgb_real[MAX_CHANNELS] = { 0, };
331

332 333
              memcpy (rgba, data, 4);
              memcpy (rgb_real, rgba, 4);
334

335 336 337
              rgba[0] = (rgba[0] / threshold) * threshold;
              rgba[1] = (rgba[1] / threshold) * threshold;
              rgba[2] = (rgba[2] / threshold) * threshold;
338

339 340 341
              colors = gimp_palette_import_store_colors (colors,
                                                         rgba, rgb_real,
                                                         n_colors);
342
            }
343

344
          data += bpp;
345 346

          if (mask_data)
347
            mask_data += mask_bpp;
348
        }
349 350
    }

351 352 353 354
  return colors;
}

GimpPalette *
355
gimp_palette_import_from_image (GimpImage   *image,
356
                                GimpContext *context,
357 358
                                const gchar *palette_name,
                                gint         n_colors,
359 360
                                gint         threshold,
                                gboolean     selection_only)
361
{
362 363 364
  GHashTable *colors;
  gint        x, y;
  gint        width, height;
365

366
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
367
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
368 369 370 371
  g_return_val_if_fail (palette_name != NULL, NULL);
  g_return_val_if_fail (n_colors > 1, NULL);
  g_return_val_if_fail (threshold > 0, NULL);

372
  gimp_pickable_flush (GIMP_PICKABLE (image));
373

374 375
  if (selection_only)
    {
376 377
      gimp_item_bounds (GIMP_ITEM (gimp_image_get_mask (image)),
                        &x, &y, &width, &height);
378 379 380 381 382
    }
  else
    {
      x      = 0;
      y      = 0;
383 384
      width  = gimp_image_get_width  (image);
      height = gimp_image_get_height (image);
385
    }
386

387
  colors = gimp_palette_import_extract (image,
388
                                        GIMP_PICKABLE (image),
389 390 391
                                        0, 0,
                                        selection_only,
                                        x, y, width, height,
392 393
                                        n_colors, threshold);

394 395
  return gimp_palette_import_make_palette (colors, palette_name, context,
                                           n_colors);
396 397
}

398

399 400 401
/*  create a palette from an indexed image  **********************************/

GimpPalette *
402
gimp_palette_import_from_indexed_image (GimpImage   *image,
403
                                        GimpContext *context,
404
                                        const gchar *palette_name)
405
{
406 407 408 409 410
  GimpPalette  *palette;
  const guchar *colormap;
  guint         n_colors;
  gint          count;
  GimpRGB       color;
411

412
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
413
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
414
  g_return_val_if_fail (gimp_image_get_base_type (image) == GIMP_INDEXED, NULL);
415 416
  g_return_val_if_fail (palette_name != NULL, NULL);

417
  palette = GIMP_PALETTE (gimp_palette_new (context, palette_name));
418

419 420 421 422
  colormap = gimp_image_get_colormap (image);
  n_colors = gimp_image_get_colormap_size (image);

  for (count = 0; count < n_colors; ++count)
423
    {
424 425 426 427
      gchar name[256];

      g_snprintf (name, sizeof (name), _("Index %d"), count);

428
      gimp_rgba_set_uchar (&color,
429 430 431
                           colormap[count * 3 + 0],
                           colormap[count * 3 + 1],
                           colormap[count * 3 + 2],
432
                           255);
433

434
      gimp_palette_add_entry (palette, -1, name, &color);
435 436 437 438
    }

  return palette;
}
439

440

441 442 443 444
/*  create a palette from a drawable  ****************************************/

GimpPalette *
gimp_palette_import_from_drawable (GimpDrawable *drawable,
445
                                   GimpContext  *context,
446 447
                                   const gchar  *palette_name,
                                   gint          n_colors,
448 449
                                   gint          threshold,
                                   gboolean      selection_only)
450 451 452 453
{
  GHashTable *colors = NULL;
  gint        x, y;
  gint        width, height;
454
  gint        off_x, off_y;
455 456

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
457
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
458 459 460 461 462
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
  g_return_val_if_fail (palette_name != NULL, NULL);
  g_return_val_if_fail (n_colors > 1, NULL);
  g_return_val_if_fail (threshold > 0, NULL);

463
  if (selection_only)
464
    {
465 466
      if (! gimp_item_mask_intersect (GIMP_ITEM (drawable),
                                      &x, &y, &width, &height))
467
        return NULL;
468
    }
469 470 471 472
  else
    {
      x      = 0;
      y      = 0;
473 474
      width  = gimp_item_get_width  (GIMP_ITEM (drawable));
      height = gimp_item_get_height (GIMP_ITEM (drawable));
475 476
    }

477
  gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
478 479 480 481 482 483 484 485

  colors =
    gimp_palette_import_extract (gimp_item_get_image (GIMP_ITEM (drawable)),
                                 GIMP_PICKABLE (drawable),
                                 off_x, off_y,
                                 selection_only,
                                 x, y, width, height,
                                 n_colors, threshold);
486

487 488
  return gimp_palette_import_make_palette (colors, palette_name, context,
                                           n_colors);
489
}
490

491

492 493
/*  create a palette from a file  **********************************/

494
GimpPalette *
495
gimp_palette_import_from_file (GimpContext  *context,
496
                               GFile        *file,
497 498 499
                               const gchar  *palette_name,
                               GError      **error)
{
500 501 502
  GList        *palette_list = NULL;
  GInputStream *input;
  GError       *my_error = NULL;
503

504
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
505
  g_return_val_if_fail (G_IS_FILE (file), NULL);
506 507 508
  g_return_val_if_fail (palette_name != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

509 510 511 512 513 514 515 516 517 518
  input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));
  if (! input)
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
                   _("Could not open '%s' for reading: %s"),
                   gimp_file_get_utf8_name (file), my_error->message);
      g_clear_error (&my_error);
      return NULL;
    }

519
  switch (gimp_palette_load_detect_format (file, input))
520
    {
521
    case GIMP_PALETTE_FILE_FORMAT_GPL:
522
      palette_list = gimp_palette_load (context, file, input, error);
523 524 525
      break;

    case GIMP_PALETTE_FILE_FORMAT_ACT:
526
      palette_list = gimp_palette_load_act (context, file, input, error);
527 528
      break;

529
    case GIMP_PALETTE_FILE_FORMAT_RIFF_PAL:
530
      palette_list = gimp_palette_load_riff (context, file, input, error);
531 532
      break;

533
    case GIMP_PALETTE_FILE_FORMAT_PSP_PAL:
534
      palette_list = gimp_palette_load_psp (context, file, input, error);
535 536
      break;

537
    case GIMP_PALETTE_FILE_FORMAT_ACO:
538
      palette_list = gimp_palette_load_aco (context, file, input, error);
539 540
      break;

541
    case GIMP_PALETTE_FILE_FORMAT_CSS:
542
      palette_list = gimp_palette_load_css (context, file, input, error);
543 544
      break;

545
    default:
546
      g_set_error (error,
547 548
                   GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                   _("Unknown type of palette file: %s"),
549
                   gimp_file_get_utf8_name (file));
550
      break;
551 552
    }

553
  g_object_unref (input);
554

555 556 557
  if (palette_list)
    {
      GimpPalette *palette = g_object_ref (palette_list->data);
558

559 560
      gimp_object_set_name (GIMP_OBJECT (palette), palette_name);

561
      g_list_free_full (palette_list, (GDestroyNotify) g_object_unref);
562

563 564 565 566 567
      return palette;
    }

  return NULL;
}