about summary refs log tree commit diff
path: root/editor/pnmcrop.c
blob: 43027da0e91efbe4a9272fe63b25c82048b02673 (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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
/* pnmcrop.c - crop a Netpbm image
**
** Copyright (C) 1988 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.
*/

/* IDEA FOR EFFICIENCY IMPROVEMENT:

   If we have to read the input into a regular file because it is not
   seekable (a pipe), find the borders as we do the copy, so that we
   do 2 passes through the file instead of 3.  Also find the background
   color in that pass to save yet another pass with -sides.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

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

static double const sqrt3 = 1.73205080756887729352;
    /* The square root of 3 */
static double const EPSILON = 1.0e-5;

enum BgChoice {BG_BLACK, BG_WHITE, BG_DEFAULT, BG_SIDES, BG_CORNER, BG_COLOR};

enum BaseOp {OP_CROP, OP_REPORT_FULL, OP_REPORT_SIZE};

enum BlankMode {BLANK_ABORT, BLANK_PASS, BLANK_MINIMIZE, BLANK_MAXCROP};

typedef enum {LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3} EdgeLocation;

typedef struct {
    EdgeLocation v;
    EdgeLocation h;
} CornerLocation;

static const char * const edgeName[] = {
    "left",
    "right",
    "top",
    "bottom"
};

typedef struct {
    unsigned int size[4];
} borderSet;

typedef enum {
    /* A position in a PNM image file stream */
    FILEPOS_BEG,
        /* Immediately before the raster */
    FILEPOS_END
        /* Immediately after the raster */
} imageFilePos;

struct CmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFilespec;
    enum BaseOp baseOperation;
    enum BgChoice background;
    bool wantCrop[4];
        /* User wants crop of left, right, top, bottom, resp. */
    unsigned int margin;
    const char * borderfile;  /* NULL if none */
    float closeness;
    CornerLocation bgCorner;     /* valid if background == BG_CORNER */
    const char * bgColor;  /* valid if background == BG_COLOR */
        /* Note that we can have only the name of the color, not the color
           itself, because we don't know the maxval at option parsing time.
        */
    enum BlankMode blankMode;
    unsigned int verbose;
};



static void
parseCommandLine(int argc, const char ** 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;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int blackOpt, whiteOpt, sidesOpt;
    unsigned int marginSpec, borderfileSpec, closenessSpec;
    unsigned int leftOpt, rightOpt, topOpt, bottomOpt;
    unsigned int bgCornerSpec, bgColorSpec;
    unsigned int blankModeSpec;
    unsigned int reportFullOpt, reportSizeOpt;

    unsigned int option_def_index;

    char * bgCornerOpt;
    char * blankModeOpOpt;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0, "black",       OPT_FLAG,   NULL, &blackOpt,            0);
    OPTENT3(0, "white",       OPT_FLAG,   NULL, &whiteOpt,            0);
    OPTENT3(0, "sides",       OPT_FLAG,   NULL, &sidesOpt,            0);
    OPTENT3(0, "bg-color",    OPT_STRING, &cmdlineP->bgColor,
            &bgColorSpec,   0);
    OPTENT3(0, "bg-corner",   OPT_STRING, &bgCornerOpt,
            &bgCornerSpec,  0);
    OPTENT3(0, "left",        OPT_FLAG,   NULL, &leftOpt,             0);
    OPTENT3(0, "right",       OPT_FLAG,   NULL, &rightOpt,            0);
    OPTENT3(0, "top",         OPT_FLAG,   NULL, &topOpt,              0);
    OPTENT3(0, "bottom",      OPT_FLAG,   NULL, &bottomOpt,           0);
    OPTENT3(0, "margin",      OPT_UINT,   &cmdlineP->margin,
            &marginSpec,     0);
    OPTENT3(0, "borderfile",  OPT_STRING, &cmdlineP->borderfile,
            &borderfileSpec, 0);
    OPTENT3(0, "closeness",   OPT_FLOAT,  &cmdlineP->closeness,
            &closenessSpec,  0);
    OPTENT3(0, "blank-image", OPT_STRING, &blankModeOpOpt,
            &blankModeSpec,  0);
    OPTENT3(0, "reportfull",  OPT_FLAG,   NULL, &reportFullOpt,       0);
    OPTENT3(0, "reportsize",  OPT_FLAG,   NULL, &reportSizeOpt,       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 = FALSE;  /* We have no parms that are negative numbers */

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

    free(option_def);

    if (argc-1 == 0)
        cmdlineP->inputFilespec = "-";  /* stdin */
    else if (argc-1 == 1)
        cmdlineP->inputFilespec = argv[1];
    else
        pm_error("Too many arguments (%d).  "
                 "Only need one: the input filespec", argc-1);

    /* Base operation */

    if (reportFullOpt && reportSizeOpt)
        pm_error("You cannot specify both -reportfull and -reportsize");

    if ((reportFullOpt || reportSizeOpt) && borderfileSpec)
        pm_error("You cannot specify -reportfull or -reportsize "
                 "with -borderfile");

    if (reportFullOpt)
        cmdlineP->baseOperation = OP_REPORT_FULL;
    else if (reportSizeOpt)
        cmdlineP->baseOperation = OP_REPORT_SIZE;
    else
        cmdlineP->baseOperation = OP_CROP;

    /* Background color */

    if (blackOpt + whiteOpt + sidesOpt + bgColorSpec + bgCornerSpec > 1)
        pm_error("You cannot specify more than one of "
                 "-black, -white, -sides, -bg-color, -bg-corner");
    else if (blackOpt)
        cmdlineP->background = BG_BLACK;
    else if (whiteOpt)
        cmdlineP->background = BG_WHITE;
    else if (sidesOpt)
        cmdlineP->background = BG_SIDES;
    else if (bgColorSpec)
        cmdlineP->background = BG_COLOR;
    else if (bgCornerSpec)
        cmdlineP->background = BG_CORNER;
    else
        cmdlineP->background = BG_DEFAULT;

    if (bgCornerSpec) {
        if (false) {
        } else if (streq(bgCornerOpt, "topleft")) {
            cmdlineP->bgCorner.v = TOP;
            cmdlineP->bgCorner.h = LEFT;
        } else if (streq(bgCornerOpt, "topright")) {
            cmdlineP->bgCorner.v = TOP;
            cmdlineP->bgCorner.h = RIGHT;
        } else if (streq(bgCornerOpt, "bottomleft")) {
            cmdlineP->bgCorner.v = BOTTOM;
            cmdlineP->bgCorner.h = LEFT;
        } else if (streq(bgCornerOpt, "bottomright")) {
            cmdlineP->bgCorner.v = BOTTOM;
            cmdlineP->bgCorner.h = RIGHT;
        } else
            pm_error("Invalid value for -bg-corner."
                     "Must be one of "
                     "'topleft', 'topright', 'bottomleft', 'bottomright'");
    }

    /* Border specification */

    if (!leftOpt && !rightOpt && !topOpt && !bottomOpt) {
        unsigned int i;
        for (i = 0; i < 4; ++i)
            cmdlineP->wantCrop[i] = true;
    } else {
        cmdlineP->wantCrop[LEFT]   = !!leftOpt;
        cmdlineP->wantCrop[RIGHT]  = !!rightOpt;
        cmdlineP->wantCrop[TOP]    = !!topOpt;
        cmdlineP->wantCrop[BOTTOM] = !!bottomOpt;
    }

    /* Blank image handling */

    if (blankModeSpec) {
        if (false) {
        } else if (streq(blankModeOpOpt, "abort"))
            cmdlineP->blankMode = BLANK_ABORT;
        else if (streq(blankModeOpOpt,   "pass"))
            cmdlineP->blankMode = BLANK_PASS;
        else if (streq(blankModeOpOpt,   "minimize"))
            cmdlineP->blankMode = BLANK_MINIMIZE;
        else if (streq(blankModeOpOpt,   "maxcrop")) {
            if (cmdlineP->baseOperation == OP_CROP)
                pm_error("Option -blank-image=maxcrop requires "
                         "-reportfull or -reportsize");
            else
                cmdlineP->blankMode = BLANK_MAXCROP;
        } else
            pm_error ("Invalid value for -blank-image");
    } else
        cmdlineP->blankMode = BLANK_ABORT; /* the default */

    /* Other options */

    if (!marginSpec)
        cmdlineP->margin = 0;

    if (!borderfileSpec)
        cmdlineP->borderfile = NULL;

    if (!closenessSpec)
        cmdlineP->closeness = 0.0;

    if (cmdlineP->closeness < 0.0)
        pm_error("-closeness value %f is negative", cmdlineP->closeness);

    if (cmdlineP->closeness > 100.0)
        pm_error("-closeness value %f is more than 100%%",
                 cmdlineP->closeness);
}



typedef struct {
/*----------------------------------------------------------------------------
   This describes a cropping operation of a single border (top, bottom,
   left, or right).

   Our definition of cropping includes padding to make a margin as well as
   chopping stuff out.
-----------------------------------------------------------------------------*/
    unsigned int removeSize;
        /* Size in pixels of the border to remove */
    unsigned int padSize;
        /* Size in pixels of the border to add */
} CropOp;


typedef struct {
    CropOp op[4];
} CropSet;



static xel
background3Corners(FILE *       const ifP,
                   unsigned int const rows,
                   unsigned int const cols,
                   pixval       const maxval,
                   int          const format) {
/*----------------------------------------------------------------------------
  Read in the whole image, and check all the corners to determine the
  background color.  This is a quite reliable way to determine the
  background color.

  Expect the file to be positioned to the start of the raster, and leave
  it positioned arbitrarily.
----------------------------------------------------------------------------*/
    unsigned int row;
    xel ** xels;
    xel background;   /* our return value */

    xels = pnm_allocarray(cols, rows);

    for (row = 0; row < rows; ++row)
        pnm_readpnmrow(ifP, xels[row], cols, maxval, format);

    background = pnm_backgroundxel(xels, cols, rows, maxval, format);

    pnm_freearray(xels, rows);

    return background;
}



static xel
background2Corners(FILE *       const ifP,
                   unsigned int const cols,
                   pixval       const maxval,
                   int          const format) {
/*----------------------------------------------------------------------------
  Look at just the top row of pixels and determine the background
  color from the top corners; often this is enough to accurately
  determine the background color.

  Expect the file to be positioned to the start of the raster, and leave
  it positioned arbitrarily.
----------------------------------------------------------------------------*/
    xel * xelrow;
    xel background;   /* our return value */

    xelrow = pnm_allocrow(cols);

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

    background = pnm_backgroundxelrow(xelrow, cols, maxval, format);

    pnm_freerow(xelrow);

    return background;
}



static xel
background1Corner(FILE *         const ifP,
                  unsigned int   const rows,
                  unsigned int   const cols,
                  pixval         const maxval,
                  int            const format,
                  CornerLocation const corner) {
/*----------------------------------------------------------------------------
  Let the pixel in corner 'corner' be the background.

  Expect the file to be positioned to the start of the raster, and leave
  it positioned arbitrarily.
----------------------------------------------------------------------------*/
    xel * xelrow;
    xel background;   /* our return value */

    xelrow = pnm_allocrow(cols);

    if (corner.v == BOTTOM) {
        /* read and discard all but bottom row */
        unsigned int row;

        for (row = 0; row < rows - 1; ++row)
            pnm_readpnmrow(ifP, xelrow, cols, maxval, format);
    }
    pnm_readpnmrow(ifP, xelrow, cols, maxval, format);

    background = corner.h == LEFT ? xelrow[0] : xelrow[cols - 1];

    pnm_freerow(xelrow);

    return background;
}



static xel
backgroundColorFmName(const char * const colorName,
                      xelval       const maxval,
                      int          const format) {
/*----------------------------------------------------------------------------
   The color indicated by 'colorName'.

   The return value is based on maxval 'maxval' and format 'format'.

   Abort the program if 'colorName' names a color that cannot be represented
   in format 'format'.

   Development note: It would be logical to relax the above restriction when
   -closeness is specified.  Implementation is harder than it seems because of
   the -margin option.  It is unlikely that there is demand for this feature.
   If really necessary, the user can convert the input image to PPM.
-----------------------------------------------------------------------------*/
    pixel const backgroundColor    =
        ppm_parsecolor(colorName, maxval);

    pixel const backgroundColorMax =
        ppm_parsecolor(colorName, PNM_MAXMAXVAL);

    bool const hasColor =
        !(backgroundColorMax.r == backgroundColorMax.g &&
          backgroundColorMax.r == backgroundColorMax.b);

    bool const hasGray  =
        !hasColor &&
        (backgroundColorMax.r != PNM_MAXMAXVAL &&
         backgroundColorMax.r !=  0 );

    xel backgroundXel;

    if (PPM_FORMAT_TYPE(format)) {
        backgroundXel = pnm_pixeltoxel(backgroundColor);
    } else {
        /* Derive PBM or PGM xel from pixel 'backgroundColor' */
        if (hasColor)
            pm_error("Invalid color specified: '%s'. "
                     "Image does not have color.", colorName);
        else {
            if (PBM_FORMAT_TYPE(format) == PBM_TYPE && hasGray)
                pm_error("Invalid color specified: '%s'. "
                         "Image has no intermediate levels of gray.",
                         colorName);
            else
                PNM_ASSIGN1(backgroundXel, backgroundColor.r);
        }
    }

    return backgroundXel;
}



static xel
backgroundColor(FILE *         const ifP,
                int            const cols,
                int            const rows,
                xelval         const maxval,
                int            const format,
                enum BgChoice  const backgroundChoice,
                CornerLocation const corner,
                const char   * const colorName) {
/*----------------------------------------------------------------------------
   Determine what color is the background color of the image in file
   *ifP, which is described by 'cols', 'rows', 'maxval', and 'format'.

   'backgroundChoice' is the method we are to use in determining the
   background color.  'colorName' is the color we are to assume is
   background in the case that 'backgroundChoice' says to use a particular
   color and meangingless otherwise.

   Expect the file to be positioned to the start of the raster, and leave
   it positioned arbitrarily.
-----------------------------------------------------------------------------*/
    xel background;  /* Our return value */

    switch (backgroundChoice) {
    case BG_WHITE:
        background = pnm_whitexel(maxval, format);
        break;
    case BG_BLACK:
        background = pnm_blackxel(maxval, format);
        break;
    case BG_COLOR:
        background =
            backgroundColorFmName(colorName, maxval, format);
        break;
    case BG_SIDES:
        background =
            background3Corners(ifP, rows, cols, maxval, format);
        break;
    case BG_DEFAULT:
        background =
            background2Corners(ifP, cols, maxval, format);
        break;
    case BG_CORNER:
        background =
            background1Corner(ifP, rows, cols, maxval, format, corner);
        break;
    }

    return background;
}



static bool
colorMatches(pixel        const comparand,
             pixel        const comparator,
             unsigned int const allowableDiff) {
/*----------------------------------------------------------------------------
   The colors 'comparand' and 'comparator' are within 'allowableDiff'
   color levels of each other, in cartesian distance.
-----------------------------------------------------------------------------*/
    /* Fast path for usual case */
    if (allowableDiff < EPSILON)
        return PPM_EQUAL(comparand, comparator);

    return PPM_DISTANCE(comparand, comparator) <= SQR(allowableDiff);
}



static void
findBordersInImage(FILE *         const ifP,
                   unsigned int   const cols,
                   unsigned int   const rows,
                   xelval         const maxval,
                   int            const format,
                   xel            const backgroundColor,
                   double         const closeness,
                   bool *         const hasBordersP,
                   borderSet *    const borderSizeP) {
/*----------------------------------------------------------------------------
   Find the left, right, top, and bottom borders in the image 'ifP'.
   Return their sizes in pixels as borderSize[n].

   Iff the image is all background, *hasBordersP == FALSE.

   Expect the input file to be positioned to the beginning of the
   image raster and leave it positioned arbitrarily.
-----------------------------------------------------------------------------*/
    unsigned int const allowableDiff = ROUNDU(sqrt3 * maxval * closeness/100);

    xel * xelrow;        /* A row of the input image */
    int row;
    bool gotTop;
    int left, right, bottom, top;
        /* leftmost, etc. nonbackground pixel found so far; -1 for none */

    xelrow = pnm_allocrow(cols);

    left   = cols;  /* initial value */
    right  = -1;    /* initial value */
    top    = rows;  /* initial value */
    bottom = -1;    /* initial value */

    for (row = 0, gotTop = false; row < rows; ++row) {
        int col;
        int thisRowLeft;
        int thisRowRight;

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

        col = 0;
        while (col < cols &&
               colorMatches(xelrow[col], backgroundColor, allowableDiff))
            ++col;
        thisRowLeft = col;

        col = cols-1;
        while (col >= thisRowLeft &&
               colorMatches(xelrow[col], backgroundColor, allowableDiff))
            --col;
        thisRowRight = col + 1;

        if (thisRowLeft < cols) {
            /* This row is not entirely background */

            left  = MIN(thisRowLeft,  left);
            right = MAX(thisRowRight, right);

            if (!gotTop) {
                top = row;
                gotTop = true;
            }
            bottom = row + 1;   /* New candidate */
        }
    }

    free(xelrow);

    if (right == -1)
        *hasBordersP = FALSE;
    else {
        *hasBordersP = TRUE;
        assert(right <= cols); assert(bottom <= rows);
        borderSizeP->size[LEFT]   = left - 0;
        borderSizeP->size[RIGHT]  = cols - right;
        borderSizeP->size[TOP]    = top - 0;
        borderSizeP->size[BOTTOM] = rows - bottom;
    }
}



static void
analyzeImage(FILE *         const ifP,
             unsigned int   const cols,
             unsigned int   const rows,
             xelval         const maxval,
             int            const format,
             enum BgChoice  const backgroundReq,
             double         const closeness,
             CornerLocation const corner,
             const char   * const colorName,
             imageFilePos   const newFilePos,
             xel *          const backgroundColorP,
             bool *         const hasBordersP,
             borderSet *    const borderSizeP) {
/*----------------------------------------------------------------------------
   Analyze the PNM image on file stream *ifP to determine its borders
   and the color of those borders (the assumed background color).

   Return as *backgroundColorP the background color.

   Return as *borderSizeP the set of border sizes (one for each of the
   four edges).  But iff there are no borders, don't return anything as
   *borderSizeP and return *hasBordersP == false.

   Expect *ifP to be positioned right after the header and seekable.
   Return with it positioned either before or after the raster, as
   requested by 'newFilePos'.
-----------------------------------------------------------------------------*/
    pm_filepos rasterpos;
    xel background;

    pm_tell2(ifP, &rasterpos, sizeof(rasterpos));

    background = backgroundColor(ifP, cols, rows, maxval, format,
                                 backgroundReq, corner, colorName);

    pm_seek2(ifP, &rasterpos, sizeof(rasterpos));

    findBordersInImage(ifP, cols, rows, maxval, format,
                       background, closeness, hasBordersP, borderSizeP);

    if (newFilePos == FILEPOS_BEG)
        pm_seek2(ifP, &rasterpos, sizeof(rasterpos));

    *backgroundColorP = background;
}



static const char *
ending(unsigned int const n) {

    return n > 1 ? "s" : "";
}



static void
reportCroppingParameters(CropSet const crop) {

    unsigned int i;

    for (i = 0; i < 4; ++i) {
        if (crop.op[i].removeSize == 0 && crop.op[i].padSize == 0)
            pm_message("Not cropping %s edge", edgeName[i]);
        else {
            if (crop.op[i].padSize > 0)
                pm_message("Adding %u pixel%s to the %s border",
                           crop.op[i].padSize, ending(crop.op[i].padSize),
                           edgeName[i]);
            if (crop.op[i].removeSize > 0)
                pm_message("Cropping %u pixel%s from the %s border",
                           crop.op[i].removeSize,
                           ending(crop.op[i].removeSize),
                           edgeName[i]);
        }
    }
}



static void
reportDimensions(CropSet      const crop,
                 unsigned int const cols,
                 unsigned int const rows) {

    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(crop.op); ++i) {
        if (crop.op[i].removeSize > 0 && crop.op[i].padSize > 0)
            pm_error("Attempt to add %u and crop %u on %s edge.  "
                     "Simultaneous pad and crop is not allowed",
                     crop.op[i].padSize, crop.op[i].removeSize, edgeName[i]);
        else if (crop.op[i].removeSize > 0)   /* crop */
            printf ("-%u ", crop.op[i].removeSize);
        else if (crop.op[i].removeSize == 0) {
            if (crop.op[i].padSize == 0)      /* no operation */
                printf ("0 ");
            else                              /* pad */
                printf ("+%u ", crop.op[i].padSize);
        }
    }

    {
        unsigned int outputCols, outputRows;

        if (crop.op[LEFT ].removeSize == cols ||
            crop.op[RIGHT].removeSize == cols)
            outputCols = cols;
        else {
            outputCols =
                cols - crop.op[LEFT].removeSize - crop.op[RIGHT].removeSize +
                crop.op[LEFT].padSize + crop.op[RIGHT].padSize;
        }

        if (crop.op[TOP   ].removeSize == rows ||
            crop.op[BOTTOM].removeSize == rows)
            outputRows = rows;
        else
            outputRows =
                rows - crop.op[TOP].removeSize - crop.op[BOTTOM].removeSize +
                crop.op[TOP].padSize + crop.op[BOTTOM].padSize;

        printf("%u %u", outputCols, outputRows);
    }
}


static void
reportSize(CropSet      const crop,
           unsigned int const cols,
           unsigned int const rows) {

    reportDimensions(crop, cols, rows);

    putchar('\n');

}



static void
reportFull(CropSet      const crop,
           unsigned int const cols,
           unsigned int const rows,
           int          const format,
           xelval       const maxval,
           xel          const bgColor,
           float        const closeness) {

    pixel const backgroundPixel = pnm_xeltopixel(bgColor, format);

    reportDimensions(crop, cols, rows);

    printf(" rgb-%u:%u/%u/%u %f\n", maxval,
           backgroundPixel.r, backgroundPixel.g, backgroundPixel.b,
           closeness);
}



static void
fillRow(xel *        const xelrow,
        unsigned int const cols,
        xel          const color) {

    unsigned int col;

    for (col = 0; col < cols; ++col)
        xelrow[col] = color;
}



static void
readOffBorderNonPbm(unsigned int const height,
                    FILE *       const ifP,
                    unsigned int const cols,
                    xelval       const maxval,
                    int          const format) {

    xel * xelrow;
    unsigned int i;

    xelrow = pnm_allocrow(cols);

    for (i = 0; i < height; ++i)
        pnm_readpnmrow(ifP, xelrow, cols, maxval, format);

    pnm_freerow(xelrow);
}



static void
outputNewBorderNonPbm(unsigned int const height,
                      unsigned int const width,
                      xel          const color,
                      FILE *       const ofP,
                      xelval       const maxval,
                      int          const format) {
/*----------------------------------------------------------------------------
   Output to 'ofP' a horizontal border (i.e. top or bottom)
   of color 'backgroundColor', height 'height', width 'width'.
-----------------------------------------------------------------------------*/
    xel * xelrow;
    unsigned int i;

    xelrow = pnm_allocrow(width);

    fillRow(xelrow, width, color);

    for (i = 0; i < height; ++i)
        pnm_writepnmrow(ofP, xelrow, width, maxval, format, 0);

    pnm_freerow(xelrow);
}



static void
writeCroppedNonPbm(FILE *       const ifP,
                   unsigned int const cols,
                   unsigned int const rows,
                   xelval       const maxval,
                   int          const format,
                   CropSet      const crop,
                   xel          const backgroundColor,
                   FILE *       const ofP) {

    /* In order to do cropping, padding or both at the same time, we have
       a rather complicated row buffer:

       xelrow[] is both the input and the output buffer.  So it contains
       the foreground pixels, the original border pixels, and the new
       border pixels.

       We're calling foreground everything that isn't being cropped out
       or padded in.  So the "foreground" may include some of what is really
       a background border in the original image -- because the user can
       choose to retain part of that border as a margin.

       The foreground pixels are in the center of the
       buffer, starting at Column 'foregroundLeft' and going to
       'foregroundRight'.

       There is space to the left of that for the larger of the input
       left border and the output left border.

       Similarly, there is space to the right of the foreground pixels
       for the larger of the input right border and the output right
       border.

       We have to read an entire row, including the pixels we'll be
       leaving out of the output, so we pick a starting location in
       the buffer that lines up the first foreground pixel at
       'foregroundLeft'.

       When we output the row, we pick a starting location in the
       buffer that includes the proper number of left border pixels
       before 'foregroundLeft'.

       That's for the middle rows.  For the top and bottom, we just use
       the left portion of xelrow[], starting at 0.

       This is the general case.  Enhancement for PBM appears below.
       (Logic works for PBM).
    */

    unsigned int const foregroundCols =
        cols - crop.op[LEFT].removeSize - crop.op[RIGHT].removeSize;
    unsigned int const outputCols     =
        foregroundCols + crop.op[LEFT].padSize + crop.op[RIGHT].padSize;
    unsigned int const foregroundRows =
        rows - crop.op[TOP].removeSize - crop.op[BOTTOM].removeSize;
    unsigned int const outputRows     =
        foregroundRows + crop.op[TOP].padSize + crop.op[BOTTOM].padSize;

    unsigned int const foregroundLeft  =
        MAX(crop.op[LEFT].removeSize, crop.op[LEFT].padSize);
        /* Index into xelrow[] of leftmost pixel of foreground */
    unsigned int const foregroundRight = foregroundLeft + foregroundCols;
        /* Index into xelrow[] just past rightmost pixel of foreground */

    unsigned int const allocCols =
        foregroundRight + MAX(crop.op[RIGHT].removeSize,
                              crop.op[RIGHT].padSize);

    xel * xelrow;
    unsigned int i;

    pnm_writepnminit(ofP, outputCols, outputRows, maxval, format, 0);

    xelrow = pnm_allocrow(allocCols);

    readOffBorderNonPbm(crop.op[TOP].removeSize, ifP, cols, maxval, format);

    outputNewBorderNonPbm(crop.op[TOP].padSize, outputCols, backgroundColor,
                          ofP, maxval, format);

    /* Set left border pixels */
    fillRow(&xelrow[foregroundLeft - crop.op[LEFT].padSize],
            crop.op[LEFT].padSize,
            backgroundColor);

    /* Set right border pixels */
    fillRow(&xelrow[foregroundRight], crop.op[RIGHT].padSize, backgroundColor);

    /* Read and output foreground rows */
    for (i = 0; i < foregroundRows; ++i) {

        /* Read foreground pixels */
        pnm_readpnmrow(ifP,
                       &(xelrow[foregroundLeft - crop.op[LEFT].removeSize]),
                       cols, maxval, format);

        pnm_writepnmrow(ofP,
                        &(xelrow[foregroundLeft - crop.op[LEFT].padSize]),
                        outputCols, maxval, format, 0);
    }

    readOffBorderNonPbm(crop.op[BOTTOM].removeSize, ifP, cols, maxval, format);

    outputNewBorderNonPbm(crop.op[BOTTOM].padSize, outputCols,
                          backgroundColor,
                          ofP, maxval, format);

    pnm_freerow(xelrow);
}



static void
fillRowPBM(unsigned char * const bitrow,
           unsigned int    const cols,
           unsigned int    const blackWhite) {
/*----------------------------------------------------------------------------
   Fill the packed PBM row buffer bitrow[] with 'cols' columns of
   black or white: black if 'blackWhite' is 1; white if it is '0'.
   'blackWhite' cannot be anything else.
-----------------------------------------------------------------------------*/
    unsigned int const colChars = pbm_packed_bytes(cols);
    unsigned int i;

    assert(blackWhite == 0 || blackWhite == 1);

    for (i = 0; i < colChars; ++i)
        bitrow[i] = blackWhite * 0xff;

    if (cols % 8 > 0)
        bitrow[colChars-1] <<= 8 - cols % 8;
}



static void
readOffBorderPbm(unsigned int const height,
                 FILE *       const ifP,
                 unsigned int const cols,
                 int          const format) {

    unsigned char * bitrow;
    unsigned int i;

    bitrow = pbm_allocrow_packed(cols);

    for (i = 0; i < height; ++i)
        pbm_readpbmrow_packed(ifP, bitrow, cols, format);

    pbm_freerow_packed(bitrow);
}



static void
outputNewBorderPbm(unsigned int const height,
                   unsigned int const width,
                   unsigned int const blackWhite,
                   FILE *       const ofP) {
/*----------------------------------------------------------------------------
   Output to 'ofP' a horizontal border (i.e. top or bottom)
   of height 'height', width 'width'.  Make it black if 'blackWhite' is
   1; white if 'blackWhite' is 0.  'blackWhite' can't be anything else.
-----------------------------------------------------------------------------*/
    unsigned char * bitrow;
    unsigned int i;

    bitrow = pbm_allocrow_packed(width);

    fillRowPBM(bitrow, width, blackWhite);

    for (i = 0; i < height; ++i)
        pbm_writepbmrow_packed(ofP, bitrow, width, 0);

    pbm_freerow_packed(bitrow);
}



static void
writeCroppedPBM(FILE *       const ifP,
                unsigned int const cols,
                unsigned int const rows,
                int          const format,
                CropSet      const crop,
                xel          const backgroundColor,
                FILE *       const ofP) {

    /* See comments for writeCroppedNonPBM(), which uses identical logic flow.
       Uses pbm functions instead of general pnm functions.
    */

    unsigned int const foregroundCols =
        cols - crop.op[LEFT].removeSize - crop.op[RIGHT].removeSize;
    unsigned int const outputCols     =
        foregroundCols + crop.op[LEFT].padSize + crop.op[RIGHT].padSize;
    unsigned int const foregroundRows =
        rows - crop.op[TOP].removeSize - crop.op[BOTTOM].removeSize;
    unsigned int const outputRows     =
        foregroundRows + crop.op[TOP].padSize + crop.op[BOTTOM].padSize;

    unsigned int const foregroundLeft  =
        MAX(crop.op[LEFT].removeSize, crop.op[LEFT].padSize);
    unsigned int const foregroundRight = foregroundLeft + foregroundCols;

    unsigned int const allocCols =
        foregroundRight +
        MAX(crop.op[RIGHT].removeSize, crop.op[RIGHT].padSize);

    unsigned int const backgroundBlackWhite =
        PNM_EQUAL(backgroundColor, pnm_whitexel(1, PBM_TYPE)) ? 0: 1;

    unsigned int const readOffset    =
        foregroundLeft - crop.op[LEFT].removeSize;
    unsigned int const writeOffset   = foregroundLeft - crop.op[LEFT].padSize;
    unsigned int const lastWriteChar = writeOffset/8 + (outputCols-1)/8;
    unsigned char * bitrow;
    unsigned int i;

    pbm_writepbminit(ofP, outputCols, outputRows, 0);

    bitrow = pbm_allocrow_packed(allocCols);

    readOffBorderPbm(crop.op[TOP].removeSize, ifP, cols, format);

    outputNewBorderPbm(crop.op[TOP].padSize, outputCols, backgroundBlackWhite,
                       ofP);

    /* Prepare padding: left and/or right */
    fillRowPBM(bitrow, allocCols, backgroundBlackWhite);

    /* Read and output foreground rows */
    for (i = 0; i < foregroundRows; ++i) {
        /* Read foreground pixels */
        pbm_readpbmrow_bitoffset(ifP, bitrow, cols, format, readOffset);

        pbm_writepbmrow_bitoffset(ofP,
                                  bitrow, outputCols, format, writeOffset);

        /* If there is right-side padding, repair the write buffer
           distorted by pbm_writepbmrow_bitoffset()
           (No need to mend any left-side padding)
        */
        if (crop.op[RIGHT].padSize > 0)
            bitrow[lastWriteChar] = backgroundBlackWhite * 0xff;
    }

    readOffBorderPbm(crop.op[BOTTOM].removeSize, ifP, cols, format);

    outputNewBorderPbm(crop.op[BOTTOM].padSize, outputCols,
                       backgroundBlackWhite,
                       ofP);

    pbm_freerow_packed(bitrow);
}



static CropSet
crops(struct CmdlineInfo const cmdline,
      borderSet          const oldBorderSize) {

    CropSet retval;

    EdgeLocation i;

    for (i = 0; i < ARRAY_SIZE(retval.op); ++i) {
        if (cmdline.wantCrop[i]) {
            if (oldBorderSize.size[i] > cmdline.margin) {
                retval.op[i].removeSize =
                    oldBorderSize.size[i] - cmdline.margin;
                retval.op[i].padSize    = 0;
            } else {
                retval.op[i].removeSize = 0;
                retval.op[i].padSize    =
                    cmdline.margin - oldBorderSize.size[i];
            }
        } else {
            retval.op[i].removeSize = 0;
            retval.op[i].padSize    = 0;
        }
    }
    return retval;
}



static CropSet
noCrops(struct CmdlineInfo const cmdline) {

    CropSet retval;

    EdgeLocation i;

    if (cmdline.verbose)
        pm_message("The image is entirely background; "
                   "there is nothing to crop.  Copying to output.");

    if (cmdline.margin > 0)
        pm_message ("-margin value %u ignored", cmdline.margin);

    for (i = 0; i < 4; ++i) {
        retval.op[i].removeSize = 0;
        retval.op[i].padSize    = 0;
    }
    return retval;
}



static void
divideAllBackgroundIntoBorders(unsigned int   const totalSz,
                               bool           const wantCropSideA,
                               bool           const wantCropSideB,
                               unsigned int   const wantMargin,
                               unsigned int * const sideASzP,
                               unsigned int * const sideBSzP) {
/*----------------------------------------------------------------------------
   Divide up an all-background space into fictional borders (that can be
   trimmed or padded).

   If there is to be a margin, those borders touch - the entire image is
   borders.  But if there is not to be a margin, there has to be one pixel,
   row, or column between the borders so that there is something left after we
   crop off those borders (because there's no such thing as a zero-pixel
   image).

   This function does the borders in one direction - top and bottom or left
   and right.  Top or left is called "Side A"; bottom or right is called "Side
   B".  'totalSz' is the width of the image in the top/bottom case and the
   height of the image in the left/right case.
-----------------------------------------------------------------------------*/
    unsigned int sideASz, sideBSz;

    if (wantCropSideA && wantCropSideB) {
        sideASz = totalSz/2;
        if (wantMargin)
            sideBSz = totalSz - sideASz;
        else
            sideBSz = totalSz - sideASz - 1;
    } else if (wantCropSideA) {
        if (wantMargin)
            sideASz = totalSz;
        else
            sideASz = totalSz - 1;
        sideBSz = 0;
    } else if (wantCropSideB) {
        sideASz = 0;
        if (wantMargin)
            sideBSz = totalSz;
        else
            sideBSz = totalSz - 1;
    } else {
        sideASz = 0;
        sideBSz = 0;
    }
    *sideASzP = sideASz;
    *sideBSzP = sideBSz;
}



static CropOp
oneSideCrop(bool         const wantCrop,
            unsigned int const borderSz,
            unsigned int const margin) {

    CropOp retval;

    if (wantCrop) {
        if (borderSz >= margin) {
            retval.removeSize = borderSz - margin;
            retval.padSize    = 0;
        } else {
            retval.removeSize = 0;
            retval.padSize    = margin - borderSz;
        }
    } else {
        retval.removeSize = 0;
        retval.padSize    = 0;
    }

    return retval;
}



static CropSet
extremeCrops(struct CmdlineInfo const cmdline,
             unsigned int       const cols,
             unsigned int       const rows) {
/*----------------------------------------------------------------------------
   Crops that crop as much as possible, reducing output to a single
   row, column, or pixel, plus margins.
-----------------------------------------------------------------------------*/
    CropSet retval;

    unsigned int leftBorderSz, rghtBorderSz;
    unsigned int topBorderSz, botBorderSz;

    if (cmdline.verbose)
        pm_message("Input image has no distinction between "
                   "border and content");

    /* Note that the "entirely background" image may have several colors: this
       happens when -closeness was specified.  That means we can't just build
       up an image from background color - we actually have to preserve some
       of the original image.

       We divide the background into individual borders, with a foreground of
       either nothing at all or a single row, column, or pixel, then crop
       those fictional borders the same as if they were real.  The "nothing
       at all" case is feasible only when there is to be a margin, because
       otherwise cropping the borders would leave nothing.
    */

    divideAllBackgroundIntoBorders(cols,
                                   cmdline.wantCrop[LEFT],
                                   cmdline.wantCrop[RIGHT],
                                   cmdline.margin > 0,
                                   &leftBorderSz,
                                   &rghtBorderSz);

    divideAllBackgroundIntoBorders(rows,
                                   cmdline.wantCrop[TOP],
                                   cmdline.wantCrop[BOTTOM],
                                   cmdline.margin > 0,
                                   &topBorderSz,
                                   &botBorderSz);

    retval.op[LEFT  ] =
        oneSideCrop(cmdline.wantCrop[LEFT  ], leftBorderSz, cmdline.margin);
    retval.op[RIGHT ] =
        oneSideCrop(cmdline.wantCrop[RIGHT ], rghtBorderSz, cmdline.margin);
    retval.op[TOP   ] =
        oneSideCrop(cmdline.wantCrop[TOP   ], topBorderSz,  cmdline.margin);
    retval.op[BOTTOM] =
        oneSideCrop(cmdline.wantCrop[BOTTOM], botBorderSz,  cmdline.margin);

    return retval;
}



static CropSet
maxcropReport(struct CmdlineInfo const cmdline,
              unsigned int       const cols,
              unsigned int       const rows) {
/*----------------------------------------------------------------------------
   Report maximum possible crop extents.
-----------------------------------------------------------------------------*/
    CropSet retval;

    if (cmdline.wantCrop[LEFT] && cmdline.wantCrop[RIGHT]) {
        retval.op[LEFT ].removeSize = cols;
        retval.op[RIGHT].removeSize = cols;
    } else if (cmdline.wantCrop[LEFT]) {
        retval.op[LEFT ].removeSize = cols;
        retval.op[RIGHT].removeSize = 0;
    } else if (cmdline.wantCrop[RIGHT]) {
        retval.op[LEFT ].removeSize = 0;
        retval.op[RIGHT].removeSize = cols;
    } else {
        retval.op[LEFT ].removeSize = 0;
        retval.op[RIGHT].removeSize = 0;
    }

    if (cmdline.wantCrop[TOP] && cmdline.wantCrop[BOTTOM]) {
        retval.op[ TOP  ].removeSize = rows;
        retval.op[BOTTOM].removeSize = rows;
    } else if (cmdline.wantCrop[TOP]) {
        retval.op[ TOP  ].removeSize = rows;
        retval.op[BOTTOM].removeSize = 0;
    } else if (cmdline.wantCrop[BOTTOM]) {
        retval.op[ TOP  ].removeSize = 0;
        retval.op[BOTTOM].removeSize = rows;
    } else {
        retval.op[ TOP  ].removeSize = 0;
        retval.op[BOTTOM].removeSize = 0;
    }


    if (cmdline.margin > 0)
        pm_message("-margin value %u ignored", cmdline.margin);

    {
        EdgeLocation i;

        for (i = 0; i < ARRAY_SIZE(retval.op); ++i)
            retval.op[i].padSize = 0;
    }
    return retval;
}



static void
validateComputableSize(unsigned int const cols,
                       unsigned int const rows,
                       CropSet      const crop) {

    double const newcols =
        (double)cols +
        (double)crop.op[LEFT].padSize + (double)crop.op[RIGHT].padSize;

    double const newrows =
        (double)rows +
        (double)crop.op[TOP].padSize + (double)crop.op[BOTTOM].padSize;

    if (newcols > INT_MAX)
       pm_error("Output width too large: %.0f.", newcols);
    if (newrows > INT_MAX)
       pm_error("Output height too large: %.0f.", newrows);
}



static void
cropOneImage(struct CmdlineInfo const cmdline,
             FILE *             const ifP,
             FILE *             const bdfP,
             FILE *             const ofP) {
/*----------------------------------------------------------------------------
   Crop the image to which the stream *ifP is currently positioned
   and write the results to *ofP.  If bdfP is non-null, use the image
   to which stream *bdfP is currently positioned as the borderfile
   (the file that tells us where the existing borders are in the input
   image).  Leave *ifP and *bdfP positioned after the image.

   Both files are seekable.
-----------------------------------------------------------------------------*/
    int rows, cols, format;
    xelval maxval;      /* The input file image */

    int brows, bcols, bformat;
    xelval bmaxval;     /* The separate border file, if specified */

    FILE * afP;
    int arows, acols, aformat;
    xelval amaxval;
    /* The file we use for analysis, either the input file or border file */

    bool hasBorders;
    borderSet oldBorder;
        /* The sizes of the borders in the input image */
    CropSet crop;
        /* The crops we have to do on each side */
    xel background;

    pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);

    if (bdfP) {
        pnm_readpnminit(bdfP, &bcols, &brows, &bmaxval, &bformat);

        if (cols != bcols || rows != brows)
            pm_error("Input file image [%u x %u] and border file image "
                     "[%u x %u] differ in size", cols, rows, bcols, brows);
        else {
            afP = bdfP;
            acols = bcols; arows = brows;
            amaxval = maxval;
            aformat = bformat;
        }
    } else {
        afP = ifP;
        acols = cols; arows = rows;
        amaxval = maxval;
        aformat = format;
    }

    analyzeImage(afP, acols, arows, amaxval, aformat,
                 cmdline.background, cmdline.closeness,
                 cmdline.bgCorner, cmdline.bgColor,
                 (bdfP || cmdline.baseOperation != OP_CROP) ?
                     FILEPOS_END : FILEPOS_BEG,
                 &background, &hasBorders, &oldBorder);

    if (cmdline.verbose) {
        pixel const backgroundPixel = pnm_xeltopixel(background, format);
        pm_message("Background color is %s",
                   ppm_colorname(&backgroundPixel, maxval, TRUE /*hexok*/));
    }
    if (!hasBorders) {
        switch (cmdline.blankMode) {
        case BLANK_ABORT:
            pm_error("The image is entirely background; "
                     "there is nothing to crop.");
            break;
        case BLANK_PASS:     crop = noCrops(cmdline);                   break;
        case BLANK_MINIMIZE: crop = extremeCrops(cmdline, cols, rows);  break;
        case BLANK_MAXCROP:  crop = maxcropReport(cmdline, cols, rows); break;
        }
    } else {
        crop = crops(cmdline, oldBorder);

        validateComputableSize(cols, rows, crop);

        if (cmdline.verbose)
            reportCroppingParameters(crop);
    }

    switch (cmdline.baseOperation) {
    case OP_CROP:
        if (PNM_FORMAT_TYPE(format) == PBM_TYPE)
            writeCroppedPBM(ifP, cols, rows, format, crop, background, ofP);
        else
            writeCroppedNonPbm(ifP, cols, rows, maxval, format, crop,
                               background, ofP);
        break;
    case OP_REPORT_FULL:
        reportFull(crop, cols, rows,
                   aformat, amaxval, background, cmdline.closeness);
        break;
    case OP_REPORT_SIZE:
        reportSize(crop, cols, rows);
        break;
    }
}



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

    struct CmdlineInfo cmdline;
    FILE * ifP;
        /* The program's regular input file.  Could be a seekable copy of
           it in a temporary file.
        */
    FILE * bdfP;
        /* The border file.  NULL if none. */
    int eof;    /* no more images in input stream */
    int beof;   /* no more images in borderfile stream */

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr_seekable(cmdline.inputFilespec);

    if (cmdline.borderfile)
        bdfP = pm_openr(cmdline.borderfile);
    else
        bdfP = NULL;

    for (eof = beof = FALSE; !eof; ) {
        cropOneImage(cmdline, ifP, bdfP, stdout);

        pnm_nextimage(ifP, &eof);

        if (bdfP) {
            pnm_nextimage(bdfP, &beof);

            if (eof != beof) {
                if (!eof)
                    pm_error("Input file has more images than border file.");
                else
                    pm_error("Border file has more images than image file.");
            }
        }
    }

    pm_close(stdout);
    pm_close(ifP);
    if (bdfP)
        pm_close(bdfP);

    return 0;
}