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.
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
)
41 assert(image
!= NULL
);
42 if (x
< 0 || x
>= image
->width
|| y
< 0 || y
>= image
->height
)
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
];
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 */
63 void RPutPixel(RImage
* image
, int x
, int y
, RColor
* color
)
67 assert(image
!= NULL
);
68 assert(color
!= NULL
);
69 if (x
< 0 || x
>= image
->width
|| y
< 0 || y
>= image
->height
)
72 if (image
->format
== RRGBAFormat
) {
73 ptr
= image
->data
+ (y
* image
->width
+ x
) * 4;
75 ptr
= image
->data
+ (y
* image
->width
+ x
) * 3;
78 if (color
->alpha
== 255) {
80 *ptr
++ = color
->green
;
82 if (image
->format
== RRGBAFormat
) {
86 register int alpha
, nalpha
, r
, g
, b
;
94 *ptr
= (((int)*ptr
* nalpha
) + (r
* alpha
)) / 256;
96 *ptr
= (((int)*ptr
* nalpha
) + (g
* alpha
)) / 256;
98 *ptr
= (((int)*ptr
* nalpha
) + (b
* alpha
)) / 256;
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;
121 case RClearOperation
:
135 case RNormalOperation
:
136 if (color
->alpha
== 255) {
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;
150 tmp
= color
->red
+ *sr
;
152 tmp
= color
->green
+ *sg
;
154 tmp
= color
->blue
+ *sb
;
157 *sa
= MIN(*sa
, color
->alpha
);
159 case RSubtractOperation
:
160 tmp
= *sr
- color
->red
;
162 tmp
= *sg
- color
->green
;
164 tmp
= *sb
- color
->blue
;
167 *sa
= MIN(*sa
, color
->alpha
);
172 void ROperatePixel(RImage
* image
, int operation
, int x
, int y
, RColor
* color
)
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
);
195 for (i
= 0; i
< npoints
; i
++) {
196 if (mode
== RAbsoluteCoordinates
) {
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
);
216 for (i
= 0; i
< npoints
; i
++) {
217 if (mode
== RAbsoluteCoordinates
) {
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
)
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
;
241 ocode1
= CHECK_OUT(*x1
, *y1
);
242 ocode2
= CHECK_OUT(*x2
, *y2
);
245 if (!ocode1
&& !ocode2
) { /* completely inside */
248 } else if (ocode1
& ocode2
) {
258 x
= *x1
+ (*x2
- *x1
) * (ymax
- *y1
) / (*y2
- *y1
);
260 } else if (ocode
& BOT
) {
261 x
= *x1
+ (*x2
- *x1
) * (ymin
- *y1
) / (*y2
- *y1
);
263 } else if (ocode
& RIG
) {
264 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
266 } else { /* //if (ocode & LEF) { */
267 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
271 if (ocode
== ocode1
) {
274 ocode1
= CHECK_OUT(x
, y
);
278 ocode2
= CHECK_OUT(x
, y
);
286 * This routine is a generic drawing routine, based on Bresenham's line
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
))
310 vofs
= -image
->width
;
314 /* Swap coordinates between them, so that always du>dv */
326 last
= (polyline
) ? du
- 1 : du
;
328 if (color
->alpha
== 255 || operation
== RCopyOperation
) {
331 if (image
->format
== RRGBAFormat
)
332 i
= (y0
* image
->width
+ x0
) * 4;
334 i
= (y0
* image
->width
+ x0
) * 3;
335 ptr
= image
->data
+ i
;
337 for (i
= 0; i
<= last
; i
++) {
340 *(ptr
+ 1) = color
->green
;
341 *(ptr
+ 2) = color
->blue
;
342 if (image
->format
== RRGBAFormat
)
345 /* Compute error for NeXT Step */
348 if (image
->format
== RRGBAFormat
)
354 if (image
->format
== RRGBAFormat
)
360 register int ofs
= y0
* image
->width
+ x0
;
362 for (i
= 0; i
<= last
; i
++) {
364 operatePixel(image
, ofs
, operation
, color
);
366 /* Compute error for NeXT Step */
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
);
402 for (i
= 1; i
< npoints
- 1; i
++) {
403 if (mode
== RAbsoluteCoordinates
) {
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
);
415 i
= npoints
- 1; /* last point */
416 if (mode
== RAbsoluteCoordinates
) {
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
);
440 for (i
= 1; i
< npoints
- 1; i
++) {
441 if (mode
== RAbsoluteCoordinates
) {
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
);
453 i
= npoints
- 1; /* last point */
454 if (mode
== RAbsoluteCoordinates
) {
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
)
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
);
477 void ROperateSegments(RImage
* image
, int operation
, RSegment
* segs
, int nsegs
, RColor
* color
)
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
);