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,
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
)
42 assert(image
!= NULL
);
43 if (x
< 0 || x
>= image
->width
|| y
< 0 || y
>= image
->height
)
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
];
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 */
64 void RPutPixel(RImage
*image
, int x
, int y
, const RColor
*color
)
68 assert(image
!= NULL
);
69 assert(color
!= NULL
);
70 if (x
< 0 || x
>= image
->width
|| y
< 0 || y
>= image
->height
)
73 if (image
->format
== RRGBAFormat
) {
74 ptr
= image
->data
+ (y
* image
->width
+ x
) * 4;
76 ptr
= image
->data
+ (y
* image
->width
+ x
) * 3;
79 if (color
->alpha
== 255) {
81 *ptr
++ = color
->green
;
83 if (image
->format
== RRGBAFormat
) {
87 register int alpha
, nalpha
, r
, g
, b
;
95 *ptr
= (((int)*ptr
* nalpha
) + (r
* alpha
)) / 256;
97 *ptr
= (((int)*ptr
* nalpha
) + (g
* alpha
)) / 256;
99 *ptr
= (((int)*ptr
* nalpha
) + (b
* alpha
)) / 256;
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;
122 case RClearOperation
:
136 case RNormalOperation
:
137 if (color
->alpha
== 255) {
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;
151 tmp
= color
->red
+ *sr
;
153 tmp
= color
->green
+ *sg
;
155 tmp
= color
->blue
+ *sb
;
158 *sa
= MIN(*sa
, color
->alpha
);
160 case RSubtractOperation
:
161 tmp
= *sr
- color
->red
;
163 tmp
= *sg
- color
->green
;
165 tmp
= *sb
- color
->blue
;
168 *sa
= MIN(*sa
, color
->alpha
);
173 void ROperatePixel(RImage
* image
, int operation
, int x
, int y
, const RColor
* color
)
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
);
196 for (i
= 0; i
< npoints
; i
++) {
197 if (mode
== RAbsoluteCoordinates
) {
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
);
217 for (i
= 0; i
< npoints
; i
++) {
218 if (mode
== RAbsoluteCoordinates
) {
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
)
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
;
242 ocode1
= CHECK_OUT(*x1
, *y1
);
243 ocode2
= CHECK_OUT(*x2
, *y2
);
246 if (!ocode1
&& !ocode2
) { /* completely inside */
249 } else if (ocode1
& ocode2
) {
259 x
= *x1
+ (*x2
- *x1
) * (ymax
- *y1
) / (*y2
- *y1
);
261 } else if (ocode
& BOT
) {
262 x
= *x1
+ (*x2
- *x1
) * (ymin
- *y1
) / (*y2
- *y1
);
264 } else if (ocode
& RIG
) {
265 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
267 } else { /* //if (ocode & LEF) { */
268 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
272 if (ocode
== ocode1
) {
275 ocode1
= CHECK_OUT(x
, y
);
279 ocode2
= CHECK_OUT(x
, y
);
287 * This routine is a generic drawing routine, based on Bresenham's line
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
))
311 vofs
= -image
->width
;
315 /* Swap coordinates between them, so that always du>dv */
327 last
= (polyline
) ? du
- 1 : du
;
329 if (color
->alpha
== 255 || operation
== RCopyOperation
) {
332 if (image
->format
== RRGBAFormat
)
333 i
= (y0
* image
->width
+ x0
) * 4;
335 i
= (y0
* image
->width
+ x0
) * 3;
336 ptr
= image
->data
+ i
;
338 for (i
= 0; i
<= last
; i
++) {
341 *(ptr
+ 1) = color
->green
;
342 *(ptr
+ 2) = color
->blue
;
343 if (image
->format
== RRGBAFormat
)
346 /* Compute error for NeXT Step */
349 if (image
->format
== RRGBAFormat
)
355 if (image
->format
== RRGBAFormat
)
361 register int ofs
= y0
* image
->width
+ x0
;
363 for (i
= 0; i
<= last
; i
++) {
365 operatePixel(image
, ofs
, operation
, color
);
367 /* Compute error for NeXT Step */
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
);
403 for (i
= 1; i
< npoints
- 1; i
++) {
404 if (mode
== RAbsoluteCoordinates
) {
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
);
416 i
= npoints
- 1; /* last point */
417 if (mode
== RAbsoluteCoordinates
) {
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
);
441 for (i
= 1; i
< npoints
- 1; i
++) {
442 if (mode
== RAbsoluteCoordinates
) {
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
);
454 i
= npoints
- 1; /* last point */
455 if (mode
== RAbsoluteCoordinates
) {
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
)
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
);
478 void ROperateSegments(RImage
* image
, int operation
, const RSegment
* segs
, int nsegs
, const RColor
* color
)
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
);