wrlib: add explicit type definition in API to allow compiler Type Checks (2/3)
[wmaker-crm.git] / wrlib / draw.c
blobbf44f8c3d6b2c9edc82c1f1535c6f56b0c18d37e
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, RPixelOperation 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, RPixelOperation 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, RCoordinatesMode 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, RPixelOperation operation,
209 const RPoint *points, int npoints, RCoordinatesMode mode,
210 const RColor *color)
212 register int x, y, i;
214 assert(image != NULL);
215 assert(points != NULL);
217 x = y = 0;
219 for (i = 0; i < npoints; i++) {
220 if (mode == RAbsoluteCoordinates) {
221 x = points[i].x;
222 y = points[i].y;
223 } else {
224 x += points[i].x;
225 y += points[i].y;
227 ROperatePixel(image, operation, x, y, color);
231 static Bool clipLineInRectangle(int xmin, int ymin, int xmax, int ymax, int *x1, int *y1, int *x2, int *y2)
233 #define TOP (1<<0)
234 #define BOT (1<<1)
235 #define LEF (1<<2)
236 #define RIG (1<<3)
237 #define CHECK_OUT(X,Y) (((Y) > ymax ? TOP : ((Y) < ymin ? BOT : 0))\
238 | ((X) > xmax ? RIG : ((X) < xmin ? LEF : 0)))
240 int ocode1, ocode2, ocode;
241 int accept = 0;
242 int x, y;
244 ocode1 = CHECK_OUT(*x1, *y1);
245 ocode2 = CHECK_OUT(*x2, *y2);
247 for (;;) {
248 if (!ocode1 && !ocode2) { /* completely inside */
249 accept = 1;
250 break;
251 } else if (ocode1 & ocode2) {
252 break;
255 if (ocode1)
256 ocode = ocode1;
257 else
258 ocode = ocode2;
260 if (ocode & TOP) {
261 x = *x1 + (*x2 - *x1) * (ymax - *y1) / (*y2 - *y1);
262 y = ymax;
263 } else if (ocode & BOT) {
264 x = *x1 + (*x2 - *x1) * (ymin - *y1) / (*y2 - *y1);
265 y = ymin;
266 } else if (ocode & RIG) {
267 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
268 x = xmax;
269 } else { /* //if (ocode & LEF) { */
270 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
271 x = xmin;
274 if (ocode == ocode1) {
275 *x1 = x;
276 *y1 = y;
277 ocode1 = CHECK_OUT(x, y);
278 } else {
279 *x2 = x;
280 *y2 = y;
281 ocode2 = CHECK_OUT(x, y);
285 return accept;
289 * This routine is a generic drawing routine, based on Bresenham's line
290 * drawing algorithm.
292 static int genericLine(RImage *image, int x0, int y0, int x1, int y1, const RColor *color,
293 RPixelOperation operation, int polyline)
295 int i, err, du, dv, du2, dv2, uofs, vofs, last;
297 assert(image != NULL);
299 if (!clipLineInRectangle(0, 0, image->width - 1, image->height - 1, &x0, &y0, &x1, &y1))
300 return True;
302 if (x0 < x1) {
303 du = x1 - x0;
304 uofs = 1;
305 } else {
306 du = x0 - x1;
307 uofs = -1;
309 if (y0 < y1) {
310 dv = y1 - y0;
311 vofs = image->width;
312 } else {
313 dv = y0 - y1;
314 vofs = -image->width;
317 if (du < dv) {
318 /* Swap coordinates between them, so that always du>dv */
319 i = du;
320 du = dv;
321 dv = i;
322 i = uofs;
323 uofs = vofs;
324 vofs = i;
327 err = 0;
328 du2 = du << 1;
329 dv2 = dv << 1;
330 last = (polyline) ? du - 1 : du;
332 if (color->alpha == 255 || operation == RCopyOperation) {
333 unsigned char *ptr;
335 if (image->format == RRGBAFormat)
336 i = (y0 * image->width + x0) * 4;
337 else
338 i = (y0 * image->width + x0) * 3;
339 ptr = image->data + i;
341 for (i = 0; i <= last; i++) {
342 /* Draw the pixel */
343 *ptr = color->red;
344 *(ptr + 1) = color->green;
345 *(ptr + 2) = color->blue;
346 if (image->format == RRGBAFormat)
347 *(ptr + 3) = 255;
349 /* Compute error for NeXT Step */
350 err += dv2;
351 if (err >= du) {
352 if (image->format == RRGBAFormat)
353 ptr += vofs * 4;
354 else
355 ptr += vofs * 3;
356 err -= du2;
358 if (image->format == RRGBAFormat)
359 ptr += uofs * 4;
360 else
361 ptr += uofs * 3;
363 } else {
364 register int ofs = y0 * image->width + x0;
366 for (i = 0; i <= last; i++) {
367 /* Draw the pixel */
368 operatePixel(image, ofs, operation, color);
370 /* Compute error for NeXT Step */
371 err += dv2;
372 if (err >= du) {
373 ofs += vofs;
374 err -= du2;
376 ofs += uofs;
380 return True;
383 int RDrawLine(RImage * image, int x0, int y0, int x1, int y1, const RColor * color)
385 return genericLine(image, x0, y0, x1, y1, color, RNormalOperation, False);
388 int ROperateLine(RImage *image, RPixelOperation operation, int x0, int y0, int x1, int y1, const RColor *color)
390 return genericLine(image, x0, y0, x1, y1, color, operation, False);
393 void RDrawLines(RImage *image, const RPoint *points, int npoints, RCoordinatesMode mode, const RColor *color)
395 register int x1, y1, x2, y2, i;
397 assert(points != NULL);
399 if (npoints == 0)
400 return;
402 x1 = points[0].x;
403 y1 = points[0].y;
404 x2 = y2 = 0;
406 for (i = 1; i < npoints - 1; i++) {
407 if (mode == RAbsoluteCoordinates) {
408 x2 = points[i].x;
409 y2 = points[i].y;
410 } else {
411 x2 += points[i - 1].x;
412 y2 += points[i - 1].y;
414 /* Don't draw pixels at junction points twice */
415 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, True);
416 x1 = x2;
417 y1 = y2;
419 i = npoints - 1; /* last point */
420 if (mode == RAbsoluteCoordinates) {
421 x2 = points[i].x;
422 y2 = points[i].y;
423 } else {
424 x2 += points[i - 1].x;
425 y2 += points[i - 1].y;
427 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
428 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, i);
431 void ROperateLines(RImage *image, RPixelOperation operation,
432 const RPoint *points, int npoints, RCoordinatesMode mode,
433 const RColor *color)
435 register int x1, y1, x2, y2, i;
437 assert(points != NULL);
439 if (npoints == 0)
440 return;
442 x1 = points[0].x;
443 y1 = points[0].y;
444 x2 = y2 = 0;
446 for (i = 1; i < npoints - 1; i++) {
447 if (mode == RAbsoluteCoordinates) {
448 x2 = points[i].x;
449 y2 = points[i].y;
450 } else {
451 x2 += points[i - 1].x;
452 y2 += points[i - 1].y;
454 /* Don't draw pixels at junction points twice */
455 genericLine(image, x1, y1, x2, y2, color, operation, True);
456 x1 = x2;
457 y1 = y2;
459 i = npoints - 1; /* last point */
460 if (mode == RAbsoluteCoordinates) {
461 x2 = points[i].x;
462 y2 = points[i].y;
463 } else {
464 x2 += points[i - 1].x;
465 y2 += points[i - 1].y;
467 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
468 genericLine(image, x1, y1, x2, y2, color, operation, i);
471 void ROperateRectangle(RImage *image, RPixelOperation operation, int x0, int y0, int x1, int y1, const RColor *color)
473 int y;
475 for (y = y0; y <= y1; y++) {
476 genericLine(image, x0, y, x1, y, color, operation, False);
480 void RDrawSegments(RImage *image, const RSegment *segs, int nsegs, const RColor *color)
482 register int i;
484 assert(segs != NULL);
486 for (i = 0; i < nsegs; i++) {
487 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, RNormalOperation, False);
488 segs++;
492 void ROperateSegments(RImage *image, RPixelOperation operation, const RSegment *segs, int nsegs, const RColor *color)
494 register int i;
496 assert(segs != NULL);
498 for (i = 0; i < nsegs; i++) {
499 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, operation, False);
500 segs++;