Coverage Report

Created: 2025-06-10 07:17

/src/ghostpdl/psi/zmatrix.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
/* Matrix operators */
18
#include "ghost.h"
19
#include "oper.h"
20
#include "igstate.h"
21
#include "gsmatrix.h"
22
#include "gscoord.h"
23
#include "store.h"
24
25
/* Forward references */
26
static int common_transform(i_ctx_t *,
27
                int (*)(gs_gstate *, double, double, gs_point *),
28
                int (*)(double, double, const gs_matrix *, gs_point *));
29
30
/* - initmatrix - */
31
static int
32
zinitmatrix(i_ctx_t *i_ctx_p)
33
35.0k
{
34
35.0k
    return gs_initmatrix(igs);
35
35.0k
}
36
37
/* <matrix> defaultmatrix <matrix> */
38
static int
39
zdefaultmatrix(i_ctx_t *i_ctx_p)
40
9.56k
{
41
9.56k
    os_ptr op = osp;
42
9.56k
    gs_matrix mat;
43
44
9.56k
    check_op(1);
45
9.56k
    gs_defaultmatrix(igs, &mat);
46
9.56k
    return write_matrix(op, &mat);
47
9.56k
}
48
49
/* - .currentmatrix <xx> <xy> <yx> <yy> <tx> <ty> */
50
static int
51
zcurrentmatrix(i_ctx_t *i_ctx_p)
52
38.6k
{
53
38.6k
    os_ptr op = osp;
54
38.6k
    gs_matrix mat;
55
38.6k
    int code = gs_currentmatrix(igs, &mat);
56
57
38.6k
    if (code < 0)
58
0
        return code;
59
38.6k
    push(6);
60
38.6k
    code = make_floats(op - 5, &mat.xx, 6);
61
38.6k
    if (code < 0)
62
0
        pop(6);
63
38.6k
    return code;
64
38.6k
}
65
66
/* <xx> <xy> <yx> <yy> <tx> <ty> .setmatrix - */
67
static int
68
zsetmatrix(i_ctx_t *i_ctx_p)
69
3.54k
{
70
3.54k
    os_ptr op = osp;
71
3.54k
    gs_matrix mat;
72
3.54k
    int code;
73
74
3.54k
    check_op(6);
75
3.54k
    code = float_params(op, 6, &mat.xx);
76
77
3.54k
    if (code < 0)
78
0
        return code;
79
3.54k
    if ((code = gs_setmatrix(igs, &mat)) < 0)
80
0
        return code;
81
3.54k
    pop(6);
82
3.54k
    return 0;
83
3.54k
}
84
85
/* <matrix|null> .setdefaultmatrix - */
86
static int
87
zsetdefaultmatrix(i_ctx_t *i_ctx_p)
88
34.9k
{
89
34.9k
    os_ptr op = osp;
90
34.9k
    int code;
91
92
34.9k
    check_op(1);
93
34.9k
    if (r_has_type(op, t_null))
94
0
        code = gs_setdefaultmatrix(igs, NULL);
95
34.9k
    else {
96
34.9k
        gs_matrix mat;
97
98
34.9k
        code = read_matrix(imemory, op, &mat);
99
34.9k
        if (code < 0)
100
0
            return code;
101
34.9k
        code = gs_setdefaultmatrix(igs, &mat);
102
34.9k
    }
103
34.9k
    if (code < 0)
104
0
        return code;
105
34.9k
    pop(1);
106
34.9k
    return 0;
107
34.9k
}
108
109
/* <tx> <ty> translate - */
110
/* <tx> <ty> <matrix> translate <matrix> */
111
static int
112
ztranslate(i_ctx_t *i_ctx_p)
113
121k
{
114
121k
    os_ptr op = osp;
115
121k
    int code;
116
121k
    double trans[2];
117
118
121k
    if ((code = num_params(op, 2, trans)) >= 0) {
119
121k
        code = gs_translate(igs, trans[0], trans[1]);
120
121k
        if (code < 0)
121
0
            return code;
122
121k
    } else {     /* matrix operand */
123
76
        gs_matrix mat;
124
125
        /* The num_params failure might be a stack underflow. */
126
76
        check_op(2);
127
74
        if ((code = num_params(op - 1, 2, trans)) < 0 ||
128
74
            (code = gs_make_translation(trans[0], trans[1], &mat)) < 0 ||
129
74
            (code = write_matrix(op, &mat)) < 0
130
74
            ) {     /* Might be a stack underflow. */
131
1
            check_op(3);
132
1
            return code;
133
1
        }
134
73
        op[-2] = *op;
135
73
    }
136
121k
    pop(2);
137
121k
    return code;
138
121k
}
139
140
/* <sx> <sy> scale - */
141
/* <sx> <sy> <matrix> scale <matrix> */
142
static int
143
zscale(i_ctx_t *i_ctx_p)
144
1.78k
{
145
1.78k
    os_ptr op = osp;
146
1.78k
    int code;
147
1.78k
    double scale[2];
148
149
1.78k
    if ((code = num_params(op, 2, scale)) >= 0) {
150
1.77k
        code = gs_scale(igs, scale[0], scale[1]);
151
1.77k
        if (code < 0)
152
0
            return code;
153
1.77k
    } else {     /* matrix operand */
154
18
        gs_matrix mat;
155
156
        /* The num_params failure might be a stack underflow. */
157
18
        check_op(2);
158
17
        if ((code = num_params(op - 1, 2, scale)) < 0 ||
159
17
            (code = gs_make_scaling(scale[0], scale[1], &mat)) < 0 ||
160
17
            (code = write_matrix(op, &mat)) < 0
161
17
            ) {     /* Might be a stack underflow. */
162
5
            check_op(3);
163
1
            return code;
164
5
        }
165
12
        op[-2] = *op;
166
12
    }
167
1.78k
    pop(2);
168
1.78k
    return code;
169
1.78k
}
170
171
/* <angle> rotate - */
172
/* <angle> <matrix> rotate <matrix> */
173
static int
174
zrotate(i_ctx_t *i_ctx_p)
175
110k
{
176
110k
    os_ptr op = osp;
177
110k
    int code;
178
110k
    double ang;
179
180
110k
    if ((code = real_param(op, &ang)) >= 0) {
181
110k
        code = gs_rotate(igs, ang);
182
110k
        if (code < 0)
183
0
            return code;
184
110k
    } else {     /* matrix operand */
185
332
        gs_matrix mat;
186
187
        /* The num_params failure might be a stack underflow. */
188
332
        check_op(1);
189
331
        if ((code = num_params(op - 1, 1, &ang)) < 0 ||
190
331
            (code = gs_make_rotation(ang, &mat)) < 0 ||
191
331
            (code = write_matrix(op, &mat)) < 0
192
331
            ) {     /* Might be a stack underflow. */
193
0
            check_op(2);
194
0
            return code;
195
0
        }
196
331
        op[-1] = *op;
197
331
    }
198
110k
    pop(1);
199
110k
    return code;
200
110k
}
201
202
/* <matrix> concat - */
203
static int
204
zconcat(i_ctx_t *i_ctx_p)
205
38.4k
{
206
38.4k
    os_ptr op = osp;
207
38.4k
    gs_matrix mat;
208
38.4k
    int code;
209
210
38.4k
    check_op(1);
211
38.4k
    code = read_matrix(imemory, op, &mat);
212
213
38.4k
    if (code < 0)
214
1
        return code;
215
38.4k
    code = gs_concat(igs, &mat);
216
38.4k
    if (code < 0)
217
0
        return code;
218
38.4k
    pop(1);
219
38.4k
    return 0;
220
38.4k
}
221
222
/* <matrix1> <matrix2> <matrix> concatmatrix <matrix> */
223
static int
224
zconcatmatrix(i_ctx_t *i_ctx_p)
225
1.06k
{
226
1.06k
    os_ptr op = osp;
227
1.06k
    gs_matrix m1, m2, mp;
228
1.06k
    int code;
229
230
1.06k
    check_op(3);
231
1.06k
    if ((code = read_matrix(imemory, op - 2, &m1)) < 0 ||
232
1.06k
        (code = read_matrix(imemory, op - 1, &m2)) < 0 ||
233
1.06k
        (code = gs_matrix_multiply(&m1, &m2, &mp)) < 0 ||
234
1.06k
        (code = write_matrix(op, &mp)) < 0
235
1.06k
        )
236
0
        return code;
237
1.06k
    op[-2] = *op;
238
1.06k
    pop(2);
239
1.06k
    return code;
240
1.06k
}
241
242
/* <x> <y> transform <xt> <yt> */
243
/* <x> <y> <matrix> transform <xt> <yt> */
244
static int
245
ztransform(i_ctx_t *i_ctx_p)
246
95.2k
{
247
95.2k
    return common_transform(i_ctx_p, gs_transform, gs_point_transform);
248
95.2k
}
249
250
/* <dx> <dy> dtransform <dxt> <dyt> */
251
/* <dx> <dy> <matrix> dtransform <dxt> <dyt> */
252
static int
253
zdtransform(i_ctx_t *i_ctx_p)
254
19.6k
{
255
19.6k
    return common_transform(i_ctx_p, gs_dtransform, gs_distance_transform);
256
19.6k
}
257
258
/* <xt> <yt> itransform <x> <y> */
259
/* <xt> <yt> <matrix> itransform <x> <y> */
260
static int
261
zitransform(i_ctx_t *i_ctx_p)
262
5
{
263
5
    return common_transform(i_ctx_p, gs_itransform, gs_point_transform_inverse);
264
5
}
265
266
/* <dxt> <dyt> idtransform <dx> <dy> */
267
/* <dxt> <dyt> <matrix> idtransform <dx> <dy> */
268
static int
269
zidtransform(i_ctx_t *i_ctx_p)
270
34.9k
{
271
34.9k
    return common_transform(i_ctx_p, gs_idtransform, gs_distance_transform_inverse);
272
34.9k
}
273
274
/* Common logic for [i][d]transform */
275
static int
276
common_transform(i_ctx_t *i_ctx_p,
277
        int (*ptproc)(gs_gstate *, double, double, gs_point *),
278
        int (*matproc)(double, double, const gs_matrix *, gs_point *))
279
149k
{
280
149k
    os_ptr op = osp;
281
149k
    double opxy[2];
282
149k
    gs_point pt;
283
149k
    int code;
284
285
    /* Optimize for the non-matrix case */
286
149k
    switch (r_type(op)) {
287
34.9k
        case t_real:
288
34.9k
            opxy[1] = op->value.realval;
289
34.9k
            break;
290
9.59k
        case t_integer:
291
9.59k
            opxy[1] = (double)op->value.intval;
292
9.59k
            break;
293
105k
        case t_array:   /* might be a matrix */
294
105k
        case t_shortarray:
295
105k
        case t_mixedarray: {
296
105k
            gs_matrix mat;
297
105k
            gs_matrix *pmat = &mat;
298
299
105k
            if ((code = read_matrix(imemory, op, pmat)) < 0 ||
300
105k
                (code = num_params(op - 1, 2, opxy)) < 0 ||
301
105k
                (code = (*matproc) (opxy[0], opxy[1], pmat, &pt)) < 0
302
105k
                ) {   /* Might be a stack underflow. */
303
0
                check_op(3);
304
0
                return code;
305
0
            }
306
105k
            op--;
307
105k
            pop(1);
308
105k
            goto out;
309
105k
        }
310
4
        default:
311
4
            return_op_typecheck(op);
312
149k
    }
313
44.5k
    switch (r_type(op - 1)) {
314
34.9k
        case t_real:
315
34.9k
            opxy[0] = (op - 1)->value.realval;
316
34.9k
            break;
317
9.59k
        case t_integer:
318
9.59k
            opxy[0] = (double)(op - 1)->value.intval;
319
9.59k
            break;
320
2
        default:
321
2
            return_op_typecheck(op - 1);
322
44.5k
    }
323
44.5k
    if ((code = (*ptproc) (igs, opxy[0], opxy[1], &pt)) < 0)
324
0
        return code;
325
149k
out:
326
149k
    make_real(op - 1, pt.x);
327
149k
    make_real(op, pt.y);
328
149k
    return 0;
329
44.5k
}
330
331
/* <matrix> <inv_matrix> invertmatrix <inv_matrix> */
332
static int
333
zinvertmatrix(i_ctx_t *i_ctx_p)
334
1
{
335
1
    os_ptr op = osp;
336
1
    gs_matrix m;
337
1
    int code;
338
339
1
    check_op(2);
340
0
    if ((code = read_matrix(imemory, op - 1, &m)) < 0 ||
341
0
        (code = gs_matrix_invert(&m, &m)) < 0 ||
342
0
        (code = write_matrix(op, &m)) < 0
343
0
        )
344
0
        return code;
345
0
    op[-1] = *op;
346
0
    pop(1);
347
0
    return code;
348
0
}
349
350
static int
351
zupdatematrices(i_ctx_t *i_ctx_p)
352
104k
{
353
104k
    return gs_updatematrices(igs);
354
104k
}
355
356
/* ------ Initialization procedure ------ */
357
358
const op_def zmatrix_op_defs[] =
359
{
360
    {"1concat", zconcat},
361
    {"2dtransform", zdtransform},
362
    {"3concatmatrix", zconcatmatrix},
363
    {"0.currentmatrix", zcurrentmatrix},
364
    {"1defaultmatrix", zdefaultmatrix},
365
    {"2idtransform", zidtransform},
366
    {"0initmatrix", zinitmatrix},
367
    {"2invertmatrix", zinvertmatrix},
368
    {"2itransform", zitransform},
369
    {"1rotate", zrotate},
370
    {"2scale", zscale},
371
    {"6.setmatrix", zsetmatrix},
372
    {"1.setdefaultmatrix", zsetdefaultmatrix},
373
    {"2transform", ztransform},
374
    {"2translate", ztranslate},
375
    op_def_end(0)
376
};
377
378
const op_def zmatrix2_op_defs[] =
379
{
380
    {"1.updatematrices", zupdatematrices},
381
    op_def_end(0)
382
};