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

« back to all changes in this revision

Viewing changes to modules/cdisplay_highcontrast.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GIMP - The GNU Image Manipulation Program
2
 
 * Copyright (C) 1999 Manish Singh <yosh@gimp.org>
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 <gtk/gtk.h>
22
 
 
23
 
#include "libgimpconfig/gimpconfig.h"
24
 
#include "libgimpmath/gimpmath.h"
25
 
#include "libgimpmodule/gimpmodule.h"
26
 
#include "libgimpwidgets/gimpwidgets.h"
27
 
 
28
 
#include "libgimp/libgimp-intl.h"
29
 
 
30
 
 
31
 
#define DEFAULT_CONTRAST 1.0
32
 
 
33
 
 
34
 
#define CDISPLAY_TYPE_CONTRAST            (cdisplay_contrast_type)
35
 
#define CDISPLAY_CONTRAST(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), CDISPLAY_TYPE_CONTRAST, CdisplayContrast))
36
 
#define CDISPLAY_CONTRAST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), CDISPLAY_TYPE_CONTRAST, CdisplayContrastClass))
37
 
#define CDISPLAY_IS_CONTRAST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CDISPLAY_TYPE_CONTRAST))
38
 
#define CDISPLAY_IS_CONTRAST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CDISPLAY_TYPE_CONTRAST))
39
 
 
40
 
 
41
 
typedef struct _CdisplayContrast      CdisplayContrast;
42
 
typedef struct _CdisplayContrastClass CdisplayContrastClass;
43
 
 
44
 
struct _CdisplayContrast
45
 
{
46
 
  GimpColorDisplay  parent_instance;
47
 
 
48
 
  gdouble           contrast;
49
 
  guchar            lookup[256];
50
 
};
51
 
 
52
 
struct _CdisplayContrastClass
53
 
{
54
 
  GimpColorDisplayClass  parent_instance;
55
 
};
56
 
 
57
 
 
58
 
enum
59
 
{
60
 
  PROP_0,
61
 
  PROP_CONTRAST
62
 
};
63
 
 
64
 
 
65
 
static GType       cdisplay_contrast_get_type     (GTypeModule           *module);
66
 
static void        cdisplay_contrast_class_init   (CdisplayContrastClass *klass);
67
 
 
68
 
static void        cdisplay_contrast_set_property (GObject          *object,
69
 
                                                   guint             property_id,
70
 
                                                   const GValue     *value,
71
 
                                                   GParamSpec       *pspec);
72
 
static void        cdisplay_contrast_get_property (GObject          *object,
73
 
                                                   guint             property_id,
74
 
                                                   GValue           *value,
75
 
                                                   GParamSpec       *pspec);
76
 
 
77
 
static void        cdisplay_contrast_convert      (GimpColorDisplay *display,
78
 
                                                   guchar           *buf,
79
 
                                                   gint              w,
80
 
                                                   gint              h,
81
 
                                                   gint              bpp,
82
 
                                                   gint              bpl);
83
 
static GtkWidget * cdisplay_contrast_configure    (GimpColorDisplay *display);
84
 
static void        cdisplay_contrast_set_contrast (CdisplayContrast *contrast,
85
 
                                                   gdouble           value);
86
 
 
87
 
 
88
 
static const GimpModuleInfo cdisplay_contrast_info =
89
 
{
90
 
  GIMP_MODULE_ABI_VERSION,
91
 
  N_("High Contrast color display filter"),
92
 
  "Jay Cox <jaycox@gimp.org>",
93
 
  "v0.2",
94
 
  "(c) 2000, released under the GPL",
95
 
  "October 14, 2000"
96
 
};
97
 
 
98
 
static GType                  cdisplay_contrast_type = 0;
99
 
static GimpColorDisplayClass *parent_class        = NULL;
100
 
 
101
 
 
102
 
G_MODULE_EXPORT const GimpModuleInfo *
103
 
gimp_module_query (GTypeModule *module)
104
 
{
105
 
  return &cdisplay_contrast_info;
106
 
}
107
 
 
108
 
G_MODULE_EXPORT gboolean
109
 
gimp_module_register (GTypeModule *module)
110
 
{
111
 
  cdisplay_contrast_get_type (module);
112
 
 
113
 
  return TRUE;
114
 
}
115
 
 
116
 
static GType
117
 
cdisplay_contrast_get_type (GTypeModule *module)
118
 
{
119
 
  if (! cdisplay_contrast_type)
120
 
    {
121
 
      const GTypeInfo display_info =
122
 
      {
123
 
        sizeof (CdisplayContrastClass),
124
 
        (GBaseInitFunc) NULL,
125
 
        (GBaseFinalizeFunc) NULL,
126
 
        (GClassInitFunc) cdisplay_contrast_class_init,
127
 
        NULL,           /* class_finalize */
128
 
        NULL,           /* class_data     */
129
 
        sizeof (CdisplayContrast),
130
 
        0,              /* n_preallocs    */
131
 
        NULL            /* instance_init  */
132
 
      };
133
 
 
134
 
      cdisplay_contrast_type =
135
 
        g_type_module_register_type (module,
136
 
                                     GIMP_TYPE_COLOR_DISPLAY,
137
 
                                     "CdisplayContrast",
138
 
                                     &display_info, 0);
139
 
    }
140
 
 
141
 
  return cdisplay_contrast_type;
142
 
}
143
 
 
144
 
static void
145
 
cdisplay_contrast_class_init (CdisplayContrastClass *klass)
146
 
{
147
 
  GObjectClass          *object_class  = G_OBJECT_CLASS (klass);
148
 
  GimpColorDisplayClass *display_class = GIMP_COLOR_DISPLAY_CLASS (klass);
149
 
 
150
 
  parent_class = g_type_class_peek_parent (klass);
151
 
 
152
 
  object_class->get_property = cdisplay_contrast_get_property;
153
 
  object_class->set_property = cdisplay_contrast_set_property;
154
 
 
155
 
  GIMP_CONFIG_INSTALL_PROP_DOUBLE (object_class, PROP_CONTRAST,
156
 
                                   "contrast", NULL,
157
 
                                   0.01, 10.0, DEFAULT_CONTRAST,
158
 
                                   0);
159
 
 
160
 
  display_class->name        = _("Contrast");
161
 
  display_class->help_id     = "gimp-colordisplay-contrast";
162
 
  display_class->stock_id    = GIMP_STOCK_DISPLAY_FILTER_CONTRAST;
163
 
 
164
 
  display_class->convert     = cdisplay_contrast_convert;
165
 
  display_class->configure   = cdisplay_contrast_configure;
166
 
}
167
 
 
168
 
static void
169
 
cdisplay_contrast_get_property (GObject    *object,
170
 
                                guint       property_id,
171
 
                                GValue     *value,
172
 
                                GParamSpec *pspec)
173
 
{
174
 
  CdisplayContrast *contrast = CDISPLAY_CONTRAST (object);
175
 
 
176
 
  switch (property_id)
177
 
    {
178
 
    case PROP_CONTRAST:
179
 
      g_value_set_double (value, contrast->contrast);
180
 
      break;
181
 
    default:
182
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
183
 
      break;
184
 
    }
185
 
}
186
 
 
187
 
static void
188
 
cdisplay_contrast_set_property (GObject      *object,
189
 
                                guint         property_id,
190
 
                                const GValue *value,
191
 
                                GParamSpec   *pspec)
192
 
{
193
 
  CdisplayContrast *contrast = CDISPLAY_CONTRAST (object);
194
 
 
195
 
  switch (property_id)
196
 
    {
197
 
    case PROP_CONTRAST:
198
 
      cdisplay_contrast_set_contrast (contrast, g_value_get_double (value));
199
 
      break;
200
 
    default:
201
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
202
 
      break;
203
 
    }
204
 
}
205
 
 
206
 
static void
207
 
cdisplay_contrast_convert (GimpColorDisplay *display,
208
 
                           guchar           *buf,
209
 
                           gint              width,
210
 
                           gint              height,
211
 
                           gint              bpp,
212
 
                           gint              bpl)
213
 
{
214
 
  CdisplayContrast *contrast = CDISPLAY_CONTRAST (display);
215
 
  gint              i, j     = height;
216
 
 
217
 
  /* You will not be using the entire buffer most of the time.
218
 
   * Hence, the simplistic code for this is as follows:
219
 
   *
220
 
   * for (j = 0; j < height; j++)
221
 
   *   {
222
 
   *     for (i = 0; i < width * bpp; i++)
223
 
   *       buf[i] = lookup[buf[i]];
224
 
   *     buf += bpl;
225
 
   *   }
226
 
   */
227
 
 
228
 
  width *= bpp;
229
 
  bpl -= width;
230
 
 
231
 
  while (j--)
232
 
    {
233
 
      i = width;
234
 
      while (i--)
235
 
        {
236
 
          *buf = contrast->lookup[*buf];
237
 
          buf++;
238
 
        }
239
 
      buf += bpl;
240
 
    }
241
 
}
242
 
 
243
 
static GtkWidget *
244
 
cdisplay_contrast_configure (GimpColorDisplay *display)
245
 
{
246
 
  CdisplayContrast *contrast = CDISPLAY_CONTRAST (display);
247
 
  GtkWidget        *hbox;
248
 
  GtkWidget        *label;
249
 
  GtkWidget        *spinbutton;
250
 
 
251
 
  hbox = gtk_hbox_new (FALSE, 6);
252
 
 
253
 
  label = gtk_label_new_with_mnemonic (_("Contrast c_ycles:"));
254
 
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
255
 
  gtk_widget_show (label);
256
 
 
257
 
  spinbutton = gimp_prop_spin_button_new (G_OBJECT (contrast), "contrast",
258
 
                                          0.1, 1.0, 3);
259
 
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
260
 
  gtk_widget_show (spinbutton);
261
 
 
262
 
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinbutton);
263
 
 
264
 
  return hbox;
265
 
}
266
 
 
267
 
static void
268
 
cdisplay_contrast_set_contrast (CdisplayContrast *contrast,
269
 
                                gdouble           value)
270
 
{
271
 
  if (value <= 0.0)
272
 
    value = 1.0;
273
 
 
274
 
  if (value != contrast->contrast)
275
 
    {
276
 
      gint i;
277
 
 
278
 
      contrast->contrast = value;
279
 
 
280
 
      for (i = 0; i < 256; i++)
281
 
        {
282
 
          contrast->lookup[i] = (guchar) (gint)
283
 
            (255 * .5 * (1 + sin (value * 2 * G_PI * i / 255.0)));
284
 
        }
285
 
 
286
 
      g_object_notify (G_OBJECT (contrast), "contrast");
287
 
      gimp_color_display_changed (GIMP_COLOR_DISPLAY (contrast));
288
 
    }
289
 
}