2 * Raster graphics library
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *----------------------------------------------------------------------
34 * Apply 3x3 1 1 1 low pass, convolution mask to image.
37 *----------------------------------------------------------------------
40 RBlurImage(RImage
*image
)
44 unsigned char *ptr
, *nptr
;
45 unsigned char *pptr
=NULL
, *tmpp
;
46 int ch
= image
->format
== RRGBAFormat
? 4 : 3;
48 pptr
= malloc(image
->width
* ch
);
50 RErrorCode
= RERR_NOMEMORY
;
54 #define MASK(prev, cur, next, ch)\
55 (*(prev-ch) + *prev + *(prev+ch)\
56 +*(cur-ch) + 2 * *cur + *(cur+ch)\
57 +*(next-ch) + *next + *(next+ch)) / 10
59 memcpy(pptr
, image
->data
, image
->width
* ch
);
62 nptr
= ptr
+ image
->width
*ch
;
70 for (y
= 1; y
< image
->height
-1; y
++) {
72 for (x
= 1; x
< image
->width
-1; x
++) {
74 *ptr
= MASK(pptr
, ptr
, nptr
, 3);
76 ptr
++; nptr
++; pptr
++;
79 *ptr
= MASK(pptr
, ptr
, nptr
, 3);
81 ptr
++; nptr
++; pptr
++;
84 *ptr
= MASK(pptr
, ptr
, nptr
, 3);
86 ptr
++; nptr
++; pptr
++;
98 for (y
= 1; y
< image
->height
-1; y
++) {
99 for (x
= 1; x
< image
->width
-1; x
++) {
101 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
103 ptr
++; nptr
++; pptr
++;
106 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
108 ptr
++; nptr
++; pptr
++;
111 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
113 ptr
++; nptr
++; pptr
++;
116 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
118 ptr
++; nptr
++; pptr
++;
134 REdgeDetectImage(RImage
*image
)
136 register int x
, y
, d1
, d2
, d3
, d4
, rsum
;
138 unsigned char *r
, *g
, *b
, *a
;
139 unsigned char *dr
, *dg
, *db
, *da
;
140 unsigned char *pr
=NULL
, *pg
=NULL
, *pb
=NULL
, *pa
=NULL
;
144 image2
= RCloneImage(image
);
146 pr
= alloca(image
->width
*sizeof(char));
150 pg
= alloca(image
->width
*sizeof(char));
154 pb
= alloca(image
->width
*sizeof(char));
158 pa
= alloca(image
->width
*sizeof(char));
168 dr
= image2
->data
[0];
169 dg
= image2
->data
[1];
170 db
= image2
->data
[2];
171 da
= image2
->data
[3];
174 for (x
=0; x
<image
->width
; x
++) {
182 for (y
=1; y
<image
->height
-1; y
++) {
191 for (x
=1; x
<image
->width
-1; x
++) {
192 d1
= r
[w
+1] - r
[-w
-1];
194 d3
= r
[-w
+1] - r
[w
-1];
198 if (rsum
< 0) rsum
= -rsum
;
199 d1
= d1
- d2
- d4
; /* vertical gradient */
200 if (d1
< 0) d1
= -d1
;
201 if (d1
> rsum
) rsum
= d1
;
206 d1
= g
[w
+1] - g
[-w
-1];
208 d3
= g
[-w
+1] - g
[w
-1];
212 if (rsum
< 0) rsum
= -rsum
;
213 d1
= d1
- d2
- d4
; /* vertical gradient */
214 if (d1
< 0) d1
= -d1
;
215 if (d1
> rsum
) rsum
= d1
;
220 d1
= b
[w
+1] - b
[-w
-1];
222 d3
= b
[-w
+1] - b
[w
-1];
226 if (rsum
< 0) rsum
= -rsum
;
227 d1
= d1
- d2
- d4
; /* vertical gradient */
228 if (d1
< 0) d1
= -d1
;
229 if (d1
> rsum
) rsum
= d1
;
253 RReleaseImage(image2
);
263 RSmoothImage(RImage
*image
)
268 int ch
= image
->format
== RRGBAFormat
;
274 for (y
=0; y
<image
->height
- 1; y
++) {
275 for (x
=0; x
<image
->width
- 1; x
++) {
276 v
= *ptr
+ 2 * *(ptr
+ ch
) + 2 * *(ptr
+ w
) + *(ptr
+ w
+ ch
);
278 v
= *(ptr
+1) + 2 * *(ptr
+1 + ch
) + 2 * *(ptr
+1 + w
) + *(ptr
+1 + w
+ ch
);
280 v
= *(ptr
+2) + 2 * *(ptr
+2 + ch
) + 2 * *(ptr
+2 + w
) + *(ptr
+2 + w
+ ch
);
286 v
= 3 * *ptr
+ 3 * *(ptr
+ w
);
288 v
= 3 * *(ptr
+1) + 3 * *(ptr
+1 + w
);
290 v
= 3 * *(ptr
+2) + 3 * *(ptr
+2 + w
);
297 for (x
=0; x
<image
->width
- 1; x
++) {
298 v
= 3 * *ptr
+ 3 * *(ptr
+ ch
);
300 v
= 3 * *(ptr
+1) + 3 * *(ptr
+1 + ch
);
302 v
= 3 * *(ptr
+2) + 3 * *(ptr
+2 + ch
);