convert-dialog.c 17.1 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
/* The GIMP -- an 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
25
#include "gui-types.h"
26

27
#include "core/gimp.h"
28 29 30 31 32 33
#include "core/gimpdatafactory.h"
#include "core/gimpimage.h"
#include "core/gimpimage-convert.h"
#include "core/gimplist.h"
#include "core/gimppalette.h"

Nate Summers's avatar
Nate Summers committed
34
#include "libgimptool/gimptool.h"
35 36
#include "tools/tool_manager.h"

37 38
#include "widgets/gimpenummenu.h"

39 40 41 42 43 44 45
#include "palette-select.h"

#include "libgimp/gimpintl.h"


typedef struct
{
46 47 48 49 50 51 52 53 54 55
  GtkWidget              *shell;
  GtkWidget              *custom_palette_button;
  GimpImage              *gimage;
  PaletteSelect          *palette_select;
  GimpConvertDitherType   dither_type;
  gboolean                alpha_dither;
  gboolean                remove_dups;
  gint                    num_colors;
  gint                    palette;
  GimpConvertPaletteType  palette_type;
56 57 58 59 60 61 62 63 64 65 66 67 68
} IndexedDialog;


static void indexed_ok_callback                     (GtkWidget *widget,
						     gpointer   data);
static void indexed_cancel_callback                 (GtkWidget *widget,
						     gpointer   data);

static void indexed_custom_palette_button_callback  (GtkWidget *widget,
						     gpointer   data);
static void indexed_palette_select_destroy_callback (GtkWidget *widget,
						     gpointer   data);

Michael Natterer's avatar
Michael Natterer committed
69
static GtkWidget * build_palette_button             (Gimp      *gimp);
70 71 72 73 74 75


static gboolean     UserHasWebPal    = FALSE;
static GimpPalette *theCustomPalette = NULL;

/* Defaults */
76 77 78 79 80
static GimpConvertDitherType   sdither_type  = GIMP_FS_DITHER;
static gboolean                salpha_dither = FALSE;
static gboolean                sremove_dups  = TRUE;
static gint                    snum_colors   = 256;
static GimpConvertPaletteType  spalette_type = GIMP_MAKE_PALETTE;
81 82 83 84 85


void
convert_to_rgb (GimpImage *gimage)
{
86 87
  g_return_if_fail (GIMP_IS_IMAGE (gimage));

88
  gimp_image_convert (gimage, GIMP_RGB, 0, 0, 0, 0, 0, NULL);
89
  gimp_image_flush (gimage);
90 91 92 93 94
}

void
convert_to_grayscale (GimpImage* gimage)
{
95 96
  g_return_if_fail (GIMP_IS_IMAGE (gimage));

97
  gimp_image_convert (gimage, GIMP_GRAY, 0, 0, 0, 0, 0, NULL);
98
  gimp_image_flush (gimage);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

void
convert_to_indexed (GimpImage *gimage)
{
  IndexedDialog *dialog;
  GtkWidget     *main_vbox;
  GtkWidget     *vbox;
  GtkWidget     *hbox;
  GtkWidget     *label;
  GtkObject     *adjustment;
  GtkWidget     *spinbutton;
  GtkWidget     *frame;
  GtkWidget     *toggle;
  GSList        *group = NULL;

115 116
  g_return_if_fail (GIMP_IS_IMAGE (gimage));

117 118 119 120
  dialog = g_new0 (IndexedDialog, 1);

  dialog->gimage = gimage;

121 122 123 124 125 126
  dialog->custom_palette_button = build_palette_button (gimage->gimp);
  dialog->dither_type           = sdither_type;
  dialog->alpha_dither          = salpha_dither;
  dialog->remove_dups           = sremove_dups;
  dialog->num_colors            = snum_colors;
  dialog->palette_type          = spalette_type;
127 128

  dialog->shell =
129 130 131 132 133 134 135
    gimp_viewable_dialog_new (GIMP_VIEWABLE (gimage),
                              _("Indexed Color Conversion"),
                              "indexed_color_conversion",
                              GIMP_STOCK_CONVERT_INDEXED,
                              _("Convert Image to Indexed Colors"),
                              gimp_standard_help_func,
                              "dialogs/convert_to_indexed.html",
136

137 138
                              GTK_STOCK_CANCEL, indexed_cancel_callback,
                              dialog, NULL, NULL, FALSE, TRUE,
139

140 141
                              GTK_STOCK_OK, indexed_ok_callback,
                              dialog, NULL, NULL, TRUE, FALSE,
142

143
                              NULL);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

  main_vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog->shell)->vbox),
		     main_vbox);
  gtk_widget_show (main_vbox);

  frame = gtk_frame_new (_("General Palette Options"));
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  vbox = gtk_vbox_new (FALSE, 1);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  /*  'generate palette'  */
  hbox = gtk_hbox_new (FALSE, 4);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

164 165
  toggle = gtk_radio_button_new_with_label (NULL, 
                                            _("Generate Optimum Palette:"));
166
  group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
167 168 169
  gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  gtk_widget_show (toggle);

170 171 172 173 174 175 176
  g_object_set_data (G_OBJECT (toggle), "gimp-item-data", 
                     GINT_TO_POINTER (GIMP_MAKE_PALETTE));
  g_signal_connect (G_OBJECT (toggle), "toggled",
		    G_CALLBACK (gimp_radio_button_update),
		    &dialog->palette_type);

  if (dialog->num_colors == 256 && gimp_image_has_alpha (gimage))
177
    {
178 179
      dialog->num_colors = 255;
    }      
180

181
  spinbutton = gimp_spin_button_new (&adjustment, dialog->num_colors,
182 183 184 185
				     2, 256, 1, 5, 0, 1, 0);
  gtk_box_pack_end (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

186 187
  g_signal_connect (G_OBJECT (adjustment), "value_changed",
		    G_CALLBACK (gimp_int_adjustment_update),
188
		    &dialog->num_colors);
189

190
  label = gtk_label_new (_("Number of Colors:"));
191 192 193
  gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

194 195
  gtk_widget_set_sensitive (GTK_WIDGET (spinbutton), dialog->num_colors);
  gtk_widget_set_sensitive (GTK_WIDGET (label), dialog->num_colors);
196 197
  g_object_set_data (G_OBJECT (toggle), "set_sensitive", spinbutton);
  g_object_set_data (G_OBJECT (spinbutton), "set_sensitive", label);
198 199 200

  gtk_widget_show (hbox);

201
  if (! UserHasWebPal)
202
    {
203 204 205 206 207 208 209 210 211 212 213 214
      /*  'web palette'
       * Only presented as an option to the user if they do not
       * already have the 'Web' GIMP palette installed on their
       * system.
       */
      hbox = gtk_hbox_new (FALSE, 0);
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
      gtk_widget_show (hbox);

      toggle = gtk_radio_button_new_with_label (group, 
                                                _("Use WWW-Optimized Palette"));
      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
215 216 217
      gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
      gtk_widget_show (toggle);

218 219
      g_object_set_data (G_OBJECT (toggle), "gimp-item-data",
                         GINT_TO_POINTER (GIMP_WEB_PALETTE));
220
      g_signal_connect (G_OBJECT (toggle), "toggled",
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
			G_CALLBACK (gimp_radio_button_update),
			&dialog->palette_type);
    }

  /*  'mono palette'  */
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  toggle = gtk_radio_button_new_with_label (group, 
                                            _("Use Black and White (1-Bit) Palette"));
  group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
  gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  gtk_widget_show (toggle);

  g_object_set_data (G_OBJECT (toggle), "gimp-item-data",
                     GINT_TO_POINTER (GIMP_MONO_PALETTE));
  g_signal_connect (G_OBJECT (toggle), "toggled",
		    G_CALLBACK (gimp_radio_button_update),
		    &dialog->palette_type);

  /* 'custom' palette from dialog */
  if (dialog->custom_palette_button)
    {
      GtkWidget *remove_toggle;

      remove_toggle = gtk_check_button_new_with_label (_("Remove Unused Colors from Final Palette"));
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (remove_toggle), 
                                    dialog->remove_dups);
      g_signal_connect (G_OBJECT (remove_toggle), "toggled",
251
			G_CALLBACK (gimp_toggle_button_update),
252
			&dialog->remove_dups);
253

254 255 256
      /* 'custom' palette from dialog */
      hbox = gtk_hbox_new (FALSE, 4);
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
257 258 259 260
      gtk_widget_show (hbox);

      toggle = gtk_radio_button_new_with_label (group, 
                                                _("Use Custom Palette:"));
261
      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
262 263
      gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
      gtk_widget_show (toggle);
264

265 266 267 268 269 270 271
      g_object_set_data (G_OBJECT (toggle), "gimp-item-data",
                         GINT_TO_POINTER (GIMP_CUSTOM_PALETTE));
      g_signal_connect (G_OBJECT (toggle), "toggled",
			G_CALLBACK (gimp_radio_button_update),
			&dialog->palette_type);
      g_object_set_data (G_OBJECT (toggle), "set_sensitive", remove_toggle);

272 273 274
      g_signal_connect (G_OBJECT (dialog->custom_palette_button), "clicked",
			G_CALLBACK (indexed_custom_palette_button_callback), 
			dialog);
275 276
      gtk_box_pack_end (GTK_BOX (hbox), 
                        dialog->custom_palette_button, TRUE, TRUE, 0);
277 278
      gtk_widget_show (dialog->custom_palette_button);

279 280 281 282 283 284
      gtk_widget_set_sensitive (remove_toggle,
				dialog->palette_type == GIMP_CUSTOM_PALETTE);
      gtk_widget_set_sensitive (dialog->custom_palette_button,
                                dialog->palette_type == GIMP_CUSTOM_PALETTE);
      g_object_set_data (G_OBJECT (toggle), "set_sensitive", remove_toggle);
      g_object_set_data (G_OBJECT (remove_toggle), "set_sensitive",
285
			 dialog->custom_palette_button);
286

287 288
      /*  add the remove-duplicates toggle  */
      hbox = gtk_hbox_new (FALSE, 4);      
289
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
290 291 292 293
      gtk_widget_show (hbox);      

      gtk_box_pack_start (GTK_BOX (hbox), remove_toggle, FALSE, FALSE, 20);
      gtk_widget_show (remove_toggle);
294 295
    }

296 297
  gimp_radio_group_set_active (GTK_RADIO_BUTTON (toggle),
                               GINT_TO_POINTER (dialog->dither_type));
298

299
  /*  the dither type  */
300
  frame = gtk_frame_new (_("Dithering Options"));
301 302 303
  gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

304 305 306 307 308 309
  vbox = gimp_enum_radio_box_new (GIMP_TYPE_CONVERT_DITHER_TYPE,
                                  G_CALLBACK (gimp_radio_button_update),
                                  &dialog->dither_type,
                                  &toggle);
  gimp_radio_group_set_active (GTK_RADIO_BUTTON (toggle),
                               GINT_TO_POINTER (dialog->dither_type));
310 311 312 313
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show(vbox);

314
  /*  the alpha-dither toggle  */
315 316 317 318
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

319 320
  toggle = 
    gtk_check_button_new_with_label (_("Enable Dithering of Transparency"));
321
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
322
				dialog->alpha_dither);
323 324
  gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  gtk_widget_show (toggle);
325 326
  g_signal_connect (G_OBJECT (toggle), "toggled",
		    G_CALLBACK (gimp_toggle_button_update),
327
		    &dialog->alpha_dither);
328 329 330 331
  
  /* if the image isn't non-alpha/layered, set the default number of
     colours to one less than max, to leave room for a transparent index
     for transparent/animated GIFs */
332
  if (gimp_image_has_alpha (gimage))
333 334 335 336 337 338 339 340 341 342 343
    {
      frame = gtk_frame_new (_("[ Warning ]"));
      gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
      gtk_widget_show (frame);

      vbox = gtk_vbox_new (FALSE, 1);
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
      gtk_container_add (GTK_CONTAINER (frame), vbox);
      gtk_widget_show (vbox);

      label = gtk_label_new
344
	(_("You are attempting to convert an image with an alpha channel "
345 346 347
           "to indexed colors.\n"
           "Do not generate a palette of more than 255 colors if you "
           "intend to create a transparent or animated GIF file."));
348 349 350 351 352 353 354 355 356 357 358
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
      gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
      gtk_container_add (GTK_CONTAINER (vbox), label);
      gtk_widget_show (label);
    }

  gtk_widget_show (dialog->shell);
}


static GtkWidget *
Michael Natterer's avatar
Michael Natterer committed
359
build_palette_button (Gimp *gimp)
360 361 362 363 364 365 366 367 368
{
  GList       *list;
  GimpPalette *palette;
  GimpPalette *theWebPalette = NULL;
  gint         i;
  gint         default_palette;

  UserHasWebPal = FALSE;

Michael Natterer's avatar
Michael Natterer committed
369
  list = GIMP_LIST (gimp->palette_factory->container)->list;
370 371 372 373 374 375 376 377 378 379 380 381 382 383

  if (! list)
    {
      return NULL;
    }

  for (i = 0, default_palette = -1;
       list;
       i++, list = g_list_next (list))
    {
      palette = (GimpPalette *) list->data;
      
      /* Preferentially, the initial default is 'Web' if available */
      if (theWebPalette == NULL &&
384
	  g_ascii_strcasecmp (GIMP_OBJECT (palette)->name, "Web") == 0)
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
	{
	  theWebPalette = palette;
	  UserHasWebPal = TRUE;
	}
      
      /* We can't dither to > 256 colors */
      if (palette->n_colors <= 256)
	{
	  if (theCustomPalette == palette)
	    {
	      default_palette = i;
	    }
	}
    }

  /* default to first one with <= 256 colors 
   * (only used if 'web' palette not available)
   */
   if (default_palette == -1)
     {
       if (theWebPalette)
	 {
	   theCustomPalette = theWebPalette;
	   default_palette = 1;  /*  dummy value  */
	 }
       else
	 {
Michael Natterer's avatar
Michael Natterer committed
412
	   for (i = 0, list = GIMP_LIST (gimp->palette_factory->container)->list;
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
		list && default_palette == -1;
		i++, list = g_list_next (list))
	     {
	       palette = (GimpPalette *) list->data;

	       if (palette->n_colors <= 256)
		 {
		   theCustomPalette = palette;
		   default_palette = i;
		 }
	     }
	 }
     }

   if (default_palette == -1)
     return NULL;
   else
     return gtk_button_new_with_label (GIMP_OBJECT (theCustomPalette)->name);
}


static void
indexed_ok_callback (GtkWidget *widget,
		     gpointer   data)
{
438 439
  IndexedDialog  *dialog;
  GimpTool       *active_tool;
440 441 442 443 444 445

  dialog = (IndexedDialog *) data;

  /* Close the dialogs when open because they're useless for indexed
   *  images and could crash the GIMP when used nevertheless
   */
446 447
  active_tool = tool_manager_get_active (dialog->gimage->gimp);

448
  if (active_tool)
449
    tool_manager_control_active (dialog->gimage->gimp, HALT, active_tool->gdisp);
450 451
  
  /*  Convert the image to indexed color  */
452 453 454 455 456 457 458 459
  gimp_image_convert (dialog->gimage, 
                      GIMP_INDEXED, 
                      dialog->num_colors,
		      dialog->dither_type, 
                      dialog->alpha_dither,
		      dialog->remove_dups, 
                      dialog->palette_type, 
                      theCustomPalette);
460
  gimp_image_flush (dialog->gimage);
461 462

  /* Save defaults for next time */
463 464 465 466 467
  sdither_type  = dialog->dither_type;
  salpha_dither = dialog->alpha_dither;
  sremove_dups  = dialog->remove_dups;
  snum_colors   = dialog->num_colors;
  spalette_type = dialog->palette_type;
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

  if (dialog->palette_select)
    gtk_widget_destroy (dialog->palette_select->shell);  

  gtk_widget_destroy (dialog->shell);

  g_free (dialog);
}

static void
indexed_cancel_callback (GtkWidget *widget,
			 gpointer   data)
{
  IndexedDialog *dialog;

  dialog = (IndexedDialog *) data;

  if (dialog->palette_select)
    gtk_widget_destroy (dialog->palette_select->shell);

  gtk_widget_destroy (dialog->shell);

  g_free (dialog);
}

static void
indexed_palette_select_destroy_callback (GtkWidget *widget,
					 gpointer   data)
{
  IndexedDialog *dialog = (IndexedDialog *)data;

  if (dialog)
    dialog->palette_select = NULL;
}

static gint
indexed_palette_select_palette (GimpContext *context,
				GimpPalette *palette,
				gpointer     data)
{
  IndexedDialog *dialog;

  dialog = (IndexedDialog *) data;

  if (palette)
    {
      if (palette->n_colors <= 256)
	{
	  theCustomPalette = palette;

	  gtk_label_set_text (GTK_LABEL (GTK_BIN (dialog->custom_palette_button)->child),
			      GIMP_OBJECT (theCustomPalette)->name);
	}
    }

  return FALSE;
}

static void
indexed_custom_palette_button_callback (GtkWidget *widget,
					gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
530
  IndexedDialog *dialog = (IndexedDialog *) data;
531 532 533 534

  if (dialog->palette_select == NULL)
    {
      dialog->palette_select =
Michael Natterer's avatar
Michael Natterer committed
535
	palette_select_new (dialog->gimage->gimp,
536
                            NULL,
Michael Natterer's avatar
Michael Natterer committed
537 538 539
                            _("Select Custom Palette"), 
			    GIMP_OBJECT (theCustomPalette)->name,
                            NULL);
540

541 542 543 544 545 546 547
      g_signal_connect (G_OBJECT (dialog->palette_select->shell), "destroy", 
			G_CALLBACK (indexed_palette_select_destroy_callback), 
			dialog);
      g_signal_connect (G_OBJECT (dialog->palette_select->context),
			"palette_changed",
			G_CALLBACK (indexed_palette_select_palette),
			dialog);
548 549 550 551 552 553
    } 
  else
    {
      gdk_window_raise (dialog->palette_select->shell->window);
    }
}