gimpviewrenderergradient.c 7.33 KB
Newer Older
1
2
3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimpviewrenderergradient.c
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
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 *
 * 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 <string.h>

#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"

#include "widgets-types.h"

#ifdef __GNUC__
#warning FIXME #include "display/display-types.h"
#endif
#include "display/display-types.h"

#include "core/gimpgradient.h"

#include "display/gimpdisplayshell-render.h"

42
#include "gimpviewrenderergradient.h"
43
44


45
46
static void   gimp_view_renderer_gradient_class_init (GimpViewRendererGradientClass *klass);
static void   gimp_view_renderer_gradient_init       (GimpViewRendererGradient      *renderer);
47

48
static void   gimp_view_renderer_gradient_finalize   (GObject             *object);
49

50
51
static void   gimp_view_renderer_gradient_render     (GimpViewRenderer    *renderer,
                                                      GtkWidget           *widget);
52
53


54
static GimpViewRendererClass *parent_class = NULL;
55
56
57


GType
58
gimp_view_renderer_gradient_get_type (void)
59
60
61
62
63
64
65
{
  static GType renderer_type = 0;

  if (! renderer_type)
    {
      static const GTypeInfo renderer_info =
      {
66
        sizeof (GimpViewRendererGradientClass),
67
68
        NULL,           /* base_init */
        NULL,           /* base_finalize */
69
        (GClassInitFunc) gimp_view_renderer_gradient_class_init,
70
71
        NULL,           /* class_finalize */
        NULL,           /* class_data */
72
        sizeof (GimpViewRendererGradient),
73
        0,              /* n_preallocs */
74
        (GInstanceInitFunc) gimp_view_renderer_gradient_init,
75
76
      };

77
      renderer_type = g_type_register_static (GIMP_TYPE_VIEW_RENDERER,
78
                                              "GimpViewRendererGradient",
79
80
81
82
83
84
85
                                              &renderer_info, 0);
    }

  return renderer_type;
}

static void
86
gimp_view_renderer_gradient_class_init (GimpViewRendererGradientClass *klass)
87
{
88
89
  GObjectClass          *object_class   = G_OBJECT_CLASS (klass);
  GimpViewRendererClass *renderer_class = GIMP_VIEW_RENDERER_CLASS (klass);
90
91
92

  parent_class = g_type_class_peek_parent (klass);

93
  object_class->finalize = gimp_view_renderer_gradient_finalize;
94

95
  renderer_class->render = gimp_view_renderer_gradient_render;
96
97
98
}

static void
99
gimp_view_renderer_gradient_init (GimpViewRendererGradient *renderer)
100
{
101
102
103
104
105
106
  renderer->even    = NULL;
  renderer->odd     = NULL;
  renderer->width   = -1;
  renderer->left    = 0.0;
  renderer->right   = 1.0;
  renderer->reverse = FALSE;
107
108
109
}

static void
110
gimp_view_renderer_gradient_finalize (GObject *object)
111
{
112
  GimpViewRendererGradient *renderer = GIMP_VIEW_RENDERER_GRADIENT (object);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

  if (renderer->even)
    {
      g_free (renderer->even);
      renderer->even = NULL;
    }

  if (renderer->odd)
    {
      g_free (renderer->odd);
      renderer->odd = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
130
131
gimp_view_renderer_gradient_render (GimpViewRenderer *renderer,
                                    GtkWidget        *widget)
132
{
133
  GimpViewRendererGradient *rendergrad;
134
  GimpGradient             *gradient;
135
  GimpGradientSegment      *seg = NULL;
136
137
138
139
140
141
142
  guchar                   *even;
  guchar                   *odd;
  guchar                   *buf;
  gint                      x;
  gint                      y;
  gdouble                   dx, cur_x;
  GimpRGB                   color;
143

144
  rendergrad = GIMP_VIEW_RENDERER_GRADIENT (renderer);
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

  gradient = GIMP_GRADIENT (renderer->viewable);

  if (renderer->width != rendergrad->width)
    {
      if (rendergrad->even)
        g_free (rendergrad->even);

      if (rendergrad->odd)
        g_free (rendergrad->odd);

      rendergrad->even = g_new (guchar, renderer->rowstride);
      rendergrad->odd  = g_new (guchar, renderer->rowstride);

      rendergrad->width = renderer->width;
    }

  even = rendergrad->even;
  odd  = rendergrad->odd;

  dx    = (rendergrad->right - rendergrad->left) / (renderer->width - 1);
  cur_x = rendergrad->left;

  for (x = 0; x < renderer->width; x++)
    {
170
      guchar r, g, b, a;
171

172
173
      seg = gimp_gradient_get_color_at (gradient, seg,
                                        cur_x, rendergrad->reverse, &color);
174
175
      cur_x += dx;

176
      gimp_rgba_get_uchar (&color, &r, &g, &b, &a);
177
178
179

      if (x & 0x4)
        {
180
181
182
          *even++ = render_blend_dark_check[(a << 8) | r];
          *even++ = render_blend_dark_check[(a << 8) | g];
          *even++ = render_blend_dark_check[(a << 8) | b];
183

184
185
186
          *odd++ = render_blend_light_check[(a << 8) | r];
          *odd++ = render_blend_light_check[(a << 8) | g];
          *odd++ = render_blend_light_check[(a << 8) | b];
187
188
189
        }
      else
        {
190
191
192
          *even++ = render_blend_light_check[(a << 8) | r];
          *even++ = render_blend_light_check[(a << 8) | g];
          *even++ = render_blend_light_check[(a << 8) | b];
193

194
195
196
          *odd++ = render_blend_dark_check[(a << 8) | r];
          *odd++ = render_blend_dark_check[(a << 8) | g];
          *odd++ = render_blend_dark_check[(a << 8) | b];
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
        }
    }

  if (! renderer->buffer)
    renderer->buffer = g_new (guchar, renderer->height * renderer->rowstride);

  buf = renderer->buffer;

  for (y = 0; y < renderer->height; y++)
    {
      if (y & 0x4)
        memcpy (buf, rendergrad->even, renderer->rowstride);
      else
        memcpy (buf, rendergrad->odd,  renderer->rowstride);

      buf += renderer->rowstride;
    }

  renderer->needs_render = FALSE;
}

void
219
220
221
222
gimp_view_renderer_gradient_set_offsets (GimpViewRendererGradient *renderer,
                                         gdouble                   left,
                                         gdouble                   right,
                                         gboolean                  instant_update)
223
{
224
  g_return_if_fail (GIMP_IS_VIEW_RENDERER_GRADIENT (renderer));
225
226
227
228
229
230
231
232
233

  left  = CLAMP (left, 0.0, 1.0);
  right = CLAMP (right, left, 1.0);

  if (left != renderer->left || right != renderer->right)
    {
      renderer->left  = left;
      renderer->right = right;

234
      gimp_view_renderer_invalidate (GIMP_VIEW_RENDERER (renderer));
235
236

      if (instant_update)
237
        gimp_view_renderer_update (GIMP_VIEW_RENDERER (renderer));
238
239
    }
}
240
241

void
242
243
gimp_view_renderer_gradient_set_reverse (GimpViewRendererGradient *renderer,
                                         gboolean                     reverse)
244
{
245
  g_return_if_fail (GIMP_IS_VIEW_RENDERER_GRADIENT (renderer));
246
247
248
249
250

  if (reverse != renderer->reverse)
    {
      renderer->reverse = reverse ? TRUE : FALSE;

251
252
      gimp_view_renderer_invalidate (GIMP_VIEW_RENDERER (renderer));
      gimp_view_renderer_update (GIMP_VIEW_RENDERER (renderer));
253
254
    }
}