about summary refs log tree commit diff
path: root/lib/libpam.c
blob: 1f58aa7b4b04dc4b205fc5ad60439dff81a68c14 (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
/*----------------------------------------------------------------------------
                                  libpam.c
------------------------------------------------------------------------------
   These are the library functions, which belong in the libnetpbm library,
   that deal with the PAM (Portable Arbitrary Format) image format.
-----------------------------------------------------------------------------*/

/* See pmfileio.c for the complicated explanation of this 32/64 bit file
   offset stuff.
*/
#define _FILE_OFFSET_BITS 64
#define _LARGE_FILES  
#define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */

#include <string.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>

#include <math.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "pam.h"
#include "ppm.h"
#include "libpbm.h"
#include "libpgm.h"
#include "libppm.h"
#include "colorname.h"
#include "fileio.h"

#include "libpam.h"



static unsigned int
allocationDepth(const struct pam * const pamP) {

    unsigned int retval;

    if (pamP->len >= PAM_STRUCT_SIZE(allocation_depth)) {
        if (pamP->allocation_depth == 0)
            retval = pamP->depth;
        else {
            if (pamP->depth > pamP->allocation_depth)
                pm_error("'allocationDepth' (%u) is smaller than 'depth' (%u)",
                         pamP->allocation_depth, pamP->depth);
            retval = pamP->allocation_depth;
        }
    } else
        retval = pamP->depth;
    return retval;
}



static const char **
pamCommentP(const struct pam * const pamP) {

    const char ** retval;

    if (pamP->len >= PAM_STRUCT_SIZE(comment_p))
        retval = pamP->comment_p;
    else
        retval = NULL;

    return retval;
}



static void
validateComputableSize(struct pam * const pamP) {
/*----------------------------------------------------------------------------
   Validate that the dimensions of the image are such that it can be
   processed in typical ways on this machine without worrying about
   overflows.  Note that in C, arithmetic is always modulus arithmetic,
   so if your values are too big, the result is not what you expect.
   That failed expectation can be disastrous if you use it to allocate
   memory.

   It is very normal to allocate space for a tuplerow, so we make sure
   the size of a tuple row, in bytes, can be represented by an 'int'.

   Another common operation is adding 1 or 2 to the highest row, column,
   or plane number in the image, so we make sure that's possible.
-----------------------------------------------------------------------------*/
    if (pamP->width == 0)
        pm_error("Width is zero.  Image must be at least one pixel wide");
    else if (pamP->height == 0)
        pm_error("Height is zero.  Image must be at least one pixel high");
    else {
        unsigned int const depth = allocationDepth(pamP);

        if (depth > INT_MAX/sizeof(sample))
            pm_error("image depth (%u) too large to be processed", depth);
        else if (depth * sizeof(sample) > INT_MAX/pamP->width)
            pm_error("image width and depth (%u, %u) too large "
                     "to be processed.", pamP->width, depth);
        else if (pamP->width * (depth * sizeof(sample)) >
                 INT_MAX - depth * sizeof(tuple *))
            pm_error("image width and depth (%u, %u) too large "
                     "to be processed.", pamP->width, depth);
        
        if (depth > INT_MAX - 2)
            pm_error("image depth (%u) too large to be processed", depth);
        if (pamP->width > INT_MAX - 2)
            pm_error("image width (%u) too large to be processed",
                     pamP->width);
        if (pamP->height > INT_MAX - 2)
            pm_error("image height (%u) too large to be processed",
                     pamP->height);
    }
}



tuple
pnm_allocpamtuple(const struct pam * const pamP) {

    tuple retval;

    retval = malloc(allocationDepth(pamP) * sizeof(retval[0]));

    if (retval == NULL)
        pm_error("Out of memory allocating %u-plane tuple", 
                 allocationDepth(pamP));

    return retval;
}



int
pnm_tupleequal(const struct pam * const pamP, 
               tuple              const comparand, 
               tuple              const comparator) {

    unsigned int plane;
    bool equal;

    equal = TRUE;  /* initial value */
    for (plane = 0; plane < pamP->depth; ++plane) 
        if (comparand[plane] != comparator[plane])
            equal = FALSE;

    return equal;
}




void
pnm_assigntuple(const struct pam * const pamP,
                tuple              const dest,
                tuple              const source) {

    unsigned int plane;
    for (plane = 0; plane < pamP->depth; ++plane) {
        dest[plane] = source[plane];
    }
}



static void
scaleTuple(const struct pam * const pamP,
           tuple              const dest,
           tuple              const source, 
           sample             const newmaxval) {

    unsigned int plane;
    for (plane = 0; plane < pamP->depth; ++plane) 
        dest[plane] = pnm_scalesample(source[plane], pamP->maxval, newmaxval);
}



void
pnm_scaletuple(const struct pam * const pamP,
               tuple              const dest,
               tuple              const source, 
               sample             const newmaxval) {

    scaleTuple(pamP, dest, source, newmaxval);
}



void
pnm_createBlackTuple(const struct pam * const pamP, 
                     tuple *            const blackTupleP) {
/*----------------------------------------------------------------------------
   Create a "black" tuple.  By that we mean a tuple all of whose elements
   are zero.  If it's an RGB, grayscale, or b&w pixel, that means it's black.
-----------------------------------------------------------------------------*/
    unsigned int i;

    *blackTupleP = pnm_allocpamtuple(pamP);

    for (i = 0; i < pamP->depth; ++i) 
        (*blackTupleP)[i] = 0;
}



static tuple *
allocPamRow(const struct pam * const pamP) {
/*----------------------------------------------------------------------------
   We assume that the dimensions of the image are such that arithmetic
   overflow will not occur in our calculations.  NOTE: pnm_readpaminit()
   ensures this assumption is valid.
-----------------------------------------------------------------------------*/
    /* The tuple row data structure starts with pointers to the tuples,
       immediately followed by the tuples themselves.
    */

    unsigned int const bytesPerTuple = allocationDepth(pamP) * sizeof(sample);
    tuple * tuplerow;

    tuplerow = malloc(pamP->width * (sizeof(tuple *) + bytesPerTuple));
                      
    if (tuplerow != NULL) {
        /* Now we initialize the pointers to the individual tuples
           to make this a regulation C two dimensional array.  
        */
        char * p;
        unsigned int col;
        
        p = (char*) (tuplerow + pamP->width);  /* location of Tuple 0 */
        for (col = 0; col < pamP->width; ++col) {
                tuplerow[col] = (tuple) p;
                p += bytesPerTuple;
        }
    }
    return tuplerow;
}



tuple *
pnm_allocpamrow(const struct pam * const pamP) {


    tuple * const tuplerow = allocPamRow(pamP);

    if (tuplerow == NULL)
        pm_error("Out of memory allocating space for a tuple row of "
                 "%d tuples by %d samples per tuple by %u bytes per sample.",
                 pamP->width, allocationDepth(pamP), (unsigned)sizeof(sample));

    return tuplerow;
}



static unsigned int 
rowimagesize(const struct pam * const pamP) {

    /* If repeatedly calculating this turns out to be a significant
       performance problem, we could keep this in struct pam like
       bytes_per_sample.
    */

    if (PAM_FORMAT_TYPE(pamP->format) == PBM_TYPE)
        return pbm_packed_bytes(pamP->width);
    else 
        return (pamP->width * pamP->bytes_per_sample * pamP->depth);
}



unsigned char *
pnm_allocrowimage(const struct pam * const pamP) {

    unsigned int const rowsize = rowimagesize(pamP);
    unsigned int const overrunSpaceNeeded = 8;
        /* This is the number of extra bytes of space libnetpbm needs to have
           at the end of the buffer so it can use fast, lazy algorithms.
        */
    unsigned int const size = rowsize + overrunSpaceNeeded;

    unsigned char * retval;

    retval = malloc(size);

    if (retval == NULL)
        pm_error("Unable to allocate %u bytes for a row image buffer",
                 size);

    return retval;
}



void
pnm_freerowimage(unsigned char * const rowimage) {
    free(rowimage);
}



void 
pnm_scaletuplerow(const struct pam * const pamP,
                  tuple *            const destRow,
                  tuple *            const sourceRow,
                  sample             const newMaxval) {

    if (pamP->maxval == newMaxval) {
        /* Fast path for common case: no scaling needed */
        if (destRow != sourceRow) {
            /* Fast path for common case: it's already what it needs to be */
            unsigned int col;
            for (col = 0; col < pamP->width; ++col)
                pnm_assigntuple(pamP, destRow[col], sourceRow[col]);
        }
    } else {        
        unsigned int col;
        for (col = 0; col < pamP->width; ++col) 
            scaleTuple(pamP, destRow[col], sourceRow[col], newMaxval);
    }
}

 

tuple **
pnm_allocpamarray(const struct pam * const pamP) {
    
    tuple **tuplearray;

    /* If the speed of this is ever an issue, it might be sped up a little
       by allocating one large chunk.
    */
    
    MALLOCARRAY(tuplearray, pamP->height);
    if (tuplearray == NULL) 
        pm_error("Out of memory allocating the row pointer section of "
                 "a %u row array", pamP->height);
    else {
        int row;
        bool outOfMemory;

        outOfMemory = FALSE;
        for (row = 0; row < pamP->height && !outOfMemory; ++row) {
            tuplearray[row] = allocPamRow(pamP);
            if (tuplearray[row] == NULL) {
                unsigned int freerow;
                outOfMemory = TRUE;
                
                for (freerow = 0; freerow < row; ++freerow)
                    pnm_freepamrow(tuplearray[row]);
            }
        }
        if (outOfMemory) {
            free(tuplearray);
            
            pm_error("Out of memory allocating the %u rows %u columns wide by "
                     "%u planes deep", pamP->height, pamP->width,
                     allocationDepth(pamP));
        }
    }
    return tuplearray;
}



void
pnm_freepamarray(tuple ** const tuplearray, const struct pam * const pamP) {

    int row;
    for (row = 0; row < pamP->height; row++)
        pnm_freepamrow(tuplearray[row]);

    free(tuplearray);
}



void 
pnm_setminallocationdepth(struct pam * const pamP,
                          unsigned int const allocationDepth) {

    if (pamP->len < PAM_STRUCT_SIZE(allocation_depth))
        pm_error("Can't set minimum allocation depth in pam structure, "
                 "because the structure is only %u bytes long, and to "
                 "have an allocation_depth field, it must bea at least %u",
                 pamP->len, (unsigned)PAM_STRUCT_SIZE(allocation_depth));

    pamP->allocation_depth = MAX(allocationDepth, pamP->depth);
        
    validateComputableSize(pamP);
}



void
pnm_setpamrow(const struct pam * const pamP,
              tuple *            const tuplerow, 
              sample             const value) {

    int col;
    for (col = 0; col < pamP->width; ++col) {
        int plane;
        for (plane = 0; plane < pamP->depth; ++plane) 
            tuplerow[col][plane] = value;
    }
}




#define MAX_LABEL_LENGTH 8
#define MAX_VALUE_LENGTH 255

static void
parseHeaderLine(const char buffer[],
                char label[MAX_LABEL_LENGTH+1], 
                char value[MAX_VALUE_LENGTH+1]) {
    
    int buffer_curs;

    buffer_curs = 0;
    /* Skip initial white space */
    while (ISSPACE(buffer[buffer_curs])) buffer_curs++;

    {
        /* Read off label, put as much as will fit into label[] */
        int label_curs;
        label_curs = 0;
        while (!ISSPACE(buffer[buffer_curs]) && buffer[buffer_curs] != '\0') {
            if (label_curs < MAX_LABEL_LENGTH) 
                label[label_curs++] = buffer[buffer_curs];
            buffer_curs++;
        }
        label[label_curs] = '\0';  /* null terminate it */
    }    

    /* Skip white space between label and value */
    while (ISSPACE(buffer[buffer_curs])) buffer_curs++;

    /* copy value into value[] */
    strncpy(value, buffer+buffer_curs, MAX_VALUE_LENGTH+1);

    {
        /* Remove trailing white space from value[] */
        int value_curs;
        value_curs = strlen(value)-1;
        while (value_curs >= 0 && ISSPACE(value[value_curs])) 
            value[value_curs--] = '\0';
    }
}



struct headerSeen {
/*----------------------------------------------------------------------------
   This structure tells what we've seen so far in our progress through the
   PAM header
-----------------------------------------------------------------------------*/
    bool width;
    bool height;
    bool depth;
    bool maxval;
    bool endhdr;
};



static void
parseHeaderUint(const char *   const valueString,
                unsigned int * const valueNumP,
                const char *   const name) {
/*----------------------------------------------------------------------------
   Interpret 'valueString' as the number in a header such as
   "WIDTH 200".

   'name' is the header name ("WIDTH" in the example).
-----------------------------------------------------------------------------*/

    if (strlen(valueString) == 0)
        pm_error("Missing value for %s in PAM file header.", name);
    else {
        char * endptr;
        long int valueNum;
        errno = 0;  /* Clear errno so we can detect strtol() failure */
        valueNum = strtol(valueString, &endptr, 10);
        if (errno != 0)
            pm_error("Too-large value for %s in "
                     "PAM file header: '%s'", name, valueString);
        else if (*endptr != '\0') 
            pm_error("Non-numeric value for %s in "
                     "PAM file header: '%s'", name, valueString);
        else if (valueNum < 0) 
            pm_error("Negative value for %s in "
                     "PAM file header: '%s'", name, valueString);
        else if ((unsigned int)valueNum != valueNum)
            pm_error("Ridiculously large value for %s in "
                     "PAM file header: %lu", name, valueNum);
        else
            *valueNumP = (unsigned int)valueNum;
    }
}



static void
parseHeaderInt(const char * const valueString,
               int *        const valueNumP,
               const char * const name) {
/*----------------------------------------------------------------------------
  This is not what it seems.  It is the same thing as
  parseHeaderUint, except that the type of the value it returns is
  "int" instead of "unsigned int".  But that doesn't mean the value can
  be negative.  We throw an error is it is not positive.
-----------------------------------------------------------------------------*/
    unsigned int valueNum;

    parseHeaderUint(valueString, &valueNum, name);

    if ((int)valueNum != valueNum)
        pm_error("Ridiculously large value for %s in "
                 "PAM file header: %u", name, valueNum);
    else
        *valueNumP = (int)valueNum;
}



static void
processHeaderLine(char                const buffer[],
                  struct pam *        const pamP,
                  struct headerSeen * const headerSeenP) {
/*----------------------------------------------------------------------------
   Process a line from the PAM header.  The line is buffer[], and it is not
   a comment or blank.

   Put the value that the line defines in *pamP (unless it's ENDHDR).

   Update *headerSeenP with whatever we see.
-----------------------------------------------------------------------------*/
    char label[MAX_LABEL_LENGTH+1];
    char value[MAX_VALUE_LENGTH+1];

    parseHeaderLine(buffer, label, value);

    if (streq(label, "ENDHDR"))
        headerSeenP->endhdr = TRUE;
    else if (streq(label, "WIDTH")) {
        parseHeaderInt(value, &pamP->width, label);
        headerSeenP->width = TRUE;
    } else if (streq(label, "HEIGHT")) {
        parseHeaderInt(value, &pamP->height, label);
        headerSeenP->height = TRUE;
    } else if (streq(label, "DEPTH")) {
        parseHeaderUint(value, &pamP->depth, label);
        headerSeenP->depth = TRUE;
    } else if (streq(label, "MAXVAL")) {
        unsigned int maxval;
        parseHeaderUint(value, &maxval, label);
        if (maxval >= (1<<16))
            pm_error("Maxval too large: %u.  Max is 65535", maxval);
        pamP->maxval = maxval;
        headerSeenP->maxval = TRUE;
    } else if (streq(label, "TUPLTYPE")) {
        if (strlen(value) == 0)
            pm_error("TUPLTYPE header does not have any tuple type text");
        else {
            size_t const oldLen = strlen(pamP->tuple_type);
            if (oldLen + strlen(value) + 1 > sizeof(pamP->tuple_type)-1)
                pm_error("TUPLTYPE value too long in PAM header");
            if (oldLen == 0)
                strcpy(pamP->tuple_type, value);
            else {
                strcat(pamP->tuple_type, " ");
                strcat(pamP->tuple_type, value);
            }
            pamP->tuple_type[sizeof(pamP->tuple_type)-1] = '\0';
        }
    } else 
        pm_error("Unrecognized header line type: '%s'.  "
                 "Possible missing ENDHDR line?", label);
}



static void
appendComment(char **      const commentsP,
              const char * const commentHeader) {

    const char * const commentLine = &commentHeader[1];

    size_t commentLen = strlen(*commentsP) + strlen(commentLine) + 1;

    assert(commentHeader[0] == '#');

    REALLOCARRAY(*commentsP, commentLen);

    if (*commentsP == NULL)
        pm_error("Couldn't get storage for %u characters of comments from "
                 "the PAM header", (unsigned)commentLen);

    strcat(*commentsP, commentLine);
}



static void
disposeOfComments(const struct pam * const pamP,
                  const char *       const comments) {

    const char ** const retP = pamCommentP(pamP);

    if (retP)
        *retP = comments;
    else
        pm_strfree(comments);
}



static void
readpaminitrest(struct pam * const pamP) {
/*----------------------------------------------------------------------------
   Read the rest of the PAM header (after the first line -- the magic
   number line).  Fill in all the information in *pamP.
-----------------------------------------------------------------------------*/
    struct headerSeen headerSeen;
    char * comments;

    headerSeen.width  = FALSE;
    headerSeen.height = FALSE;
    headerSeen.depth  = FALSE;
    headerSeen.maxval = FALSE;
    headerSeen.endhdr = FALSE;

    pamP->tuple_type[0] = '\0';

    comments = strdup("");

    { 
        int c;
        /* Read off rest of 1st line -- probably just the newline after the 
           magic number 
        */
        while ((c = getc(pamP->file)) != -1 && c != '\n');
    }    

    while (!headerSeen.endhdr) {
        char buffer[256];
        char * rc;
        rc = fgets(buffer, sizeof(buffer), pamP->file);
        if (rc == NULL)
            pm_error("EOF or error reading file while trying to read the "
                     "PAM header");
        else {
            buffer[256-1-1] = '\n';  /* In case fgets() truncated */
            if (buffer[0] == '#')
                appendComment(&comments, buffer);
            else if (pm_stripeq(buffer, ""));
                /* Ignore it; it's a blank line */
            else 
                processHeaderLine(buffer, pamP, &headerSeen);
        }
    }

    disposeOfComments(pamP, comments);

    if (!headerSeen.height)
        pm_error("No HEIGHT header line in PAM header");
    if (!headerSeen.width)
        pm_error("No WIDTH header line in PAM header");
    if (!headerSeen.depth)
        pm_error("No DEPTH header line in PAM header");
    if (!headerSeen.maxval)
        pm_error("No MAXVAL header line in PAM header");

    if (pamP->height == 0) 
        pm_error("HEIGHT value is zero in PAM header");
    if (pamP->width == 0) 
        pm_error("WIDTH value is zero in PAM header");
    if (pamP->depth == 0) 
        pm_error("DEPTH value is zero in PAM header");
    if (pamP->maxval == 0) 
        pm_error("MAXVAL value is zero in PAM header");
    if (pamP->maxval > PAM_OVERALL_MAXVAL)
        pm_error("MAXVAL value (%lu) in PAM header is greater than %u",
                 pamP->maxval, PAM_OVERALL_MAXVAL);
}



void
pnm_readpaminitrestaspnm(FILE * const fileP, 
                         int *  const colsP, 
                         int *  const rowsP, 
                         gray * const maxvalP,
                         int *  const formatP) {
/*----------------------------------------------------------------------------
   Read the rest of the PAM header (after the first line) and return
   information as if it were PPM or PGM.

   Die if it isn't a PAM of the sort we can treat as PPM or PGM.
-----------------------------------------------------------------------------*/
    struct pam pam;

    pam.size   = sizeof(struct pam);
    pam.file   = fileP;
    pam.len    = PAM_STRUCT_SIZE(tuple_type);
    pam.format = PAM_FORMAT;

    readpaminitrest(&pam);


    /* A PAM raster of depth 1 is identical to a PGM raster.  A PAM
       raster of depth 3 is identical to PPM raster.  So
       ppm_readppmrow() will be able to read the PAM raster as long as
       the format it thinks it is (PGM or PPM) corresponds to the PAM
       depth.  Similar for pgm_readpgmrow().
    */
    switch (pam.depth) {
    case 3:
        *formatP = RPPM_FORMAT;
        break;
    case 1:
        *formatP = RPGM_FORMAT;
        break;
    default:
        pm_error("Cannot treat PAM image as PPM or PGM, "
                 "because its depth (%u) "
                 "is not 1 or 3.", pam.depth);
    }

    *colsP   = pam.width;
    *rowsP   = pam.height;
    *maxvalP = (gray)pam.maxval;
}


                
unsigned int
pnm_bytespersample(sample const maxval) {
/*----------------------------------------------------------------------------
   Return the number of bytes per sample in the PAM raster of a PAM image
   with maxval 'maxval'.  It's defined to be the minimum number of bytes
   needed for that maxval, i.e. 1 for maxval < 256, 2 otherwise.
-----------------------------------------------------------------------------*/

    /* The PAM format requires maxval to be greater than zero and less than
       1<<16, but since that is a largely arbitrary restriction, we don't want
       to rely on it.
    */

    unsigned int i;
    sample a;

    for (i = 0, a = maxval; i <= sizeof(maxval); ++i) {
        if (a == 0)
            return i;
        a >>= 8;
    }
    return 0;  /* silence compiler warning */
}



static void
validateMinDepth(const struct pam * const pamP,
                 unsigned int       const minDepth) {

    if (pamP->depth < minDepth)
        pm_error("Depth %u is insufficient for tuple type '%s'.  "
                 "Minimum depth is %u",
                 pamP->depth, pamP->tuple_type, minDepth);
}



static void
interpretTupleType(struct pam * const pamP) {
/*----------------------------------------------------------------------------
   Fill in redundant convenience fields in *pamP with information implied by
   the pamP->tuple_type implies:

     visual
     colorDepth
     haveOpacity
     opacityPlane

   Validate the tuple type against the depth and maxval as well.
-----------------------------------------------------------------------------*/
    const char * const tupleType =
        pamP->len >= PAM_STRUCT_SIZE(tuple_type) ? pamP->tuple_type : "";

    bool         visual;
    unsigned int colorDepth;
    bool         haveOpacity;
    unsigned int opacityPlane;

    assert(pamP->depth > 0);

    switch (PAM_FORMAT_TYPE(pamP->format)) {
    case PAM_TYPE: {
        if (STRSEQ(tupleType, "BLACKANDWHITE")) {
            visual = true;
            colorDepth = 1;
            haveOpacity = false;
            if (pamP->maxval != 1)
                pm_error("maxval %u is not consistent with tuple type "
                         "BLACKANDWHITE (should be 1)",
                         (unsigned)pamP->maxval);
        } else if (STRSEQ(tupleType, "GRAYSCALE")) {
            visual = true;
            colorDepth = 1;
            haveOpacity = false;
        } else if (STRSEQ(tupleType, "GRAYSCALE_ALPHA")) {
            visual = true;
            colorDepth = 1;
            haveOpacity = true;
            opacityPlane = PAM_GRAY_TRN_PLANE;
            validateMinDepth(pamP, 2);
        } else if (STRSEQ(tupleType, "RGB")) {
            visual = true;
            colorDepth = 3;
            haveOpacity = false;
            validateMinDepth(pamP, 3);
        } else if (STRSEQ(tupleType, "RGB_ALPHA")) {
            visual = true;
            colorDepth = 3;
            haveOpacity = true;
            opacityPlane = PAM_TRN_PLANE;
            validateMinDepth(pamP, 4);
        } else {
            visual = false;
        }
    } break;
    case PPM_TYPE:
        visual = true;
        colorDepth = 3;
        haveOpacity = false;
        assert(pamP->depth == 3);
        break;
    case PGM_TYPE:
        visual = true;
        colorDepth = 1;
        haveOpacity = false;
        break;
    case PBM_TYPE:
        visual = true;
        colorDepth = 1;
        haveOpacity = false;
        break;
    default:
        assert(false);
    }
    if (pamP->size >= PAM_STRUCT_SIZE(visual))
        pamP->visual = visual;
    if (pamP->size >= PAM_STRUCT_SIZE(color_depth))
        pamP->color_depth = colorDepth;
    if (pamP->size >= PAM_STRUCT_SIZE(have_opacity))
        pamP->have_opacity = haveOpacity;
    if (pamP->size >= PAM_STRUCT_SIZE(opacity_plane))
        pamP->have_opacity = haveOpacity;
}



void 
pnm_readpaminit(FILE *       const file, 
                struct pam * const pamP, 
                int          const size) {

    if (size < PAM_STRUCT_SIZE(tuple_type)) 
        pm_error("pam object passed to pnm_readpaminit() is too small.  "
                 "It must be large "
                 "enough to hold at least up to the "
                 "'tuple_type' member, but according "
                 "to the 'size' argument, it is only %d bytes long.", 
                 size);

    pamP->size = size;
    pamP->file = file;
    pamP->len = MIN(pamP->size, sizeof(struct pam));

    if (size >= PAM_STRUCT_SIZE(allocation_depth))
        pamP->allocation_depth = 0;

    /* Get magic number. */
    pamP->format = pm_readmagicnumber(file);

    switch (PAM_FORMAT_TYPE(pamP->format)) {
    case PAM_TYPE: 
        readpaminitrest(pamP);
        break;
    case PPM_TYPE: {
        pixval maxval;
        ppm_readppminitrest(pamP->file, &pamP->width, &pamP->height, &maxval);
        pamP->maxval = (sample) maxval;
        pamP->depth = 3;
        strcpy(pamP->tuple_type, PAM_PPM_TUPLETYPE);
        if (pamCommentP(pamP))
            *pamP->comment_p = strdup("");
    } break;

    case PGM_TYPE: {
        gray maxval;
        pgm_readpgminitrest(pamP->file, &pamP->width, &pamP->height, &maxval);
        pamP->maxval = (sample) maxval;
        pamP->depth = 1;
        strcpy(pamP->tuple_type, PAM_PGM_TUPLETYPE);
        if (pamCommentP(pamP))
            *pamP->comment_p = strdup("");
    } break;

    case PBM_TYPE:
        pbm_readpbminitrest(pamP->file, &pamP->width,&pamP->height);
        pamP->maxval = (sample) 1;
        pamP->depth = 1;
        strcpy(pamP->tuple_type, PAM_PBM_TUPLETYPE);
        if (pamCommentP(pamP))
            *pamP->comment_p = strdup("");
        break;
        
    default:
        pm_error("bad magic number 0x%x - not a PAM, PPM, PGM, or PBM file",
                 pamP->format);
    }
    
    pamP->bytes_per_sample = pnm_bytespersample(pamP->maxval);
    pamP->plainformat = FALSE;
        /* See below for complex explanation of why this is FALSE. */

    interpretTupleType(pamP);

    validateComputableSize(pamP);
}



static void
writeComments(const struct pam * const pamP) {
/*----------------------------------------------------------------------------
   Write comments for a PAM header, insofar as *pamP specifies comments.
-----------------------------------------------------------------------------*/
    const char ** const commentP = pamCommentP(pamP);

    if (commentP) {
        const char * const comment = *commentP;

        const char * p;
        bool startOfLine;

        for (p = &comment[0], startOfLine = TRUE; *p; ++p) {
            if (startOfLine)
                fputc('#', pamP->file);
                
            fputc(*p, pamP->file);

            if (*p == '\n')
                startOfLine = TRUE;
            else
                startOfLine = FALSE;
        }
        if (!startOfLine)
            fputc('\n', pamP->file);
    }
}



/* About the 'plainformat' member on image input operations:

   'plainformat' is meaningless when reading an image, but we always
   set it FALSE anyway.

   That's because it is common for a program to copy a pam structure
   used for input as a pam structure for output, and just update the
   few fields it cares about -- mainly 'file'.  We want a program like
   that to write a raw format image, and 'plainformat' in an output
   pam structure is what determines whether it is raw or plain.  So we
   set it false here so that it is false in the copied output pam
   structure.
   
   Before 10.32, we set 'plainformat' according to the
   plainness of the input image, and thought it was a good
   idea for a program that reads a plain format input image to
   write a plain format output image.  But that is not what
   the older libnetpbm facilities (e.g. pnm_writepnm()) do, so
   for compatibility, the pam facility shouldn't either.  This
   came to light as we converted programs from the pnm/pbm/ppm
   facilities to pam.
*/

void 
pnm_writepaminit(struct pam * const pamP) {

    const char * tupleType;

    if (pamP->size < pamP->len)
        pm_error("pam object passed to pnm_writepaminit() is smaller "
                 "(%u bytes, according to its 'size' element) "
                 "than the amount of data in it "
                 "(%u bytes, according to its 'len' element).",
                 pamP->size, pamP->len);

    if (pamP->size < PAM_STRUCT_SIZE(bytes_per_sample))
        pm_error("pam object passed to pnm_writepaminit() is too small.  "
                 "It must be large "
                 "enough to hold at least up through the "
                 "'bytes_per_sample' member, but according "
                 "to its 'size' member, it is only %u bytes long.", 
                 pamP->size);
    if (pamP->len < PAM_STRUCT_SIZE(maxval))
        pm_error("pam object must contain members at least through 'maxval', "
                 "but according to the 'len' member, it is only %u bytes "
                 "long.", pamP->len);

    if (pamP->maxval > PAM_OVERALL_MAXVAL)
        pm_error("maxval (%lu) passed to pnm_writepaminit() "
                 "is greater than %u", pamP->maxval, PAM_OVERALL_MAXVAL);

    if (pamP->len < PAM_STRUCT_SIZE(tuple_type)) {
        tupleType = "";
        if (pamP->size >= PAM_STRUCT_SIZE(tuple_type))
            pamP->tuple_type[0] = '\0';
    } else
        tupleType = pamP->tuple_type;

    pamP->bytes_per_sample = pnm_bytespersample(pamP->maxval);

    if (pamP->size >= PAM_STRUCT_SIZE(comment_p) &&
        pamP->len < PAM_STRUCT_SIZE(comment_p))
        pamP->comment_p = NULL;

    if (pamP->size >= PAM_STRUCT_SIZE(allocation_depth) &&
        pamP->len < PAM_STRUCT_SIZE(allocation_depth))
        pamP->allocation_depth = 0;

    interpretTupleType(pamP);

    pamP->len = MIN(pamP->size, PAM_STRUCT_SIZE(opacity_plane));
    
    switch (PAM_FORMAT_TYPE(pamP->format)) {
    case PAM_TYPE:
        if (pm_plain_output)
            pm_error("There is no plain version of PAM.  -plain option "
                     "is not allowed");
        fprintf(pamP->file, "P7\n");
        writeComments(pamP);
        fprintf(pamP->file, "WIDTH %u\n",   (unsigned)pamP->width);
        fprintf(pamP->file, "HEIGHT %u\n",  (unsigned)pamP->height);
        fprintf(pamP->file, "DEPTH %u\n",   pamP->depth);
        fprintf(pamP->file, "MAXVAL %lu\n", pamP->maxval);
        if (!pm_stripeq(tupleType, ""))
            fprintf(pamP->file, "TUPLTYPE %s\n", pamP->tuple_type);
        fprintf(pamP->file, "ENDHDR\n");
        break;
    case PPM_TYPE:
        /* The depth must be exact, because pnm_writepamrow() is controlled
           by it, without regard to format.
        */
        if (pamP->depth != 3)
            pm_error("pnm_writepaminit() got PPM format, but depth = %d "
                     "instead of 3, as required for PPM.", 
                     pamP->depth);
        if (pamP->maxval > PPM_OVERALLMAXVAL) 
            pm_error("pnm_writepaminit() got PPM format, but maxval = %ld, "
                     "which exceeds the maximum allowed for PPM: %d", 
                     pamP->maxval, PPM_OVERALLMAXVAL);
        ppm_writeppminit(pamP->file, pamP->width, pamP->height, 
                         (pixval) pamP->maxval, pamP->plainformat);
        break;

    case PGM_TYPE:
        if (pamP->depth != 1)
            pm_error("pnm_writepaminit() got PGM format, but depth = %d "
                     "instead of 1, as required for PGM.",
                     pamP->depth);
        if (pamP->maxval > PGM_OVERALLMAXVAL)
            pm_error("pnm_writepaminit() got PGM format, but maxval = %ld, "
                     "which exceeds the maximum allowed for PGM: %d", 
                     pamP->maxval, PGM_OVERALLMAXVAL);
        pgm_writepgminit(pamP->file, pamP->width, pamP->height, 
                         (gray) pamP->maxval, pamP->plainformat);
        break;

    case PBM_TYPE:
        if (pamP->depth != 1)
            pm_error("pnm_writepaminit() got PBM format, but depth = %d "
                     "instead of 1, as required for PBM.",
                     pamP->depth);
        if (pamP->maxval != 1) 
            pm_error("pnm_writepaminit() got PBM format, but maxval = %ld "
                     "instead of 1, as required for PBM.", pamP->maxval);
        pbm_writepbminit(pamP->file, pamP->width, pamP->height, 
                         pamP->plainformat);
        break;

    default:
        pm_error("Invalid format passed to pnm_writepaminit(): %d",
                 pamP->format);
    }
}



void
pnm_checkpam(const struct pam *   const pamP, 
             enum pm_check_type   const checkType, 
             enum pm_check_code * const retvalP) {

    if (checkType != PM_CHECK_BASIC) {
        if (retvalP) *retvalP = PM_CHECK_UNKNOWN_TYPE;
    } else switch (PAM_FORMAT_TYPE(pamP->format)) {
    case PAM_TYPE: {
        pm_filepos const need_raster_size = 
            pamP->width * pamP->height * pamP->depth * pamP->bytes_per_sample;
        pm_check(pamP->file, checkType, need_raster_size, retvalP);
    }
        break;
    case PPM_TYPE:
        pgm_check(pamP->file, checkType, pamP->format, 
                  pamP->width, pamP->height, pamP->maxval, retvalP);
        break;
    case PGM_TYPE:
        pgm_check(pamP->file, checkType, pamP->format, 
                  pamP->width, pamP->height, pamP->maxval, retvalP);
        break;
    case PBM_TYPE:
        pbm_check(pamP->file, checkType, pamP->format, 
                  pamP->width, pamP->height, retvalP);
        break;
    default:
        if (retvalP) *retvalP = PM_CHECK_UNCHECKABLE;
    }
}



void 
pnm_maketuplergb(const struct pam * const pamP,
                 tuple              const tuple) {

    if (allocationDepth(pamP) < 3)
        pm_error("allocation depth %u passed to pnm_maketuplergb().  "
                 "Must be at least 3.", allocationDepth(pamP));

    if (pamP->depth < 3)
        tuple[2] = tuple[1] = tuple[0];
}



void 
pnm_makerowrgb(const struct pam * const pamP,
               tuple *            const tuplerow) {
    
    if (pamP->depth < 3) {
        unsigned int col;

        if (allocationDepth(pamP) < 3)
            pm_error("allocation depth %u passed to pnm_makerowrgb().  "
                     "Must be at least 3.", allocationDepth(pamP));
        
        if (strncmp(pamP->tuple_type, "RGB", 3) != 0) {
            for (col = 0; col < pamP->width; ++col) {
                tuple const thisTuple = tuplerow[col];
                thisTuple[2] = thisTuple[1] = thisTuple[0];
            }
        }
    }
}



void 
pnm_makearrayrgb(const struct pam * const pamP,
                 tuple **           const tuples) {

    if (pamP->depth < 3) {
        unsigned int row;
        if (allocationDepth(pamP) < 3)
            pm_error("allocation depth %u passed to pnm_makearrayrgb().  "
                     "Must be at least 3.", allocationDepth(pamP));
        
        for (row = 0; row < pamP->height; ++row) {
            tuple * const tuplerow = tuples[row];
            unsigned int col;
            for (col = 0; col < pamP->width; ++col) {
                tuple const thisTuple = tuplerow[col];
                thisTuple[2] = thisTuple[1] = thisTuple[0];
            }
        }
    }
}



void 
pnm_makerowrgba(const struct pam * const pamP,
                tuple *            const tuplerow) {
/*----------------------------------------------------------------------------
   Make the tuples 'tuplerow' the RGBA equivalent of what they are now,
   which is described by *pamP.

   This means afterward, *pamP no longer correctly describes these tuples;
   Caller must be sure to update *pamP it or not use it anymore.

   We fail if Caller did not supply enough allocated space in 'tuplerow' for
   the extra planes (tuple allocation depth).
-----------------------------------------------------------------------------*/
    if (pamP->len < PAM_STRUCT_SIZE(opacity_plane)) {
        pm_message("struct pam length %u is too small for pnm_makerowrgba().  "
                   "This function requires struct pam fields through "
                   "'opacity_plane'", pamP->len);
        abort();
    } else {
        if (!pamP->visual)
            pm_error("Non-visual tuples given to pnm_addopacityrow()");
        
        if (pamP->color_depth >= 3 && pamP->have_opacity) {
            /* It's already in RGBA format.  Leave it alone. */
        } else {
            unsigned int col;

            if (allocationDepth(pamP) < 4)
                pm_error("allocation depth %u passed to pnm_makerowrgba().  "
                         "Must be at least 4.", allocationDepth(pamP));
        
            for (col = 0; col < pamP->width; ++col) {
                tuple const thisTuple = tuplerow[col];
                thisTuple[PAM_TRN_PLANE] = 
                    pamP->have_opacity ? thisTuple[pamP->opacity_plane] :
                    pamP->maxval;

                assert(PAM_RED_PLANE == 0);
                thisTuple[PAM_BLU_PLANE] = thisTuple[0];
                thisTuple[PAM_GRN_PLANE] = thisTuple[0];
            }
        }
    }
}



void 
pnm_addopacityrow(const struct pam * const pamP,
                  tuple *            const tuplerow) {
/*----------------------------------------------------------------------------
   Add an opacity plane to the tuples in 'tuplerow', if one isn't already
   there.

   This means afterward, *pamP no longer correctly describes these tuples;
   Caller must be sure to update *pamP it or not use it anymore.

   We fail if Caller did not supply enough allocated space in 'tuplerow' for
   the extra plane (tuple allocation depth).
-----------------------------------------------------------------------------*/
    if (pamP->len < PAM_STRUCT_SIZE(opacity_plane)) {
        pm_message("struct pam length %u is too small for pnm_makerowrgba().  "
                   "This function requires struct pam fields through "
                   "'opacity_plane'", pamP->len);
        abort();
    } else {
        if (!pamP->visual)
            pm_error("Non-visual tuples given to pnm_addopacityrow()");
        
        if (pamP->have_opacity) {
            /* It already has opacity.  Leave it alone. */
        } else {
            unsigned int const opacityPlane = pamP->color_depth;

            unsigned int col;

            if (allocationDepth(pamP) < opacityPlane + 1)
                pm_error("allocation depth %u passed to pnm_addopacityrow().  "
                         "Must be at least %u.",
                         allocationDepth(pamP), opacityPlane + 1);
        
            for (col = 0; col < pamP->width; ++col)
                tuplerow[col][opacityPlane] = pamP->maxval;
        }
    }
}



void
pnm_getopacity(const struct pam * const pamP,
               bool *             const haveOpacityP,
               unsigned int *     const opacityPlaneP) {

    /* Usage note: this is obsolete since we added 'haveOpacity', etc.
       to struct pam.
    */
    if (streq(pamP->tuple_type, "RGB_ALPHA")) {
        *haveOpacityP = TRUE;
        *opacityPlaneP = PAM_TRN_PLANE;
    } else if (streq(pamP->tuple_type, "GRAYSCALE_ALPHA")) {
        *haveOpacityP = TRUE;
        *opacityPlaneP = PAM_GRAY_TRN_PLANE;
    } else
        *haveOpacityP = FALSE;
}



tuple
pnm_backgroundtuple(struct pam *  const pamP,
                    tuple      ** const tuples) {
/*--------------------------------------------------------------------
  This function was copied from libpnm3.c's pnm_backgroundxel() and
  modified to use tuples instead of xels.
----------------------------------------------------------------------*/
    tuple tuplePtr, bgtuple, ul, ur, ll, lr;

    /* Guess a good background value. */
    ul = tuples[0][0];
    ur = tuples[0][pamP->width-1];
    ll = tuples[pamP->height-1][0];
    lr = tuples[pamP->height-1][pamP->width-1];
    bgtuple = NULL;

    /* We first recognize three corners equal.  If not, we look for any
       two.  If not, we just average all four.
    */
    if (pnm_tupleequal(pamP, ul, ur) && pnm_tupleequal(pamP, ur, ll))
        tuplePtr = ul;
    else if (pnm_tupleequal(pamP, ul, ur) &&
             pnm_tupleequal(pamP, ur, lr))
        tuplePtr = ul;
    else if (pnm_tupleequal(pamP, ul, ll) &&
             pnm_tupleequal(pamP, ll, lr))
        tuplePtr = ul;
    else if (pnm_tupleequal(pamP, ur, ll) &&
             pnm_tupleequal(pamP, ll, lr))
        tuplePtr = ur;
    else if (pnm_tupleequal(pamP, ul, ur))
        tuplePtr = ul;
    else if (pnm_tupleequal(pamP, ul, ll))
        tuplePtr = ul;
    else if (pnm_tupleequal(pamP, ul, lr))
        tuplePtr = ul;
    else if (pnm_tupleequal(pamP, ur, ll))
        tuplePtr = ur;
    else if (pnm_tupleequal(pamP, ur, lr))
        tuplePtr = ur;
    else if (pnm_tupleequal(pamP, ll, lr))
        tuplePtr = ll;
    else {
        /* Reimplement libpnm3.c's mean4() but for tuples. */
        unsigned int plane;
        bgtuple = pnm_allocpamtuple(pamP);
        for (plane = 0; plane < pamP->depth; ++plane)
          bgtuple[plane] = (ul[plane] + ur[plane] + ll[plane] + lr[plane]) / 4;
    }
    if (!bgtuple) {
        unsigned int plane;
        bgtuple = pnm_allocpamtuple(pamP);
        for (plane = 0; plane < pamP->depth; ++plane)
          bgtuple[plane] = tuplePtr[plane];
    }

    return bgtuple;
}



/*=============================================================================
   pm_system() Standard Input feeder and Standard Output accepter functions.   
=============================================================================*/

void
pm_feed_from_pamtuples(int    const pipeToFeedFd,
                       void * const feederParm) {

    struct pamtuples * const inputTuplesP = feederParm;

    struct pam outpam;

    outpam = *inputTuplesP->pamP;
    outpam.file = fdopen(pipeToFeedFd, "w");

    /* The following signals (and normally kills) the process with
       SIGPIPE if the pipe does not take all the data.
    */
    pnm_writepam(&outpam, *inputTuplesP->tuplesP);

    pm_close(outpam.file);
}



void
pm_accept_to_pamtuples(int    const pipeToSuckFd,
                       void * const accepterParm ) {

    struct pamtuples * const outputTuplesP = accepterParm;

    struct pam * const inpamP = outputTuplesP->pamP;

    *outputTuplesP->tuplesP =
        pnm_readpam(fdopen(pipeToSuckFd, "r"), inpamP,
                    PAM_STRUCT_SIZE(tuple_type));

    pm_close(inpamP->file);
}