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

« back to all changes in this revision

Viewing changes to app/core/gimpdrawable-desaturate.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
 
1
/* GIMP - The GNU Image Manipulation Program
2
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or modify
20
20
 
21
21
#include <glib-object.h>
22
22
 
 
23
#include "libgimpcolor/gimpcolor.h"
 
24
 
23
25
#include "core-types.h"
24
26
 
 
27
#include "base/pixel-processor.h"
25
28
#include "base/pixel-region.h"
26
29
 
27
30
#include "gimpdrawable.h"
31
34
#include "gimp-intl.h"
32
35
 
33
36
 
 
37
static void  desaturate_region_lightness  (gpointer     data,
 
38
                                           PixelRegion *srcPR,
 
39
                                           PixelRegion *destPR);
 
40
 
 
41
static void  desaturate_region_luminosity (gpointer     data,
 
42
                                           PixelRegion *srcPR,
 
43
                                           PixelRegion *destPR);
 
44
 
 
45
static void  desaturate_region_average    (gpointer     data,
 
46
                                           PixelRegion *srcPR,
 
47
                                           PixelRegion *destPR);
 
48
 
 
49
 
34
50
void
35
 
gimp_drawable_desaturate (GimpDrawable *drawable)
 
51
gimp_drawable_desaturate (GimpDrawable       *drawable,
 
52
                          GimpDesaturateMode  mode)
36
53
{
37
 
  PixelRegion  srcPR, destPR;
38
 
  guchar      *src, *s;
39
 
  guchar      *dest, *d;
40
 
  gint         h, j;
41
 
  gint         lightness, min, max;
42
 
  gboolean     has_alpha;
43
 
  gpointer     pr;
44
 
  gint         x, y, width, height;
 
54
  PixelRegion         srcPR, destPR;
 
55
  PixelProcessorFunc  function;
 
56
  gint                x, y;
 
57
  gint                width, height;
 
58
  gboolean            has_alpha;
45
59
 
46
60
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
47
61
  g_return_if_fail (gimp_drawable_is_rgb (drawable));
48
62
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
49
63
 
50
 
  has_alpha = gimp_drawable_has_alpha (drawable);
51
 
 
52
64
  if (! gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height))
53
65
    return;
54
66
 
55
 
  pixel_region_init (&srcPR, gimp_drawable_data (drawable),
56
 
                     x, y, width, height, FALSE);
57
 
  pixel_region_init (&destPR, gimp_drawable_shadow (drawable),
58
 
                     x, y, width, height, TRUE);
59
 
 
60
 
  for (pr = pixel_regions_register (2, &srcPR, &destPR);
61
 
       pr != NULL;
62
 
       pr = pixel_regions_process (pr))
 
67
  switch (mode)
63
68
    {
64
 
      src  = srcPR.data;
65
 
      dest = destPR.data;
66
 
      h    = srcPR.h;
67
 
 
68
 
      while (h--)
69
 
        {
70
 
          s = src;
71
 
          d = dest;
72
 
 
73
 
          for (j = 0; j < srcPR.w; j++)
74
 
            {
75
 
              max = MAX (s[RED_PIX], s[GREEN_PIX]);
76
 
              max = MAX (max, s[BLUE_PIX]);
77
 
              min = MIN (s[RED_PIX], s[GREEN_PIX]);
78
 
              min = MIN (min, s[BLUE_PIX]);
79
 
 
80
 
              lightness = (max + min) / 2;
81
 
 
82
 
              d[RED_PIX]   = lightness;
83
 
              d[GREEN_PIX] = lightness;
84
 
              d[BLUE_PIX]  = lightness;
85
 
 
86
 
              if (has_alpha)
87
 
                d[ALPHA_PIX] = s[ALPHA_PIX];
88
 
 
89
 
              d += destPR.bytes;
90
 
              s += srcPR.bytes;
91
 
            }
92
 
 
93
 
          src += srcPR.rowstride;
94
 
          dest += destPR.rowstride;
95
 
        }
 
69
    case GIMP_DESATURATE_LIGHTNESS:
 
70
      function = (PixelProcessorFunc) desaturate_region_lightness;
 
71
      break;
 
72
 
 
73
      break;
 
74
    case GIMP_DESATURATE_LUMINOSITY:
 
75
      function = (PixelProcessorFunc) desaturate_region_luminosity;
 
76
      break;
 
77
 
 
78
    case GIMP_DESATURATE_AVERAGE:
 
79
      function = (PixelProcessorFunc) desaturate_region_average;
 
80
      break;
 
81
 
 
82
    default:
 
83
      g_return_if_reached ();
 
84
      return;
96
85
    }
97
86
 
 
87
  has_alpha = gimp_drawable_has_alpha (drawable);
 
88
 
 
89
  pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
 
90
                     x, y, width, height, FALSE);
 
91
  pixel_region_init (&destPR, gimp_drawable_get_shadow_tiles (drawable),
 
92
                     x, y, width, height, TRUE);
 
93
 
 
94
  pixel_regions_process_parallel (function, GINT_TO_POINTER (has_alpha),
 
95
                                  2, &srcPR, &destPR);
 
96
 
98
97
  gimp_drawable_merge_shadow (drawable, TRUE, _("Desaturate"));
99
98
 
100
99
  gimp_drawable_update (drawable, x, y, width, height);
101
100
}
 
101
 
 
102
static void
 
103
desaturate_region_lightness (gpointer     data,
 
104
                             PixelRegion *srcPR,
 
105
                             PixelRegion *destPR)
 
106
{
 
107
  const guchar *src       = srcPR->data;
 
108
  guchar       *dest      = destPR->data;
 
109
  gint          h         = srcPR->h;
 
110
  gboolean      has_alpha = GPOINTER_TO_INT (data);
 
111
 
 
112
  while (h--)
 
113
    {
 
114
      const guchar *s = src;
 
115
      guchar       *d = dest;
 
116
      gint          j;
 
117
 
 
118
      for (j = 0; j < srcPR->w; j++)
 
119
        {
 
120
          gint min, max;
 
121
          gint lightness;
 
122
 
 
123
          max = MAX (s[RED_PIX], s[GREEN_PIX]);
 
124
          max = MAX (max, s[BLUE_PIX]);
 
125
          min = MIN (s[RED_PIX], s[GREEN_PIX]);
 
126
          min = MIN (min, s[BLUE_PIX]);
 
127
 
 
128
          lightness = (max + min) / 2;
 
129
 
 
130
          d[RED_PIX]   = lightness;
 
131
          d[GREEN_PIX] = lightness;
 
132
          d[BLUE_PIX]  = lightness;
 
133
 
 
134
          if (has_alpha)
 
135
            d[ALPHA_PIX] = s[ALPHA_PIX];
 
136
 
 
137
          d += destPR->bytes;
 
138
          s += srcPR->bytes;
 
139
        }
 
140
 
 
141
      src += srcPR->rowstride;
 
142
      dest += destPR->rowstride;
 
143
    }
 
144
}
 
145
 
 
146
static void
 
147
desaturate_region_luminosity (gpointer     data,
 
148
                              PixelRegion *srcPR,
 
149
                              PixelRegion *destPR)
 
150
{
 
151
  const guchar *src       = srcPR->data;
 
152
  guchar       *dest      = destPR->data;
 
153
  gint          h         = srcPR->h;
 
154
  gboolean      has_alpha = GPOINTER_TO_INT (data);
 
155
 
 
156
  while (h--)
 
157
    {
 
158
      const guchar *s = src;
 
159
      guchar       *d = dest;
 
160
      gint          j;
 
161
 
 
162
      for (j = 0; j < srcPR->w; j++)
 
163
        {
 
164
          gint luminosity = GIMP_RGB_LUMINANCE (s[RED_PIX],
 
165
                                                s[GREEN_PIX],
 
166
                                                s[BLUE_PIX]) + 0.5;
 
167
 
 
168
          d[RED_PIX]   = luminosity;
 
169
          d[GREEN_PIX] = luminosity;
 
170
          d[BLUE_PIX]  = luminosity;
 
171
 
 
172
          if (has_alpha)
 
173
            d[ALPHA_PIX] = s[ALPHA_PIX];
 
174
 
 
175
          d += destPR->bytes;
 
176
          s += srcPR->bytes;
 
177
        }
 
178
 
 
179
      src += srcPR->rowstride;
 
180
      dest += destPR->rowstride;
 
181
    }
 
182
}
 
183
 
 
184
static void
 
185
desaturate_region_average (gpointer     data,
 
186
                           PixelRegion *srcPR,
 
187
                           PixelRegion *destPR)
 
188
{
 
189
  const guchar *src       = srcPR->data;
 
190
  guchar       *dest      = destPR->data;
 
191
  gint          h         = srcPR->h;
 
192
  gboolean      has_alpha = GPOINTER_TO_INT (data);
 
193
 
 
194
  while (h--)
 
195
    {
 
196
      const guchar *s = src;
 
197
      guchar       *d = dest;
 
198
      gint          j;
 
199
 
 
200
      for (j = 0; j < srcPR->w; j++)
 
201
        {
 
202
          gint average = (s[RED_PIX] + s[GREEN_PIX] + s[BLUE_PIX] + 1) / 3;
 
203
 
 
204
          d[RED_PIX]   = average;
 
205
          d[GREEN_PIX] = average;
 
206
          d[BLUE_PIX]  = average;
 
207
 
 
208
          if (has_alpha)
 
209
            d[ALPHA_PIX] = s[ALPHA_PIX];
 
210
 
 
211
          d += destPR->bytes;
 
212
          s += srcPR->bytes;
 
213
        }
 
214
 
 
215
      src += srcPR->rowstride;
 
216
      dest += destPR->rowstride;
 
217
    }
 
218
}