Update Serbian translation from master branch
[wmaker-crm.git] / wrlib / convolve.c
blob8873a620619dd8e802cbfcc34d1f41a95d6c4dfc
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>
29 #include "wraster.h"
30 #include "wr_i18n.h"
34 *----------------------------------------------------------------------
35 * RBlurImage--
36 * Apply 3x3 1 1 1 low pass, convolution mask to image.
37 * 1 2 1
38 * 1 1 1 /10
39 *----------------------------------------------------------------------
41 int RBlurImage(RImage * image)
43 register int x, y;
44 register int tmp;
45 unsigned char *ptr, *nptr;
46 unsigned char *pptr = NULL, *tmpp;
47 int ch = image->format == RRGBAFormat ? 4 : 3;
49 pptr = malloc(image->width * ch);
50 if (!pptr) {
51 RErrorCode = RERR_NOMEMORY;
52 return False;
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);
61 ptr = image->data;
62 nptr = ptr + image->width * ch;
63 tmpp = pptr;
65 if (ch == 3) {
66 ptr += 3;
67 nptr += 3;
68 pptr += 3;
70 for (y = 1; y < image->height - 1; y++) {
72 for (x = 1; x < image->width - 1; x++) {
73 tmp = *ptr;
74 *ptr = MASK(pptr, ptr, nptr, 3);
75 *pptr = tmp;
76 ptr++;
77 nptr++;
78 pptr++;
80 tmp = *ptr;
81 *ptr = MASK(pptr, ptr, nptr, 3);
82 *pptr = tmp;
83 ptr++;
84 nptr++;
85 pptr++;
87 tmp = *ptr;
88 *ptr = MASK(pptr, ptr, nptr, 3);
89 *pptr = tmp;
90 ptr++;
91 nptr++;
92 pptr++;
94 pptr = tmpp;
95 ptr += 6;
96 nptr += 6;
97 pptr += 6;
99 } else {
100 ptr += 4;
101 nptr += 4;
102 pptr += 4;
104 for (y = 1; y < image->height - 1; y++) {
105 for (x = 1; x < image->width - 1; x++) {
106 tmp = *ptr;
107 *ptr = MASK(pptr, ptr, nptr, 4);
108 *pptr = tmp;
109 ptr++;
110 nptr++;
111 pptr++;
113 tmp = *ptr;
114 *ptr = MASK(pptr, ptr, nptr, 4);
115 *pptr = tmp;
116 ptr++;
117 nptr++;
118 pptr++;
120 tmp = *ptr;
121 *ptr = MASK(pptr, ptr, nptr, 4);
122 *pptr = tmp;
123 ptr++;
124 nptr++;
125 pptr++;
127 tmp = *ptr;
128 *ptr = MASK(pptr, ptr, nptr, 4);
129 *pptr = tmp;
130 ptr++;
131 nptr++;
132 pptr++;
134 pptr = tmpp;
135 ptr += 8;
136 nptr += 8;
137 pptr += 8;
141 free(tmpp);
143 return True;