wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / convolve.c
blob57072f05df0f1c5e88d4ba2ac88d5135d083f278
1 /*
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., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
22 #include <config.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include "wraster.h"
31 *----------------------------------------------------------------------
32 * RBlurImage--
33 * Apply 3x3 1 1 1 low pass, convolution mask to image.
34 * 1 2 1
35 * 1 1 1 /10
36 *----------------------------------------------------------------------
38 int RBlurImage(RImage * image)
40 register int x, y;
41 register int tmp;
42 unsigned char *ptr, *nptr;
43 unsigned char *pptr = NULL, *tmpp;
44 int ch = image->format == RRGBAFormat ? 4 : 3;
46 pptr = malloc(image->width * ch);
47 if (!pptr) {
48 RErrorCode = RERR_NOMEMORY;
49 return False;
51 #define MASK(prev, cur, next, ch)\
52 (*(prev-ch) + *prev + *(prev+ch)\
53 +*(cur-ch) + 2 * *cur + *(cur+ch)\
54 +*(next-ch) + *next + *(next+ch)) / 10
56 memcpy(pptr, image->data, image->width * ch);
58 ptr = image->data;
59 nptr = ptr + image->width * ch;
60 tmpp = pptr;
62 if (ch == 3) {
63 ptr += 3;
64 nptr += 3;
65 pptr += 3;
67 for (y = 1; y < image->height - 1; y++) {
69 for (x = 1; x < image->width - 1; x++) {
70 tmp = *ptr;
71 *ptr = MASK(pptr, ptr, nptr, 3);
72 *pptr = tmp;
73 ptr++;
74 nptr++;
75 pptr++;
77 tmp = *ptr;
78 *ptr = MASK(pptr, ptr, nptr, 3);
79 *pptr = tmp;
80 ptr++;
81 nptr++;
82 pptr++;
84 tmp = *ptr;
85 *ptr = MASK(pptr, ptr, nptr, 3);
86 *pptr = tmp;
87 ptr++;
88 nptr++;
89 pptr++;
91 pptr = tmpp;
92 ptr += 6;
93 nptr += 6;
94 pptr += 6;
96 } else {
97 ptr += 4;
98 nptr += 4;
99 pptr += 4;
101 for (y = 1; y < image->height - 1; y++) {
102 for (x = 1; x < image->width - 1; x++) {
103 tmp = *ptr;
104 *ptr = MASK(pptr, ptr, nptr, 4);
105 *pptr = tmp;
106 ptr++;
107 nptr++;
108 pptr++;
110 tmp = *ptr;
111 *ptr = MASK(pptr, ptr, nptr, 4);
112 *pptr = tmp;
113 ptr++;
114 nptr++;
115 pptr++;
117 tmp = *ptr;
118 *ptr = MASK(pptr, ptr, nptr, 4);
119 *pptr = tmp;
120 ptr++;
121 nptr++;
122 pptr++;
124 tmp = *ptr;
125 *ptr = MASK(pptr, ptr, nptr, 4);
126 *pptr = tmp;
127 ptr++;
128 nptr++;
129 pptr++;
131 pptr = tmpp;
132 ptr += 8;
133 nptr += 8;
134 pptr += 8;
138 return True;