File: gimpimage-preview.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (412 lines) | stat: -rw-r--r-- 12,450 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <glib-object.h>

#include "core-types.h"

#include "base/pixel-region.h"
#include "base/temp-buf.h"

#include "paint-funcs/paint-funcs.h"

#include "config/gimpcoreconfig.h"

#include "gimp.h"
#include "gimp-utils.h"
#include "gimpdrawable-preview.h"
#include "gimpimage.h"
#include "gimpimage-preview.h"
#include "gimplayer.h"
#include "gimplayermask.h"
#include "gimplist.h"


void
gimp_image_get_preview_size (GimpViewable *viewable,
                             gint          size,
                             gboolean      is_popup,
                             gboolean      dot_for_dot,
                             gint         *width,
                             gint         *height)
{
  GimpImage *gimage = GIMP_IMAGE (viewable);

  if (! gimage->gimp->config->layer_previews && ! is_popup)
    {
      *width  = size;
      *height = size;
      return;
    }

  gimp_viewable_calc_preview_size (gimage->width,
                                   gimage->height,
                                   size,
                                   size,
                                   dot_for_dot,
                                   gimage->xresolution,
                                   gimage->yresolution,
                                   width,
                                   height,
                                   NULL);
}

gboolean
gimp_image_get_popup_size (GimpViewable *viewable,
                           gint          width,
                           gint          height,
                           gboolean      dot_for_dot,
                           gint         *popup_width,
                           gint         *popup_height)
{
  GimpImage *gimage = GIMP_IMAGE (viewable);

  if (! gimage->gimp->config->layer_previews)
    return FALSE;

  if (gimage->width > width || gimage->height > height)
    {
      gboolean scaling_up;

      gimp_viewable_calc_preview_size (gimage->width,
                                       gimage->height,
                                       width  * 2,
                                       height * 2,
                                       dot_for_dot, 1.0, 1.0,
                                       popup_width,
                                       popup_height,
                                       &scaling_up);

      if (scaling_up)
        {
          *popup_width  = gimage->width;
          *popup_height = gimage->height;
        }

      return TRUE;
    }

  return FALSE;
}

TempBuf *
gimp_image_get_preview (GimpViewable *viewable,
			gint          width,
			gint          height)
{
  GimpImage *gimage = GIMP_IMAGE (viewable);

  if (! gimage->gimp->config->layer_previews)
    return NULL;

  if (gimage->comp_preview_valid            &&
      gimage->comp_preview->width  == width &&
      gimage->comp_preview->height == height)
    {
      /*  The easy way  */
      return gimage->comp_preview;
    }
  else
    {
      /*  The hard way  */
      if (gimage->comp_preview)
	temp_buf_free (gimage->comp_preview);

      /*  Actually construct the composite preview from the layer previews!
       *  This might seem ridiculous, but it's actually the best way, given
       *  a number of unsavory alternatives.
       */
      gimage->comp_preview = gimp_image_get_new_preview (viewable,
							 width, height);

      gimage->comp_preview_valid = TRUE;

      return gimage->comp_preview;
    }
}

TempBuf *
gimp_image_get_new_preview (GimpViewable *viewable,
			    gint          width,
			    gint          height)
{
  GimpImage   *gimage;
  GimpLayer   *layer;
  GimpLayer   *floating_sel;
  PixelRegion  src1PR, src2PR, maskPR;
  PixelRegion *mask;
  TempBuf     *comp;
  TempBuf     *layer_buf;
  TempBuf     *mask_buf;
  GList       *list;
  GSList      *reverse_list = NULL;
  gdouble      ratio;
  gint         x, y, w, h;
  gint         x1, y1, x2, y2;
  gint         bytes;
  gboolean     construct_flag;
  gboolean     visible_components[MAX_CHANNELS] = { TRUE, TRUE, TRUE, TRUE };
  gint         off_x, off_y;

  gimage = GIMP_IMAGE (viewable);

  if (! gimage->gimp->config->layer_previews)
    return NULL;

  ratio = (gdouble) width / (gdouble) gimage->width;

  switch (gimp_image_base_type (gimage))
    {
    case GIMP_RGB:
    case GIMP_INDEXED:
      bytes = 4;
      break;
    case GIMP_GRAY:
      bytes = 2;
      break;
    default:
      bytes = 0;
      g_assert_not_reached ();
      break;
    }

  /*  The construction buffer  */
  comp = temp_buf_new (width, height, bytes, 0, 0, NULL);
  temp_buf_data_clear (comp);

  floating_sel = NULL;

  for (list = GIMP_LIST (gimage->layers)->list;
       list;
       list = g_list_next (list))
    {
      layer = (GimpLayer *) list->data;

      /*  only add layers that are visible to the list  */
      if (gimp_item_get_visible (GIMP_ITEM (layer)))
	{
	  /*  floating selections are added right above the layer
	   *  they are attached to
	   */
	  if (gimp_layer_is_floating_sel (layer))
	    {
	      floating_sel = layer;
	    }
	  else
	    {
	      if (floating_sel &&
		  floating_sel->fs.drawable == GIMP_DRAWABLE (layer))
		{
		  reverse_list = g_slist_prepend (reverse_list, floating_sel);
		}

	      reverse_list = g_slist_prepend (reverse_list, layer);
	    }
	}
    }

  construct_flag = FALSE;

  for (; reverse_list; reverse_list = g_slist_next (reverse_list))
    {
      gint     src_x, src_y;
      gint     src_width, src_height;
      gboolean use_sub_preview = FALSE;

      layer = (GimpLayer *) reverse_list->data;

      gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y);

      if (! gimp_rectangle_intersect (0, 0,
                                      gimp_item_width  (GIMP_ITEM (layer)),
                                      gimp_item_height (GIMP_ITEM (layer)),
                                      -off_x, -off_y,
                                      gimage->width, gimage->height,
                                      &src_x, &src_y,
                                      &src_width, &src_height))
        {
          continue;
        }

      x = (gint) RINT (ratio * off_x);
      y = (gint) RINT (ratio * off_y);
      w = (gint) RINT (ratio * gimp_item_width  (GIMP_ITEM (layer)));
      h = (gint) RINT (ratio * gimp_item_height (GIMP_ITEM (layer)));

      if (w < 1 || h < 1)
	continue;

      if ((w * h) > (width * height * 4))
        use_sub_preview = TRUE;

      x1 = CLAMP (x, 0, width);
      y1 = CLAMP (y, 0, height);
      x2 = CLAMP (x + w, 0, width);
      y2 = CLAMP (y + h, 0, height);

      if (x2 == x1 || y2 == y1)
	continue;

      src1PR.bytes     = comp->bytes;
      src1PR.x         = x1;
      src1PR.y         = y1;
      src1PR.w         = (x2 - x1);
      src1PR.h         = (y2 - y1);
      src1PR.rowstride = comp->width * src1PR.bytes;
      src1PR.data      = (temp_buf_data (comp) +
                          y1 * src1PR.rowstride + x1 * src1PR.bytes);

      if (use_sub_preview)
        {
          layer_buf = gimp_drawable_get_sub_preview (GIMP_DRAWABLE (layer),
                                                     src_x, src_y,
                                                     src_width, src_height,
                                                     (x2 - x1),
                                                     (y2 - y1));

          g_assert (layer_buf);
          g_assert (layer_buf->bytes <= comp->bytes);

          src2PR.bytes     = layer_buf->bytes;
          src2PR.x         = 0;
          src2PR.y         = 0;
          src2PR.w         = src1PR.w;
          src2PR.h         = src1PR.h;
          src2PR.rowstride = layer_buf->width * src2PR.bytes;
          src2PR.data      = temp_buf_data (layer_buf);
        }
      else
        {
          layer_buf = gimp_viewable_get_preview (GIMP_VIEWABLE (layer), w, h);

          g_assert (layer_buf);
          g_assert (layer_buf->bytes <= comp->bytes);

          src2PR.bytes     = layer_buf->bytes;
          src2PR.x         = src1PR.x;
          src2PR.y         = src1PR.y;
          src2PR.w         = src1PR.w;
          src2PR.h         = src1PR.h;
          src2PR.rowstride = layer_buf->width * src2PR.bytes;
          src2PR.data      = (temp_buf_data (layer_buf) +
                              (y1 - y) * src2PR.rowstride +
                              (x1 - x) * src2PR.bytes);
        }

      if (layer->mask && layer->mask->apply_mask)
	{
          if (use_sub_preview)
            {
              mask_buf =
                gimp_drawable_get_sub_preview (GIMP_DRAWABLE (layer->mask),
                                               src_x, src_y,
                                               src_width, src_height,
                                               x2 - x1,
                                               y2 - y1);

              maskPR.bytes     = mask_buf->bytes;
              maskPR.x         = 0;
              maskPR.y         = 0;
              maskPR.w         = src1PR.w;
              maskPR.h         = src1PR.h;
              maskPR.rowstride = mask_buf->width * mask_buf->bytes;
              maskPR.data      = mask_buf_data (mask_buf);
            }
          else
            {
              mask_buf = gimp_viewable_get_preview (GIMP_VIEWABLE (layer->mask),
                                                    w, h);

              maskPR.bytes     = mask_buf->bytes;
              maskPR.x         = src1PR.x;
              maskPR.y         = src1PR.y;
              maskPR.w         = src1PR.w;
              maskPR.h         = src1PR.h;
              maskPR.rowstride = mask_buf->width * mask_buf->bytes;
              maskPR.data      = (mask_buf_data (mask_buf) +
                                  (y1 - y) * maskPR.rowstride +
                                  (x1 - x) * maskPR.bytes);
            }

          mask = &maskPR;
	}
      else
	{
          mask_buf = NULL;
	  mask     = NULL;
	}

      /*  Based on the type of the layer, project the layer onto the
       *   composite preview...
       *  Indexed images are actually already converted to RGB and RGBA,
       *   so just project them as if they were type "intensity"
       *  Send in all TRUE for visible since that info doesn't matter
       *   for previews
       */
      if (gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
	{
	  if (! construct_flag)
	    initial_region (&src2PR, &src1PR,
			    mask, NULL,
                            layer->opacity * 255.999,
			    layer->mode,
                            visible_components,
                            INITIAL_INTENSITY_ALPHA);
	  else
	    combine_regions (&src1PR, &src2PR, &src1PR,
			     mask, NULL,
                             layer->opacity * 255.999,
			     layer->mode,
                             visible_components,
                             COMBINE_INTEN_A_INTEN_A);
        }
      else
        {
	  if (! construct_flag)
	    initial_region (&src2PR, &src1PR,
			    mask, NULL,
                            layer->opacity * 255.999,
			    layer->mode,
                            visible_components,
                            INITIAL_INTENSITY);
	  else
	    combine_regions (&src1PR, &src2PR, &src1PR,
			     mask, NULL,
                             layer->opacity * 255.999,
			     layer->mode,
                             visible_components,
                             COMBINE_INTEN_A_INTEN);
        }

      if (use_sub_preview)
        {
          temp_buf_free (layer_buf);

          if (mask_buf)
            temp_buf_free (mask_buf);
        }

      construct_flag = TRUE;
    }

  g_slist_free (reverse_list);

  return comp;
}