Moved parameters of WPrefs's expert check-buttons to a single place
[wmaker-crm.git] / wrlib / draw.c
blob071f016ad0a5553f8af86cffe275011d500c2238
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;
146 *sa = alpha + ((int)*sa * nalpha) / 256;
148 break;
149 case RAddOperation:
150 tmp = color->red + *sr;
151 *sr = MIN(255, tmp);
152 tmp = color->green + *sg;
153 *sg = MIN(255, tmp);
154 tmp = color->blue + *sb;
155 *sb = MIN(255, tmp);
156 if (hasAlpha)
157 *sa = MIN(*sa, color->alpha);
158 break;
159 case RSubtractOperation:
160 tmp = *sr - color->red;
161 *sr = MAX(0, tmp);
162 tmp = *sg - color->green;
163 *sg = MAX(0, tmp);
164 tmp = *sb - color->blue;
165 *sb = MAX(0, tmp);
166 if (hasAlpha)
167 *sa = MIN(*sa, color->alpha);
168 break;
172 void ROperatePixel(RImage * image, int operation, int x, int y, RColor * color)
174 int ofs;
176 assert(image != NULL);
177 assert(color != NULL);
178 assert(x >= 0 && x < image->width);
179 assert(y >= 0 && y < image->height);
181 ofs = y * image->width + x;
183 operatePixel(image, ofs, operation, color);
186 void RPutPixels(RImage * image, RPoint * points, int npoints, int mode, RColor * color)
188 register int x, y, i;
190 assert(image != NULL);
191 assert(points != NULL);
193 x = y = 0;
195 for (i = 0; i < npoints; i++) {
196 if (mode == RAbsoluteCoordinates) {
197 x = points[i].x;
198 y = points[i].y;
199 } else {
200 x += points[i].x;
201 y += points[i].y;
203 RPutPixel(image, x, y, color);
207 void ROperatePixels(RImage * image, int operation, RPoint * points, int npoints, int mode, RColor * color)
209 register int x, y, i;
211 assert(image != NULL);
212 assert(points != NULL);
214 x = y = 0;
216 for (i = 0; i < npoints; i++) {
217 if (mode == RAbsoluteCoordinates) {
218 x = points[i].x;
219 y = points[i].y;
220 } else {
221 x += points[i].x;
222 y += points[i].y;
224 ROperatePixel(image, operation, x, y, color);
228 static Bool clipLineInRectangle(int xmin, int ymin, int xmax, int ymax, int *x1, int *y1, int *x2, int *y2)
230 #define TOP (1<<0)
231 #define BOT (1<<1)
232 #define LEF (1<<2)
233 #define RIG (1<<3)
234 #define CHECK_OUT(X,Y) (((Y) > ymax ? TOP : ((Y) < ymin ? BOT : 0))\
235 | ((X) > xmax ? RIG : ((X) < xmin ? LEF : 0)))
237 int ocode1, ocode2, ocode;
238 int accept = 0;
239 int x, y;
241 ocode1 = CHECK_OUT(*x1, *y1);
242 ocode2 = CHECK_OUT(*x2, *y2);
244 for (;;) {
245 if (!ocode1 && !ocode2) { /* completely inside */
246 accept = 1;
247 break;
248 } else if (ocode1 & ocode2) {
249 break;
252 if (ocode1)
253 ocode = ocode1;
254 else
255 ocode = ocode2;
257 if (ocode & TOP) {
258 x = *x1 + (*x2 - *x1) * (ymax - *y1) / (*y2 - *y1);
259 y = ymax;
260 } else if (ocode & BOT) {
261 x = *x1 + (*x2 - *x1) * (ymin - *y1) / (*y2 - *y1);
262 y = ymin;
263 } else if (ocode & RIG) {
264 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
265 x = xmax;
266 } else { /* //if (ocode & LEF) { */
267 y = *y1 + (*y2 - *y1) * (xmax - *x1) / (*x2 - *x1);
268 x = xmin;
271 if (ocode == ocode1) {
272 *x1 = x;
273 *y1 = y;
274 ocode1 = CHECK_OUT(x, y);
275 } else {
276 *x2 = x;
277 *y2 = y;
278 ocode2 = CHECK_OUT(x, y);
282 return accept;
286 * This routine is a generic drawing routine, based on Bresenham's line
287 * drawing algorithm.
289 static int genericLine(RImage * image, int x0, int y0, int x1, int y1, RColor * color, int operation, int polyline)
291 int i, err, du, dv, du2, dv2, uofs, vofs, last;
293 assert(image != NULL);
295 if (!clipLineInRectangle(0, 0, image->width - 1, image->height - 1, &x0, &y0, &x1, &y1))
296 return True;
298 if (x0 < x1) {
299 du = x1 - x0;
300 uofs = 1;
301 } else {
302 du = x0 - x1;
303 uofs = -1;
305 if (y0 < y1) {
306 dv = y1 - y0;
307 vofs = image->width;
308 } else {
309 dv = y0 - y1;
310 vofs = -image->width;
313 if (du < dv) {
314 /* Swap coordinates between them, so that always du>dv */
315 i = du;
316 du = dv;
317 dv = i;
318 i = uofs;
319 uofs = vofs;
320 vofs = i;
323 err = 0;
324 du2 = du << 1;
325 dv2 = dv << 1;
326 last = (polyline) ? du - 1 : du;
328 if (color->alpha == 255 || operation == RCopyOperation) {
329 unsigned char *ptr;
331 if (image->format == RRGBAFormat)
332 i = (y0 * image->width + x0) * 4;
333 else
334 i = (y0 * image->width + x0) * 3;
335 ptr = image->data + i;
337 for (i = 0; i <= last; i++) {
338 /* Draw the pixel */
339 *ptr = color->red;
340 *(ptr + 1) = color->green;
341 *(ptr + 2) = color->blue;
342 if (image->format == RRGBAFormat)
343 *(ptr + 3) = 255;
345 /* Compute error for NeXT Step */
346 err += dv2;
347 if (err >= du) {
348 if (image->format == RRGBAFormat)
349 ptr += vofs * 4;
350 else
351 ptr += vofs * 3;
352 err -= du2;
354 if (image->format == RRGBAFormat)
355 ptr += uofs * 4;
356 else
357 ptr += uofs * 3;
359 } else {
360 register int ofs = y0 * image->width + x0;
362 for (i = 0; i <= last; i++) {
363 /* Draw the pixel */
364 operatePixel(image, ofs, operation, color);
366 /* Compute error for NeXT Step */
367 err += dv2;
368 if (err >= du) {
369 ofs += vofs;
370 err -= du2;
372 ofs += uofs;
376 return True;
379 int RDrawLine(RImage * image, int x0, int y0, int x1, int y1, RColor * color)
381 return genericLine(image, x0, y0, x1, y1, color, RNormalOperation, False);
384 int ROperateLine(RImage * image, int operation, int x0, int y0, int x1, int y1, RColor * color)
386 return genericLine(image, x0, y0, x1, y1, color, operation, False);
389 void RDrawLines(RImage * image, RPoint * points, int npoints, int mode, RColor * color)
391 register int x1, y1, x2, y2, i;
393 assert(points != NULL);
395 if (npoints == 0)
396 return;
398 x1 = points[0].x;
399 y1 = points[0].y;
400 x2 = y2 = 0;
402 for (i = 1; i < npoints - 1; i++) {
403 if (mode == RAbsoluteCoordinates) {
404 x2 = points[i].x;
405 y2 = points[i].y;
406 } else {
407 x2 += points[i - 1].x;
408 y2 += points[i - 1].y;
410 /* Don't draw pixels at junction points twice */
411 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, True);
412 x1 = x2;
413 y1 = y2;
415 i = npoints - 1; /* last point */
416 if (mode == RAbsoluteCoordinates) {
417 x2 = points[i].x;
418 y2 = points[i].y;
419 } else {
420 x2 += points[i - 1].x;
421 y2 += points[i - 1].y;
423 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
424 genericLine(image, x1, y1, x2, y2, color, RNormalOperation, i);
427 void ROperateLines(RImage * image, int operation, RPoint * points, int npoints, int mode, RColor * color)
429 register int x1, y1, x2, y2, i;
431 assert(points != NULL);
433 if (npoints == 0)
434 return;
436 x1 = points[0].x;
437 y1 = points[0].y;
438 x2 = y2 = 0;
440 for (i = 1; i < npoints - 1; i++) {
441 if (mode == RAbsoluteCoordinates) {
442 x2 = points[i].x;
443 y2 = points[i].y;
444 } else {
445 x2 += points[i - 1].x;
446 y2 += points[i - 1].y;
448 /* Don't draw pixels at junction points twice */
449 genericLine(image, x1, y1, x2, y2, color, operation, True);
450 x1 = x2;
451 y1 = y2;
453 i = npoints - 1; /* last point */
454 if (mode == RAbsoluteCoordinates) {
455 x2 = points[i].x;
456 y2 = points[i].y;
457 } else {
458 x2 += points[i - 1].x;
459 y2 += points[i - 1].y;
461 i = (points[0].x == x2 && points[0].y == y2 && npoints > 1);
462 genericLine(image, x1, y1, x2, y2, color, operation, i);
465 void RDrawSegments(RImage * image, RSegment * segs, int nsegs, RColor * color)
467 register int i;
469 assert(segs != NULL);
471 for (i = 0; i < nsegs; i++) {
472 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, RNormalOperation, False);
473 segs++;
477 void ROperateSegments(RImage * image, int operation, RSegment * segs, int nsegs, RColor * color)
479 register int i;
481 assert(segs != NULL);
483 for (i = 0; i < nsegs; i++) {
484 genericLine(image, segs->x1, segs->y1, segs->x2, segs->y2, color, operation, False);
485 segs++;