summaryrefslogtreecommitdiff
blob: 4bd9ddb21785288e4d146b35b0cca1d025a3ef04 (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
/* 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.
*/


/* ImageType 4 image implementation */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gscspace.h"
#include "gsiparm4.h"
#include "gxiparam.h"
#include "gximage.h"
#include "stream.h"

/* Forward references */
static dev_proc_begin_typed_image(gx_begin_image4);

/* Structure descriptor */
private_st_gs_image4();

/* Define the image type for ImageType 4 images. */
static image_proc_sput(gx_image4_sput);
static image_proc_sget(gx_image4_sget);
static image_proc_release(gx_image4_release);
const gx_image_type_t gs_image_type_4 = {
    &st_gs_image4, gx_begin_image4, gx_data_image_source_size,
    gx_image4_sput, gx_image4_sget, gx_image4_release, 4
};
/*
 * The implementation is shared with ImageType 1, so we don't need our own
 * enum_procs.
 */
/*
  static const gx_image_enum_procs_t image4_enum_procs = {
    gx_image1_plane_data, gx_image1_end_image
  };
*/

/* Initialize an ImageType 4 image. */
void
gs_image4_t_init(gs_image4_t * pim, gs_color_space * color_space)
{
    gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space);
    pim->type = &gs_image_type_4;
    pim->MaskColor_is_range = false;
    pim->image_parent_type = gs_image_type4;
}

/* Start processing an ImageType 4 image. */
static int
gx_begin_image4(gx_device * dev,
                const gs_gstate * pgs, const gs_matrix * pmat,
                const gs_image_common_t * pic, const gs_int_rect * prect,
                const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
                gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
{
    gx_image_enum *penum;
    const gs_image4_t *pim = (const gs_image4_t *)pic;
    int code = gx_image_enum_alloc(pic, prect, mem, &penum);

    if (code < 0)
        return code;
    penum->alpha = gs_image_alpha_none;
    penum->masked = false;
    penum->adjust = fixed_0;
    penum->image_parent_type = gs_image_type4;
    /* Check that MaskColor values are within the valid range. */
    {
        bool opaque = false;
        uint max_value = (1 << pim->BitsPerComponent) - 1;
        int spp = cs_num_components(pim->ColorSpace);
        int i;

        for (i = 0; i < spp * 2; i += 2) {
            uint c0, c1;

            if (pim->MaskColor_is_range)
                c0 = pim->MaskColor[i], c1 = pim->MaskColor[i + 1];
            else
                c0 = c1 = pim->MaskColor[i >> 1];

            if ((c0 | c1) > max_value) {
                gs_free_object(mem, penum, "gx_begin_image4");
                return_error(gs_error_rangecheck);
            }
            if (c0 > c1) {
                opaque = true;	/* pixel can never match mask color */
                break;
            }
            penum->mask_color.values[i] = c0;
            penum->mask_color.values[i + 1] = c1;
        }
        penum->use_mask_color = !opaque;
    }
    code = gx_image_enum_begin(dev, pgs, pmat, pic, pdcolor, pcpath, mem,
                               penum);
    if (code >= 0)
        *pinfo = (gx_image_enum_common_t *)penum;
    else {
        *pinfo = NULL;
    }
    return code;
}

/* Serialization */

static int
gx_image4_sput(const gs_image_common_t *pic, stream *s,
               const gs_color_space **ppcs)
{
    const gs_image4_t *pim = (const gs_image4_t *)pic;
    bool is_range = pim->MaskColor_is_range;
    int code = gx_pixel_image_sput((const gs_pixel_image_t *)pim, s, ppcs,
                                   is_range);
    int num_values =
        gs_color_space_num_components(pim->ColorSpace) * (is_range ? 2 : 1);
    int i;

    if (code < 0)
        return code;
    for (i = 0; i < num_values; ++i)
        sput_variable_uint(s, pim->MaskColor[i]);
    *ppcs = pim->ColorSpace;
    return 0;
}

static int
gx_image4_sget(gs_image_common_t *pic, stream *s,
               gs_color_space *pcs)
{
    gs_image4_t *const pim = (gs_image4_t *)pic;
    int num_values;
    int i;
    int code = gx_pixel_image_sget((gs_pixel_image_t *)pim, s, pcs);

    if (code < 0)
        return code;
    pim->type = &gs_image_type_4;
    pim->MaskColor_is_range = code;
    num_values =
        gs_color_space_num_components(pcs) *
        (pim->MaskColor_is_range ? 2 : 1);
    for (i = 0; i < num_values; ++i) {
        code = sget_variable_uint(s, &pim->MaskColor[i]);
        if (code < 0)
            return code;
    }
    pim->image_parent_type = gs_image_type4;
    return 0;
}

static void
gx_image4_release(gs_image_common_t *pic, gs_memory_t *mem)
{
    gx_pixel_image_release((gs_pixel_image_t *)pic, mem);
}