about summary refs log tree commit diff
path: root/converter/ppm/ppmtoarbtxt.c
blob: 6d4c6eac46b62fc3549db38ef55953bcf4a794db (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
/* ppmtoarbtxt.c - convert PPM to a custom text-based format
**
** Renamed from ppmtotxt.c by Bryan Henderson in January 2003.
**
** Copyright (C) 1995 by Peter Kirchgessner
**
** 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.
*/

#include <assert.h>
#include <string.h>
#ifdef __GLIBC__
  #include <printf.h>  /* Necessary for parse_printf_format() */
#endif

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

/* HAVE_PARSE_PRINTF_FORMAT means the system library has
   parse_printf_format(), declared in <printf.h>.  This essentially means
   systems with GNU libc.
*/

#ifndef HAVE_PARSE_PRINTF_FORMAT
  #ifdef PA_FLAG_MASK                   /* Defined in printf.h */
    #define HAVE_PARSE_PRINTF_FORMAT 1
  #else
    #define HAVE_PARSE_PRINTF_FORMAT 0
  #endif
#endif



struct CmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFileName;
    const char * bodySklFileName;
    const char * hd;
    const char * tl;
    unsigned int debug;
};



static void
parseCommandLine(int argc, const char ** argv,
                 struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that many of the strings that this function returns in the
   *cmdline_p structure are actually in the supplied argv array.  And
   sometimes, one of these strings is actually just a suffix of an entry
   in argv!
-----------------------------------------------------------------------------*/
    optEntry * option_def;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int hdSpec, tlSpec;

    unsigned int option_def_index;
    
    MALLOCARRAY(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0,   "hd",   OPT_STRING, &cmdlineP->hd, 
            &hdSpec,             0);
    OPTENT3(0,   "tl",   OPT_STRING, &cmdlineP->tl,
            &tlSpec,             0);
    OPTENT3(0,   "debug", OPT_FLAG, NULL,
            &cmdlineP->debug,      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);
    free(option_def);

    if (!hdSpec)
        cmdlineP->hd = NULL;

    if (!tlSpec)
        cmdlineP->tl = NULL;

    if (argc-1 < 1)
        pm_error("You must specify the body skeleton file name as an "
                 "argument");
    else {
        cmdlineP->bodySklFileName = strdup(argv[1]);

        if (argc-1 < 2)
            cmdlineP->inputFileName = strdup("-");  /* he wants stdin */
        else {
            cmdlineP->inputFileName = strdup(argv[2]);
            if (argc-1 > 2)
                pm_error("Too many arguments.  The only possible arguments "
                         "are the body skeleton file name and input image "
                         "file name");
        }
    }
}




typedef enum {
/* The types of object we handle */
    BDATA, IRED, IGREEN, IBLUE, ILUM, FRED, FGREEN, FBLUE, FLUM,
    WIDTH, HEIGHT, POSX, POSY
} SkeletonObjectType;

typedef enum {
    OBJTYP_ICOLOR, OBJTYP_FCOLOR, OBJTYP_INT, OBJTYP_BDATA
} SkeletonObjectClass;

/* Maximum size for a format string ("%d" etc.) */
/* Add one to this size for the terminating '\0'. */
#define MAXFORMAT 16

typedef union {
/* The data we keep for each object */
    struct Bndat {
        char * bdat;   /* Binary data (text with newlines etc.) */
        unsigned int ndat;
    } binData;
    
    struct Icdat {
        char icformat[MAXFORMAT+1];  /* Integer colors */
        unsigned int icolmin, icolmax;
    } icolData;

    struct Fcdat {
        char fcformat[MAXFORMAT+1];  /* Float colors */
        double fcolmin, fcolmax;
    } fcolData;
    
    struct Idat {
        char iformat[MAXFORMAT+1];   /* Integer data */
    } iData;
} SkeletonObjectData;


/* Each object has a type and some data */
typedef struct { 
    SkeletonObjectType objType;
    SkeletonObjectData odata;
} SkeletonObject;



#define MAX_SKL_HEAD_OBJ 64
#define MAX_SKL_BODY_OBJ 256
#define MAX_SKL_TAIL_OBJ 64
#define MAX_LINE_BUF 1024
#define MAX_OBJ_BUF 80



static void
dumpSkeleton(SkeletonObject ** const skeletonPList,
             unsigned int      const nSkeleton) {

    unsigned int i;

    pm_message("%u objects", nSkeleton);

    for (i = 0; i < nSkeleton; ++i) {
        SkeletonObject * const skeletonP = skeletonPList[i];

        pm_message("  Object: Type %u", skeletonP->objType);
    }
}



static void
dumpAllSkeleton(SkeletonObject ** const bodySkeletonPList,
                unsigned int      const bodyNskl,
                SkeletonObject ** const headSkeletonPList, 
                unsigned int      const headNskl,
                SkeletonObject ** const tailSkeletonPList,
                unsigned int      const tailNskl) {
    
    pm_message("Body skeleton:");
    dumpSkeleton(bodySkeletonPList, bodyNskl);

    pm_message("Head skeleton:");
    dumpSkeleton(headSkeletonPList, headNskl);

    pm_message("Tail skeleton:");
    dumpSkeleton(tailSkeletonPList, tailNskl);
}



static void
writeBndat(FILE *           const ofP,
           SkeletonObject * const objectP) {

    struct Bndat * const bdataP = &objectP->odata.binData;

    fwrite(bdataP->bdat, bdataP->ndat, 1, ofP);
}



static void
writeIcol(FILE *           const ofP,
          SkeletonObject * const objectP,
          double           const value) {

    /* Unlike Netpbm, the output format does not have an upper limit for
       maxval.  Here we allow all values representable by unsigned int.
    */

    struct Icdat * const icdataP = &objectP->odata.icolData;
    unsigned int const outValue =
        ROUNDU( icdataP->icolmin +
                ((double)icdataP->icolmax - icdataP->icolmin) * value);

    fprintf(ofP, icdataP->icformat, outValue);
}



static void
writeFcol(FILE *           const ofP,
          SkeletonObject * const objectP,
          double           const value) {

    struct Fcdat * const fcdataP = &objectP->odata.fcolData;
    
    fprintf(ofP, fcdataP->fcformat,
            (double)
            (fcdataP->fcolmin
             + (fcdataP->fcolmax - fcdataP->fcolmin) * value));
}



static void
writeIdat(FILE *           const ofP,
          SkeletonObject * const objectP,
          unsigned int     const value) {

    struct Idat * const idataP = &objectP->odata.iData;
    
    fprintf(ofP, idataP->iformat, value);
}



static void
writeText(FILE *            const ofP,
          unsigned int      const nObj,
          SkeletonObject ** const obj,
          unsigned int      const width,
          unsigned int      const height,
          unsigned int      const x,
          unsigned int      const y,
          double            const red,
          double            const green,
          double            const blue) {
    
    unsigned int i;

    for (i = 0; i < nObj; ++i) {
        switch (obj[i]->objType) {
        case BDATA:
            writeBndat(ofP, obj[i]);
            break;
        case IRED:
            writeIcol(ofP, obj[i], red);
            break;
        case IGREEN:
            writeIcol(ofP, obj[i], green);
            break;
        case IBLUE:
            writeIcol(ofP, obj[i], blue);
            break;
        case ILUM:
            writeIcol(ofP, obj[i],
                      PPM_LUMINR*red + PPM_LUMING*green + PPM_LUMINB*blue);
            break;
        case FRED:
            writeFcol(ofP, obj[i], red);
            break;
        case FGREEN:
            writeFcol(ofP, obj[i], green);
            break;
        case FBLUE:
            writeFcol(ofP, obj[i], blue);
            break;
        case FLUM:
            writeFcol(ofP, obj[i],
                      PPM_LUMINR*red + PPM_LUMING*green + PPM_LUMINB*blue);
            break;
        case WIDTH:
            writeIdat(ofP, obj[i], width);
            break;
        case HEIGHT:
            writeIdat(ofP, obj[i], height);
            break;
        case POSX:
            writeIdat(ofP, obj[i], x);
            break;
        case POSY:
            writeIdat(ofP, obj[i], y);
            break;
        }
    }
}



static SkeletonObjectClass
objClass(SkeletonObjectType const objType) {

    switch (objType) {
    case IRED:
    case IGREEN:
    case IBLUE:
    case ILUM:
        return OBJTYP_ICOLOR;

    case FRED:
    case FGREEN:
    case FBLUE:
    case FLUM:
        return OBJTYP_FCOLOR;

    case WIDTH:
    case HEIGHT:
    case POSX:
    case POSY:
        return OBJTYP_INT;
    case BDATA:
        return OBJTYP_BDATA;
    }
    return 999; /* quiet compiler warning */
}


/*----------------------------------------------------------------------------
  Format string validation

  We validate format strings (such as "%f" "%03d") found in the skeleton files
  for convenience, even though behavior is documented as undefined when the
  user supplies a bogus format string.  Certain strings, most notably those
  with "%n", are especially risky; they pose a security threat.

  On systems with Glibc, we check with parse_printf_format().  On other
  systems we conduct a cursory scan of the characters in the format string,
  looking for characters that trigger non-numeric conversions, etc.

  Documentation for parse_printf_format() is usually available in texinfo
  format on GNU/Linux systems.  As of Dec. 2014 there is no official man page.
  
  Online documentation is available from:
  https://
  www.gnu.org/software/libc/manual/html_node/Parsing-a-Template-String.html
-----------------------------------------------------------------------------*/

#if HAVE_PARSE_PRINTF_FORMAT
static void
validateParsePrintfFlag(int                const printfConversion,
                        SkeletonObjectType const ctyp,
                        const char **      const errorP) {
/*----------------------------------------------------------------------------
  Assuming 'printfConversion' is the value reported by parse_printf_format()
  as the type of argument a format string requires, 
  return an explanation of how it is incompatible with 'ctyp' as
  *errorP -- return null string if it is compatible.
-----------------------------------------------------------------------------*/
    /* We first check for "%n", then the type modifiers, and finally the
       actual conversion type (char, int, float, double, string or pointer.)
    */
    switch (printfConversion & PA_FLAG_MASK) {
    case PA_FLAG_PTR:  /* This means %n */
        pm_asprintf(errorP, "Contains a %%n conversion specifier");
        break;

    case PA_FLAG_SHORT:
    case PA_FLAG_LONG:
    case PA_FLAG_LONG_LONG:
        /* We omit PA_FLAG_LONG_DOUBLE because it is a synonym for
           PA_FLAG_LONG_LONG: listing both causes compilation errors.
        */
        pm_asprintf(errorP, "Invalid type modifier");
        break;

    default:
        switch (printfConversion & ~PA_FLAG_MASK) {
        case PA_CHAR:
            pm_message("Warning: char type conversion."); 
        case PA_INT:
            if(objClass(ctyp) == OBJTYP_ICOLOR ||
               objClass(ctyp) == OBJTYP_INT )
                *errorP = NULL;
            else
                pm_asprintf(errorP, "Conversion specifier requires a "
                            "character or integer argument, but it is in "
                            "a replacement sequence for a different type");
            break;
        case PA_DOUBLE:
            if(objClass(ctyp) == OBJTYP_FCOLOR)
                *errorP = NULL;
            else
                pm_asprintf(errorP, "Conversion specifier requires a "
                            "double precision floating point argument, "
                            "but it is in "
                            "a replacement sequence for a different type");
            break;
        case PA_FLOAT:
        case PA_STRING:    /* %s */
        case PA_POINTER:   /* %p */
        default:
            pm_asprintf(errorP, "Conversion specifier requires an argument of "
                        "a type that this program never provides for "
                        "any replacement sequence");
        }
    }
}
#endif



#if HAVE_PARSE_PRINTF_FORMAT
static void
validateFormatWithPpf(const char *       const format,
                      SkeletonObjectType const ctyp,
                      const char **      const errorP) {
/*----------------------------------------------------------------------------
  Validate format string 'format' for use with a skeleton of type 'ctyp',
  using the system parse_printf_format() function.

  Return as *errorP an explanation of how it is invalid, or a null string
  if it is valid.
-----------------------------------------------------------------------------*/
    /* We request parse_printf_format() to report the details of the first
       8 conversions.  8 because the maximum length of format is 16 means it
       can have up to 8 conversions: "%d%d%d%d%d%d%d%d".

       Actually this is more than necessary: we are concerned with only the
       first conversion and whether there it is the only one.
    */

    int printfConversion[MAXFORMAT/2] = {0, 0, 0, 0, 0, 0, 0, 0};

    size_t const n =
        parse_printf_format(format, MAXFORMAT/2, printfConversion);

    switch (n) {
    case 0:
        pm_asprintf(errorP, "No transformation found");
        break;

    case 1:
        validateParsePrintfFlag(printfConversion[0], ctyp, errorP);
        break;

    default:
        pm_asprintf(errorP, "Has %lu extra transformation%s ",
                    (unsigned long)n-1, n-1 > 1 ? "s" : "");
        break;
    }
}
#endif



static void
validateFormatOne(char               const typeSpecifier,
                  bool               const isLastInString,
                  SkeletonObjectType const ctyp,
                  bool *             const validatedP,
                  const char **      const errorP) {

    switch (typeSpecifier) {
        /* Valid character encountered.  Skip. */
        /* ' ' (space) is listed here, but should never occur for
           we use sscanf() to parse the fields.
        */
    case ' ': case '-': case '+': case '\'': case '#': case '.':
    case '0': case '1': case '2': case '3': case '4': case '5':
    case '6': case '7': case '8': case '9':
        break;
        
    case 'c': case 'C':
        pm_message("Warning: char type conversion: %%%c.", typeSpecifier);
    case 'i': case 'd': case 'u': case 'o': case 'x': case 'X':
        if (!isLastInString)
            pm_asprintf(errorP, "Extra characters at end");
        else if(objClass(ctyp) != OBJTYP_ICOLOR &&
                objClass(ctyp) != OBJTYP_INT )
            pm_asprintf(errorP, "Conversion type mismatch");
        else
            *validatedP = true;
        break;

    case 'f': case 'F': case 'g': case 'G': case 'a': case 'A':
        if (!isLastInString)
            pm_asprintf(errorP, "Extra characters at end");
        else if(objClass(ctyp) != OBJTYP_FCOLOR)
            pm_asprintf(errorP, "Conversion type mismatch");
        else
            *validatedP = true;
        break;

    case '\0':
        pm_asprintf(errorP, "No conversion specified");
        break;
    case '%':
        pm_asprintf(errorP, "No more than one %% is allowed");
        break;
    case '$':
    case '*':
        pm_asprintf(errorP, "%c is not allowed", typeSpecifier);
        break;
    case 'h': case 'l': case 'L': case 'q': case 'j': case 'Z': case 't':
        pm_asprintf(errorP, "Modifier %c is not allowed in format",
                    typeSpecifier);
        break;
    case 's': case 'S': case 'm': case 'p': case 'n':
        pm_asprintf(errorP, "Invalid conversion type");
        break;
    default:
        pm_asprintf(errorP, "Abnormal character");
        break;
    }
}



static void
validateFormatGen(const char *       const format,
                  SkeletonObjectType const ctyp,
                  const char **      const errorP)  {
/*----------------------------------------------------------------------------
  Validate format string 'format' for use with a skeleton of type 'ctyp',
  without using the system parse_printf_format() function.

  The string must begin with "%" and end with the translation type character
  ("%d", "%x", "%f", etc.)

  We check only for invalid characters.  Invalid constructs, such as
  "%00.00.00d" will pass this test.

  Return as *errorP an explanation of how it is invalid, or a null string
  if it is valid.
-----------------------------------------------------------------------------*/
    if (format[0] != '%')
        pm_asprintf(errorP, "Does not start with %%");
    else {
        unsigned int i;
        bool validated;

        for (i = 1, validated = false, *errorP = NULL;
             !validated && !*errorP;
             ++i) {

            validateFormatOne(format[i], format[i+1] == '\0', ctyp,
                              &validated, errorP);
        }
    }
}



static void
validateFormat(const char *       const format,
               SkeletonObjectType const ctyp) {

    const char * error;

    if (strlen(format) > MAXFORMAT)
        pm_asprintf(&error, "Too long");
    else {
#if HAVE_PARSE_PRINTF_FORMAT
        if (true)
            validateFormatWithPpf(format, ctyp, &error);
        else  /* Silence compiler warning about unused function */
            validateFormatGen(format, ctyp, &error);
#else
        validateFormatGen(format, ctyp, &error);
#endif
    }

    if (error)
        pm_error("Invalid format string '%s'.  %s", format, error);
}              
               


static SkeletonObject *
newBinDataObj(unsigned int const nDat, 
              const char * const bdat) {
/*----------------------------------------------------------------------------
  Create a binary data object.
-----------------------------------------------------------------------------*/
    SkeletonObject * objectP;

    objectP = malloc(sizeof(*objectP) + nDat);

    if (!objectP)
        pm_error("Failed to allocate memory for binary data object "
                 "with %u bytes", nDat);

    objectP->objType = BDATA;
    objectP->odata.binData.ndat = nDat;
    objectP->odata.binData.bdat = ((char *)objectP) + sizeof(SkeletonObject);
    memcpy(objectP->odata.binData.bdat, bdat, nDat);

    return objectP;
}



static SkeletonObject *
newIcolDataObj(SkeletonObjectType const ctyp,
               const char *       const format,
               unsigned int       const icolmin,
               unsigned int       const icolmax) {
/*----------------------------------------------------------------------------
  Create integer color data object.
-----------------------------------------------------------------------------*/
    SkeletonObject * objectP;

    MALLOCVAR(objectP);

    if (!objectP)
        pm_error("Failed to allocate memory for an integer color data "
                 "object");

    objectP->objType = ctyp;
    validateFormat(format, ctyp);
    strcpy(objectP->odata.icolData.icformat, format);
    objectP->odata.icolData.icolmin = icolmin;
    objectP->odata.icolData.icolmax = icolmax;

    return objectP;
}



static SkeletonObject *
newFcolDataObj(SkeletonObjectType  const ctyp,
               const char *        const format,
               double              const fcolmin,
               double              const fcolmax) {
/*----------------------------------------------------------------------------
  Create float color data object.
-----------------------------------------------------------------------------*/
    SkeletonObject * objectP;

    MALLOCVAR(objectP);

    if (!objectP)
        pm_error("Failed to allocate memory for a float color data object");

    objectP->objType = ctyp;
    validateFormat(format, ctyp);
    strcpy(objectP->odata.fcolData.fcformat, format);
    objectP->odata.fcolData.fcolmin = fcolmin;
    objectP->odata.fcolData.fcolmax = fcolmax;

    return objectP;
}



static SkeletonObject *
newIdataObj(SkeletonObjectType const ctyp,
            const char *       const format) {
/*----------------------------------------------------------------------------
  Create universal data object.
-----------------------------------------------------------------------------*/
    SkeletonObject * objectP;

    MALLOCVAR(objectP);

    if (!objectP)
        pm_error("Failed to allocate memory for a universal data object");

    objectP->objType = ctyp;
    validateFormat(format, ctyp);
    strcpy(objectP->odata.iData.iformat, format);

    return objectP;
}



static char const escape = '#';



static SkeletonObjectType
interpretObjType(const char * const typstr) {

    SkeletonObjectType objType;

    /* handle integer colors */
    if      (streq(typstr, "ired")  ) objType = IRED;
    else if (streq(typstr, "igreen")) objType = IGREEN;
    else if (streq(typstr, "iblue") ) objType = IBLUE;
    else if (streq(typstr, "ilum")  ) objType = ILUM;
    /* handle real colors */
    else if (streq(typstr, "fred")  ) objType = FRED;
    else if (streq(typstr, "fgreen")) objType = FGREEN;
    else if (streq(typstr, "fblue") ) objType = FBLUE;
    else if (streq(typstr, "flum")  ) objType = FLUM;
    /* handle integer data */
    else if (streq(typstr, "width") ) objType = WIDTH;
    else if (streq(typstr, "height")) objType = HEIGHT;
    else if (streq(typstr, "posx")  ) objType = POSX;
    else if (streq(typstr, "posy")  ) objType = POSY;
    else                              objType = BDATA;

    return objType;
}



static SkeletonObject *
newIcSkelFromReplString(const char *       const icolorObjstr,
                        SkeletonObjectType const objType) {
/*----------------------------------------------------------------------------
  A new skeleton for an integer color substitution specifier (class
  OBJTYP_ICOLOR) whose replacement string (the stuff between the parentheses
  in #(...)) says substitution type 'objType' and the rest of the
  replacement string is 'icolorObjstr'.
-----------------------------------------------------------------------------*/
    SkeletonObject * retval;
    unsigned int icolmin, icolmax;
    char formstr[MAX_OBJ_BUF];
    int nOdata;

    nOdata = sscanf(icolorObjstr, "%s%u%u", formstr, &icolmin, &icolmax);

    if (nOdata == 3)
        retval = newIcolDataObj(objType, formstr, icolmin, icolmax);
    else if (nOdata == EOF) {
        /* No arguments specified.  Use defaults */
        retval = newIcolDataObj(objType, "%u", 0, 255);
    } else
        retval = NULL;

    return retval;
}



static SkeletonObject *
newFcSkelFromReplString(const char *       const fcolorObjstr,
                        SkeletonObjectType const objType) {
/*----------------------------------------------------------------------------
  A new skeleton for a floating point color substitution specifier (class
  OBJTYP_FCOLOR) whose replacement string (the stuff between the parentheses
  in #(...)) says substitution type 'objType' and the rest of the
  replacement string is 'fcolorObjstr'.
-----------------------------------------------------------------------------*/
    SkeletonObject * retval;
    double fcolmin, fcolmax;
    char formstr[MAX_OBJ_BUF];
    int nOdata;

    nOdata = sscanf(fcolorObjstr, "%s%lf%lf", formstr, &fcolmin, &fcolmax);

    if (nOdata == 3)
        retval = newFcolDataObj(objType, formstr, fcolmin, fcolmax);
    else if (nOdata == EOF) {
        /* No arguments specified.  Use defaults */
        retval = newFcolDataObj(objType, "%f", 0.0, 1.0);
    } else
        retval = NULL;

    return retval;
} 



static SkeletonObject *
newISkelFromReplString(const char *       const intObjstr,
                       SkeletonObjectType const objType) {
/*----------------------------------------------------------------------------
  A new skeleton for an integer substitution specifier (class OBJTYP_INT)
  whose replacement string (the stuff between the parentheses in #(...))
  says substitution type 'objType' and the rest of the replacement string is
  'intObjstr'.
-----------------------------------------------------------------------------*/
    SkeletonObject * retval;
    char formstr[MAX_OBJ_BUF];
    int nOdata;

    nOdata = sscanf(intObjstr, "%s", formstr);
    
    if (nOdata == 1)
        retval = newIdataObj(objType, formstr);
    else if (nOdata == EOF) {
        /* No arguments specified.  Use defaults */
        retval = newIdataObj(objType, "%u");
    } else
        retval = NULL;

    return retval;
} 



static SkeletonObject *
newSkeletonFromReplString(const char * const objstr) {
/*----------------------------------------------------------------------------
  A new skeleton created from the replacement string 'objstr' (the stuff
  between the parentheses in #(...) ).

  Return NULL if it isn't a valid replacement string.
-----------------------------------------------------------------------------*/
    /* We use sscanf() to parse the contents of objstr, giving it a format
       template with the largest number of fields possible plus one extra to
       pick up and check for the existence of invalid trailing characters.  We
       read and discard fields beyond the first, if any.  The appropriate
       new**SkelFromReplString() function determines their contents with a
       separate call to sscanf().
    */

    SkeletonObject * retval;
    char typstr[MAX_OBJ_BUF];
    int typlen;
    SkeletonObjectType objType;
    int conversionCt;
    char s1[MAX_OBJ_BUF];    /* Dry read. */
    char s2[MAX_OBJ_BUF];    /* Extra tailing characters. */
    float f1, f2;            /* Dry read. */ 

    typstr[0] = '\0';  /* initial value */

    conversionCt = sscanf(objstr, "%s%n%s%f%f%s",
                          typstr, &typlen, s1, &f1, &f2, s2);
    switch (conversionCt) {
    case 1: case 2: case 4:
        objType = interpretObjType(typstr);
      break;
    default:
        objType = BDATA;
    }

    switch (objClass(objType)) {
    case OBJTYP_ICOLOR:
        retval = newIcSkelFromReplString(&objstr[typlen], objType);
        break;
    case OBJTYP_FCOLOR:
        retval = newFcSkelFromReplString(&objstr[typlen], objType);
        break;
    case OBJTYP_INT:
        retval = newISkelFromReplString(&objstr[typlen], objType);
        break;
    case OBJTYP_BDATA:
        retval = NULL;
    }
    return retval;
}



static void
readThroughCloseParen(FILE * const ifP,
                      char * const objstr,
                      size_t const objstrSize,
                      bool * const unclosedP) {
/*----------------------------------------------------------------------------
   Read *ifP up through close parenthesis ( ')' ) into 'objstr', which
   is of size 'objstrSize'.  Make it a NUL-terminated string.

   Return *unclosedP true iff we run out of file or run out of objstr
   before we see a close parenthesis.  In this case, return the rest of
   the file, or as much as fits, in 'objstr', not NUL-terminated.
-----------------------------------------------------------------------------*/
    unsigned int i;
    bool eof;
    bool gotEscSeq;

    for (i= 0, eof = false, gotEscSeq = false;
         i < objstrSize - 1 && !gotEscSeq && !eof;
         ++i) {

        int rc;

        rc = getc(ifP);
        if (rc == EOF)
            eof = true;
        else {
            char const chr = rc;
            if (chr == ')') {
                gotEscSeq = true;
                objstr[i] = '\0';
	        } else
                objstr[i] = chr;
        }
    }
    *unclosedP = !gotEscSeq;
}



typedef struct {
    unsigned int      capacity;
    SkeletonObject ** skeletonPList;
    unsigned int      nSkeleton;
} SkeletonBuffer;



static void
SkeletonBuffer_init(SkeletonBuffer *  const bufferP,
                    unsigned int      const capacity,
                    SkeletonObject ** const skeletonPList) {

    bufferP->capacity      = capacity;
    bufferP->skeletonPList = skeletonPList;
    bufferP->nSkeleton     = 0;
}



static void
SkeletonBuffer_add(SkeletonBuffer * const bufferP,
                   SkeletonObject * const skeletonP) {

    if (bufferP->nSkeleton >= bufferP->capacity)
        pm_error("Too many skeletons.  Max = %u", bufferP->capacity);

    bufferP->skeletonPList[bufferP->nSkeleton++] = skeletonP;
}                   



typedef struct {

    char data[MAX_LINE_BUF + MAX_OBJ_BUF + 16];

    unsigned int length;

    SkeletonBuffer * skeletonBufferP;
        /* The buffer to which we flush.  Flushing means turning all the
           characters currently in our buffer into a binary skeleton object
           here.
        */

} Buffer;



static void
Buffer_init(Buffer *         const bufferP,
            SkeletonBuffer * const skeletonBufferP) {

    bufferP->skeletonBufferP = skeletonBufferP;
    bufferP->length = 0;
}



static void
Buffer_flush(Buffer * const bufferP) {
/*----------------------------------------------------------------------------
   Flush the buffer out to a binary skeleton object.
-----------------------------------------------------------------------------*/
    SkeletonBuffer_add(bufferP->skeletonBufferP,
                       newBinDataObj(bufferP->length, bufferP->data));

    bufferP->length = 0;
}



static void
Buffer_add(Buffer * const bufferP,
           char     const newChar) {

    if (bufferP->length >= MAX_LINE_BUF)
        Buffer_flush(bufferP);

    assert(bufferP->length < MAX_LINE_BUF);

    bufferP->data[bufferP->length++] = newChar;
}



static void
Buffer_dropFinalNewline(Buffer * const bufferP) {
/*----------------------------------------------------------------------------
   If the last thing in the buffer is a newline, remove it.
-----------------------------------------------------------------------------*/
    if (bufferP->length >= 1 && bufferP->data[bufferP->length-1] == '\n') {
            /* Drop finishing newline character */
            --bufferP->length;
    }
}



static void
addImpostorReplacementSeq(Buffer *     const bufferP,
                          const char * const seqContents) {
/*----------------------------------------------------------------------------
  Add to buffer *bufferP something that looks like a replacement sequence but
  doesn't have the proper contents (the stuff between the parentheses) to be
  one.  For example,

  "#(fread x)"

  seqContents[] is the contents, NUL-terminated.
-----------------------------------------------------------------------------*/
    const char * p;

    Buffer_add(bufferP, escape);
    Buffer_add(bufferP, '(');

    for (p = &seqContents[0]; *p; ++p)
        Buffer_add(bufferP, *p);

    Buffer_add(bufferP, ')');
}



static void
readSkeletonFile(const char *      const filename,
                 unsigned int      const maxskl,
                 const char **     const errorP,
                 unsigned int *    const nSkeletonP,
                 SkeletonObject ** const skeletonPList) {
/*----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
    FILE * sklfileP;
    SkeletonBuffer skeletonBuffer;
        /* A buffer for accumulating skeleton objects */
    Buffer buffer;
        /* A buffer for accumulating binary (literal; unsubstituted) data, on
           its way to becoming a binary skeleton object. 
        */
    bool eof;
    const char * error;

    SkeletonBuffer_init(&skeletonBuffer, maxskl, skeletonPList);

    Buffer_init(&buffer, &skeletonBuffer);

    sklfileP = pm_openr(filename);

    for (eof = false, error = NULL; !eof && !error; ) {

        int rc;

        rc = getc(sklfileP);

        if (rc == EOF)
            eof = true;
        else {
            char const chr = rc;

            if (chr != escape) {
                /* Not a replacement sequence; just a literal character */
                Buffer_add(&buffer, chr);
            } else {
                int rc;
                rc = getc(sklfileP);
                if (rc == EOF) {
                    /* Not a replacement sequence, just an escape character
                       at the end of the file.
                    */
                    Buffer_add(&buffer, escape);
                    eof = true;
                } else {
                    char const chr = rc;

                    if (chr != '(') {
                        /* Not a replacement sequence, just a lone escape
                           character
                        */
                        Buffer_add(&buffer, escape);
                        Buffer_add(&buffer, chr);
                    } else {
                        char objstr[MAX_OBJ_BUF];
                        bool unclosed;
                        readThroughCloseParen(sklfileP,
                                              objstr, sizeof(objstr),
                                              &unclosed);
                        if (unclosed)
                            pm_asprintf(&error, "Unclosed parentheses "
                                        "in #() escape sequence");
                        else {
                            SkeletonObject * const skeletonP =
                                newSkeletonFromReplString(objstr);

                            if (skeletonP) {
                                Buffer_flush(&buffer);
                                SkeletonBuffer_add(&skeletonBuffer, skeletonP);
                            } else
                                addImpostorReplacementSeq(&buffer, objstr);
                        }
                    }
                }
            }
        }
    }

    if (!error) {
        Buffer_dropFinalNewline(&buffer);
        Buffer_flush(&buffer);
    }
    *errorP = error;
    *nSkeletonP = skeletonBuffer.nSkeleton;

    fclose(sklfileP);
}



static void
convertIt(FILE *            const ifP,
          FILE *            const ofP,
          SkeletonObject ** const bodySkeletonPList,
          unsigned int      const bodyNskl,
          SkeletonObject ** const headSkeletonPList, 
          unsigned int      const headNskl,
          SkeletonObject ** const tailSkeletonPList,
          unsigned int      const tailNskl) {

    pixel * pixelrow;
    pixval maxval;
    double dmaxval;
    int rows, cols;
    int format;
    unsigned int row;

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

    pixelrow = ppm_allocrow(cols);

    dmaxval = (double)maxval;

    /* Write header */
    writeText(ofP, headNskl, headSkeletonPList, 
              cols, rows , 0, 0, 0.0, 0.0, 0.0);

    /* Write raster */
    for (row = 0; row < rows; ++row) {
        unsigned int col;

        ppm_readppmrow(ifP, pixelrow, cols, maxval, format);

        for (col = 0; col < cols; ++col) {
            pixel const thisPixel = pixelrow[col];

            writeText(ofP, bodyNskl, bodySkeletonPList,
                      cols, rows, col, row,
                      PPM_GETR(thisPixel)/dmaxval,
                      PPM_GETG(thisPixel)/dmaxval,
                      PPM_GETB(thisPixel)/dmaxval);
        }
    }

    /* Write trailer */
    writeText(ofP, tailNskl, tailSkeletonPList, 
              cols, rows, 0, 0, 0.0, 0.0, 0.0);
}



int
main(int           argc,
     const char ** argv) {
    
    struct CmdlineInfo cmdline;

    unsigned int headNskl, bodyNskl, tailNskl;
    SkeletonObject * headSkeletonPList[MAX_SKL_HEAD_OBJ];
    SkeletonObject * bodySkeletonPList[MAX_SKL_BODY_OBJ];
    SkeletonObject * tailSkeletonPList[MAX_SKL_TAIL_OBJ];
    FILE * ifP;
    const char * error;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    readSkeletonFile(cmdline.bodySklFileName, ARRAY_SIZE(bodySkeletonPList),
                     &error, &bodyNskl, bodySkeletonPList);
    if (error)
        pm_error("Invalid body skeleton file '%s'.  %s",
                 cmdline.bodySklFileName, error);

    if (cmdline.hd) {
        readSkeletonFile(cmdline.hd, ARRAY_SIZE(headSkeletonPList),
                         &error, &headNskl, headSkeletonPList);
        if (error)
            pm_error("Invalid head skeleton file '%s'.  %s",
                     cmdline.hd, error);
    } else
        headNskl = 0;

    if (cmdline.tl) {
        readSkeletonFile(cmdline.tl, ARRAY_SIZE(tailSkeletonPList),
                         &error, &tailNskl, tailSkeletonPList);
        if (error)
            pm_error("Invalid tail skeleton file '%s'.  %s",
                     cmdline.tl, error);
    } else
        tailNskl = 0;

    if (cmdline.debug)
        dumpAllSkeleton(bodySkeletonPList, bodyNskl,
                        headSkeletonPList, headNskl,
                        tailSkeletonPList, tailNskl);

    convertIt(ifP, stdout,
              bodySkeletonPList, bodyNskl,
              headSkeletonPList, headNskl,
              tailSkeletonPList, tailNskl);

    pm_close(ifP);

    return 0;
}