~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/core/gimpbrush-scale.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20070502163303-6wchheivjxgjtlna
Tags: upstream-2.3.16
ImportĀ upstreamĀ versionĀ 2.3.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpbrush-scale.c
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <glib-object.h>
 
24
 
 
25
#include "core-types.h"
 
26
 
 
27
#include "gimpbrush.h"
 
28
#include "gimpbrush-scale.h"
 
29
 
 
30
#include "base/pixel-region.h"
 
31
#include "base/temp-buf.h"
 
32
 
 
33
#include "paint-funcs/scale-funcs.h"
 
34
 
 
35
 
 
36
/*  local function prototypes  */
 
37
 
 
38
static TempBuf * gimp_brush_scale_buf_up      (TempBuf *brush_buf,
 
39
                                               gint     dest_width,
 
40
                                               gint     dest_height);
 
41
static TempBuf * gimp_brush_scale_mask_down   (TempBuf *brush_mask,
 
42
                                               gint     dest_width,
 
43
                                               gint     dest_height);
 
44
static TempBuf * gimp_brush_scale_pixmap_down (TempBuf *pixmap,
 
45
                                               gint     dest_width,
 
46
                                               gint     dest_height);
 
47
 
 
48
 
 
49
/*  public functions  */
 
50
 
 
51
void
 
52
gimp_brush_real_scale_size (GimpBrush *brush,
 
53
                            gdouble    scale,
 
54
                            gint      *width,
 
55
                            gint      *height)
 
56
{
 
57
  *width  = (gint) (brush->mask->width  * scale + 0.5);
 
58
  *height = (gint) (brush->mask->height * scale + 0.5);
 
59
}
 
60
 
 
61
TempBuf *
 
62
gimp_brush_real_scale_mask (GimpBrush *brush,
 
63
                            gdouble    scale)
 
64
{
 
65
  gint dest_width;
 
66
  gint dest_height;
 
67
 
 
68
  gimp_brush_scale_size (brush, scale, &dest_width, &dest_height);
 
69
 
 
70
  if (dest_width <= 0 || dest_height <= 0)
 
71
    return NULL;
 
72
 
 
73
  if (scale <= 1.0)
 
74
    {
 
75
      /*  Downscaling with brush_scale_mask is much faster than with
 
76
       *  gimp_brush_scale_buf.
 
77
       */
 
78
      return gimp_brush_scale_mask_down (brush->mask,
 
79
                                         dest_width, dest_height);
 
80
    }
 
81
 
 
82
  return gimp_brush_scale_buf_up (brush->mask, dest_width, dest_height);
 
83
}
 
84
 
 
85
TempBuf *
 
86
gimp_brush_real_scale_pixmap (GimpBrush *brush,
 
87
                              gdouble    scale)
 
88
{
 
89
  gint dest_width;
 
90
  gint dest_height;
 
91
 
 
92
  gimp_brush_scale_size (brush, scale, &dest_width, &dest_height);
 
93
 
 
94
  if (dest_width <= 0 || dest_height <= 0)
 
95
    return NULL;
 
96
 
 
97
  if (scale <= 1.0)
 
98
    {
 
99
      /*  Downscaling with brush_scale_pixmap is much faster than with
 
100
       *  gimp_brush_scale_buf.
 
101
       */
 
102
      return gimp_brush_scale_pixmap_down (brush->pixmap,
 
103
                                           dest_width, dest_height);
 
104
    }
 
105
 
 
106
  return gimp_brush_scale_buf_up (brush->pixmap, dest_width, dest_height);
 
107
}
 
108
 
 
109
 
 
110
/*  private functions  */
 
111
 
 
112
static TempBuf *
 
113
gimp_brush_scale_buf_up (TempBuf *brush_buf,
 
114
                         gint     dest_width,
 
115
                         gint     dest_height)
 
116
{
 
117
  PixelRegion  source_region;
 
118
  PixelRegion  dest_region;
 
119
  TempBuf     *dest_brush_buf;
 
120
 
 
121
  pixel_region_init_temp_buf (&source_region, brush_buf,
 
122
                              0, 0,
 
123
                              brush_buf->width,
 
124
                              brush_buf->height);
 
125
 
 
126
  dest_brush_buf = temp_buf_new (dest_width, dest_height, brush_buf->bytes,
 
127
                                 0, 0, NULL);
 
128
 
 
129
  pixel_region_init_temp_buf (&dest_region, dest_brush_buf,
 
130
                              0, 0,
 
131
                              dest_width,
 
132
                              dest_height);
 
133
 
 
134
  scale_region (&source_region, &dest_region, GIMP_INTERPOLATION_LINEAR,
 
135
                NULL, NULL);
 
136
 
 
137
  return dest_brush_buf;
 
138
}
 
139
 
 
140
static TempBuf *
 
141
gimp_brush_scale_mask_down (TempBuf *brush_mask,
 
142
                            gint     dest_width,
 
143
                            gint     dest_height)
 
144
{
 
145
  TempBuf *scale_brush;
 
146
  gint     src_width;
 
147
  gint     src_height;
 
148
  gint     value;
 
149
  gint     area;
 
150
  gint     i, j;
 
151
  gint     x, x0, y, y0;
 
152
  gint     dx, dx0, dy, dy0;
 
153
  gint     fx, fx0, fy, fy0;
 
154
  guchar  *src, *dest;
 
155
 
 
156
  g_return_val_if_fail (brush_mask != NULL &&
 
157
                        dest_width != 0 && dest_height != 0, NULL);
 
158
 
 
159
  src_width  = brush_mask->width;
 
160
  src_height = brush_mask->height;
 
161
 
 
162
  scale_brush = temp_buf_new (dest_width, dest_height, 1, 0, 0, NULL);
 
163
  g_return_val_if_fail (scale_brush != NULL, NULL);
 
164
 
 
165
  /*  get the data  */
 
166
  dest = temp_buf_data (scale_brush);
 
167
  src  = temp_buf_data (brush_mask);
 
168
 
 
169
  fx = fx0 = (src_width << 8) / dest_width;
 
170
  fy = fy0 = (src_height << 8) / dest_height;
 
171
 
 
172
  area = (fx0 * fy0) >> 8;
 
173
 
 
174
  x = x0 = 0;
 
175
  y = y0 = 0;
 
176
  dx = dx0 = 0;
 
177
  dy = dy0 = 0;
 
178
 
 
179
  for (i=0; i<dest_height; i++)
 
180
    {
 
181
      for (j=0; j<dest_width; j++)
 
182
        {
 
183
          value  = 0;
 
184
 
 
185
          fy = fy0;
 
186
          y  = y0;
 
187
          dy = dy0;
 
188
 
 
189
          if (dy)
 
190
            {
 
191
              fx = fx0;
 
192
              x  = x0;
 
193
              dx = dx0;
 
194
 
 
195
              if (dx)
 
196
                {
 
197
                  value += (dx * dy * src[x + src_width * y]) >> 8;
 
198
                  x++;
 
199
                  fx -= dx;
 
200
                  dx = 0;
 
201
                }
 
202
              while (fx >= 256)
 
203
                {
 
204
                  value += dy * src[x + src_width * y];
 
205
                  x++;
 
206
                  fx -= 256;
 
207
                }
 
208
              if (fx)
 
209
                {
 
210
                  value += fx * dy * src[x + src_width * y] >> 8;
 
211
                  dx = 256 - fx;
 
212
                }
 
213
 
 
214
              y++;
 
215
              fy -= dy;
 
216
              dy = 0;
 
217
            }
 
218
 
 
219
          while (fy >= 256)
 
220
            {
 
221
              fx = fx0;
 
222
              x  = x0;
 
223
              dx = dx0;
 
224
 
 
225
              if (dx)
 
226
                {
 
227
                  value += dx * src[x + src_width * y];
 
228
                  x++;
 
229
                  fx -= dx;
 
230
                  dx = 0;
 
231
                }
 
232
              while (fx >= 256)
 
233
                {
 
234
                  value += 256 * src[x + src_width * y];
 
235
                  x++;
 
236
                  fx -= 256;
 
237
                }
 
238
              if (fx)
 
239
                {
 
240
                  value += fx * src[x + src_width * y];
 
241
                  dx = 256 - fx;
 
242
                }
 
243
 
 
244
              y++;
 
245
              fy -= 256;
 
246
            }
 
247
 
 
248
          if (fy)
 
249
            {
 
250
              fx = fx0;
 
251
              x  = x0;
 
252
              dx = dx0;
 
253
 
 
254
              if (dx)
 
255
                {
 
256
                  value += (dx * fy * src[x + src_width * y]) >> 8;
 
257
                  x++;
 
258
                  fx -= dx;
 
259
                  dx = 0;
 
260
                }
 
261
              while (fx >= 256)
 
262
                {
 
263
                  value += fy * src[x + src_width * y];
 
264
                  x++;
 
265
                  fx -= 256;
 
266
                }
 
267
              if (fx)
 
268
                {
 
269
                  value += (fx * fy * src[x + src_width * y]) >> 8;
 
270
                  dx = 256 - fx;
 
271
                }
 
272
 
 
273
              dy = 256 - fy;
 
274
            }
 
275
 
 
276
          value /= area;
 
277
          *dest++ = MIN (value, 255);
 
278
 
 
279
          x0  = x;
 
280
          dx0 = dx;
 
281
        }
 
282
 
 
283
      x0  = 0;
 
284
      dx0 = 0;
 
285
      y0  = y;
 
286
      dy0 = dy;
 
287
    }
 
288
 
 
289
  return scale_brush;
 
290
}
 
291
 
 
292
 
 
293
#define ADD_RGB(dest, factor, src) \
 
294
  dest[0] += factor * src[0]; \
 
295
  dest[1] += factor * src[1]; \
 
296
  dest[2] += factor * src[2];
 
297
 
 
298
static TempBuf *
 
299
gimp_brush_scale_pixmap_down (TempBuf *pixmap,
 
300
                              gint     dest_width,
 
301
                              gint     dest_height)
 
302
{
 
303
  TempBuf *scale_brush;
 
304
  gint     src_width;
 
305
  gint     src_height;
 
306
  gint     value[3];
 
307
  gint     factor;
 
308
  gint     area;
 
309
  gint     i, j;
 
310
  gint     x, x0, y, y0;
 
311
  gint     dx, dx0, dy, dy0;
 
312
  gint     fx, fx0, fy, fy0;
 
313
  guchar  *src, *src_ptr, *dest;
 
314
 
 
315
  g_return_val_if_fail (pixmap != NULL && pixmap->bytes == 3 &&
 
316
                        dest_width != 0 && dest_height != 0, NULL);
 
317
 
 
318
  src_width  = pixmap->width;
 
319
  src_height = pixmap->height;
 
320
 
 
321
  scale_brush = temp_buf_new (dest_width, dest_height, 3, 0, 0, NULL);
 
322
  g_return_val_if_fail (scale_brush != NULL, NULL);
 
323
 
 
324
  /*  get the data  */
 
325
  dest = temp_buf_data (scale_brush);
 
326
  src  = temp_buf_data (pixmap);
 
327
 
 
328
  fx = fx0 = (src_width << 8) / dest_width;
 
329
  fy = fy0 = (src_height << 8) / dest_height;
 
330
  area = (fx0 * fy0) >> 8;
 
331
 
 
332
  x = x0 = 0;
 
333
  y = y0 = 0;
 
334
  dx = dx0 = 0;
 
335
  dy = dy0 = 0;
 
336
 
 
337
  for (i=0; i<dest_height; i++)
 
338
    {
 
339
      for (j=0; j<dest_width; j++)
 
340
        {
 
341
          value[0] = 0;
 
342
          value[1] = 0;
 
343
          value[2] = 0;
 
344
 
 
345
          fy = fy0;
 
346
          y  = y0;
 
347
          dy = dy0;
 
348
 
 
349
          if (dy)
 
350
            {
 
351
              fx = fx0;
 
352
              x  = x0;
 
353
              dx = dx0;
 
354
 
 
355
              if (dx)
 
356
                {
 
357
                  factor = (dx * dy) >> 8;
 
358
                  src_ptr = src + 3 * (x + y * src_width);
 
359
                  ADD_RGB (value, factor, src_ptr);
 
360
                  x++;
 
361
                  fx -= dx;
 
362
                  dx = 0;
 
363
                }
 
364
              while (fx >= 256)
 
365
                {
 
366
                  factor = dy;
 
367
                  src_ptr = src + 3 * (x + y * src_width);
 
368
                  ADD_RGB (value, factor, src_ptr);
 
369
                  x++;
 
370
                  fx -= 256;
 
371
                }
 
372
              if (fx)
 
373
                {
 
374
                  factor = (fx * dy) >> 8;
 
375
                  src_ptr = src + 3 * (x + y * src_width);
 
376
                  ADD_RGB (value, factor, src_ptr);
 
377
                  dx = 256 - fx;
 
378
                }
 
379
 
 
380
              y++;
 
381
              fy -= dy;
 
382
              dy = 0;
 
383
            }
 
384
 
 
385
          while (fy >= 256)
 
386
            {
 
387
              fx = fx0;
 
388
              x  = x0;
 
389
              dx = dx0;
 
390
 
 
391
              if (dx)
 
392
                {
 
393
                  factor = dx;
 
394
                  src_ptr = src + 3 * (x + y * src_width);
 
395
                  ADD_RGB (value, factor, src_ptr);
 
396
                  x++;
 
397
                  fx -= dx;
 
398
                  dx = 0;
 
399
                }
 
400
              while (fx >= 256)
 
401
                {
 
402
                  factor = 256;
 
403
                  src_ptr = src + 3 * (x + y * src_width);
 
404
                  ADD_RGB (value, factor, src_ptr);
 
405
                  x++;
 
406
                  fx -= 256;
 
407
                }
 
408
              if (fx)
 
409
                {
 
410
                  factor = fx;
 
411
                  src_ptr = src + 3 * (x + y * src_width);
 
412
                  ADD_RGB (value, factor, src_ptr);
 
413
                  dx = 256 - fx;
 
414
                }
 
415
 
 
416
              y++;
 
417
              fy -= 256;
 
418
            }
 
419
 
 
420
          if (fy)
 
421
            {
 
422
              fx = fx0;
 
423
              x  = x0;
 
424
              dx = dx0;
 
425
 
 
426
              if (dx)
 
427
                {
 
428
                  factor = (dx * fy) >> 8;
 
429
                  src_ptr = src + 3 * (x + y * src_width);
 
430
                  ADD_RGB (value, factor, src_ptr);
 
431
                  x++;
 
432
                  fx -= dx;
 
433
                  dx = 0;
 
434
                }
 
435
              while (fx >= 256)
 
436
                {
 
437
                  factor = fy;
 
438
                  src_ptr = src + 3 * (x + y * src_width);
 
439
                  ADD_RGB (value, factor, src_ptr);
 
440
                  x++;
 
441
                  fx -= 256;
 
442
                }
 
443
              if (fx)
 
444
                {
 
445
                  factor = (fx * fy) >> 8;
 
446
                  src_ptr = src + 3 * (x + y * src_width);
 
447
                  ADD_RGB (value, factor, src_ptr);
 
448
                  dx = 256 - fx;
 
449
                }
 
450
 
 
451
              dy = 256 - fy;
 
452
            }
 
453
 
 
454
          value[0] /= area;
 
455
          value[1] /= area;
 
456
          value[2] /= area;
 
457
 
 
458
          *dest++ = MIN (value[0], 255);
 
459
          *dest++ = MIN (value[1], 255);
 
460
          *dest++ = MIN (value[2], 255);
 
461
 
 
462
          x0  = x;
 
463
          dx0 = dx;
 
464
        }
 
465
 
 
466
      x0  = 0;
 
467
      dx0 = 0;
 
468
      y0  = y;
 
469
      dy0 = dy;
 
470
    }
 
471
 
 
472
  return scale_brush;
 
473
}
 
474
 
 
475
#undef ADD_RGB