Coverage Report

Created: 2025-06-10 06:59

/src/ghostpdl/base/gsmatrix.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 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
4.00M
{
37
4.00M
    *pmat = gs_identity_matrix;
38
4.00M
}
39
40
/* Create a translation matrix */
41
int
42
gs_make_translation(double dx, double dy, gs_matrix * pmat)
43
154
{
44
154
    *pmat = gs_identity_matrix;
45
154
    pmat->tx = dx;
46
154
    pmat->ty = dy;
47
154
    return 0;
48
154
}
49
50
/* Create a scaling matrix */
51
int
52
gs_make_scaling(double sx, double sy, gs_matrix * pmat)
53
2.97k
{
54
2.97k
    *pmat = gs_identity_matrix;
55
2.97k
    pmat->xx = sx;
56
2.97k
    pmat->yy = sy;
57
2.97k
    return 0;
58
2.97k
}
59
60
/* Create a rotation matrix. */
61
/* The angle is in degrees. */
62
int
63
gs_make_rotation(double ang, gs_matrix * pmat)
64
52.6k
{
65
52.6k
    gs_sincos_t sincos;
66
67
52.6k
    gs_sincos_degrees(ang, &sincos);
68
52.6k
    pmat->yy = pmat->xx = sincos.cos;
69
52.6k
    pmat->xy = sincos.sin;
70
52.6k
    pmat->yx = -sincos.sin;
71
52.6k
    pmat->tx = pmat->ty = 0.0;
72
52.6k
    return 0;
73
52.6k
}
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
2.92M
{
83
2.92M
    double xx1 = pm1->xx, yy1 = pm1->yy;
84
2.92M
    double tx1 = pm1->tx, ty1 = pm1->ty;
85
2.92M
    double xx2 = pm2->xx, yy2 = pm2->yy;
86
2.92M
    double xy2 = pm2->xy, yx2 = pm2->yx;
87
88
2.92M
    if (is_xxyy(pm1)) {
89
2.83M
        pmr->tx = tx1 * xx2 + pm2->tx;
90
2.83M
        pmr->ty = ty1 * yy2 + pm2->ty;
91
2.83M
        if (is_fzero(xy2))
92
2.79M
            pmr->xy = 0;
93
43.1k
        else
94
43.1k
            pmr->xy = xx1 * xy2,
95
43.1k
                pmr->ty += tx1 * xy2;
96
2.83M
        pmr->xx = xx1 * xx2;
97
2.83M
        if (is_fzero(yx2))
98
2.79M
            pmr->yx = 0;
99
39.8k
        else
100
39.8k
            pmr->yx = yy1 * yx2,
101
39.8k
                pmr->tx += ty1 * yx2;
102
2.83M
        pmr->yy = yy1 * yy2;
103
2.83M
    } else {
104
87.9k
        double xy1 = pm1->xy, yx1 = pm1->yx;
105
106
87.9k
        pmr->xx = xx1 * xx2 + xy1 * yx2;
107
87.9k
        pmr->xy = xx1 * xy2 + xy1 * yy2;
108
87.9k
        pmr->yy = yx1 * xy2 + yy1 * yy2;
109
87.9k
        pmr->yx = yx1 * xx2 + yy1 * yx2;
110
87.9k
        pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
111
87.9k
        pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
112
87.9k
    }
113
2.92M
    return 0;
114
2.92M
}
115
int
116
gs_matrix_multiply_double(const gs_matrix_double * pm1, const gs_matrix * pm2, gs_matrix_double * pmr)
117
153k
{
118
153k
    double xx1 = pm1->xx, yy1 = pm1->yy;
119
153k
    double tx1 = pm1->tx, ty1 = pm1->ty;
120
153k
    double xx2 = pm2->xx, yy2 = pm2->yy;
121
153k
    double xy2 = pm2->xy, yx2 = pm2->yx;
122
123
153k
    if (is_xxyy(pm1)) {
124
132k
        pmr->tx = tx1 * xx2 + pm2->tx;
125
132k
        pmr->ty = ty1 * yy2 + pm2->ty;
126
132k
        if (is_fzero(xy2))
127
115k
            pmr->xy = 0;
128
17.2k
        else
129
17.2k
            pmr->xy = xx1 * xy2,
130
17.2k
                pmr->ty += tx1 * xy2;
131
132k
        pmr->xx = xx1 * xx2;
132
132k
        if (is_fzero(yx2))
133
114k
            pmr->yx = 0;
134
17.5k
        else
135
17.5k
            pmr->yx = yy1 * yx2,
136
17.5k
                pmr->tx += ty1 * yx2;
137
132k
        pmr->yy = yy1 * yy2;
138
132k
    } else {
139
21.0k
        double xy1 = pm1->xy, yx1 = pm1->yx;
140
141
21.0k
        pmr->xx = xx1 * xx2 + xy1 * yx2;
142
21.0k
        pmr->xy = xx1 * xy2 + xy1 * yy2;
143
21.0k
        pmr->yy = yx1 * xy2 + yy1 * yy2;
144
21.0k
        pmr->yx = yx1 * xx2 + yy1 * yx2;
145
21.0k
        pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
146
21.0k
        pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
147
21.0k
    }
148
153k
    return 0;
149
153k
}
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
1.56M
{       /* We have to be careful about fetch/store order, */
155
    /* because pm might be the same as pmr. */
156
1.56M
    if (is_xxyy(pm)) {
157
1.39M
        if (is_fzero(pm->xx) || is_fzero(pm->yy))
158
11
            return_error(gs_error_undefinedresult);
159
1.39M
        pmr->tx = -(pmr->xx = 1.0 / pm->xx) * pm->tx;
160
1.39M
        pmr->xy = 0.0;
161
1.39M
        pmr->yx = 0.0;
162
1.39M
        pmr->ty = -(pmr->yy = 1.0 / pm->yy) * pm->ty;
163
1.39M
    } else {
164
172k
        float mxx = pm->xx, myy = pm->yy, mxy = pm->xy, myx = pm->yx;
165
172k
        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
172k
        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
172k
        if (det == 0)
181
414
            return_error(gs_error_undefinedresult);
182
171k
        pmr->xx = myy / det;
183
171k
        pmr->xy = -mxy / det;
184
171k
        pmr->yx = -myx / det;
185
171k
        pmr->yy = mxx / det;
186
171k
        pmr->tx = (((float)(mty * myx) - (float)(mtx * myy))) / det;
187
171k
        pmr->ty = (((float)(mtx * mxy) - (float)(mty * mxx))) / det;
188
171k
    }
189
1.56M
    return 0;
190
1.56M
}
191
int
192
gs_matrix_invert_to_double(const gs_matrix * pm, gs_matrix_double * pmr)
193
153k
{       /* We have to be careful about fetch/store order, */
194
    /* because pm might be the same as pmr. */
195
153k
    if (is_xxyy(pm)) {
196
132k
        if (is_fzero(pm->xx) || is_fzero(pm->yy))
197
4
            return_error(gs_error_undefinedresult);
198
132k
        pmr->tx = -(pmr->xx = 1.0 / pm->xx) * pm->tx;
199
132k
        pmr->xy = 0.0;
200
132k
        pmr->yx = 0.0;
201
132k
        pmr->ty = -(pmr->yy = 1.0 / pm->yy) * pm->ty;
202
132k
    } else {
203
21.0k
        double mxx = pm->xx, myy = pm->yy, mxy = pm->xy, myx = pm->yx;
204
21.0k
        double mtx = pm->tx, mty = pm->ty;
205
21.0k
        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
21.0k
        if (det == 0)
212
1
            return_error(gs_error_undefinedresult);
213
21.0k
        pmr->xx = myy / det;
214
21.0k
        pmr->xy = -mxy / det;
215
21.0k
        pmr->yx = -myx / det;
216
21.0k
        pmr->yy = mxx / det;
217
21.0k
        pmr->tx = (((mty * myx) - (mtx * myy))) / det;
218
21.0k
        pmr->ty = (((mtx * mxy) - (mty * mxx))) / det;
219
21.0k
    }
220
153k
    return 0;
221
153k
}
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
0
{
227
0
    gs_point trans;
228
0
    int code = gs_distance_transform(dx, dy, pm, &trans);
229
230
0
    if (code < 0)
231
0
        return code;
232
0
    if (pmr != pm)
233
0
        *pmr = *pm;
234
0
    pmr->tx += trans.x;
235
0
    pmr->ty += trans.y;
236
0
    return 0;
237
0
}
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
25
{
243
25
    pmr->xx = pm->xx * sx;
244
25
    pmr->xy = pm->xy * sx;
245
25
    pmr->yx = pm->yx * sy;
246
25
    pmr->yy = pm->yy * sy;
247
25
    if (pmr != pm) {
248
25
        pmr->tx = pm->tx;
249
25
        pmr->ty = pm->ty;
250
25
    }
251
25
    return 0;
252
25
}
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
302k
{
258
302k
    double mxx, mxy;
259
302k
    gs_sincos_t sincos;
260
261
302k
    gs_sincos_degrees(ang, &sincos);
262
302k
    mxx = pm->xx, mxy = pm->xy;
263
302k
    pmr->xx = sincos.cos * mxx + sincos.sin * pm->yx;
264
302k
    pmr->xy = sincos.cos * mxy + sincos.sin * pm->yy;
265
302k
    pmr->yx = sincos.cos * pm->yx - sincos.sin * mxx;
266
302k
    pmr->yy = sincos.cos * pm->yy - sincos.sin * mxy;
267
302k
    if (pmr != pm) {
268
0
        pmr->tx = pm->tx;
269
0
        pmr->ty = pm->ty;
270
0
    }
271
302k
    return 0;
272
302k
}
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
15.8M
{
284
    /*
285
     * The float casts are there to reproduce results in CET 10-01.ps
286
     * page 4.
287
     */
288
15.8M
    ppt->x = (float)(x * pmat->xx) + pmat->tx;
289
15.8M
    ppt->y = (float)(y * pmat->yy) + pmat->ty;
290
15.8M
    if (!is_fzero(pmat->yx))
291
4.79M
        ppt->x += (float)(y * pmat->yx);
292
15.8M
    if (!is_fzero(pmat->xy))
293
5.25M
        ppt->y += (float)(x * pmat->xy);
294
15.8M
    return 0;
295
15.8M
}
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
4.06M
{
303
4.06M
    if (is_xxyy(pmat)) {
304
3.93M
        if (is_fzero(pmat->xx) || is_fzero(pmat->yy))
305
341
            return_error(gs_error_undefinedresult);
306
3.93M
        ppt->x = (x - pmat->tx) / pmat->xx;
307
3.93M
        ppt->y = (y - pmat->ty) / pmat->yy;
308
3.93M
        return 0;
309
3.93M
    } else if (is_xyyx(pmat)) {
310
87.5k
        if (is_fzero(pmat->xy) || is_fzero(pmat->yx))
311
2
            return_error(gs_error_undefinedresult);
312
87.5k
        ppt->x = (y - pmat->ty) / pmat->xy;
313
87.5k
        ppt->y = (x - pmat->tx) / pmat->yx;
314
87.5k
        return 0;
315
87.5k
    } else {     /* There are faster ways to do this, */
316
        /* but we won't implement one unless we have to. */
317
44.5k
        gs_matrix imat;
318
44.5k
        int code = gs_matrix_invert(pmat, &imat);
319
320
44.5k
        if (code < 0)
321
21
            return code;
322
44.5k
        return gs_point_transform(x, y, &imat, ppt);
323
44.5k
    }
324
4.06M
}
325
326
/* Transform a distance. */
327
int
328
gs_distance_transform(double dx, double dy, const gs_matrix * pmat,
329
                      gs_point * pdpt)
330
8.14M
{
331
8.14M
    pdpt->x = dx * pmat->xx;
332
8.14M
    pdpt->y = dy * pmat->yy;
333
8.14M
    if (!is_fzero(pmat->yx))
334
1.43M
        pdpt->x += dy * pmat->yx;
335
8.14M
    if (!is_fzero(pmat->xy))
336
1.40M
        pdpt->y += dx * pmat->xy;
337
8.14M
    return 0;
338
8.14M
}
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
9.85M
{
346
9.85M
    if (is_xxyy(pmat)) {
347
975k
        if (is_fzero(pmat->xx) || is_fzero(pmat->yy))
348
3.44k
            return_error(gs_error_undefinedresult);
349
972k
        pdpt->x = dx / pmat->xx;
350
972k
        pdpt->y = dy / pmat->yy;
351
8.87M
    } else if (is_xyyx(pmat)) {
352
24.3k
        if (is_fzero(pmat->xy) || is_fzero(pmat->yx))
353
311
            return_error(gs_error_undefinedresult);
354
24.0k
        pdpt->x = dy / pmat->xy;
355
24.0k
        pdpt->y = dx / pmat->yx;
356
8.85M
    } else {
357
8.85M
        double det = pmat->xx * pmat->yy - pmat->xy * pmat->yx;
358
359
8.85M
        if (det == 0)
360
143k
            return_error(gs_error_undefinedresult);
361
8.70M
        pdpt->x = (dx * pmat->yy - dy * pmat->yx) / det;
362
8.70M
        pdpt->y = (dy * pmat->xx - dx * pmat->xy) / det;
363
8.70M
    }
364
9.70M
    return 0;
365
9.85M
}
366
367
/* Compute the bounding box of 4 points. */
368
int
369
gs_points_bbox(const gs_point pts[4], gs_rect * pbox)
370
2.29M
{
371
2.29M
#define assign_min_max(vmin, vmax, v0, v1)\
372
9.17M
  if ( v0 < v1 ) vmin = v0, vmax = v1; else vmin = v1, vmax = v0
373
2.29M
#define assign_min_max_4(vmin, vmax, v0, v1, v2, v3)\
374
4.58M
  { double min01, max01, min23, max23;\
375
4.58M
    assign_min_max(min01, max01, v0, v1);\
376
4.58M
    assign_min_max(min23, max23, v2, v3);\
377
4.58M
    vmin = min(min01, min23);\
378
4.58M
    vmax = max(max01, max23);\
379
4.58M
  }
380
2.29M
    assign_min_max_4(pbox->p.x, pbox->q.x,
381
2.29M
                     pts[0].x, pts[1].x, pts[2].x, pts[3].x);
382
2.29M
    assign_min_max_4(pbox->p.y, pbox->q.y,
383
2.29M
                     pts[0].y, pts[1].y, pts[2].y, pts[3].y);
384
2.29M
#undef assign_min_max
385
2.29M
#undef assign_min_max_4
386
2.29M
    return 0;
387
2.29M
}
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
2.29M
{
396
2.29M
    int code;
397
398
2.29M
    if ((code = (*point_xform) (pbox_in->p.x, pbox_in->p.y, pmat, &pts[0])) < 0 ||
399
2.29M
        (code = (*point_xform) (pbox_in->p.x, pbox_in->q.y, pmat, &pts[1])) < 0 ||
400
2.29M
        (code = (*point_xform) (pbox_in->q.x, pbox_in->p.y, pmat, &pts[2])) < 0 ||
401
2.29M
     (code = (*point_xform) (pbox_in->q.x, pbox_in->q.y, pmat, &pts[3])) < 0
402
2.29M
        )
403
2.29M
        DO_NOTHING;
404
2.29M
    return code;
405
2.29M
}
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
2.29M
{
412
2.29M
    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
2.29M
    gs_point pts[4];
422
423
2.29M
    if ((code = bbox_transform_either_only(pbox_in, pmat, pts, point_xform)) < 0)
424
47
        return code;
425
2.29M
    return gs_points_bbox(pts, pbox_out);
426
2.29M
}
427
int
428
gs_bbox_transform(const gs_rect * pbox_in, const gs_matrix * pmat,
429
                  gs_rect * pbox_out)
430
1.70M
{
431
1.70M
    return bbox_transform_either(pbox_in, pmat, pbox_out,
432
1.70M
                                 gs_point_transform);
433
1.70M
}
434
int
435
gs_bbox_transform_only(const gs_rect * pbox_in, const gs_matrix * pmat,
436
                       gs_point points[4])
437
0
{
438
0
    return bbox_transform_either_only(pbox_in, pmat, points,
439
0
                                      gs_point_transform);
440
0
}
441
int
442
gs_bbox_transform_inverse(const gs_rect * pbox_in, const gs_matrix * pmat,
443
                          gs_rect * pbox_out)
444
591k
{
445
591k
    int code = bbox_transform_either(pbox_in, pmat, pbox_out,
446
591k
                                 gs_point_transform_inverse);
447
448
591k
    return code;
449
591k
}
450
451
/* ------ Coordinate transformations (to fixed point) ------ */
452
453
286k
#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
19.9k
{
459
19.9k
    *(gs_matrix *)pfmat = *pmat;
460
19.9k
    if (f_fits_in_fixed(pmat->tx) && f_fits_in_fixed(pmat->ty)) {
461
19.9k
        pfmat->tx = fixed2float(pfmat->tx_fixed = float2fixed(pmat->tx));
462
19.9k
        pfmat->ty = fixed2float(pfmat->ty_fixed = float2fixed(pmat->ty));
463
19.9k
        pfmat->txy_fixed_valid = true;
464
19.9k
    } else {
465
0
        pfmat->txy_fixed_valid = false;
466
0
    }
467
19.9k
    return 0;
468
19.9k
}
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
520k
{
475
520k
    fixed px, py, t;
476
520k
    double xtemp, ytemp;
477
520k
    int code;
478
479
520k
    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
15
        gs_point fpt;
483
484
15
        gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
485
15
        if (!(f_fits_in_fixed(fpt.x) && f_fits_in_fixed(fpt.y)))
486
6
            return_error(gs_error_limitcheck);
487
9
        ppt->x = float2fixed(fpt.x);
488
9
        ppt->y = float2fixed(fpt.y);
489
9
        return 0;
490
15
    }
491
520k
    if (!is_fzero(pmat->xy)) { /* Hope for 90 degree rotation */
492
12.9k
        if ((code = CHECK_DFMUL2FIXED_VARS(px, y, pmat->yx, xtemp)) < 0 ||
493
12.9k
            (code = CHECK_DFMUL2FIXED_VARS(py, x, pmat->xy, ytemp)) < 0
494
12.9k
            )
495
0
            return code;
496
12.9k
        FINISH_DFMUL2FIXED_VARS(px, xtemp);
497
12.9k
        FINISH_DFMUL2FIXED_VARS(py, ytemp);
498
12.9k
        if (!is_fzero(pmat->xx)) {
499
11.2k
            if ((code = CHECK_DFMUL2FIXED_VARS(t, x, pmat->xx, xtemp)) < 0)
500
0
                return code;
501
11.2k
            FINISH_DFMUL2FIXED_VARS(t, xtemp);
502
11.2k
            if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
503
0
                return code;
504
11.2k
        }
505
12.9k
        if (!is_fzero(pmat->yy)) {
506
11.2k
            if ((code = CHECK_DFMUL2FIXED_VARS(t, y, pmat->yy, ytemp)) < 0)
507
0
                return code;
508
11.2k
            FINISH_DFMUL2FIXED_VARS(t, ytemp);
509
11.2k
            if ((code = CHECK_SET_FIXED_SUM(py, py, t)) < 0)
510
0
                return code;
511
11.2k
        }
512
507k
    } else {
513
507k
        if ((code = CHECK_DFMUL2FIXED_VARS(px, x, pmat->xx, xtemp)) < 0 ||
514
507k
            (code = CHECK_DFMUL2FIXED_VARS(py, y, pmat->yy, ytemp)) < 0
515
507k
            )
516
570
            return code;
517
507k
        FINISH_DFMUL2FIXED_VARS(px, xtemp);
518
507k
        FINISH_DFMUL2FIXED_VARS(py, ytemp);
519
507k
        if (!is_fzero(pmat->yx)) {
520
0
            if ((code = CHECK_DFMUL2FIXED_VARS(t, y, pmat->yx, ytemp)) < 0)
521
0
                return code;
522
0
            FINISH_DFMUL2FIXED_VARS(t, ytemp);
523
0
            if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
524
0
                return code;
525
0
        }
526
507k
    }
527
519k
    if (((code = CHECK_SET_FIXED_SUM(ppt->x, px, pmat->tx_fixed)) < 0) ||
528
519k
        ((code = CHECK_SET_FIXED_SUM(ppt->y, py, pmat->ty_fixed)) < 0) )
529
0
        return code;
530
519k
    return 0;
531
519k
}
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
123k
{
541
123k
    gs_point fpt;
542
543
123k
    gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
544
123k
    if (!(f_fits_in_fixed(fpt.x) && f_fits_in_fixed(fpt.y)))
545
14
        return_error(gs_error_limitcheck);
546
123k
    ppt->x = float2fixed_rounded(fpt.x);
547
123k
    ppt->y = float2fixed_rounded(fpt.y);
548
123k
    return 0;
549
123k
}
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
7.13M
{
557
7.13M
    fixed px, py, t;
558
7.13M
    double xtemp, ytemp;
559
7.13M
    int code;
560
561
7.13M
    if ((code = CHECK_DFMUL2FIXED_VARS(px, dx, pmat->xx, xtemp)) < 0 ||
562
7.13M
        (code = CHECK_DFMUL2FIXED_VARS(py, dy, pmat->yy, ytemp)) < 0
563
7.13M
        )
564
17.0k
        return code;
565
7.12M
    FINISH_DFMUL2FIXED_VARS(px, xtemp);
566
7.12M
    FINISH_DFMUL2FIXED_VARS(py, ytemp);
567
7.12M
    if (!is_fzero(pmat->yx)) {
568
114k
        if ((code = CHECK_DFMUL2FIXED_VARS(t, dy, pmat->yx, ytemp)) < 0)
569
1.79k
            return code;
570
112k
        FINISH_DFMUL2FIXED_VARS(t, ytemp);
571
112k
        if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
572
0
            return code;
573
112k
    }
574
7.12M
    if (!is_fzero(pmat->xy)) {
575
111k
        if ((code = CHECK_DFMUL2FIXED_VARS(t, dx, pmat->xy, xtemp)) < 0)
576
322
            return code;
577
111k
        FINISH_DFMUL2FIXED_VARS(t, xtemp);
578
111k
        if ((code = CHECK_SET_FIXED_SUM(py, py, t)) < 0)
579
0
            return code;
580
111k
    }
581
7.12M
    ppt->x = px;
582
7.12M
    ppt->y = py;
583
7.12M
    return 0;
584
7.12M
}
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
791k
{
606
791k
    byte buf[1 + 6 * sizeof(float)];
607
791k
    byte *cp = buf + 1;
608
791k
    byte b = 0;
609
791k
    float coeff[6];
610
791k
    int i;
611
791k
    uint ignore;
612
613
791k
    coeff[0] = pmat->xx;
614
791k
    coeff[1] = pmat->xy;
615
791k
    coeff[2] = pmat->yx;
616
791k
    coeff[3] = pmat->yy;
617
791k
    coeff[4] = pmat->tx;
618
791k
    coeff[5] = pmat->ty;
619
2.37M
    for (i = 0; i < 4; i += 2) {
620
1.58M
        float u = coeff[i], v = coeff[i ^ 3];
621
622
1.58M
        b <<= 2;
623
1.58M
        if (u != 0 || v != 0) {
624
918k
            memcpy(cp, &u, sizeof(float));
625
918k
            cp += sizeof(float);
626
627
918k
            if (v == u)
628
60.9k
                b += 1;
629
857k
            else if (v == -u)
630
488k
                b += 2;
631
369k
            else {
632
369k
                b += 3;
633
369k
                memcpy(cp, &v, sizeof(float));
634
369k
                cp += sizeof(float);
635
369k
            }
636
918k
        }
637
1.58M
    }
638
2.37M
    for (; i < 6; ++i) {
639
1.58M
        float v = coeff[i];
640
641
1.58M
        b <<= 1;
642
1.58M
        if (v != 0) {
643
1.40M
            ++b;
644
1.40M
            memcpy(cp, &v, sizeof(float));
645
1.40M
            cp += sizeof(float);
646
1.40M
        }
647
1.58M
    }
648
791k
    buf[0] = b << 2;
649
791k
    return sputs(s, buf, cp - buf, &ignore);
650
791k
}
651
652
/* Get a matrix from a stream. */
653
int
654
sget_matrix(stream *s, gs_matrix *pmat)
655
1.09M
{
656
1.09M
    int b = sgetc(s);
657
1.09M
    float coeff[6];
658
1.09M
    int i;
659
1.09M
    int status;
660
1.09M
    uint nread;
661
662
1.09M
    if (b < 0)
663
0
        return b;
664
3.28M
    for (i = 0; i < 4; i += 2, b <<= 2)
665
2.19M
        if (!(b & 0xc0))
666
1.03M
            coeff[i] = coeff[i ^ 3] = 0.0;
667
1.15M
        else {
668
1.15M
            float value;
669
670
1.15M
            status = sgets(s, (byte *)&value, sizeof(value), &nread);
671
1.15M
            if (status < 0 && status != EOFC)
672
0
                return_error(gs_error_ioerror);
673
1.15M
            coeff[i] = value;
674
1.15M
            switch ((b >> 6) & 3) {
675
25.9k
                case 1:
676
25.9k
                    coeff[i ^ 3] = value;
677
25.9k
                    break;
678
793k
                case 2:
679
793k
                    coeff[i ^ 3] = -value;
680
793k
                    break;
681
337k
                case 3:
682
337k
                    status = sgets(s, (byte *)&coeff[i ^ 3],
683
337k
                                   sizeof(coeff[0]), &nread);
684
337k
                    if (status < 0 && status != EOFC)
685
0
                        return_error(gs_error_ioerror);
686
1.15M
            }
687
1.15M
        }
688
3.28M
    for (; i < 6; ++i, b <<= 1)
689
2.19M
        if (b & 0x80) {
690
1.88M
            status = sgets(s, (byte *)&coeff[i], sizeof(coeff[0]), &nread);
691
1.88M
            if (status < 0 && status != EOFC)
692
0
                return_error(gs_error_ioerror);
693
1.88M
        } else
694
309k
            coeff[i] = 0.0;
695
1.09M
    pmat->xx = coeff[0];
696
1.09M
    pmat->xy = coeff[1];
697
1.09M
    pmat->yx = coeff[2];
698
1.09M
    pmat->yy = coeff[3];
699
1.09M
    pmat->tx = coeff[4];
700
1.09M
    pmat->ty = coeff[5];
701
1.09M
    return 0;
702
1.09M
}
703
704
/* Compare two matrices */
705
int
706
0
gs_matrix_compare(const gs_matrix *pmat1, const gs_matrix *pmat2) {
707
0
  if (pmat1->xx != pmat2->xx)
708
0
    return(1);
709
0
  if (pmat1->xy != pmat2->xy)
710
0
    return(1);
711
0
  if (pmat1->yx != pmat2->yx)
712
0
    return(1);
713
0
  if (pmat1->yy != pmat2->yy)
714
0
    return(1);
715
0
  if (pmat1->tx != pmat2->tx)
716
0
    return(1);
717
0
  if (pmat1->ty != pmat2->ty)
718
0
    return(1);
719
0
  return(0);
720
0
}