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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/* Copyright (C) 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
This file is part of Aladdin Ghostscript.
Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the Aladdin Ghostscript Free Public
License (the "License") for full details.
Every copy of Aladdin Ghostscript must include a copy of the License,
normally in a plain ASCII text file named PUBLIC. The License grants you
the right to copy, modify and redistribute Aladdin Ghostscript, but only
under certain conditions described in the License. Among other things, the
License requires that the copyright notice and this notice be preserved on
all copies.
*/
/*$Id$ */
/* Routines for "flipping" image data */
#include "gx.h"
#include "gsbittab.h"
#include "gsflip.h"
#define arch_has_byte_regs 1
/* Transpose a block of bits between registers. */
#define transpose(r,s,mask,shift)\
r ^= (temp = ((s << shift) ^ r) & mask);\
s ^= temp >> shift
/* Define the size of byte temporaries. On Intel CPUs, this should be */
/* byte, but on all other CPUs, it should be uint. */
#if arch_has_byte_regs
typedef byte byte_var;
#else
typedef uint byte_var;
#endif
#define vtab(v80,v40,v20,v10,v8,v4,v2,v1)\
bit_table_8(0,v80,v40,v20,v10,v8,v4,v2,v1)
/* Convert 3Mx1 to 3x1. */
private void
flip3x1(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
uint n = nbytes;
static const bits32 tab3x1[256] =
{
vtab(0x800000, 0x100000, 0x20000, 0x4000, 0x800, 0x100, 0x20, 4)
};
for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
bits32 b24 =
tab3x1[*in1] | (tab3x1[*in2] >> 1) | (tab3x1[*in3] >> 2);
out[0] = (byte) (b24 >> 16);
out[1] = (byte) (b24 >> 8);
out[2] = (byte) b24;
}
}
/* Convert 3Mx2 to 3x2. */
private void
flip3x2(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
uint n = nbytes;
static const bits32 tab3x2[256] =
{
vtab(0x800000, 0x400000, 0x20000, 0x10000, 0x800, 0x400, 0x20, 0x10)
};
for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
bits32 b24 =
tab3x2[*in1] | (tab3x2[*in2] >> 2) | (tab3x2[*in3] >> 4);
out[0] = (byte) (b24 >> 16);
out[1] = (byte) (b24 >> 8);
out[2] = (byte) b24;
}
}
/* Convert 3Mx4 to 3x4. */
private void
flip3x4(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
uint n = nbytes;
for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
byte_var b1 = *in1, b2 = *in2, b3 = *in3;
out[0] = (b1 & 0xf0) | (b2 >> 4);
out[1] = (b3 & 0xf0) | (b1 & 0xf);
out[2] = (byte) (b2 << 4) | (b3 & 0xf);
}
}
/* Convert 3Mx8 to 3x8. */
private void
flip3x8(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
uint n = nbytes;
for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
out[0] = *in1;
out[1] = *in2;
out[2] = *in3;
}
}
/* Convert 3Mx12 to 3x12. */
private void
flip3x12(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *pa = planes[0] + offset;
const byte *pb = planes[1] + offset;
const byte *pc = planes[2] + offset;
uint n = nbytes;
/* We are guaranteed that the input is an integral number of pixels. */
/* This implies that n = 0 mod 3. */
for (; n > 0; out += 9, pa += 3, pb += 3, pc += 3, n -= 3) {
byte_var a1 = pa[1], b0 = pb[0], b1 = pb[1], b2 = pb[2], c1 = pc[1];
out[0] = pa[0];
out[1] = (a1 & 0xf0) | (b0 >> 4);
out[2] = (byte) ((b0 << 4) | (b1 >> 4));
out[3] = pc[0];
out[4] = (c1 & 0xf0) | (a1 & 0xf);
out[5] = pa[2];
out[6] = (byte) ((b1 << 4) | (b2 >> 4));
out[7] = (byte) ((b2 << 4) | (c1 & 0xf));
out[8] = pc[2];
}
}
/* Convert 4Mx1 to 4x1. */
private void
flip4x1(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
const byte *in4 = planes[3] + offset;
uint n = nbytes;
for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
byte_var b1 = *in1, b2 = *in2, b3 = *in3, b4 = *in4;
byte_var temp;
/* Transpose blocks of 1 */
transpose(b1, b2, 0x55, 1);
transpose(b3, b4, 0x55, 1);
/* Transpose blocks of 2 */
transpose(b1, b3, 0x33, 2);
transpose(b2, b4, 0x33, 2);
/* There's probably a faster way to do this.... */
out[0] = (b1 & 0xf0) | (b2 >> 4);
out[1] = (b3 & 0xf0) | (b4 >> 4);
out[2] = (byte) ((b1 << 4) | (b2 & 0xf));
out[3] = (byte) ((b3 << 4) | (b4 & 0xf));
}
}
/* Convert 4Mx2 to 4x2. */
private void
flip4x2(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
const byte *in4 = planes[3] + offset;
uint n = nbytes;
for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
byte_var b1 = *in1, b2 = *in2, b3 = *in3, b4 = *in4;
byte_var temp;
/* Transpose blocks of 4x2 */
transpose(b1, b3, 0x0f, 4);
transpose(b2, b4, 0x0f, 4);
/* Transpose blocks of 2x1 */
transpose(b1, b2, 0x33, 2);
transpose(b3, b4, 0x33, 2);
out[0] = b1;
out[1] = b2;
out[2] = b3;
out[3] = b4;
}
}
/* Convert 4Mx4 to 4x4. */
private void
flip4x4(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
const byte *in4 = planes[3] + offset;
uint n = nbytes;
for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
byte_var b1 = *in1, b2 = *in2, b3 = *in3, b4 = *in4;
out[0] = (b1 & 0xf0) | (b2 >> 4);
out[1] = (b3 & 0xf0) | (b4 >> 4);
out[2] = (byte) ((b1 << 4) | (b2 & 0xf));
out[3] = (byte) ((b3 << 4) | (b4 & 0xf));
}
}
/* Convert 4Mx8 to 4x8. */
private void
flip4x8(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *in1 = planes[0] + offset;
const byte *in2 = planes[1] + offset;
const byte *in3 = planes[2] + offset;
const byte *in4 = planes[3] + offset;
uint n = nbytes;
for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
out[0] = *in1;
out[1] = *in2;
out[2] = *in3;
out[3] = *in4;
}
}
/* Convert 4Mx12 to 4x12. */
private void
flip4x12(byte * buffer, const byte ** planes, uint offset, uint nbytes)
{
byte *out = buffer;
const byte *pa = planes[0] + offset;
const byte *pb = planes[1] + offset;
const byte *pc = planes[2] + offset;
const byte *pd = planes[3] + offset;
uint n = nbytes;
/* We are guaranteed that the input is an integral number of pixels. */
/* This implies that n = 0 mod 3. */
for (; n > 0; out += 12, pa += 3, pb += 3, pc += 3, pd += 3, n -= 3) {
byte_var a1 = pa[1], b1 = pb[1], c1 = pc[1], d1 = pd[1];
{
byte_var v0;
out[0] = pa[0];
v0 = pb[0];
out[1] = (a1 & 0xf0) | (v0 >> 4);
out[2] = (byte) ((v0 << 4) | (b1 >> 4));
out[3] = pc[0];
v0 = pd[0];
out[4] = (c1 & 0xf0) | (v0 >> 4);
out[5] = (byte) ((v0 << 4) | (d1 >> 4));
}
{
byte_var v2;
v2 = pa[2];
out[6] = (byte) ((a1 << 4) | (v2 >> 4));
out[7] = (byte) ((v2 << 4) | (b1 & 0xf));
out[8] = pb[2];
v2 = pc[2];
out[9] = (byte) ((c1 << 4) | (v2 >> 4));
out[10] = (byte) ((v2 << 4) | (d1 & 0xf));
out[11] = pd[2];
}
}
}
/* Flip data given number of planes and bits per pixel. */
typedef void (*image_flip_proc) (P4(byte *, const byte **, uint, uint));
private const image_flip_proc image_flip_procs[2][13] =
{
{0, flip3x1, flip3x2, 0, flip3x4, 0, 0, 0, flip3x8, 0, 0, 0, flip3x12},
{0, flip4x1, flip4x2, 0, flip4x4, 0, 0, 0, flip4x8, 0, 0, 0, flip4x12}
};
/* Here is the public interface to all of the above. */
int
image_flip_planes(byte * buffer, const byte ** planes, uint offset, uint nbytes,
int num_planes, int bits_per_sample)
{
void (*proc) (P4(byte * buffer, const byte ** planes, uint offset, uint nbytes));
if (num_planes < 3 || num_planes > 4 ||
bits_per_sample < 1 || bits_per_sample > 12 ||
(proc = image_flip_procs[num_planes - 3][bits_per_sample]) == 0
)
return -1;
(*proc) (buffer, planes, offset, nbytes);
return 0;
}
|