summaryrefslogtreecommitdiff
path: root/base/gximdecode.c
blob: ab710a3daa6536a507315a84555e8e782b7f9bce (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
/* Copyright (C) 2014-2023 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.,  39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/

/* Methods for decoding and unpacking image data.  Used for color
monitoring in clist and for creating TIFF files for xpswrite device */

#include "gximdecode.h"
#include "string_.h"

/* We need to have the unpacking proc so that we can monitor the data for color
   or decode during xpswrite */
void
get_unpack_proc(gx_image_enum_common_t *pie, image_decode_t *imd,
                gs_image_format_t format, const float *decode) {

    static sample_unpack_proc_t procs[2][6] = {
        { sample_unpack_1, sample_unpack_2,
        sample_unpack_4, sample_unpack_8,
        sample_unpack_12, sample_unpackicc_16
        },
        { sample_unpack_1_interleaved, sample_unpack_2_interleaved,
        sample_unpack_4_interleaved, sample_unpack_8_interleaved,
        sample_unpack_12, sample_unpackicc_16
        } };
    int num_planes = pie->num_planes;
    bool interleaved = (num_planes == 1 && pie->plane_depths[0] != imd->bps);
    int i;
    int index_bps = (imd->bps < 8 ? imd->bps >> 1 : (imd->bps >> 2) + 1);
    int log2_xbytes = (imd->bps <= 8 ? 0 : arch_log2_sizeof_frac);

    imd->unpack = NULL;
    if (index_bps < 0 || index_bps > 5)
        return;

    switch (format) {
    case gs_image_format_chunky:
        imd->spread = 1 << log2_xbytes;
        break;
    case gs_image_format_component_planar:
        imd->spread = (imd->spp) << log2_xbytes;
        break;
    case gs_image_format_bit_planar:
        imd->spread = (imd->spp) << log2_xbytes;
        break;
    default:
        imd->spread = 0;
    }

    if (interleaved) {
        int num_components = pie->plane_depths[0] / imd->bps;

        for (i = 1; i < num_components; i++) {
            if (decode[0] != decode[i * 2 + 0] ||
                decode[1] != decode[i * 2 + 1])
                break;
        }
        if (i == num_components)
            interleaved = false; /* Use single table. */
    }
    imd->unpack = procs[interleaved][index_bps];
}

/* We also need the mapping method for the unpacking proc */
void
get_map(image_decode_t *imd, gs_image_format_t format, const float *decode)
{
    int ci = 0;
    int decode_type;
    int bps = imd->bps;
    int spp = imd->spp;
    static const float default_decode[] = {
        0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0
    };
    const float *this_decode = &decode[ci * 2];
    const float *map_decode;        /* decoding used to */
                              /* construct the expansion map */
    const float *real_decode;       /* decoding for expanded samples */

    decode_type = 3; /* 0=custom, 1=identity, 2=inverted, 3=impossible */
    for (ci = 0; ci < spp; ci += 2) {
        decode_type &= (decode[ci] == 0. && decode[ci + 1] == 1.) |
            (decode[ci] == 1. && decode[ci + 1] == 0.) << 1;
    }

    /* Initialize the maps from samples to intensities. */
    for (ci = 0; ci < spp; ci++) {
        sample_map *pmap = &imd->map[ci];

        if (bps > 8)
            imd->applymap = applymap16;
        else
            imd->applymap = applymap8;

        /* If the decoding is [0 1] or [1 0], we can fold it */
        /* into the expansion of the sample values; */
        /* otherwise, we have to use the floating point method. */

        this_decode = &decode[ci * 2];

        map_decode = real_decode = this_decode;
        if (!(decode_type & 1)) {
            if ((decode_type & 2) && bps <= 8) {
                real_decode = default_decode;
            }
            else {
                map_decode = default_decode;
            }
        }
        if (bps > 2 || format != gs_image_format_chunky) {
            if (bps <= 8)
                image_init_map(&pmap->table.lookup8[0], 1 << bps,
                map_decode);
        }
        else {                /* The map index encompasses more than one pixel. */
            byte map[4];
            register int i;

            image_init_map(&map[0], 1 << bps, map_decode);
            switch (bps) {
            case 1:
            {
                register bits32 *p = &pmap->table.lookup4x1to32[0];

                if (map[0] == 0 && map[1] == 0xff)
                    memcpy((byte *)p, lookup4x1to32_identity, 16 * 4);
                else if (map[0] == 0xff && map[1] == 0)
                    memcpy((byte *)p, lookup4x1to32_inverted, 16 * 4);
                else
                    for (i = 0; i < 16; i++, p++)
                        ((byte *)p)[0] = map[i >> 3],
                        ((byte *)p)[1] = map[(i >> 2) & 1],
                        ((byte *)p)[2] = map[(i >> 1) & 1],
                        ((byte *)p)[3] = map[i & 1];
            }
            break;
            case 2:
            {
                register bits16 *p = &pmap->table.lookup2x2to16[0];

                for (i = 0; i < 16; i++, p++)
                    ((byte *)p)[0] = map[i >> 2],
                    ((byte *)p)[1] = map[i & 3];
            }
            break;
            }
        }
        pmap->decode_base /* = decode_lookup[0] */ = real_decode[0];
        pmap->decode_factor =
            (real_decode[1] - real_decode[0]) /
            (bps <= 8 ? 255.0 : (float)frac_1);
        pmap->decode_max /* = decode_lookup[15] */ = real_decode[1];
        if (decode_type) {
            pmap->decoding = sd_none;
            pmap->inverted = map_decode[0] != 0;
        }
        else if (bps <= 4) {
            int step = 15 / ((1 << bps) - 1);
            int i;

            pmap->decoding = sd_lookup;
            for (i = 15 - step; i > 0; i -= step)
                pmap->decode_lookup[i] = pmap->decode_base +
                i * (255.0 / 15) * pmap->decode_factor;
        }
        else
            pmap->decoding = sd_compute;
    }
}

/* We only provide 8 or 16 bit output with the application of the mapping */
void applymap8(sample_map map[], const void *psrc_in, int spp, void *pdes,
    void *bufend)
{
    byte* psrc = (byte*)psrc_in;
    byte *curr_pos = (byte*) pdes;
    int k;
    float temp;

    while (curr_pos < (byte*) bufend) {
        for (k = 0; k < spp; k++) {
            switch (map[k].decoding) {
            case sd_none:
                *curr_pos = *psrc;
                break;
            case sd_lookup:
                temp = map[k].decode_lookup[(*psrc) >> 4] * 255;
                if (temp > 255) temp = 255;
                if (temp < 0) temp = 0;
                *curr_pos = (byte)temp;
                break;
            case sd_compute:
                temp = map[k].decode_base +
                    *(psrc) * map[k].decode_factor;
                temp *= 255;
                if (temp > 255) temp = 255;
                if (temp < 0) temp = 0;
                *curr_pos = (byte)temp;
            default:
                break;
            }
            curr_pos++;
            psrc++;
        }
    }
}

void applymap16(sample_map map[], const void *psrc_in, int spp, void *pdes,
    void *bufend)
{
    unsigned short *curr_pos = (unsigned short*)pdes;
    unsigned short *psrc = (unsigned short*)psrc_in;
    int k;
    float temp;

    while (curr_pos < (unsigned short*) bufend) {
        for (k = 0; k < spp; k++) {
            switch (map[k].decoding) {
            case sd_none:
                *curr_pos = *psrc;
                break;
            case sd_lookup:
                temp = map[k].decode_lookup[*(psrc) >> 4] * 65535.0;
                if (temp > 65535) temp = 65535;
                if (temp < 0) temp = 0;
                *curr_pos = (unsigned short)temp;
                break;
            case sd_compute:
                temp = map[k].decode_base +
                    *psrc * map[k].decode_factor;
                temp *= 65535;
                if (temp > 65535) temp = 65535;
                if (temp < 0) temp = 0;
                *curr_pos = (unsigned short)temp;
            default:
                break;
            }
            curr_pos++;
            psrc++;
        }
    }
}