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
, RPixelOperation 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
, RPixelOperation 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
, RCoordinatesMode 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
, RPixelOperation operation
,
209 const RPoint
*points
, int npoints
, RCoordinatesMode mode
,
212 register int x
, y
, i
;
214 assert(image
!= NULL
);
215 assert(points
!= NULL
);
219 for (i
= 0; i
< npoints
; i
++) {
220 if (mode
== RAbsoluteCoordinates
) {
227 ROperatePixel(image
, operation
, x
, y
, color
);
231 static Bool
clipLineInRectangle(int xmin
, int ymin
, int xmax
, int ymax
, int *x1
, int *y1
, int *x2
, int *y2
)
237 #define CHECK_OUT(X,Y) (((Y) > ymax ? TOP : ((Y) < ymin ? BOT : 0))\
238 | ((X) > xmax ? RIG : ((X) < xmin ? LEF : 0)))
240 int ocode1
, ocode2
, ocode
;
244 ocode1
= CHECK_OUT(*x1
, *y1
);
245 ocode2
= CHECK_OUT(*x2
, *y2
);
248 if (!ocode1
&& !ocode2
) { /* completely inside */
251 } else if (ocode1
& ocode2
) {
261 x
= *x1
+ (*x2
- *x1
) * (ymax
- *y1
) / (*y2
- *y1
);
263 } else if (ocode
& BOT
) {
264 x
= *x1
+ (*x2
- *x1
) * (ymin
- *y1
) / (*y2
- *y1
);
266 } else if (ocode
& RIG
) {
267 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
269 } else { /* //if (ocode & LEF) { */
270 y
= *y1
+ (*y2
- *y1
) * (xmax
- *x1
) / (*x2
- *x1
);
274 if (ocode
== ocode1
) {
277 ocode1
= CHECK_OUT(x
, y
);
281 ocode2
= CHECK_OUT(x
, y
);
289 * This routine is a generic drawing routine, based on Bresenham's line
292 static int genericLine(RImage
*image
, int x0
, int y0
, int x1
, int y1
, const RColor
*color
,
293 RPixelOperation operation
, int polyline
)
295 int i
, err
, du
, dv
, du2
, dv2
, uofs
, vofs
, last
;
297 assert(image
!= NULL
);
299 if (!clipLineInRectangle(0, 0, image
->width
- 1, image
->height
- 1, &x0
, &y0
, &x1
, &y1
))
314 vofs
= -image
->width
;
318 /* Swap coordinates between them, so that always du>dv */
330 last
= (polyline
) ? du
- 1 : du
;
332 if (color
->alpha
== 255 || operation
== RCopyOperation
) {
335 if (image
->format
== RRGBAFormat
)
336 i
= (y0
* image
->width
+ x0
) * 4;
338 i
= (y0
* image
->width
+ x0
) * 3;
339 ptr
= image
->data
+ i
;
341 for (i
= 0; i
<= last
; i
++) {
344 *(ptr
+ 1) = color
->green
;
345 *(ptr
+ 2) = color
->blue
;
346 if (image
->format
== RRGBAFormat
)
349 /* Compute error for NeXT Step */
352 if (image
->format
== RRGBAFormat
)
358 if (image
->format
== RRGBAFormat
)
364 register int ofs
= y0
* image
->width
+ x0
;
366 for (i
= 0; i
<= last
; i
++) {
368 operatePixel(image
, ofs
, operation
, color
);
370 /* Compute error for NeXT Step */
383 int RDrawLine(RImage
* image
, int x0
, int y0
, int x1
, int y1
, const RColor
* color
)
385 return genericLine(image
, x0
, y0
, x1
, y1
, color
, RNormalOperation
, False
);
388 int ROperateLine(RImage
*image
, RPixelOperation operation
, int x0
, int y0
, int x1
, int y1
, const RColor
*color
)
390 return genericLine(image
, x0
, y0
, x1
, y1
, color
, operation
, False
);
393 void RDrawLines(RImage
*image
, const RPoint
*points
, int npoints
, RCoordinatesMode mode
, const RColor
*color
)
395 register int x1
, y1
, x2
, y2
, i
;
397 assert(points
!= NULL
);
406 for (i
= 1; i
< npoints
- 1; i
++) {
407 if (mode
== RAbsoluteCoordinates
) {
411 x2
+= points
[i
- 1].x
;
412 y2
+= points
[i
- 1].y
;
414 /* Don't draw pixels at junction points twice */
415 genericLine(image
, x1
, y1
, x2
, y2
, color
, RNormalOperation
, True
);
419 i
= npoints
- 1; /* last point */
420 if (mode
== RAbsoluteCoordinates
) {
424 x2
+= points
[i
- 1].x
;
425 y2
+= points
[i
- 1].y
;
427 i
= (points
[0].x
== x2
&& points
[0].y
== y2
&& npoints
> 1);
428 genericLine(image
, x1
, y1
, x2
, y2
, color
, RNormalOperation
, i
);
431 void ROperateLines(RImage
*image
, RPixelOperation operation
,
432 const RPoint
*points
, int npoints
, RCoordinatesMode mode
,
435 register int x1
, y1
, x2
, y2
, i
;
437 assert(points
!= NULL
);
446 for (i
= 1; i
< npoints
- 1; i
++) {
447 if (mode
== RAbsoluteCoordinates
) {
451 x2
+= points
[i
- 1].x
;
452 y2
+= points
[i
- 1].y
;
454 /* Don't draw pixels at junction points twice */
455 genericLine(image
, x1
, y1
, x2
, y2
, color
, operation
, True
);
459 i
= npoints
- 1; /* last point */
460 if (mode
== RAbsoluteCoordinates
) {
464 x2
+= points
[i
- 1].x
;
465 y2
+= points
[i
- 1].y
;
467 i
= (points
[0].x
== x2
&& points
[0].y
== y2
&& npoints
> 1);
468 genericLine(image
, x1
, y1
, x2
, y2
, color
, operation
, i
);
471 void ROperateRectangle(RImage
*image
, RPixelOperation operation
, int x0
, int y0
, int x1
, int y1
, const RColor
*color
)
475 for (y
= y0
; y
<= y1
; y
++) {
476 genericLine(image
, x0
, y
, x1
, y
, color
, operation
, False
);
480 void RDrawSegments(RImage
*image
, const RSegment
*segs
, int nsegs
, const RColor
*color
)
484 assert(segs
!= NULL
);
486 for (i
= 0; i
< nsegs
; i
++) {
487 genericLine(image
, segs
->x1
, segs
->y1
, segs
->x2
, segs
->y2
, color
, RNormalOperation
, False
);
492 void ROperateSegments(RImage
*image
, RPixelOperation operation
, const RSegment
*segs
, int nsegs
, const RColor
*color
)
496 assert(segs
!= NULL
);
498 for (i
= 0; i
< nsegs
; i
++) {
499 genericLine(image
, segs
->x1
, segs
->y1
, segs
->x2
, segs
->y2
, color
, operation
, False
);