summaryrefslogtreecommitdiff
path: root/base/gsmatrix.c
blob: 56c1053dd90656c0a0c099e97b7553b062b3f583 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/* Copyright (C) 2001-2023 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.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Matrix operators for Ghostscript library */
#include "math_.h"
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gxfarith.h"
#include "gxfixed.h"
#include "gxmatrix.h"
#include "stream.h"

/* The identity matrix */
static const gs_matrix gs_identity_matrix =
{identity_matrix_body};

/* ------ Matrix creation ------ */

/* Create an identity matrix */
void
gs_make_identity(gs_matrix * pmat)
{
    *pmat = gs_identity_matrix;
}

/* Create a translation matrix */
int
gs_make_translation(double dx, double dy, gs_matrix * pmat)
{
    *pmat = gs_identity_matrix;
    pmat->tx = dx;
    pmat->ty = dy;
    return 0;
}

/* Create a scaling matrix */
int
gs_make_scaling(double sx, double sy, gs_matrix * pmat)
{
    *pmat = gs_identity_matrix;
    pmat->xx = sx;
    pmat->yy = sy;
    return 0;
}

/* Create a rotation matrix. */
/* The angle is in degrees. */
int
gs_make_rotation(double ang, gs_matrix * pmat)
{
    gs_sincos_t sincos;

    gs_sincos_degrees(ang, &sincos);
    pmat->yy = pmat->xx = sincos.cos;
    pmat->xy = sincos.sin;
    pmat->yx = -sincos.sin;
    pmat->tx = pmat->ty = 0.0;
    return 0;
}

/* ------ Matrix arithmetic ------ */

/* Multiply two matrices.  We should check for floating exceptions, */
/* but for the moment it's just too awkward. */
/* Since this is used heavily, we check for shortcuts. */
int
gs_matrix_multiply(const gs_matrix * pm1, const gs_matrix * pm2, gs_matrix * pmr)
{
    double xx1 = pm1->xx, yy1 = pm1->yy;
    double tx1 = pm1->tx, ty1 = pm1->ty;
    double xx2 = pm2->xx, yy2 = pm2->yy;
    double xy2 = pm2->xy, yx2 = pm2->yx;

    if (is_xxyy(pm1)) {
        pmr->tx = tx1 * xx2 + pm2->tx;
        pmr->ty = ty1 * yy2 + pm2->ty;
        if (is_fzero(xy2))
            pmr->xy = 0;
        else
            pmr->xy = xx1 * xy2,
                pmr->ty += tx1 * xy2;
        pmr->xx = xx1 * xx2;
        if (is_fzero(yx2))
            pmr->yx = 0;
        else
            pmr->yx = yy1 * yx2,
                pmr->tx += ty1 * yx2;
        pmr->yy = yy1 * yy2;
    } else {
        double xy1 = pm1->xy, yx1 = pm1->yx;

        pmr->xx = xx1 * xx2 + xy1 * yx2;
        pmr->xy = xx1 * xy2 + xy1 * yy2;
        pmr->yy = yx1 * xy2 + yy1 * yy2;
        pmr->yx = yx1 * xx2 + yy1 * yx2;
        pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
        pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
    }
    return 0;
}
int
gs_matrix_multiply_double(const gs_matrix_double * pm1, const gs_matrix * pm2, gs_matrix_double * pmr)
{
    double xx1 = pm1->xx, yy1 = pm1->yy;
    double tx1 = pm1->tx, ty1 = pm1->ty;
    double xx2 = pm2->xx, yy2 = pm2->yy;
    double xy2 = pm2->xy, yx2 = pm2->yx;

    if (is_xxyy(pm1)) {
        pmr->tx = tx1 * xx2 + pm2->tx;
        pmr->ty = ty1 * yy2 + pm2->ty;
        if (is_fzero(xy2))
            pmr->xy = 0;
        else
            pmr->xy = xx1 * xy2,
                pmr->ty += tx1 * xy2;
        pmr->xx = xx1 * xx2;
        if (is_fzero(yx2))
            pmr->yx = 0;
        else
            pmr->yx = yy1 * yx2,
                pmr->tx += ty1 * yx2;
        pmr->yy = yy1 * yy2;
    } else {
        double xy1 = pm1->xy, yx1 = pm1->yx;

        pmr->xx = xx1 * xx2 + xy1 * yx2;
        pmr->xy = xx1 * xy2 + xy1 * yy2;
        pmr->yy = yx1 * xy2 + yy1 * yy2;
        pmr->yx = yx1 * xx2 + yy1 * yx2;
        pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
        pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
    }
    return 0;
}

/* Invert a matrix.  Return gs_error_undefinedresult if not invertible. */
int
gs_matrix_invert(const gs_matrix * pm, gs_matrix * pmr)
{				/* We have to be careful about fetch/store order, */
    /* because pm might be the same as pmr. */
    if (is_xxyy(pm)) {
        if (is_fzero(pm->xx) || is_fzero(pm->yy))
            return_error(gs_error_undefinedresult);
        pmr->tx = -(pmr->xx = 1.0 / pm->xx) * pm->tx;
        pmr->xy = 0.0;
        pmr->yx = 0.0;
        pmr->ty = -(pmr->yy = 1.0 / pm->yy) * pm->ty;
    } else {
        float mxx = pm->xx, myy = pm->yy, mxy = pm->xy, myx = pm->yx;
        float mtx = pm->tx, mty = pm->ty;
        /* we declare det as double since on at least some computer (i.e. peeves)
           declaring it as a float results in different values for pmr depending
           on whether or not optimization is turned on.  I believe this is caused
           by the compiler keeping the det value in an internal register when
           optimization is enable.  As evidence of this if you add a debugging
           statement to print out det the optimized code acts the same as the
           unoptimized code.  declearing det as double does not change the CET 10-09.ps
           output. */
        double det = (float)(mxx * myy) - (float)(mxy * myx);

        /*
         * We are doing the math as floats instead of doubles to reproduce
         * the results in page 1 of CET 10-09.ps
         */
        if (det == 0)
            return_error(gs_error_undefinedresult);
        pmr->xx = myy / det;
        pmr->xy = -mxy / det;
        pmr->yx = -myx / det;
        pmr->yy = mxx / det;
        pmr->tx = (((float)(mty * myx) - (float)(mtx * myy))) / det;
        pmr->ty = (((float)(mtx * mxy) - (float)(mty * mxx))) / det;
    }
    return 0;
}
int
gs_matrix_invert_to_double(const gs_matrix * pm, gs_matrix_double * pmr)
{				/* We have to be careful about fetch/store order, */
    /* because pm might be the same as pmr. */
    if (is_xxyy(pm)) {
        if (is_fzero(pm->xx) || is_fzero(pm->yy))
            return_error(gs_error_undefinedresult);
        pmr->tx = -(pmr->xx = 1.0 / pm->xx) * pm->tx;
        pmr->xy = 0.0;
        pmr->yx = 0.0;
        pmr->ty = -(pmr->yy = 1.0 / pm->yy) * pm->ty;
    } else {
        double mxx = pm->xx, myy = pm->yy, mxy = pm->xy, myx = pm->yx;
        double mtx = pm->tx, mty = pm->ty;
        double det = (mxx * myy) - (mxy * myx);

        /*
         * We are doing the math as floats instead of doubles to reproduce
         * the results in page 1 of CET 10-09.ps
         */
        if (det == 0)
            return_error(gs_error_undefinedresult);
        pmr->xx = myy / det;
        pmr->xy = -mxy / det;
        pmr->yx = -myx / det;
        pmr->yy = mxx / det;
        pmr->tx = (((mty * myx) - (mtx * myy))) / det;
        pmr->ty = (((mtx * mxy) - (mty * mxx))) / det;
    }
    return 0;
}

/* Translate a matrix, possibly in place. */
int
gs_matrix_translate(const gs_matrix * pm, double dx, double dy, gs_matrix * pmr)
{
    gs_point trans;
    int code = gs_distance_transform(dx, dy, pm, &trans);

    if (code < 0)
        return code;
    if (pmr != pm)
        *pmr = *pm;
    pmr->tx += trans.x;
    pmr->ty += trans.y;
    return 0;
}

/* Scale a matrix, possibly in place. */
int
gs_matrix_scale(const gs_matrix * pm, double sx, double sy, gs_matrix * pmr)
{
    pmr->xx = pm->xx * sx;
    pmr->xy = pm->xy * sx;
    pmr->yx = pm->yx * sy;
    pmr->yy = pm->yy * sy;
    if (pmr != pm) {
        pmr->tx = pm->tx;
        pmr->ty = pm->ty;
    }
    return 0;
}

/* Rotate a matrix, possibly in place.  The angle is in degrees. */
int
gs_matrix_rotate(const gs_matrix * pm, double ang, gs_matrix * pmr)
{
    double mxx, mxy;
    gs_sincos_t sincos;

    gs_sincos_degrees(ang, &sincos);
    mxx = pm->xx, mxy = pm->xy;
    pmr->xx = sincos.cos * mxx + sincos.sin * pm->yx;
    pmr->xy = sincos.cos * mxy + sincos.sin * pm->yy;
    pmr->yx = sincos.cos * pm->yx - sincos.sin * mxx;
    pmr->yy = sincos.cos * pm->yy - sincos.sin * mxy;
    if (pmr != pm) {
        pmr->tx = pm->tx;
        pmr->ty = pm->ty;
    }
    return 0;
}

/* ------ Coordinate transformations (floating point) ------ */

/* Note that all the transformation routines take separate */
/* x and y arguments, but return their result in a point. */

/* Transform a point. */
int
gs_point_transform(double x, double y, const gs_matrix * pmat,
                   gs_point * ppt)
{
    /*
     * The float casts are there to reproduce results in CET 10-01.ps
     * page 4.
     */
    ppt->x = (float)(x * pmat->xx) + pmat->tx;
    ppt->y = (float)(y * pmat->yy) + pmat->ty;
    if (!is_fzero(pmat->yx))
        ppt->x += (float)(y * pmat->yx);
    if (!is_fzero(pmat->xy))
        ppt->y += (float)(x * pmat->xy);
    return 0;
}

/* Inverse-transform a point. */
/* Return gs_error_undefinedresult if the matrix is not invertible. */
int
gs_point_transform_inverse(double x, double y, const gs_matrix * pmat,
                           gs_point * ppt)
{
    if (is_xxyy(pmat)) {
        if (is_fzero(pmat->xx) || is_fzero(pmat->yy))
            return_error(gs_error_undefinedresult);
        ppt->x = (x - pmat->tx) / pmat->xx;
        ppt->y = (y - pmat->ty) / pmat->yy;
        return 0;
    } else if (is_xyyx(pmat)) {
        if (is_fzero(pmat->xy) || is_fzero(pmat->yx))
            return_error(gs_error_undefinedresult);
        ppt->x = (y - pmat->ty) / pmat->xy;
        ppt->y = (x - pmat->tx) / pmat->yx;
        return 0;
    } else {			/* There are faster ways to do this, */
        /* but we won't implement one unless we have to. */
        gs_matrix imat;
        int code = gs_matrix_invert(pmat, &imat);

        if (code < 0)
            return code;
        return gs_point_transform(x, y, &imat, ppt);
    }
}

/* Transform a distance. */
int
gs_distance_transform(double dx, double dy, const gs_matrix * pmat,
                      gs_point * pdpt)
{
    pdpt->x = dx * pmat->xx;
    pdpt->y = dy * pmat->yy;
    if (!is_fzero(pmat->yx))
        pdpt->x += dy * pmat->yx;
    if (!is_fzero(pmat->xy))
        pdpt->y += dx * pmat->xy;
    return 0;
}

/* Inverse-transform a distance. */
/* Return gs_error_undefinedresult if the matrix is not invertible. */
int
gs_distance_transform_inverse(double dx, double dy,
                              const gs_matrix * pmat, gs_point * pdpt)
{
    if (is_xxyy(pmat)) {
        if (is_fzero(pmat->xx) || is_fzero(pmat->yy))
            return_error(gs_error_undefinedresult);
        pdpt->x = dx / pmat->xx;
        pdpt->y = dy / pmat->yy;
    } else if (is_xyyx(pmat)) {
        if (is_fzero(pmat->xy) || is_fzero(pmat->yx))
            return_error(gs_error_undefinedresult);
        pdpt->x = dy / pmat->xy;
        pdpt->y = dx / pmat->yx;
    } else {
        double det = pmat->xx * pmat->yy - pmat->xy * pmat->yx;

        if (det == 0)
            return_error(gs_error_undefinedresult);
        pdpt->x = (dx * pmat->yy - dy * pmat->yx) / det;
        pdpt->y = (dy * pmat->xx - dx * pmat->xy) / det;
    }
    return 0;
}

/* Compute the bounding box of 4 points. */
int
gs_points_bbox(const gs_point pts[4], gs_rect * pbox)
{
#define assign_min_max(vmin, vmax, v0, v1)\
  if ( v0 < v1 ) vmin = v0, vmax = v1; else vmin = v1, vmax = v0
#define assign_min_max_4(vmin, vmax, v0, v1, v2, v3)\
  { double min01, max01, min23, max23;\
    assign_min_max(min01, max01, v0, v1);\
    assign_min_max(min23, max23, v2, v3);\
    vmin = min(min01, min23);\
    vmax = max(max01, max23);\
  }
    assign_min_max_4(pbox->p.x, pbox->q.x,
                     pts[0].x, pts[1].x, pts[2].x, pts[3].x);
    assign_min_max_4(pbox->p.y, pbox->q.y,
                     pts[0].y, pts[1].y, pts[2].y, pts[3].y);
#undef assign_min_max
#undef assign_min_max_4
    return 0;
}

/* Transform or inverse-transform a bounding box. */
/* Return gs_error_undefinedresult if the matrix is not invertible. */
static int
bbox_transform_either_only(const gs_rect * pbox_in, const gs_matrix * pmat,
                           gs_point pts[4],
     int (*point_xform) (double, double, const gs_matrix *, gs_point *))
{
    int code;

    if ((code = (*point_xform) (pbox_in->p.x, pbox_in->p.y, pmat, &pts[0])) < 0 ||
        (code = (*point_xform) (pbox_in->p.x, pbox_in->q.y, pmat, &pts[1])) < 0 ||
        (code = (*point_xform) (pbox_in->q.x, pbox_in->p.y, pmat, &pts[2])) < 0 ||
     (code = (*point_xform) (pbox_in->q.x, pbox_in->q.y, pmat, &pts[3])) < 0
        )
        DO_NOTHING;
    return code;
}

static int
bbox_transform_either(const gs_rect * pbox_in, const gs_matrix * pmat,
                      gs_rect * pbox_out,
     int (*point_xform) (double, double, const gs_matrix *, gs_point *))
{
    int code;

    /*
     * In principle, we could transform only one point and two
     * distance vectors; however, because of rounding, we will only
     * get fully consistent results if we transform all 4 points.
     * We must compute the max and min after transforming,
     * since a rotation may be involved.
     */
    gs_point pts[4];

    if ((code = bbox_transform_either_only(pbox_in, pmat, pts, point_xform)) < 0)
        return code;
    return gs_points_bbox(pts, pbox_out);
}
int
gs_bbox_transform(const gs_rect * pbox_in, const gs_matrix * pmat,
                  gs_rect * pbox_out)
{
    return bbox_transform_either(pbox_in, pmat, pbox_out,
                                 gs_point_transform);
}
int
gs_bbox_transform_only(const gs_rect * pbox_in, const gs_matrix * pmat,
                       gs_point points[4])
{
    return bbox_transform_either_only(pbox_in, pmat, points,
                                      gs_point_transform);
}
int
gs_bbox_transform_inverse(const gs_rect * pbox_in, const gs_matrix * pmat,
                          gs_rect * pbox_out)
{
    int code = bbox_transform_either(pbox_in, pmat, pbox_out,
                                 gs_point_transform_inverse);

    return code;
}

/* ------ Coordinate transformations (to fixed point) ------ */

#define f_fits_in_fixed(f) f_fits_in_bits(f, fixed_int_bits)

/* Make a gs_matrix_fixed from a gs_matrix. */
int
gs_matrix_fixed_from_matrix(gs_matrix_fixed *pfmat, const gs_matrix *pmat)
{
    *(gs_matrix *)pfmat = *pmat;
    if (f_fits_in_fixed(pmat->tx) && f_fits_in_fixed(pmat->ty)) {
        pfmat->tx = fixed2float(pfmat->tx_fixed = float2fixed(pmat->tx));
        pfmat->ty = fixed2float(pfmat->ty_fixed = float2fixed(pmat->ty));
        pfmat->txy_fixed_valid = true;
    } else {
        pfmat->txy_fixed_valid = false;
    }
    return 0;
}

/* Transform a point with a fixed-point result. */
int
gs_point_transform2fixed(const gs_matrix_fixed * pmat,
                         double x, double y, gs_fixed_point * ppt)
{
    fixed px, py, t;
    double xtemp, ytemp;
    int code;

    if (!pmat->txy_fixed_valid) {	/* The translation is out of range.  Do the */
        /* computation in floating point, and convert to */
        /* fixed at the end. */
        gs_point fpt;

        gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
        if (!(f_fits_in_fixed(fpt.x) && f_fits_in_fixed(fpt.y)))
            return_error(gs_error_limitcheck);
        ppt->x = float2fixed(fpt.x);
        ppt->y = float2fixed(fpt.y);
        return 0;
    }
    if (!is_fzero(pmat->xy)) {	/* Hope for 90 degree rotation */
        if ((code = CHECK_DFMUL2FIXED_VARS(px, y, pmat->yx, xtemp)) < 0 ||
            (code = CHECK_DFMUL2FIXED_VARS(py, x, pmat->xy, ytemp)) < 0
            )
            return code;
        FINISH_DFMUL2FIXED_VARS(px, xtemp);
        FINISH_DFMUL2FIXED_VARS(py, ytemp);
        if (!is_fzero(pmat->xx)) {
            if ((code = CHECK_DFMUL2FIXED_VARS(t, x, pmat->xx, xtemp)) < 0)
                return code;
            FINISH_DFMUL2FIXED_VARS(t, xtemp);
            if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
                return code;
        }
        if (!is_fzero(pmat->yy)) {
            if ((code = CHECK_DFMUL2FIXED_VARS(t, y, pmat->yy, ytemp)) < 0)
                return code;
            FINISH_DFMUL2FIXED_VARS(t, ytemp);
            if ((code = CHECK_SET_FIXED_SUM(py, py, t)) < 0)
                return code;
        }
    } else {
        if ((code = CHECK_DFMUL2FIXED_VARS(px, x, pmat->xx, xtemp)) < 0 ||
            (code = CHECK_DFMUL2FIXED_VARS(py, y, pmat->yy, ytemp)) < 0
            )
            return code;
        FINISH_DFMUL2FIXED_VARS(px, xtemp);
        FINISH_DFMUL2FIXED_VARS(py, ytemp);
        if (!is_fzero(pmat->yx)) {
            if ((code = CHECK_DFMUL2FIXED_VARS(t, y, pmat->yx, ytemp)) < 0)
                return code;
            FINISH_DFMUL2FIXED_VARS(t, ytemp);
            if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
                return code;
        }
    }
    if (((code = CHECK_SET_FIXED_SUM(ppt->x, px, pmat->tx_fixed)) < 0) ||
        ((code = CHECK_SET_FIXED_SUM(ppt->y, py, pmat->ty_fixed)) < 0) )
        return code;
    return 0;
}

#if PRECISE_CURRENTPOINT
/* Transform a point with a fixed-point result. */
/* Used for the best precision of the current point,
   see comment in clamp_point_aux. */
int
gs_point_transform2fixed_rounding(const gs_matrix_fixed * pmat,
                         double x, double y, gs_fixed_point * ppt)
{
    gs_point fpt;

    gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
    if (!(f_fits_in_fixed(fpt.x) && f_fits_in_fixed(fpt.y)))
        return_error(gs_error_limitcheck);
    ppt->x = float2fixed_rounded(fpt.x);
    ppt->y = float2fixed_rounded(fpt.y);
    return 0;
}
#endif

/* Transform a distance with a fixed-point result. */
int
gs_distance_transform2fixed(const gs_matrix_fixed * pmat,
                            double dx, double dy, gs_fixed_point * ppt)
{
    fixed px, py, t;
    double xtemp, ytemp;
    int code;

    if ((code = CHECK_DFMUL2FIXED_VARS(px, dx, pmat->xx, xtemp)) < 0 ||
        (code = CHECK_DFMUL2FIXED_VARS(py, dy, pmat->yy, ytemp)) < 0
        )
        return code;
    FINISH_DFMUL2FIXED_VARS(px, xtemp);
    FINISH_DFMUL2FIXED_VARS(py, ytemp);
    if (!is_fzero(pmat->yx)) {
        if ((code = CHECK_DFMUL2FIXED_VARS(t, dy, pmat->yx, ytemp)) < 0)
            return code;
        FINISH_DFMUL2FIXED_VARS(t, ytemp);
        if ((code = CHECK_SET_FIXED_SUM(px, px, t)) < 0)
            return code;
    }
    if (!is_fzero(pmat->xy)) {
        if ((code = CHECK_DFMUL2FIXED_VARS(t, dx, pmat->xy, xtemp)) < 0)
            return code;
        FINISH_DFMUL2FIXED_VARS(t, xtemp);
        if ((code = CHECK_SET_FIXED_SUM(py, py, t)) < 0)
            return code;
    }
    ppt->x = px;
    ppt->y = py;
    return 0;
}

/* ------ Serialization ------ */

/*
 * For maximum conciseness in band lists, we write a matrix as a control
 * byte followed by 0 to 6 values.  The control byte has the format
 * AABBCD00.  AA and BB control (xx,yy) and (xy,yx) as follows:
 *	00 = values are (0.0, 0.0)
 *	01 = values are (V, V) [1 value follows]
 *	10 = values are (V, -V) [1 value follows]
 *	11 = values are (U, V) [2 values follow]
 * C and D control tx and ty as follows:
 *	0 = value is 0.0
 *	1 = value follows
 * The following code is the only place that knows this representation.
 */

/* Put a matrix on a stream. */
int
sput_matrix(stream *s, const gs_matrix *pmat)
{
    byte buf[1 + 6 * sizeof(float)];
    byte *cp = buf + 1;
    byte b = 0;
    float coeff[6];
    int i;
    uint ignore;

    coeff[0] = pmat->xx;
    coeff[1] = pmat->xy;
    coeff[2] = pmat->yx;
    coeff[3] = pmat->yy;
    coeff[4] = pmat->tx;
    coeff[5] = pmat->ty;
    for (i = 0; i < 4; i += 2) {
        float u = coeff[i], v = coeff[i ^ 3];

        b <<= 2;
        if (u != 0 || v != 0) {
            memcpy(cp, &u, sizeof(float));
            cp += sizeof(float);

            if (v == u)
                b += 1;
            else if (v == -u)
                b += 2;
            else {
                b += 3;
                memcpy(cp, &v, sizeof(float));
                cp += sizeof(float);
            }
        }
    }
    for (; i < 6; ++i) {
        float v = coeff[i];

        b <<= 1;
        if (v != 0) {
            ++b;
            memcpy(cp, &v, sizeof(float));
            cp += sizeof(float);
        }
    }
    buf[0] = b << 2;
    return sputs(s, buf, cp - buf, &ignore);
}

/* Get a matrix from a stream. */
int
sget_matrix(stream *s, gs_matrix *pmat)
{
    int b = sgetc(s);
    float coeff[6];
    int i;
    int status;
    uint nread;

    if (b < 0)
        return b;
    for (i = 0; i < 4; i += 2, b <<= 2)
        if (!(b & 0xc0))
            coeff[i] = coeff[i ^ 3] = 0.0;
        else {
            float value;

            status = sgets(s, (byte *)&value, sizeof(value), &nread);
            if (status < 0 && status != EOFC)
                return_error(gs_error_ioerror);
            coeff[i] = value;
            switch ((b >> 6) & 3) {
                case 1:
                    coeff[i ^ 3] = value;
                    break;
                case 2:
                    coeff[i ^ 3] = -value;
                    break;
                case 3:
                    status = sgets(s, (byte *)&coeff[i ^ 3],
                                   sizeof(coeff[0]), &nread);
                    if (status < 0 && status != EOFC)
                        return_error(gs_error_ioerror);
            }
        }
    for (; i < 6; ++i, b <<= 1)
        if (b & 0x80) {
            status = sgets(s, (byte *)&coeff[i], sizeof(coeff[0]), &nread);
            if (status < 0 && status != EOFC)
                return_error(gs_error_ioerror);
        } else
            coeff[i] = 0.0;
    pmat->xx = coeff[0];
    pmat->xy = coeff[1];
    pmat->yx = coeff[2];
    pmat->yy = coeff[3];
    pmat->tx = coeff[4];
    pmat->ty = coeff[5];
    return 0;
}

/* Compare two matrices */
int
gs_matrix_compare(const gs_matrix *pmat1, const gs_matrix *pmat2) {
  if (pmat1->xx != pmat2->xx)
    return(1);
  if (pmat1->xy != pmat2->xy)
    return(1);
  if (pmat1->yx != pmat2->yx)
    return(1);
  if (pmat1->yy != pmat2->yy)
    return(1);
  if (pmat1->tx != pmat2->tx)
    return(1);
  if (pmat1->ty != pmat2->ty)
    return(1);
  return(0);
}