File: colormap-remap.c

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; 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 (821 lines) | stat: -rw-r--r-- 28,071 bytes parent folder | download
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* 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/>.
 */

/*
 * Colormap remapping plug-in
 * Copyright (C) 2006 Mukund Sivaraman <muks@mukund.org>
 *
 * This plug-in takes the colormap and lets you move colors from one index
 * to another while keeping the original image visually unmodified.
 *
 * Such functionality is useful for creating graphics files for applications
 * which expect certain indices to contain some specific colors.
 *
 */

#include "config.h"

#include <string.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "libgimp/stdplugins-intl.h"

/* Magic numbers */

#define PLUG_IN_PROC_REMAP  "plug-in-colormap-remap"
#define PLUG_IN_PROC_SWAP   "plug-in-colormap-swap"
#define PLUG_IN_BINARY      "colormap-remap"
#define PLUG_IN_ROLE        "gimp-colormap-remap"

typedef struct _Remap Remap;
typedef struct _RemapClass RemapClass;

struct _Remap
{
  GimpPlugIn parent_instance;
};

struct _RemapClass
{
  GimpPlugInClass parent_class;
};

#define REMAP_TYPE  (remap_get_type ())
#define REMAP (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), REMAP_TYPE, Remap))

GType                   remap_get_type         (void) G_GNUC_CONST;
static GList          * remap_query_procedures (GimpPlugIn           *plug_in);
static GimpProcedure  * remap_create_procedure (GimpPlugIn           *plug_in,
                                                const gchar          *name);

static GimpValueArray * remap_run              (GimpProcedure        *procedure,
                                                GimpRunMode           run_mode,
                                                GimpImage            *image,
                                                GimpDrawable        **drawables,
                                                GimpProcedureConfig  *config,
                                                gpointer              run_data);

static gboolean         remap_dialog           (GimpProcedure        *procedure,
                                                GObject              *config,
                                                GimpImage            *image);

static gboolean         read_image_palette     (GimpImage            *image,
                                                gint                 *ncolors_out);

static GtkWidget *      create_icon_view       (gint                  col_count,
                                                gint                 *item_width,
                                                gint                 *item_height);

static GtkWidget *      create_scrolled_window (GtkWidget            *dialog,
                                                gint                  item_width,
                                                gint                  item_height,
                                                gint                  ncolors,
                                                gint                  col_count);

static void             remap_sort             (gint             column,
                                                GtkSortType      order);
static void             remap_sort_hue         (GtkButton       *action,
                                                gpointer         user_data);
static void             remap_sort_sat         (GtkButton       *action,
                                                gpointer         user_data);
static void             remap_sort_val         (GtkButton       *action,
                                                gpointer        user_data);
static void             remap_sort_index       (GtkButton       *action,
                                                gpointer         user_data);
static void             remap_reverse_order    (GtkButton       *action,
                                                gpointer         user_data);

static gboolean         real_remap             (GimpImage       *image);

static gboolean         check_index_map        (GimpImage *image);

static void             populate_index_map_from_store (void);

static void             store_last_vals (GimpProcedureConfig *config);
static void             read_last_vals  (GimpProcedureConfig *config);


G_DEFINE_TYPE (Remap, remap, GIMP_TYPE_PLUG_IN)

GIMP_MAIN (REMAP_TYPE)
DEFINE_STD_SET_I18N


enum
{
  COLOR_INDEX,
  COLOR_INDEX_TEXT,
  COLOR_RGB,
  COLOR_H,
  COLOR_S,
  COLOR_V,
  NUM_COLS
};


static GtkListStore *store; /* To store the color indices and other data */
static gint          reverse_order[256]; /* store indices to reverse order of icons*/
static guchar        index_map_old_new[256]; /* store remapped indices */

static void
remap_class_init (RemapClass *klass)
{
  GimpPlugInClass *plug_in_class  = GIMP_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = remap_query_procedures;
  plug_in_class->create_procedure = remap_create_procedure;
  plug_in_class->set_i18n         = STD_SET_I18N;
}

static void
remap_init (Remap *remap)
{
}

static GList *
remap_query_procedures (GimpPlugIn *plug_in)
{
  GList *list = NULL;

  list = g_list_prepend (list, g_strdup (PLUG_IN_PROC_REMAP));
  list = g_list_prepend (list, g_strdup (PLUG_IN_PROC_SWAP));

  return list;
}

static GimpProcedure *
remap_create_procedure (GimpPlugIn  *plug_in,
                        const gchar *name)
{
  GimpProcedure *procedure = NULL;

  if (strcmp (name, PLUG_IN_PROC_REMAP) == 0)
    {
      procedure = gimp_image_procedure_new (plug_in, name,
                                            GIMP_PDB_PROC_TYPE_PLUGIN,
                                            remap_run, NULL, NULL);

      gimp_procedure_set_image_types (procedure, "INDEXED*");
      gimp_procedure_set_sensitivity_mask (procedure,
                                           GIMP_PROCEDURE_SENSITIVE_DRAWABLE  |
                                           GIMP_PROCEDURE_SENSITIVE_DRAWABLES |
                                           GIMP_PROCEDURE_SENSITIVE_NO_DRAWABLES);

      gimp_procedure_set_menu_label (procedure, _("R_earrange Colormap..."));
      gimp_procedure_set_icon_name (procedure, GIMP_ICON_COLORMAP);
      gimp_procedure_add_menu_path (procedure, "<Image>/Colors/Map/[Colormap]");
      gimp_procedure_add_menu_path (procedure, "<Colormap>/Colormap Menu");

      gimp_procedure_set_documentation (procedure,
                                        _("Rearrange the colormap"),
                                        _("This procedure takes an indexed "
                                          "image and lets you alter the "
                                          "positions of colors in the colormap "
                                          "without visually changing the image."),
                                        name);
      gimp_procedure_set_attribution (procedure,
                                      "Mukund Sivaraman <muks@mukund.org>",
                                      "Mukund Sivaraman <muks@mukund.org>",
                                      "June 2006");

      gimp_procedure_add_bytes_argument (procedure, "map",
                                         _("Map"),
                                         _("Remap array for the colormap"),
                                         G_PARAM_READWRITE);
    }
  else if (strcmp (name, PLUG_IN_PROC_SWAP) == 0)
    {
      procedure = gimp_image_procedure_new (plug_in, name,
                                            GIMP_PDB_PROC_TYPE_PLUGIN,
                                            remap_run, NULL, NULL);

      gimp_procedure_set_image_types (procedure, "INDEXED*");
      gimp_procedure_set_sensitivity_mask (procedure,
                                           GIMP_PROCEDURE_SENSITIVE_DRAWABLE  |
                                           GIMP_PROCEDURE_SENSITIVE_DRAWABLES |
                                           GIMP_PROCEDURE_SENSITIVE_NO_DRAWABLES);

      gimp_procedure_set_menu_label (procedure, _("_Swap Colors"));
      gimp_procedure_set_icon_name (procedure, GIMP_ICON_COLORMAP);

      gimp_procedure_set_documentation (procedure,
                                        _("Swap two colors in the colormap"),
                                        _("This procedure takes an indexed "
                                          "image and lets you swap the "
                                          "positions of two colors in the "
                                          "colormap without visually changing "
                                          "the image."),
                                        name);
      gimp_procedure_set_attribution (procedure,
                                      "Mukund Sivaraman <muks@mukund.org>",
                                      "Mukund Sivaraman <muks@mukund.org>",
                                      "June 2006");

      gimp_procedure_add_int_argument (procedure, "index1",
                                       _("Index 1"),
                                       _("First index in the colormap"),
                                       0, 255, 0,
                                       G_PARAM_READWRITE);

      gimp_procedure_add_int_argument (procedure, "index2",
                                       _("Index 2"),
                                       _("Second (other) index in the colormap"),
                                       0, 255, 0,
                                       G_PARAM_READWRITE);
    }

  return procedure;
}

static GimpValueArray *
remap_run (GimpProcedure        *procedure,
           GimpRunMode           run_mode,
           GimpImage            *image,
           GimpDrawable        **drawables,
           GimpProcedureConfig  *config,
           gpointer              run_data)
{
  gint i;

  gegl_init (NULL, NULL);

  for (i = 0; i < 256; ++i)
    {
      index_map_old_new[i] = i;
    }

  if (gimp_image_get_base_type (image) != GIMP_INDEXED)
    {
      GError *error;
      g_set_error (&error, GIMP_PLUG_IN_ERROR, 0,
                   _("Procedure '%s' only works with indexed images."),
                   gimp_procedure_get_name (procedure));

      return gimp_procedure_new_return_values (procedure,
                                               GIMP_PDB_EXECUTION_ERROR,
                                               error);
    }

  if (strcmp (gimp_procedure_get_name (procedure), PLUG_IN_PROC_SWAP) == 0)
    {
      gint          index1;
      gint          index2;
      gint          temp;
      GimpPalette  *palette;
      gint          ncolors;

      if (run_mode != GIMP_RUN_NONINTERACTIVE)
        {
          return gimp_procedure_new_return_values (procedure,
                                                   GIMP_PDB_CALLING_ERROR,
                                                   NULL);
        }

      g_object_get (config,
                    "index1", &index1,
                    "index2", &index2,
                    NULL);

      palette = gimp_image_get_palette       (image);
      ncolors = gimp_palette_get_color_count (palette);

      if (index1 >= ncolors || index2 >= ncolors
          || index1 < 0 || index2 < 0)
        {
          return gimp_procedure_new_return_values (procedure,
                                                   GIMP_PDB_CALLING_ERROR,
                                                   NULL);
        }

      temp = index_map_old_new[index1];
      index_map_old_new[index1] = index_map_old_new[index2];
      index_map_old_new[index2] = temp;

      real_remap (image);
      return gimp_procedure_new_return_values (procedure,
                                               GIMP_PDB_SUCCESS,
                                               NULL);
    }

  if (run_mode == GIMP_RUN_NONINTERACTIVE ||
      run_mode == GIMP_RUN_WITH_LAST_VALS)
    {
      read_last_vals (config);
      if (! check_index_map (image))
        {
          return gimp_procedure_new_return_values (procedure,
                                                   GIMP_PDB_CALLING_ERROR,
                                                   NULL);
        }
      real_remap (image);
      gimp_displays_flush ();
      return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
    }

  if (run_mode == GIMP_RUN_INTERACTIVE &&
      remap_dialog (procedure, G_OBJECT (config), image))
    {
      populate_index_map_from_store ();
      real_remap (image);
      store_last_vals (config);
      gimp_displays_flush ();
      return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
    }

  return gimp_procedure_new_return_values (procedure, GIMP_PDB_CANCEL, NULL);
}

static gboolean
check_index_map (GimpImage *image)
{
  gint         i;
  gint         ncolors;
  GimpPalette *palette;

  palette = gimp_image_get_palette       (image);
  ncolors = gimp_palette_get_color_count (palette);

  for (i = 0; i < ncolors; ++i)
    {
      if (index_map_old_new[i] >= ncolors)
        {
          return FALSE;
        }
    }
  return TRUE;
}


static void
read_last_vals (GimpProcedureConfig *config)
{
  const guchar *last_vals;

  GBytes *bytes;
  gint    i;

  g_object_get (G_OBJECT (config), "map", &bytes, NULL);
  last_vals = g_bytes_get_data (bytes, NULL);

  if (bytes != NULL)
    {
      for (i = 0; i < 256; ++i)
        {
          index_map_old_new[i] = last_vals[i];
        }
    }

  g_bytes_unref (bytes);
}

static void
store_last_vals (GimpProcedureConfig *config)
{
  GBytes *bytes;
  bytes = g_bytes_new (&index_map_old_new, sizeof (guchar) * 256);
  g_object_set (G_OBJECT (config), "map", bytes, NULL);
  g_bytes_unref (bytes);
}


static gboolean
read_image_palette (GimpImage *image,
                    gint      *ncolors_out)
{
  GtkTreeIter   iter;
  gint          index;
  gint          ncolors;
  GimpPalette  *palette;
  GeglColor   **colors;

  palette = gimp_image_get_palette       (image);
  ncolors = gimp_palette_get_color_count (palette);

  g_return_val_if_fail ((ncolors > 0) && (ncolors <= 256), FALSE);

  colors = gimp_palette_get_colors (palette);
  for (index = 0; index < ncolors; ++index)
    {
      GeglColor *c   = colors[index];
      gfloat     hsv[3];
      gchar     *text = g_strdup_printf ("%d", index);

      gegl_color_get_pixel (c, babl_format ("HSV float"), hsv);

      reverse_order[index] = ncolors - index - 1;

      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          COLOR_INDEX,       index,
                          COLOR_INDEX_TEXT,  text,
                          COLOR_RGB,         c,
                          COLOR_H,           hsv[0],
                          COLOR_S,           hsv[1],
                          COLOR_V,           hsv[2],
                          -1);
      g_free (text);
    }
  gimp_color_array_free (colors);

  if (ncolors_out != NULL)
    {
      *ncolors_out = ncolors;
    }

  return TRUE;
}

static GtkWidget *
create_icon_view (gint  col_count,
                  gint *item_width,
                  gint *item_height)
{
  GtkWidget       *iv;
  GtkCellRenderer *renderer;
  gint             icon_width;
  gint             icon_height;
  GtkIconSize      icon_size    = GTK_ICON_SIZE_LARGE_TOOLBAR;
  gint             item_padding = 5;
  gint             row_spacing  = 2;
  gint             col_spacing  = 2;
  gint             text_height  = 21;

  gtk_icon_size_lookup (icon_size, &icon_width, &icon_height);

  *item_width  = icon_width + item_padding * 2 + row_spacing + col_spacing;

  *item_height = icon_height + text_height + item_padding * 2;

  iv = gtk_icon_view_new_with_model (GTK_TREE_MODEL (store));

  gtk_icon_view_set_selection_mode   (GTK_ICON_VIEW (iv), GTK_SELECTION_SINGLE);
  gtk_icon_view_set_item_orientation (GTK_ICON_VIEW (iv), GTK_ORIENTATION_VERTICAL);
  gtk_icon_view_set_item_padding     (GTK_ICON_VIEW (iv), item_padding);
  gtk_icon_view_set_columns          (GTK_ICON_VIEW (iv), col_count);
  gtk_icon_view_set_row_spacing      (GTK_ICON_VIEW (iv), row_spacing);
  gtk_icon_view_set_column_spacing   (GTK_ICON_VIEW (iv), col_spacing);
  gtk_icon_view_set_reorderable      (GTK_ICON_VIEW (iv), TRUE);

  gtk_widget_set_can_focus (iv, TRUE);

  renderer = gimp_cell_renderer_color_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (iv), renderer, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (iv), renderer,
                                  "color", COLOR_RGB,
                                  NULL);
  g_object_set (renderer,
                "icon-size", icon_size,
                "xpad",      0,
                "ypad",      0,
                NULL);

  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (iv), renderer, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (iv), renderer,
                                  "text", COLOR_INDEX_TEXT,
                                  NULL);
  g_object_set (renderer,
                "size-points", 9.0,   /*TODO: should it be scaled with the UI scale ? */
                "xalign",      0.5,
                NULL);
  return iv;
}

static GtkWidget *
create_scrolled_window (GtkWidget *dialog,
                        gint       item_width,
                        gint       item_height,
                        gint       ncolors,
                        gint       col_count)
{
  GtkWidget *sw;
  gint       nrows;
  gint       widget_padding = 6 * 2;

  nrows = MIN (ncolors / col_count + 1, 8);
  if (ncolors > 99)
    item_width += 4; /* take into account increased text width when indices > 99 */

  /* last parameter is intentionally set to 'dummy'
   * so we can create scrolled window before the container contents.
   * The window contents will be inserted at the later step */
  sw = gimp_procedure_dialog_fill_scrolled_window (GIMP_PROCEDURE_DIALOG (dialog), "sw",
                                                   "intentional-dummy");

  gtk_widget_set_size_request (sw, item_width * col_count, -1);
  gtk_widget_set_vexpand (sw, TRUE);

  gtk_scrolled_window_set_min_content_width  (GTK_SCROLLED_WINDOW (sw),
                                              item_width * col_count);
  gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (sw),
                                              item_height * nrows + widget_padding);
  return sw;
}

static void
remap_sort (gint             column,
            GtkSortType      order)
{
#define REMAP_COL_ID GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), column, order);
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), REMAP_COL_ID, 0);
#undef REMAP_COL_ID
}

static void
remap_sort_hue (GtkButton *button,
                gpointer  user_data)
{
  remap_sort (COLOR_H, GTK_SORT_ASCENDING);
}

static void
remap_sort_sat (GtkButton *button,
                gpointer  user_data)
{
  remap_sort (COLOR_S, GTK_SORT_ASCENDING);
}

static void
remap_sort_val (GtkButton *button,
                gpointer   user_data)
{
  remap_sort (COLOR_V, GTK_SORT_ASCENDING);
}

static void
remap_sort_index (GtkButton *button,
                  gpointer   user_data)
{
  remap_sort (COLOR_INDEX, GTK_SORT_ASCENDING);
}

static void
remap_reverse_order (GtkButton *button,
                     gpointer   user_data)
{
  gtk_list_store_reorder (store, reverse_order);
}


static gboolean
remap_dialog (GimpProcedure *procedure,
              GObject       *config,
              GimpImage     *image)
{
  GtkWidget *dialog;
  GtkWidget *button;
  GtkWidget *w;
  GtkWidget *hintbox;
  GtkWidget *box;
  GtkWidget *box2;
  GtkWidget *icons;
  gboolean   run;
  gint       ncolors;
  gint       item_width;
  gint       item_height;
  gint       col_count = 16;

  gimp_ui_init (PLUG_IN_BINARY);


  dialog = gimp_procedure_dialog_new (procedure,
                                      GIMP_PROCEDURE_CONFIG (config),
                                      _("Rearrange Colors"));

  gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                            GTK_RESPONSE_OK,
                                            GTK_RESPONSE_CANCEL,
                                            -1);
  gimp_window_set_transient (GTK_WINDOW (dialog));

  store = gtk_list_store_new (NUM_COLS,
                              G_TYPE_INT, G_TYPE_STRING, GEGL_TYPE_COLOR,
                              G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE);

  if (! read_image_palette (image, &ncolors))
    {
      return FALSE;
    }

  icons = create_icon_view (col_count, &item_width, &item_height);

  /* scrolled window widget name is "sw" */
  w = create_scrolled_window (dialog, item_width, item_height, ncolors, col_count);
  gtk_container_add (GTK_CONTAINER (w), icons);

  /* "dummy" is intentional. so we can "fill the box" and then actually fill it with our controls */
  box2 = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (dialog), "buttonbox",
                                         "intentional-dummy", NULL);
  gtk_orientable_set_orientation (GTK_ORIENTABLE (box2),
                                  GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_margin_bottom (box2, 12);
  gtk_widget_set_margin_top    (box2, 12);

  button = gtk_button_new_with_label (_("Sort on Hue"));
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, FALSE, 0);
  g_signal_connect (button, "clicked",
                    G_CALLBACK (remap_sort_hue),
                    NULL);

  button = gtk_button_new_with_label (_("Sort on Saturation"));
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, FALSE, 0);
  g_signal_connect (button, "clicked",
                    G_CALLBACK (remap_sort_sat),
                    NULL);

  button = gtk_button_new_with_label (_("Sort on Value"));
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, FALSE, 0);
  g_signal_connect (button, "clicked",
                    G_CALLBACK (remap_sort_val),
                    NULL);

  button = gtk_button_new_with_label (_("Reverse Order"));
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, FALSE, 0);
  g_signal_connect (button, "clicked",
                    G_CALLBACK (remap_reverse_order),
                    NULL);

  button = gtk_button_new_with_label (_("Reset Order"));
  gtk_box_pack_start (GTK_BOX (box2), button, TRUE, FALSE, 0);
  g_signal_connect (button, "clicked",
                    G_CALLBACK (remap_sort_index),
                    NULL);

  box = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (dialog), "vbox",
                                        "sw", "buttonbox", NULL);

  hintbox = gimp_hint_box_new (_("Drag and drop colors to rearrange the colormap.\n"
                                 "The numbers shown are the original indices."));
  gtk_box_pack_start (GTK_BOX (box), hintbox, FALSE, FALSE, 0);

  gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dialog),
                               "vbox", NULL);

  gtk_widget_show_all (dialog);

  run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));

  gtk_widget_destroy (dialog);
  return run;
}

static void
populate_index_map_from_store (void)
{
  GtkTreeIter iter;
  gint        old_index = 0;
  gboolean    valid;

  for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
       valid;
       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter))
    {
      gint new_index;

      gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
                          COLOR_INDEX, &new_index,
                          -1);
      index_map_old_new[old_index] = new_index;
      ++old_index;
    }
}

static gboolean
real_remap (GimpImage *image)
{
  guchar        pixmap[256]; /* map new-pixel index to old. pixmap[new-index] = old-index */
  gint          i;
  gint          ncolors;
  GimpPalette  *palette;
  GeglColor   **colors;
  GList        *layers;
  GList        *list;
  glong         total_pixels = 0;
  glong         processed    = 0;

  palette = gimp_image_get_palette       (image);
  ncolors = gimp_palette_get_color_count (palette);
  colors  = gimp_palette_get_colors      (palette);

  gimp_image_undo_group_start (image);
  for (i = 0; i < ncolors; ++i)
    {
      gint new_index    = index_map_old_new[i];
      pixmap[new_index] = i;
      gimp_palette_set_entry_color (palette, i, colors[new_index]);
    }
  gimp_color_array_free (colors);

  gimp_progress_init (_("Rearranging the colormap"));
  layers = gimp_image_list_layers (image);

  /**  There is no needs to process the layers recursively, because
   *  indexed images cannot have layer groups.
   */
  for (list = layers; list; list = list->next)
    {
      total_pixels += gimp_drawable_get_width (list->data) *
                      gimp_drawable_get_height (list->data);
    }

  for (list = layers; list; list = list->next)
    {
      GeglBuffer         *buffer;
      GeglBuffer         *shadow;
      const Babl         *format;
      GeglBufferIterator *iter;
      GeglRectangle      *src_roi;
      GeglRectangle      *dest_roi;
      gint                width, height, bpp;
      gint                update = 0;

      buffer = gimp_drawable_get_buffer        (list->data);
      shadow = gimp_drawable_get_shadow_buffer (list->data);

      width  = gegl_buffer_get_width  (buffer);
      height = gegl_buffer_get_height (buffer);
      format = gegl_buffer_get_format (buffer);
      bpp    = babl_format_get_bytes_per_pixel (format);

      iter = gegl_buffer_iterator_new (buffer,
                                       GEGL_RECTANGLE (0, 0, width, height), 0,
                                       format,
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
      src_roi = &iter->items[0].roi;

      gegl_buffer_iterator_add (iter, shadow,
                                GEGL_RECTANGLE (0, 0, width, height), 0,
                                format,
                                GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
      dest_roi = &iter->items[1].roi;

      while (gegl_buffer_iterator_next (iter))
        {
          const guchar *src_row  = iter->items[0].data;
          guchar       *dest_row = iter->items[1].data;
          gint          y;

          for (y = 0; y < src_roi->height; y++)
            {
              const guchar *src  = src_row;
              guchar       *dest = dest_row;
              gint          x;

              if (bpp == 1)
                {
                  for (x = 0; x < src_roi->width; x++)
                    *dest++ = pixmap[*src++];
                }
              else
                {
                  for (x = 0; x < src_roi->width; x++)
                    {
                      *dest++ = pixmap[*src++];
                      *dest++ = *src++;
                    }
                }

              src_row  += src_roi->width  * bpp;
              dest_row += dest_roi->width * bpp;
            }

          processed += src_roi->width * src_roi->height;
          update %= 16;

          if (update == 0)
            {
              gimp_progress_update ((gdouble) processed / total_pixels);
            }

          update++;
        }

      g_object_unref (buffer);
      g_object_unref (shadow);

      gimp_drawable_merge_shadow (list->data, TRUE);
      gimp_drawable_update (list->data, 0, 0, width, height);
    }

  gimp_image_undo_group_end (image);
  g_list_free (layers);
  gimp_progress_update (1.0);
  return TRUE;
}