Update Serbian translation from master branch
[wmaker-crm.git] / wrlib / gradient.c
blob84db1ce0d85befc1e0db5f89b76f6599c46c2621
1 /* gradient.c - renders gradients
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
24 #include <config.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <assert.h>
32 #include "wraster.h"
33 #include "wr_i18n.h"
36 static RImage *renderHGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf);
37 static RImage *renderVGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf);
38 static RImage *renderDGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf);
40 static RImage *renderMHGradient(unsigned width, unsigned height, RColor ** colors, int count);
41 static RImage *renderMVGradient(unsigned width, unsigned height, RColor ** colors, int count);
42 static RImage *renderMDGradient(unsigned width, unsigned height, RColor ** colors, int count);
44 RImage *RRenderMultiGradient(unsigned width, unsigned height, RColor **colors, RGradientStyle style)
46 int count;
48 count = 0;
49 while (colors[count] != NULL)
50 count++;
52 if (count > 2) {
53 switch (style) {
54 case RHorizontalGradient:
55 return renderMHGradient(width, height, colors, count);
56 case RVerticalGradient:
57 return renderMVGradient(width, height, colors, count);
58 case RDiagonalGradient:
59 return renderMDGradient(width, height, colors, count);
61 } else if (count > 1) {
62 return RRenderGradient(width, height, colors[0], colors[1], style);
63 } else if (count > 0) {
64 return RRenderGradient(width, height, colors[0], colors[0], style);
66 assert(0);
67 return NULL;
70 RImage *RRenderGradient(unsigned width, unsigned height, const RColor *from, const RColor *to, RGradientStyle style)
72 switch (style) {
73 case RHorizontalGradient:
74 return renderHGradient(width, height, from->red, from->green,
75 from->blue, to->red, to->green, to->blue);
76 case RVerticalGradient:
77 return renderVGradient(width, height, from->red, from->green,
78 from->blue, to->red, to->green, to->blue);
80 case RDiagonalGradient:
81 return renderDGradient(width, height, from->red, from->green,
82 from->blue, to->red, to->green, to->blue);
84 assert(0);
85 return NULL;
89 *----------------------------------------------------------------------
90 * renderHGradient--
91 * Renders a horizontal linear gradient of the specified size in the
92 * RImage format with a border of the specified type.
94 * Returns:
95 * A 24bit RImage with the gradient (no alpha channel).
97 * Side effects:
98 * None
99 *----------------------------------------------------------------------
101 static RImage *renderHGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf)
103 int i;
104 long r, g, b, dr, dg, db;
105 unsigned lineSize = width * 3;
106 RImage *image;
107 unsigned char *ptr;
109 image = RCreateImage(width, height, False);
110 if (!image) {
111 return NULL;
113 ptr = image->data;
115 r = r0 << 16;
116 g = g0 << 16;
117 b = b0 << 16;
119 dr = ((rf - r0) << 16) / (int)width;
120 dg = ((gf - g0) << 16) / (int)width;
121 db = ((bf - b0) << 16) / (int)width;
122 /* render the first line */
123 for (i = 0; i < width; i++) {
124 *(ptr++) = (unsigned char)(r >> 16);
125 *(ptr++) = (unsigned char)(g >> 16);
126 *(ptr++) = (unsigned char)(b >> 16);
127 r += dr;
128 g += dg;
129 b += db;
132 /* copy the first line to the other lines */
133 for (i = 1; i < height; i++) {
134 memcpy(&(image->data[i * lineSize]), image->data, lineSize);
136 return image;
139 static inline unsigned char *renderGradientWidth(unsigned char *ptr, unsigned width, unsigned char r, unsigned char g, unsigned char b)
141 int i;
143 for (i = width / 4; i--;) {
144 *ptr++ = r;
145 *ptr++ = g;
146 *ptr++ = b;
148 *ptr++ = r;
149 *ptr++ = g;
150 *ptr++ = b;
152 *ptr++ = r;
153 *ptr++ = g;
154 *ptr++ = b;
156 *ptr++ = r;
157 *ptr++ = g;
158 *ptr++ = b;
160 switch (width % 4) {
161 case 3:
162 *ptr++ = r;
163 *ptr++ = g;
164 *ptr++ = b;
165 /* FALLTHRU */
166 case 2:
167 *ptr++ = r;
168 *ptr++ = g;
169 *ptr++ = b;
170 /* FALLTHRU */
171 case 1:
172 *ptr++ = r;
173 *ptr++ = g;
174 *ptr++ = b;
176 return ptr;
180 *----------------------------------------------------------------------
181 * renderVGradient--
182 * Renders a vertical linear gradient of the specified size in the
183 * RImage format with a border of the specified type.
185 * Returns:
186 * A 24bit RImage with the gradient (no alpha channel).
188 * Side effects:
189 * None
190 *----------------------------------------------------------------------
192 static RImage *renderVGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf)
194 int i;
195 long r, g, b, dr, dg, db;
196 RImage *image;
197 unsigned char *ptr;
199 image = RCreateImage(width, height, False);
200 if (!image) {
201 return NULL;
203 ptr = image->data;
205 r = r0 << 16;
206 g = g0 << 16;
207 b = b0 << 16;
209 dr = ((rf - r0) << 16) / (int)height;
210 dg = ((gf - g0) << 16) / (int)height;
211 db = ((bf - b0) << 16) / (int)height;
213 for (i = 0; i < height; i++) {
214 ptr = renderGradientWidth(ptr, width, r >> 16, g >> 16, b >> 16);
215 r += dr;
216 g += dg;
217 b += db;
219 return image;
223 *----------------------------------------------------------------------
224 * renderDGradient--
225 * Renders a diagonal linear gradient of the specified size in the
226 * RImage format with a border of the specified type.
228 * Returns:
229 * A 24bit RImage with the gradient (no alpha channel).
231 * Side effects:
232 * None
233 *----------------------------------------------------------------------
236 static RImage *renderDGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf)
238 RImage *image, *tmp;
239 int j;
240 float a, offset;
241 unsigned char *ptr;
243 if (width == 1)
244 return renderVGradient(width, height, r0, g0, b0, rf, gf, bf);
245 else if (height == 1)
246 return renderHGradient(width, height, r0, g0, b0, rf, gf, bf);
248 image = RCreateImage(width, height, False);
249 if (!image) {
250 return NULL;
253 tmp = renderHGradient(2 * width - 1, 1, r0, g0, b0, rf, gf, bf);
254 if (!tmp) {
255 RReleaseImage(image);
256 return NULL;
259 ptr = tmp->data;
261 a = ((float)(width - 1)) / ((float)(height - 1));
262 width = width * 3;
264 /* copy the first line to the other lines with corresponding offset */
265 for (j = 0, offset = 0.0; j < width * height; j += width) {
266 memcpy(&(image->data[j]), &ptr[3 * (int)offset], width);
267 offset += a;
270 RReleaseImage(tmp);
271 return image;
274 static RImage *renderMHGradient(unsigned width, unsigned height, RColor ** colors, int count)
276 int i, j, k;
277 long r, g, b, dr, dg, db;
278 unsigned lineSize = width * 3;
279 RImage *image;
280 unsigned char *ptr;
281 unsigned width2;
283 assert(count > 2);
285 image = RCreateImage(width, height, False);
286 if (!image) {
287 return NULL;
289 ptr = image->data;
291 if (count > width)
292 count = width;
294 if (count > 1)
295 width2 = width / (count - 1);
296 else
297 width2 = width;
299 k = 0;
301 r = colors[0]->red << 16;
302 g = colors[0]->green << 16;
303 b = colors[0]->blue << 16;
305 /* render the first line */
306 for (i = 1; i < count; i++) {
307 dr = ((int)(colors[i]->red - colors[i - 1]->red) << 16) / (int)width2;
308 dg = ((int)(colors[i]->green - colors[i - 1]->green) << 16) / (int)width2;
309 db = ((int)(colors[i]->blue - colors[i - 1]->blue) << 16) / (int)width2;
310 for (j = 0; j < width2; j++) {
311 *ptr++ = (unsigned char)(r >> 16);
312 *ptr++ = (unsigned char)(g >> 16);
313 *ptr++ = (unsigned char)(b >> 16);
314 r += dr;
315 g += dg;
316 b += db;
317 k++;
319 r = colors[i]->red << 16;
320 g = colors[i]->green << 16;
321 b = colors[i]->blue << 16;
323 for (j = k; j < width; j++) {
324 *ptr++ = (unsigned char)(r >> 16);
325 *ptr++ = (unsigned char)(g >> 16);
326 *ptr++ = (unsigned char)(b >> 16);
329 /* copy the first line to the other lines */
330 for (i = 1; i < height; i++) {
331 memcpy(&(image->data[i * lineSize]), image->data, lineSize);
333 return image;
336 static RImage *renderMVGradient(unsigned width, unsigned height, RColor ** colors, int count)
338 int i, j, k;
339 long r, g, b, dr, dg, db;
340 unsigned lineSize = width * 3;
341 RImage *image;
342 unsigned char *ptr, *tmp;
343 unsigned height2;
345 assert(count > 2);
347 image = RCreateImage(width, height, False);
348 if (!image) {
349 return NULL;
351 ptr = image->data;
353 if (count > height)
354 count = height;
356 if (count > 1)
357 height2 = height / (count - 1);
358 else
359 height2 = height;
361 k = 0;
363 r = colors[0]->red << 16;
364 g = colors[0]->green << 16;
365 b = colors[0]->blue << 16;
367 for (i = 1; i < count; i++) {
368 dr = ((int)(colors[i]->red - colors[i - 1]->red) << 16) / (int)height2;
369 dg = ((int)(colors[i]->green - colors[i - 1]->green) << 16) / (int)height2;
370 db = ((int)(colors[i]->blue - colors[i - 1]->blue) << 16) / (int)height2;
372 for (j = 0; j < height2; j++) {
373 ptr = renderGradientWidth(ptr, width, r >> 16, g >> 16, b >> 16);
374 r += dr;
375 g += dg;
376 b += db;
377 k++;
379 r = colors[i]->red << 16;
380 g = colors[i]->green << 16;
381 b = colors[i]->blue << 16;
384 if (k < height) {
385 tmp = ptr;
386 ptr = renderGradientWidth(ptr, width, r >> 16, g >> 16, b >> 16);
387 for (j = k + 1; j < height; j++) {
388 memcpy(ptr, tmp, lineSize);
389 ptr += lineSize;
393 return image;
396 static RImage *renderMDGradient(unsigned width, unsigned height, RColor ** colors, int count)
398 RImage *image, *tmp;
399 float a, offset;
400 int j;
401 unsigned char *ptr;
403 assert(count > 2);
405 if (width == 1)
406 return renderMVGradient(width, height, colors, count);
407 else if (height == 1)
408 return renderMHGradient(width, height, colors, count);
410 image = RCreateImage(width, height, False);
411 if (!image) {
412 return NULL;
415 if (count > width)
416 count = width;
417 if (count > height)
418 count = height;
420 if (count > 2)
421 tmp = renderMHGradient(2 * width - 1, 1, colors, count);
422 else
423 tmp = renderHGradient(2 * width - 1, 1, colors[0]->red << 8,
424 colors[0]->green << 8, colors[0]->blue << 8,
425 colors[1]->red << 8, colors[1]->green << 8, colors[1]->blue << 8);
427 if (!tmp) {
428 RReleaseImage(image);
429 return NULL;
431 ptr = tmp->data;
433 a = ((float)(width - 1)) / ((float)(height - 1));
434 width = width * 3;
436 /* copy the first line to the other lines with corresponding offset */
437 for (j = 0, offset = 0; j < width * height; j += width) {
438 memcpy(&(image->data[j]), &ptr[3 * (int)offset], width);
439 offset += a;
441 RReleaseImage(tmp);
442 return image;
445 RImage *RRenderInterwovenGradient(unsigned width, unsigned height,
446 RColor colors1[2], int thickness1, RColor colors2[2], int thickness2)
448 int i, k, l, ll;
449 long r1, g1, b1, dr1, dg1, db1;
450 long r2, g2, b2, dr2, dg2, db2;
451 RImage *image;
452 unsigned char *ptr;
454 image = RCreateImage(width, height, False);
455 if (!image) {
456 return NULL;
458 ptr = image->data;
460 r1 = colors1[0].red << 16;
461 g1 = colors1[0].green << 16;
462 b1 = colors1[0].blue << 16;
464 r2 = colors2[0].red << 16;
465 g2 = colors2[0].green << 16;
466 b2 = colors2[0].blue << 16;
468 dr1 = ((colors1[1].red - colors1[0].red) << 16) / (int)height;
469 dg1 = ((colors1[1].green - colors1[0].green) << 16) / (int)height;
470 db1 = ((colors1[1].blue - colors1[0].blue) << 16) / (int)height;
472 dr2 = ((colors2[1].red - colors2[0].red) << 16) / (int)height;
473 dg2 = ((colors2[1].green - colors2[0].green) << 16) / (int)height;
474 db2 = ((colors2[1].blue - colors2[0].blue) << 16) / (int)height;
476 for (i = 0, k = 0, l = 0, ll = thickness1; i < height; i++) {
477 if (k == 0)
478 ptr = renderGradientWidth(ptr, width, r1 >> 16, g1 >> 16, b1 >> 16);
479 else
480 ptr = renderGradientWidth(ptr, width, r2 >> 16, g2 >> 16, b2 >> 16);
482 if (++l == ll) {
483 if (k == 0) {
484 k = 1;
485 ll = thickness2;
486 } else {
487 k = 0;
488 ll = thickness1;
490 l = 0;
492 r1 += dr1;
493 g1 += dg1;
494 b1 += db1;
496 r2 += dr2;
497 g2 += dg2;
498 b2 += db2;
500 return image;