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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#include "fitz.h"
typedef unsigned char byte;
static inline void srown(byte * restrict src, byte * restrict dst, int w, int denom, int n)
{
int invdenom = (1<<16) / denom;
int x, left, k;
unsigned sum[FZ_MAXCOLORS];
left = 0;
for (k = 0; k < n; k++)
sum[k] = 0;
for (x = 0; x < w; x++)
{
for (k = 0; k < n; k++)
sum[k] += src[x * n + k];
if (++left == denom)
{
left = 0;
for (k = 0; k < n; k++)
{
dst[k] = (sum[k] * invdenom + (1<<15)) >> 16;
sum[k] = 0;
}
dst += n;
}
}
/* left overs */
if (left)
for (k = 0; k < n; k++)
dst[k] = sum[k] / left;
}
/* special-case common 1-5 components - the compiler optimizes this */
static inline void srowc(byte * restrict src, byte * restrict dst, int w, int denom, int n)
{
int invdenom = (1<<16) / denom;
int x, left;
unsigned sum1 = 0;
unsigned sum2 = 0;
unsigned sum3 = 0;
unsigned sum4 = 0;
unsigned sum5 = 0;
assert(n <= 5);
left = 0;
for (x = 0; x < w; x++)
{
sum1 += src[x * n + 0];
/* the compiler eliminates these if-tests */
if (n >= 2)
sum2 += src[x * n + 1];
if (n >= 3)
sum3 += src[x * n + 2];
if (n >= 4)
sum4 += src[x * n + 3];
if (n >= 5)
sum5 += src[x * n + 4];
if (++left == denom)
{
left = 0;
dst[0] = (sum1 * invdenom + (1<<15)) >> 16;
sum1 = 0;
if (n >= 2) {
dst[1] = (sum2 * invdenom + (1<<15)) >> 16;
sum2 = 0;
}
if (n >= 3) {
dst[2] = (sum3 * invdenom + (1<<15)) >> 16;
sum3 = 0;
}
if (n >= 4) {
dst[3] = (sum4 * invdenom + (1<<15)) >> 16;
sum4 = 0;
}
if (n >= 5) {
dst[4] = (sum5 * invdenom + (1<<15)) >> 16;
sum5 = 0;
}
dst += n;
}
}
/* left overs */
if (left) {
dst[0] = sum1 / left;
if (n >=2)
dst[1] = sum2 / left;
if (n >=3)
dst[2] = sum3 / left;
if (n >=4)
dst[3] = sum4 / left;
if (n >=5)
dst[4] = sum5 / left;
}
}
static void srow1(byte *src, byte *dst, int w, int denom)
{
srowc(src, dst, w, denom, 1);
}
static void srow2(byte *src, byte *dst, int w, int denom)
{
srowc(src, dst, w, denom, 2);
}
static void srow4(byte *src, byte *dst, int w, int denom)
{
srowc(src, dst, w, denom, 4);
}
static void srow5(byte *src, byte *dst, int w, int denom)
{
srowc(src, dst, w, denom, 5);
}
static inline void scoln(byte * restrict src, byte * restrict dst, int w, int denom, int n)
{
int invdenom = (1<<16) / denom;
int x, y, k;
byte *s;
int sum[FZ_MAXCOLORS];
for (x = 0; x < w; x++)
{
s = src + (x * n);
for (k = 0; k < n; k++)
sum[k] = 0;
for (y = 0; y < denom; y++)
for (k = 0; k < n; k++)
sum[k] += s[y * w * n + k];
for (k = 0; k < n; k++)
dst[k] = (sum[k] * invdenom + (1<<15)) >> 16;
dst += n;
}
}
static void scol1(byte *src, byte *dst, int w, int denom)
{
scoln(src, dst, w, denom, 1);
}
static void scol2(byte *src, byte *dst, int w, int denom)
{
scoln(src, dst, w, denom, 2);
}
static void scol4(byte *src, byte *dst, int w, int denom)
{
scoln(src, dst, w, denom, 4);
}
static void scol5(byte *src, byte *dst, int w, int denom)
{
scoln(src, dst, w, denom, 5);
}
void (*fz_srown)(byte *src, byte *dst, int w, int denom, int n) = srown;
void (*fz_srow1)(byte *src, byte *dst, int w, int denom) = srow1;
void (*fz_srow2)(byte *src, byte *dst, int w, int denom) = srow2;
void (*fz_srow4)(byte *src, byte *dst, int w, int denom) = srow4;
void (*fz_srow5)(byte *src, byte *dst, int w, int denom) = srow5;
void (*fz_scoln)(byte *src, byte *dst, int w, int denom, int n) = scoln;
void (*fz_scol1)(byte *src, byte *dst, int w, int denom) = scol1;
void (*fz_scol2)(byte *src, byte *dst, int w, int denom) = scol2;
void (*fz_scol4)(byte *src, byte *dst, int w, int denom) = scol4;
void (*fz_scol5)(byte *src, byte *dst, int w, int denom) = scol5;
fz_pixmap *
fz_scalepixmap(fz_pixmap *src, int xdenom, int ydenom)
{
fz_pixmap *dst;
unsigned char *buf;
int y, iy, oy;
int ow, oh, n;
int remaining;
void (*srowx)(byte *src, byte *dst, int w, int denom) = nil;
void (*scolx)(byte *src, byte *dst, int w, int denom) = nil;
ow = (src->w + xdenom - 1) / xdenom;
oh = (src->h + ydenom - 1) / ydenom;
n = src->n;
buf = fz_malloc(ow * n * ydenom);
dst = fz_newpixmap(src->colorspace, 0, 0, ow, oh);
switch (n)
{
case 1: srowx = fz_srow1; scolx = fz_scol1; break;
case 2: srowx = fz_srow2; scolx = fz_scol2; break;
case 4: srowx = fz_srow4; scolx = fz_scol4; break;
case 5: srowx = fz_srow5; scolx = fz_scol5; break;
}
if (srowx && scolx)
{
for (y = 0, oy = 0; y < (src->h / ydenom) * ydenom; y += ydenom, oy++)
{
for (iy = 0; iy < ydenom; iy++)
{
srowx(src->samples + (y + iy) * src->w * n,
buf + iy * ow * n,
src->w, xdenom);
}
scolx(buf, dst->samples + oy * dst->w * n, dst->w, ydenom);
}
remaining = src->h - y;
if (remaining)
{
for (iy = 0; iy < remaining; iy++)
{
srowx(src->samples + (y + iy) * src->w * n,
buf + iy * ow * n,
src->w, xdenom);
}
scolx(buf, dst->samples + oy * dst->w * n, dst->w, remaining);
}
}
else
{
for (y = 0, oy = 0; y < (src->h / ydenom) * ydenom; y += ydenom, oy++)
{
for (iy = 0; iy < ydenom; iy++)
{
fz_srown(src->samples + (y + iy) * src->w * n,
buf + iy * ow * n,
src->w, xdenom, n);
}
fz_scoln(buf, dst->samples + oy * dst->w * n, dst->w, ydenom, n);
}
remaining = src->h - y;
if (remaining)
{
for (iy = 0; iy < remaining; iy++)
{
fz_srown(src->samples + (y + iy) * src->w * n,
buf + iy * ow * n,
src->w, xdenom, n);
}
fz_scoln(buf, dst->samples + oy * dst->w * n, dst->w, remaining, n);
}
}
fz_free(buf);
return dst;
}
|