wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / draw.c
blob350837c65e47db95ad2e3623c9a20e0093d70539
1 /* draw.c - pixel plotting, line drawing
3 * Raster graphics library
5 * Copyright (c) 1998-2003 Dan Pascu
6 * Copyright (c) 2000-2003 Alfredo K. Kojima
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 <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
30 #include "wraster.h"
32 #define MIN(a,b) ((a) < (b) ? (a) : (b))
33 #define MAX(a,b) ((a) > (b) ? (a) : (b))
36 * Returns the color of the pixel at coordinates (x, y) in "color".
38 Bool RGetPixel(RImage * image, int x, int y, RColor * color)
40 int ofs;
42 assert(image != NULL);
43 if (x < 0 || x >= image->width || y < 0 || y >= image->height)
44 return False;
46 if (image->format == RRGBAFormat) {
47 ofs = (y * image->width + x) * 4;
48 color->red = image->data[ofs++];
49 color->green = image->data[ofs++];
50 color->blue = image->data[ofs++];
51 color->alpha = image->data[ofs];
52 } else {
53 ofs = (y * image->width + x) * 3;
54 color->red = image->data[ofs++];
55 color->green = image->data[ofs++];
56 color->blue = image->data[ofs];
57 /* If the image does not have alpha channel, we consider alpha 255 */
58 color->alpha = 255;
61 return True;
64 void RPutPixel(RImage *image, int x, int y, const RColor *color)
66 unsigned char *ptr;
68 assert(image != NULL);
69 assert(color != NULL);
70 if (x < 0 || x >= image->width || y < 0 || y >= image->height)
71 return;
73 if (image->format == RRGBAFormat) {
74 ptr = image->data + (y * image->width + x) * 4;
75 } else {
76 ptr = image->data + (y * image->width + x) * 3;
79 if (color->alpha == 255) {
80 *ptr++ = color->red;
81 *ptr++ = color->green;
82 *ptr++ = color->blue;
83 if (image->format == RRGBAFormat) {
84 *ptr = 255;
86 } else {
87 register int alpha, nalpha, r, g, b;
89 r = color->red;
90 g = color->green;
91 b = color->blue;
92 alpha = color->alpha;
93 nalpha = 255 - alpha;
95 *ptr = (((int)*ptr * nalpha) + (r * alpha)) / 256;
96 ptr++;
97 *ptr = (((int)*ptr * nalpha) + (g * alpha)) / 256;
98 ptr++;
99 *ptr = (((int)*ptr * nalpha) + (b * alpha)) / 256;
100 ptr++;
101 if (image->format == RRGBAFormat) {
102 *ptr = alpha + ((int)*ptr * nalpha) / 256;
107 static void operatePixel(RImage * image, int ofs, int operation, const RColor * color)
109 unsigned char *sr, *sg, *sb, *sa;
110 register int alpha, nalpha, tmp;
111 int hasAlpha = image->format == RRGBAFormat;
113 alpha = color->alpha;
114 nalpha = 255 - alpha;
116 sr = image->data + ofs * (hasAlpha ? 4 : 3);
117 sg = image->data + ofs * (hasAlpha ? 4 : 3) + 1;
118 sb = image->data + ofs * (hasAlpha ? 4 : 3) + 2;
119 sa = image->data + ofs * (hasAlpha ? 4 : 3) + 3;
121 switch (operation) {
122 case RClearOperation:
123 *sr = 0;
124 *sg = 0;
125 *sb = 0;
126 if (hasAlpha)
127 *sa = 0;
128 break;
129 case RCopyOperation:
130 *sr = color->red;
131 *sg = color->green;
132 *sb = color->blue;
133 if (hasAlpha)
134 *sa = color->alpha;
135 break;
136 case RNormalOperation:
137 if (color->alpha == 255) {
138 *sr = color->red;
139 *sg = color->green;
140 *sb = color->blue;
141 if (hasAlpha)
142 *sa = 255;
143 } else {
144 *sr = (((int)*sr * nalpha) + ((int)color->red * alpha)) / 256;
145 *sg = (((int)*sg * nalpha) + ((int)color->green * alpha)) / 256;
146 *sb = (((int)*sb * nalpha) + ((int)color->blue * alpha)) / 256;
147 *sa = alpha + ((int)*sa * nalpha) / 256;
149 break;
150 case RAddOperation:
151 tmp = color->red + *sr;
152 *sr = MIN(255, tmp);
153 tmp = color->green + *sg;
154 *sg = MIN(255, tmp);
155 tmp = color->blue + *sb;
156 *sb = MIN(255, tmp);
157 if (hasAlpha)
158 *sa = MIN(*sa, color->alpha);
159 break;
160 case RSubtractOperation:
161 tmp = *sr - color->red;
162 *sr = MAX(0, tmp);
163 tmp = *sg - color->green;
164 *sg = MAX(0, tmp);
165 tmp = *sb - color->blue;
166 *sb = MAX(0, tmp);
167 if (hasAlpha)
168 *sa = MIN(*sa, color->alpha);
169 break;
173 void ROperatePixel(RImage * image, int operation, int x, int y, const RColor * color)
175 int ofs;
177 assert(image != NULL);
178 assert(color != NULL);
179 assert(x >= 0 && x < image->width);
180 assert(y >= 0 && y < image->height);
182 ofs = y * image->width + x;
184 operatePixel(image, ofs, operation, color);
187 void RPutPixels(RImage * image, const RPoint * points, int npoints, int mode, const RColor * color)
189 register int x, y, i;
191 assert(image != NULL);
192 assert(points != NULL);
194 x = y = 0;
196 for (i = 0; i < npoints; i++) {
197 if (mode == RAbsoluteCoordinates) {
198 x = points[i].x;
199 y = points[i].y;
200 } else {
201 x += points[i].x;
202 y += points[i].y;
204 RPutPixel(image, x, y, color);
208 void ROperatePixels(RImage * image, int operation, const RPoint * points, int npoints, int mode, const RColor * color)
210 register int x, y, i;
212 assert(image != NULL);
213 assert(points != NULL);
215 x = y = 0;
217 for (i = 0; i < npoints; i++) {
218 if (mode == RAbsoluteCoordinates) {
219 x = points[i].x;
220 y = points[i].y;
221 } else {
222 x += points[i].x;
223 y += points[i].y;
225 ROperatePixel(image, operation, x, y, color);
229 static Bool clipLineInRectangle(int xmin, int ymin, int xmax, int ymax, int *x1, int *y1, int *x2, int *y2)
231 #define TOP (1<<0)
232 #define BOT (1<<1)
233 #define LEF (1<<2)
234 #define RIG (1<<3)
235 #define CHECK_OUT(X,Y) (((Y) > ymax ? TOP : ((Y) < ymin ? BOT : 0))\
236 | ((X) > xmax ? RIG : ((X) < xmin ? LEF : 0)))
238 int ocode1, ocode2, ocode;
239 int accept = 0;
240 int x, y;
242 ocode1 = CHECK_OUT(*x1, *y1);
243 ocode2 = CHECK_OUT(*x2, *y2);
245 for (;;) {
246 if (!ocode1 && !ocode2) { /* completely inside */
247 accept = 1;
248 break;
249 } else if (ocode1 & ocode2) {
250 break;
253 if (ocode1)
254 ocode = ocode1;
255 else
256 ocode = ocode2;
258 if (ocode & TOP) {
259 x = *x1 + (*x2 - *x1) * (ymax - *y1) / (*y2 - *y1);
260 y = ymax;
261 } else if (ocode & BOT) {
262 x = *x1 + (*x2 - *x1) * (ymin - *y1) / (*y2 - *y1);
263 y = ymin;
264 } else if (ocode & RIG) {
265 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
266 x = xmax;
267 } else { /* //if (ocode & LEF) { */
268 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
269 x = xmin;
272 if (ocode == ocode1) {
273 *x1 = x;
274 *y1 = y;
275 ocode1 = CHECK_OUT(x, y);
276 } else {
277 *x2 = x;
278 *y2 = y;
279 ocode2 = CHECK_OUT(x, y);
283 return accept;
287 * This routine is a generic drawing routine, based on Bresenham's line
288 * drawing algorithm.
290 static int genericLine(RImage * image, int x0, int y0, int x1, int y1, const RColor * color, int operation, int polyline)
292 int i, err, du, dv, du2, dv2, uofs, vofs, last;
294 assert(image != NULL);
296 if (!clipLineInRectangle(0, 0, image->width - 1, image->height - 1, &x0, &y0, &x1, &y1))
297 return True;
299 if (x0 < x1) {
300 du = x1 - x0;
301 uofs = 1;
302 } else {
303 du = x0 - x1;
304 uofs = -1;
306 if (y0 < y1) {
307 dv = y1 - y0;
308 vofs = image->width;
309 } else {
310 dv = y0 - y1;
311 vofs = -image->width;
314 if (du < dv) {
315 /* Swap coordinates between them, so that always du>dv */
316 i = du;
317 du = dv;
318 dv = i;
319 i = uofs;
320 uofs = vofs;
321 vofs = i;
324 err = 0;
325 du2 = du << 1;
326 dv2 = dv << 1;
327 last = (polyline) ? du - 1 : du;
329 if (color->alpha == 255 || operation == RCopyOperation) {
330 unsigned char *ptr;
332 if (image->format == RRGBAFormat)
333 i = (y0 * image->width + x0) * 4;
334 else
335 i = (y0 * image->width + x0) * 3;
336 ptr = image->data + i;
338 for (i = 0; i <= last; i++) {
339 /* Draw the pixel */
340 *ptr = color->red;
341 *(ptr + 1) = color->green;
342 *(ptr + 2) = color->blue;
343 if (image->format == RRGBAFormat)
344 *(ptr + 3) = 255;
346 /* Compute error for NeXT Step */
347 err += dv2;
348 if (err >= du) {
349 if (image->format == RRGBAFormat)
350 ptr += vofs * 4;
351 else
352 ptr += vofs * 3;
353 err -= du2;
355 if (image->format == RRGBAFormat)
356 ptr += uofs * 4;
357 else
358 ptr += uofs * 3;
360 } else {
361 register int ofs = y0 * image->width + x0;
363 for (i = 0; i <= last; i++) {
364 /* Draw the pixel */
365 operatePixel(image, ofs, operation, color);
367 /* Compute error for NeXT Step */
368 err += dv2;
369 if (err >= du) {
370 ofs += vofs;
371 err -= du2;
373 ofs += uofs;
377 return True;
380 int RDrawLine(RImage * image, int x0, int y0, int x1, int y1, const RColor * color)
382 return genericLine(image, x0, y0, x1, y1, color, RNormalOperation, False);
385 int ROperateLine(RImage * image, int operation, int x0, int y0, int x1, int y1, const RColor * color)
387 return genericLine(image, x0, y0, x1, y1, color, operation, False);
390 void RDrawLines(RImage * image, const RPoint * points, int npoints, int mode, const RColor * color)
392 register int x1, y1, x2, y2, i;
394 assert(points != NULL);
396 if (npoints == 0)
397 return;
399 x1 = points[0].x;
400 y1 = points[0].y;
401 x2 = y2 = 0;
403 for (i = 1; i < npoints - 1; i++) {
404 if (mode == RAbsoluteCoordinates) {
405 x2 = points[i].x;
406 y2 = points[i].y;
407 } else {
408 x2 += points[i - 1].x;
409 y2 += points[i - 1].y;
411 /* Don't draw pixels at junction points twice */
412 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, True);
413 x1 = x2;
414 y1 = y2;
416 i = npoints - 1; /* last point */
417 if (mode == RAbsoluteCoordinates) {
418 x2 = points[i].x;
419 y2 = points[i].y;
420 } else {
421 x2 += points[i - 1].x;
422 y2 += points[i - 1].y;
424 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
425 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, i);
428 void ROperateLines(RImage * image, int operation, const RPoint * points, int npoints, int mode, const RColor * color)
430 register int x1, y1, x2, y2, i;
432 assert(points != NULL);
434 if (npoints == 0)
435 return;
437 x1 = points[0].x;
438 y1 = points[0].y;
439 x2 = y2 = 0;
441 for (i = 1; i < npoints - 1; i++) {
442 if (mode == RAbsoluteCoordinates) {
443 x2 = points[i].x;
444 y2 = points[i].y;
445 } else {
446 x2 += points[i - 1].x;
447 y2 += points[i - 1].y;
449 /* Don't draw pixels at junction points twice */
450 genericLine(image, x1, y1, x2, y2, color, operation, True);
451 x1 = x2;
452 y1 = y2;
454 i = npoints - 1; /* last point */
455 if (mode == RAbsoluteCoordinates) {
456 x2 = points[i].x;
457 y2 = points[i].y;
458 } else {
459 x2 += points[i - 1].x;
460 y2 += points[i - 1].y;
462 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
463 genericLine(image, x1, y1, x2, y2, color, operation, i);
466 void RDrawSegments(RImage * image, const RSegment * segs, int nsegs, const RColor * color)
468 register int i;
470 assert(segs != NULL);
472 for (i = 0; i < nsegs; i++) {
473 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, RNormalOperation, False);
474 segs++;
478 void ROperateSegments(RImage * image, int operation, const RSegment * segs, int nsegs, const RColor * color)
480 register int i;
482 assert(segs != NULL);
484 for (i = 0; i < nsegs; i++) {
485 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, operation, False);
486 segs++;