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

« back to all changes in this revision

Viewing changes to app/base/brush-scale.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

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 "base-types.h"
24
 
 
25
 
#include "brush-scale.h"
26
 
#include "temp-buf.h"
27
 
 
28
 
 
29
 
MaskBuf *
30
 
brush_scale_mask (MaskBuf *brush_mask,
31
 
                  gint     dest_width,
32
 
                  gint     dest_height)
33
 
{
34
 
  MaskBuf *scale_brush;
35
 
  gint     src_width;
36
 
  gint     src_height;
37
 
  gint     value;
38
 
  gint     area;
39
 
  gint     i, j;
40
 
  gint     x, x0, y, y0;
41
 
  gint     dx, dx0, dy, dy0;
42
 
  gint     fx, fx0, fy, fy0;
43
 
  guchar  *src, *dest;
44
 
 
45
 
  g_return_val_if_fail (brush_mask != NULL &&
46
 
                        dest_width != 0 && dest_height != 0, NULL);
47
 
 
48
 
  src_width  = brush_mask->width;
49
 
  src_height = brush_mask->height;
50
 
 
51
 
  scale_brush = mask_buf_new (dest_width, dest_height);
52
 
  g_return_val_if_fail (scale_brush != NULL, NULL);
53
 
 
54
 
  /*  get the data  */
55
 
  dest = mask_buf_data (scale_brush);
56
 
  src  = mask_buf_data (brush_mask);
57
 
 
58
 
  fx = fx0 = (256.0 * src_width) / dest_width;
59
 
  fy = fy0 = (256.0 * src_height) / dest_height;
60
 
  area = (fx0 * fy0) >> 8;
61
 
 
62
 
  x = x0 = 0;
63
 
  y = y0 = 0;
64
 
  dx = dx0 = 0;
65
 
  dy = dy0 = 0;
66
 
 
67
 
  for (i=0; i<dest_height; i++)
68
 
    {
69
 
      for (j=0; j<dest_width; j++)
70
 
        {
71
 
          value  = 0;
72
 
 
73
 
          fy = fy0;
74
 
          y  = y0;
75
 
          dy = dy0;
76
 
 
77
 
          if (dy)
78
 
            {
79
 
              fx = fx0;
80
 
              x  = x0;
81
 
              dx = dx0;
82
 
 
83
 
              if (dx)
84
 
                {
85
 
                  value += (dx * dy * src[x + src_width * y]) >> 8;
86
 
                  x++;
87
 
                  fx -= dx;
88
 
                  dx = 0;
89
 
                }
90
 
              while (fx >= 256)
91
 
                {
92
 
                  value += dy * src[x + src_width * y];
93
 
                  x++;
94
 
                  fx -= 256;
95
 
                }
96
 
              if (fx)
97
 
                {
98
 
                  value += fx * dy * src[x + src_width * y] >> 8;
99
 
                  dx = 256 - fx;
100
 
                }
101
 
              y++;
102
 
              fy -= dy;
103
 
              dy = 0;
104
 
            }
105
 
 
106
 
          while (fy >= 256)
107
 
            {
108
 
              fx = fx0;
109
 
              x  = x0;
110
 
              dx = dx0;
111
 
 
112
 
              if (dx)
113
 
                {
114
 
                  value += dx * src[x + src_width * y];
115
 
                  x++;
116
 
                  fx -= dx;
117
 
                  dx = 0;
118
 
                }
119
 
              while (fx >= 256)
120
 
                {
121
 
                  value += 256 * src[x + src_width * y];
122
 
                  x++;
123
 
                  fx -= 256;
124
 
                }
125
 
              if (fx)
126
 
                {
127
 
                  value += fx * src[x + src_width * y];
128
 
                  dx = 256 - fx;
129
 
                }
130
 
              y++;
131
 
              fy -= 256;
132
 
            }
133
 
 
134
 
          if (fy)
135
 
            {
136
 
              fx = fx0;
137
 
              x  = x0;
138
 
              dx = dx0;
139
 
 
140
 
              if (dx)
141
 
                {
142
 
                  value += (dx * fy * src[x + src_width * y]) >> 8;
143
 
                  x++;
144
 
                  fx -= dx;
145
 
                  dx = 0;
146
 
                }
147
 
              while (fx >= 256)
148
 
                {
149
 
                  value += fy * src[x + src_width * y];
150
 
                  x++;
151
 
                  fx -= 256;
152
 
                }
153
 
              if (fx)
154
 
                {
155
 
                  value += (fx * fy * src[x + src_width * y]) >> 8;
156
 
                  dx = 256 - fx;
157
 
                }
158
 
              dy = 256 - fy;
159
 
            }
160
 
 
161
 
          *dest++ = MIN ((value / area), 255);
162
 
 
163
 
          x0  = x;
164
 
          dx0 = dx;
165
 
        }
166
 
      x0  = 0;
167
 
      dx0 = 0;
168
 
      y0  = y;
169
 
      dy0 = dy;
170
 
    }
171
 
 
172
 
  return scale_brush;
173
 
}
174
 
 
175
 
 
176
 
#define ADD_RGB(dest, factor, src) \
177
 
  dest[0] += factor * src[0]; \
178
 
  dest[1] += factor * src[1]; \
179
 
  dest[2] += factor * src[2];
180
 
 
181
 
MaskBuf *
182
 
brush_scale_pixmap (MaskBuf *pixmap,
183
 
                    gint     dest_width,
184
 
                    gint     dest_height)
185
 
{
186
 
  MaskBuf *scale_brush;
187
 
  gint     src_width;
188
 
  gint     src_height;
189
 
  gint     value[3];
190
 
  gint     factor;
191
 
  gint     area;
192
 
  gint     i, j;
193
 
  gint     x, x0, y, y0;
194
 
  gint     dx, dx0, dy, dy0;
195
 
  gint     fx, fx0, fy, fy0;
196
 
  guchar  *src, *src_ptr, *dest;
197
 
 
198
 
  g_return_val_if_fail (pixmap != NULL && pixmap->bytes == 3 &&
199
 
                        dest_width != 0 && dest_height != 0, NULL);
200
 
 
201
 
  src_width  = pixmap->width;
202
 
  src_height = pixmap->height;
203
 
 
204
 
  scale_brush = temp_buf_new (dest_width, dest_height, 3, 0, 0, NULL);
205
 
  g_return_val_if_fail (scale_brush != NULL, NULL);
206
 
 
207
 
  /*  get the data  */
208
 
  dest = mask_buf_data (scale_brush);
209
 
  src  = mask_buf_data (pixmap);
210
 
 
211
 
  fx = fx0 = (256.0 * src_width) / dest_width;
212
 
  fy = fy0 = (256.0 * src_height) / dest_height;
213
 
  area = (fx0 * fy0) >> 8;
214
 
 
215
 
  x = x0 = 0;
216
 
  y = y0 = 0;
217
 
  dx = dx0 = 0;
218
 
  dy = dy0 = 0;
219
 
 
220
 
  for (i=0; i<dest_height; i++)
221
 
    {
222
 
      for (j=0; j<dest_width; j++)
223
 
        {
224
 
          value[0] = 0;
225
 
          value[1] = 0;
226
 
          value[2] = 0;
227
 
 
228
 
          fy = fy0;
229
 
          y  = y0;
230
 
          dy = dy0;
231
 
 
232
 
          if (dy)
233
 
            {
234
 
              fx = fx0;
235
 
              x  = x0;
236
 
              dx = dx0;
237
 
 
238
 
              if (dx)
239
 
                {
240
 
                  factor = (dx * dy) >> 8;
241
 
                  src_ptr = src + 3 * (x + y * src_width);
242
 
                  ADD_RGB (value, factor, src_ptr);
243
 
                  x++;
244
 
                  fx -= dx;
245
 
                  dx = 0;
246
 
                }
247
 
              while (fx >= 256)
248
 
                {
249
 
                  factor = dy;
250
 
                  src_ptr = src + 3 * (x + y * src_width);
251
 
                  ADD_RGB (value, factor, src_ptr);
252
 
                  x++;
253
 
                  fx -= 256;
254
 
                }
255
 
              if (fx)
256
 
                {
257
 
                  factor = (fx * dy) >> 8;
258
 
                  src_ptr = src + 3 * (x + y * src_width);
259
 
                  ADD_RGB (value, factor, src_ptr);
260
 
                  dx = 256 - fx;
261
 
                }
262
 
              y++;
263
 
              fy -= dy;
264
 
              dy = 0;
265
 
            }
266
 
 
267
 
          while (fy >= 256)
268
 
            {
269
 
              fx = fx0;
270
 
              x  = x0;
271
 
              dx = dx0;
272
 
 
273
 
              if (dx)
274
 
                {
275
 
                  factor = dx;
276
 
                  src_ptr = src + 3 * (x + y * src_width);
277
 
                  ADD_RGB (value, factor, src_ptr);
278
 
                  x++;
279
 
                  fx -= dx;
280
 
                  dx = 0;
281
 
                }
282
 
              while (fx >= 256)
283
 
                {
284
 
                  factor = 256;
285
 
                  src_ptr = src + 3 * (x + y * src_width);
286
 
                  ADD_RGB (value, factor, src_ptr);
287
 
                  x++;
288
 
                  fx -= 256;
289
 
                }
290
 
              if (fx)
291
 
                {
292
 
                  factor = fx;
293
 
                  src_ptr = src + 3 * (x + y * src_width);
294
 
                  ADD_RGB (value, factor, src_ptr);
295
 
                  dx = 256 - fx;
296
 
                }
297
 
              y++;
298
 
              fy -= 256;
299
 
            }
300
 
 
301
 
          if (fy)
302
 
            {
303
 
              fx = fx0;
304
 
              x  = x0;
305
 
              dx = dx0;
306
 
 
307
 
              if (dx)
308
 
                {
309
 
                  factor = (dx * fy) >> 8;
310
 
                  src_ptr = src + 3 * (x + y * src_width);
311
 
                  ADD_RGB (value, factor, src_ptr);
312
 
                  x++;
313
 
                  fx -= dx;
314
 
                  dx = 0;
315
 
                }
316
 
              while (fx >= 256)
317
 
                {
318
 
                  factor = fy;
319
 
                  src_ptr = src + 3 * (x + y * src_width);
320
 
                  ADD_RGB (value, factor, src_ptr);
321
 
                  x++;
322
 
                  fx -= 256;
323
 
                }
324
 
              if (fx)
325
 
                {
326
 
                  factor = (fx * fy) >> 8;
327
 
                  src_ptr = src + 3 * (x + y * src_width);
328
 
                  ADD_RGB (value, factor, src_ptr);
329
 
                  dx = 256 - fx;
330
 
                }
331
 
              dy = 256 - fy;
332
 
            }
333
 
 
334
 
          *dest++ = MIN ((value[0] / area), 255);
335
 
          *dest++ = MIN ((value[1] / area), 255);
336
 
          *dest++ = MIN ((value[2] / area), 255);
337
 
 
338
 
          x0  = x;
339
 
          dx0 = dx;
340
 
        }
341
 
      x0  = 0;
342
 
      dx0 = 0;
343
 
      y0  = y;
344
 
      dy0 = dy;
345
 
    }
346
 
 
347
 
  return scale_brush;
348
 
}
349
 
 
350
 
#undef ADD_RGB