summaryrefslogtreecommitdiff
blob: 9df48630187c8107fd0c9fa20efa4ea173fea0c8 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Copyright (C) 2001-2019 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.
*/


/* Generic image support */
#include "memory_.h"
#include "gx.h"
#include "gscspace.h"
#include "gserrors.h"
#include "gsmatrix.h"
#include "gsutil.h"
#include "gxcolor2.h"		/* for lookup map */
#include "gxiparam.h"
#include "stream.h"

/* ---------------- Generic image support ---------------- */

/* Structure descriptors */
public_st_gs_image_common();
public_st_gs_data_image();
public_st_gs_pixel_image();

/* Initialize the common parts of image structures. */
void
gs_image_common_t_init(gs_image_common_t * pic)
{
    gs_make_identity(&pic->ImageMatrix);
}
void
gs_data_image_t_init(gs_data_image_t * pim, int num_components)
{
    int i;

    gs_image_common_t_init((gs_image_common_t *) pim);
    pim->Width = pim->Height = 0;
    pim->BitsPerComponent = 1;
    if (num_components >= 0) {
        for (i = 0; i < num_components * 2; i += 2)
            pim->Decode[i] = 0, pim->Decode[i + 1] = 1;
    } else {
        for (i = 0; i < num_components * -2; i += 2)
            pim->Decode[i] = 1, pim->Decode[i + 1] = 0;
    }
    pim->Interpolate = false;
}
void
gs_pixel_image_t_init(gs_pixel_image_t * pim,
                      gs_color_space * color_space)
{
    int num_components;

    if (color_space == 0 ||
        (num_components =
         gs_color_space_num_components(color_space)) < 0
        )
        num_components = 0;
    gs_data_image_t_init((gs_data_image_t *) pim, num_components);
    pim->format = gs_image_format_chunky;
    pim->ColorSpace = color_space;
    pim->CombineWithColor = false;
    pim->override_in_smask = 0;
}

/* Initialize the common part of an image-processing enumerator. */
int
gx_image_enum_common_init(gx_image_enum_common_t * piec,
                          const gs_data_image_t * pic,
                          const gx_image_enum_procs_t * piep,
                          gx_device * dev, int num_components,
                          gs_image_format_t format)
{
    int bpc = pic->BitsPerComponent;
    int i;

    piec->image_type = pic->type;
    piec->procs = piep;
    piec->dev = dev;
    piec->id = gs_next_ids(dev->memory, 1);
    piec->skipping = false;
    switch (format) {
        case gs_image_format_chunky:
            piec->num_planes = 1;
            piec->plane_depths[0] = bpc * num_components;
            break;
        case gs_image_format_component_planar:
            piec->num_planes = num_components;
            for (i = 0; i < num_components; ++i)
                piec->plane_depths[i] = bpc;
            break;
        case gs_image_format_bit_planar:
            piec->num_planes = bpc * num_components;
            for (i = 0; i < piec->num_planes; ++i)
                piec->plane_depths[i] = 1;
            break;
        default:
            return_error(gs_error_rangecheck);
    }
    for (i = 0; i < piec->num_planes; ++i)
        piec->plane_widths[i] = pic->Width;
    return 0;
}

/* Compute the source size of an ordinary image with explicit data. */
int
gx_data_image_source_size(const gs_gstate * pgs,
                          const gs_image_common_t * pim, gs_int_point * psize)
{
    const gs_data_image_t *pdi = (const gs_data_image_t *)pim;

    psize->x = pdi->Width;
    psize->y = pdi->Height;
    return 0;
}

/* Process the next piece of an image with no source data. */
/* This procedure should never be called. */
int
gx_no_plane_data(gx_image_enum_common_t * info,
                 const gx_image_plane_t * planes, int height,
                 int *height_used)
{
    return_error(gs_error_Fatal);
}

/* Clean up after processing an image with no source data. */
/* This procedure may be called, but should do nothing. */
int
gx_ignore_end_image(gx_image_enum_common_t * info, bool draw_last)
{
    return 0;
}

/* ---------------- Client procedures ---------------- */

int
gx_image_data(gx_image_enum_common_t * info, const byte ** plane_data,
              int data_x, uint raster, int height)
{
    int num_planes = info->num_planes;
    gx_image_plane_t planes[GS_IMAGE_MAX_COMPONENTS];
    int i;

#ifdef DEBUG
    if (num_planes > GS_IMAGE_MAX_COMPONENTS) {
        lprintf2("num_planes=%d > GS_IMAGE_MAX_COMPONENTS=%d!\n",
                 num_planes, GS_IMAGE_MAX_COMPONENTS);
        return_error(gs_error_Fatal);
    }
#endif
    for (i = 0; i < num_planes; ++i) {
        planes[i].data = plane_data[i];
        planes[i].data_x = data_x;
        planes[i].raster = raster;
    }
    return gx_image_plane_data(info, planes, height);
}

int
gx_image_plane_data(gx_image_enum_common_t * info,
                    const gx_image_plane_t * planes, int height)
{
    int ignore_rows_used;

    return gx_image_plane_data_rows(info, planes, height, &ignore_rows_used);
}

int
gx_image_plane_data_rows(gx_image_enum_common_t * info,
                         const gx_image_plane_t * planes, int height,
                         int *rows_used)
{
    return info->procs->plane_data(info, planes, height, rows_used);
}

int
gx_image_flush(gx_image_enum_common_t * info)
{
    int (*flush)(gx_image_enum_common_t *) = info->procs->flush;

    return (flush ? flush(info) : 0);
}

bool
gx_image_planes_wanted(const gx_image_enum_common_t *info, byte *wanted)
{
    bool (*planes_wanted)(const gx_image_enum_common_t *, byte *) =
        info->procs->planes_wanted;

    if (planes_wanted)
        return planes_wanted(info, wanted);
    else {
        memset(wanted, 0xff, info->num_planes);
        return true;
    }
}

int
gx_image_end(gx_image_enum_common_t * info, bool draw_last)
{
    return info->procs->end_image(info, draw_last);
}

/* ---------------- Serialization ---------------- */

/*
 * Define dummy sput/sget/release procedures for image types that don't
 * implement these functions.
 */

int
gx_image_no_sput(const gs_image_common_t *pic, stream *s,
                 const gs_color_space **ppcs)
{
    return_error(gs_error_rangecheck);
}

int
gx_image_no_sget(gs_image_common_t *pic, stream *s,
                 gs_color_space *pcs)
{
    return_error(gs_error_rangecheck);
}

void
gx_image_default_release(gs_image_common_t *pic, gs_memory_t *mem)
{
    gs_free_object(mem, pic, "gx_image_default_release");
}

#ifdef DEBUG
static void
debug_b_print_matrix(const gs_pixel_image_t *pim)
{
    if_debug6('b', "      ImageMatrix=[%g %g %g %g %g %g]\n",
              pim->ImageMatrix.xx, pim->ImageMatrix.xy,
              pim->ImageMatrix.yx, pim->ImageMatrix.yy,
              pim->ImageMatrix.tx, pim->ImageMatrix.ty);
}
static void
debug_b_print_decode(const gs_pixel_image_t *pim, int num_decode)
{
    if (gs_debug_c('b')) {
        const char *str = "      Decode=[";
        int i;

        for (i = 0; i < num_decode; str = " ", ++i)
            dprintf2("%s%g", str, pim->Decode[i]);
        dputs("]\n");
    }
}
#else
#  define debug_b_print_matrix(pim) DO_NOTHING
#  define debug_b_print_decode(pim, num_decode) DO_NOTHING
#endif

/* Test whether an image has a default ImageMatrix. */
bool
gx_image_matrix_is_default(const gs_data_image_t *pid)
{
    return (is_xxyy(&pid->ImageMatrix) &&
            pid->ImageMatrix.xx == pid->Width &&
            pid->ImageMatrix.yy == -pid->Height &&
            is_fzero(pid->ImageMatrix.tx) &&
            pid->ImageMatrix.ty == pid->Height);
}

/* Put a variable-length uint on a stream. */
void
sput_variable_uint(stream *s, uint w)
{
    for (; w > 0x7f; w >>= 7)
        sputc(s, (byte)(w | 0x80));
    sputc(s, (byte)w);
}

/*
 * Write generic pixel image parameters.  The format is the following,
 * encoded as a variable-length uint in the usual way:
 *	xxxFEDCCBBBBA
 *	    A = 0 if standard ImageMatrix, 1 if explicit ImageMatrix
 *	    BBBB = BitsPerComponent - 1
 *	    CC = format
 *	    D = 0 if standard (0..1) Decode, 1 if explicit Decode
 *	    E = Interpolate
 *	    F = CombineWithColor
 *	    xxx = extra information from caller
 */
#define PI_ImageMatrix 0x001
#define PI_BPC_SHIFT 1
#define PI_BPC_MASK 0xf
#define PI_FORMAT_SHIFT 5
#define PI_FORMAT_MASK 0x3
#define PI_Decode 0x080
#define PI_Interpolate 0x100
#define PI_CombineWithColor 0x200
#define PI_BITS 10
/*
 *	Width, encoded as a variable-length uint
 *	Height, encoded ditto
 *	ImageMatrix (if A = 1), per gs_matrix_store/fetch
 *	Decode (if D = 1): blocks of up to 4 components
 *	    aabbccdd, where each xx is decoded as:
 *		00 = default, 01 = swapped default,
 *		10 = (0,V), 11 = (U,V)
 *	    non-defaulted components (up to 8 floats)
 */
int
gx_pixel_image_sput(const gs_pixel_image_t *pim, stream *s,
                    const gs_color_space **ppcs, int extra)
{
    const gs_color_space *pcs = pim->ColorSpace;
    int bpc = pim->BitsPerComponent;
    int num_components = gs_color_space_num_components(pcs);
    int num_decode;
    uint control = extra << PI_BITS;
    float decode_default_1 = 1;
    int i;
    uint ignore;

    /* Construct the control word. */

    if (!gx_image_matrix_is_default((const gs_data_image_t *)pim))
        control |= PI_ImageMatrix;
    switch (pim->format) {
    case gs_image_format_chunky:
    case gs_image_format_component_planar:
        switch (bpc) {
        case 1: case 2: case 4: case 8: case 12: case 16: break;
        default: return_error(gs_error_rangecheck);
        }
        break;
    case gs_image_format_bit_planar:
        if (bpc < 1 || bpc > 8)
            return_error(gs_error_rangecheck);
    }
    control |= (bpc - 1) << PI_BPC_SHIFT;
    control |= pim->format << PI_FORMAT_SHIFT;
    num_decode = num_components * 2;
    if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed)
        decode_default_1 = (float)pcs->params.indexed.hival;
    for (i = 0; i < num_decode; ++i)
        if (pim->Decode[i] != DECODE_DEFAULT(i, decode_default_1)) {
            control |= PI_Decode;
            break;
        }
    if (pim->Interpolate)
        control |= PI_Interpolate;
    if (pim->CombineWithColor)
        control |= PI_CombineWithColor;

    /* Write the encoding on the stream. */

    if_debug3('b', "[b]put control=0x%x, Width=%d, Height=%d\n",
              control, pim->Width, pim->Height);
    sput_variable_uint(s, control);
    sput_variable_uint(s, (uint)pim->Width);
    sput_variable_uint(s, (uint)pim->Height);
    if (control & PI_ImageMatrix) {
        debug_b_print_matrix(pim);
        sput_matrix(s, &pim->ImageMatrix);
    }
    if (control & PI_Decode) {
        int i;
        uint dflags = 1;
        float decode[8];
        int di = 0;

        debug_b_print_decode(pim, num_decode);
        for (i = 0; i < num_decode; i += 2) {
            float u = pim->Decode[i], v = pim->Decode[i + 1];
            float dv = DECODE_DEFAULT(i + 1, decode_default_1);

            if (dflags >= 0x100) {
                sputc(s, (byte)(dflags & 0xff));
                sputs(s, (const byte *)decode, di * sizeof(float), &ignore);
                dflags = 1;
                di = 0;
            }
            dflags <<= 2;
            if (u == 0 && v == dv)
                DO_NOTHING;
            else if (u == dv && v == 0)
                dflags += 1;
            else {
                if (u != 0) {
                    dflags++;
                    decode[di++] = u;
                }
                dflags += 2;
                decode[di++] = v;
            }
        }
        sputc(s, (byte)((dflags << (8 - num_decode)) & 0xff));
        sputs(s, (const byte *)decode, di * sizeof(float), &ignore);
    }
    *ppcs = pcs;
    return 0;
}

/* Set an image's ImageMatrix to the default. */
void
gx_image_matrix_set_default(gs_data_image_t *pid)
{
    pid->ImageMatrix.xx = (float)pid->Width;
    pid->ImageMatrix.xy = 0;
    pid->ImageMatrix.yx = 0;
    pid->ImageMatrix.yy = (float)-pid->Height;
    pid->ImageMatrix.tx = 0;
    pid->ImageMatrix.ty = (float)pid->Height;
}

/* Get a variable-length uint from a stream. */
int
sget_variable_uint(stream *s, uint *pw)
{
    uint w = 0;
    int shift = 0;
    int ch;

    for (; (ch = sgetc(s)) >= 0x80; shift += 7)
        w += (ch & 0x7f) << shift;
    if (ch < 0)
        return_error(gs_error_ioerror);
    *pw = w + (ch << shift);
    return 0;
}

/*
 * Read generic pixel image parameters.
 */
int
gx_pixel_image_sget(gs_pixel_image_t *pim, stream *s,
                    gs_color_space *pcs)
{
    uint control;
    float decode_default_1 = 1;
    int num_components, num_decode;
    int i;
    int code;
    uint ignore;

    if ((code = sget_variable_uint(s, &control)) < 0 ||
        (code = sget_variable_uint(s, (uint *)&pim->Width)) < 0 ||
        (code = sget_variable_uint(s, (uint *)&pim->Height)) < 0
        )
        return code;
    if_debug3('b', "[b]get control=0x%x, Width=%d, Height=%d\n",
              control, pim->Width, pim->Height);
    if (control & PI_ImageMatrix) {
        if ((code = sget_matrix(s, &pim->ImageMatrix)) < 0)
            return code;
        debug_b_print_matrix(pim);
    } else
        gx_image_matrix_set_default((gs_data_image_t *)pim);
    pim->BitsPerComponent = ((control >> PI_BPC_SHIFT) & PI_BPC_MASK) + 1;
    pim->format = (control >> PI_FORMAT_SHIFT) & PI_FORMAT_MASK;
    pim->ColorSpace = pcs;
    num_components = gs_color_space_num_components(pcs);
    num_decode = num_components * 2;
    if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed)
        decode_default_1 = (float)pcs->params.indexed.hival;
    if (control & PI_Decode) {
        uint dflags = 0x10000;
        float *dp = pim->Decode;

        for (i = 0; i < num_decode; i += 2, dp += 2, dflags <<= 2) {
            if (dflags >= 0x10000) {
                dflags = sgetc(s) + 0x100;
                if (dflags < 0x100)
                    return_error(gs_error_ioerror);
            }
            switch (dflags & 0xc0) {
            case 0x00:
                dp[0] = 0, dp[1] = DECODE_DEFAULT(i + 1, decode_default_1);
                break;
            case 0x40:
                dp[0] = DECODE_DEFAULT(i + 1, decode_default_1), dp[1] = 0;
                break;
            case 0x80:
                dp[0] = 0;
                if (sgets(s, (byte *)(dp + 1), sizeof(float), &ignore) < 0)
                    return_error(gs_error_ioerror);
                break;
            case 0xc0:
                if (sgets(s, (byte *)dp, sizeof(float) * 2, &ignore) < 0)
                    return_error(gs_error_ioerror);
                break;
            }
        }
        debug_b_print_decode(pim, num_decode);
    } else {
        for (i = 0; i < num_decode; ++i)
            pim->Decode[i] = DECODE_DEFAULT(i, decode_default_1);
    }
    pim->Interpolate = (control & PI_Interpolate) != 0;
    pim->CombineWithColor = (control & PI_CombineWithColor) != 0;
    return control >> PI_BITS;
}

/*
 * Release a pixel image object.  Currently this just frees the object.
 */
void
gx_pixel_image_release(gs_pixel_image_t *pic, gs_memory_t *mem)
{
    gx_image_default_release((gs_image_common_t *)pic, mem);
}