Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / draw.c
blobd34a8b52f22b2a036a41bab357d38f098bdde543
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
29 #include "wraster.h"
31 #define MIN(a,b) ((a) < (b) ? (a) : (b))
32 #define MAX(a,b) ((a) > (b) ? (a) : (b))
35 * Returns the color of the pixel at coordinates (x, y) in "color".
37 Bool RGetPixel(RImage * image, int x, int y, RColor * color)
39 int ofs;
41 assert(image != NULL);
42 if (x < 0 || x >= image->width || y < 0 || y >= image->height)
43 return False;
45 if (image->format == RRGBAFormat) {
46 ofs = (y * image->width + x) * 4;
47 color->red = image->data[ofs++];
48 color->green = image->data[ofs++];
49 color->blue = image->data[ofs++];
50 color->alpha = image->data[ofs];
51 } else {
52 ofs = (y * image->width + x) * 3;
53 color->red = image->data[ofs++];
54 color->green = image->data[ofs++];
55 color->blue = image->data[ofs];
56 /* If the image does not have alpha channel, we consider alpha 255 */
57 color->alpha = 255;
60 return True;
63 void RPutPixel(RImage * image, int x, int y, RColor * color)
65 unsigned char *ptr;
67 assert(image != NULL);
68 assert(color != NULL);
69 if (x < 0 || x >= image->width || y < 0 || y >= image->height)
70 return;
72 if (image->format == RRGBAFormat) {
73 ptr = image->data + (y * image->width + x) * 4;
74 } else {
75 ptr = image->data + (y * image->width + x) * 3;
78 if (color->alpha == 255) {
79 *ptr++ = color->red;
80 *ptr++ = color->green;
81 *ptr++ = color->blue;
82 if (image->format == RRGBAFormat) {
83 *ptr = 255;
85 } else {
86 register int alpha, nalpha, r, g, b;
88 r = color->red;
89 g = color->green;
90 b = color->blue;
91 alpha = color->alpha;
92 nalpha = 255 - alpha;
94 *ptr = (((int)*ptr * nalpha) + (r * alpha)) / 256;
95 ptr++;
96 *ptr = (((int)*ptr * nalpha) + (g * alpha)) / 256;
97 ptr++;
98 *ptr = (((int)*ptr * nalpha) + (b * alpha)) / 256;
99 ptr++;
100 if (image->format == RRGBAFormat) {
101 *ptr = alpha + ((int)*ptr * nalpha) / 256;
106 static void operatePixel(RImage * image, int ofs, int operation, RColor * color)
108 unsigned char *sr, *sg, *sb, *sa;
109 register int alpha, nalpha, tmp;
110 int hasAlpha = image->format == RRGBAFormat;
112 alpha = color->alpha;
113 nalpha = 255 - alpha;
115 sr = image->data + ofs * (hasAlpha ? 4 : 3);
116 sg = image->data + ofs * (hasAlpha ? 4 : 3) + 1;
117 sb = image->data + ofs * (hasAlpha ? 4 : 3) + 2;
118 sa = image->data + ofs * (hasAlpha ? 4 : 3) + 3;
120 switch (operation) {
121 case RClearOperation:
122 *sr = 0;
123 *sg = 0;
124 *sb = 0;
125 if (hasAlpha)
126 *sa = 0;
127 break;
128 case RCopyOperation:
129 *sr = color->red;
130 *sg = color->green;
131 *sb = color->blue;
132 if (hasAlpha)
133 *sa = color->alpha;
134 break;
135 case RNormalOperation:
136 if (color->alpha == 255) {
137 *sr = color->red;
138 *sg = color->green;
139 *sb = color->blue;
140 if (hasAlpha)
141 *sa = 255;
142 } else {
143 *sr = (((int)*sr * nalpha) + ((int)color->red * alpha)) / 256;
144 *sg = (((int)*sg * nalpha) + ((int)color->green * alpha)) / 256;
145 *sb = (((int)*sb * nalpha) + ((int)color->blue * alpha)) / 256;
147 break;
148 case RAddOperation:
149 tmp = color->red + *sr;
150 *sr = MIN(255, tmp);
151 tmp = color->green + *sg;
152 *sg = MIN(255, tmp);
153 tmp = color->blue + *sb;
154 *sb = MIN(255, tmp);
155 if (hasAlpha)
156 *sa = MIN(*sa, color->alpha);
157 break;
158 case RSubtractOperation:
159 tmp = *sr - color->red;
160 *sr = MAX(0, tmp);
161 tmp = *sg - color->green;
162 *sg = MAX(0, tmp);
163 tmp = *sb - color->blue;
164 *sb = MAX(0, tmp);
165 if (hasAlpha)
166 *sa = MIN(*sa, color->alpha);
167 break;
171 void ROperatePixel(RImage * image, int operation, int x, int y, RColor * color)
173 int ofs;
175 assert(image != NULL);
176 assert(color != NULL);
177 assert(x >= 0 && x < image->width);
178 assert(y >= 0 && y < image->height);
180 ofs = y * image->width + x;
182 operatePixel(image, ofs, operation, color);
185 void RPutPixels(RImage * image, RPoint * points, int npoints, int mode, RColor * color)
187 register int x, y, i;
189 assert(image != NULL);
190 assert(points != NULL);
192 x = y = 0;
194 for (i = 0; i < npoints; i++) {
195 if (mode == RAbsoluteCoordinates) {
196 x = points[i].x;
197 y = points[i].y;
198 } else {
199 x += points[i].x;
200 y += points[i].y;
202 RPutPixel(image, x, y, color);
206 void ROperatePixels(RImage * image, int operation, RPoint * points, int npoints, int mode, RColor * color)
208 register int x, y, i;
210 assert(image != NULL);
211 assert(points != NULL);
213 x = y = 0;
215 for (i = 0; i < npoints; i++) {
216 if (mode == RAbsoluteCoordinates) {
217 x = points[i].x;
218 y = points[i].y;
219 } else {
220 x += points[i].x;
221 y += points[i].y;
223 ROperatePixel(image, operation, x, y, color);
227 static Bool clipLineInRectangle(int xmin, int ymin, int xmax, int ymax, int *x1, int *y1, int *x2, int *y2)
229 #define TOP (1<<0)
230 #define BOT (1<<1)
231 #define LEF (1<<2)
232 #define RIG (1<<3)
233 #define CHECK_OUT(X,Y) (((Y) > ymax ? TOP : ((Y) < ymin ? BOT : 0))\
234 | ((X) > xmax ? RIG : ((X) < xmin ? LEF : 0)))
236 int ocode1, ocode2, ocode;
237 int accept = 0;
238 int x, y;
240 ocode1 = CHECK_OUT(*x1, *y1);
241 ocode2 = CHECK_OUT(*x2, *y2);
243 for (;;) {
244 if (!ocode1 && !ocode2) { /* completely inside */
245 accept = 1;
246 break;
247 } else if (ocode1 & ocode2) {
248 break;
251 if (ocode1)
252 ocode = ocode1;
253 else
254 ocode = ocode2;
256 if (ocode & TOP) {
257 x = *x1 + (*x2 - *x1) * (ymax - *y1) / (*y2 - *y1);
258 y = ymax;
259 } else if (ocode & BOT) {
260 x = *x1 + (*x2 - *x1) * (ymin - *y1) / (*y2 - *y1);
261 y = ymin;
262 } else if (ocode & RIG) {
263 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
264 x = xmax;
265 } else { /* //if (ocode & LEF) { */
266 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
267 x = xmin;
270 if (ocode == ocode1) {
271 *x1 = x;
272 *y1 = y;
273 ocode1 = CHECK_OUT(x, y);
274 } else {
275 *x2 = x;
276 *y2 = y;
277 ocode2 = CHECK_OUT(x, y);
281 return accept;
285 * This routine is a generic drawing routine, based on Bresenham's line
286 * drawing algorithm.
288 static int genericLine(RImage * image, int x0, int y0, int x1, int y1, RColor * color, int operation, int polyline)
290 int i, err, du, dv, du2, dv2, uofs, vofs, last;
292 assert(image != NULL);
294 if (!clipLineInRectangle(0, 0, image->width - 1, image->height - 1, &x0, &y0, &x1, &y1))
295 return True;
297 if (x0 < x1) {
298 du = x1 - x0;
299 uofs = 1;
300 } else {
301 du = x0 - x1;
302 uofs = -1;
304 if (y0 < y1) {
305 dv = y1 - y0;
306 vofs = image->width;
307 } else {
308 dv = y0 - y1;
309 vofs = -image->width;
312 if (du < dv) {
313 /* Swap coordinates between them, so that always du>dv */
314 i = du;
315 du = dv;
316 dv = i;
317 i = uofs;
318 uofs = vofs;
319 vofs = i;
322 err = 0;
323 du2 = du << 1;
324 dv2 = dv << 1;
325 last = (polyline) ? du - 1 : du;
327 if (color->alpha == 255 || operation == RCopyOperation) {
328 unsigned char *ptr;
330 if (image->format == RRGBAFormat)
331 i = (y0 * image->width + x0) * 4;
332 else
333 i = (y0 * image->width + x0) * 3;
334 ptr = image->data + i;
336 for (i = 0; i <= last; i++) {
337 /* Draw the pixel */
338 *ptr = color->red;
339 *(ptr + 1) = color->green;
340 *(ptr + 2) = color->blue;
341 if (image->format == RRGBAFormat)
342 *(ptr + 3) = 255;
344 /* Compute error for NeXT Step */
345 err += dv2;
346 if (err >= du) {
347 if (image->format == RRGBAFormat)
348 ptr += vofs * 4;
349 else
350 ptr += vofs * 3;
351 err -= du2;
353 if (image->format == RRGBAFormat)
354 ptr += uofs * 4;
355 else
356 ptr += uofs * 3;
358 } else {
359 register int ofs = y0 * image->width + x0;
361 for (i = 0; i <= last; i++) {
362 /* Draw the pixel */
363 operatePixel(image, ofs, operation, color);
365 /* Compute error for NeXT Step */
366 err += dv2;
367 if (err >= du) {
368 ofs += vofs;
369 err -= du2;
371 ofs += uofs;
375 #if 0
376 if (mode == RALTER_PIXELS) {
377 RColorOffset *cdelta = (RColorOffset *) cdata;
378 register short r, g, b, a;
380 for (i = 0; i <= last; i++) {
381 /* Change the pixel with offset */
382 r = (short)*sr + cdelta->red;
383 g = (short)*sg + cdelta->green;
384 b = (short)*sb + cdelta->blue;
385 if (r > 255)
386 r = 255;
387 else if (r < 0)
388 r = 0;
389 if (g > 255)
390 g = 255;
391 else if (g < 0)
392 g = 0;
393 if (b > 255)
394 b = 255;
395 else if (b < 0)
396 b = 0;
397 *sr = (unsigned char)r;
398 *sg = (unsigned char)g;
399 *sb = (unsigned char)b;
400 if (image->data[3]) {
401 a = (short)*sa + cdelta->alpha;
402 if (a > 255)
403 a = 255;
404 else if (a < 0)
405 a = 0;
406 *sa = (unsigned char)a;
409 /* Compute error for NeXT Step */
410 err += dv2;
411 if (err >= du) {
412 sr += vofs;
413 sg += vofs;
414 sb += vofs;
415 sa += vofs;
416 err -= du2;
418 sr += uofs;
419 sg += uofs;
420 sb += uofs;
421 sa += uofs;
423 } else {
424 RColor *color = (RColor *) cdata;
426 if (color->alpha == 255) {
427 for (i = 0; i <= last; i++) {
428 /* Draw the pixel */
429 *sr = color->red;
430 *sg = color->green;
431 *sb = color->blue;
432 if (image->data[3])
433 *sa = 255;
435 /* Compute error for NeXT Step */
436 err += dv2;
437 if (err >= du) {
438 sr += vofs;
439 sg += vofs;
440 sb += vofs;
441 sa += vofs;
442 err -= du2;
444 sr += uofs;
445 sg += uofs;
446 sb += uofs;
447 sa += uofs;
449 } else {
450 register short alpha, nalpha, r, g, b;
452 alpha = color->alpha;
453 nalpha = 255 - alpha;
454 r = color->red;
455 g = color->green;
456 b = color->blue;
458 for (i = 0; i <= last; i++) {
459 /* Draw the pixel */
460 *sr = (((int)*sr * nalpha) + (r * alpha)) / 256;
461 *sg = (((int)*sg * nalpha) + (g * alpha)) / 256;
462 *sb = (((int)*sb * nalpha) + (b * alpha)) / 256;
463 if (image->data[3])
464 *sa = alpha + ((int)*sa * nalpha) / 256;
466 /* Compute error for NeXT Step */
467 err += dv2;
468 if (err >= du) {
469 sr += vofs;
470 sg += vofs;
471 sb += vofs;
472 sa += vofs;
473 err -= du2;
475 sr += uofs;
476 sg += uofs;
477 sb += uofs;
478 sa += uofs;
482 #endif
483 return True;
486 int RDrawLine(RImage * image, int x0, int y0, int x1, int y1, RColor * color)
488 return genericLine(image, x0, y0, x1, y1, color, RNormalOperation, False);
491 int ROperateLine(RImage * image, int operation, int x0, int y0, int x1, int y1, RColor * color)
493 return genericLine(image, x0, y0, x1, y1, color, operation, False);
496 void RDrawLines(RImage * image, RPoint * points, int npoints, int mode, RColor * color)
498 register int x1, y1, x2, y2, i;
500 assert(points != NULL);
502 if (npoints == 0)
503 return;
505 x1 = points[0].x;
506 y1 = points[0].y;
507 x2 = y2 = 0;
509 for (i = 1; i < npoints - 1; i++) {
510 if (mode == RAbsoluteCoordinates) {
511 x2 = points[i].x;
512 y2 = points[i].y;
513 } else {
514 x2 += points[i - 1].x;
515 y2 += points[i - 1].y;
517 /* Don't draw pixels at junction points twice */
518 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, True);
519 x1 = x2;
520 y1 = y2;
522 i = npoints - 1; /* last point */
523 if (mode == RAbsoluteCoordinates) {
524 x2 = points[i].x;
525 y2 = points[i].y;
526 } else {
527 x2 += points[i - 1].x;
528 y2 += points[i - 1].y;
530 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
531 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, i);
534 void ROperateLines(RImage * image, int operation, RPoint * points, int npoints, int mode, RColor * color)
536 register int x1, y1, x2, y2, i;
538 assert(points != NULL);
540 if (npoints == 0)
541 return;
543 x1 = points[0].x;
544 y1 = points[0].y;
545 x2 = y2 = 0;
547 for (i = 1; i < npoints - 1; i++) {
548 if (mode == RAbsoluteCoordinates) {
549 x2 = points[i].x;
550 y2 = points[i].y;
551 } else {
552 x2 += points[i - 1].x;
553 y2 += points[i - 1].y;
555 /* Don't draw pixels at junction points twice */
556 genericLine(image, x1, y1, x2, y2, color, operation, True);
557 x1 = x2;
558 y1 = y2;
560 i = npoints - 1; /* last point */
561 if (mode == RAbsoluteCoordinates) {
562 x2 = points[i].x;
563 y2 = points[i].y;
564 } else {
565 x2 += points[i - 1].x;
566 y2 += points[i - 1].y;
568 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
569 genericLine(image, x1, y1, x2, y2, color, operation, i);
572 void RDrawSegments(RImage * image, RSegment * segs, int nsegs, RColor * color)
574 register int i;
576 assert(segs != NULL);
578 for (i = 0; i < nsegs; i++) {
579 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, RNormalOperation, False);
580 segs++;
584 void ROperateSegments(RImage * image, int operation, RSegment * segs, int nsegs, RColor * color)
586 register int i;
588 assert(segs != NULL);
590 for (i = 0; i < nsegs; i++) {
591 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, operation, False);
592 segs++;