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.
30 *----------------------------------------------------------------------
32 * Apply 3x3 1 1 1 low pass, convolution mask to image.
35 *----------------------------------------------------------------------
37 int RBlurImage(RImage
* image
)
41 unsigned char *ptr
, *nptr
;
42 unsigned char *pptr
= NULL
, *tmpp
;
43 int ch
= image
->format
== RRGBAFormat
? 4 : 3;
45 pptr
= malloc(image
->width
* ch
);
47 RErrorCode
= RERR_NOMEMORY
;
50 #define MASK(prev, cur, next, ch)\
51 (*(prev-ch) + *prev + *(prev+ch)\
52 +*(cur-ch) + 2 * *cur + *(cur+ch)\
53 +*(next-ch) + *next + *(next+ch)) / 10
55 memcpy(pptr
, image
->data
, image
->width
* ch
);
58 nptr
= ptr
+ image
->width
* ch
;
66 for (y
= 1; y
< image
->height
- 1; y
++) {
68 for (x
= 1; x
< image
->width
- 1; x
++) {
70 *ptr
= MASK(pptr
, ptr
, nptr
, 3);
77 *ptr
= MASK(pptr
, ptr
, nptr
, 3);
84 *ptr
= MASK(pptr
, ptr
, nptr
, 3);
100 for (y
= 1; y
< image
->height
- 1; y
++) {
101 for (x
= 1; x
< image
->width
- 1; x
++) {
103 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
110 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
117 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
124 *ptr
= MASK(pptr
, ptr
, nptr
, 4);
141 int REdgeDetectImage(RImage
* image
)
143 register int x
, y
, d1
, d2
, d3
, d4
, rsum
;
145 unsigned char *r
, *g
, *b
, *a
;
146 unsigned char *dr
, *dg
, *db
, *da
;
147 unsigned char *pr
= NULL
, *pg
= NULL
, *pb
= NULL
, *pa
= NULL
;
150 image2
= RCloneImage(image
);
152 pr
= alloca(image
->width
* sizeof(char));
156 pg
= alloca(image
->width
* sizeof(char));
160 pb
= alloca(image
->width
* sizeof(char));
164 pa
= alloca(image
->width
* sizeof(char));
173 dr
= image2
->data
[0];
174 dg
= image2
->data
[1];
175 db
= image2
->data
[2];
176 da
= image2
->data
[3];
178 for (x
= 0; x
< image
->width
; x
++) {
186 for (y
= 1; y
< image
->height
- 1; y
++) {
187 dr
[w
- 1] = r
[w
- 1];
188 dg
[w
- 1] = g
[w
- 1];
189 db
[w
- 1] = b
[w
- 1];
195 for (x
= 1; x
< image
->width
- 1; x
++) {
196 d1
= r
[w
+ 1] - r
[-w
- 1];
198 d3
= r
[-w
+ 1] - r
[w
- 1];
204 d1
= d1
- d2
- d4
; /* vertical gradient */
213 d1
= g
[w
+ 1] - g
[-w
- 1];
215 d3
= g
[-w
+ 1] - g
[w
- 1];
221 d1
= d1
- d2
- d4
; /* vertical gradient */
230 d1
= b
[w
+ 1] - b
[-w
- 1];
232 d3
= b
[-w
+ 1] - b
[w
- 1];
238 d1
= d1
- d2
- d4
; /* vertical gradient */
266 RReleaseImage(image2
);
274 int RSmoothImage(RImage
* image
)
279 int ch
= image
->format
== RRGBAFormat
;
283 w
= image
->width
* ch
;
284 for (y
= 0; y
< image
->height
- 1; y
++) {
285 for (x
= 0; x
< image
->width
- 1; x
++) {
286 v
= *ptr
+ 2 * *(ptr
+ ch
) + 2 * *(ptr
+ w
) + *(ptr
+ w
+ ch
);
288 v
= *(ptr
+ 1) + 2 * *(ptr
+ 1 + ch
) + 2 * *(ptr
+ 1 + w
) + *(ptr
+ 1 + w
+ ch
);
290 v
= *(ptr
+ 2) + 2 * *(ptr
+ 2 + ch
) + 2 * *(ptr
+ 2 + w
) + *(ptr
+ 2 + w
+ ch
);
296 v
= 3 * *ptr
+ 3 * *(ptr
+ w
);
298 v
= 3 * *(ptr
+ 1) + 3 * *(ptr
+ 1 + w
);
300 v
= 3 * *(ptr
+ 2) + 3 * *(ptr
+ 2 + w
);
307 for (x
= 0; x
< image
->width
- 1; x
++) {
308 v
= 3 * *ptr
+ 3 * *(ptr
+ ch
);
310 v
= 3 * *(ptr
+ 1) + 3 * *(ptr
+ 1 + ch
);
312 v
= 3 * *(ptr
+ 2) + 3 * *(ptr
+ 2 + ch
);