image-commands.c 14.2 KB
Newer Older
Michael Natterer's avatar
Michael Natterer committed
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"

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

27
#include "core/gimp.h"
28
#include "core/gimpcontext.h"
Michael Natterer's avatar
Michael Natterer committed
29
#include "core/gimpimage.h"
30
#include "core/gimpimage-crop.h"
Michael Natterer's avatar
Michael Natterer committed
31
#include "core/gimpimage-duplicate.h"
32
#include "core/gimpimage-mask.h"
33
#include "core/gimpimage-merge.h"
34 35
#include "core/gimpimage-resize.h"
#include "core/gimpimage-scale.h"
36
#include "core/gimpimage-undo.h"
Michael Natterer's avatar
Michael Natterer committed
37

38 39
#include "widgets/gimpviewabledialog.h"

40
#include "display/gimpdisplay.h"
41
#include "display/gimpdisplayshell.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "display/gimpprogress.h"
43

Michael Natterer's avatar
Michael Natterer committed
44
#include "convert-dialog.h"
45
#include "image-commands.h"
Michael Natterer's avatar
Michael Natterer committed
46 47 48 49 50 51 52
#include "resize-dialog.h"

#include "undo.h"

#include "libgimp/gimpintl.h"


Michael Natterer's avatar
Michael Natterer committed
53 54 55 56 57 58 59 60
typedef struct
{
  Resize      *resize;
  GimpDisplay *gdisp;
  GimpImage   *gimage;
} ImageResize;


Michael Natterer's avatar
Michael Natterer committed
61
#define return_if_no_display(gdisp,data) \
62 63 64 65 66 67
  if (GIMP_IS_DISPLAY (data)) \
    gdisp = data; \
  else if (GIMP_IS_GIMP (data)) \
    gdisp = gimp_context_get_display (gimp_get_user_context (GIMP (data))); \
  else \
    gdisp = NULL; \
Michael Natterer's avatar
Michael Natterer committed
68 69 70 71
  if (! gdisp) \
    return

#define return_if_no_image(gimage,data) \
72 73 74 75 76 77
  if (GIMP_IS_DISPLAY (data)) \
    gimage = ((GimpDisplay *) data)->gimage; \
  else if (GIMP_IS_GIMP (data)) \
    gimage = gimp_context_get_image (gimp_get_user_context (GIMP (data))); \
  else \
    gimage = NULL; \
Michael Natterer's avatar
Michael Natterer committed
78 79
  if (! gimage) \
    return
Michael Natterer's avatar
Michael Natterer committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99


/*  local functions  */

static void     image_resize_callback        (GtkWidget   *widget,
					      gpointer     data);
static void     image_scale_callback         (GtkWidget   *widget,
					      gpointer     data);
static void     image_scale_warn_callback    (GtkWidget   *widget,
					      gboolean     do_scale,
					      gpointer     data);
static void     image_scale_implement        (ImageResize *image_scale);


/*  public functions  */

void
image_convert_rgb_cmd_callback (GtkWidget *widget,
				gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
100 101
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
102

Michael Natterer's avatar
Michael Natterer committed
103
  convert_to_rgb (gimage);
Michael Natterer's avatar
Michael Natterer committed
104 105 106 107 108 109
}

void
image_convert_grayscale_cmd_callback (GtkWidget *widget,
				      gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
110 111
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
112

Michael Natterer's avatar
Michael Natterer committed
113
  convert_to_grayscale (gimage);
Michael Natterer's avatar
Michael Natterer committed
114 115 116 117 118 119
}

void
image_convert_indexed_cmd_callback (GtkWidget *widget,
				    gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
120 121
  GimpImage *gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
122

Michael Natterer's avatar
Michael Natterer committed
123
  convert_to_indexed (gimage);
Michael Natterer's avatar
Michael Natterer committed
124 125 126 127 128 129
}

void
image_resize_cmd_callback (GtkWidget *widget,
			   gpointer   data)
{
130
  GimpDisplay *gdisp;
Michael Natterer's avatar
Michael Natterer committed
131 132
  GimpImage   *gimage;
  ImageResize *image_resize;
Michael Natterer's avatar
Michael Natterer committed
133
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
134 135 136 137 138

  gimage = gdisp->gimage;

  image_resize = g_new0 (ImageResize, 1);

Michael Natterer's avatar
Michael Natterer committed
139
  image_resize->gdisp  = gdisp;
Michael Natterer's avatar
Michael Natterer committed
140
  image_resize->gimage = gimage;
141 142

  image_resize->resize =
143
    resize_widget_new (GIMP_VIEWABLE (gimage),
144 145 146 147 148 149 150 151 152
                       ResizeWidget,
                       gimage->width,
                       gimage->height,
                       gimage->xresolution,
                       gimage->yresolution,
                       gimage->unit,
                       GIMP_DISPLAY_SHELL (gdisp->shell)->dot_for_dot,
                       G_CALLBACK (image_resize_callback),
                       image_resize);
Michael Natterer's avatar
Michael Natterer committed
153

154
  g_signal_connect_object (gdisp, "disconnect",
155
                           G_CALLBACK (gtk_widget_destroy),
156
                           image_resize->resize->resize_shell,
157 158
                           G_CONNECT_SWAPPED);

159 160 161
  g_object_weak_ref (G_OBJECT (image_resize->resize->resize_shell),
		     (GWeakNotify) g_free,
		     image_resize);
Michael Natterer's avatar
Michael Natterer committed
162 163 164 165 166 167 168 169

  gtk_widget_show (image_resize->resize->resize_shell);
}

void
image_scale_cmd_callback (GtkWidget *widget,
			  gpointer   data)
{
170
  GimpDisplay *gdisp;
Michael Natterer's avatar
Michael Natterer committed
171 172
  GimpImage   *gimage;
  ImageResize *image_scale;
Michael Natterer's avatar
Michael Natterer committed
173
  return_if_no_display (gdisp, data);
Michael Natterer's avatar
Michael Natterer committed
174 175 176 177 178

  gimage = gdisp->gimage;

  image_scale = g_new0 (ImageResize, 1);

Michael Natterer's avatar
Michael Natterer committed
179
  image_scale->gdisp  = gdisp;
Michael Natterer's avatar
Michael Natterer committed
180
  image_scale->gimage = gimage;
181 182

  image_scale->resize =
183
    resize_widget_new (GIMP_VIEWABLE (gimage),
184 185 186 187 188 189 190 191 192
                       ScaleWidget,
                       gimage->width,
                       gimage->height,
                       gimage->xresolution,
                       gimage->yresolution,
                       gimage->unit,
                       GIMP_DISPLAY_SHELL (gdisp->shell)->dot_for_dot,
                       G_CALLBACK (image_scale_callback),
                       image_scale);
Michael Natterer's avatar
Michael Natterer committed
193

194
  g_signal_connect_object (gdisp, "disconnect",
195
                           G_CALLBACK (gtk_widget_destroy),
196
                           image_scale->resize->resize_shell,
197 198
                           G_CONNECT_SWAPPED);

199 200 201
  g_object_weak_ref (G_OBJECT (image_scale->resize->resize_shell),
		     (GWeakNotify) g_free,
		     image_scale);
Michael Natterer's avatar
Michael Natterer committed
202 203 204 205

  gtk_widget_show (image_scale->resize->resize_shell);
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
void
image_crop_cmd_callback (GtkWidget *widget,
                         gpointer   data)
{
  GimpDisplay *gdisp;
  gint         x1, y1, x2, y2;
  return_if_no_display (gdisp, data);

  if (! gimp_image_mask_bounds (gdisp->gimage, &x1, &y1, &x2, &y2))
    {
      g_message (_("Cannot crop because the current selection is empty."));
      return;
    }

  gimp_image_crop (gdisp->gimage, x1, y1, x2, y2, FALSE, TRUE);
}

Michael Natterer's avatar
Michael Natterer committed
223 224 225 226
void
image_duplicate_cmd_callback (GtkWidget *widget,
			      gpointer   data)
{
Michael Natterer's avatar
Michael Natterer committed
227 228 229
  GimpImage *gimage;
  GimpImage *new_gimage;
  return_if_no_image (gimage, data);
Michael Natterer's avatar
Michael Natterer committed
230

Michael Natterer's avatar
Michael Natterer committed
231
  new_gimage = gimp_image_duplicate (gimage);
232

Michael Natterer's avatar
Michael Natterer committed
233 234
  gimp_create_display (new_gimage->gimp, new_gimage, 0x0101);

235
  g_object_unref (new_gimage);
Michael Natterer's avatar
Michael Natterer committed
236 237
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
void
image_merge_layers_cmd_callback (GtkWidget *widget,
                                 gpointer   data)
{
  GimpImage *gimage;
  return_if_no_image (gimage, data);

  image_layers_merge_query (gimage, TRUE);
}

void
image_flatten_image_cmd_callback (GtkWidget *widget,
                                  gpointer   data)
{
  GimpImage *gimage;
  return_if_no_image (gimage, data);

  gimp_image_flatten (gimage);
256
  gimp_image_flush (gimage);
257
}
258 259


260 261 262 263 264 265 266 267
/****************************/
/*  The layer merge dialog  */
/****************************/

typedef struct _LayerMergeOptions LayerMergeOptions;

struct _LayerMergeOptions
{
268 269 270 271
  GtkWidget     *query_box;
  GimpImage     *gimage;
  gboolean       merge_visible;
  GimpMergeType  merge_type;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
};

static void
image_layers_merge_query_ok_callback (GtkWidget *widget,
                                      gpointer   data)
{
  LayerMergeOptions *options;
  GimpImage         *gimage;

  options = (LayerMergeOptions *) data;
  if (! (gimage = options->gimage))
    return;

  if (options->merge_visible)
    gimp_image_merge_visible_layers (gimage, options->merge_type);

288
  gimp_image_flush (gimage);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

  gtk_widget_destroy (options->query_box);
}

void
image_layers_merge_query (GimpImage   *gimage,
                          /*  if FALSE, anchor active layer  */
                          gboolean     merge_visible)
{
  LayerMergeOptions *options;
  GtkWidget         *vbox;
  GtkWidget         *frame;

  /*  The new options structure  */
  options = g_new (LayerMergeOptions, 1);
  options->gimage        = gimage;
  options->merge_visible = merge_visible;
306
  options->merge_type    = GIMP_EXPAND_AS_NECESSARY;
307 308 309

  /* The dialog  */
  options->query_box =
310 311 312 313 314 315
    gimp_viewable_dialog_new (GIMP_VIEWABLE (gimage),
                              _("Merge Layers"), "layers_merge_options",
                              GIMP_STOCK_MERGE_DOWN,
                              _("Layers Merge Options"),
                              gimp_standard_help_func,
                              "dialogs/layers/merge_visible_layers.html",
316

317 318
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
319

320 321
                              GTK_STOCK_OK, image_layers_merge_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
322

323
                              NULL);
324 325 326 327 328 329 330 331 332 333 334

  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);

  /*  The main vbox  */
  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     vbox);

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  frame =
    gimp_radio_group_new2 (TRUE,
                           merge_visible ?
                           _("Final, Merged Layer should be:") :
                           _("Final, Anchored Layer should be:"),
                           G_CALLBACK (gimp_radio_button_update),
                           &options->merge_type,
                           GINT_TO_POINTER (options->merge_type),

                           _("Expanded as necessary"),
                           GINT_TO_POINTER (GIMP_EXPAND_AS_NECESSARY), NULL,

                           _("Clipped to image"),
                           GINT_TO_POINTER (GIMP_CLIP_TO_IMAGE), NULL,

                           _("Clipped to bottom layer"),
                           GINT_TO_POINTER (GIMP_CLIP_TO_BOTTOM_LAYER), NULL,

                           NULL);
354 355 356 357 358 359 360 361

  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  gtk_widget_show (vbox);
  gtk_widget_show (options->query_box);
}

Michael Natterer's avatar
Michael Natterer committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

/*  private functions  */

static void
image_resize_callback (GtkWidget *widget,
		       gpointer   data)
{
  ImageResize *image_resize;

  image_resize = (ImageResize *) data;

  g_assert (image_resize != NULL);
  g_assert (image_resize->gimage != NULL);

  gtk_widget_set_sensitive (image_resize->resize->resize_shell, FALSE);

  if (image_resize->resize->width > 0 &&
      image_resize->resize->height > 0) 
    {
      gimp_image_resize (image_resize->gimage,
			 image_resize->resize->width,
			 image_resize->resize->height,
			 image_resize->resize->offset_x,
			 image_resize->resize->offset_y);
386
      gimp_image_flush (image_resize->gimage);
Michael Natterer's avatar
Michael Natterer committed
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
    }
  else 
    {
      g_message (_("Resize Error: Both width and height must be "
		   "greater than zero."));
    }

  gtk_widget_destroy (image_resize->resize->resize_shell);
}

static void
image_scale_callback (GtkWidget *widget,
		      gpointer   data)
{
  ImageResize *image_scale;

  image_scale = (ImageResize *) data;

  g_assert (image_scale != NULL);
  g_assert (image_scale->gimage != NULL);

  gtk_widget_set_sensitive (image_scale->resize->resize_shell, FALSE);

  if (gimp_image_check_scaling (image_scale->gimage,
				image_scale->resize->width,
				image_scale->resize->height))
    {
      image_scale_implement (image_scale);

      gtk_widget_destroy (image_scale->resize->resize_shell);
    }
  else
    {
      GtkWidget *dialog;

      dialog =
	gimp_query_boolean_box (_("Layer Too Small"),
				gimp_standard_help_func,
				"dialogs/scale_layer_warn.html",
426
				GTK_STOCK_DIALOG_QUESTION,
Michael Natterer's avatar
Michael Natterer committed
427 428 429
				_("The chosen image size will shrink\n"
				  "some layers completely away.\n"
				  "Is this what you want?"),
430 431
				GTK_STOCK_OK, GTK_STOCK_CANCEL,
				G_OBJECT (image_scale->resize->resize_shell),
Michael Natterer's avatar
Michael Natterer committed
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
				"destroy",
				image_scale_warn_callback,
				image_scale);
      gtk_widget_show (dialog);
    }
}

static void
image_scale_warn_callback (GtkWidget *widget,
			   gboolean   do_scale,
			   gpointer   data)
{
  ImageResize *image_scale;
  GimpImage   *gimage;

  image_scale = (ImageResize *) data;
  gimage      = image_scale->gimage;

  if (do_scale) /* User doesn't mind losing layers... */
    {
      image_scale_implement (image_scale);

      gtk_widget_destroy (image_scale->resize->resize_shell);
    }
  else
    {
      gtk_widget_set_sensitive (image_scale->resize->resize_shell, TRUE);
    }
}

static void
image_scale_implement (ImageResize *image_scale)
{
  GimpImage *gimage        = NULL;
  gboolean   display_flush = FALSE;  /* this is a bit ugly: 
					we hijack the flush variable 
					to check if an undo_group was 
					already started */

  g_assert (image_scale != NULL);
  g_assert (image_scale->gimage != NULL);

  gimage = image_scale->gimage;

  if (image_scale->resize->resolution_x != gimage->xresolution ||
      image_scale->resize->resolution_y != gimage->yresolution)
    {
479 480
      gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_SCALE,
                                   _("Scale Image"));
Michael Natterer's avatar
Michael Natterer committed
481 482 483 484 485 486 487 488 489 490
	  
      gimp_image_set_resolution (gimage,
				 image_scale->resize->resolution_x,
				 image_scale->resize->resolution_y);

      display_flush = TRUE;
    }

  if (image_scale->resize->unit != gimage->unit)
    {
Michael Natterer's avatar
Michael Natterer committed
491
      if (! display_flush)
492 493
	gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_SCALE,
                                     _("Scale Image"));
Michael Natterer's avatar
Michael Natterer committed
494 495 496 497 498 499 500 501 502 503 504 505

      gimp_image_set_unit (gimage, image_scale->resize->unit);

      display_flush = TRUE;
    }

  if (image_scale->resize->width != gimage->width ||
      image_scale->resize->height != gimage->height)
    {
      if (image_scale->resize->width > 0 &&
	  image_scale->resize->height > 0) 
	{
Michael Natterer's avatar
Michael Natterer committed
506 507
          GimpProgress *progress;

Michael Natterer's avatar
Michael Natterer committed
508
	  if (! display_flush)
509 510
	    gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_SCALE,
                                         _("Scale Image"));
Michael Natterer's avatar
Michael Natterer committed
511

Michael Natterer's avatar
Michael Natterer committed
512 513 514
          progress = gimp_progress_start (image_scale->gdisp,
                                          _("Scaling..."),
                                          TRUE, NULL, NULL);
Michael Natterer's avatar
Michael Natterer committed
515

Michael Natterer's avatar
Michael Natterer committed
516 517
	  gimp_image_scale (gimage,
			    image_scale->resize->width,
Michael Natterer's avatar
Michael Natterer committed
518
			    image_scale->resize->height,
519
                            image_scale->resize->interpolation,
Michael Natterer's avatar
Michael Natterer committed
520
                            gimp_progress_update_and_flush, progress);
Michael Natterer's avatar
Michael Natterer committed
521

Michael Natterer's avatar
Michael Natterer committed
522
          gimp_progress_end (progress);
Michael Natterer's avatar
Michael Natterer committed
523 524 525 526 527 528 529 530 531 532 533 534 535

	  display_flush = TRUE;
	}
      else
	{
	  g_message (_("Scale Error: Both width and height must be "
		       "greater than zero."));
	  return;
	}
    }

  if (display_flush)
    {
536
      gimp_image_undo_group_end (gimage);
537
      gimp_image_flush (gimage);
Michael Natterer's avatar
Michael Natterer committed
538 539
    }
}