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,
34 #define MIN(a,b) ((a) < (b) ? (a) : (b))
35 #define MAX(a,b) ((a) > (b) ? (a) : (b))
38 * Returns the color of the pixel at coordinates (x, y) in "color".
40 Bool
RGetPixel(RImage
* image
, int x
, int y
, RColor
* color
)
44 assert(image
!= NULL
);
45 if (x
< 0 || x
>= image
->width
|| y
< 0 || y
>= image
->height
)
48 if (image
->format
== RRGBAFormat
) {
49 ofs
= (y
* image
->width
+ x
) * 4;
50 color
->red
= image
->data
[ofs
++];
51 color
->green
= image
->data
[ofs
++];
52 color
->blue
= image
->data
[ofs
++];
53 color
->alpha
= image
->data
[ofs
];
55 ofs
= (y
* image
->width
+ x
) * 3;
56 color
->red
= image
->data
[ofs
++];
57 color
->green
= image
->data
[ofs
++];
58 color
->blue
= image
->data
[ofs
];
59 /* If the image does not have alpha channel, we consider alpha 255 */
66 void RPutPixel(RImage
*image
, int x
, int y
, const RColor
*color
)
70 assert(image
!= NULL
);
71 assert(color
!= NULL
);
72 if (x
< 0 || x
>= image
->width
|| y
< 0 || y
>= image
->height
)
75 if (image
->format
== RRGBAFormat
) {
76 ptr
= image
->data
+ (y
* image
->width
+ x
) * 4;
78 ptr
= image
->data
+ (y
* image
->width
+ x
) * 3;
81 if (color
->alpha
== 255) {
83 *ptr
++ = color
->green
;
85 if (image
->format
== RRGBAFormat
) {
89 register int alpha
, nalpha
, r
, g
, b
;
97 *ptr
= (((int)*ptr
* nalpha
) + (r
* alpha
)) / 256;
99 *ptr
= (((int)*ptr
* nalpha
) + (g
* alpha
)) / 256;
101 *ptr
= (((int)*ptr
* nalpha
) + (b
* alpha
)) / 256;
103 if (image
->format
== RRGBAFormat
) {
104 *ptr
= alpha
+ ((int)*ptr
* nalpha
) / 256;
109 static void operatePixel(RImage
*image
, int ofs
, RPixelOperation operation
, const RColor
*color
)
111 unsigned char *sr
, *sg
, *sb
, *sa
;
112 register int alpha
, nalpha
, tmp
;
113 int hasAlpha
= image
->format
== RRGBAFormat
;
115 alpha
= color
->alpha
;
116 nalpha
= 255 - alpha
;
118 sr
= image
->data
+ ofs
* (hasAlpha
? 4 : 3);
119 sg
= image
->data
+ ofs
* (hasAlpha
? 4 : 3) + 1;
120 sb
= image
->data
+ ofs
* (hasAlpha
? 4 : 3) + 2;
121 sa
= image
->data
+ ofs
* (hasAlpha
? 4 : 3) + 3;
124 case RClearOperation
:
138 case RNormalOperation
:
139 if (color
->alpha
== 255) {
146 *sr
= (((int)*sr
* nalpha
) + ((int)color
->red
* alpha
)) / 256;
147 *sg
= (((int)*sg
* nalpha
) + ((int)color
->green
* alpha
)) / 256;
148 *sb
= (((int)*sb
* nalpha
) + ((int)color
->blue
* alpha
)) / 256;
149 *sa
= alpha
+ ((int)*sa
* nalpha
) / 256;
153 tmp
= color
->red
+ *sr
;
155 tmp
= color
->green
+ *sg
;
157 tmp
= color
->blue
+ *sb
;
160 *sa
= MIN(*sa
, color
->alpha
);
162 case RSubtractOperation
:
163 tmp
= *sr
- color
->red
;
165 tmp
= *sg
- color
->green
;
167 tmp
= *sb
- color
->blue
;
170 *sa
= MIN(*sa
, color
->alpha
);
175 void ROperatePixel(RImage
*image
, RPixelOperation operation
, int x
, int y
, const RColor
*color
)
179 assert(image
!= NULL
);
180 assert(color
!= NULL
);
181 assert(x
>= 0 && x
< image
->width
);
182 assert(y
>= 0 && y
< image
->height
);
184 ofs
= y
* image
->width
+ x
;
186 operatePixel(image
, ofs
, operation
, color
);
189 void RPutPixels(RImage
*image
, const RPoint
*points
, int npoints
, RCoordinatesMode mode
, const RColor
*color
)
191 register int x
, y
, i
;
193 assert(image
!= NULL
);
194 assert(points
!= NULL
);
198 for (i
= 0; i
< npoints
; i
++) {
199 if (mode
== RAbsoluteCoordinates
) {
206 RPutPixel(image
, x
, y
, color
);
210 void ROperatePixels(RImage
*image
, RPixelOperation operation
,
211 const RPoint
*points
, int npoints
, RCoordinatesMode mode
,
214 register int x
, y
, i
;
216 assert(image
!= NULL
);
217 assert(points
!= NULL
);
221 for (i
= 0; i
< npoints
; i
++) {
222 if (mode
== RAbsoluteCoordinates
) {
229 ROperatePixel(image
, operation
, x
, y
, color
);
233 static Bool
clipLineInRectangle(int xmin
, int ymin
, int xmax
, int ymax
, int *x1
, int *y1
, int *x2
, int *y2
)
239 #define CHECK_OUT(X,Y) (((Y) > ymax ? TOP : ((Y) < ymin ? BOT : 0))\
240 | ((X) > xmax ? RIG : ((X) < xmin ? LEF : 0)))
242 int ocode1
, ocode2
, ocode
;
246 ocode1
= CHECK_OUT(*x1
, *y1
);
247 ocode2
= CHECK_OUT(*x2
, *y2
);
250 if (!ocode1
&& !ocode2
) { /* completely inside */
253 } else if (ocode1
& ocode2
) {
263 x
= *x1
+ (*x2
- *x1
) * (ymax
- *y1
) / (*y2
- *y1
);
265 } else if (ocode
& BOT
) {
266 x
= *x1
+ (*x2
- *x1
) * (ymin
- *y1
) / (*y2
- *y1
);
268 } else if (ocode
& RIG
) {
269 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
271 } else { /* //if (ocode & LEF) { */
272 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
276 if (ocode
== ocode1
) {
279 ocode1
= CHECK_OUT(x
, y
);
283 ocode2
= CHECK_OUT(x
, y
);
291 * This routine is a generic drawing routine, based on Bresenham's line
294 static int genericLine(RImage
*image
, int x0
, int y0
, int x1
, int y1
, const RColor
*color
,
295 RPixelOperation operation
, int polyline
)
297 int i
, err
, du
, dv
, du2
, dv2
, uofs
, vofs
, last
;
299 assert(image
!= NULL
);
301 if (!clipLineInRectangle(0, 0, image
->width
- 1, image
->height
- 1, &x0
, &y0
, &x1
, &y1
))
316 vofs
= -image
->width
;
320 /* Swap coordinates between them, so that always du>dv */
332 last
= (polyline
) ? du
- 1 : du
;
334 if (color
->alpha
== 255 || operation
== RCopyOperation
) {
337 if (image
->format
== RRGBAFormat
)
338 i
= (y0
* image
->width
+ x0
) * 4;
340 i
= (y0
* image
->width
+ x0
) * 3;
341 ptr
= image
->data
+ i
;
343 for (i
= 0; i
<= last
; i
++) {
346 *(ptr
+ 1) = color
->green
;
347 *(ptr
+ 2) = color
->blue
;
348 if (image
->format
== RRGBAFormat
)
351 /* Compute error for NeXT Step */
354 if (image
->format
== RRGBAFormat
)
360 if (image
->format
== RRGBAFormat
)
366 register int ofs
= y0
* image
->width
+ x0
;
368 for (i
= 0; i
<= last
; i
++) {
370 operatePixel(image
, ofs
, operation
, color
);
372 /* Compute error for NeXT Step */
385 int RDrawLine(RImage
* image
, int x0
, int y0
, int x1
, int y1
, const RColor
* color
)
387 return genericLine(image
, x0
, y0
, x1
, y1
, color
, RNormalOperation
, False
);
390 int ROperateLine(RImage
*image
, RPixelOperation operation
, int x0
, int y0
, int x1
, int y1
, const RColor
*color
)
392 return genericLine(image
, x0
, y0
, x1
, y1
, color
, operation
, False
);
395 void RDrawLines(RImage
*image
, const RPoint
*points
, int npoints
, RCoordinatesMode mode
, const RColor
*color
)
397 register int x1
, y1
, x2
, y2
, i
;
399 assert(points
!= NULL
);
408 for (i
= 1; i
< npoints
- 1; i
++) {
409 if (mode
== RAbsoluteCoordinates
) {
413 x2
+= points
[i
- 1].x
;
414 y2
+= points
[i
- 1].y
;
416 /* Don't draw pixels at junction points twice */
417 genericLine(image
, x1
, y1
, x2
, y2
, color
, RNormalOperation
, True
);
421 i
= npoints
- 1; /* last point */
422 if (mode
== RAbsoluteCoordinates
) {
426 x2
+= points
[i
- 1].x
;
427 y2
+= points
[i
- 1].y
;
429 i
= (points
[0].x
== x2
&& points
[0].y
== y2
&& npoints
> 1);
430 genericLine(image
, x1
, y1
, x2
, y2
, color
, RNormalOperation
, i
);
433 void ROperateLines(RImage
*image
, RPixelOperation operation
,
434 const RPoint
*points
, int npoints
, RCoordinatesMode mode
,
437 register int x1
, y1
, x2
, y2
, i
;
439 assert(points
!= NULL
);
448 for (i
= 1; i
< npoints
- 1; i
++) {
449 if (mode
== RAbsoluteCoordinates
) {
453 x2
+= points
[i
- 1].x
;
454 y2
+= points
[i
- 1].y
;
456 /* Don't draw pixels at junction points twice */
457 genericLine(image
, x1
, y1
, x2
, y2
, color
, operation
, True
);
461 i
= npoints
- 1; /* last point */
462 if (mode
== RAbsoluteCoordinates
) {
466 x2
+= points
[i
- 1].x
;
467 y2
+= points
[i
- 1].y
;
469 i
= (points
[0].x
== x2
&& points
[0].y
== y2
&& npoints
> 1);
470 genericLine(image
, x1
, y1
, x2
, y2
, color
, operation
, i
);
473 void ROperateRectangle(RImage
*image
, RPixelOperation operation
, int x0
, int y0
, int x1
, int y1
, const RColor
*color
)
477 for (y
= y0
; y
<= y1
; y
++) {
478 genericLine(image
, x0
, y
, x1
, y
, color
, operation
, False
);
482 void RDrawSegments(RImage
*image
, const RSegment
*segs
, int nsegs
, const RColor
*color
)
486 assert(segs
!= NULL
);
488 for (i
= 0; i
< nsegs
; i
++) {
489 genericLine(image
, segs
->x1
, segs
->y1
, segs
->x2
, segs
->y2
, color
, RNormalOperation
, False
);
494 void ROperateSegments(RImage
*image
, RPixelOperation operation
, const RSegment
*segs
, int nsegs
, const RColor
*color
)
498 assert(segs
!= NULL
);
500 for (i
= 0; i
< nsegs
; i
++) {
501 genericLine(image
, segs
->x1
, segs
->y1
, segs
->x2
, segs
->y2
, color
, operation
, False
);