~ubuntu-branches/ubuntu/breezy/gimp/breezy

« back to all changes in this revision

Viewing changes to app/core/gimpprojection-construct.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-10-04 19:04:46 UTC
  • Revision ID: james.westby@ubuntu.com-20051004190446-ukh32kwk56s4sjhu
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

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 <glib-object.h>
 
22
 
 
23
#include "libgimpcolor/gimpcolor.h"
 
24
 
 
25
#include "core-types.h"
 
26
 
 
27
#include "base/pixel-region.h"
 
28
#include "base/tile-manager.h"
 
29
 
 
30
#include "paint-funcs/paint-funcs.h"
 
31
 
 
32
#include "gimpimage.h"
 
33
#include "gimplayer.h"
 
34
#include "gimplayer-floating-sel.h"
 
35
#include "gimplayermask.h"
 
36
#include "gimplist.h"
 
37
#include "gimpprojection.h"
 
38
#include "gimpprojection-construct.h"
 
39
 
 
40
 
 
41
/*  local function prototypes  */
 
42
 
 
43
static void   gimp_projection_construct_layers   (GimpProjection *proj,
 
44
                                                  gint            x,
 
45
                                                  gint            y,
 
46
                                                  gint            w,
 
47
                                                  gint            h);
 
48
static void   gimp_projection_construct_channels (GimpProjection *proj,
 
49
                                                  gint            x,
 
50
                                                  gint            y,
 
51
                                                  gint            w,
 
52
                                                  gint            h);
 
53
static void   gimp_projection_initialize         (GimpProjection *proj,
 
54
                                                  gint            x,
 
55
                                                  gint            y,
 
56
                                                  gint            w,
 
57
                                                  gint            h);
 
58
 
 
59
static void   project_intensity                  (GimpProjection *proj,
 
60
                                                  GimpLayer      *layer,
 
61
                                                  PixelRegion    *src,
 
62
                                                  PixelRegion    *dest,
 
63
                                                  PixelRegion    *mask);
 
64
static void   project_intensity_alpha            (GimpProjection *proj,
 
65
                                                  GimpLayer      *layer,
 
66
                                                  PixelRegion    *src,
 
67
                                                  PixelRegion    *dest,
 
68
                                                  PixelRegion    *mask);
 
69
static void   project_indexed                    (GimpProjection *proj,
 
70
                                                  GimpLayer      *layer,
 
71
                                                  PixelRegion    *src,
 
72
                                                  PixelRegion    *dest);
 
73
static void   project_indexed_alpha              (GimpProjection *proj,
 
74
                                                  GimpLayer      *layer,
 
75
                                                  PixelRegion    *src,
 
76
                                                  PixelRegion    *dest,
 
77
                                                  PixelRegion    *mask);
 
78
static void   project_channel                    (GimpProjection *proj,
 
79
                                                  GimpChannel    *channel,
 
80
                                                  PixelRegion    *src,
 
81
                                                  PixelRegion    *src2);
 
82
 
 
83
 
 
84
/*  public functions  */
 
85
 
 
86
void
 
87
gimp_projection_construct (GimpProjection *proj,
 
88
                           gint            x,
 
89
                           gint            y,
 
90
                           gint            w,
 
91
                           gint            h)
 
92
{
 
93
  g_return_if_fail (GIMP_IS_PROJECTION (proj));
 
94
 
 
95
#if 0
 
96
  gint xoff;
 
97
  gint yoff;
 
98
 
 
99
  /*  set the construct flag, used to determine if anything
 
100
   *  has been written to the gimage raw image yet.
 
101
   */
 
102
  gimage->construct_flag = FALSE;
 
103
 
 
104
  if (gimage->layers)
 
105
    {
 
106
      gimp_item_offsets (GIMP_ITEM (gimage->layers->data), &xoff, &yoff);
 
107
    }
 
108
 
 
109
  if ((gimage->layers) &&                         /* There's a layer.      */
 
110
      (! g_slist_next (gimage->layers)) &&        /* It's the only layer.  */
 
111
      (gimp_drawable_has_alpha (GIMP_DRAWABLE (gimage->layers->data))) &&
 
112
                                                  /* It's !flat.           */
 
113
      (gimp_item_get_visible (GIMP_ITEM (gimage->layers->data))) &&
 
114
                                                  /* It's visible.         */
 
115
      (gimp_item_width  (GIMP_ITEM (gimage->layers->data)) ==
 
116
       gimage->width) &&
 
117
      (gimp_item_height (GIMP_ITEM (gimage->layers->data)) ==
 
118
       gimage->height) &&                         /* Covers all.           */
 
119
      (!gimp_drawable_is_indexed (GIMP_DRAWABLE (gimage->layers->data))) &&
 
120
                                                  /* Not indexed.          */
 
121
      (((GimpLayer *)(gimage->layers->data))->opacity == GIMP_OPACITY_OPAQUE)
 
122
                                                  /* Opaque                */
 
123
      )
 
124
    {
 
125
      gint xoff;
 
126
      gint yoff;
 
127
 
 
128
      gimp_item_offsets (GIMP_ITEM (gimage->layers->data), &xoff, &yoff);
 
129
 
 
130
      if ((xoff==0) && (yoff==0)) /* Starts at 0,0         */
 
131
        {
 
132
          PixelRegion srcPR, destPR;
 
133
          gpointer    pr;
 
134
 
 
135
          g_warning("Can use cow-projection hack.  Yay!");
 
136
 
 
137
          pixel_region_init (&srcPR, gimp_drawable_data
 
138
                             (GIMP_DRAWABLE (gimage->layers->data)),
 
139
                             x, y, w,h, FALSE);
 
140
          pixel_region_init (&destPR,
 
141
                             gimp_image_projection (gimage),
 
142
                             x, y, w,h, TRUE);
 
143
 
 
144
          for (pr = pixel_regions_register (2, &srcPR, &destPR);
 
145
               pr != NULL;
 
146
               pr = pixel_regions_process (pr))
 
147
            {
 
148
              tile_manager_map_over_tile (destPR.tiles,
 
149
                                          destPR.curtile, srcPR.curtile);
 
150
            }
 
151
 
 
152
          gimage->construct_flag = TRUE;
 
153
          gimp_image_construct_channels (gimage, x, y, w, h);
 
154
 
 
155
          return;
 
156
        }
 
157
    }
 
158
#else
 
159
  proj->construct_flag = FALSE;
 
160
#endif
 
161
 
 
162
  /*  First, determine if the projection image needs to be
 
163
   *  initialized--this is the case when there are no visible
 
164
   *  layers that cover the entire canvas--either because layers
 
165
   *  are offset or only a floating selection is visible
 
166
   */
 
167
  gimp_projection_initialize (proj, x, y, w, h);
 
168
 
 
169
  /*  call functions which process the list of layers and
 
170
   *  the list of channels
 
171
   */
 
172
  gimp_projection_construct_layers (proj, x, y, w, h);
 
173
  gimp_projection_construct_channels (proj, x, y, w, h);
 
174
}
 
175
 
 
176
 
 
177
/*  private functions  */
 
178
 
 
179
static void
 
180
gimp_projection_construct_layers (GimpProjection *proj,
 
181
                                  gint            x,
 
182
                                  gint            y,
 
183
                                  gint            w,
 
184
                                  gint            h)
 
185
{
 
186
  GimpLayer   *layer;
 
187
  gint         x1, y1, x2, y2;
 
188
  PixelRegion  src1PR, src2PR, maskPR;
 
189
  PixelRegion * mask;
 
190
  GList       *list;
 
191
  GList       *reverse_list;
 
192
  gint         off_x;
 
193
  gint         off_y;
 
194
 
 
195
  /*  composite the floating selection if it exists  */
 
196
  if ((layer = gimp_image_floating_sel (proj->gimage)))
 
197
    floating_sel_composite (layer, x, y, w, h, FALSE);
 
198
 
 
199
  reverse_list = NULL;
 
200
 
 
201
  for (list = GIMP_LIST (proj->gimage->layers)->list;
 
202
       list;
 
203
       list = g_list_next (list))
 
204
    {
 
205
      layer = (GimpLayer *) list->data;
 
206
 
 
207
      /*  only add layers that are visible and not floating selections
 
208
       *  to the list
 
209
       */
 
210
      if (! gimp_layer_is_floating_sel (layer) &&
 
211
          gimp_item_get_visible (GIMP_ITEM (layer)))
 
212
        {
 
213
          reverse_list = g_list_prepend (reverse_list, layer);
 
214
        }
 
215
    }
 
216
 
 
217
  for (list = reverse_list; list; list = g_list_next (list))
 
218
    {
 
219
      layer = (GimpLayer *) list->data;
 
220
 
 
221
      gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y);
 
222
 
 
223
      x1 = CLAMP (off_x, x, x + w);
 
224
      y1 = CLAMP (off_y, y, y + h);
 
225
      x2 = CLAMP (off_x + gimp_item_width  (GIMP_ITEM (layer)), x, x + w);
 
226
      y2 = CLAMP (off_y + gimp_item_height (GIMP_ITEM (layer)), y, y + h);
 
227
 
 
228
      /* configure the pixel regions  */
 
229
      pixel_region_init (&src1PR, gimp_projection_get_tiles (proj),
 
230
                         x1, y1, (x2 - x1), (y2 - y1),
 
231
                         TRUE);
 
232
 
 
233
      /*  If we're showing the layer mask instead of the layer...  */
 
234
      if (layer->mask && layer->mask->show_mask)
 
235
        {
 
236
          pixel_region_init (&src2PR,
 
237
                             gimp_drawable_data (GIMP_DRAWABLE (layer->mask)),
 
238
                             (x1 - off_x), (y1 - off_y),
 
239
                             (x2 - x1), (y2 - y1), FALSE);
 
240
 
 
241
          copy_gray_to_region (&src2PR, &src1PR);
 
242
        }
 
243
      /*  Otherwise, normal  */
 
244
      else
 
245
        {
 
246
          pixel_region_init (&src2PR,
 
247
                             gimp_drawable_data (GIMP_DRAWABLE (layer)),
 
248
                             (x1 - off_x), (y1 - off_y),
 
249
                             (x2 - x1), (y2 - y1), FALSE);
 
250
 
 
251
          if (layer->mask && layer->mask->apply_mask)
 
252
            {
 
253
              pixel_region_init (&maskPR,
 
254
                                 gimp_drawable_data (GIMP_DRAWABLE (layer->mask)),
 
255
                                 (x1 - off_x), (y1 - off_y),
 
256
                                 (x2 - x1), (y2 - y1), FALSE);
 
257
              mask = &maskPR;
 
258
            }
 
259
          else
 
260
            mask = NULL;
 
261
 
 
262
          /*  Based on the type of the layer, project the layer onto the
 
263
           *  projection image...
 
264
           */
 
265
          switch (gimp_drawable_type (GIMP_DRAWABLE (layer)))
 
266
            {
 
267
            case GIMP_RGB_IMAGE: case GIMP_GRAY_IMAGE:
 
268
              /* no mask possible */
 
269
              project_intensity (proj, layer, &src2PR, &src1PR, mask);
 
270
              break;
 
271
 
 
272
            case GIMP_RGBA_IMAGE: case GIMP_GRAYA_IMAGE:
 
273
              project_intensity_alpha (proj, layer, &src2PR, &src1PR, mask);
 
274
              break;
 
275
 
 
276
            case GIMP_INDEXED_IMAGE:
 
277
              /* no mask possible */
 
278
              project_indexed (proj, layer, &src2PR, &src1PR);
 
279
              break;
 
280
 
 
281
            case GIMP_INDEXEDA_IMAGE:
 
282
              project_indexed_alpha (proj, layer, &src2PR, &src1PR, mask);
 
283
              break;
 
284
 
 
285
            default:
 
286
              break;
 
287
            }
 
288
        }
 
289
 
 
290
      proj->construct_flag = TRUE;  /*  something was projected  */
 
291
    }
 
292
 
 
293
  g_list_free (reverse_list);
 
294
}
 
295
 
 
296
static void
 
297
gimp_projection_construct_channels (GimpProjection *proj,
 
298
                                    gint            x,
 
299
                                    gint            y,
 
300
                                    gint            w,
 
301
                                    gint            h)
 
302
{
 
303
  GimpChannel *channel;
 
304
  PixelRegion  src1PR;
 
305
  PixelRegion  src2PR;
 
306
  GList       *list;
 
307
  GList       *reverse_list = NULL;
 
308
 
 
309
  /*  reverse the channel list  */
 
310
  for (list = GIMP_LIST (proj->gimage->channels)->list;
 
311
       list;
 
312
       list = g_list_next (list))
 
313
    {
 
314
      reverse_list = g_list_prepend (reverse_list, list->data);
 
315
    }
 
316
 
 
317
  for (list = reverse_list; list; list = g_list_next (list))
 
318
    {
 
319
      channel = (GimpChannel *) list->data;
 
320
 
 
321
      if (gimp_item_get_visible (GIMP_ITEM (channel)))
 
322
        {
 
323
          /* configure the pixel regions  */
 
324
          pixel_region_init (&src1PR,
 
325
                             gimp_projection_get_tiles (proj),
 
326
                             x, y, w, h,
 
327
                             TRUE);
 
328
          pixel_region_init (&src2PR,
 
329
                             gimp_drawable_data (GIMP_DRAWABLE (channel)),
 
330
                             x, y, w, h,
 
331
                             FALSE);
 
332
 
 
333
          project_channel (proj, channel, &src1PR, &src2PR);
 
334
 
 
335
          proj->construct_flag = TRUE;
 
336
        }
 
337
    }
 
338
 
 
339
  g_list_free (reverse_list);
 
340
}
 
341
 
 
342
static void
 
343
gimp_projection_initialize (GimpProjection *proj,
 
344
                            gint            x,
 
345
                            gint            y,
 
346
                            gint            w,
 
347
                            gint            h)
 
348
{
 
349
 
 
350
  GList    *list;
 
351
  gboolean  coverage = FALSE;
 
352
 
 
353
  /*  this function determines whether a visible layer
 
354
   *  provides complete coverage over the image.  If not,
 
355
   *  the projection is initialized to transparent
 
356
   */
 
357
 
 
358
  for (list = GIMP_LIST (proj->gimage->layers)->list;
 
359
       list;
 
360
       list = g_list_next (list))
 
361
    {
 
362
      GimpItem *item = list->data;
 
363
      gint      off_x, off_y;
 
364
 
 
365
      gimp_item_offsets (item, &off_x, &off_y);
 
366
 
 
367
      if (gimp_item_get_visible (item)                     &&
 
368
          ! gimp_drawable_has_alpha (GIMP_DRAWABLE (item)) &&
 
369
          (off_x <= x)                                     &&
 
370
          (off_y <= y)                                     &&
 
371
          (off_x + gimp_item_width  (item) >= x + w)       &&
 
372
          (off_y + gimp_item_height (item) >= y + h))
 
373
        {
 
374
          coverage = TRUE;
 
375
          break;
 
376
        }
 
377
    }
 
378
 
 
379
  if (!coverage)
 
380
    {
 
381
      PixelRegion PR;
 
382
      guchar      clear[4] = { 0, 0, 0, 0 };
 
383
 
 
384
      pixel_region_init (&PR, gimp_projection_get_tiles (proj),
 
385
                         x, y, w, h, TRUE);
 
386
      color_region (&PR, clear);
 
387
    }
 
388
}
 
389
 
 
390
static void
 
391
project_intensity (GimpProjection *proj,
 
392
                   GimpLayer      *layer,
 
393
                   PixelRegion    *src,
 
394
                   PixelRegion    *dest,
 
395
                   PixelRegion    *mask)
 
396
{
 
397
  if (! proj->construct_flag)
 
398
    initial_region (src, dest, mask, NULL,
 
399
                    layer->opacity * 255.999,
 
400
                    layer->mode,
 
401
                    proj->gimage->visible,
 
402
                    INITIAL_INTENSITY);
 
403
  else
 
404
    combine_regions (dest, src, dest, mask, NULL,
 
405
                     layer->opacity * 255.999,
 
406
                     layer->mode,
 
407
                     proj->gimage->visible,
 
408
                     COMBINE_INTEN_A_INTEN);
 
409
}
 
410
 
 
411
static void
 
412
project_intensity_alpha (GimpProjection *proj,
 
413
                         GimpLayer      *layer,
 
414
                         PixelRegion    *src,
 
415
                         PixelRegion    *dest,
 
416
                         PixelRegion    *mask)
 
417
{
 
418
  if (! proj->construct_flag)
 
419
    initial_region (src, dest, mask, NULL,
 
420
                    layer->opacity * 255.999,
 
421
                    layer->mode,
 
422
                    proj->gimage->visible,
 
423
                    INITIAL_INTENSITY_ALPHA);
 
424
  else
 
425
    combine_regions (dest, src, dest, mask, NULL,
 
426
                     layer->opacity * 255.999,
 
427
                     layer->mode,
 
428
                     proj->gimage->visible,
 
429
                     COMBINE_INTEN_A_INTEN_A);
 
430
}
 
431
 
 
432
static void
 
433
project_indexed (GimpProjection *proj,
 
434
                 GimpLayer      *layer,
 
435
                 PixelRegion    *src,
 
436
                 PixelRegion    *dest)
 
437
{
 
438
  g_return_if_fail (proj->gimage->cmap != NULL);
 
439
 
 
440
  if (! proj->construct_flag)
 
441
    initial_region (src, dest, NULL, proj->gimage->cmap,
 
442
                    layer->opacity * 255.999,
 
443
                    layer->mode,
 
444
                    proj->gimage->visible,
 
445
                    INITIAL_INDEXED);
 
446
  else
 
447
    g_warning ("%s: unable to project indexed image.", G_STRFUNC);
 
448
}
 
449
 
 
450
static void
 
451
project_indexed_alpha (GimpProjection *proj,
 
452
                       GimpLayer      *layer,
 
453
                       PixelRegion    *src,
 
454
                       PixelRegion    *dest,
 
455
                       PixelRegion    *mask)
 
456
{
 
457
  g_return_if_fail (proj->gimage->cmap != NULL);
 
458
 
 
459
  if (! proj->construct_flag)
 
460
    initial_region (src, dest, mask, proj->gimage->cmap,
 
461
                    layer->opacity * 255.999,
 
462
                    layer->mode,
 
463
                    proj->gimage->visible,
 
464
                    INITIAL_INDEXED_ALPHA);
 
465
  else
 
466
    combine_regions (dest, src, dest, mask, proj->gimage->cmap,
 
467
                     layer->opacity * 255.999,
 
468
                     layer->mode,
 
469
                     proj->gimage->visible,
 
470
                     COMBINE_INTEN_A_INDEXED_A);
 
471
}
 
472
 
 
473
static void
 
474
project_channel (GimpProjection *proj,
 
475
                 GimpChannel    *channel,
 
476
                 PixelRegion    *src,
 
477
                 PixelRegion    *src2)
 
478
{
 
479
  guchar  col[3];
 
480
  guchar  opacity;
 
481
  gint    type;
 
482
 
 
483
  gimp_rgba_get_uchar (&channel->color,
 
484
                       &col[0], &col[1], &col[2], &opacity);
 
485
 
 
486
  if (! proj->construct_flag)
 
487
    {
 
488
      type = (channel->show_masked) ?
 
489
        INITIAL_CHANNEL_MASK : INITIAL_CHANNEL_SELECTION;
 
490
 
 
491
      initial_region (src2, src, NULL, col,
 
492
                      opacity,
 
493
                      GIMP_NORMAL_MODE,
 
494
                      NULL,
 
495
                      type);
 
496
    }
 
497
  else
 
498
    {
 
499
      type = (channel->show_masked) ?
 
500
        COMBINE_INTEN_A_CHANNEL_MASK : COMBINE_INTEN_A_CHANNEL_SELECTION;
 
501
 
 
502
      combine_regions (src, src2, src, NULL, col,
 
503
                       opacity,
 
504
                       GIMP_NORMAL_MODE,
 
505
                       NULL,
 
506
                       type);
 
507
    }
 
508
}