Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/psi/zimage3.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* LanguageLevel 3 ImageTypes (3 & 4 - masked images) */
18
#include "memory_.h"
19
#include "ghost.h"
20
#include "oper.h"
21
#include "gscspace.h"   /* for gscolor2.h */
22
#include "gscolor2.h"
23
#include "gsiparm3.h"
24
#include "gsiparm4.h"
25
#include "gxiparam.h"   /* for image enumerator */
26
#include "idict.h"
27
#include "idparam.h"
28
#include "igstate.h"
29
#include "iimage.h"
30
#include "ialloc.h"
31
32
/* <dict> .image3 - */
33
static int
34
zimage3(i_ctx_t *i_ctx_p)
35
0
{
36
0
    os_ptr op = osp;
37
0
    gs_image3_t image;
38
0
    int interleave_type;
39
0
    ref *pDataDict;
40
0
    ref *pMaskDict;
41
0
    image_params ip_data, ip_mask;
42
0
    int ignored;
43
0
    int code, mcode;
44
45
0
    check_op(1);
46
0
    check_type(*op, t_dictionary);
47
0
    check_dict_read(*op);
48
0
    if ((code = dict_int_param(op, "InterleaveType", 1, 3, -1,
49
0
                               &interleave_type)) < 0
50
0
        )
51
0
        return code;
52
0
    gs_image3_t_init(&image, NULL, interleave_type);
53
0
    if (dict_find_string(op, "DataDict", &pDataDict) <= 0 ||
54
0
        dict_find_string(op, "MaskDict", &pMaskDict) <= 0
55
0
        )
56
0
        return_error(gs_error_rangecheck);
57
0
    check_type(*pDataDict, t_dictionary);
58
0
    check_type(*pMaskDict, t_dictionary);
59
0
    if ((code = pixel_image_params(i_ctx_p, pDataDict,
60
0
                        (gs_pixel_image_t *)&image, &ip_data,
61
0
                        12, gs_currentcolorspace(igs))) < 0 ||
62
0
        (mcode = code = data_image_params(imemory, pMaskDict, &image.MaskDict,
63
0
                                   &ip_mask, false, 1, 12, false)) < 0 ||
64
0
        (code = dict_int_param(pDataDict, "ImageType", 1, 1, 0, &ignored)) < 0 ||
65
0
        (code = dict_int_param(pMaskDict, "ImageType", 1, 1, 0, &ignored)) < 0
66
0
        )
67
0
        return code;
68
    /*
69
     * MaskDict must have a DataSource iff InterleaveType == 3.
70
     */
71
0
    if ((ip_data.MultipleDataSources && interleave_type != 3) ||
72
0
        ip_mask.MultipleDataSources ||
73
0
        mcode != (image.InterleaveType != 3)
74
0
        )
75
0
        return_error(gs_error_rangecheck);
76
0
    if (image.InterleaveType == 3) {
77
        /* Insert the mask DataSource before the data DataSources. */
78
0
        memmove(&ip_data.DataSource[1], &ip_data.DataSource[0],
79
0
                (countof(ip_data.DataSource) - 1) *
80
0
                sizeof(ip_data.DataSource[0]));
81
0
        ip_data.DataSource[0] = ip_mask.DataSource[0];
82
0
    }
83
    /* We never interpolate images with masks */
84
0
    image.Interpolate = 0;
85
0
    return zimage_setup(i_ctx_p, (gs_pixel_image_t *)&image,
86
0
                        &ip_data.DataSource[0],
87
0
                        image.CombineWithColor, 1);
88
0
}
89
90
/* <dict> .image4 - */
91
static int
92
zimage4(i_ctx_t *i_ctx_p)
93
0
{
94
0
    os_ptr op = osp;
95
0
    gs_image4_t image;
96
0
    image_params ip;
97
0
    int num_components =
98
0
        gs_color_space_num_components(gs_currentcolorspace(igs));
99
0
    int colors[countof(image.MaskColor)];
100
0
    int code;
101
0
    int i;
102
103
0
    check_op(1);
104
0
    gs_image4_t_init(&image, NULL);
105
0
    code = pixel_image_params(i_ctx_p, op, (gs_pixel_image_t *)&image, &ip,
106
0
                              12, gs_currentcolorspace(igs));
107
0
    if (code < 0)
108
0
        return code;
109
0
    code = dict_int_array_check_param(imemory, op, "MaskColor",
110
0
       num_components * 2, colors, 0, gs_error_rangecheck);
111
    /* Clamp the color values to the unsigned range. */
112
0
    if (code == num_components) {
113
0
        image.MaskColor_is_range = false;
114
0
        for (i = 0; i < code; ++i)
115
0
            image.MaskColor[i] = (colors[i] < 0 ? ~(uint)0 : colors[i]);
116
0
    }
117
0
    else if (code == num_components * 2) {
118
0
        image.MaskColor_is_range = true;
119
0
        for (i = 0; i < code; i += 2) {
120
0
            if (colors[i+1] < 0) /* no match possible */
121
0
                image.MaskColor[i] = 1, image.MaskColor[i+1] = 0;
122
0
            else {
123
0
                image.MaskColor[i+1] = colors[i+1];
124
0
                image.MaskColor[i] = max(colors[i], 0);
125
0
            }
126
0
        }
127
0
    } else
128
0
        return_error(code < 0 ? code : gs_note_error(gs_error_rangecheck));
129
0
    return zimage_setup(i_ctx_p, (gs_pixel_image_t *)&image, &ip.DataSource[0],
130
0
                        image.CombineWithColor, 1);
131
0
}
132
133
/* ------ Initialization procedure ------ */
134
135
const op_def zimage3_op_defs[] =
136
{
137
    op_def_begin_ll3(),
138
    {"1.image3", zimage3},
139
    {"1.image4", zimage4},
140
    op_def_end(0)
141
};