2 * art_render_gradient.c: Gradient image source for modular rendering.
4 * Libart_LGPL - library of basic graphic primitives
5 * Copyright (C) 2000 Raph Levien
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Authors: Raph Levien <raph@acm.org>
23 * Alexander Larsson <alla@lysator.liu.se>
29 #include "art_alphagamma.h"
30 #include "art_filterlevel.h"
32 #include "art_render.h"
33 #include "art_render_gradient.h"
35 typedef struct _ArtImageSourceGradLin ArtImageSourceGradLin;
36 typedef struct _ArtImageSourceGradRad ArtImageSourceGradRad;
38 struct _ArtImageSourceGradLin {
40 const ArtGradientLinear *gradient;
43 struct _ArtImageSourceGradRad {
45 const ArtGradientRadial *gradient;
52 * art_render_gradient_setpix: Set a gradient pixel.
53 * @render: The render object.
54 * @dst: Pointer to destination (where to store pixel).
55 * @n_stops: Number of stops in @stops.
56 * @stops: The stops for the gradient.
57 * @offset: The offset.
59 * @n_stops must be > 0.
61 * Sets a gradient pixel, storing it at @dst.
64 art_render_gradient_setpix (ArtRender *render,
66 int n_stops, ArtGradientStop *stops,
72 int n_ch = render->n_chan + 1;
74 for (ix = 0; ix < n_stops; ix++)
75 if (stops[ix].offset > offset)
77 /* stops[ix - 1].offset < offset < stops[ix].offset */
78 if (ix > 0 && ix < n_stops)
80 off0 = stops[ix - 1].offset;
81 off1 = stops[ix].offset;
82 if (fabs (off1 - off0) > EPSILON)
86 interp = (offset - off0) / (off1 - off0);
87 for (j = 0; j < n_ch; j++)
91 z0 = stops[ix - 1].color[j];
92 z1 = stops[ix].color[j];
93 z = floor (z0 + (z1 - z0) * interp + 0.5);
94 if (render->buf_depth == 8)
95 dst[j] = ART_PIX_8_FROM_MAX (z);
96 else /* (render->buf_depth == 16) */
97 ((art_u16 *)dst)[j] = z;
102 else if (ix == n_stops)
105 for (j = 0; j < n_ch; j++)
108 z = stops[ix].color[j];
109 if (render->buf_depth == 8)
110 dst[j] = ART_PIX_8_FROM_MAX (z);
111 else /* (render->buf_depth == 16) */
112 ((art_u16 *)dst)[j] = z;
117 art_render_gradient_linear_done (ArtRenderCallback *self, ArtRender *render)
123 art_render_gradient_linear_render (ArtRenderCallback *self, ArtRender *render,
126 ArtImageSourceGradLin *z = (ArtImageSourceGradLin *)self;
127 const ArtGradientLinear *gradient = z->gradient;
128 int pixstride = (render->n_chan + 1) * (render->depth >> 3);
130 int width = render->x1 - render->x0;
131 double offset, d_offset;
132 double actual_offset;
133 int n_stops = gradient->n_stops;
134 ArtGradientStop *stops = gradient->stops;
135 art_u8 *bufp = render->image_buf;
136 ArtGradientSpread spread = gradient->spread;
138 offset = render->x0 * gradient->a + y * gradient->b + gradient->c;
139 d_offset = gradient->a;
141 for (x = 0; x < width; x++)
143 if (spread == ART_GRADIENT_PAD)
144 actual_offset = offset;
145 else if (spread == ART_GRADIENT_REPEAT)
146 actual_offset = offset - floor (offset);
147 else /* (spread == ART_GRADIENT_REFLECT) */
151 tmp = offset - 2 * floor (0.5 * offset);
152 actual_offset = tmp > 1 ? 2 - tmp : tmp;
154 art_render_gradient_setpix (render, bufp, n_stops, stops, actual_offset);
161 art_render_gradient_linear_negotiate (ArtImageSource *self, ArtRender *render,
162 ArtImageSourceFlags *p_flags,
163 int *p_buf_depth, ArtAlphaType *p_alpha)
165 self->super.render = art_render_gradient_linear_render;
167 *p_buf_depth = render->depth;
168 *p_alpha = ART_ALPHA_PREMUL;
172 * art_render_gradient_linear: Add a linear gradient image source.
173 * @render: The render object.
174 * @gradient: The linear gradient.
176 * Adds the linear gradient @gradient as the image source for rendering
177 * in the render object @render.
180 art_render_gradient_linear (ArtRender *render,
181 const ArtGradientLinear *gradient,
182 ArtFilterLevel level)
184 ArtImageSourceGradLin *image_source = art_new (ArtImageSourceGradLin, 1);
186 image_source->super.super.render = NULL;
187 image_source->super.super.done = art_render_gradient_linear_done;
188 image_source->super.negotiate = art_render_gradient_linear_negotiate;
190 image_source->gradient = gradient;
192 art_render_add_image_source (render, &image_source->super);
196 art_render_gradient_radial_done (ArtRenderCallback *self, ArtRender *render)
202 art_render_gradient_radial_render (ArtRenderCallback *self, ArtRender *render,
205 ArtImageSourceGradRad *z = (ArtImageSourceGradRad *)self;
206 const ArtGradientRadial *gradient = z->gradient;
207 int pixstride = (render->n_chan + 1) * (render->depth >> 3);
210 int width = render->x1 - x0;
211 int n_stops = gradient->n_stops;
212 ArtGradientStop *stops = gradient->stops;
213 art_u8 *bufp = render->image_buf;
214 double fx = gradient->fx;
215 double fy = gradient->fy;
217 double *affine = gradient->affine;
218 double aff0 = affine[0];
219 double aff1 = affine[1];
220 const double a = z->a;
221 const double arecip = 1.0 / a;
225 double rad, drad, ddrad;
227 dx = x0 * aff0 + y * affine[2] + affine[4] - fx;
228 dy = x0 * aff1 + y * affine[3] + affine[5] - fy;
229 b = dx * fx + dy * fy;
230 db = aff0 * fx + aff1 * fy;
231 c = dx * dx + dy * dy;
232 dc = 2 * aff0 * dx + aff0 * aff0 + 2 * aff1 * dy + aff1 * aff1;
233 ddc = 2 * aff0 * aff0 + 2 * aff1 * aff1;
238 rad = b_a * b_a + c * arecip;
239 drad = 2 * b_a * db_a + db_a * db_a + dc * arecip;
240 ddrad = 2 * db_a * db_a + ddc * arecip;
242 for (x = 0; x < width; x++)
247 z = b_a + sqrt (rad);
250 art_render_gradient_setpix (render, bufp, n_stops, stops, z);
259 art_render_gradient_radial_negotiate (ArtImageSource *self, ArtRender *render,
260 ArtImageSourceFlags *p_flags,
261 int *p_buf_depth, ArtAlphaType *p_alpha)
263 self->super.render = art_render_gradient_radial_render;
265 *p_buf_depth = render->depth;
266 *p_alpha = ART_ALPHA_PREMUL;
270 * art_render_gradient_radial: Add a radial gradient image source.
271 * @render: The render object.
272 * @gradient: The radial gradient.
274 * Adds the radial gradient @gradient as the image source for rendering
275 * in the render object @render.
278 art_render_gradient_radial (ArtRender *render,
279 const ArtGradientRadial *gradient,
280 ArtFilterLevel level)
282 ArtImageSourceGradRad *image_source = art_new (ArtImageSourceGradRad, 1);
283 double fx = gradient->fx;
284 double fy = gradient->fy;
286 image_source->super.super.render = NULL;
287 image_source->super.super.done = art_render_gradient_radial_done;
288 image_source->super.negotiate = art_render_gradient_radial_negotiate;
290 image_source->gradient = gradient;
291 /* todo: sanitycheck fx, fy? */
292 image_source->a = 1 - fx * fx - fy * fy;
294 art_render_add_image_source (render, &image_source->super);