summaryrefslogtreecommitdiff
blob: b86dcf41b557fe42e77e277c1289e929c400cc1d (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright (C) 2001-2020 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* Routines for "flipping" image data */
#include "gx.h"
#include "gserrors.h"		/* for rangecheck in sample macros */
#include "gsbitops.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. */
static int
flip3x1(byte * buffer, const byte ** planes, int offset, int nbytes)
{
    byte *out = buffer;
    const byte *in1 = planes[0] + offset;
    const byte *in2 = planes[1] + offset;
    const byte *in3 = planes[2] + offset;
    int 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;
    }
    return 0;
}

/* Convert 3Mx2 to 3x2. */
static int
flip3x2(byte * buffer, const byte ** planes, int offset, int nbytes)
{
    byte *out = buffer;
    const byte *in1 = planes[0] + offset;
    const byte *in2 = planes[1] + offset;
    const byte *in3 = planes[2] + offset;
    int 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;
    }
    return 0;
}

/* Convert 3Mx4 to 3x4. */
static int
flip3x4(byte * buffer, const byte ** planes, int offset, int nbytes)
{
    byte *out = buffer;
    const byte *in1 = planes[0] + offset;
    const byte *in2 = planes[1] + offset;
    const byte *in3 = planes[2] + offset;
    int 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);
    }
    return 0;
}

/* Convert 3Mx8 to 3x8. */
static int
flip3x8(byte * buffer, const byte ** planes, int offset, int nbytes)
{
    byte *out = buffer;
    const byte *in1 = planes[0] + offset;
    const byte *in2 = planes[1] + offset;
    const byte *in3 = planes[2] + offset;
    int n = nbytes;

    for (; n > 0; out += 3, ++in1, ++in2, ++in3, --n) {
        out[0] = *in1;
        out[1] = *in2;
        out[2] = *in3;
    }
    return 0;
}

/* Convert 3Mx12 to 3x12. */
static int
flip3x12(byte * buffer, const byte ** planes, int offset, int nbytes)
{
    byte *out = buffer;
    const byte *pa = planes[0] + offset;
    const byte *pb = planes[1] + offset;
    const byte *pc = planes[2] + offset;
    int n = nbytes;

    /*
     * We assume that the input is an integral number of pixels, and
     * round up n to a multiple of 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];
    }
    return 0;
}

/* Convert 4Mx1 to 4x1. */
static int
flip4x1(byte * buffer, const byte ** planes, int offset, int 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;
    int 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));
    }
    return 0;
}

/* Convert 4Mx2 to 4x2. */
static int
flip4x2(byte * buffer, const byte ** planes, int offset, int 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;
    int 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;
    }
    return 0;
}

/* Convert 4Mx4 to 4x4. */
static int
flip4x4(byte * buffer, const byte ** planes, int offset, int 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;
    int 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));
    }
    return 0;
}

/* Convert 4Mx8 to 4x8. */
static int
flip4x8(byte * buffer, const byte ** planes, int offset, int 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;
    int n = nbytes;

    for (; n > 0; out += 4, ++in1, ++in2, ++in3, ++in4, --n) {
        out[0] = *in1;
        out[1] = *in2;
        out[2] = *in3;
        out[3] = *in4;
    }
    return 0;
}

/* Convert 4Mx12 to 4x12. */
static int
flip4x12(byte * buffer, const byte ** planes, int offset, int 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;
    int n = nbytes;

    /*
     * We assume that the input is an integral number of pixels, and
     * round up n to a multiple of 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];
        }
    }
    return 0;
}

/* Convert NMx{1,2,4,8} to Nx{1,2,4,8}. */
static int
flipNx1to8(byte * buffer, const byte ** planes, int offset, int nbytes,
           int num_planes, int bits_per_sample)
{
    /* This is only needed for DeviceN colors, so it can be slow. */
    uint mask = (1 << bits_per_sample) - 1;
    int bi, pi;
    byte *dptr = buffer;
    int dbit = 0;
    byte dbbyte = 0;

    for (bi = 0; bi < nbytes * 8; bi += bits_per_sample) {
        for (pi = 0; pi < num_planes; ++pi) {
            const byte *sptr = planes[pi] + offset + (bi >> 3);
            uint value = (*sptr >> (8 - (bi & 7) - bits_per_sample)) & mask;

            if (sample_store_next8(value, &dptr, &dbit, bits_per_sample, &dbbyte) < 0)
                return_error(gs_error_rangecheck);
        }
    }
    sample_store_flush(dptr, dbit, dbbyte);
    return 0;
}

/* Convert NMx12 to Nx12. */
static int
flipNx12(byte * buffer, const byte ** planes, int offset, int nbytes,
         int num_planes, int ignore_bits_per_sample)
{
    /* This is only needed for DeviceN colors, so it can be slow. */
    int bi, pi;
    byte *dptr = buffer;
    int dbit = 0;
    byte dbbyte = 0;

    for (bi = 0; bi < nbytes * 8; bi += 12) {
        for (pi = 0; pi < num_planes; ++pi) {
            const byte *sptr = planes[pi] + offset + (bi >> 3);
            uint value =
                (bi & 4 ? ((*sptr & 0xf) << 8) | sptr[1] :
                 (*sptr << 4) | (sptr[1] >> 4));

            sample_store_next_12(value, &dptr, &dbit, &dbbyte);
        }
    }
    sample_store_flush(dptr, dbit, dbbyte);
    return 0;
}

/* Flip data given number of planes and bits per pixel. */
typedef int (*image_flip_proc) (byte *, const byte **, int, int);
static int
flip_fail(byte * buffer, const byte ** planes, int offset, int nbytes)
{
    return -1;
}
static const image_flip_proc image_flip3_procs[13] = {
    flip_fail, flip3x1, flip3x2, flip_fail, flip3x4,
    flip_fail, flip_fail, flip_fail, flip3x8,
    flip_fail, flip_fail, flip_fail, flip3x12
};
static const image_flip_proc image_flip4_procs[13] = {
    flip_fail, flip4x1, flip4x2, flip_fail, flip4x4,
    flip_fail, flip_fail, flip_fail, flip4x8,
    flip_fail, flip_fail, flip_fail, flip4x12
};
typedef int (*image_flipN_proc) (byte *, const byte **, int, int, int, int);
static int
flipN_fail(byte * buffer, const byte ** planes, int offset, int nbytes,
           int num_planes, int bits_per_sample)
{
    return -1;
}
static const image_flipN_proc image_flipN_procs[13] = {
    flipN_fail, flipNx1to8, flipNx1to8, flipN_fail, flipNx1to8,
    flipN_fail, flipN_fail, flipN_fail, flipNx1to8,
    flipN_fail, flipN_fail, flipN_fail, flipNx12
};

/* Here is the public interface to all of the above. */
int
image_flip_planes(byte * buffer, const byte ** planes, int offset, int nbytes,
                  int num_planes, int bits_per_sample)
{
    if (bits_per_sample < 1 || bits_per_sample > 12)
        return -1;
    switch (num_planes) {

    case 3:
        return image_flip3_procs[bits_per_sample]
            (buffer, planes, offset, nbytes);
    case 4:
        return image_flip4_procs[bits_per_sample]
            (buffer, planes, offset, nbytes);
    default:
        if (num_planes < 0)
            return -1;
        return image_flipN_procs[bits_per_sample]
            (buffer, planes, offset, nbytes, num_planes, bits_per_sample);
    }
}