1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
#include <fitz.h>
/* TODO: these should go in fz_rastfuncs */
typedef void (*rowfunc)(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom);
typedef void (*colfunc)(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom);
static void
scalerow(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom)
{
int x, left, k;
int sum[FZ_MAXCOLORS];
left = 0;
for (k = 0; k < ncomp; k++)
sum[k] = 0;
for (x = 0; x < w; x++)
{
for (k = 0; k < ncomp; k++)
sum[k] += src[x * ncomp + k];
if (++left == denom)
{
left = 0;
for (k = 0; k < ncomp; k++)
{
dst[k] = sum[k] / denom;
sum[k] = 0;
}
dst += ncomp;
}
}
/* left overs */
if (left)
for (k = 0; k < ncomp; k++)
dst[k] = sum[k] / left;
}
static void
scalerow1(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom)
{
int x, left;
int sum;
left = 0;
sum = 0;
for (x = 0; x < w; x++)
{
sum += *src++;
if (++left == denom)
{
left = 0;
*dst++ = sum / denom;
sum = 0;
}
}
/* left overs */
if (left)
{
*dst++ = sum / left;
}
}
static void
scalerow2(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom)
{
int x, left;
int sum0, sum1;
left = 0;
sum0 = 0;
sum1 = 0;
for (x = 0; x < w; x++)
{
sum0 += *src++;
sum1 += *src++;
if (++left == denom)
{
left = 0;
*dst++ = sum0 / denom;
*dst++ = sum1 / denom;
sum0 = 0;
sum1 = 0;
}
}
/* left overs */
if (left)
{
*dst++ = sum0 / left;
*dst++ = sum1 / left;
}
}
static void
scalecols(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom)
{
int x, y, k;
unsigned char *s;
int sum[FZ_MAXCOLORS];
for (x = 0; x < w; x++)
{
s = src + (x * ncomp);
for (k = 0; k < ncomp; k++)
sum[k] = 0;
for (y = 0; y < denom; y++)
for (k = 0; k < ncomp; k++)
sum[k] += s[y * w * ncomp + k];
for (k = 0; k < ncomp; k++)
dst[k] = sum[k] / denom;
dst += ncomp;
}
}
static void
scalecols1(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom)
{
int x, y;
unsigned char *s;
int sum;
for (x = 0; x < w; x++)
{
s = src + x;
sum = 0;
for (y = 0; y < denom; y++)
sum += s[y * w];
*dst++ = sum / denom;
}
}
static void
scalecols2(unsigned char *src, unsigned char *dst, int w, int ncomp, int denom)
{
int x, y;
unsigned char *s;
int sum0, sum1;
for (x = 0; x < w; x++)
{
s = src + (x * 2);
sum0 = 0;
sum1 = 0;
for (y = 0; y < denom; y++)
{
sum0 += s[y * w * 2 + 0];
sum1 += s[y * w * 2 + 1];
}
*dst++ = sum0 / denom;
*dst++ = sum1 / denom;
}
}
fz_error *
fz_scalepixmap(fz_pixmap **dstp, fz_pixmap *src, int xdenom, int ydenom)
{
fz_error *error;
fz_pixmap *dst;
unsigned char *buf;
int y, iy, oy;
int ow, oh, n;
rowfunc rowfunc;
colfunc colfunc;
ow = (src->w + xdenom - 1) / xdenom;
oh = (src->h + ydenom - 1) / ydenom;
n = src->n;
buf = fz_malloc(ow * n * ydenom);
if (!buf)
return fz_outofmem;
error = fz_newpixmap(&dst, 0, 0, ow, oh, src->n);
if (error)
{
fz_free(buf);
return error;
}
switch (n)
{
case 1:
rowfunc = scalerow1;
colfunc = scalecols1;
break;
case 2:
rowfunc = scalerow2;
colfunc = scalecols2;
break;
default:
rowfunc = scalerow;
colfunc = scalecols;
break;
}
for (y = 0, oy = 0; y < (src->h / ydenom) * ydenom; y += ydenom, oy++)
{
for (iy = 0; iy < ydenom; iy++)
rowfunc(src->samples + (y + iy) * src->w * n,
buf + iy * ow * n,
src->w, n, xdenom);
colfunc(buf, dst->samples + oy * dst->w * n, dst->w, n, ydenom);
}
ydenom = src->h - y;
if (ydenom)
{
for (iy = 0; iy < ydenom; iy++)
rowfunc(src->samples + (y + iy) * src->w * n,
buf + iy * ow * n,
src->w, n, xdenom);
colfunc(buf, dst->samples + oy * dst->w * n, dst->w, n, ydenom);
}
fz_free(buf);
*dstp = dst;
return nil;
}
|