Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gsmatrix.c
Line
Count
Source
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 for Ghostscript library */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gxfarith.h"
23
#include "gxfixed.h"
24
#include "gxmatrix.h"
25
#include "stream.h"
26
27
/* The identity matrix */
28
static const gs_matrix gs_identity_matrix =
29
{identity_matrix_body};
30
31
/* ------ Matrix creation ------ */
32
33
/* Create an identity matrix */
34
void
35
gs_make_identity(gs_matrix * pmat)
36
78.6M
{
37
78.6M
    *pmat = gs_identity_matrix;
38
78.6M
}
39
40
/* Create a translation matrix */
41
int
42
gs_make_translation(double dx, double dy, gs_matrix * pmat)
43
1.47M
{
44
1.47M
    *pmat = gs_identity_matrix;
45
1.47M
    pmat->tx = dx;
46
1.47M
    pmat->ty = dy;
47
1.47M
    return 0;
48
1.47M
}
49
50
/* Create a scaling matrix */
51
int
52
gs_make_scaling(double sx, double sy, gs_matrix * pmat)
53
5.90M
{
54
5.90M
    *pmat = gs_identity_matrix;
55
5.90M
    pmat->xx = sx;
56
5.90M
    pmat->yy = sy;
57
5.90M
    return 0;
58
5.90M
}
59
60
/* Create a rotation matrix. */
61
/* The angle is in degrees. */
62
int
63
gs_make_rotation(double ang, gs_matrix * pmat)
64
1.52M
{
65
1.52M
    gs_sincos_t sincos;
66
67
1.52M
    gs_sincos_degrees(ang, &sincos);
68
1.52M
    pmat->yy = pmat->xx = sincos.cos;
69
1.52M
    pmat->xy = sincos.sin;
70
1.52M
    pmat->yx = -sincos.sin;
71
1.52M
    pmat->tx = pmat->ty = 0.0;
72
1.52M
    return 0;
73
1.52M
}
74
75
/* ------ Matrix arithmetic ------ */
76
77
/* Multiply two matrices.  We should check for floating exceptions, */
78
/* but for the moment it's just too awkward. */
79
/* Since this is used heavily, we check for shortcuts. */
80
int
81
gs_matrix_multiply(const gs_matrix * pm1, const gs_matrix * pm2, gs_matrix * pmr)
82
53.9M
{
83
53.9M
    double xx1 = pm1->xx, yy1 = pm1->yy;
84
53.9M
    double tx1 = pm1->tx, ty1 = pm1->ty;
85
53.9M
    double xx2 = pm2->xx, yy2 = pm2->yy;
86
53.9M
    double xy2 = pm2->xy, yx2 = pm2->yx;
87
88
53.9M
    if (is_xxyy(pm1)) {
89
52.1M
        pmr->tx = tx1 * xx2 + pm2->tx;
90
52.1M
        pmr->ty = ty1 * yy2 + pm2->ty;
91
52.1M
        if (is_fzero(xy2))
92
50.1M
            pmr->xy = 0;
93
1.99M
        else
94
1.99M
            pmr->xy = xx1 * xy2,
95
1.99M
                pmr->ty += tx1 * xy2;
96
52.1M
        pmr->xx = xx1 * xx2;
97
52.1M
        if (is_fzero(yx2))
98
50.0M
            pmr->yx = 0;
99
2.06M
        else
100
2.06M
            pmr->yx = yy1 * yx2,
101
2.06M
                pmr->tx += ty1 * yx2;
102
52.1M
        pmr->yy = yy1 * yy2;
103
52.1M
    } else {
104
1.80M
        double xy1 = pm1->xy, yx1 = pm1->yx;
105
106
1.80M
        pmr->xx = xx1 * xx2 + xy1 * yx2;
107
1.80M
        pmr->xy = xx1 * xy2 + xy1 * yy2;
108
1.80M
        pmr->yy = yx1 * xy2 + yy1 * yy2;
109
1.80M
        pmr->yx = yx1 * xx2 + yy1 * yx2;
110
1.80M
        pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
111
1.80M
        pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
112
1.80M
    }
113
53.9M
    return 0;
114
53.9M
}
115
int
116
gs_matrix_multiply_double(const gs_matrix_double * pm1, const gs_matrix * pm2, gs_matrix_double * pmr)
117
1.53M
{
118
1.53M
    double xx1 = pm1->xx, yy1 = pm1->yy;
119
1.53M
    double tx1 = pm1->tx, ty1 = pm1->ty;
120
1.53M
    double xx2 = pm2->xx, yy2 = pm2->yy;
121
1.53M
    double xy2 = pm2->xy, yx2 = pm2->yx;
122
123
1.53M
    if (is_xxyy(pm1)) {
124
1.46M
        pmr->tx = tx1 * xx2 + pm2->tx;
125
1.46M
        pmr->ty = ty1 * yy2 + pm2->ty;
126
1.46M
        if (is_fzero(xy2))
127
1.16M
            pmr->xy = 0;
128
297k
        else
129
297k
            pmr->xy = xx1 * xy2,
130
297k
                pmr->ty += tx1 * xy2;
131
1.46M
        pmr->xx = xx1 * xx2;
132
1.46M
        if (is_fzero(yx2))
133
1.15M
            pmr->yx = 0;
134
310k
        else
135
310k
            pmr->yx = yy1 * yx2,
136
310k
                pmr->tx += ty1 * yx2;
137
1.46M
        pmr->yy = yy1 * yy2;
138
1.46M
    } else {
139
71.4k
        double xy1 = pm1->xy, yx1 = pm1->yx;
140
141
71.4k
        pmr->xx = xx1 * xx2 + xy1 * yx2;
142
71.4k
        pmr->xy = xx1 * xy2 + xy1 * yy2;
143
71.4k
        pmr->yy = yx1 * xy2 + yy1 * yy2;
144
71.4k
        pmr->yx = yx1 * xx2 + yy1 * yx2;
145
71.4k
        pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
146
71.4k
        pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
147
71.4k
    }
148
1.53M
    return 0;
149
1.53M
}
150
151
/* Invert a matrix.  Return gs_error_undefinedresult if not invertible. */
152
int
153
gs_matrix_invert(const gs_matrix * pm, gs_matrix * pmr)
154
32.1M
{       /* We have to be careful about fetch/store order, */
155
    /* because pm might be the same as pmr. */
156
32.1M
    if (is_xxyy(pm)) {
157
21.7M
        if (is_fzero(pm->xx) || is_fzero(pm->yy))
158
24.8k
            return_error(gs_error_undefinedresult);
159
21.7M
        pmr->tx = -(pmr->xx = 1.0 / pm->xx) * pm->tx;
160
21.7M
        pmr->xy = 0.0;
161
21.7M
        pmr->yx = 0.0;
162
21.7M
        pmr->ty = -(pmr->yy = 1.0 / pm->yy) * pm->ty;
163
21.7M
    } else {
164
10.4M
        float mxx = pm->xx, myy = pm->yy, mxy = pm->xy, myx = pm->yx;
165
10.4M
        float mtx = pm->tx, mty = pm->ty;
166
        /* we declare det as double since on at least some computer (i.e. peeves)
167
           declaring it as a float results in different values for pmr depending
168
           on whether or not optimization is turned on.  I believe this is caused
169
           by the compiler keeping the det value in an internal register when
170
           optimization is enable.  As evidence of this if you add a debugging
171
           statement to print out det the optimized code acts the same as the
172
           unoptimized code.  declearing det as double does not change the CET 10-09.ps
173
           output. */
174
10.4M
        double det = (float)(mxx * myy) - (float)(mxy * myx);
175
176
        /*
177
         * We are doing the math as floats instead of doubles to reproduce
178
         * the results in page 1 of CET 10-09.ps
179
         */
180
10.4M
        if (det == 0)
181
7.47k
            return_error(gs_error_undefinedresult);
182
10.4M
        pmr->xx = myy / det;
183
10.4M
        pmr->xy = -mxy / det;
184
10.4M
        pmr->yx = -myx / det;
185
10.4M
        pmr->yy = mxx / det;
186
10.4M
        pmr->tx = (((float)(mty * myx) - (float)(mtx * myy))) / det;
187
10.4M
        pmr->ty = (((float)(mtx * mxy) - (float)(mty * mxx))) / det;
188
10.4M
    }
189
32.1M
    return 0;
190
32.1M
}
191
int
192
gs_matrix_invert_to_double(const gs_matrix * pm, gs_matrix_double * pmr)
193
1.53M
{       /* We have to be careful about fetch/store order, */
194
    /* because pm might be the same as pmr. */
195
1.53M
    if (is_xxyy(pm)) {
196
1.46M
        if (is_fzero(pm->xx) || is_fzero(pm->yy))
197
112
            return_error(gs_error_undefinedresult);
198
1.46M
        pmr->tx = -(pmr->xx = 1.0 / pm->xx) * pm->tx;
199
1.46M
        pmr->xy = 0.0;
200
1.46M
        pmr->yx = 0.0;
201
1.46M
        pmr->ty = -(pmr->yy = 1.0 / pm->yy) * pm->ty;
202
1.46M
    } else {
203
71.4k
        double mxx = pm->xx, myy = pm->yy, mxy = pm->xy, myx = pm->yx;
204
71.4k
        double mtx = pm->tx, mty = pm->ty;
205
71.4k
        double det = (mxx * myy) - (mxy * myx);
206
207
        /*
208
         * We are doing the math as floats instead of doubles to reproduce
209
         * the results in page 1 of CET 10-09.ps
210
         */
211
71.4k
        if (det == 0)
212
5
            return_error(gs_error_undefinedresult);
213
71.4k
        pmr->xx = myy / det;
214
71.4k
        pmr->xy = -mxy / det;
215
71.4k
        pmr->yx = -myx / det;
216
71.4k
        pmr->yy = mxx / det;
217
71.4k
        pmr->tx = (((mty * myx) - (mtx * myy))) / det;
218
71.4k
        pmr->ty = (((mtx * mxy) - (mty * mxx))) / det;
219
71.4k
    }
220
1.53M
    return 0;
221
1.53M
}
222
223
/* Translate a matrix, possibly in place. */
224
int
225
gs_matrix_translate(const gs_matrix * pm, double dx, double dy, gs_matrix * pmr)
226
1.87M
{
227
1.87M
    gs_point trans;
228
1.87M
    int code = gs_distance_transform(dx, dy, pm, &trans);
229
230
1.87M
    if (code < 0)
231
0
        return code;
232
1.87M
    if (pmr != pm)
233
58.4k
        *pmr = *pm;
234
1.87M
    pmr->tx += trans.x;
235
1.87M
    pmr->ty += trans.y;
236
1.87M
    return 0;
237
1.87M
}
238
239
/* Scale a matrix, possibly in place. */
240
int
241
gs_matrix_scale(const gs_matrix * pm, double sx, double sy, gs_matrix * pmr)
242
4.05M
{
243
4.05M
    pmr->xx = pm->xx * sx;
244
4.05M
    pmr->xy = pm->xy * sx;
245
4.05M
    pmr->yx = pm->yx * sy;
246
4.05M
    pmr->yy = pm->yy * sy;
247
4.05M
    if (pmr != pm) {
248
117k
        pmr->tx = pm->tx;
249
117k
        pmr->ty = pm->ty;
250
117k
    }
251
4.05M
    return 0;
252
4.05M
}
253
254
/* Rotate a matrix, possibly in place.  The angle is in degrees. */
255
int
256
gs_matrix_rotate(const gs_matrix * pm, double ang, gs_matrix * pmr)
257
9.21M
{
258
9.21M
    double mxx, mxy;
259
9.21M
    gs_sincos_t sincos;
260
261
9.21M
    gs_sincos_degrees(ang, &sincos);
262
9.21M
    mxx = pm->xx, mxy = pm->xy;
263
9.21M
    pmr->xx = sincos.cos * mxx + sincos.sin * pm->yx;
264
9.21M
    pmr->xy = sincos.cos * mxy + sincos.sin * pm->yy;
265
9.21M
    pmr->yx = sincos.cos * pm->yx - sincos.sin * mxx;
266
9.21M
    pmr->yy = sincos.cos * pm->yy - sincos.sin * mxy;
267
9.21M
    if (pmr != pm) {
268
0
        pmr->tx = pm->tx;
269
0
        pmr->ty = pm->ty;
270
0
    }
271
9.21M
    return 0;
272
9.21M
}
273
274
/* ------ Coordinate transformations (floating point) ------ */
275
276
/* Note that all the transformation routines take separate */
277
/* x and y arguments, but return their result in a point. */
278
279
/* Transform a point. */
280
int
281
gs_point_transform(double x, double y, const gs_matrix * pmat,
282
                   gs_point * ppt)
283
3.07G
{
284
    /*
285
     * The float casts are there to reproduce results in CET 10-01.ps
286
     * page 4.
287
     */
288
3.07G
    ppt->x = (float)(x * pmat->xx) + pmat->tx;
289
3.07G
    ppt->y = (float)(y * pmat->yy) + pmat->ty;
290
3.07G
    if (!is_fzero(pmat->yx))
291
133M
        ppt->x += (float)(y * pmat->yx);
292
3.07G
    if (!is_fzero(pmat->xy))
293
1.95G
        ppt->y += (float)(x * pmat->xy);
294
3.07G
    return 0;
295
3.07G
}
296
297
/* Inverse-transform a point. */
298
/* Return gs_error_undefinedresult if the matrix is not invertible. */
299
int
300
gs_point_transform_inverse(double x, double y, const gs_matrix * pmat,
301
                           gs_point * ppt)
302
142M
{
303
142M
    if (is_xxyy(pmat)) {
304
132M
        if (is_fzero(pmat->xx) || is_fzero(pmat->yy))
305
6.33k
            return_error(gs_error_undefinedresult);
306
132M
        ppt->x = (x - pmat->tx) / pmat->xx;
307
132M
        ppt->y = (y - pmat->ty) / pmat->yy;
308
132M
        return 0;
309
132M
    } else if (is_xyyx(pmat)) {
310
2.82M
        if (is_fzero(pmat->xy) || is_fzero(pmat->yx))
311
314
            return_error(gs_error_undefinedresult);
312
2.82M
        ppt->x = (y - pmat->ty) / pmat->xy;
313
2.82M
        ppt->y = (x - pmat->tx) / pmat->yx;
314
2.82M
        return 0;
315
6.80M
    } else {     /* There are faster ways to do this, */
316
        /* but we won't implement one unless we have to. */
317
6.80M
        gs_matrix imat;
318
6.80M
        int code = gs_matrix_invert(pmat, &imat);
319
320
6.80M
        if (code < 0)
321
533
            return code;
322
6.80M
        return gs_point_transform(x, y, &imat, ppt);
323
6.80M
    }
324
142M
}
325
326
/* Transform a distance. */
327
int
328
gs_distance_transform(double dx, double dy, const gs_matrix * pmat,
329
                      gs_point * pdpt)
330
203M
{
331
203M
    pdpt->x = dx * pmat->xx;
332
203M
    pdpt->y = dy * pmat->yy;
333
203M
    if (!is_fzero(pmat->yx))
334
53.1M
        pdpt->x += dy * pmat->yx;
335
203M
    if (!is_fzero(pmat->xy))
336
52.2M
        pdpt->y += dx * pmat->xy;
337
203M
    return 0;
338
203M
}
339
340
/* Inverse-transform a distance. */
341
/* Return gs_error_undefinedresult if the matrix is not invertible. */
342
int
343
gs_distance_transform_inverse(double dx, double dy,
344
                              const gs_matrix * pmat, gs_point * pdpt)
345
50.6M
{
346
50.6M
    if (is_xxyy(pmat)) {
347
23.5M
        if (is_fzero(pmat->xx) || is_fzero(pmat->yy))
348
53.4k
            return_error(gs_error_undefinedresult);
349
23.5M
        pdpt->x = dx / pmat->xx;
350
23.5M
        pdpt->y = dy / pmat->yy;
351
27.1M
    } else if (is_xyyx(pmat)) {
352
319k
        if (is_fzero(pmat->xy) || is_fzero(pmat->yx))
353
24.6k
            return_error(gs_error_undefinedresult);
354
295k
        pdpt->x = dy / pmat->xy;
355
295k
        pdpt->y = dx / pmat->yx;
356
26.7M
    } else {
357
26.7M
        double det = pmat->xx * pmat->yy - pmat->xy * pmat->yx;
358
359
26.7M
        if (det == 0)
360
595k
            return_error(gs_error_undefinedresult);
361
26.1M
        pdpt->x = (dx * pmat->yy - dy * pmat->yx) / det;
362
26.1M
        pdpt->y = (dy * pmat->xx - dx * pmat->xy) / det;
363
26.1M
    }
364
50.0M
    return 0;
365
50.6M
}
366
367
/* Compute the bounding box of 4 points. */
368
int
369
gs_points_bbox(const gs_point pts[4], gs_rect * pbox)
370
30.9M
{
371
30.9M
#define assign_min_max(vmin, vmax, v0, v1)\
372
123M
  if ( v0 < v1 ) vmin = v0, vmax = v1; else vmin = v1, vmax = v0
373
30.9M
#define assign_min_max_4(vmin, vmax, v0, v1, v2, v3)\
374
61.9M
  { double min01, max01, min23, max23;\
375
61.9M
    assign_min_max(min01, max01, v0, v1);\
376
61.9M
    assign_min_max(min23, max23, v2, v3);\
377
61.9M
    vmin = min(min01, min23);\
378
61.9M
    vmax = max(max01, max23);\
379
61.9M
  }
380
30.9M
    assign_min_max_4(pbox->p.x, pbox->q.x,
381
30.9M
                     pts[0].x, pts[1].x, pts[2].x, pts[3].x);
382
30.9M
    assign_min_max_4(pbox->p.y, pbox->q.y,
383
30.9M
                     pts[0].y, pts[1].y, pts[2].y, pts[3].y);
384
30.9M
#undef assign_min_max
385
30.9M
#undef assign_min_max_4
386
30.9M
    return 0;
387
30.9M
}
388
389
/* Transform or inverse-transform a bounding box. */
390
/* Return gs_error_undefinedresult if the matrix is not invertible. */
391
static int
392
bbox_transform_either_only(const gs_rect * pbox_in, const gs_matrix * pmat,
393
                           gs_point pts[4],
394
     int (*point_xform) (double, double, const gs_matrix *, gs_point *))
395
30.9M
{
396
30.9M
    int code;
397
398
30.9M
    if ((code = (*point_xform) (pbox_in->p.x, pbox_in->p.y, pmat, &pts[0])) < 0 ||
399
30.9M
        (code = (*point_xform) (pbox_in->p.x, pbox_in->q.y, pmat, &pts[1])) < 0 ||
400
30.9M
        (code = (*point_xform) (pbox_in->q.x, pbox_in->p.y, pmat, &pts[2])) < 0 ||
401
30.9M
     (code = (*point_xform) (pbox_in->q.x, pbox_in->q.y, pmat, &pts[3])) < 0
402
30.9M
        )
403
30.9M
        DO_NOTHING;
404
30.9M
    return code;
405
30.9M
}
406
407
static int
408
bbox_transform_either(const gs_rect * pbox_in, const gs_matrix * pmat,
409
                      gs_rect * pbox_out,
410
     int (*point_xform) (double, double, const gs_matrix *, gs_point *))
411
30.9M
{
412
30.9M
    int code;
413
414
    /*
415
     * In principle, we could transform only one point and two
416
     * distance vectors; however, because of rounding, we will only
417
     * get fully consistent results if we transform all 4 points.
418
     * We must compute the max and min after transforming,
419
     * since a rotation may be involved.
420
     */
421
30.9M
    gs_point pts[4];
422
423
30.9M
    if ((code = bbox_transform_either_only(pbox_in, pmat, pts, point_xform)) < 0)
424
781
        return code;
425
30.9M
    return gs_points_bbox(pts, pbox_out);
426
30.9M
}
427
int
428
gs_bbox_transform(const gs_rect * pbox_in, const gs_matrix * pmat,
429
                  gs_rect * pbox_out)
430
25.3M
{
431
25.3M
    return bbox_transform_either(pbox_in, pmat, pbox_out,
432
25.3M
                                 gs_point_transform);
433
25.3M
}
434
int
435
gs_bbox_transform_only(const gs_rect * pbox_in, const gs_matrix * pmat,
436
                       gs_point points[4])
437
18.2k
{
438
18.2k
    return bbox_transform_either_only(pbox_in, pmat, points,
439
18.2k
                                      gs_point_transform);
440
18.2k
}
441
int
442
gs_bbox_transform_inverse(const gs_rect * pbox_in, const gs_matrix * pmat,
443
                          gs_rect * pbox_out)
444
5.61M
{
445
5.61M
    int code = bbox_transform_either(pbox_in, pmat, pbox_out,
446
5.61M
                                 gs_point_transform_inverse);
447
448
5.61M
    return code;
449
5.61M
}
450
451
/* ------ Coordinate transformations (to fixed point) ------ */
452
453
33.5M
#define f_fits_in_fixed(f) f_fits_in_bits(f, fixed_int_bits)
454
455
/* Make a gs_matrix_fixed from a gs_matrix. */
456
int
457
gs_matrix_fixed_from_matrix(gs_matrix_fixed *pfmat, const gs_matrix *pmat)
458
15.6M
{
459
15.6M
    *(gs_matrix *)pfmat = *pmat;
460
15.6M
    if (f_fits_in_fixed(pmat->tx) && f_fits_in_fixed(pmat->ty)) {
461
15.6M
        pfmat->tx = fixed2float(pfmat->tx_fixed = float2fixed(pmat->tx));
462
15.6M
        pfmat->ty = fixed2float(pfmat->ty_fixed = float2fixed(pmat->ty));
463
15.6M
        pfmat->txy_fixed_valid = true;
464
15.6M
    } else {
465
0
        pfmat->txy_fixed_valid = false;
466
0
    }
467
15.6M
    return 0;
468
15.6M
}
469
470
/* Transform a point with a fixed-point result. */
471
int
472
gs_point_transform2fixed(const gs_matrix_fixed * pmat,
473
                         double x, double y, gs_fixed_point * ppt)
474
8.48M
{
475
8.48M
    fixed px, py, t;
476
8.48M
    double xtemp, ytemp;
477
8.48M
    int code;
478
479
8.48M
    if (!pmat->txy_fixed_valid) { /* The translation is out of range.  Do the */
480
        /* computation in floating point, and convert to */
481
        /* fixed at the end. */
482
215k
        gs_point fpt;
483
484
215k
        gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
485
215k
        if (!(f_fits_in_fixed(fpt.x) && f_fits_in_fixed(fpt.y)))
486
30
            return_error(gs_error_limitcheck);
487
215k
        ppt->x = float2fixed(fpt.x);
488
215k
        ppt->y = float2fixed(fpt.y);
489
215k
        return 0;
490
215k
    }
491
8.27M
    if (!is_fzero(pmat->xy)) { /* Hope for 90 degree rotation */
492
28.4k
        if ((code = CHECK_DFMUL2FIXED_VARS(px, y, pmat->yx, xtemp)) < 0 ||
493
28.4k
            (code = CHECK_DFMUL2FIXED_VARS(py, x, pmat->xy, ytemp)) < 0
494
28.4k
            )
495
6
            return code;
496
28.4k
        FINISH_DFMUL2FIXED_VARS(px, xtemp);
497
28.4k
        FINISH_DFMUL2FIXED_VARS(py, ytemp);
498
28.4k
        if (!is_fzero(pmat->xx)) {
499
23.2k
            if ((code = CHECK_DFMUL2FIXED_VARS(t, x, pmat->xx, xtemp)) < 0)
500
0
                return code;
501
23.2k
            FINISH_DFMUL2FIXED_VARS(t, xtemp);
502
23.2k
            if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
503
0
                return code;
504
23.2k
        }
505
28.4k
        if (!is_fzero(pmat->yy)) {
506
23.2k
            if ((code = CHECK_DFMUL2FIXED_VARS(t, y, pmat->yy, ytemp)) < 0)
507
0
                return code;
508
23.2k
            FINISH_DFMUL2FIXED_VARS(t, ytemp);
509
23.2k
            if ((code = CHECK_SET_FIXED_SUM(py, py, t)) < 0)
510
0
                return code;
511
23.2k
        }
512
8.24M
    } else {
513
8.24M
        if ((code = CHECK_DFMUL2FIXED_VARS(px, x, pmat->xx, xtemp)) < 0 ||
514
8.24M
            (code = CHECK_DFMUL2FIXED_VARS(py, y, pmat->yy, ytemp)) < 0
515
8.24M
            )
516
7.68k
            return code;
517
8.23M
        FINISH_DFMUL2FIXED_VARS(px, xtemp);
518
8.23M
        FINISH_DFMUL2FIXED_VARS(py, ytemp);
519
8.23M
        if (!is_fzero(pmat->yx)) {
520
4
            if ((code = CHECK_DFMUL2FIXED_VARS(t, y, pmat->yx, ytemp)) < 0)
521
0
                return code;
522
4
            FINISH_DFMUL2FIXED_VARS(t, ytemp);
523
4
            if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
524
0
                return code;
525
4
        }
526
8.23M
    }
527
8.26M
    if (((code = CHECK_SET_FIXED_SUM(ppt->x, px, pmat->tx_fixed)) < 0) ||
528
8.26M
        ((code = CHECK_SET_FIXED_SUM(ppt->y, py, pmat->ty_fixed)) < 0) )
529
136
        return code;
530
8.26M
    return 0;
531
8.26M
}
532
533
#if PRECISE_CURRENTPOINT
534
/* Transform a point with a fixed-point result. */
535
/* Used for the best precision of the current point,
536
   see comment in clamp_point_aux. */
537
int
538
gs_point_transform2fixed_rounding(const gs_matrix_fixed * pmat,
539
                         double x, double y, gs_fixed_point * ppt)
540
938k
{
541
938k
    gs_point fpt;
542
543
938k
    gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
544
938k
    if (!(f_fits_in_fixed(fpt.x) && f_fits_in_fixed(fpt.y)))
545
69
        return_error(gs_error_limitcheck);
546
938k
    ppt->x = float2fixed_rounded(fpt.x);
547
938k
    ppt->y = float2fixed_rounded(fpt.y);
548
938k
    return 0;
549
938k
}
550
#endif
551
552
/* Transform a distance with a fixed-point result. */
553
int
554
gs_distance_transform2fixed(const gs_matrix_fixed * pmat,
555
                            double dx, double dy, gs_fixed_point * ppt)
556
55.8M
{
557
55.8M
    fixed px, py, t;
558
55.8M
    double xtemp, ytemp;
559
55.8M
    int code;
560
561
55.8M
    if ((code = CHECK_DFMUL2FIXED_VARS(px, dx, pmat->xx, xtemp)) < 0 ||
562
55.6M
        (code = CHECK_DFMUL2FIXED_VARS(py, dy, pmat->yy, ytemp)) < 0
563
55.8M
        )
564
239k
        return code;
565
55.6M
    FINISH_DFMUL2FIXED_VARS(px, xtemp);
566
55.6M
    FINISH_DFMUL2FIXED_VARS(py, ytemp);
567
55.6M
    if (!is_fzero(pmat->yx)) {
568
2.80M
        if ((code = CHECK_DFMUL2FIXED_VARS(t, dy, pmat->yx, ytemp)) < 0)
569
19.4k
            return code;
570
2.78M
        FINISH_DFMUL2FIXED_VARS(t, ytemp);
571
2.78M
        if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
572
12
            return code;
573
2.78M
    }
574
55.6M
    if (!is_fzero(pmat->xy)) {
575
2.61M
        if ((code = CHECK_DFMUL2FIXED_VARS(t, dx, pmat->xy, xtemp)) < 0)
576
11.5k
            return code;
577
2.60M
        FINISH_DFMUL2FIXED_VARS(t, xtemp);
578
2.60M
        if ((code = CHECK_SET_FIXED_SUM(py, py, t)) < 0)
579
0
            return code;
580
2.60M
    }
581
55.6M
    ppt->x = px;
582
55.6M
    ppt->y = py;
583
55.6M
    return 0;
584
55.6M
}
585
586
/* ------ Serialization ------ */
587
588
/*
589
 * For maximum conciseness in band lists, we write a matrix as a control
590
 * byte followed by 0 to 6 values.  The control byte has the format
591
 * AABBCD00.  AA and BB control (xx,yy) and (xy,yx) as follows:
592
 *  00 = values are (0.0, 0.0)
593
 *  01 = values are (V, V) [1 value follows]
594
 *  10 = values are (V, -V) [1 value follows]
595
 *  11 = values are (U, V) [2 values follow]
596
 * C and D control tx and ty as follows:
597
 *  0 = value is 0.0
598
 *  1 = value follows
599
 * The following code is the only place that knows this representation.
600
 */
601
602
/* Put a matrix on a stream. */
603
int
604
sput_matrix(stream *s, const gs_matrix *pmat)
605
10.8M
{
606
10.8M
    byte buf[1 + 6 * sizeof(float)];
607
10.8M
    byte *cp = buf + 1;
608
10.8M
    byte b = 0;
609
10.8M
    float coeff[6];
610
10.8M
    int i;
611
10.8M
    uint ignore;
612
613
10.8M
    coeff[0] = pmat->xx;
614
10.8M
    coeff[1] = pmat->xy;
615
10.8M
    coeff[2] = pmat->yx;
616
10.8M
    coeff[3] = pmat->yy;
617
10.8M
    coeff[4] = pmat->tx;
618
10.8M
    coeff[5] = pmat->ty;
619
32.6M
    for (i = 0; i < 4; i += 2) {
620
21.7M
        float u = coeff[i], v = coeff[i ^ 3];
621
622
21.7M
        b <<= 2;
623
21.7M
        if (u != 0 || v != 0) {
624
16.9M
            memcpy(cp, &u, sizeof(float));
625
16.9M
            cp += sizeof(float);
626
627
16.9M
            if (v == u)
628
540k
                b += 1;
629
16.4M
            else if (v == -u)
630
3.16M
                b += 2;
631
13.2M
            else {
632
13.2M
                b += 3;
633
13.2M
                memcpy(cp, &v, sizeof(float));
634
13.2M
                cp += sizeof(float);
635
13.2M
            }
636
16.9M
        }
637
21.7M
    }
638
32.6M
    for (; i < 6; ++i) {
639
21.7M
        float v = coeff[i];
640
641
21.7M
        b <<= 1;
642
21.7M
        if (v != 0) {
643
20.7M
            ++b;
644
20.7M
            memcpy(cp, &v, sizeof(float));
645
20.7M
            cp += sizeof(float);
646
20.7M
        }
647
21.7M
    }
648
10.8M
    buf[0] = b << 2;
649
10.8M
    return sputs(s, buf, cp - buf, &ignore);
650
10.8M
}
651
652
/* Get a matrix from a stream. */
653
int
654
sget_matrix(stream *s, gs_matrix *pmat)
655
11.1M
{
656
11.1M
    int b = sgetc(s);
657
11.1M
    float coeff[6];
658
11.1M
    int i;
659
11.1M
    int status;
660
11.1M
    uint nread;
661
662
11.1M
    if (b < 0)
663
0
        return b;
664
33.3M
    for (i = 0; i < 4; i += 2, b <<= 2)
665
22.2M
        if (!(b & 0xc0))
666
8.14M
            coeff[i] = coeff[i ^ 3] = 0.0;
667
14.0M
        else {
668
14.0M
            float value;
669
670
14.0M
            status = sgets(s, (byte *)&value, sizeof(value), &nread);
671
14.0M
            if (status < 0 && status != EOFC)
672
0
                return_error(gs_error_ioerror);
673
14.0M
            coeff[i] = value;
674
14.0M
            switch ((b >> 6) & 3) {
675
1.16M
                case 1:
676
1.16M
                    coeff[i ^ 3] = value;
677
1.16M
                    break;
678
5.45M
                case 2:
679
5.45M
                    coeff[i ^ 3] = -value;
680
5.45M
                    break;
681
7.46M
                case 3:
682
7.46M
                    status = sgets(s, (byte *)&coeff[i ^ 3],
683
7.46M
                                   sizeof(coeff[0]), &nread);
684
7.46M
                    if (status < 0 && status != EOFC)
685
0
                        return_error(gs_error_ioerror);
686
14.0M
            }
687
14.0M
        }
688
33.3M
    for (; i < 6; ++i, b <<= 1)
689
22.2M
        if (b & 0x80) {
690
18.7M
            status = sgets(s, (byte *)&coeff[i], sizeof(coeff[0]), &nread);
691
18.7M
            if (status < 0 && status != EOFC)
692
0
                return_error(gs_error_ioerror);
693
18.7M
        } else
694
3.49M
            coeff[i] = 0.0;
695
11.1M
    pmat->xx = coeff[0];
696
11.1M
    pmat->xy = coeff[1];
697
11.1M
    pmat->yx = coeff[2];
698
11.1M
    pmat->yy = coeff[3];
699
11.1M
    pmat->tx = coeff[4];
700
11.1M
    pmat->ty = coeff[5];
701
11.1M
    return 0;
702
11.1M
}
703
704
/* Compare two matrices */
705
int
706
13.0M
gs_matrix_compare(const gs_matrix *pmat1, const gs_matrix *pmat2) {
707
13.0M
  if (pmat1->xx != pmat2->xx)
708
155k
    return(1);
709
12.8M
  if (pmat1->xy != pmat2->xy)
710
1.48k
    return(1);
711
12.8M
  if (pmat1->yx != pmat2->yx)
712
109
    return(1);
713
12.8M
  if (pmat1->yy != pmat2->yy)
714
7.39k
    return(1);
715
12.8M
  if (pmat1->tx != pmat2->tx)
716
9.99M
    return(1);
717
2.87M
  if (pmat1->ty != pmat2->ty)
718
57.6k
    return(1);
719
2.81M
  return(0);
720
2.87M
}