~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/core/gimpimage.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <string.h>
 
22
#include <time.h>
 
23
 
 
24
#include <glib-object.h>
 
25
 
 
26
#include "libgimpcolor/gimpcolor.h"
 
27
#include "libgimpmath/gimpmath.h"
 
28
#include "libgimpbase/gimpbase.h"
 
29
 
 
30
#include "core-types.h"
 
31
 
 
32
#include "base/temp-buf.h"
 
33
#include "base/tile-manager.h"
 
34
 
 
35
#include "config/gimpconfig.h"
 
36
#include "config/gimpconfig-utils.h"
 
37
#include "config/gimpcoreconfig.h"
 
38
 
 
39
#include "gimp.h"
 
40
#include "gimp-parasites.h"
 
41
#include "gimp-utils.h"
 
42
#include "gimpcontext.h"
 
43
#include "gimpgrid.h"
 
44
#include "gimpimage.h"
 
45
#include "gimpimage-colorhash.h"
 
46
#include "gimpimage-colormap.h"
 
47
#include "gimpimage-guides.h"
 
48
#include "gimpimage-preview.h"
 
49
#include "gimpimage-qmask.h"
 
50
#include "gimpimage-undo.h"
 
51
#include "gimpimage-undo-push.h"
 
52
#include "gimplayer.h"
 
53
#include "gimplayer-floating-sel.h"
 
54
#include "gimplayermask.h"
 
55
#include "gimplist.h"
 
56
#include "gimpmarshal.h"
 
57
#include "gimpparasitelist.h"
 
58
#include "gimpprojection.h"
 
59
#include "gimpselection.h"
 
60
#include "gimptemplate.h"
 
61
#include "gimpundostack.h"
 
62
 
 
63
#include "file/file-utils.h"
 
64
 
 
65
#include "vectors/gimpvectors.h"
 
66
 
 
67
#include "gimp-intl.h"
 
68
 
 
69
 
 
70
#ifdef DEBUG
 
71
#define TRC(x) printf x
 
72
#else
 
73
#define TRC(x)
 
74
#endif
 
75
 
 
76
 
 
77
enum
 
78
{
 
79
  MODE_CHANGED,
 
80
  ALPHA_CHANGED,
 
81
  FLOATING_SELECTION_CHANGED,
 
82
  ACTIVE_LAYER_CHANGED,
 
83
  ACTIVE_CHANNEL_CHANGED,
 
84
  ACTIVE_VECTORS_CHANGED,
 
85
  COMPONENT_VISIBILITY_CHANGED,
 
86
  COMPONENT_ACTIVE_CHANGED,
 
87
  MASK_CHANGED,
 
88
  RESOLUTION_CHANGED,
 
89
  UNIT_CHANGED,
 
90
  QMASK_CHANGED,
 
91
  SELECTION_CONTROL,
 
92
  CLEAN,
 
93
  DIRTY,
 
94
  UPDATE,
 
95
  UPDATE_GUIDE,
 
96
  COLORMAP_CHANGED,
 
97
  UNDO_EVENT,
 
98
  FLUSH,
 
99
  LAST_SIGNAL
 
100
};
 
101
 
 
102
enum
 
103
{
 
104
  PROP_0,
 
105
  PROP_GIMP,
 
106
  PROP_ID,
 
107
  PROP_WIDTH,
 
108
  PROP_HEIGHT,
 
109
  PROP_BASE_TYPE
 
110
};
 
111
 
 
112
 
 
113
/*  local function prototypes  */
 
114
 
 
115
static void     gimp_image_class_init            (GimpImageClass *klass);
 
116
static void     gimp_image_init                  (GimpImage      *gimage);
 
117
 
 
118
static GObject *gimp_image_constructor           (GType           type,
 
119
                                                  guint           n_params,
 
120
                                                  GObjectConstructParam *params);
 
121
static void     gimp_image_set_property          (GObject        *object,
 
122
                                                  guint           property_id,
 
123
                                                  const GValue   *value,
 
124
                                                  GParamSpec     *pspec);
 
125
static void     gimp_image_get_property          (GObject        *object,
 
126
                                                  guint           property_id,
 
127
                                                  GValue         *value,
 
128
                                                  GParamSpec     *pspec);
 
129
static void     gimp_image_dispose               (GObject        *object);
 
130
static void     gimp_image_finalize              (GObject        *object);
 
131
 
 
132
static void     gimp_image_name_changed          (GimpObject     *object);
 
133
static gint64   gimp_image_get_memsize           (GimpObject     *object,
 
134
                                                  gint64         *gui_size);
 
135
 
 
136
static void     gimp_image_invalidate_preview    (GimpViewable   *viewable);
 
137
static void     gimp_image_size_changed          (GimpViewable   *viewable);
 
138
static gchar  * gimp_image_get_description       (GimpViewable   *viewable,
 
139
                                                  gchar         **tooltip);
 
140
static void     gimp_image_real_colormap_changed (GimpImage      *gimage,
 
141
                                                  gint            color_index);
 
142
static void     gimp_image_real_flush            (GimpImage      *gimage);
 
143
 
 
144
static void     gimp_image_mask_update           (GimpDrawable   *drawable,
 
145
                                                  gint            x,
 
146
                                                  gint            y,
 
147
                                                  gint            width,
 
148
                                                  gint            height,
 
149
                                                  GimpImage      *gimage);
 
150
static void     gimp_image_drawable_update       (GimpDrawable   *drawable,
 
151
                                                  gint            x,
 
152
                                                  gint            y,
 
153
                                                  gint            width,
 
154
                                                  gint            height,
 
155
                                                  GimpImage      *gimage);
 
156
static void     gimp_image_drawable_visibility   (GimpItem       *item,
 
157
                                                  GimpImage      *gimage);
 
158
static void     gimp_image_layer_alpha_changed   (GimpDrawable   *drawable,
 
159
                                                  GimpImage      *gimage);
 
160
static void     gimp_image_layer_add             (GimpContainer  *container,
 
161
                                                  GimpLayer      *layer,
 
162
                                                  GimpImage      *gimage);
 
163
static void     gimp_image_layer_remove          (GimpContainer  *container,
 
164
                                                  GimpLayer      *layer,
 
165
                                                  GimpImage      *gimage);
 
166
static void     gimp_image_channel_add           (GimpContainer  *container,
 
167
                                                  GimpChannel    *channel,
 
168
                                                  GimpImage      *gimage);
 
169
static void     gimp_image_channel_remove        (GimpContainer  *container,
 
170
                                                  GimpChannel    *channel,
 
171
                                                  GimpImage      *gimage);
 
172
static void     gimp_image_channel_name_changed  (GimpChannel    *channel,
 
173
                                                  GimpImage      *gimage);
 
174
static void     gimp_image_channel_color_changed (GimpChannel    *channel,
 
175
                                                  GimpImage      *gimage);
 
176
 
 
177
 
 
178
static gint valid_combinations[][MAX_CHANNELS + 1] =
 
179
{
 
180
  /* GIMP_RGB_IMAGE */
 
181
  { -1, -1, -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A },
 
182
  /* GIMP_RGBA_IMAGE */
 
183
  { -1, -1, -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A },
 
184
  /* GIMP_GRAY_IMAGE */
 
185
  { -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A, -1, -1 },
 
186
  /* GIMP_GRAYA_IMAGE */
 
187
  { -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A, -1, -1 },
 
188
  /* GIMP_INDEXED_IMAGE */
 
189
  { -1, COMBINE_INDEXED_INDEXED, COMBINE_INDEXED_INDEXED_A, -1, -1 },
 
190
  /* GIMP_INDEXEDA_IMAGE */
 
191
  { -1, -1, COMBINE_INDEXED_A_INDEXED_A, -1, -1 },
 
192
};
 
193
 
 
194
static guint gimp_image_signals[LAST_SIGNAL] = { 0 };
 
195
 
 
196
static GimpViewableClass *parent_class = NULL;
 
197
 
 
198
 
 
199
GType
 
200
gimp_image_get_type (void)
 
201
{
 
202
  static GType image_type = 0;
 
203
 
 
204
  if (! image_type)
 
205
    {
 
206
      static const GTypeInfo image_info =
 
207
      {
 
208
        sizeof (GimpImageClass),
 
209
        NULL,           /* base_init */
 
210
        NULL,           /* base_finalize */
 
211
        (GClassInitFunc) gimp_image_class_init,
 
212
        NULL,           /* class_finalize */
 
213
        NULL,           /* class_data */
 
214
        sizeof (GimpImage),
 
215
        0,              /* n_preallocs */
 
216
        (GInstanceInitFunc) gimp_image_init,
 
217
      };
 
218
 
 
219
      image_type = g_type_register_static (GIMP_TYPE_VIEWABLE,
 
220
                                           "GimpImage",
 
221
                                           &image_info, 0);
 
222
    }
 
223
 
 
224
  return image_type;
 
225
}
 
226
 
 
227
 
 
228
/*  private functions  */
 
229
 
 
230
static void
 
231
gimp_image_class_init (GimpImageClass *klass)
 
232
{
 
233
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
 
234
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);
 
235
  GimpViewableClass *viewable_class    = GIMP_VIEWABLE_CLASS (klass);
 
236
 
 
237
  parent_class = g_type_class_peek_parent (klass);
 
238
 
 
239
  gimp_image_signals[MODE_CHANGED] =
 
240
    g_signal_new ("mode_changed",
 
241
                  G_TYPE_FROM_CLASS (klass),
 
242
                  G_SIGNAL_RUN_FIRST,
 
243
                  G_STRUCT_OFFSET (GimpImageClass, mode_changed),
 
244
                  NULL, NULL,
 
245
                  gimp_marshal_VOID__VOID,
 
246
                  G_TYPE_NONE, 0);
 
247
 
 
248
  gimp_image_signals[ALPHA_CHANGED] =
 
249
    g_signal_new ("alpha_changed",
 
250
                  G_TYPE_FROM_CLASS (klass),
 
251
                  G_SIGNAL_RUN_FIRST,
 
252
                  G_STRUCT_OFFSET (GimpImageClass, alpha_changed),
 
253
                  NULL, NULL,
 
254
                  gimp_marshal_VOID__VOID,
 
255
                  G_TYPE_NONE, 0);
 
256
 
 
257
  gimp_image_signals[FLOATING_SELECTION_CHANGED] =
 
258
    g_signal_new ("floating_selection_changed",
 
259
                  G_TYPE_FROM_CLASS (klass),
 
260
                  G_SIGNAL_RUN_FIRST,
 
261
                  G_STRUCT_OFFSET (GimpImageClass, floating_selection_changed),
 
262
                  NULL, NULL,
 
263
                  gimp_marshal_VOID__VOID,
 
264
                  G_TYPE_NONE, 0);
 
265
 
 
266
  gimp_image_signals[ACTIVE_LAYER_CHANGED] =
 
267
    g_signal_new ("active_layer_changed",
 
268
                  G_TYPE_FROM_CLASS (klass),
 
269
                  G_SIGNAL_RUN_FIRST,
 
270
                  G_STRUCT_OFFSET (GimpImageClass, active_layer_changed),
 
271
                  NULL, NULL,
 
272
                  gimp_marshal_VOID__VOID,
 
273
                  G_TYPE_NONE, 0);
 
274
 
 
275
  gimp_image_signals[ACTIVE_CHANNEL_CHANGED] =
 
276
    g_signal_new ("active_channel_changed",
 
277
                  G_TYPE_FROM_CLASS (klass),
 
278
                  G_SIGNAL_RUN_FIRST,
 
279
                  G_STRUCT_OFFSET (GimpImageClass, active_channel_changed),
 
280
                  NULL, NULL,
 
281
                  gimp_marshal_VOID__VOID,
 
282
                  G_TYPE_NONE, 0);
 
283
 
 
284
  gimp_image_signals[ACTIVE_VECTORS_CHANGED] =
 
285
    g_signal_new ("active_vectors_changed",
 
286
                  G_TYPE_FROM_CLASS (klass),
 
287
                  G_SIGNAL_RUN_FIRST,
 
288
                  G_STRUCT_OFFSET (GimpImageClass, active_vectors_changed),
 
289
                  NULL, NULL,
 
290
                  gimp_marshal_VOID__VOID,
 
291
                  G_TYPE_NONE, 0);
 
292
 
 
293
  gimp_image_signals[COMPONENT_VISIBILITY_CHANGED] =
 
294
    g_signal_new ("component_visibility_changed",
 
295
                  G_TYPE_FROM_CLASS (klass),
 
296
                  G_SIGNAL_RUN_FIRST,
 
297
                  G_STRUCT_OFFSET (GimpImageClass, component_visibility_changed),
 
298
                  NULL, NULL,
 
299
                  gimp_marshal_VOID__ENUM,
 
300
                  G_TYPE_NONE, 1,
 
301
                  GIMP_TYPE_CHANNEL_TYPE);
 
302
 
 
303
  gimp_image_signals[COMPONENT_ACTIVE_CHANGED] =
 
304
    g_signal_new ("component_active_changed",
 
305
                  G_TYPE_FROM_CLASS (klass),
 
306
                  G_SIGNAL_RUN_FIRST,
 
307
                  G_STRUCT_OFFSET (GimpImageClass, component_active_changed),
 
308
                  NULL, NULL,
 
309
                  gimp_marshal_VOID__ENUM,
 
310
                  G_TYPE_NONE, 1,
 
311
                  GIMP_TYPE_CHANNEL_TYPE);
 
312
 
 
313
  gimp_image_signals[MASK_CHANGED] =
 
314
    g_signal_new ("mask_changed",
 
315
                  G_TYPE_FROM_CLASS (klass),
 
316
                  G_SIGNAL_RUN_FIRST,
 
317
                  G_STRUCT_OFFSET (GimpImageClass, mask_changed),
 
318
                  NULL, NULL,
 
319
                  gimp_marshal_VOID__VOID,
 
320
                  G_TYPE_NONE, 0);
 
321
 
 
322
  gimp_image_signals[RESOLUTION_CHANGED] =
 
323
    g_signal_new ("resolution_changed",
 
324
                  G_TYPE_FROM_CLASS (klass),
 
325
                  G_SIGNAL_RUN_FIRST,
 
326
                  G_STRUCT_OFFSET (GimpImageClass, resolution_changed),
 
327
                  NULL, NULL,
 
328
                  gimp_marshal_VOID__VOID,
 
329
                  G_TYPE_NONE, 0);
 
330
 
 
331
  gimp_image_signals[UNIT_CHANGED] =
 
332
    g_signal_new ("unit_changed",
 
333
                  G_TYPE_FROM_CLASS (klass),
 
334
                  G_SIGNAL_RUN_FIRST,
 
335
                  G_STRUCT_OFFSET (GimpImageClass, unit_changed),
 
336
                  NULL, NULL,
 
337
                  gimp_marshal_VOID__VOID,
 
338
                  G_TYPE_NONE, 0);
 
339
 
 
340
  gimp_image_signals[QMASK_CHANGED] =
 
341
    g_signal_new ("qmask_changed",
 
342
                  G_TYPE_FROM_CLASS (klass),
 
343
                  G_SIGNAL_RUN_FIRST,
 
344
                  G_STRUCT_OFFSET (GimpImageClass, qmask_changed),
 
345
                  NULL, NULL,
 
346
                  gimp_marshal_VOID__VOID,
 
347
                  G_TYPE_NONE, 0);
 
348
 
 
349
  gimp_image_signals[SELECTION_CONTROL] =
 
350
    g_signal_new ("selection_control",
 
351
                  G_TYPE_FROM_CLASS (klass),
 
352
                  G_SIGNAL_RUN_FIRST,
 
353
                  G_STRUCT_OFFSET (GimpImageClass, selection_control),
 
354
                  NULL, NULL,
 
355
                  gimp_marshal_VOID__ENUM,
 
356
                  G_TYPE_NONE, 1,
 
357
                  GIMP_TYPE_SELECTION_CONTROL);
 
358
 
 
359
  gimp_image_signals[CLEAN] =
 
360
    g_signal_new ("clean",
 
361
                  G_TYPE_FROM_CLASS (klass),
 
362
                  G_SIGNAL_RUN_FIRST,
 
363
                  G_STRUCT_OFFSET (GimpImageClass, clean),
 
364
                  NULL, NULL,
 
365
                  gimp_marshal_VOID__FLAGS,
 
366
                  G_TYPE_NONE, 1,
 
367
                  GIMP_TYPE_DIRTY_MASK);
 
368
 
 
369
  gimp_image_signals[DIRTY] =
 
370
    g_signal_new ("dirty",
 
371
                  G_TYPE_FROM_CLASS (klass),
 
372
                  G_SIGNAL_RUN_FIRST,
 
373
                  G_STRUCT_OFFSET (GimpImageClass, dirty),
 
374
                  NULL, NULL,
 
375
                  gimp_marshal_VOID__FLAGS,
 
376
                  G_TYPE_NONE, 1,
 
377
                  GIMP_TYPE_DIRTY_MASK);
 
378
 
 
379
  gimp_image_signals[UPDATE] =
 
380
    g_signal_new ("update",
 
381
                  G_TYPE_FROM_CLASS (klass),
 
382
                  G_SIGNAL_RUN_FIRST,
 
383
                  G_STRUCT_OFFSET (GimpImageClass, update),
 
384
                  NULL, NULL,
 
385
                  gimp_marshal_VOID__INT_INT_INT_INT,
 
386
                  G_TYPE_NONE, 4,
 
387
                  G_TYPE_INT,
 
388
                  G_TYPE_INT,
 
389
                  G_TYPE_INT,
 
390
                  G_TYPE_INT);
 
391
 
 
392
  gimp_image_signals[UPDATE_GUIDE] =
 
393
    g_signal_new ("update_guide",
 
394
                  G_TYPE_FROM_CLASS (klass),
 
395
                  G_SIGNAL_RUN_FIRST,
 
396
                  G_STRUCT_OFFSET (GimpImageClass, update_guide),
 
397
                  NULL, NULL,
 
398
                  gimp_marshal_VOID__POINTER,
 
399
                  G_TYPE_NONE, 1,
 
400
                  G_TYPE_POINTER);
 
401
 
 
402
  gimp_image_signals[COLORMAP_CHANGED] =
 
403
    g_signal_new ("colormap_changed",
 
404
                  G_TYPE_FROM_CLASS (klass),
 
405
                  G_SIGNAL_RUN_FIRST,
 
406
                  G_STRUCT_OFFSET (GimpImageClass, colormap_changed),
 
407
                  NULL, NULL,
 
408
                  gimp_marshal_VOID__INT,
 
409
                  G_TYPE_NONE, 1,
 
410
                  G_TYPE_INT);
 
411
 
 
412
  gimp_image_signals[UNDO_EVENT] =
 
413
    g_signal_new ("undo_event",
 
414
                  G_TYPE_FROM_CLASS (klass),
 
415
                  G_SIGNAL_RUN_FIRST,
 
416
                  G_STRUCT_OFFSET (GimpImageClass, undo_event),
 
417
                  NULL, NULL,
 
418
                  gimp_marshal_VOID__ENUM_OBJECT,
 
419
                  G_TYPE_NONE, 2,
 
420
                  GIMP_TYPE_UNDO_EVENT,
 
421
                  GIMP_TYPE_UNDO);
 
422
 
 
423
  gimp_image_signals[FLUSH] =
 
424
    g_signal_new ("flush",
 
425
                  G_TYPE_FROM_CLASS (klass),
 
426
                  G_SIGNAL_RUN_FIRST,
 
427
                  G_STRUCT_OFFSET (GimpImageClass, flush),
 
428
                  NULL, NULL,
 
429
                  gimp_marshal_VOID__VOID,
 
430
                  G_TYPE_NONE, 0);
 
431
 
 
432
  object_class->constructor           = gimp_image_constructor;
 
433
  object_class->set_property          = gimp_image_set_property;
 
434
  object_class->get_property          = gimp_image_get_property;
 
435
  object_class->dispose               = gimp_image_dispose;
 
436
  object_class->finalize              = gimp_image_finalize;
 
437
 
 
438
  gimp_object_class->name_changed     = gimp_image_name_changed;
 
439
  gimp_object_class->get_memsize      = gimp_image_get_memsize;
 
440
 
 
441
  viewable_class->default_stock_id    = "gimp-image";
 
442
  viewable_class->invalidate_preview  = gimp_image_invalidate_preview;
 
443
  viewable_class->size_changed        = gimp_image_size_changed;
 
444
  viewable_class->get_preview_size    = gimp_image_get_preview_size;
 
445
  viewable_class->get_popup_size      = gimp_image_get_popup_size;
 
446
  viewable_class->get_preview         = gimp_image_get_preview;
 
447
  viewable_class->get_new_preview     = gimp_image_get_new_preview;
 
448
  viewable_class->get_description     = gimp_image_get_description;
 
449
 
 
450
  klass->mode_changed                 = NULL;
 
451
  klass->alpha_changed                = NULL;
 
452
  klass->floating_selection_changed   = NULL;
 
453
  klass->active_layer_changed         = NULL;
 
454
  klass->active_channel_changed       = NULL;
 
455
  klass->active_vectors_changed       = NULL;
 
456
  klass->component_visibility_changed = NULL;
 
457
  klass->component_active_changed     = NULL;
 
458
  klass->mask_changed                 = NULL;
 
459
 
 
460
  klass->clean                        = NULL;
 
461
  klass->dirty                        = NULL;
 
462
  klass->update                       = NULL;
 
463
  klass->update_guide                 = NULL;
 
464
  klass->colormap_changed             = gimp_image_real_colormap_changed;
 
465
  klass->undo_event                   = NULL;
 
466
  klass->flush                        = gimp_image_real_flush;
 
467
 
 
468
  g_object_class_install_property (object_class, PROP_GIMP,
 
469
                                   g_param_spec_object ("gimp", NULL, NULL,
 
470
                                                        GIMP_TYPE_GIMP,
 
471
                                                        G_PARAM_READWRITE |
 
472
                                                        G_PARAM_CONSTRUCT_ONLY));
 
473
 
 
474
  g_object_class_install_property (object_class, PROP_ID,
 
475
                                   g_param_spec_int ("id", NULL, NULL,
 
476
                                                     0, G_MAXINT, 0,
 
477
                                                     G_PARAM_READABLE));
 
478
 
 
479
  g_object_class_install_property (object_class, PROP_WIDTH,
 
480
                                   g_param_spec_int ("width", NULL, NULL,
 
481
                                                     1, GIMP_MAX_IMAGE_SIZE, 1,
 
482
                                                     G_PARAM_READWRITE |
 
483
                                                     G_PARAM_CONSTRUCT));
 
484
 
 
485
  g_object_class_install_property (object_class, PROP_HEIGHT,
 
486
                                   g_param_spec_int ("height", NULL, NULL,
 
487
                                                     1, GIMP_MAX_IMAGE_SIZE, 1,
 
488
                                                     G_PARAM_READWRITE |
 
489
                                                     G_PARAM_CONSTRUCT));
 
490
 
 
491
  g_object_class_install_property (object_class, PROP_BASE_TYPE,
 
492
                                   g_param_spec_enum ("base-type", NULL, NULL,
 
493
                                                      GIMP_TYPE_IMAGE_BASE_TYPE,
 
494
                                                      GIMP_RGB,
 
495
                                                      G_PARAM_READWRITE |
 
496
                                                      G_PARAM_CONSTRUCT));
 
497
 
 
498
  gimp_image_color_hash_init ();
 
499
}
 
500
 
 
501
static void
 
502
gimp_image_init (GimpImage *gimage)
 
503
{
 
504
  gint i;
 
505
 
 
506
  gimage->ID                    = 0;
 
507
 
 
508
  gimage->save_proc             = NULL;
 
509
 
 
510
  gimage->width                 = 0;
 
511
  gimage->height                = 0;
 
512
  gimage->xresolution           = 1.0;
 
513
  gimage->yresolution           = 1.0;
 
514
  gimage->resolution_unit       = GIMP_UNIT_INCH;
 
515
  gimage->base_type             = GIMP_RGB;
 
516
 
 
517
  gimage->cmap                  = NULL;
 
518
  gimage->num_cols              = 0;
 
519
 
 
520
  gimage->dirty                 = 1;
 
521
  gimage->dirty_time            = 0;
 
522
  gimage->undo_freeze_count     = 0;
 
523
 
 
524
  gimage->instance_count        = 0;
 
525
  gimage->disp_count            = 0;
 
526
 
 
527
  gimage->tattoo_state          = 0;
 
528
 
 
529
  gimage->shadow                = NULL;
 
530
 
 
531
  gimage->projection            = gimp_projection_new (gimage);
 
532
 
 
533
  gimage->guides                = NULL;
 
534
 
 
535
  gimage->grid                  = NULL;
 
536
 
 
537
  gimage->layers                = gimp_list_new (GIMP_TYPE_LAYER,   TRUE);
 
538
  gimage->channels              = gimp_list_new (GIMP_TYPE_CHANNEL, TRUE);
 
539
  gimage->vectors               = gimp_list_new (GIMP_TYPE_VECTORS, TRUE);
 
540
  gimage->layer_stack           = NULL;
 
541
 
 
542
  gimage->layer_update_handler =
 
543
    gimp_container_add_handler (gimage->layers, "update",
 
544
                                G_CALLBACK (gimp_image_drawable_update),
 
545
                                gimage);
 
546
  gimage->layer_visible_handler =
 
547
    gimp_container_add_handler (gimage->layers, "visibility_changed",
 
548
                                G_CALLBACK (gimp_image_drawable_visibility),
 
549
                                gimage);
 
550
  gimage->layer_alpha_handler =
 
551
    gimp_container_add_handler (gimage->layers, "alpha_changed",
 
552
                                G_CALLBACK (gimp_image_layer_alpha_changed),
 
553
                                gimage);
 
554
 
 
555
  gimage->channel_update_handler =
 
556
    gimp_container_add_handler (gimage->channels, "update",
 
557
                                G_CALLBACK (gimp_image_drawable_update),
 
558
                                gimage);
 
559
  gimage->channel_visible_handler =
 
560
    gimp_container_add_handler (gimage->channels, "visibility_changed",
 
561
                                G_CALLBACK (gimp_image_drawable_visibility),
 
562
                                gimage);
 
563
  gimage->channel_name_changed_handler =
 
564
    gimp_container_add_handler (gimage->channels, "name_changed",
 
565
                                G_CALLBACK (gimp_image_channel_name_changed),
 
566
                                gimage);
 
567
  gimage->channel_color_changed_handler =
 
568
    gimp_container_add_handler (gimage->channels, "color_changed",
 
569
                                G_CALLBACK (gimp_image_channel_color_changed),
 
570
                                gimage);
 
571
 
 
572
  g_signal_connect (gimage->layers, "add",
 
573
                    G_CALLBACK (gimp_image_layer_add),
 
574
                    gimage);
 
575
  g_signal_connect (gimage->layers, "remove",
 
576
                    G_CALLBACK (gimp_image_layer_remove),
 
577
                    gimage);
 
578
 
 
579
  g_signal_connect (gimage->channels, "add",
 
580
                    G_CALLBACK (gimp_image_channel_add),
 
581
                    gimage);
 
582
  g_signal_connect (gimage->channels, "remove",
 
583
                    G_CALLBACK (gimp_image_channel_remove),
 
584
                    gimage);
 
585
 
 
586
  gimage->active_layer          = NULL;
 
587
  gimage->active_channel        = NULL;
 
588
  gimage->active_vectors        = NULL;
 
589
 
 
590
  gimage->floating_sel          = NULL;
 
591
  gimage->selection_mask        = NULL;
 
592
 
 
593
  gimage->parasites             = gimp_parasite_list_new ();
 
594
 
 
595
  for (i = 0; i < MAX_CHANNELS; i++)
 
596
    {
 
597
      gimage->visible[i] = TRUE;
 
598
      gimage->active[i]  = TRUE;
 
599
    }
 
600
 
 
601
  gimage->qmask_state           = FALSE;
 
602
  gimage->qmask_inverted        = FALSE;
 
603
  gimp_rgba_set (&gimage->qmask_color, 1.0, 0.0, 0.0, 0.5);
 
604
 
 
605
  gimage->undo_stack            = gimp_undo_stack_new (gimage);
 
606
  gimage->redo_stack            = gimp_undo_stack_new (gimage);
 
607
  gimage->group_count           = 0;
 
608
  gimage->pushing_undo_group    = GIMP_UNDO_GROUP_NONE;
 
609
 
 
610
  gimage->comp_preview          = NULL;
 
611
  gimage->comp_preview_valid    = FALSE;
 
612
 
 
613
  gimage->flush_accum.alpha_changed = FALSE;
 
614
  gimage->flush_accum.mask_changed  = FALSE;
 
615
}
 
616
 
 
617
static GObject *
 
618
gimp_image_constructor (GType                  type,
 
619
                        guint                  n_params,
 
620
                        GObjectConstructParam *params)
 
621
{
 
622
  GObject        *object;
 
623
  GimpImage      *gimage;
 
624
  GimpCoreConfig *config;
 
625
 
 
626
  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
 
627
 
 
628
  gimage = GIMP_IMAGE (object);
 
629
 
 
630
  g_assert (GIMP_IS_GIMP (gimage->gimp));
 
631
 
 
632
  config = gimage->gimp->config;
 
633
 
 
634
  gimage->ID = gimage->gimp->next_image_ID++;
 
635
 
 
636
  g_hash_table_insert (gimage->gimp->image_table,
 
637
                       GINT_TO_POINTER (gimage->ID),
 
638
                       gimage);
 
639
 
 
640
  gimage->xresolution     = config->default_image->xresolution;
 
641
  gimage->yresolution     = config->default_image->yresolution;
 
642
  gimage->resolution_unit = config->default_image->resolution_unit;
 
643
 
 
644
  gimage->grid = gimp_config_duplicate (GIMP_CONFIG (config->default_grid));
 
645
 
 
646
  switch (gimage->base_type)
 
647
    {
 
648
    case GIMP_RGB:
 
649
    case GIMP_GRAY:
 
650
      break;
 
651
    case GIMP_INDEXED:
 
652
      /* always allocate 256 colors for the colormap */
 
653
      gimage->num_cols = 0;
 
654
      gimage->cmap     = g_new0 (guchar, GIMP_IMAGE_COLORMAP_SIZE);
 
655
      break;
 
656
    default:
 
657
      break;
 
658
    }
 
659
 
 
660
  /* create the selection mask */
 
661
  gimage->selection_mask = gimp_selection_new (gimage,
 
662
                                               gimage->width,
 
663
                                               gimage->height);
 
664
  g_object_ref (gimage->selection_mask);
 
665
  gimp_item_sink (GIMP_ITEM (gimage->selection_mask));
 
666
 
 
667
  g_signal_connect (gimage->selection_mask, "update",
 
668
                    G_CALLBACK (gimp_image_mask_update),
 
669
                    gimage);
 
670
 
 
671
  g_signal_connect_object (config, "notify::transparency-type",
 
672
                           G_CALLBACK (gimp_image_invalidate_layer_previews),
 
673
                           gimage, G_CONNECT_SWAPPED);
 
674
  g_signal_connect_object (config, "notify::transparency-size",
 
675
                           G_CALLBACK (gimp_image_invalidate_layer_previews),
 
676
                           gimage, G_CONNECT_SWAPPED);
 
677
  g_signal_connect_object (config, "notify::layer-previews",
 
678
                           G_CALLBACK (gimp_viewable_size_changed),
 
679
                           gimage, G_CONNECT_SWAPPED);
 
680
 
 
681
  return object;
 
682
}
 
683
 
 
684
static void
 
685
gimp_image_set_property (GObject      *object,
 
686
                         guint         property_id,
 
687
                         const GValue *value,
 
688
                         GParamSpec   *pspec)
 
689
{
 
690
  GimpImage *gimage = GIMP_IMAGE (object);
 
691
 
 
692
  switch (property_id)
 
693
    {
 
694
    case PROP_GIMP:
 
695
      gimage->gimp = g_value_get_object (value);
 
696
      break;
 
697
    case PROP_ID:
 
698
      g_assert_not_reached ();
 
699
      break;
 
700
    case PROP_WIDTH:
 
701
      gimage->width = g_value_get_int (value);
 
702
      break;
 
703
    case PROP_HEIGHT:
 
704
      gimage->height = g_value_get_int (value);
 
705
      break;
 
706
    case PROP_BASE_TYPE:
 
707
      gimage->base_type = g_value_get_enum (value);
 
708
      break;
 
709
    default:
 
710
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
711
      break;
 
712
    }
 
713
}
 
714
 
 
715
static void
 
716
gimp_image_get_property (GObject    *object,
 
717
                         guint       property_id,
 
718
                         GValue     *value,
 
719
                         GParamSpec *pspec)
 
720
{
 
721
  GimpImage *gimage = GIMP_IMAGE (object);
 
722
 
 
723
  switch (property_id)
 
724
    {
 
725
    case PROP_GIMP:
 
726
      g_value_set_object (value, gimage->gimp);
 
727
      break;
 
728
    case PROP_ID:
 
729
      g_value_set_int (value, gimage->ID);
 
730
      break;
 
731
    case PROP_WIDTH:
 
732
      g_value_set_int (value, gimage->width);
 
733
      break;
 
734
    case PROP_HEIGHT:
 
735
      g_value_set_int (value, gimage->height);
 
736
      break;
 
737
    case PROP_BASE_TYPE:
 
738
      g_value_set_enum (value, gimage->base_type);
 
739
      break;
 
740
    default:
 
741
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
742
      break;
 
743
    }
 
744
}
 
745
 
 
746
static void
 
747
gimp_image_dispose (GObject *object)
 
748
{
 
749
  GimpImage *gimage = GIMP_IMAGE (object);
 
750
 
 
751
  gimp_image_undo_free (gimage);
 
752
 
 
753
  gimp_container_remove_handler (gimage->layers,
 
754
                                 gimage->layer_update_handler);
 
755
  gimp_container_remove_handler (gimage->layers,
 
756
                                 gimage->layer_visible_handler);
 
757
  gimp_container_remove_handler (gimage->layers,
 
758
                                 gimage->layer_alpha_handler);
 
759
 
 
760
  gimp_container_remove_handler (gimage->channels,
 
761
                                 gimage->channel_update_handler);
 
762
  gimp_container_remove_handler (gimage->channels,
 
763
                                 gimage->channel_visible_handler);
 
764
  gimp_container_remove_handler (gimage->channels,
 
765
                                 gimage->channel_name_changed_handler);
 
766
  gimp_container_remove_handler (gimage->channels,
 
767
                                 gimage->channel_color_changed_handler);
 
768
 
 
769
  g_signal_handlers_disconnect_by_func (gimage->layers,
 
770
                                        gimp_image_layer_add,
 
771
                                        gimage);
 
772
  g_signal_handlers_disconnect_by_func (gimage->layers,
 
773
                                        gimp_image_layer_remove,
 
774
                                        gimage);
 
775
 
 
776
  g_signal_handlers_disconnect_by_func (gimage->channels,
 
777
                                        gimp_image_channel_add,
 
778
                                        gimage);
 
779
  g_signal_handlers_disconnect_by_func (gimage->channels,
 
780
                                        gimp_image_channel_remove,
 
781
                                        gimage);
 
782
 
 
783
  gimp_container_foreach (gimage->layers,   (GFunc) gimp_item_removed, NULL);
 
784
  gimp_container_foreach (gimage->channels, (GFunc) gimp_item_removed, NULL);
 
785
  gimp_container_foreach (gimage->vectors,  (GFunc) gimp_item_removed, NULL);
 
786
 
 
787
  G_OBJECT_CLASS (parent_class)->dispose (object);
 
788
}
 
789
 
 
790
static void
 
791
gimp_image_finalize (GObject *object)
 
792
{
 
793
  GimpImage *gimage = GIMP_IMAGE (object);
 
794
 
 
795
  if (gimage->projection)
 
796
    {
 
797
      g_object_unref (gimage->projection);
 
798
      gimage->projection = NULL;
 
799
    }
 
800
 
 
801
  if (gimage->shadow)
 
802
    gimp_image_free_shadow (gimage);
 
803
 
 
804
  if (gimage->cmap)
 
805
    {
 
806
      g_free (gimage->cmap);
 
807
      gimage->cmap = NULL;
 
808
    }
 
809
 
 
810
  if (gimage->layers)
 
811
    {
 
812
      g_object_unref (gimage->layers);
 
813
      gimage->layers = NULL;
 
814
    }
 
815
  if (gimage->channels)
 
816
    {
 
817
      g_object_unref (gimage->channels);
 
818
      gimage->channels = NULL;
 
819
    }
 
820
  if (gimage->vectors)
 
821
    {
 
822
      g_object_unref (gimage->vectors);
 
823
      gimage->vectors = NULL;
 
824
    }
 
825
  if (gimage->layer_stack)
 
826
    {
 
827
      g_slist_free (gimage->layer_stack);
 
828
      gimage->layer_stack = NULL;
 
829
    }
 
830
 
 
831
  if (gimage->selection_mask)
 
832
    {
 
833
      g_object_unref (gimage->selection_mask);
 
834
      gimage->selection_mask = NULL;
 
835
    }
 
836
 
 
837
  if (gimage->comp_preview)
 
838
    {
 
839
      temp_buf_free (gimage->comp_preview);
 
840
      gimage->comp_preview = NULL;
 
841
    }
 
842
 
 
843
  if (gimage->parasites)
 
844
    {
 
845
      g_object_unref (gimage->parasites);
 
846
      gimage->parasites = NULL;
 
847
    }
 
848
 
 
849
  if (gimage->guides)
 
850
    {
 
851
      g_list_foreach (gimage->guides, (GFunc) gimp_image_guide_unref, NULL);
 
852
      g_list_free (gimage->guides);
 
853
      gimage->guides = NULL;
 
854
    }
 
855
 
 
856
  if (gimage->grid)
 
857
    {
 
858
      g_object_unref (gimage->grid);
 
859
      gimage->grid = NULL;
 
860
    }
 
861
 
 
862
  if (gimage->undo_stack)
 
863
    {
 
864
      g_object_unref (gimage->undo_stack);
 
865
      gimage->undo_stack = NULL;
 
866
    }
 
867
  if (gimage->redo_stack)
 
868
    {
 
869
      g_object_unref (gimage->redo_stack);
 
870
      gimage->redo_stack = NULL;
 
871
    }
 
872
 
 
873
  if (gimage->gimp && gimage->gimp->image_table)
 
874
    {
 
875
      g_hash_table_remove (gimage->gimp->image_table,
 
876
                           GINT_TO_POINTER (gimage->ID));
 
877
      gimage->gimp = NULL;
 
878
    }
 
879
 
 
880
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
881
}
 
882
 
 
883
static void
 
884
gimp_image_name_changed (GimpObject *object)
 
885
{
 
886
  const gchar *name;
 
887
 
 
888
  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
 
889
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
 
890
 
 
891
  name = gimp_object_get_name (object);
 
892
 
 
893
  if (! (name && strlen (name)))
 
894
    {
 
895
      g_free (object->name);
 
896
      object->name = NULL;
 
897
    }
 
898
}
 
899
 
 
900
static gint64
 
901
gimp_image_get_memsize (GimpObject *object,
 
902
                        gint64     *gui_size)
 
903
{
 
904
  GimpImage *gimage  = GIMP_IMAGE (object);
 
905
  gint64     memsize = 0;
 
906
 
 
907
  if (gimage->cmap)
 
908
    memsize += GIMP_IMAGE_COLORMAP_SIZE;
 
909
 
 
910
  if (gimage->shadow)
 
911
    memsize += tile_manager_get_memsize (gimage->shadow, FALSE);
 
912
 
 
913
  if (gimage->projection)
 
914
    memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->projection),
 
915
                                        gui_size);
 
916
 
 
917
  memsize += gimp_g_list_get_memsize (gimage->guides, sizeof (GimpGuide));
 
918
 
 
919
  if (gimage->grid)
 
920
    memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->grid), gui_size);
 
921
 
 
922
  memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->layers),
 
923
                                      gui_size);
 
924
  memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->channels),
 
925
                                      gui_size);
 
926
  memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->vectors),
 
927
                                      gui_size);
 
928
 
 
929
  memsize += gimp_g_slist_get_memsize (gimage->layer_stack, 0);
 
930
 
 
931
  if (gimage->selection_mask)
 
932
    memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->selection_mask),
 
933
                                        gui_size);
 
934
 
 
935
  memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->parasites),
 
936
                                      gui_size);
 
937
 
 
938
  memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->undo_stack),
 
939
                                      gui_size);
 
940
  memsize += gimp_object_get_memsize (GIMP_OBJECT (gimage->redo_stack),
 
941
                                      gui_size);
 
942
 
 
943
  if (gimage->comp_preview)
 
944
    *gui_size += temp_buf_get_memsize (gimage->comp_preview);
 
945
 
 
946
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
 
947
                                                                  gui_size);
 
948
}
 
949
 
 
950
static void
 
951
gimp_image_invalidate_preview (GimpViewable *viewable)
 
952
{
 
953
  GimpImage *gimage = GIMP_IMAGE (viewable);
 
954
 
 
955
  if (GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview)
 
956
    GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);
 
957
 
 
958
  gimage->comp_preview_valid = FALSE;
 
959
 
 
960
  if (gimage->comp_preview)
 
961
    {
 
962
      temp_buf_free (gimage->comp_preview);
 
963
      gimage->comp_preview = NULL;
 
964
    }
 
965
}
 
966
 
 
967
static void
 
968
gimp_image_size_changed (GimpViewable *viewable)
 
969
{
 
970
  GimpImage *gimage = GIMP_IMAGE (viewable);
 
971
  GList     *list;
 
972
 
 
973
  if (GIMP_VIEWABLE_CLASS (parent_class)->size_changed)
 
974
    GIMP_VIEWABLE_CLASS (parent_class)->size_changed (viewable);
 
975
 
 
976
  gimp_container_foreach (gimage->layers,
 
977
                          (GFunc) gimp_viewable_size_changed,
 
978
                          NULL);
 
979
  gimp_container_foreach (gimage->channels,
 
980
                          (GFunc) gimp_viewable_size_changed,
 
981
                          NULL);
 
982
  gimp_container_foreach (gimage->vectors,
 
983
                          (GFunc) gimp_viewable_size_changed,
 
984
                          NULL);
 
985
 
 
986
  for (list = GIMP_LIST (gimage->layers)->list; list; list = g_list_next (list))
 
987
    {
 
988
      GimpLayerMask *mask = gimp_layer_get_mask (GIMP_LAYER (list->data));
 
989
 
 
990
      if (mask)
 
991
        gimp_viewable_size_changed (GIMP_VIEWABLE (mask));
 
992
    }
 
993
 
 
994
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimp_image_get_mask (gimage)));
 
995
}
 
996
 
 
997
static gchar *
 
998
gimp_image_get_description (GimpViewable  *viewable,
 
999
                            gchar        **tooltip)
 
1000
{
 
1001
  GimpImage   *gimage = GIMP_IMAGE (viewable);
 
1002
  const gchar *uri;
 
1003
  gchar       *basename;
 
1004
  gchar       *retval;
 
1005
 
 
1006
  uri = gimp_image_get_uri (GIMP_IMAGE (gimage));
 
1007
 
 
1008
  basename = file_utils_uri_to_utf8_basename (uri);
 
1009
 
 
1010
  if (tooltip)
 
1011
    *tooltip = file_utils_uri_to_utf8_filename (uri);
 
1012
 
 
1013
  retval = g_strdup_printf ("%s-%d", basename, gimp_image_get_ID (gimage));
 
1014
 
 
1015
  g_free (basename);
 
1016
 
 
1017
  return retval;
 
1018
}
 
1019
 
 
1020
static void
 
1021
gimp_image_real_colormap_changed (GimpImage *gimage,
 
1022
                                  gint       color_index)
 
1023
{
 
1024
  if (gimp_image_base_type (gimage) == GIMP_INDEXED)
 
1025
    {
 
1026
      gimp_image_color_hash_invalidate (gimage, color_index);
 
1027
 
 
1028
      /* A colormap alteration affects the whole image */
 
1029
      gimp_image_update (gimage, 0, 0, gimage->width, gimage->height);
 
1030
 
 
1031
      gimp_image_invalidate_layer_previews (gimage);
 
1032
      gimp_viewable_invalidate_preview (GIMP_VIEWABLE (gimage));
 
1033
    }
 
1034
}
 
1035
 
 
1036
void
 
1037
gimp_image_real_flush (GimpImage *gimage)
 
1038
{
 
1039
  if (gimage->flush_accum.alpha_changed)
 
1040
    {
 
1041
      gimp_image_alpha_changed (gimage);
 
1042
      gimage->flush_accum.alpha_changed = FALSE;
 
1043
    }
 
1044
 
 
1045
  if (gimage->flush_accum.mask_changed)
 
1046
    {
 
1047
      gimp_image_mask_changed (gimage);
 
1048
      gimage->flush_accum.mask_changed = FALSE;
 
1049
    }
 
1050
}
 
1051
 
 
1052
static void
 
1053
gimp_image_mask_update (GimpDrawable *drawable,
 
1054
                        gint          x,
 
1055
                        gint          y,
 
1056
                        gint          width,
 
1057
                        gint          height,
 
1058
                        GimpImage    *gimage)
 
1059
{
 
1060
  gimage->flush_accum.mask_changed = TRUE;
 
1061
}
 
1062
 
 
1063
static void
 
1064
gimp_image_drawable_update (GimpDrawable *drawable,
 
1065
                            gint          x,
 
1066
                            gint          y,
 
1067
                            gint          width,
 
1068
                            gint          height,
 
1069
                            GimpImage    *gimage)
 
1070
{
 
1071
  GimpItem *item = GIMP_ITEM (drawable);
 
1072
 
 
1073
  if (gimp_item_get_visible (item))
 
1074
    {
 
1075
      gint offset_x;
 
1076
      gint offset_y;
 
1077
 
 
1078
      gimp_item_offsets (item, &offset_x, &offset_y);
 
1079
      x += offset_x;
 
1080
      y += offset_y;
 
1081
 
 
1082
      gimp_image_update (gimage, x, y, width, height);
 
1083
      gimp_viewable_invalidate_preview (GIMP_VIEWABLE (gimage));
 
1084
    }
 
1085
}
 
1086
 
 
1087
static void
 
1088
gimp_image_drawable_visibility (GimpItem  *item,
 
1089
                                GimpImage *gimage)
 
1090
{
 
1091
  gint offset_x;
 
1092
  gint offset_y;
 
1093
 
 
1094
  gimp_item_offsets (item, &offset_x, &offset_y);
 
1095
 
 
1096
  gimp_image_update (gimage,
 
1097
                     offset_x, offset_y,
 
1098
                     gimp_item_width (item),
 
1099
                     gimp_item_height (item));
 
1100
  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (gimage));
 
1101
}
 
1102
 
 
1103
static void
 
1104
gimp_image_layer_alpha_changed (GimpDrawable *drawable,
 
1105
                                GimpImage    *gimage)
 
1106
{
 
1107
  if (gimp_container_num_children (gimage->layers) == 1)
 
1108
    gimage->flush_accum.alpha_changed = TRUE;
 
1109
}
 
1110
 
 
1111
static void
 
1112
gimp_image_layer_add (GimpContainer *container,
 
1113
                      GimpLayer     *layer,
 
1114
                      GimpImage     *gimage)
 
1115
{
 
1116
  GimpItem *item = GIMP_ITEM (layer);
 
1117
 
 
1118
  if (gimp_item_get_visible (item))
 
1119
    gimp_image_drawable_visibility (item, gimage);
 
1120
}
 
1121
 
 
1122
static void
 
1123
gimp_image_layer_remove (GimpContainer *container,
 
1124
                         GimpLayer     *layer,
 
1125
                         GimpImage     *gimage)
 
1126
{
 
1127
  GimpItem *item = GIMP_ITEM (layer);
 
1128
 
 
1129
  if (gimp_item_get_visible (item))
 
1130
    gimp_image_drawable_visibility (item, gimage);
 
1131
}
 
1132
 
 
1133
static void
 
1134
gimp_image_channel_add (GimpContainer *container,
 
1135
                        GimpChannel   *channel,
 
1136
                        GimpImage     *gimage)
 
1137
{
 
1138
  GimpItem *item = GIMP_ITEM (channel);
 
1139
 
 
1140
  if (gimp_item_get_visible (item))
 
1141
    gimp_image_drawable_visibility (item, gimage);
 
1142
 
 
1143
  if (! strcmp (GIMP_IMAGE_QMASK_NAME,
 
1144
                gimp_object_get_name (GIMP_OBJECT (channel))))
 
1145
    {
 
1146
      gimp_image_set_qmask_state (gimage, TRUE);
 
1147
    }
 
1148
}
 
1149
 
 
1150
static void
 
1151
gimp_image_channel_remove (GimpContainer *container,
 
1152
                           GimpChannel   *channel,
 
1153
                           GimpImage     *gimage)
 
1154
{
 
1155
  GimpItem *item = GIMP_ITEM (channel);
 
1156
 
 
1157
  if (gimp_item_get_visible (item))
 
1158
    gimp_image_drawable_visibility (item, gimage);
 
1159
 
 
1160
  if (! strcmp (GIMP_IMAGE_QMASK_NAME,
 
1161
                gimp_object_get_name (GIMP_OBJECT (channel))))
 
1162
    {
 
1163
      gimp_image_set_qmask_state (gimage, FALSE);
 
1164
    }
 
1165
}
 
1166
 
 
1167
static void
 
1168
gimp_image_channel_name_changed (GimpChannel *channel,
 
1169
                                 GimpImage   *gimage)
 
1170
{
 
1171
  if (! strcmp (GIMP_IMAGE_QMASK_NAME,
 
1172
                gimp_object_get_name (GIMP_OBJECT (channel))))
 
1173
    {
 
1174
      gimp_image_set_qmask_state (gimage, TRUE);
 
1175
    }
 
1176
  else if (gimp_image_get_qmask_state (gimage) &&
 
1177
           ! gimp_image_get_qmask (gimage))
 
1178
    {
 
1179
      gimp_image_set_qmask_state (gimage, FALSE);
 
1180
    }
 
1181
}
 
1182
 
 
1183
static void
 
1184
gimp_image_channel_color_changed (GimpChannel *channel,
 
1185
                                  GimpImage   *gimage)
 
1186
{
 
1187
  if (! strcmp (GIMP_IMAGE_QMASK_NAME,
 
1188
                gimp_object_get_name (GIMP_OBJECT (channel))))
 
1189
    {
 
1190
      gimage->qmask_color = channel->color;
 
1191
    }
 
1192
}
 
1193
 
 
1194
 
 
1195
/*  public functions  */
 
1196
 
 
1197
GimpImage *
 
1198
gimp_image_new (Gimp              *gimp,
 
1199
                gint               width,
 
1200
                gint               height,
 
1201
                GimpImageBaseType  base_type)
 
1202
{
 
1203
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
1204
 
 
1205
  return g_object_new (GIMP_TYPE_IMAGE,
 
1206
                       "gimp",      gimp,
 
1207
                       "width",     width,
 
1208
                       "height",    height,
 
1209
                       "base-type", base_type,
 
1210
                       NULL);
 
1211
}
 
1212
 
 
1213
GimpImageBaseType
 
1214
gimp_image_base_type (const GimpImage *gimage)
 
1215
{
 
1216
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
1217
 
 
1218
  return gimage->base_type;
 
1219
}
 
1220
 
 
1221
GimpImageType
 
1222
gimp_image_base_type_with_alpha (const GimpImage *gimage)
 
1223
{
 
1224
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
1225
 
 
1226
  switch (gimage->base_type)
 
1227
    {
 
1228
    case GIMP_RGB:
 
1229
      return GIMP_RGBA_IMAGE;
 
1230
    case GIMP_GRAY:
 
1231
      return GIMP_GRAYA_IMAGE;
 
1232
    case GIMP_INDEXED:
 
1233
      return GIMP_INDEXEDA_IMAGE;
 
1234
    }
 
1235
 
 
1236
  return GIMP_RGB_IMAGE;
 
1237
}
 
1238
 
 
1239
CombinationMode
 
1240
gimp_image_get_combination_mode (GimpImageType dest_type,
 
1241
                                 gint          src_bytes)
 
1242
{
 
1243
  return valid_combinations[dest_type][src_bytes];
 
1244
}
 
1245
 
 
1246
gint
 
1247
gimp_image_get_ID (const GimpImage *gimage)
 
1248
{
 
1249
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
1250
 
 
1251
  return gimage->ID;
 
1252
}
 
1253
 
 
1254
GimpImage *
 
1255
gimp_image_get_by_ID (Gimp *gimp,
 
1256
                      gint  image_id)
 
1257
{
 
1258
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
1259
 
 
1260
  if (gimp->image_table == NULL)
 
1261
    return NULL;
 
1262
 
 
1263
  return (GimpImage *) g_hash_table_lookup (gimp->image_table,
 
1264
                                            GINT_TO_POINTER (image_id));
 
1265
}
 
1266
 
 
1267
void
 
1268
gimp_image_set_uri (GimpImage   *gimage,
 
1269
                    const gchar *uri)
 
1270
{
 
1271
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1272
 
 
1273
  gimp_object_set_name (GIMP_OBJECT (gimage), uri);
 
1274
}
 
1275
 
 
1276
const gchar *
 
1277
gimp_image_get_uri (const GimpImage *gimage)
 
1278
{
 
1279
  const gchar *uri;
 
1280
 
 
1281
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
1282
 
 
1283
  uri = gimp_object_get_name (GIMP_OBJECT (gimage));
 
1284
 
 
1285
  return uri ? uri : _("Untitled");
 
1286
}
 
1287
 
 
1288
void
 
1289
gimp_image_set_filename (GimpImage   *gimage,
 
1290
                         const gchar *filename)
 
1291
{
 
1292
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1293
 
 
1294
  if (filename && strlen (filename))
 
1295
    {
 
1296
      gchar *uri;
 
1297
 
 
1298
      uri = file_utils_filename_to_uri (gimage->gimp->load_procs, filename,
 
1299
                                        NULL);
 
1300
 
 
1301
      gimp_image_set_uri (gimage, uri);
 
1302
 
 
1303
      g_free (uri);
 
1304
    }
 
1305
  else
 
1306
    {
 
1307
      gimp_image_set_uri (gimage, NULL);
 
1308
    }
 
1309
}
 
1310
 
 
1311
gchar *
 
1312
gimp_image_get_filename (const GimpImage *gimage)
 
1313
{
 
1314
  const gchar *uri;
 
1315
 
 
1316
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
1317
 
 
1318
  uri = gimp_object_get_name (GIMP_OBJECT (gimage));
 
1319
 
 
1320
  if (! uri)
 
1321
    return NULL;
 
1322
 
 
1323
  return g_filename_from_uri (uri, NULL, NULL);
 
1324
}
 
1325
 
 
1326
void
 
1327
gimp_image_set_save_proc (GimpImage     *gimage,
 
1328
                          PlugInProcDef *proc)
 
1329
{
 
1330
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1331
 
 
1332
  gimage->save_proc = proc;
 
1333
}
 
1334
 
 
1335
PlugInProcDef *
 
1336
gimp_image_get_save_proc (const GimpImage *gimage)
 
1337
{
 
1338
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
1339
 
 
1340
  return gimage->save_proc;
 
1341
}
 
1342
 
 
1343
void
 
1344
gimp_image_set_resolution (GimpImage *gimage,
 
1345
                           gdouble    xresolution,
 
1346
                           gdouble    yresolution)
 
1347
{
 
1348
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1349
 
 
1350
  /* don't allow to set the resolution out of bounds */
 
1351
  if (xresolution < GIMP_MIN_RESOLUTION || xresolution > GIMP_MAX_RESOLUTION ||
 
1352
      yresolution < GIMP_MIN_RESOLUTION || yresolution > GIMP_MAX_RESOLUTION)
 
1353
    return;
 
1354
 
 
1355
  if ((ABS (gimage->xresolution - xresolution) >= 1e-5) ||
 
1356
      (ABS (gimage->yresolution - yresolution) >= 1e-5))
 
1357
    {
 
1358
      gimp_image_undo_push_image_resolution (gimage,
 
1359
                                             _("Change Image Resolution"));
 
1360
 
 
1361
      gimage->xresolution = xresolution;
 
1362
      gimage->yresolution = yresolution;
 
1363
 
 
1364
      gimp_image_resolution_changed (gimage);
 
1365
      gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
 
1366
    }
 
1367
}
 
1368
 
 
1369
void
 
1370
gimp_image_get_resolution (const GimpImage *gimage,
 
1371
                           gdouble         *xresolution,
 
1372
                           gdouble         *yresolution)
 
1373
{
 
1374
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1375
  g_return_if_fail (xresolution && yresolution);
 
1376
 
 
1377
  *xresolution = gimage->xresolution;
 
1378
  *yresolution = gimage->yresolution;
 
1379
}
 
1380
 
 
1381
void
 
1382
gimp_image_resolution_changed (GimpImage *gimage)
 
1383
{
 
1384
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1385
 
 
1386
  g_signal_emit (gimage, gimp_image_signals[RESOLUTION_CHANGED], 0);
 
1387
}
 
1388
 
 
1389
void
 
1390
gimp_image_set_unit (GimpImage *gimage,
 
1391
                     GimpUnit   unit)
 
1392
{
 
1393
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1394
  g_return_if_fail (unit > GIMP_UNIT_PIXEL);
 
1395
 
 
1396
  if (gimage->resolution_unit != unit)
 
1397
    {
 
1398
      gimp_image_undo_push_image_resolution (gimage,
 
1399
                                             _("Change Image Unit"));
 
1400
 
 
1401
      gimage->resolution_unit = unit;
 
1402
      gimp_image_unit_changed (gimage);
 
1403
    }
 
1404
}
 
1405
 
 
1406
GimpUnit
 
1407
gimp_image_get_unit (const GimpImage *gimage)
 
1408
{
 
1409
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), GIMP_UNIT_INCH);
 
1410
 
 
1411
  return gimage->resolution_unit;
 
1412
}
 
1413
 
 
1414
void
 
1415
gimp_image_unit_changed (GimpImage *gimage)
 
1416
{
 
1417
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1418
 
 
1419
  g_signal_emit (gimage, gimp_image_signals[UNIT_CHANGED], 0);
 
1420
}
 
1421
 
 
1422
gint
 
1423
gimp_image_get_width (const GimpImage *gimage)
 
1424
{
 
1425
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), 0);
 
1426
 
 
1427
  return gimage->width;
 
1428
}
 
1429
 
 
1430
gint
 
1431
gimp_image_get_height (const GimpImage *gimage)
 
1432
{
 
1433
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), 0);
 
1434
 
 
1435
  return gimage->height;
 
1436
}
 
1437
 
 
1438
gboolean
 
1439
gimp_image_has_alpha (const GimpImage *gimage)
 
1440
{
 
1441
  GimpLayer *layer;
 
1442
 
 
1443
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), TRUE);
 
1444
 
 
1445
  layer = (GimpLayer *) gimp_container_get_child_by_index (gimage->layers, 0);
 
1446
 
 
1447
  return ((gimp_container_num_children (gimage->layers) > 1) ||
 
1448
          (layer && gimp_drawable_has_alpha (GIMP_DRAWABLE (layer))));
 
1449
}
 
1450
 
 
1451
gboolean
 
1452
gimp_image_is_empty (const GimpImage *gimage)
 
1453
{
 
1454
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), TRUE);
 
1455
 
 
1456
  return (gimp_container_num_children (gimage->layers) == 0);
 
1457
}
 
1458
 
 
1459
GimpLayer *
 
1460
gimp_image_floating_sel (const GimpImage *gimage)
 
1461
{
 
1462
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
1463
 
 
1464
  return gimage->floating_sel;
 
1465
}
 
1466
 
 
1467
void
 
1468
gimp_image_floating_selection_changed (GimpImage *gimage)
 
1469
{
 
1470
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1471
 
 
1472
  g_signal_emit (gimage, gimp_image_signals[FLOATING_SELECTION_CHANGED], 0);
 
1473
}
 
1474
 
 
1475
GimpChannel *
 
1476
gimp_image_get_mask (const GimpImage *gimage)
 
1477
{
 
1478
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
1479
 
 
1480
  return gimage->selection_mask;
 
1481
}
 
1482
 
 
1483
void
 
1484
gimp_image_mask_changed (GimpImage *gimage)
 
1485
{
 
1486
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1487
 
 
1488
  g_signal_emit (gimage, gimp_image_signals[MASK_CHANGED], 0);
 
1489
}
 
1490
 
 
1491
gint
 
1492
gimp_image_get_component_index (const GimpImage *gimage,
 
1493
                                GimpChannelType  channel)
 
1494
{
 
1495
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
1496
 
 
1497
  switch (channel)
 
1498
    {
 
1499
    case GIMP_RED_CHANNEL:     return RED_PIX;
 
1500
    case GIMP_GREEN_CHANNEL:   return GREEN_PIX;
 
1501
    case GIMP_BLUE_CHANNEL:    return BLUE_PIX;
 
1502
    case GIMP_GRAY_CHANNEL:    return GRAY_PIX;
 
1503
    case GIMP_INDEXED_CHANNEL: return INDEXED_PIX;
 
1504
    case GIMP_ALPHA_CHANNEL:
 
1505
      switch (gimp_image_base_type (gimage))
 
1506
        {
 
1507
        case GIMP_RGB:     return ALPHA_PIX;
 
1508
        case GIMP_GRAY:    return ALPHA_G_PIX;
 
1509
        case GIMP_INDEXED: return ALPHA_I_PIX;
 
1510
        }
 
1511
    }
 
1512
 
 
1513
  return -1;
 
1514
}
 
1515
 
 
1516
void
 
1517
gimp_image_set_component_active (GimpImage       *gimage,
 
1518
                                 GimpChannelType  channel,
 
1519
                                 gboolean         active)
 
1520
{
 
1521
  gint index = -1;
 
1522
 
 
1523
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1524
 
 
1525
  index = gimp_image_get_component_index (gimage, channel);
 
1526
 
 
1527
  if (index != -1 && active != gimage->active[index])
 
1528
    {
 
1529
      gimage->active[index] = active ? TRUE : FALSE;
 
1530
 
 
1531
      /*  If there is an active channel and we mess with the components,
 
1532
       *  the active channel gets unset...
 
1533
       */
 
1534
      gimp_image_unset_active_channel (gimage);
 
1535
 
 
1536
      g_signal_emit (gimage,
 
1537
                     gimp_image_signals[COMPONENT_ACTIVE_CHANGED], 0,
 
1538
                     channel);
 
1539
    }
 
1540
}
 
1541
 
 
1542
gboolean
 
1543
gimp_image_get_component_active (const GimpImage *gimage,
 
1544
                                 GimpChannelType  channel)
 
1545
{
 
1546
  gint index = -1;
 
1547
 
 
1548
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1549
 
 
1550
  index = gimp_image_get_component_index (gimage, channel);
 
1551
 
 
1552
  if (index != -1)
 
1553
    return gimage->active[index];
 
1554
 
 
1555
  return FALSE;
 
1556
}
 
1557
 
 
1558
void
 
1559
gimp_image_set_component_visible (GimpImage       *gimage,
 
1560
                                  GimpChannelType  channel,
 
1561
                                  gboolean         visible)
 
1562
{
 
1563
  gint index = -1;
 
1564
 
 
1565
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1566
 
 
1567
  index = gimp_image_get_component_index (gimage, channel);
 
1568
 
 
1569
  if (index != -1 && visible != gimage->visible[index])
 
1570
    {
 
1571
      gimage->visible[index] = visible ? TRUE : FALSE;
 
1572
 
 
1573
      g_signal_emit (gimage,
 
1574
                     gimp_image_signals[COMPONENT_VISIBILITY_CHANGED], 0,
 
1575
                     channel);
 
1576
 
 
1577
      gimp_image_update (gimage, 0, 0, gimage->width, gimage->height);
 
1578
      gimp_viewable_invalidate_preview (GIMP_VIEWABLE (gimage));
 
1579
    }
 
1580
}
 
1581
 
 
1582
gboolean
 
1583
gimp_image_get_component_visible (const GimpImage *gimage,
 
1584
                                  GimpChannelType  channel)
 
1585
{
 
1586
  gint index = -1;
 
1587
 
 
1588
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1589
 
 
1590
  index = gimp_image_get_component_index (gimage, channel);
 
1591
 
 
1592
  if (index != -1)
 
1593
    return gimage->visible[index];
 
1594
 
 
1595
  return FALSE;
 
1596
}
 
1597
 
 
1598
void
 
1599
gimp_image_mode_changed (GimpImage *gimage)
 
1600
{
 
1601
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1602
 
 
1603
  g_signal_emit (gimage, gimp_image_signals[MODE_CHANGED], 0);
 
1604
}
 
1605
 
 
1606
void
 
1607
gimp_image_alpha_changed (GimpImage *gimage)
 
1608
{
 
1609
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1610
 
 
1611
  g_signal_emit (gimage, gimp_image_signals[ALPHA_CHANGED], 0);
 
1612
}
 
1613
 
 
1614
void
 
1615
gimp_image_update (GimpImage *gimage,
 
1616
                   gint       x,
 
1617
                   gint       y,
 
1618
                   gint       width,
 
1619
                   gint       height)
 
1620
{
 
1621
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1622
 
 
1623
  g_signal_emit (gimage, gimp_image_signals[UPDATE], 0,
 
1624
                 x, y, width, height);
 
1625
}
 
1626
 
 
1627
void
 
1628
gimp_image_update_guide (GimpImage *gimage,
 
1629
                         GimpGuide *guide)
 
1630
{
 
1631
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1632
  g_return_if_fail (guide != NULL);
 
1633
 
 
1634
  g_signal_emit (gimage, gimp_image_signals[UPDATE_GUIDE], 0, guide);
 
1635
}
 
1636
 
 
1637
void
 
1638
gimp_image_colormap_changed (GimpImage *gimage,
 
1639
                             gint       color_index)
 
1640
{
 
1641
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1642
  g_return_if_fail (color_index >= -1 && color_index < gimage->num_cols);
 
1643
 
 
1644
  g_signal_emit (gimage, gimp_image_signals[COLORMAP_CHANGED], 0,
 
1645
                 color_index);
 
1646
}
 
1647
 
 
1648
void
 
1649
gimp_image_selection_control (GimpImage            *gimage,
 
1650
                              GimpSelectionControl  control)
 
1651
{
 
1652
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1653
 
 
1654
  g_signal_emit (gimage, gimp_image_signals[SELECTION_CONTROL], 0, control);
 
1655
}
 
1656
 
 
1657
void
 
1658
gimp_image_qmask_changed (GimpImage *gimage)
 
1659
{
 
1660
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1661
 
 
1662
  g_signal_emit (gimage, gimp_image_signals[QMASK_CHANGED], 0);
 
1663
}
 
1664
 
 
1665
 
 
1666
/*  undo  */
 
1667
 
 
1668
gboolean
 
1669
gimp_image_undo_is_enabled (const GimpImage *gimage)
 
1670
{
 
1671
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1672
 
 
1673
  return (gimage->undo_freeze_count == 0);
 
1674
}
 
1675
 
 
1676
gboolean
 
1677
gimp_image_undo_enable (GimpImage *gimage)
 
1678
{
 
1679
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1680
 
 
1681
  /*  Free all undo steps as they are now invalidated  */
 
1682
  gimp_image_undo_free (gimage);
 
1683
 
 
1684
  return gimp_image_undo_thaw (gimage);
 
1685
}
 
1686
 
 
1687
gboolean
 
1688
gimp_image_undo_disable (GimpImage *gimage)
 
1689
{
 
1690
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1691
 
 
1692
  return gimp_image_undo_freeze (gimage);
 
1693
}
 
1694
 
 
1695
gboolean
 
1696
gimp_image_undo_freeze (GimpImage *gimage)
 
1697
{
 
1698
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1699
 
 
1700
  gimage->undo_freeze_count++;
 
1701
 
 
1702
  if (gimage->undo_freeze_count == 1)
 
1703
    gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_UNDO_FREEZE, NULL);
 
1704
 
 
1705
  return TRUE;
 
1706
}
 
1707
 
 
1708
gboolean
 
1709
gimp_image_undo_thaw (GimpImage *gimage)
 
1710
{
 
1711
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1712
  g_return_val_if_fail (gimage->undo_freeze_count > 0, FALSE);
 
1713
 
 
1714
  gimage->undo_freeze_count--;
 
1715
 
 
1716
  if (gimage->undo_freeze_count == 0)
 
1717
    gimp_image_undo_event (gimage, GIMP_UNDO_EVENT_UNDO_THAW, NULL);
 
1718
 
 
1719
  return TRUE;
 
1720
}
 
1721
 
 
1722
void
 
1723
gimp_image_undo_event (GimpImage     *gimage,
 
1724
                       GimpUndoEvent  event,
 
1725
                       GimpUndo      *undo)
 
1726
{
 
1727
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1728
  g_return_if_fail (((event == GIMP_UNDO_EVENT_UNDO_FREE   ||
 
1729
                      event == GIMP_UNDO_EVENT_UNDO_FREEZE ||
 
1730
                      event == GIMP_UNDO_EVENT_UNDO_THAW) && undo == NULL) ||
 
1731
                    GIMP_IS_UNDO (undo));
 
1732
 
 
1733
  g_signal_emit (gimage, gimp_image_signals[UNDO_EVENT], 0, event, undo);
 
1734
}
 
1735
 
 
1736
 
 
1737
/* NOTE about the gimage->dirty counter:
 
1738
 *   If 0, then the image is clean (ie, copy on disk is the same as the one
 
1739
 *      in memory).
 
1740
 *   If positive, then that's the number of dirtying operations done
 
1741
 *       on the image since the last save.
 
1742
 *   If negative, then user has hit undo and gone back in time prior
 
1743
 *       to the saved copy.  Hitting redo will eventually come back to
 
1744
 *       the saved copy.
 
1745
 *
 
1746
 *   The image is dirty (ie, needs saving) if counter is non-zero.
 
1747
 *
 
1748
 *   If the counter is around 10000, this is due to undo-ing back
 
1749
 *   before a saved version, then mutating the image (thus destroying
 
1750
 *   the redo stack).  Once this has happened, it's impossible to get
 
1751
 *   the image back to the state on disk, since the redo info has been
 
1752
 *   freed.  See undo.c for the gorey details.
 
1753
 */
 
1754
 
 
1755
 
 
1756
/*
 
1757
 * NEVER CALL gimp_image_dirty() directly!
 
1758
 *
 
1759
 * If your code has just dirtied the image, push an undo instead.
 
1760
 * Failing that, push the trivial undo which tells the user the
 
1761
 * command is not undoable: undo_push_cantundo() (But really, it would
 
1762
 * be best to push a proper undo).  If you just dirty the image
 
1763
 * without pushing an undo then the dirty count is increased, but
 
1764
 * popping that many undo actions won't lead to a clean image.
 
1765
 */
 
1766
 
 
1767
gint
 
1768
gimp_image_dirty (GimpImage     *gimage,
 
1769
                  GimpDirtyMask  dirty_mask)
 
1770
{
 
1771
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1772
 
 
1773
  gimage->dirty++;
 
1774
 
 
1775
  if (! gimage->dirty_time)
 
1776
    gimage->dirty_time = time (NULL);
 
1777
 
 
1778
  g_signal_emit (gimage, gimp_image_signals[DIRTY], 0, dirty_mask);
 
1779
 
 
1780
  TRC (("dirty %d -> %d\n", gimage->dirty - 1, gimage->dirty));
 
1781
 
 
1782
  return gimage->dirty;
 
1783
}
 
1784
 
 
1785
gint
 
1786
gimp_image_clean (GimpImage     *gimage,
 
1787
                  GimpDirtyMask  dirty_mask)
 
1788
{
 
1789
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
1790
 
 
1791
  gimage->dirty--;
 
1792
 
 
1793
  g_signal_emit (gimage, gimp_image_signals[CLEAN], 0, dirty_mask);
 
1794
 
 
1795
  TRC (("clean %d -> %d\n", gimage->dirty + 1, gimage->dirty));
 
1796
 
 
1797
  return gimage->dirty;
 
1798
}
 
1799
 
 
1800
void
 
1801
gimp_image_clean_all (GimpImage *gimage)
 
1802
{
 
1803
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1804
 
 
1805
  gimage->dirty      = 0;
 
1806
  gimage->dirty_time = 0;
 
1807
 
 
1808
  g_signal_emit (gimage, gimp_image_signals[CLEAN], 0);
 
1809
}
 
1810
 
 
1811
 
 
1812
/*  flush this image's displays  */
 
1813
 
 
1814
void
 
1815
gimp_image_flush (GimpImage *gimage)
 
1816
{
 
1817
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1818
 
 
1819
  g_signal_emit (gimage, gimp_image_signals[FLUSH], 0);
 
1820
}
 
1821
 
 
1822
 
 
1823
/*  color transforms / utilities  */
 
1824
 
 
1825
void
 
1826
gimp_image_get_foreground (const GimpImage    *gimage,
 
1827
                           const GimpDrawable *drawable,
 
1828
                           GimpContext        *context,
 
1829
                           guchar             *fg)
 
1830
{
 
1831
  GimpRGB  color;
 
1832
  guchar   pfg[3];
 
1833
 
 
1834
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1835
  g_return_if_fail (! drawable || GIMP_IS_DRAWABLE (drawable));
 
1836
  g_return_if_fail (GIMP_IS_CONTEXT (context));
 
1837
  g_return_if_fail (fg != NULL);
 
1838
 
 
1839
  gimp_context_get_foreground (context, &color);
 
1840
 
 
1841
  gimp_rgb_get_uchar (&color, &pfg[0], &pfg[1], &pfg[2]);
 
1842
 
 
1843
  gimp_image_transform_color (gimage, drawable, fg, GIMP_RGB, pfg);
 
1844
}
 
1845
 
 
1846
void
 
1847
gimp_image_get_background (const GimpImage    *gimage,
 
1848
                           const GimpDrawable *drawable,
 
1849
                           GimpContext        *context,
 
1850
                           guchar             *bg)
 
1851
{
 
1852
  GimpRGB  color;
 
1853
  guchar   pbg[3];
 
1854
 
 
1855
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
1856
  g_return_if_fail (! drawable || GIMP_IS_DRAWABLE (drawable));
 
1857
  g_return_if_fail (GIMP_IS_CONTEXT (context));
 
1858
  g_return_if_fail (bg != NULL);
 
1859
 
 
1860
  gimp_context_get_background (context, &color);
 
1861
 
 
1862
  gimp_rgb_get_uchar (&color, &pbg[0], &pbg[1], &pbg[2]);
 
1863
 
 
1864
  gimp_image_transform_color (gimage, drawable, bg, GIMP_RGB, pbg);
 
1865
}
 
1866
 
 
1867
void
 
1868
gimp_image_get_color (const GimpImage *src_gimage,
 
1869
                      GimpImageType    src_type,
 
1870
                      const guchar    *src,
 
1871
                      guchar          *rgba)
 
1872
{
 
1873
  gboolean has_alpha = FALSE;
 
1874
 
 
1875
  g_return_if_fail (GIMP_IS_IMAGE (src_gimage));
 
1876
 
 
1877
  switch (src_type)
 
1878
    {
 
1879
    case GIMP_RGBA_IMAGE:
 
1880
      has_alpha = TRUE;
 
1881
 
 
1882
    case GIMP_RGB_IMAGE:
 
1883
      /*  Straight copy  */
 
1884
      *rgba++ = *src++;
 
1885
      *rgba++ = *src++;
 
1886
      *rgba++ = *src++;
 
1887
      break;
 
1888
 
 
1889
    case GIMP_GRAYA_IMAGE:
 
1890
      has_alpha = TRUE;
 
1891
 
 
1892
    case GIMP_GRAY_IMAGE:
 
1893
      /*  Gray to RG&B */
 
1894
      *rgba++ = *src;
 
1895
      *rgba++ = *src;
 
1896
      *rgba++ = *src++;
 
1897
      break;
 
1898
 
 
1899
    case GIMP_INDEXEDA_IMAGE:
 
1900
      has_alpha = TRUE;
 
1901
 
 
1902
    case GIMP_INDEXED_IMAGE:
 
1903
      /*  Indexed palette lookup  */
 
1904
      {
 
1905
        gint index = *src++ * 3;
 
1906
 
 
1907
        *rgba++ = src_gimage->cmap[index++];
 
1908
        *rgba++ = src_gimage->cmap[index++];
 
1909
        *rgba++ = src_gimage->cmap[index++];
 
1910
      }
 
1911
      break;
 
1912
    }
 
1913
 
 
1914
  if (has_alpha)
 
1915
    *rgba = *src;
 
1916
  else
 
1917
    *rgba = OPAQUE_OPACITY;
 
1918
}
 
1919
 
 
1920
void
 
1921
gimp_image_transform_rgb (const GimpImage    *dest_gimage,
 
1922
                          const GimpDrawable *dest_drawable,
 
1923
                          const GimpRGB      *rgb,
 
1924
                          guchar             *color)
 
1925
{
 
1926
  guchar col[3];
 
1927
 
 
1928
  g_return_if_fail (GIMP_IS_IMAGE (dest_gimage));
 
1929
  g_return_if_fail (! dest_drawable || GIMP_IS_DRAWABLE (dest_drawable));
 
1930
  g_return_if_fail (rgb != NULL);
 
1931
  g_return_if_fail (color != NULL);
 
1932
 
 
1933
  gimp_rgb_get_uchar (rgb, &col[0], &col[1], &col[2]);
 
1934
 
 
1935
  gimp_image_transform_color (dest_gimage, dest_drawable, color, GIMP_RGB, col);
 
1936
}
 
1937
 
 
1938
void
 
1939
gimp_image_transform_color (const GimpImage    *dest_gimage,
 
1940
                            const GimpDrawable *dest_drawable,
 
1941
                            guchar             *dest,
 
1942
                            GimpImageBaseType   src_type,
 
1943
                            const guchar       *src)
 
1944
{
 
1945
  GimpImageType dest_type;
 
1946
 
 
1947
  g_return_if_fail (GIMP_IS_IMAGE (dest_gimage));
 
1948
  g_return_if_fail (src_type != GIMP_INDEXED);
 
1949
 
 
1950
  dest_type = (dest_drawable ?
 
1951
               gimp_drawable_type (dest_drawable) :
 
1952
               gimp_image_base_type_with_alpha (dest_gimage));
 
1953
 
 
1954
  switch (src_type)
 
1955
    {
 
1956
    case GIMP_RGB:
 
1957
      switch (dest_type)
 
1958
        {
 
1959
        case GIMP_RGB_IMAGE:
 
1960
        case GIMP_RGBA_IMAGE:
 
1961
          /*  Straight copy  */
 
1962
          *dest++ = *src++;
 
1963
          *dest++ = *src++;
 
1964
          *dest++ = *src++;
 
1965
          break;
 
1966
 
 
1967
        case GIMP_GRAY_IMAGE:
 
1968
        case GIMP_GRAYA_IMAGE:
 
1969
          /*  NTSC conversion  */
 
1970
          *dest = GIMP_RGB_INTENSITY (src[RED_PIX],
 
1971
                                      src[GREEN_PIX],
 
1972
                                      src[BLUE_PIX]) + 0.5;
 
1973
          break;
 
1974
 
 
1975
        case GIMP_INDEXED_IMAGE:
 
1976
        case GIMP_INDEXEDA_IMAGE:
 
1977
          /*  Least squares method  */
 
1978
          *dest = gimp_image_color_hash_rgb_to_indexed (dest_gimage,
 
1979
                                                        src[RED_PIX],
 
1980
                                                        src[GREEN_PIX],
 
1981
                                                        src[BLUE_PIX]);
 
1982
          break;
 
1983
        }
 
1984
      break;
 
1985
 
 
1986
    case GIMP_GRAY:
 
1987
      switch (dest_type)
 
1988
        {
 
1989
        case GIMP_RGB_IMAGE:
 
1990
        case GIMP_RGBA_IMAGE:
 
1991
          /*  Gray to RG&B */
 
1992
          *dest++ = *src;
 
1993
          *dest++ = *src;
 
1994
          *dest++ = *src;
 
1995
          break;
 
1996
 
 
1997
        case GIMP_GRAY_IMAGE:
 
1998
        case GIMP_GRAYA_IMAGE:
 
1999
          /*  Straight copy  */
 
2000
          *dest = *src;
 
2001
          break;
 
2002
 
 
2003
        case GIMP_INDEXED_IMAGE:
 
2004
        case GIMP_INDEXEDA_IMAGE:
 
2005
          /*  Least squares method  */
 
2006
          *dest = gimp_image_color_hash_rgb_to_indexed (dest_gimage,
 
2007
                                                        src[GRAY_PIX],
 
2008
                                                        src[GRAY_PIX],
 
2009
                                                        src[GRAY_PIX]);
 
2010
          break;
 
2011
        }
 
2012
      break;
 
2013
 
 
2014
    default:
 
2015
      break;
 
2016
    }
 
2017
}
 
2018
 
 
2019
TempBuf *
 
2020
gimp_image_transform_temp_buf (const GimpImage    *dest_image,
 
2021
                               const GimpDrawable *dest_drawable,
 
2022
                               TempBuf            *temp_buf,
 
2023
                               gboolean           *new_buf)
 
2024
{
 
2025
  TempBuf       *ret_buf;
 
2026
  GimpImageType  ret_buf_type;
 
2027
  gboolean       has_alpha;
 
2028
  gboolean       is_rgb;
 
2029
  gint           in_bytes;
 
2030
  gint           out_bytes;
 
2031
 
 
2032
  g_return_val_if_fail (GIMP_IMAGE (dest_image), NULL);
 
2033
  g_return_val_if_fail (GIMP_DRAWABLE (dest_drawable), NULL);
 
2034
  g_return_val_if_fail (temp_buf != NULL, NULL);
 
2035
  g_return_val_if_fail (new_buf != NULL, NULL);
 
2036
 
 
2037
  in_bytes  = temp_buf->bytes;
 
2038
 
 
2039
  has_alpha = (in_bytes == 2 || in_bytes == 4);
 
2040
  is_rgb    = (in_bytes == 3 || in_bytes == 4);
 
2041
 
 
2042
  if (has_alpha)
 
2043
    ret_buf_type = gimp_drawable_type_with_alpha (dest_drawable);
 
2044
  else
 
2045
    ret_buf_type = gimp_drawable_type_without_alpha (dest_drawable);
 
2046
 
 
2047
  out_bytes = GIMP_IMAGE_TYPE_BYTES (ret_buf_type);
 
2048
 
 
2049
  /*  If the pattern doesn't match the image in terms of color type,
 
2050
   *  transform it.  (ie  pattern is RGB, image is indexed)
 
2051
   */
 
2052
  if (in_bytes != out_bytes || gimp_drawable_is_indexed (dest_drawable))
 
2053
    {
 
2054
      guchar *src;
 
2055
      guchar *dest;
 
2056
      gint    size;
 
2057
 
 
2058
      ret_buf = temp_buf_new (temp_buf->width, temp_buf->height,
 
2059
                              out_bytes, 0, 0, NULL);
 
2060
 
 
2061
      src  = temp_buf_data (temp_buf);
 
2062
      dest = temp_buf_data (ret_buf);
 
2063
 
 
2064
      size = temp_buf->width * temp_buf->height;
 
2065
 
 
2066
      while (size--)
 
2067
        {
 
2068
          gimp_image_transform_color (dest_image, dest_drawable, dest,
 
2069
                                      is_rgb ? GIMP_RGB : GIMP_GRAY, src);
 
2070
 
 
2071
          /* Handle alpha */
 
2072
          if (has_alpha)
 
2073
            dest[out_bytes - 1] = src[in_bytes - 1];
 
2074
 
 
2075
          src  += in_bytes;
 
2076
          dest += out_bytes;
 
2077
        }
 
2078
 
 
2079
      *new_buf = TRUE;
 
2080
    }
 
2081
  else
 
2082
    {
 
2083
      ret_buf = temp_buf;
 
2084
      *new_buf = FALSE;
 
2085
    }
 
2086
 
 
2087
  return ret_buf;
 
2088
}
 
2089
 
 
2090
 
 
2091
/*  shadow tiles  */
 
2092
 
 
2093
TileManager *
 
2094
gimp_image_shadow (GimpImage *gimage,
 
2095
                   gint       width,
 
2096
                   gint       height,
 
2097
                   gint       bpp)
 
2098
{
 
2099
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2100
 
 
2101
  if (gimage->shadow)
 
2102
    {
 
2103
      if ((width  != tile_manager_width  (gimage->shadow)) ||
 
2104
          (height != tile_manager_height (gimage->shadow)) ||
 
2105
          (bpp    != tile_manager_bpp    (gimage->shadow)))
 
2106
        {
 
2107
          gimp_image_free_shadow (gimage);
 
2108
        }
 
2109
      else
 
2110
        {
 
2111
          return gimage->shadow;
 
2112
        }
 
2113
    }
 
2114
 
 
2115
  gimage->shadow = tile_manager_new (width, height, bpp);
 
2116
 
 
2117
  return gimage->shadow;
 
2118
}
 
2119
 
 
2120
void
 
2121
gimp_image_free_shadow (GimpImage *gimage)
 
2122
{
 
2123
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
2124
 
 
2125
  if (gimage->shadow)
 
2126
    {
 
2127
      tile_manager_unref (gimage->shadow);
 
2128
      gimage->shadow = NULL;
 
2129
    }
 
2130
}
 
2131
 
 
2132
 
 
2133
/*  parasites  */
 
2134
 
 
2135
GimpParasite *
 
2136
gimp_image_parasite_find (const GimpImage *gimage,
 
2137
                          const gchar     *name)
 
2138
{
 
2139
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2140
 
 
2141
  return gimp_parasite_list_find (gimage->parasites, name);
 
2142
}
 
2143
 
 
2144
static void
 
2145
list_func (gchar          *key,
 
2146
           GimpParasite   *p,
 
2147
           gchar        ***cur)
 
2148
{
 
2149
  *(*cur)++ = (gchar *) g_strdup (key);
 
2150
}
 
2151
 
 
2152
gchar **
 
2153
gimp_image_parasite_list (const GimpImage *gimage,
 
2154
                          gint            *count)
 
2155
{
 
2156
  gchar **list;
 
2157
  gchar **cur;
 
2158
 
 
2159
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2160
 
 
2161
  *count = gimp_parasite_list_length (gimage->parasites);
 
2162
  cur = list = g_new (gchar*, *count);
 
2163
 
 
2164
  gimp_parasite_list_foreach (gimage->parasites, (GHFunc) list_func, &cur);
 
2165
 
 
2166
  return list;
 
2167
}
 
2168
 
 
2169
void
 
2170
gimp_image_parasite_attach (GimpImage    *gimage,
 
2171
                            GimpParasite *parasite)
 
2172
{
 
2173
  g_return_if_fail (GIMP_IS_IMAGE (gimage) && parasite != NULL);
 
2174
 
 
2175
  /* only set the dirty bit manually if we can be saved and the new
 
2176
     parasite differs from the current one and we aren't undoable */
 
2177
  if (gimp_parasite_is_undoable (parasite))
 
2178
    gimp_image_undo_push_image_parasite (gimage,
 
2179
                                         _("Attach Parasite to Image"),
 
2180
                                         parasite);
 
2181
 
 
2182
  /*  We used to push an cantundo on te stack here. This made the undo stack
 
2183
      unusable (NULL on the stack) and prevented people from undoing after a
 
2184
      save (since most save plug-ins attach an undoable comment parasite).
 
2185
      Now we simply attach the parasite without pushing an undo. That way it's
 
2186
      undoable but does not block the undo system.   --Sven
 
2187
   */
 
2188
 
 
2189
  gimp_parasite_list_add (gimage->parasites, parasite);
 
2190
 
 
2191
  if (gimp_parasite_has_flag (parasite, GIMP_PARASITE_ATTACH_PARENT))
 
2192
    {
 
2193
      gimp_parasite_shift_parent (parasite);
 
2194
      gimp_parasite_attach (gimage->gimp, parasite);
 
2195
    }
 
2196
}
 
2197
 
 
2198
void
 
2199
gimp_image_parasite_detach (GimpImage   *gimage,
 
2200
                            const gchar *parasite)
 
2201
{
 
2202
  GimpParasite *p;
 
2203
 
 
2204
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
2205
  g_return_if_fail (parasite != NULL);
 
2206
 
 
2207
  if (!(p = gimp_parasite_list_find (gimage->parasites, parasite)))
 
2208
    return;
 
2209
 
 
2210
  if (gimp_parasite_is_undoable (p))
 
2211
    gimp_image_undo_push_image_parasite_remove (gimage,
 
2212
                                                _("Remove Parasite from Image"),
 
2213
                                                gimp_parasite_name (p));
 
2214
 
 
2215
  gimp_parasite_list_remove (gimage->parasites, parasite);
 
2216
}
 
2217
 
 
2218
 
 
2219
/*  tattoos  */
 
2220
 
 
2221
GimpTattoo
 
2222
gimp_image_get_new_tattoo (GimpImage *gimage)
 
2223
{
 
2224
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), 0);
 
2225
 
 
2226
  gimage->tattoo_state++;
 
2227
 
 
2228
  if (gimage->tattoo_state <= 0)
 
2229
    g_warning ("%s: Tattoo state corrupted (integer overflow).", G_STRFUNC);
 
2230
 
 
2231
  return gimage->tattoo_state;
 
2232
}
 
2233
 
 
2234
GimpTattoo
 
2235
gimp_image_get_tattoo_state (GimpImage *gimage)
 
2236
{
 
2237
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), 0);
 
2238
 
 
2239
  return gimage->tattoo_state;
 
2240
}
 
2241
 
 
2242
gboolean
 
2243
gimp_image_set_tattoo_state (GimpImage  *gimage,
 
2244
                             GimpTattoo  val)
 
2245
{
 
2246
  GList      *list;
 
2247
  gboolean    retval = TRUE;
 
2248
  GimpTattoo  maxval = 0;
 
2249
 
 
2250
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2251
 
 
2252
  /* Check that the layer tatoos don't overlap with channel or vector ones */
 
2253
  for (list = GIMP_LIST (gimage->layers)->list;
 
2254
       list;
 
2255
       list = g_list_next (list))
 
2256
    {
 
2257
      GimpTattoo ltattoo;
 
2258
 
 
2259
      ltattoo = gimp_item_get_tattoo (GIMP_ITEM (list->data));
 
2260
      if (ltattoo > maxval)
 
2261
        maxval = ltattoo;
 
2262
 
 
2263
      if (gimp_image_get_channel_by_tattoo (gimage, ltattoo))
 
2264
        retval = FALSE; /* Oopps duplicated tattoo in channel */
 
2265
 
 
2266
      if (gimp_image_get_vectors_by_tattoo (gimage, ltattoo))
 
2267
        retval = FALSE; /* Oopps duplicated tattoo in vectors */
 
2268
    }
 
2269
 
 
2270
  /* Now check that the channel and vectors tattoos don't overlap */
 
2271
  for (list = GIMP_LIST (gimage->channels)->list;
 
2272
       list;
 
2273
       list = g_list_next (list))
 
2274
    {
 
2275
      GimpTattoo ctattoo;
 
2276
 
 
2277
      ctattoo = gimp_item_get_tattoo (GIMP_ITEM (list->data));
 
2278
      if (ctattoo > maxval)
 
2279
        maxval = ctattoo;
 
2280
 
 
2281
      if (gimp_image_get_vectors_by_tattoo (gimage, ctattoo))
 
2282
        retval = FALSE; /* Oopps duplicated tattoo in vectors */
 
2283
    }
 
2284
 
 
2285
  /* Find the max tattoo value in the vectors */
 
2286
  for (list = GIMP_LIST (gimage->vectors)->list;
 
2287
       list;
 
2288
       list = g_list_next (list))
 
2289
    {
 
2290
      GimpTattoo vtattoo;
 
2291
 
 
2292
      vtattoo = gimp_item_get_tattoo (GIMP_ITEM (list->data));
 
2293
      if (vtattoo > maxval)
 
2294
        maxval = vtattoo;
 
2295
    }
 
2296
 
 
2297
  if (val < maxval)
 
2298
    retval = FALSE;
 
2299
 
 
2300
  /* Must check if the state is valid */
 
2301
  if (retval == TRUE)
 
2302
    gimage->tattoo_state = val;
 
2303
 
 
2304
  return retval;
 
2305
}
 
2306
 
 
2307
 
 
2308
/*  layers / channels / vectors  */
 
2309
 
 
2310
GimpContainer *
 
2311
gimp_image_get_layers (const GimpImage *gimage)
 
2312
{
 
2313
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2314
 
 
2315
  return gimage->layers;
 
2316
}
 
2317
 
 
2318
GimpContainer *
 
2319
gimp_image_get_channels (const GimpImage *gimage)
 
2320
{
 
2321
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2322
 
 
2323
  return gimage->channels;
 
2324
}
 
2325
 
 
2326
GimpContainer *
 
2327
gimp_image_get_vectors (const GimpImage *gimage)
 
2328
{
 
2329
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2330
 
 
2331
  return gimage->vectors;
 
2332
}
 
2333
 
 
2334
GimpDrawable *
 
2335
gimp_image_active_drawable (const GimpImage *gimage)
 
2336
{
 
2337
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2338
 
 
2339
  /*  If there is an active channel (a saved selection, etc.),
 
2340
   *  we ignore the active layer
 
2341
   */
 
2342
  if (gimage->active_channel)
 
2343
    {
 
2344
      return GIMP_DRAWABLE (gimage->active_channel);
 
2345
    }
 
2346
  else if (gimage->active_layer)
 
2347
    {
 
2348
      GimpLayer *layer = gimage->active_layer;
 
2349
 
 
2350
      if (layer->mask && layer->mask->edit_mask)
 
2351
        return GIMP_DRAWABLE (layer->mask);
 
2352
      else
 
2353
        return GIMP_DRAWABLE (layer);
 
2354
    }
 
2355
 
 
2356
  return NULL;
 
2357
}
 
2358
 
 
2359
GimpLayer *
 
2360
gimp_image_get_active_layer (const GimpImage *gimage)
 
2361
{
 
2362
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2363
 
 
2364
  return gimage->active_layer;
 
2365
}
 
2366
 
 
2367
GimpChannel *
 
2368
gimp_image_get_active_channel (const GimpImage *gimage)
 
2369
{
 
2370
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2371
 
 
2372
  return gimage->active_channel;
 
2373
}
 
2374
 
 
2375
GimpVectors *
 
2376
gimp_image_get_active_vectors (const GimpImage *gimage)
 
2377
{
 
2378
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2379
 
 
2380
  return gimage->active_vectors;
 
2381
}
 
2382
 
 
2383
GimpLayer *
 
2384
gimp_image_set_active_layer (GimpImage *gimage,
 
2385
                             GimpLayer *layer)
 
2386
{
 
2387
  GimpLayer *floating_sel;
 
2388
 
 
2389
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2390
  g_return_val_if_fail (layer == NULL || GIMP_IS_LAYER (layer), NULL);
 
2391
  g_return_val_if_fail (layer == NULL ||
 
2392
                        gimp_container_have (gimage->layers,
 
2393
                                             GIMP_OBJECT (layer)), NULL);
 
2394
 
 
2395
  floating_sel = gimp_image_floating_sel (gimage);
 
2396
 
 
2397
  /*  Make sure the floating_sel always is the active layer  */
 
2398
  if (floating_sel && layer != floating_sel)
 
2399
    return floating_sel;
 
2400
 
 
2401
  if (layer != gimage->active_layer)
 
2402
    {
 
2403
      if (layer)
 
2404
        {
 
2405
          /*  Configure the layer stack to reflect this change  */
 
2406
          gimage->layer_stack = g_slist_remove (gimage->layer_stack, layer);
 
2407
          gimage->layer_stack = g_slist_prepend (gimage->layer_stack, layer);
 
2408
        }
 
2409
 
 
2410
      /*  Don't cache selection info for the previous active layer  */
 
2411
      if (gimage->active_layer)
 
2412
        gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (gimage->active_layer));
 
2413
 
 
2414
      gimage->active_layer = layer;
 
2415
 
 
2416
      g_signal_emit (gimage, gimp_image_signals[ACTIVE_LAYER_CHANGED], 0);
 
2417
 
 
2418
      if (layer && gimage->active_channel)
 
2419
        gimp_image_set_active_channel (gimage, NULL);
 
2420
    }
 
2421
 
 
2422
  return gimage->active_layer;
 
2423
}
 
2424
 
 
2425
GimpChannel *
 
2426
gimp_image_set_active_channel (GimpImage   *gimage,
 
2427
                               GimpChannel *channel)
 
2428
{
 
2429
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2430
  g_return_val_if_fail (channel == NULL || GIMP_IS_CHANNEL (channel), NULL);
 
2431
  g_return_val_if_fail (channel == NULL ||
 
2432
                        gimp_container_have (gimage->channels,
 
2433
                                             GIMP_OBJECT (channel)), NULL);
 
2434
 
 
2435
  /*  Not if there is a floating selection  */
 
2436
  if (channel && gimp_image_floating_sel (gimage))
 
2437
    return NULL;
 
2438
 
 
2439
  if (channel != gimage->active_channel)
 
2440
    {
 
2441
      gimage->active_channel = channel;
 
2442
 
 
2443
      g_signal_emit (gimage, gimp_image_signals[ACTIVE_CHANNEL_CHANGED], 0);
 
2444
 
 
2445
      if (channel && gimage->active_layer)
 
2446
        gimp_image_set_active_layer (gimage, NULL);
 
2447
    }
 
2448
 
 
2449
  return gimage->active_channel;
 
2450
}
 
2451
 
 
2452
GimpChannel *
 
2453
gimp_image_unset_active_channel (GimpImage *gimage)
 
2454
{
 
2455
  GimpChannel *channel;
 
2456
 
 
2457
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2458
 
 
2459
  channel = gimp_image_get_active_channel (gimage);
 
2460
 
 
2461
  if (channel)
 
2462
    {
 
2463
      gimp_image_set_active_channel (gimage, NULL);
 
2464
 
 
2465
      if (gimage->layer_stack)
 
2466
        gimp_image_set_active_layer (gimage, gimage->layer_stack->data);
 
2467
    }
 
2468
 
 
2469
  return channel;
 
2470
}
 
2471
 
 
2472
GimpVectors *
 
2473
gimp_image_set_active_vectors (GimpImage   *gimage,
 
2474
                               GimpVectors *vectors)
 
2475
{
 
2476
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2477
  g_return_val_if_fail (vectors == NULL || GIMP_IS_VECTORS (vectors), NULL);
 
2478
  g_return_val_if_fail (vectors == NULL ||
 
2479
                        gimp_container_have (gimage->vectors,
 
2480
                                             GIMP_OBJECT (vectors)), NULL);
 
2481
 
 
2482
  if (vectors != gimage->active_vectors)
 
2483
    {
 
2484
      gimage->active_vectors = vectors;
 
2485
 
 
2486
      g_signal_emit (gimage, gimp_image_signals[ACTIVE_VECTORS_CHANGED], 0);
 
2487
    }
 
2488
 
 
2489
  return gimage->active_vectors;
 
2490
}
 
2491
 
 
2492
void
 
2493
gimp_image_active_layer_changed (GimpImage *gimage)
 
2494
{
 
2495
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
2496
 
 
2497
  g_signal_emit (gimage, gimp_image_signals[ACTIVE_LAYER_CHANGED], 0);
 
2498
}
 
2499
 
 
2500
void
 
2501
gimp_image_active_channel_changed (GimpImage *gimage)
 
2502
{
 
2503
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
2504
 
 
2505
  g_signal_emit (gimage, gimp_image_signals[ACTIVE_CHANNEL_CHANGED], 0);
 
2506
}
 
2507
 
 
2508
void
 
2509
gimp_image_active_vectors_changed (GimpImage *gimage)
 
2510
{
 
2511
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
2512
 
 
2513
  g_signal_emit (gimage, gimp_image_signals[ACTIVE_VECTORS_CHANGED], 0);
 
2514
}
 
2515
 
 
2516
gint
 
2517
gimp_image_get_layer_index (const GimpImage   *gimage,
 
2518
                            const GimpLayer   *layer)
 
2519
{
 
2520
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
2521
  g_return_val_if_fail (GIMP_IS_LAYER (layer), -1);
 
2522
 
 
2523
  return gimp_container_get_child_index (gimage->layers,
 
2524
                                         GIMP_OBJECT (layer));
 
2525
}
 
2526
 
 
2527
gint
 
2528
gimp_image_get_channel_index (const GimpImage   *gimage,
 
2529
                              const GimpChannel *channel)
 
2530
{
 
2531
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
2532
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), -1);
 
2533
 
 
2534
  return gimp_container_get_child_index (gimage->channels,
 
2535
                                         GIMP_OBJECT (channel));
 
2536
}
 
2537
 
 
2538
gint
 
2539
gimp_image_get_vectors_index (const GimpImage   *gimage,
 
2540
                              const GimpVectors *vectors)
 
2541
{
 
2542
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
 
2543
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), -1);
 
2544
 
 
2545
  return gimp_container_get_child_index (gimage->vectors,
 
2546
                                         GIMP_OBJECT (vectors));
 
2547
}
 
2548
 
 
2549
GimpLayer *
 
2550
gimp_image_get_layer_by_tattoo (const GimpImage *gimage,
 
2551
                                GimpTattoo       tattoo)
 
2552
{
 
2553
  GList *list;
 
2554
 
 
2555
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2556
 
 
2557
  for (list = GIMP_LIST (gimage->layers)->list;
 
2558
       list;
 
2559
       list = g_list_next (list))
 
2560
    {
 
2561
      GimpLayer *layer = list->data;
 
2562
 
 
2563
      if (gimp_item_get_tattoo (GIMP_ITEM (layer)) == tattoo)
 
2564
        return layer;
 
2565
    }
 
2566
 
 
2567
  return NULL;
 
2568
}
 
2569
 
 
2570
GimpChannel *
 
2571
gimp_image_get_channel_by_tattoo (const GimpImage *gimage,
 
2572
                                  GimpTattoo       tattoo)
 
2573
{
 
2574
  GList *list;
 
2575
 
 
2576
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2577
 
 
2578
  for (list = GIMP_LIST (gimage->channels)->list;
 
2579
       list;
 
2580
       list = g_list_next (list))
 
2581
    {
 
2582
      GimpChannel *channel = list->data;
 
2583
 
 
2584
      if (gimp_item_get_tattoo (GIMP_ITEM (channel)) == tattoo)
 
2585
        return channel;
 
2586
    }
 
2587
 
 
2588
  return NULL;
 
2589
}
 
2590
 
 
2591
GimpVectors *
 
2592
gimp_image_get_vectors_by_tattoo (const GimpImage *gimage,
 
2593
                                  GimpTattoo       tattoo)
 
2594
{
 
2595
  GList *list;
 
2596
 
 
2597
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2598
 
 
2599
  for (list = GIMP_LIST (gimage->vectors)->list;
 
2600
       list;
 
2601
       list = g_list_next (list))
 
2602
    {
 
2603
      GimpVectors *vectors = list->data;
 
2604
 
 
2605
      if (gimp_item_get_tattoo (GIMP_ITEM (vectors)) == tattoo)
 
2606
        return vectors;
 
2607
    }
 
2608
 
 
2609
  return NULL;
 
2610
}
 
2611
 
 
2612
GimpLayer *
 
2613
gimp_image_get_layer_by_name (const GimpImage *gimage,
 
2614
                              const gchar     *name)
 
2615
{
 
2616
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2617
 
 
2618
  return (GimpLayer *) gimp_container_get_child_by_name (gimage->layers,
 
2619
                                                         name);
 
2620
}
 
2621
 
 
2622
GimpChannel *
 
2623
gimp_image_get_channel_by_name (const GimpImage *gimage,
 
2624
                                const gchar     *name)
 
2625
{
 
2626
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2627
 
 
2628
  return (GimpChannel *) gimp_container_get_child_by_name (gimage->channels,
 
2629
                                                           name);
 
2630
}
 
2631
 
 
2632
GimpVectors *
 
2633
gimp_image_get_vectors_by_name (const GimpImage *gimage,
 
2634
                                const gchar     *name)
 
2635
{
 
2636
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
2637
 
 
2638
  return (GimpVectors *) gimp_container_get_child_by_name (gimage->vectors,
 
2639
                                                           name);
 
2640
}
 
2641
 
 
2642
gboolean
 
2643
gimp_image_add_layer (GimpImage *gimage,
 
2644
                      GimpLayer *layer,
 
2645
                      gint       position)
 
2646
{
 
2647
  GimpLayer *active_layer;
 
2648
  GimpLayer *floating_sel;
 
2649
  gboolean   old_has_alpha;
 
2650
 
 
2651
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2652
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
 
2653
 
 
2654
  if (GIMP_ITEM (layer)->gimage != NULL &&
 
2655
      GIMP_ITEM (layer)->gimage != gimage)
 
2656
    {
 
2657
      g_warning ("%s: attempting to add layer to wrong image.", G_STRFUNC);
 
2658
      return FALSE;
 
2659
    }
 
2660
 
 
2661
  if (gimp_container_have (gimage->layers, GIMP_OBJECT (layer)))
 
2662
    {
 
2663
      g_warning ("%s: trying to add layer to image twice.", G_STRFUNC);
 
2664
      return FALSE;
 
2665
    }
 
2666
 
 
2667
  floating_sel = gimp_image_floating_sel (gimage);
 
2668
 
 
2669
  if (floating_sel && gimp_layer_is_floating_sel (layer))
 
2670
    {
 
2671
      g_warning ("%s: trying to add floating layer to image which alyready "
 
2672
                 "has a floating selection.", G_STRFUNC);
 
2673
      return FALSE;
 
2674
    }
 
2675
 
 
2676
  active_layer = gimp_image_get_active_layer (gimage);
 
2677
 
 
2678
  old_has_alpha = gimp_image_has_alpha (gimage);
 
2679
 
 
2680
  gimp_image_undo_push_layer_add (gimage, _("Add Layer"),
 
2681
                                  layer, 0, active_layer);
 
2682
 
 
2683
  gimp_item_set_image (GIMP_ITEM (layer), gimage);
 
2684
 
 
2685
  if (layer->mask)
 
2686
    gimp_item_set_image (GIMP_ITEM (layer->mask), gimage);
 
2687
 
 
2688
  /*  If the layer is a floating selection, set the ID  */
 
2689
  if (gimp_layer_is_floating_sel (layer))
 
2690
    gimage->floating_sel = layer;
 
2691
 
 
2692
  /*  add the layer to the list at the specified position  */
 
2693
  if (position == -1)
 
2694
    {
 
2695
      if (active_layer)
 
2696
        position = gimp_container_get_child_index (gimage->layers,
 
2697
                                                   GIMP_OBJECT (active_layer));
 
2698
      else
 
2699
        position = 0;
 
2700
    }
 
2701
 
 
2702
  /*  If there is a floating selection (and this isn't it!),
 
2703
   *  make sure the insert position is greater than 0
 
2704
   */
 
2705
  if (position == 0 && floating_sel)
 
2706
    position = 1;
 
2707
 
 
2708
  /*  Don't add at a non-existing index  */
 
2709
  if (position > gimp_container_num_children (gimage->layers))
 
2710
    position = gimp_container_num_children (gimage->layers);
 
2711
 
 
2712
  gimp_container_insert (gimage->layers, GIMP_OBJECT (layer), position);
 
2713
  gimp_item_sink (GIMP_ITEM (layer));
 
2714
 
 
2715
  /*  notify the layers dialog of the currently active layer  */
 
2716
  gimp_image_set_active_layer (gimage, layer);
 
2717
 
 
2718
  if (old_has_alpha != gimp_image_has_alpha (gimage))
 
2719
    gimp_image_alpha_changed (gimage);
 
2720
 
 
2721
  if (gimp_layer_is_floating_sel (layer))
 
2722
    gimp_image_floating_selection_changed (gimage);
 
2723
 
 
2724
  return TRUE;
 
2725
}
 
2726
 
 
2727
void
 
2728
gimp_image_remove_layer (GimpImage *gimage,
 
2729
                         GimpLayer *layer)
 
2730
{
 
2731
  GimpLayer *active_layer;
 
2732
  gint       index;
 
2733
  gboolean   old_has_alpha;
 
2734
  gboolean   undo_group = FALSE;
 
2735
 
 
2736
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
2737
  g_return_if_fail (GIMP_IS_LAYER (layer));
 
2738
  g_return_if_fail (gimp_container_have (gimage->layers,
 
2739
                                         GIMP_OBJECT (layer)));
 
2740
 
 
2741
  if (gimp_drawable_has_floating_sel (GIMP_DRAWABLE (layer)))
 
2742
    {
 
2743
      gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_ITEM_REMOVE,
 
2744
                                   _("Remove Layer"));
 
2745
      undo_group = TRUE;
 
2746
 
 
2747
      floating_sel_remove (gimp_image_floating_sel (gimage));
 
2748
    }
 
2749
 
 
2750
  active_layer = gimp_image_get_active_layer (gimage);
 
2751
 
 
2752
  index = gimp_container_get_child_index (gimage->layers,
 
2753
                                          GIMP_OBJECT (layer));
 
2754
 
 
2755
  old_has_alpha = gimp_image_has_alpha (gimage);
 
2756
 
 
2757
  gimp_image_undo_push_layer_remove (gimage, _("Remove Layer"),
 
2758
                                     layer, index, active_layer);
 
2759
 
 
2760
  g_object_ref (layer);
 
2761
 
 
2762
  /*  Make sure we're not caching any old selection info  */
 
2763
  if (layer == active_layer)
 
2764
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (layer));
 
2765
 
 
2766
  gimp_container_remove (gimage->layers, GIMP_OBJECT (layer));
 
2767
  gimage->layer_stack = g_slist_remove (gimage->layer_stack, layer);
 
2768
 
 
2769
  if (gimage->floating_sel == layer)
 
2770
    {
 
2771
      /*  If this was the floating selection, reset the fs pointer
 
2772
       *  and activate the underlying drawable
 
2773
       */
 
2774
      gimage->floating_sel = NULL;
 
2775
 
 
2776
      floating_sel_activate_drawable (layer);
 
2777
 
 
2778
      gimp_image_floating_selection_changed (gimage);
 
2779
    }
 
2780
  else if (layer == active_layer)
 
2781
    {
 
2782
      if (gimage->layer_stack)
 
2783
        {
 
2784
          active_layer = gimage->layer_stack->data;
 
2785
        }
 
2786
      else
 
2787
        {
 
2788
          gint n_children = gimp_container_num_children (gimage->layers);
 
2789
 
 
2790
          if (n_children > 0)
 
2791
            {
 
2792
              index = CLAMP (index, 0, n_children - 1);
 
2793
 
 
2794
              active_layer = (GimpLayer *)
 
2795
                gimp_container_get_child_by_index (gimage->layers, index);
 
2796
            }
 
2797
          else
 
2798
            {
 
2799
              active_layer = NULL;
 
2800
            }
 
2801
        }
 
2802
 
 
2803
      gimp_image_set_active_layer (gimage, active_layer);
 
2804
    }
 
2805
 
 
2806
  gimp_item_removed (GIMP_ITEM (layer));
 
2807
 
 
2808
  g_object_unref (layer);
 
2809
 
 
2810
  if (old_has_alpha != gimp_image_has_alpha (gimage))
 
2811
    gimp_image_alpha_changed (gimage);
 
2812
 
 
2813
  if (undo_group)
 
2814
    gimp_image_undo_group_end (gimage);
 
2815
}
 
2816
 
 
2817
gboolean
 
2818
gimp_image_raise_layer (GimpImage *gimage,
 
2819
                        GimpLayer *layer)
 
2820
{
 
2821
  gint index;
 
2822
 
 
2823
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2824
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
 
2825
 
 
2826
  index = gimp_container_get_child_index (gimage->layers,
 
2827
                                          GIMP_OBJECT (layer));
 
2828
 
 
2829
  if (index == 0)
 
2830
    {
 
2831
      g_message (_("Layer cannot be raised higher."));
 
2832
      return FALSE;
 
2833
    }
 
2834
 
 
2835
  if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
 
2836
    {
 
2837
      g_message (_("Cannot raise a layer without alpha."));
 
2838
      return FALSE;
 
2839
    }
 
2840
 
 
2841
  return gimp_image_position_layer (gimage, layer, index - 1,
 
2842
                                    TRUE, _("Raise Layer"));
 
2843
}
 
2844
 
 
2845
gboolean
 
2846
gimp_image_lower_layer (GimpImage *gimage,
 
2847
                        GimpLayer *layer)
 
2848
{
 
2849
  gint index;
 
2850
 
 
2851
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2852
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
 
2853
 
 
2854
  index = gimp_container_get_child_index (gimage->layers,
 
2855
                                          GIMP_OBJECT (layer));
 
2856
 
 
2857
  if (index == gimp_container_num_children (gimage->layers) - 1)
 
2858
    {
 
2859
      g_message (_("Layer cannot be lowered more."));
 
2860
      return FALSE;
 
2861
    }
 
2862
 
 
2863
  return gimp_image_position_layer (gimage, layer, index + 1,
 
2864
                                    TRUE, _("Lower Layer"));
 
2865
}
 
2866
 
 
2867
gboolean
 
2868
gimp_image_raise_layer_to_top (GimpImage *gimage,
 
2869
                               GimpLayer *layer)
 
2870
{
 
2871
  gint index;
 
2872
 
 
2873
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2874
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
 
2875
 
 
2876
  index = gimp_container_get_child_index (gimage->layers,
 
2877
                                          GIMP_OBJECT (layer));
 
2878
 
 
2879
  if (index == 0)
 
2880
    {
 
2881
      g_message (_("Layer is already on top."));
 
2882
      return FALSE;
 
2883
    }
 
2884
 
 
2885
  if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
 
2886
    {
 
2887
      g_message (_("Cannot raise a layer without alpha."));
 
2888
      return FALSE;
 
2889
    }
 
2890
 
 
2891
  return gimp_image_position_layer (gimage, layer, 0,
 
2892
                                    TRUE, _("Raise Layer to Top"));
 
2893
}
 
2894
 
 
2895
gboolean
 
2896
gimp_image_lower_layer_to_bottom (GimpImage *gimage,
 
2897
                                  GimpLayer *layer)
 
2898
{
 
2899
  gint index;
 
2900
  gint length;
 
2901
 
 
2902
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2903
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
 
2904
 
 
2905
  index = gimp_container_get_child_index (gimage->layers,
 
2906
                                          GIMP_OBJECT (layer));
 
2907
 
 
2908
  length = gimp_container_num_children (gimage->layers);
 
2909
 
 
2910
  if (index == length - 1)
 
2911
    {
 
2912
      g_message (_("Layer is already on the bottom."));
 
2913
      return FALSE;
 
2914
    }
 
2915
 
 
2916
  return gimp_image_position_layer (gimage, layer, length - 1,
 
2917
                                    TRUE, _("Lower Layer to Bottom"));
 
2918
}
 
2919
 
 
2920
gboolean
 
2921
gimp_image_position_layer (GimpImage   *gimage,
 
2922
                           GimpLayer   *layer,
 
2923
                           gint         new_index,
 
2924
                           gboolean     push_undo,
 
2925
                           const gchar *undo_desc)
 
2926
{
 
2927
  gint index;
 
2928
  gint num_layers;
 
2929
 
 
2930
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2931
  g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
 
2932
 
 
2933
  index = gimp_container_get_child_index (gimage->layers,
 
2934
                                          GIMP_OBJECT (layer));
 
2935
  if (index < 0)
 
2936
    return FALSE;
 
2937
 
 
2938
  num_layers = gimp_container_num_children (gimage->layers);
 
2939
 
 
2940
  new_index = CLAMP (new_index, 0, num_layers - 1);
 
2941
 
 
2942
  if (new_index == index)
 
2943
    return TRUE;
 
2944
 
 
2945
  /* check if we want to move it below a bottom layer without alpha */
 
2946
  if (new_index == num_layers - 1)
 
2947
    {
 
2948
      GimpLayer *tmp;
 
2949
 
 
2950
      tmp = (GimpLayer *) gimp_container_get_child_by_index (gimage->layers,
 
2951
                                                             num_layers - 1);
 
2952
 
 
2953
      if (new_index == num_layers - 1 &&
 
2954
          ! gimp_drawable_has_alpha (GIMP_DRAWABLE (tmp)))
 
2955
        {
 
2956
          g_message (_("Layer '%s' has no alpha. Layer was placed above it."),
 
2957
                     GIMP_OBJECT (tmp)->name);
 
2958
          new_index--;
 
2959
        }
 
2960
    }
 
2961
 
 
2962
  if (push_undo)
 
2963
    gimp_image_undo_push_layer_reposition (gimage, undo_desc, layer);
 
2964
 
 
2965
  gimp_container_reorder (gimage->layers, GIMP_OBJECT (layer), new_index);
 
2966
 
 
2967
  if (gimp_item_get_visible (GIMP_ITEM (layer)))
 
2968
    {
 
2969
      gint off_x, off_y;
 
2970
 
 
2971
      gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y);
 
2972
 
 
2973
      gimp_image_update (gimage,
 
2974
                         off_x, off_y,
 
2975
                         gimp_item_width  (GIMP_ITEM (layer)),
 
2976
                         gimp_item_height (GIMP_ITEM (layer)));
 
2977
      gimp_viewable_invalidate_preview (GIMP_VIEWABLE (gimage));
 
2978
    }
 
2979
 
 
2980
  return TRUE;
 
2981
}
 
2982
 
 
2983
gboolean
 
2984
gimp_image_add_channel (GimpImage   *gimage,
 
2985
                        GimpChannel *channel,
 
2986
                        gint         position)
 
2987
{
 
2988
  GimpChannel *active_channel;
 
2989
 
 
2990
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
2991
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
 
2992
 
 
2993
  if (GIMP_ITEM (channel)->gimage != NULL &&
 
2994
      GIMP_ITEM (channel)->gimage != gimage)
 
2995
    {
 
2996
      g_warning ("%s: attempting to add channel to wrong image.", G_STRFUNC);
 
2997
      return FALSE;
 
2998
    }
 
2999
 
 
3000
  if (gimp_container_have (gimage->channels, GIMP_OBJECT (channel)))
 
3001
    {
 
3002
      g_warning ("%s: trying to add channel to image twice.", G_STRFUNC);
 
3003
      return FALSE;
 
3004
    }
 
3005
 
 
3006
  active_channel = gimp_image_get_active_channel (gimage);
 
3007
 
 
3008
  gimp_image_undo_push_channel_add (gimage, _("Add Channel"),
 
3009
                                    channel, 0, active_channel);
 
3010
 
 
3011
  gimp_item_set_image (GIMP_ITEM (channel), gimage);
 
3012
 
 
3013
  /*  add the layer to the list at the specified position  */
 
3014
  if (position == -1)
 
3015
    {
 
3016
      if (active_channel)
 
3017
        position = gimp_container_get_child_index (gimage->channels,
 
3018
                                                   GIMP_OBJECT (active_channel));
 
3019
      else
 
3020
        position = 0;
 
3021
    }
 
3022
 
 
3023
  /*  Don't add at a non-existing index  */
 
3024
  if (position > gimp_container_num_children (gimage->channels))
 
3025
    position = gimp_container_num_children (gimage->channels);
 
3026
 
 
3027
  gimp_container_insert (gimage->channels, GIMP_OBJECT (channel), position);
 
3028
  gimp_item_sink (GIMP_ITEM (channel));
 
3029
 
 
3030
  /*  notify this gimage of the currently active channel  */
 
3031
  gimp_image_set_active_channel (gimage, channel);
 
3032
 
 
3033
  return TRUE;
 
3034
}
 
3035
 
 
3036
void
 
3037
gimp_image_remove_channel (GimpImage   *gimage,
 
3038
                           GimpChannel *channel)
 
3039
{
 
3040
  GimpChannel *active_channel;
 
3041
  gint         index;
 
3042
  gboolean     undo_group = FALSE;
 
3043
 
 
3044
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
3045
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
 
3046
  g_return_if_fail (gimp_container_have (gimage->channels,
 
3047
                                         GIMP_OBJECT (channel)));
 
3048
 
 
3049
  if (gimp_drawable_has_floating_sel (GIMP_DRAWABLE (channel)))
 
3050
    {
 
3051
      gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_ITEM_REMOVE,
 
3052
                                   _("Remove Channel"));
 
3053
      undo_group = TRUE;
 
3054
 
 
3055
      floating_sel_remove (gimp_image_floating_sel (gimage));
 
3056
    }
 
3057
 
 
3058
  active_channel = gimp_image_get_active_channel (gimage);
 
3059
 
 
3060
  index = gimp_container_get_child_index (gimage->channels,
 
3061
                                          GIMP_OBJECT (channel));
 
3062
 
 
3063
  gimp_image_undo_push_channel_remove (gimage, _("Remove Channel"),
 
3064
                                       channel, index, active_channel);
 
3065
 
 
3066
  g_object_ref (channel);
 
3067
 
 
3068
  gimp_container_remove (gimage->channels, GIMP_OBJECT (channel));
 
3069
  gimp_item_removed (GIMP_ITEM (channel));
 
3070
 
 
3071
  if (channel == active_channel)
 
3072
    {
 
3073
      gint n_children = gimp_container_num_children (gimage->channels);
 
3074
 
 
3075
      if (n_children > 0)
 
3076
        {
 
3077
          index = CLAMP (index, 0, n_children - 1);
 
3078
 
 
3079
          active_channel = (GimpChannel *)
 
3080
            gimp_container_get_child_by_index (gimage->channels, index);
 
3081
 
 
3082
          gimp_image_set_active_channel (gimage, active_channel);
 
3083
        }
 
3084
      else
 
3085
        {
 
3086
          gimp_image_unset_active_channel (gimage);
 
3087
        }
 
3088
    }
 
3089
 
 
3090
  g_object_unref (channel);
 
3091
 
 
3092
  if (undo_group)
 
3093
    gimp_image_undo_group_end (gimage);
 
3094
}
 
3095
 
 
3096
gboolean
 
3097
gimp_image_raise_channel (GimpImage   *gimage,
 
3098
                          GimpChannel *channel)
 
3099
{
 
3100
  gint index;
 
3101
 
 
3102
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3103
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
 
3104
 
 
3105
  index = gimp_container_get_child_index (gimage->channels,
 
3106
                                          GIMP_OBJECT (channel));
 
3107
 
 
3108
  if (index == 0)
 
3109
    {
 
3110
      g_message (_("Channel cannot be raised higher."));
 
3111
      return FALSE;
 
3112
    }
 
3113
 
 
3114
  return gimp_image_position_channel (gimage, channel, index - 1,
 
3115
                                      TRUE, _("Raise Channel"));
 
3116
}
 
3117
 
 
3118
gboolean
 
3119
gimp_image_raise_channel_to_top (GimpImage   *gimage,
 
3120
                                 GimpChannel *channel)
 
3121
{
 
3122
  gint index;
 
3123
 
 
3124
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3125
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
 
3126
 
 
3127
  index = gimp_container_get_child_index (gimage->channels,
 
3128
                                          GIMP_OBJECT (channel));
 
3129
 
 
3130
  if (index == 0)
 
3131
    {
 
3132
      g_message (_("Channel is already on top."));
 
3133
      return FALSE;
 
3134
    }
 
3135
 
 
3136
  return gimp_image_position_channel (gimage, channel, 0,
 
3137
                                      TRUE, _("Raise Channel to Top"));
 
3138
}
 
3139
 
 
3140
gboolean
 
3141
gimp_image_lower_channel (GimpImage   *gimage,
 
3142
                          GimpChannel *channel)
 
3143
{
 
3144
  gint index;
 
3145
 
 
3146
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3147
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
 
3148
 
 
3149
  index = gimp_container_get_child_index (gimage->channels,
 
3150
                                          GIMP_OBJECT (channel));
 
3151
 
 
3152
  if (index == gimp_container_num_children (gimage->channels) - 1)
 
3153
    {
 
3154
      g_message (_("Channel cannot be lowered more."));
 
3155
      return FALSE;
 
3156
    }
 
3157
 
 
3158
  return gimp_image_position_channel (gimage, channel, index + 1,
 
3159
                                      TRUE, _("Lower Channel"));
 
3160
}
 
3161
 
 
3162
gboolean
 
3163
gimp_image_lower_channel_to_bottom (GimpImage   *gimage,
 
3164
                                    GimpChannel *channel)
 
3165
{
 
3166
  gint index;
 
3167
  gint length;
 
3168
 
 
3169
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3170
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
 
3171
 
 
3172
  index = gimp_container_get_child_index (gimage->channels,
 
3173
                                          GIMP_OBJECT (channel));
 
3174
 
 
3175
  length = gimp_container_num_children (gimage->channels);
 
3176
 
 
3177
  if (index == length - 1)
 
3178
    {
 
3179
      g_message (_("Channel is already on the bottom."));
 
3180
      return FALSE;
 
3181
    }
 
3182
 
 
3183
  return gimp_image_position_channel (gimage, channel, length - 1,
 
3184
                                      TRUE, _("Lower Channel to Bottom"));
 
3185
}
 
3186
 
 
3187
gboolean
 
3188
gimp_image_position_channel (GimpImage   *gimage,
 
3189
                             GimpChannel *channel,
 
3190
                             gint         new_index,
 
3191
                             gboolean     push_undo,
 
3192
                             const gchar *undo_desc)
 
3193
{
 
3194
  gint index;
 
3195
  gint num_channels;
 
3196
 
 
3197
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3198
  g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
 
3199
 
 
3200
  index = gimp_container_get_child_index (gimage->channels,
 
3201
                                          GIMP_OBJECT (channel));
 
3202
  if (index < 0)
 
3203
    return FALSE;
 
3204
 
 
3205
  num_channels = gimp_container_num_children (gimage->channels);
 
3206
 
 
3207
  new_index = CLAMP (new_index, 0, num_channels - 1);
 
3208
 
 
3209
  if (new_index == index)
 
3210
    return TRUE;
 
3211
 
 
3212
  if (push_undo)
 
3213
    gimp_image_undo_push_channel_reposition (gimage, undo_desc, channel);
 
3214
 
 
3215
  gimp_container_reorder (gimage->channels,
 
3216
                          GIMP_OBJECT (channel), new_index);
 
3217
 
 
3218
  if (gimp_item_get_visible (GIMP_ITEM (channel)))
 
3219
    {
 
3220
      gint off_x, off_y;
 
3221
 
 
3222
      gimp_item_offsets (GIMP_ITEM (channel), &off_x, &off_y);
 
3223
 
 
3224
      gimp_image_update (gimage,
 
3225
                         off_x, off_y,
 
3226
                         gimp_item_width  (GIMP_ITEM (channel)),
 
3227
                         gimp_item_height (GIMP_ITEM (channel)));
 
3228
      gimp_viewable_invalidate_preview (GIMP_VIEWABLE (gimage));
 
3229
    }
 
3230
 
 
3231
  return TRUE;
 
3232
}
 
3233
 
 
3234
gboolean
 
3235
gimp_image_add_vectors (GimpImage   *gimage,
 
3236
                        GimpVectors *vectors,
 
3237
                        gint         position)
 
3238
{
 
3239
  GimpVectors *active_vectors;
 
3240
 
 
3241
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3242
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
 
3243
 
 
3244
  if (GIMP_ITEM (vectors)->gimage != NULL &&
 
3245
      GIMP_ITEM (vectors)->gimage != gimage)
 
3246
    {
 
3247
      g_warning ("%s: attempting to add vectors to wrong image.", G_STRFUNC);
 
3248
      return FALSE;
 
3249
    }
 
3250
 
 
3251
  if (gimp_container_have (gimage->vectors, GIMP_OBJECT (vectors)))
 
3252
    {
 
3253
      g_warning ("%s: trying to add vectors to image twice.", G_STRFUNC);
 
3254
      return FALSE;
 
3255
    }
 
3256
 
 
3257
  active_vectors = gimp_image_get_active_vectors (gimage);
 
3258
 
 
3259
  gimp_image_undo_push_vectors_add (gimage, _("Add Path"),
 
3260
                                    vectors, 0, active_vectors);
 
3261
 
 
3262
  gimp_item_set_image (GIMP_ITEM (vectors), gimage);
 
3263
 
 
3264
  /*  add the layer to the list at the specified position  */
 
3265
  if (position == -1)
 
3266
    {
 
3267
      if (active_vectors)
 
3268
        position = gimp_container_get_child_index (gimage->vectors,
 
3269
                                                   GIMP_OBJECT (active_vectors));
 
3270
      else
 
3271
        position = 0;
 
3272
    }
 
3273
 
 
3274
  /*  Don't add at a non-existing index  */
 
3275
  if (position > gimp_container_num_children (gimage->vectors))
 
3276
    position = gimp_container_num_children (gimage->vectors);
 
3277
 
 
3278
  gimp_container_insert (gimage->vectors, GIMP_OBJECT (vectors), position);
 
3279
  gimp_item_sink (GIMP_ITEM (vectors));
 
3280
 
 
3281
  /*  notify this gimage of the currently active vectors  */
 
3282
  gimp_image_set_active_vectors (gimage, vectors);
 
3283
 
 
3284
  return TRUE;
 
3285
}
 
3286
 
 
3287
void
 
3288
gimp_image_remove_vectors (GimpImage   *gimage,
 
3289
                           GimpVectors *vectors)
 
3290
{
 
3291
  GimpVectors *active_vectors;
 
3292
  gint         index;
 
3293
 
 
3294
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
3295
  g_return_if_fail (GIMP_IS_VECTORS (vectors));
 
3296
  g_return_if_fail (gimp_container_have (gimage->vectors,
 
3297
                                         GIMP_OBJECT (vectors)));
 
3298
 
 
3299
  active_vectors = gimp_image_get_active_vectors (gimage);
 
3300
 
 
3301
  index = gimp_container_get_child_index (gimage->vectors,
 
3302
                                          GIMP_OBJECT (vectors));
 
3303
 
 
3304
  gimp_image_undo_push_vectors_remove (gimage, _("Remove Path"),
 
3305
                                       vectors, index, active_vectors);
 
3306
 
 
3307
  g_object_ref (vectors);
 
3308
 
 
3309
  gimp_container_remove (gimage->vectors, GIMP_OBJECT (vectors));
 
3310
  gimp_item_removed (GIMP_ITEM (vectors));
 
3311
 
 
3312
  if (vectors == active_vectors)
 
3313
    {
 
3314
      gint n_children = gimp_container_num_children (gimage->vectors);
 
3315
 
 
3316
      if (n_children > 0)
 
3317
        {
 
3318
          index = CLAMP (index, 0, n_children - 1);
 
3319
 
 
3320
          active_vectors = (GimpVectors *)
 
3321
            gimp_container_get_child_by_index (gimage->vectors, index);
 
3322
        }
 
3323
      else
 
3324
        {
 
3325
          active_vectors = NULL;
 
3326
        }
 
3327
 
 
3328
      gimp_image_set_active_vectors (gimage, active_vectors);
 
3329
    }
 
3330
 
 
3331
  g_object_unref (vectors);
 
3332
}
 
3333
 
 
3334
gboolean
 
3335
gimp_image_raise_vectors (GimpImage   *gimage,
 
3336
                          GimpVectors *vectors)
 
3337
{
 
3338
  gint index;
 
3339
 
 
3340
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3341
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
 
3342
 
 
3343
  index = gimp_container_get_child_index (gimage->vectors,
 
3344
                                          GIMP_OBJECT (vectors));
 
3345
 
 
3346
  if (index == 0)
 
3347
    {
 
3348
      g_message (_("Path cannot be raised higher."));
 
3349
      return FALSE;
 
3350
    }
 
3351
 
 
3352
  return gimp_image_position_vectors (gimage, vectors, index - 1,
 
3353
                                      TRUE, _("Raise Path"));
 
3354
}
 
3355
 
 
3356
gboolean
 
3357
gimp_image_raise_vectors_to_top (GimpImage   *gimage,
 
3358
                                 GimpVectors *vectors)
 
3359
{
 
3360
  gint index;
 
3361
 
 
3362
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3363
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
 
3364
 
 
3365
  index = gimp_container_get_child_index (gimage->vectors,
 
3366
                                          GIMP_OBJECT (vectors));
 
3367
 
 
3368
  if (index == 0)
 
3369
    {
 
3370
      g_message (_("Path is already on top."));
 
3371
      return FALSE;
 
3372
    }
 
3373
 
 
3374
  return gimp_image_position_vectors (gimage, vectors, 0,
 
3375
                                      TRUE, _("Raise Path to Top"));
 
3376
}
 
3377
 
 
3378
gboolean
 
3379
gimp_image_lower_vectors (GimpImage   *gimage,
 
3380
                          GimpVectors *vectors)
 
3381
{
 
3382
  gint index;
 
3383
 
 
3384
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3385
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
 
3386
 
 
3387
  index = gimp_container_get_child_index (gimage->vectors,
 
3388
                                          GIMP_OBJECT (vectors));
 
3389
 
 
3390
  if (index == gimp_container_num_children (gimage->vectors) - 1)
 
3391
    {
 
3392
      g_message (_("Path cannot be lowered more."));
 
3393
      return FALSE;
 
3394
    }
 
3395
 
 
3396
  return gimp_image_position_vectors (gimage, vectors, index + 1,
 
3397
                                      TRUE, _("Lower Path"));
 
3398
}
 
3399
 
 
3400
gboolean
 
3401
gimp_image_lower_vectors_to_bottom (GimpImage   *gimage,
 
3402
                                    GimpVectors *vectors)
 
3403
{
 
3404
  gint index;
 
3405
  gint length;
 
3406
 
 
3407
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3408
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
 
3409
 
 
3410
  index = gimp_container_get_child_index (gimage->vectors,
 
3411
                                          GIMP_OBJECT (vectors));
 
3412
 
 
3413
  length = gimp_container_num_children (gimage->vectors);
 
3414
 
 
3415
  if (index == length - 1)
 
3416
    {
 
3417
      g_message (_("Path is already on the bottom."));
 
3418
      return FALSE;
 
3419
    }
 
3420
 
 
3421
  return gimp_image_position_vectors (gimage, vectors, length - 1,
 
3422
                                      TRUE, _("Lower Path to Bottom"));
 
3423
}
 
3424
 
 
3425
gboolean
 
3426
gimp_image_position_vectors (GimpImage   *gimage,
 
3427
                             GimpVectors *vectors,
 
3428
                             gint         new_index,
 
3429
                             gboolean     push_undo,
 
3430
                             const gchar *undo_desc)
 
3431
{
 
3432
  gint index;
 
3433
  gint num_vectors;
 
3434
 
 
3435
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3436
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
 
3437
 
 
3438
  index = gimp_container_get_child_index (gimage->vectors,
 
3439
                                          GIMP_OBJECT (vectors));
 
3440
  if (index < 0)
 
3441
    return FALSE;
 
3442
 
 
3443
  num_vectors = gimp_container_num_children (gimage->vectors);
 
3444
 
 
3445
  new_index = CLAMP (new_index, 0, num_vectors - 1);
 
3446
 
 
3447
  if (new_index == index)
 
3448
    return TRUE;
 
3449
 
 
3450
  if (push_undo)
 
3451
    gimp_image_undo_push_vectors_reposition (gimage, undo_desc, vectors);
 
3452
 
 
3453
  gimp_container_reorder (gimage->vectors,
 
3454
                          GIMP_OBJECT (vectors), new_index);
 
3455
 
 
3456
  return TRUE;
 
3457
}
 
3458
 
 
3459
gboolean
 
3460
gimp_image_layer_boundary (const GimpImage  *gimage,
 
3461
                           BoundSeg        **segs,
 
3462
                           gint             *n_segs)
 
3463
{
 
3464
  GimpLayer *layer;
 
3465
 
 
3466
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3467
  g_return_val_if_fail (segs != NULL, FALSE);
 
3468
  g_return_val_if_fail (n_segs != NULL, FALSE);
 
3469
 
 
3470
  /*  The second boundary corresponds to the active layer's
 
3471
   *  perimeter...
 
3472
   */
 
3473
  layer = gimp_image_get_active_layer (gimage);
 
3474
 
 
3475
  if (layer)
 
3476
    {
 
3477
      *segs = gimp_layer_boundary (layer, n_segs);
 
3478
      return TRUE;
 
3479
    }
 
3480
  else
 
3481
    {
 
3482
      *segs = NULL;
 
3483
      *n_segs = 0;
 
3484
      return FALSE;
 
3485
    }
 
3486
}
 
3487
 
 
3488
GimpLayer *
 
3489
gimp_image_pick_correlate_layer (const GimpImage *gimage,
 
3490
                                 gint             x,
 
3491
                                 gint             y)
 
3492
{
 
3493
  GList *list;
 
3494
 
 
3495
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
 
3496
 
 
3497
  for (list = GIMP_LIST (gimage->layers)->list;
 
3498
       list;
 
3499
       list = g_list_next (list))
 
3500
    {
 
3501
      GimpLayer *layer = list->data;
 
3502
 
 
3503
      if (gimp_layer_pick_correlate (layer, x, y))
 
3504
        return layer;
 
3505
    }
 
3506
 
 
3507
  return NULL;
 
3508
}
 
3509
 
 
3510
gboolean
 
3511
gimp_image_coords_in_active_drawable (GimpImage        *gimage,
 
3512
                                      const GimpCoords *coords)
 
3513
{
 
3514
  GimpDrawable *drawable;
 
3515
 
 
3516
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
 
3517
 
 
3518
  drawable = gimp_image_active_drawable (gimage);
 
3519
 
 
3520
  if (drawable)
 
3521
    {
 
3522
      GimpItem *item = GIMP_ITEM (drawable);
 
3523
      gint      x, y;
 
3524
 
 
3525
      gimp_item_offsets (item, &x, &y);
 
3526
 
 
3527
      x = ROUND (coords->x) - x;
 
3528
      y = ROUND (coords->y) - y;
 
3529
 
 
3530
      if (x < 0 || x > gimp_item_width (item))
 
3531
        return FALSE;
 
3532
 
 
3533
      if (y < 0 || y > gimp_item_height (item))
 
3534
        return FALSE;
 
3535
 
 
3536
      return TRUE;
 
3537
    }
 
3538
 
 
3539
  return FALSE;
 
3540
}
 
3541
 
 
3542
void
 
3543
gimp_image_invalidate_layer_previews (GimpImage *gimage)
 
3544
{
 
3545
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
3546
 
 
3547
  gimp_container_foreach (gimage->layers,
 
3548
                          (GFunc) gimp_viewable_invalidate_preview,
 
3549
                          NULL);
 
3550
}
 
3551
 
 
3552
void
 
3553
gimp_image_invalidate_channel_previews (GimpImage *gimage)
 
3554
{
 
3555
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
 
3556
 
 
3557
  gimp_container_foreach (gimage->channels,
 
3558
                          (GFunc) gimp_viewable_invalidate_preview,
 
3559
                          NULL);
 
3560
}