wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / gradient.c
blob930d122556525144d142b8ddef24fd7767ad3760
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"
34 static RImage *renderHGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf);
35 static RImage *renderVGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf);
36 static RImage *renderDGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf);
38 static RImage *renderMHGradient(unsigned width, unsigned height, RColor ** colors, int count);
39 static RImage *renderMVGradient(unsigned width, unsigned height, RColor ** colors, int count);
40 static RImage *renderMDGradient(unsigned width, unsigned height, RColor ** colors, int count);
42 RImage *RRenderMultiGradient(unsigned width, unsigned height, RColor ** colors, int style)
44 int count;
46 count = 0;
47 while (colors[count] != NULL)
48 count++;
50 if (count > 2) {
51 switch (style) {
52 case RHorizontalGradient:
53 return renderMHGradient(width, height, colors, count);
54 case RVerticalGradient:
55 return renderMVGradient(width, height, colors, count);
56 case RDiagonalGradient:
57 return renderMDGradient(width, height, colors, count);
59 } else if (count > 1) {
60 return RRenderGradient(width, height, colors[0], colors[1], style);
61 } else if (count > 0) {
62 return RRenderGradient(width, height, colors[0], colors[0], style);
64 assert(0);
65 return NULL;
68 RImage *RRenderGradient(unsigned width, unsigned height, const RColor * from, const RColor * to, int style)
70 switch (style) {
71 case RHorizontalGradient:
72 return renderHGradient(width, height, from->red, from->green,
73 from->blue, to->red, to->green, to->blue);
74 case RVerticalGradient:
75 return renderVGradient(width, height, from->red, from->green,
76 from->blue, to->red, to->green, to->blue);
78 case RDiagonalGradient:
79 return renderDGradient(width, height, from->red, from->green,
80 from->blue, to->red, to->green, to->blue);
82 assert(0);
83 return NULL;
87 *----------------------------------------------------------------------
88 * renderHGradient--
89 * Renders a horizontal linear gradient of the specified size in the
90 * RImage format with a border of the specified type.
92 * Returns:
93 * A 24bit RImage with the gradient (no alpha channel).
95 * Side effects:
96 * None
97 *----------------------------------------------------------------------
99 static RImage *renderHGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf)
101 int i;
102 long r, g, b, dr, dg, db;
103 unsigned lineSize = width * 3;
104 RImage *image;
105 unsigned char *ptr;
107 image = RCreateImage(width, height, False);
108 if (!image) {
109 return NULL;
111 ptr = image->data;
113 r = r0 << 16;
114 g = g0 << 16;
115 b = b0 << 16;
117 dr = ((rf - r0) << 16) / (int)width;
118 dg = ((gf - g0) << 16) / (int)width;
119 db = ((bf - b0) << 16) / (int)width;
120 /* render the first line */
121 for (i = 0; i < width; i++) {
122 *(ptr++) = (unsigned char)(r >> 16);
123 *(ptr++) = (unsigned char)(g >> 16);
124 *(ptr++) = (unsigned char)(b >> 16);
125 r += dr;
126 g += dg;
127 b += db;
130 /* copy the first line to the other lines */
131 for (i = 1; i < height; i++) {
132 memcpy(&(image->data[i * lineSize]), image->data, lineSize);
134 return image;
138 *----------------------------------------------------------------------
139 * renderVGradient--
140 * Renders a vertical linear gradient of the specified size in the
141 * RImage format with a border of the specified type.
143 * Returns:
144 * A 24bit RImage with the gradient (no alpha channel).
146 * Side effects:
147 * None
148 *----------------------------------------------------------------------
150 static RImage *renderVGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf)
152 int i, j;
153 long r, g, b, dr, dg, db;
154 RImage *image;
155 unsigned char *ptr;
156 unsigned char rr, gg, bb;
158 image = RCreateImage(width, height, False);
159 if (!image) {
160 return NULL;
162 ptr = image->data;
164 r = r0 << 16;
165 g = g0 << 16;
166 b = b0 << 16;
168 dr = ((rf - r0) << 16) / (int)height;
169 dg = ((gf - g0) << 16) / (int)height;
170 db = ((bf - b0) << 16) / (int)height;
172 for (i = 0; i < height; i++) {
173 rr = r >> 16;
174 gg = g >> 16;
175 bb = b >> 16;
176 for (j = 0; j < width / 8; j++) {
177 *(ptr++) = rr;
178 *(ptr++) = gg;
179 *(ptr++) = bb;
180 *(ptr++) = rr;
181 *(ptr++) = gg;
182 *(ptr++) = bb;
183 *(ptr++) = rr;
184 *(ptr++) = gg;
185 *(ptr++) = bb;
186 *(ptr++) = rr;
187 *(ptr++) = gg;
188 *(ptr++) = bb;
189 *(ptr++) = rr;
190 *(ptr++) = gg;
191 *(ptr++) = bb;
192 *(ptr++) = rr;
193 *(ptr++) = gg;
194 *(ptr++) = bb;
195 *(ptr++) = rr;
196 *(ptr++) = gg;
197 *(ptr++) = bb;
198 *(ptr++) = rr;
199 *(ptr++) = gg;
200 *(ptr++) = bb;
202 switch (width % 8) {
203 case 7:
204 *(ptr++) = rr;
205 *(ptr++) = gg;
206 *(ptr++) = bb;
207 case 6:
208 *(ptr++) = rr;
209 *(ptr++) = gg;
210 *(ptr++) = bb;
211 case 5:
212 *(ptr++) = rr;
213 *(ptr++) = gg;
214 *(ptr++) = bb;
215 case 4:
216 *(ptr++) = rr;
217 *(ptr++) = gg;
218 *(ptr++) = bb;
219 case 3:
220 *(ptr++) = rr;
221 *(ptr++) = gg;
222 *(ptr++) = bb;
223 case 2:
224 *(ptr++) = rr;
225 *(ptr++) = gg;
226 *(ptr++) = bb;
227 case 1:
228 *(ptr++) = rr;
229 *(ptr++) = gg;
230 *(ptr++) = bb;
232 r += dr;
233 g += dg;
234 b += db;
236 return image;
240 *----------------------------------------------------------------------
241 * renderDGradient--
242 * Renders a diagonal linear gradient of the specified size in the
243 * RImage format with a border of the specified type.
245 * Returns:
246 * A 24bit RImage with the gradient (no alpha channel).
248 * Side effects:
249 * None
250 *----------------------------------------------------------------------
253 static RImage *renderDGradient(unsigned width, unsigned height, int r0, int g0, int b0, int rf, int gf, int bf)
255 RImage *image, *tmp;
256 int j;
257 float a, offset;
258 unsigned char *ptr;
260 if (width == 1)
261 return renderVGradient(width, height, r0, g0, b0, rf, gf, bf);
262 else if (height == 1)
263 return renderHGradient(width, height, r0, g0, b0, rf, gf, bf);
265 image = RCreateImage(width, height, False);
266 if (!image) {
267 return NULL;
270 tmp = renderHGradient(2 * width - 1, 1, r0, g0, b0, rf, gf, bf);
271 if (!tmp) {
272 RReleaseImage(image);
273 return NULL;
276 ptr = tmp->data;
278 a = ((float)(width - 1)) / ((float)(height - 1));
279 width = width * 3;
281 /* copy the first line to the other lines with corresponding offset */
282 for (j = 0, offset = 0.0; j < width * height; j += width) {
283 memcpy(&(image->data[j]), &ptr[3 * (int)offset], width);
284 offset += a;
287 RReleaseImage(tmp);
288 return image;
291 static RImage *renderMHGradient(unsigned width, unsigned height, RColor ** colors, int count)
293 int i, j, k;
294 long r, g, b, dr, dg, db;
295 unsigned lineSize = width * 3;
296 RImage *image;
297 unsigned char *ptr;
298 unsigned width2;
300 assert(count > 2);
302 image = RCreateImage(width, height, False);
303 if (!image) {
304 return NULL;
306 ptr = image->data;
308 if (count > width)
309 count = width;
311 if (count > 1)
312 width2 = width / (count - 1);
313 else
314 width2 = width;
316 k = 0;
318 r = colors[0]->red << 16;
319 g = colors[0]->green << 16;
320 b = colors[0]->blue << 16;
322 /* render the first line */
323 for (i = 1; i < count; i++) {
324 dr = ((int)(colors[i]->red - colors[i - 1]->red) << 16) / (int)width2;
325 dg = ((int)(colors[i]->green - colors[i - 1]->green) << 16) / (int)width2;
326 db = ((int)(colors[i]->blue - colors[i - 1]->blue) << 16) / (int)width2;
327 for (j = 0; j < width2; j++) {
328 *ptr++ = (unsigned char)(r >> 16);
329 *ptr++ = (unsigned char)(g >> 16);
330 *ptr++ = (unsigned char)(b >> 16);
331 r += dr;
332 g += dg;
333 b += db;
334 k++;
336 r = colors[i]->red << 16;
337 g = colors[i]->green << 16;
338 b = colors[i]->blue << 16;
340 for (j = k; j < width; j++) {
341 *ptr++ = (unsigned char)(r >> 16);
342 *ptr++ = (unsigned char)(g >> 16);
343 *ptr++ = (unsigned char)(b >> 16);
346 /* copy the first line to the other lines */
347 for (i = 1; i < height; i++) {
348 memcpy(&(image->data[i * lineSize]), image->data, lineSize);
350 return image;
353 static RImage *renderMVGradient(unsigned width, unsigned height, RColor ** colors, int count)
355 int i, j, k;
356 long r, g, b, dr, dg, db;
357 unsigned lineSize = width * 3;
358 RImage *image;
359 unsigned char *ptr, *tmp;
360 unsigned height2;
361 int x;
362 unsigned char rr, gg, bb;
364 assert(count > 2);
366 image = RCreateImage(width, height, False);
367 if (!image) {
368 return NULL;
370 ptr = image->data;
372 if (count > height)
373 count = height;
375 if (count > 1)
376 height2 = height / (count - 1);
377 else
378 height2 = height;
380 k = 0;
382 r = colors[0]->red << 16;
383 g = colors[0]->green << 16;
384 b = colors[0]->blue << 16;
386 for (i = 1; i < count; i++) {
387 dr = ((int)(colors[i]->red - colors[i - 1]->red) << 16) / (int)height2;
388 dg = ((int)(colors[i]->green - colors[i - 1]->green) << 16) / (int)height2;
389 db = ((int)(colors[i]->blue - colors[i - 1]->blue) << 16) / (int)height2;
391 for (j = 0; j < height2; j++) {
392 rr = r >> 16;
393 gg = g >> 16;
394 bb = b >> 16;
396 for (x = 0; x < width / 4; x++) {
397 *ptr++ = rr;
398 *ptr++ = gg;
399 *ptr++ = bb;
400 *ptr++ = rr;
401 *ptr++ = gg;
402 *ptr++ = bb;
403 *ptr++ = rr;
404 *ptr++ = gg;
405 *ptr++ = bb;
406 *ptr++ = rr;
407 *ptr++ = gg;
408 *ptr++ = bb;
410 switch (width % 4) {
411 case 3:
412 *ptr++ = rr;
413 *ptr++ = gg;
414 *ptr++ = bb;
415 case 2:
416 *ptr++ = rr;
417 *ptr++ = gg;
418 *ptr++ = bb;
419 case 1:
420 *ptr++ = rr;
421 *ptr++ = gg;
422 *ptr++ = bb;
424 r += dr;
425 g += dg;
426 b += db;
427 k++;
429 r = colors[i]->red << 16;
430 g = colors[i]->green << 16;
431 b = colors[i]->blue << 16;
434 rr = r >> 16;
435 gg = g >> 16;
436 bb = b >> 16;
438 if (k < height) {
439 tmp = ptr;
440 for (x = 0; x < width / 4; x++) {
441 *ptr++ = rr;
442 *ptr++ = gg;
443 *ptr++ = bb;
444 *ptr++ = rr;
445 *ptr++ = gg;
446 *ptr++ = bb;
447 *ptr++ = rr;
448 *ptr++ = gg;
449 *ptr++ = bb;
450 *ptr++ = rr;
451 *ptr++ = gg;
452 *ptr++ = bb;
454 switch (width % 4) {
455 case 3:
456 *ptr++ = rr;
457 *ptr++ = gg;
458 *ptr++ = bb;
459 case 2:
460 *ptr++ = rr;
461 *ptr++ = gg;
462 *ptr++ = bb;
463 case 1:
464 *ptr++ = rr;
465 *ptr++ = gg;
466 *ptr++ = bb;
467 default:
468 break;
471 for (j = k + 1; j < height; j++) {
472 memcpy(ptr, tmp, lineSize);
473 ptr += lineSize;
477 return image;
480 static RImage *renderMDGradient(unsigned width, unsigned height, RColor ** colors, int count)
482 RImage *image, *tmp;
483 float a, offset;
484 int j;
485 unsigned char *ptr;
487 assert(count > 2);
489 if (width == 1)
490 return renderMVGradient(width, height, colors, count);
491 else if (height == 1)
492 return renderMHGradient(width, height, colors, count);
494 image = RCreateImage(width, height, False);
495 if (!image) {
496 return NULL;
499 if (count > width)
500 count = width;
501 if (count > height)
502 count = height;
504 if (count > 2)
505 tmp = renderMHGradient(2 * width - 1, 1, colors, count);
506 else
507 tmp = renderHGradient(2 * width - 1, 1, colors[0]->red << 8,
508 colors[0]->green << 8, colors[0]->blue << 8,
509 colors[1]->red << 8, colors[1]->green << 8, colors[1]->blue << 8);
511 if (!tmp) {
512 RReleaseImage(image);
513 return NULL;
515 ptr = tmp->data;
517 a = ((float)(width - 1)) / ((float)(height - 1));
518 width = width * 3;
520 /* copy the first line to the other lines with corresponding offset */
521 for (j = 0, offset = 0; j < width * height; j += width) {
522 memcpy(&(image->data[j]), &ptr[3 * (int)offset], width);
523 offset += a;
525 RReleaseImage(tmp);
526 return image;
529 RImage *RRenderInterwovenGradient(unsigned width, unsigned height,
530 RColor colors1[2], int thickness1, RColor colors2[2], int thickness2)
532 int i, j, k, l, ll;
533 long r1, g1, b1, dr1, dg1, db1;
534 long r2, g2, b2, dr2, dg2, db2;
535 RImage *image;
536 unsigned char *ptr;
537 unsigned char rr, gg, bb;
539 image = RCreateImage(width, height, False);
540 if (!image) {
541 return NULL;
543 ptr = image->data;
545 r1 = colors1[0].red << 16;
546 g1 = colors1[0].green << 16;
547 b1 = colors1[0].blue << 16;
549 r2 = colors2[0].red << 16;
550 g2 = colors2[0].green << 16;
551 b2 = colors2[0].blue << 16;
553 dr1 = ((colors1[1].red - colors1[0].red) << 16) / (int)height;
554 dg1 = ((colors1[1].green - colors1[0].green) << 16) / (int)height;
555 db1 = ((colors1[1].blue - colors1[0].blue) << 16) / (int)height;
557 dr2 = ((colors2[1].red - colors2[0].red) << 16) / (int)height;
558 dg2 = ((colors2[1].green - colors2[0].green) << 16) / (int)height;
559 db2 = ((colors2[1].blue - colors2[0].blue) << 16) / (int)height;
561 for (i = 0, k = 0, l = 0, ll = thickness1; i < height; i++) {
562 if (k == 0) {
563 rr = r1 >> 16;
564 gg = g1 >> 16;
565 bb = b1 >> 16;
566 } else {
567 rr = r2 >> 16;
568 gg = g2 >> 16;
569 bb = b2 >> 16;
571 for (j = 0; j < width / 8; j++) {
572 *(ptr++) = rr;
573 *(ptr++) = gg;
574 *(ptr++) = bb;
575 *(ptr++) = rr;
576 *(ptr++) = gg;
577 *(ptr++) = bb;
578 *(ptr++) = rr;
579 *(ptr++) = gg;
580 *(ptr++) = bb;
581 *(ptr++) = rr;
582 *(ptr++) = gg;
583 *(ptr++) = bb;
584 *(ptr++) = rr;
585 *(ptr++) = gg;
586 *(ptr++) = bb;
587 *(ptr++) = rr;
588 *(ptr++) = gg;
589 *(ptr++) = bb;
590 *(ptr++) = rr;
591 *(ptr++) = gg;
592 *(ptr++) = bb;
593 *(ptr++) = rr;
594 *(ptr++) = gg;
595 *(ptr++) = bb;
597 switch (width % 8) {
598 case 7:
599 *(ptr++) = rr;
600 *(ptr++) = gg;
601 *(ptr++) = bb;
602 case 6:
603 *(ptr++) = rr;
604 *(ptr++) = gg;
605 *(ptr++) = bb;
606 case 5:
607 *(ptr++) = rr;
608 *(ptr++) = gg;
609 *(ptr++) = bb;
610 case 4:
611 *(ptr++) = rr;
612 *(ptr++) = gg;
613 *(ptr++) = bb;
614 case 3:
615 *(ptr++) = rr;
616 *(ptr++) = gg;
617 *(ptr++) = bb;
618 case 2:
619 *(ptr++) = rr;
620 *(ptr++) = gg;
621 *(ptr++) = bb;
622 case 1:
623 *(ptr++) = rr;
624 *(ptr++) = gg;
625 *(ptr++) = bb;
627 if (++l == ll) {
628 if (k == 0) {
629 k = 1;
630 ll = thickness2;
631 } else {
632 k = 0;
633 ll = thickness1;
635 l = 0;
637 r1 += dr1;
638 g1 += dg1;
639 b1 += db1;
641 r2 += dr2;
642 g2 += dg2;
643 b2 += db2;
645 return image;