about summary refs log tree commit diff
path: root/editor/pnmrotate.c
blob: da5de3a8985b32a39c84e025b0aa0a28a1c5c432 (plain) (blame)
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/* pnmrotate.c - read a portable anymap and rotate it by some angle
**
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#define _XOPEN_SOURCE   /* get M_PI in math.h */

#include <math.h>
#include <assert.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "ppm.h"
#include "pnm.h"

#define SCALE 4096
#define HALFSCALE 2048

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFilespec;  /* Filespecs of input file */
    float angle;                /* Angle to rotate, in radians */
    unsigned int noantialias;
    const char * background;  /* NULL if none */
    unsigned int keeptemp;  /* For debugging */
    unsigned int verbose;
};


enum rotationDirection {CLOCKWISE, COUNTERCLOCKWISE};

struct shearParm {
    /* These numbers tell how to shear a pixel, but I haven't figured out 
       yet exactly what each means.
    */
    long fracnew0;
    long omfracnew0;
    unsigned int shiftWhole;
    unsigned int shiftUnits;
};



static void
parseCommandLine(int argc, char ** const argv,
                 struct cmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry *option_def = malloc(100*sizeof(optEntry));
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int backgroundSpec;
    unsigned int option_def_index;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "background",  OPT_STRING, &cmdlineP->background, 
            &backgroundSpec,        0);
    OPTENT3(0, "noantialias", OPT_FLAG,   NULL, 
            &cmdlineP->noantialias, 0);
    OPTENT3(0, "keeptemp",    OPT_FLAG,   NULL, 
            &cmdlineP->keeptemp,    0);
    OPTENT3(0, "verbose",     OPT_FLAG,   NULL, 
            &cmdlineP->verbose,     0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = TRUE;  /* We may have parms that are negative numbers */

    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (!backgroundSpec)
        cmdlineP->background = NULL;

    if (argc-1 < 1)
        pm_error("You must specify at least one argument:  the angle "
                 "to rotate.");
    else {
        int rc;
        float angleArg;

        rc = sscanf(argv[1], "%f", &angleArg);

        if (rc != 1)
            pm_error("Invalid angle argument: '%s'.  Must be a floating point "
                     "number of degrees.", argv[1]);
        else if (angleArg < -90.0 || angleArg > 90.0)
            pm_error("angle must be between -90 and 90, inclusive.  "
                     "You specified %f.  "
                     "Use 'pamflip' for other rotations.", angleArg);
        else {
            /* Convert to radians */
            cmdlineP->angle = angleArg * M_PI / 180.0;

            if (argc-1 < 2)
                cmdlineP->inputFilespec = "-";
            else {
                cmdlineP->inputFilespec = argv[2];
                
                if (argc-1 > 2)
                    pm_error("Program takes at most two arguments "
                             "(angle and filename).  You specified %d",
                             argc-1);
            }
        }
    }
}



static void
storeImage(const char * const fileName,
           xel **       const xels,
           unsigned int const cols,
           unsigned int const rows,
           xelval       const maxval,
           int          const format) {

    FILE * ofP;

    ofP = pm_openw(fileName);

    pnm_writepnm(ofP, xels, cols, rows, maxval, format, 0);

    pm_close(ofP);
}

  

static void
computeNewFormat(bool     const antialias, 
                 int      const format,
                 xelval   const maxval,
                 int *    const newformatP,
                 xelval * const newmaxvalP) {

    if (antialias && PNM_FORMAT_TYPE(format) == PBM_TYPE) {
        *newformatP = PGM_TYPE;
        *newmaxvalP = PGM_MAXMAXVAL;
        pm_message("promoting from PBM to PGM - "
                   "use -noantialias to avoid this");
    } else {
        *newformatP = format;
        *newmaxvalP = maxval;
    }
}



static xel
backgroundColor(const char * const backgroundColorName,
                xel *        const topRow,
                int          const cols,
                xelval       const maxval,
                int          const format) {

    xel retval;

    if (backgroundColorName) {
        retval = pnm_parsecolorxel(backgroundColorName, maxval, format);
    } else 
        retval = pnm_backgroundxelrow(topRow, cols, maxval, format);

    return retval;
}



static void
reportBackground(xel const bgColor) {

    pm_message("Background color %u/%u/%u",
               PPM_GETR(bgColor), PPM_GETG(bgColor), PPM_GETB(bgColor));
}



static void
shearX(xel * const inRow, 
       xel * const outRow, 
       int   const cols, 
       int   const format,
       xel   const bgxel,
       bool  const antialias,
       float const shiftAmount,
       int   const newcols) {
/*----------------------------------------------------------------------------
   Shift a the row inRow[] right by 'shiftAmount' pixels and return the
   result as outRow[].

   The input row is 'cols' columns wide, whereas the output row is
   'newcols'.

   The format of the input row is 'format'.

   We shift the row on a background of color 'bgxel'.

   The output row has the same format and maxval as the input.
   
   'shiftAmount' may not be negative.
   
   'shiftAmount' can be fractional, so we either just go by the
   nearest integer value or mix pixels to achieve the shift, depending
   on 'antialias'.
-----------------------------------------------------------------------------*/
    assert(shiftAmount >= 0.0);

    if (antialias) {
        unsigned int const shiftWhole = (unsigned int) shiftAmount;
        long const fracShift = (shiftAmount - shiftWhole) * SCALE;
        long const omfracShift = SCALE - fracShift;

        unsigned int col;
        xel * nxP;
        xel prevxel;

        for (col = 0; col < newcols; ++col)
            outRow[col] = bgxel;
            
        prevxel = bgxel;
        for (col = 0, nxP = &(outRow[shiftWhole]);
             col < cols; ++col, ++nxP) {

            xel const p = inRow[col];

            switch (PNM_FORMAT_TYPE(format)) {
            case PPM_TYPE:
                PPM_ASSIGN(*nxP,
                           (fracShift * PPM_GETR(prevxel) 
                            + omfracShift * PPM_GETR(p) 
                            + HALFSCALE) / SCALE,
                           (fracShift * PPM_GETG(prevxel) 
                            + omfracShift * PPM_GETG(p) 
                            + HALFSCALE) / SCALE,
                           (fracShift * PPM_GETB(prevxel) 
                            + omfracShift * PPM_GETB(p) 
                            + HALFSCALE) / SCALE );
                break;
                
            default:
                PNM_ASSIGN1(*nxP,
                            (fracShift * PNM_GET1(prevxel) 
                             + omfracShift * PNM_GET1(p) 
                             + HALFSCALE) / SCALE );
                break;
            }
            prevxel = p;
        }
        if (fracShift> 0 && shiftWhole + cols < newcols) {
            switch (PNM_FORMAT_TYPE(format)) {
            case PPM_TYPE:
                PPM_ASSIGN(*nxP,
                           (fracShift * PPM_GETR(prevxel) 
                            + omfracShift * PPM_GETR(bgxel) 
                            + HALFSCALE) / SCALE,
                           (fracShift * PPM_GETG(prevxel) 
                            + omfracShift * PPM_GETG(bgxel) 
                            + HALFSCALE) / SCALE,
                           (fracShift * PPM_GETB(prevxel) 
                            + omfracShift * PPM_GETB(bgxel) 
                            + HALFSCALE) / SCALE );
                break;
                    
            default:
                PNM_ASSIGN1(*nxP,
                            (fracShift * PNM_GET1(prevxel) 
                             + omfracShift * PNM_GET1(bgxel) 
                             + HALFSCALE) / SCALE );
                break;
            }
        }
    } else {
        unsigned int const shiftCols = (unsigned int) (shiftAmount + 0.5);
        unsigned int col;
        unsigned int outcol;

        outcol = 0;  /* initial value */
        
        for (col = 0; col < shiftCols; ++col)
            outRow[outcol++] = bgxel;
        for (col = 0; col < cols; ++col)
            outRow[outcol++] = inRow[col];
        for (col = shiftCols + cols; col < newcols; ++col)
            outRow[outcol++] = bgxel;
        
        assert(outcol == newcols);
    }
}



static void
shearXFromInputFile(FILE *                 const ifP,
                    unsigned int           const cols,
                    unsigned int           const rows,
                    xelval                 const maxval,
                    int                    const format,
                    enum rotationDirection const direction,
                    float                  const xshearfac,
                    xelval                 const newmaxval,
                    int                    const newformat,
                    bool                   const antialias,
                    const char *           const background,
                    xel ***                const shearedXelsP,
                    unsigned int *         const newcolsP,
                    xel *                  const bgColorP) {
/*----------------------------------------------------------------------------
   Shear X from input file into newly malloced xel array.  Return that
   array as *shearedColsP, and its width as *tempColsP.  Everything else
   about the sheared image is the same as for the input image.

   The input image on file 'ifP' is described by 'cols', 'rows',
   'maxval', and 'format'.

   Along the way, figure out what the background color of the output should
   be based on the contents of the file and the user's directive
   'background' and return that as *bgColorP.
-----------------------------------------------------------------------------*/
    unsigned int const maxShear = (rows - 0.5) * xshearfac + 0.5;
    unsigned int const newcols = cols + maxShear;
    
    xel ** shearedXels;
    xel * xelrow;
    xel bgColor;
    unsigned int row;

    shearedXels = pnm_allocarray(newcols, rows);

    xelrow = pnm_allocrow(cols);

    for (row = 0; row < rows; ++row) {
        /* The shear factor is designed to shear over the entire width
           from the left edge of of the left pixel to the right edge of
           the right pixel.  We use the distance of the center of this
           pixel from the relevant edge to compute shift amount:
        */
        float const xDistance = 
            (direction == COUNTERCLOCKWISE ? row + 0.5 : (rows-0.5 - row));
        float const shiftAmount = xshearfac * xDistance;

        pnm_readpnmrow(ifP, xelrow, cols, maxval, format);

        pnm_promoteformatrow(xelrow, cols, maxval, format, 
                             newmaxval, newformat);

        if (row == 0)
            bgColor =
                backgroundColor(background, xelrow, cols, newmaxval, format);

        shearX(xelrow, shearedXels[row], cols, newformat, bgColor,
               antialias, shiftAmount, newcols);
    }
    pnm_freerow(xelrow);

    *shearedXelsP = shearedXels;
    *newcolsP = newcols;

    assert(rows >= 1);  /* Ergo, bgColor is defined */
    *bgColorP = bgColor;
}



static void 
shearYNoAntialias(xel **           const inxels,
                  xel **           const outxels,
                  int              const cols,
                  int              const inrows,
                  int              const outrows,
                  int              const format,
                  xel              const bgColor,
                  struct shearParm const shearParm[]) {
/*----------------------------------------------------------------------------
   Shear the image in 'inxels' ('cols' x 'inrows') vertically into
   'outxels' ('cols' x 'outrows'), both format 'format'.  shearParm[X]
   tells how much to shear pixels in Column X (clipped to Rows 0
   through 'outrow' -1) and 'bgColor' is what to use for background
   where there is none of the input in the output.

   We do not do any antialiasing.  We simply move whole pixels.

   We go row by row instead of column by column to save real memory.  Going
   row by row, the working set is only a few pages, whereas going column by
   column, it would be one page per output row plus one page per input row.
-----------------------------------------------------------------------------*/
    unsigned int inrow;
    unsigned int outrow;

    /* Fill the output with background */
    for (outrow = 0; outrow < outrows; ++outrow) {
        unsigned int col;
        for (col = 0; col < cols; ++col)
            outxels[outrow][col] = bgColor;
    }

    /* Overlay that background with sheared image */
    for (inrow = 0; inrow < inrows; ++inrow) {
        unsigned int col;
        for (col = 0; col < cols; ++col) {
            int const outrow = inrow + shearParm[col].shiftUnits;
            if (outrow >= 0 && outrow < outrows)
                outxels[outrow][col] = inxels[inrow][col];
        }
    }
}



static void
shearYColAntialias(xel ** const inxels, 
                   xel ** const outxels,
                   int    const col,
                   int    const inrows,
                   int    const outrows,
                   int    const format,
                   xel    const bgxel,
                   struct shearParm shearParm[]) {
/*-----------------------------------------------------------------------------
  Shear a column vertically.
-----------------------------------------------------------------------------*/
    long const fracnew0   = shearParm[col].fracnew0;
    long const omfracnew0 = shearParm[col].omfracnew0;
    int  const shiftWhole = shearParm[col].shiftWhole;
        
    int outrow;

    xel prevxel;
    int inrow;
        
    /* Initialize everything to background color */
    for (outrow = 0; outrow < outrows; ++outrow)
        outxels[outrow][col] = bgxel;

    prevxel = bgxel;
    for (inrow = 0; inrow < inrows; ++inrow) {
        int const outrow = inrow + shiftWhole;

        if (outrow >= 0 && outrow < outrows) {
            xel * const nxP = &(outxels[outrow][col]);
            xel const x = inxels[inrow][col];
            switch ( PNM_FORMAT_TYPE(format) ) {
            case PPM_TYPE:
                PPM_ASSIGN(*nxP,
                           (fracnew0 * PPM_GETR(prevxel) 
                            + omfracnew0 * PPM_GETR(x) 
                            + HALFSCALE) / SCALE,
                           (fracnew0 * PPM_GETG(prevxel) 
                            + omfracnew0 * PPM_GETG(x) 
                            + HALFSCALE) / SCALE,
                           (fracnew0 * PPM_GETB(prevxel) 
                            + omfracnew0 * PPM_GETB(x) 
                            + HALFSCALE) / SCALE );
                break;
                        
            default:
                PNM_ASSIGN1(*nxP,
                            (fracnew0 * PNM_GET1(prevxel) 
                             + omfracnew0 * PNM_GET1(x) 
                             + HALFSCALE) / SCALE );
                break;
            }
            prevxel = x;
        }
    }
    if (fracnew0 > 0 && shiftWhole + inrows < outrows) {
        xel * const nxP = &(outxels[shiftWhole + inrows][col]);
        switch (PNM_FORMAT_TYPE(format)) {
        case PPM_TYPE:
            PPM_ASSIGN(*nxP,
                       (fracnew0 * PPM_GETR(prevxel) 
                        + omfracnew0 * PPM_GETR(bgxel) 
                        + HALFSCALE) / SCALE,
                       (fracnew0 * PPM_GETG(prevxel) 
                        + omfracnew0 * PPM_GETG(bgxel) 
                        + HALFSCALE) / SCALE,
                       (fracnew0 * PPM_GETB(prevxel) 
                        + omfracnew0 * PPM_GETB(bgxel) 
                        + HALFSCALE) / SCALE);
            break;
                
        default:
            PNM_ASSIGN1(*nxP,
                        (fracnew0 * PNM_GET1(prevxel) 
                         + omfracnew0 * PNM_GET1(bgxel) 
                         + HALFSCALE) / SCALE);
            break;
        }
    }
} 



static void
shearImageY(xel **                 const inxels,
            int                    const cols,
            int                    const inrows,
            int                    const format,
            xel                    const bgxel,
            bool                   const antialias,
            enum rotationDirection const direction,
            float                  const yshearfac,
            int                    const yshearjunk,
            xel ***                const outxelsP,
            unsigned int *         const outrowsP) {
    
    unsigned int const maxShear = (cols - 0.5) * yshearfac + 0.5;
    unsigned int const outrows = inrows + maxShear - 2 * yshearjunk;

    struct shearParm * shearParm;  /* malloc'ed */
    int col;
    xel ** outxels;
    
    outxels = pnm_allocarray(cols, outrows);

    MALLOCARRAY(shearParm, cols);
    if (shearParm == NULL)
        pm_error("Unable to allocate memory for shearParm");

    for (col = 0; col < cols; ++col) {
        /* The shear factor is designed to shear over the entire height
           from the top edge of of the top pixel to the bottom edge of
           the bottom pixel.  We use the distance of the center of this
           pixel from the relevant edge to compute shift amount:
        */
        float const yDistance = 
            (direction == CLOCKWISE ? col + 0.5 : (cols-0.5 - col));
        float const shiftAmount = yshearfac * yDistance;

        shearParm[col].fracnew0   = (shiftAmount - (int)shiftAmount) * SCALE;
        shearParm[col].omfracnew0 = SCALE - shearParm[col].fracnew0;
        shearParm[col].shiftWhole = (int)shiftAmount - yshearjunk;
        shearParm[col].shiftUnits = (int)(shiftAmount + 0.5) - yshearjunk;
    }
    if (!antialias)
        shearYNoAntialias(inxels, outxels, cols, inrows, outrows, format,
                          bgxel, shearParm);
    else {
        /* TODO: do this row-by-row, same as for noantialias, to save
           real memory.
        */
        for (col = 0; col < cols; ++col) 
            shearYColAntialias(inxels, outxels, col, inrows, outrows, format, 
                               bgxel, shearParm);
    }
    free(shearParm);
    
    *outxelsP = outxels;
    *outrowsP = outrows;
}



static void
shearFinal(xel * const inRow, 
           xel * const outRow, 
           int   const incols, 
           int   const outcols,
           int   const format,
           xel   const bgxel,
           bool  const antialias,
           float const shiftAmount,
           int   const x2shearjunk) {


    assert(shiftAmount >= 0.0);

    {
        unsigned int col;
        for (col = 0; col < outcols; ++col)
            outRow[col] = bgxel;
    }

    if (antialias) {
        long const fracnew0   = (shiftAmount - (int) shiftAmount) * SCALE; 
        long const omfracnew0 = SCALE - fracnew0; 
        unsigned int const shiftWhole = (int)shiftAmount - x2shearjunk;

        xel prevxel;
        unsigned int col;

        prevxel = bgxel;
        for (col = 0; col < incols; ++col) {
            int const new = shiftWhole + col;
            if (new >= 0 && new < outcols) {
                xel * const nxP = &(outRow[new]);
                xel const x = inRow[col];
                switch (PNM_FORMAT_TYPE(format)) {
                case PPM_TYPE:
                    PPM_ASSIGN(*nxP,
                               (fracnew0 * PPM_GETR(prevxel) 
                                + omfracnew0 * PPM_GETR(x) 
                                + HALFSCALE) / SCALE,
                               (fracnew0 * PPM_GETG(prevxel) 
                                + omfracnew0 * PPM_GETG(x) 
                                + HALFSCALE) / SCALE,
                               (fracnew0 * PPM_GETB(prevxel) 
                                + omfracnew0 * PPM_GETB(x) 
                                + HALFSCALE) / SCALE);
                    break;
                    
                default:
                    PNM_ASSIGN1(*nxP,
                                (fracnew0 * PNM_GET1(prevxel) 
                                 + omfracnew0 * PNM_GET1(x) 
                                 + HALFSCALE) / SCALE );
                    break;
                }
                prevxel = x;
            }
        }
        if (fracnew0 > 0 && shiftWhole + incols < outcols) {
            xel * const nxP = &(outRow[shiftWhole + incols]);
            switch (PNM_FORMAT_TYPE(format)) {
            case PPM_TYPE:
                PPM_ASSIGN(*nxP,
                           (fracnew0 * PPM_GETR(prevxel) 
                            + omfracnew0 * PPM_GETR(bgxel) 
                            + HALFSCALE) / SCALE,
                           (fracnew0 * PPM_GETG(prevxel) 
                            + omfracnew0 * PPM_GETG(bgxel) 
                            + HALFSCALE) / SCALE,
                           (fracnew0 * PPM_GETB(prevxel) 
                            + omfracnew0 * PPM_GETB(bgxel) 
                            + HALFSCALE) / SCALE);
                break;
                
            default:
                PNM_ASSIGN1(*nxP,
                            (fracnew0 * PNM_GET1(prevxel) 
                             + omfracnew0 * PNM_GET1(bgxel) 
                             + HALFSCALE) / SCALE );
                break;
            }
        }
    } else {
        unsigned int const shiftCols =
            (unsigned int)(shiftAmount + 0.5) - x2shearjunk;

        unsigned int col;
        for (col = 0; col < incols; ++col) {
            unsigned int const outcol = shiftCols + col;
            if (outcol >= 0 && outcol < outcols)
                outRow[outcol] = inRow[col];
        }
    }
}



static void
shearXToOutputFile(FILE *                 const ofP,
                   xel **                 const xels,
                   unsigned int           const cols, 
                   unsigned int           const rows,
                   xelval                 const maxval,
                   int                    const format,
                   enum rotationDirection const direction,
                   float                  const xshearfac,
                   int                    const x2shearjunk,
                   xel                    const bgColor,
                   bool                   const antialias) {
/*----------------------------------------------------------------------------
   Shear horizontally the image in 'xels' and write the result to file
   'ofP'.  'cols', 'rows', 'maxval', and 'format' describe the image in
   'xels'.  They also describe the output image, except that it will be
   wider as dictated by the shearing parameters.

   Shear over background color 'bgColor'.

   Do a smooth pixel-mixing shear iff 'antialias' is true.
-----------------------------------------------------------------------------*/
    unsigned int const maxShear = (rows - 0.5) * xshearfac + 0.5;
    unsigned int const newcols = cols + maxShear - 2 * x2shearjunk;

    unsigned int row;
    xel * xelrow;
    
    pnm_writepnminit(ofP, newcols, rows, maxval, format, 0);

    xelrow = pnm_allocrow(newcols);

    for (row = 0; row < rows; ++row) {
        /* The shear factor is designed to shear over the entire width
           from the left edge of of the left pixel to the right edge of
           the right pixel.  We use the distance of the center of this
           pixel from the relevant edge to compute shift amount:
        */
        float const xDistance = 
            (direction == COUNTERCLOCKWISE ? row + 0.5 : (rows-0.5 - row));
        float const shiftAmount = xshearfac * xDistance;

        shearFinal(xels[row], xelrow, cols, newcols, format, 
                   bgColor, antialias, shiftAmount, x2shearjunk);

        pnm_writepnmrow(ofP, xelrow, newcols, maxval, format, 0);
    }
    pnm_freerow(xelrow);
}



int
main(int argc, char *argv[]) { 

    struct cmdlineInfo cmdline;
    FILE * ifP;
    xel ** shear1xels;
    xel ** shear2xels;
    xel bgColor;
    int rows, cols, format;
    int newformat;
    unsigned int newrows;
    int newRowsWithJunk;
    unsigned int shear1Cols;
    int yshearjunk, x2shearjunk;
    xelval maxval, newmaxval;
    float xshearfac, yshearfac;
    enum rotationDirection direction;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);
    
    computeNewFormat(!cmdline.noantialias, format, maxval, 
                     &newformat, &newmaxval);

    xshearfac = fabs(tan(cmdline.angle / 2.0));
    yshearfac = fabs(sin(cmdline.angle));
    direction = cmdline.angle > 0 ? COUNTERCLOCKWISE : CLOCKWISE;

    /* The algorithm we use, for maximum speed, is 3 simple shears:
       A horizontal, a vertical, and another horizontal.
    */

    shearXFromInputFile(ifP, cols, rows, maxval, format,
                        direction, xshearfac,
                        newmaxval, newformat,
                        !cmdline.noantialias, cmdline.background,
                        &shear1xels, &shear1Cols, &bgColor);
    
    pm_close(ifP);

    if (cmdline.verbose)
        reportBackground(bgColor);

    if (cmdline.keeptemp)
        storeImage("pnmrotate_stage1.pnm", shear1xels, shear1Cols, rows,
                   newmaxval, newformat);

    yshearjunk = (shear1Cols - cols) * yshearfac;
    newRowsWithJunk = (shear1Cols - 1) * yshearfac + rows + 0.999999;
    x2shearjunk = (newRowsWithJunk - rows - yshearjunk - 1) * xshearfac;

    shearImageY(shear1xels, shear1Cols, rows, newformat,
                bgColor, !cmdline.noantialias, direction,
                yshearfac, yshearjunk,
                &shear2xels, &newrows);

    pnm_freearray(shear1xels, rows);

    if (cmdline.keeptemp)
        storeImage("pnmrotate_stage2.pnm", shear2xels, shear1Cols, newrows, 
                   newmaxval, newformat);

    shearXToOutputFile(stdout, shear2xels, shear1Cols, newrows,
                       newmaxval, newformat,
                       direction, xshearfac, x2shearjunk, 
                       bgColor, !cmdline.noantialias);

    pnm_freearray(shear2xels, newrows);
    pm_close(stdout);
    
    return 0;
}