about summary refs log tree commit diff
path: root/editor/pnmremap.c
blob: dcd4e2a52e8395c7198108a4e14d3be20ab29089 (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
/******************************************************************************
                               pnmremap.c
*******************************************************************************

  Replace colors in an input image with colors from a given colormap image.

  For PGM input, do the equivalent.

  By Bryan Henderson, San Jose, CA 2001.12.17

  Derived from ppmquant, originally by Jef Poskanzer.

  Copyright (C) 1989, 1991 by Jef Poskanzer.
  Copyright (C) 2001 by Bryan Henderson.

  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 <limits.h>
#include <math.h>
#include <assert.h>

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

#define MAXCOLORS 32767u

enum missingMethod {
    MISSING_FIRST,
    MISSING_SPECIFIED,
    MISSING_CLOSE
};

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFilespec;  /* Filespec of input file */
    const char * mapFilespec;    /* Filespec of colormap file */
    unsigned int floyd;   /* Boolean: -floyd/-fs option */
    unsigned int norandom;
    enum missingMethod missingMethod;
    char * missingcolor;      
        /* -missingcolor value.  Null if not specified */
    unsigned int verbose;
};



static void
parseCommandLine (int argc, char ** argv,
                  struct cmdlineInfo *cmdlineP) {
/*----------------------------------------------------------------------------
   parse program command line described in Unix standard form by argc
   and argv.  Return the information in the options as *cmdlineP.  

   If command line is internally inconsistent (invalid options, etc.),
   issue error message to stderr and abort program.

   Note that the strings we return are stored in the storage that
   was passed to us as the argv array.  We also trash *argv.
-----------------------------------------------------------------------------*/
    optEntry * option_def;
        /* Instructions to pm_optParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    unsigned int nofloyd, firstisdefault;
    unsigned int missingSpec, mapfileSpec;

    MALLOCARRAY_NOFAIL(option_def, 100);
    
    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0,   "floyd",        OPT_FLAG,   
            NULL,                       &cmdlineP->floyd, 0);
    OPTENT3(0,   "fs",           OPT_FLAG,   
            NULL,                       &cmdlineP->floyd, 0);
    OPTENT3(0,   "nofloyd",      OPT_FLAG,   
            NULL,                       &nofloyd, 0);
    OPTENT3(0,   "nofs",         OPT_FLAG,   
            NULL,                       &nofloyd, 0);
    OPTENT3(0,   "norandom",     OPT_FLAG,   
            NULL,                       &cmdlineP->norandom, 0);
    OPTENT3(0,   "firstisdefault", OPT_FLAG,   
            NULL,                       &firstisdefault, 0);
    OPTENT3(0,   "mapfile",      OPT_STRING, 
            &cmdlineP->mapFilespec,    &mapfileSpec, 0);
    OPTENT3(0,   "missingcolor", OPT_STRING, 
            &cmdlineP->missingcolor,   &missingSpec, 0);
    OPTENT3(0, "verbose",        OPT_FLAG,   NULL,                  
            &cmdlineP->verbose,        0 );

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

    cmdlineP->missingcolor = NULL;  /* default value */
    
    pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
        /* Uses and sets argc, argv, and some of *cmdline_p and others. */

    if (cmdlineP->floyd && nofloyd)
        pm_error("You cannot specify both -floyd and -nofloyd options.");

    if (firstisdefault && missingSpec)
        pm_error("You cannot specify both -missing and -firstisdefault.");

    if (firstisdefault)
        cmdlineP->missingMethod = MISSING_FIRST;
    else if (missingSpec)
        cmdlineP->missingMethod = MISSING_SPECIFIED;
    else
        cmdlineP->missingMethod = MISSING_CLOSE;

    if (!mapfileSpec)
        pm_error("You must specify the -mapfile option.");

    if (argc-1 > 1)
        pm_error("Program takes at most one argument: the input file "
                 "specification.  "
                 "You specified %d arguments.", argc-1);
    if (argc-1 < 1)
        cmdlineP->inputFilespec = "-";
    else
        cmdlineP->inputFilespec = argv[1];
}


typedef enum {
    ADJUST_NONE,
    ADJUST_RGBTO1,
    ADJUST_GRAYSCALETO3
} depthAdjustment;



static void
rgbToDepth1(tuple const tuple) {

    unsigned int plane;
    double grayvalue;

    grayvalue = 0.0;  /* initial value */

    for (plane = 0; plane < 3; ++plane)
        grayvalue += pnm_lumin_factor[plane] * tuple[plane];

    tuple[0] = (sample) (grayvalue + 0.5);
}



static void
grayscaleToDepth3(tuple const tuple) {

    tuple[1] = tuple[0];
    tuple[2] = tuple[0];
}



static void
adjustDepthTuple(tuple           const tuple,
                 depthAdjustment const adjustment) {
    
    switch (adjustment) {
    case ADJUST_NONE:
        break;
    case ADJUST_RGBTO1:
        rgbToDepth1(tuple);
        break;
    case ADJUST_GRAYSCALETO3:
        grayscaleToDepth3(tuple);
        break;
    }
}



static void
inverseAdjustDepthTuple(tuple           const tuple,
                        depthAdjustment const adjustment) {
    
    switch (adjustment) {
    case ADJUST_NONE:
        break;
    case ADJUST_RGBTO1:
        grayscaleToDepth3(tuple);
        break;
    case ADJUST_GRAYSCALETO3:
        rgbToDepth1(tuple);
        break;
    }
}



static void
adjustDepthRow(tuple *         const tupleRow,
               unsigned int    const width,
               depthAdjustment const adjustment) {
/*----------------------------------------------------------------------------
   Change tupleRow[] depth as indicated by 'adjustment',
   i.e. turned from RGB to grayscale or grayscale to RGB.

   We assume tupleRow[] is consistent with 'adjustment' -- i.e. if
   'adjustment' says grayscale to RGB, tupleRow[] has an allocation depth of
   at least 3 and if 'adjustment' says from RGB to grayscale, tupleRow[] has
   RGB tuples.
-----------------------------------------------------------------------------*/
    if (adjustment == ADJUST_NONE) {
    } else {
        unsigned int col;

        for (col = 0; col < width; ++col) {
            if (adjustment == ADJUST_RGBTO1)
                rgbToDepth1(tupleRow[col]);
            else {
                assert(adjustment == ADJUST_GRAYSCALETO3);
                grayscaleToDepth3(tupleRow[col]);
            }
        }
    }
}



static void
selectDepthAdjustment(const struct pam * const pamP,
                      unsigned int       const newDepth,
                      depthAdjustment *  const adjustmentP) {
/*----------------------------------------------------------------------------
   Determine what kind of depth adjustment the pixels of an image described
   by 'pamP' need to be comparable to pixels with depth 'newDepth'.

   The only depth changes we know how to do are:

     - from tuple type RGB, depth 3 to depth 1 

       We change it to grayscale or black and white.

       For this, we return *adjustmentP == ADJUST_RGBTO1.

     - from tuple type GRAYSCALE or BLACKANDWHITE depth 1 to depth 3.

       We change it to RGB.

       For this, we return *adjustmentP == ADJUST_GRAYSCALETO3.

   For any other depth change request, we issue an error message and abort
   the program.

   If 'newDepth' is the same depth as the original (no depth change required),
   we return *adjustmentP == ADJUST_NONE.
-----------------------------------------------------------------------------*/
    if (newDepth == pamP->depth)
        *adjustmentP = ADJUST_NONE;
    else {
        if (pm_stripeq(pamP->tuple_type, "RGB")) {
            if (newDepth != 1) {
                pm_error("Map image depth of %u differs from input image "
                         "depth of %u, and the tuple type is RGB.  "
                         "The only depth to which I know how to convert "
                         "an RGB tuple is 1.",
                         newDepth, pamP->depth);
            } else
                *adjustmentP = ADJUST_RGBTO1;
        } else if (pm_stripeq(pamP->tuple_type, "GRAYSCALE") ||
                   pm_stripeq(pamP->tuple_type, "BLACKANDWHITE")) {
            if (newDepth != 3) {
                pm_error("Map image depth of %u differs from input image "
                         "depth of %u, and the tuple type is GRAYSCALE "
                         "or BLACKANDWHITE.  "
                         "The only depth to which I know how to convert "
                         "a GRAYSCALE or BLACKANDWHITE tuple is 3.",
                         newDepth, pamP->depth);
            } else
                *adjustmentP = ADJUST_GRAYSCALETO3;
        } else {
            pm_error("Map image depth of %u differs from input image depth "
                     "of %u, and the input image does not have a tuple type "
                     "that I know how to convert to the map depth.  "
                     "I can convert RGB, GRAYSCALE, and BLACKANDWHITE.  "
                     "The input image is '%.*s'.",
                     newDepth, pamP->depth, 
                     (int)sizeof(pamP->tuple_type), pamP->tuple_type);
        }
    }
}



static void
computeColorMapFromMap(struct pam *   const mappamP, 
                       tuple **       const maptuples, 
                       tupletable *   const colormapP,
                       unsigned int * const newcolorsP) {
/*----------------------------------------------------------------------------
   Produce a colormap containing the colors that we will use in the output.

   Make it include exactly those colors that are in the image
   described by *mappamP and maptuples[][].

   Return the number of colors in the returned colormap as *newcolorsP.
-----------------------------------------------------------------------------*/
    unsigned int colors; 

    if (mappamP->width == 0 || mappamP->height == 0)
        pm_error("colormap file contains no pixels");

    *colormapP = 
        pnm_computetuplefreqtable(mappamP, maptuples, MAXCOLORS, &colors);
    if (*colormapP == NULL)
        pm_error("too many colors in colormap!");
    pm_message("%u colors found in colormap", colors);
    *newcolorsP = colors;
}



#define FS_SCALE 1024

struct fserr {
    unsigned int width;
        /* Width of the image being dithered */
    long ** thiserr;
    long ** nexterr;
    bool    fsForward;
        /* We are in a left-to-right row */
    int     begCol;
        /* First column in the sweep.  Determined by 'fsForward': either
           the leftmost or rightmost column in the row
        */
    int     endCol;
        /* Column after the last column in the sweep.  Determined by
           'fsForward': either one past the left end or one past the right
           end of the row.
        */
    int     step;
        /* What we add to a column number to get the next one in the sweep.
           Determined by 'fsForward': +1 or -1.
        */
};



static void
randomizeError(long **      const err,
               unsigned int const width,
               unsigned int const depth) {
/*----------------------------------------------------------------------------
   Set a random error in the range [-1 .. 1] (normalized via FS_SCALE)
   in the error array err[][].
-----------------------------------------------------------------------------*/
    unsigned int col;

    srand(pm_randseed());

    for (col = 0; col < width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < depth; ++plane) 
            err[plane][col] = rand() % (FS_SCALE * 2) - FS_SCALE;
    }
}



static void
fserrSetForward(struct fserr * const fserrP) {

    fserrP->fsForward = TRUE;
    fserrP->begCol = 0;
    fserrP->endCol = fserrP->width;
    fserrP->step   = +1;
}



static void
fserrSetBackward(struct fserr * const fserrP) {

    fserrP->fsForward = FALSE;
    fserrP->begCol = fserrP->width - 1;
    fserrP->endCol = -1;
    fserrP->step   = -1;
}



static void
initFserr(struct pam *   const pamP,
          struct fserr * const fserrP,
          bool           const initRandom) {
/*----------------------------------------------------------------------------
   Initialize the Floyd-Steinberg error vectors
-----------------------------------------------------------------------------*/
    unsigned int plane;

    unsigned int const fserrSize = pamP->width + 2;

    fserrP->width = pamP->width;

    MALLOCARRAY(fserrP->thiserr, pamP->depth);
    if (fserrP->thiserr == NULL)
        pm_error("Out of memory allocating Floyd-Steinberg structures "
                 "for depth %u", pamP->depth);
    MALLOCARRAY(fserrP->nexterr, pamP->depth);
    if (fserrP->nexterr == NULL)
        pm_error("Out of memory allocating Floyd-Steinberg structures "
                 "for depth %u", pamP->depth);
    
    for (plane = 0; plane < pamP->depth; ++plane) {
        MALLOCARRAY(fserrP->thiserr[plane], fserrSize);
        if (fserrP->thiserr[plane] == NULL)
            pm_error("Out of memory allocating Floyd-Steinberg structures "
                     "for Plane %u, size %u", plane, fserrSize);
        MALLOCARRAY(fserrP->nexterr[plane], fserrSize);
        if (fserrP->nexterr[plane] == NULL)
            pm_error("Out of memory allocating Floyd-Steinberg structures "
                     "for Plane %u, size %u", plane, fserrSize);
    }

    if (initRandom)
        randomizeError(fserrP->thiserr, fserrSize, pamP->depth);

    fserrSetForward(fserrP);
}



static void
floydInitRow(struct pam * const pamP, struct fserr * const fserrP) {

    int col;
    
    for (col = 0; col < pamP->width + 2; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane) 
            fserrP->nexterr[plane][col] = 0;
    }
}



static void
floydAdjustColor(struct pam *   const pamP, 
                 tuple          const intuple, 
                 tuple          const outtuple, 
                 struct fserr * const fserrP, 
                 int            const col) {
/*----------------------------------------------------------------------------
  Use Floyd-Steinberg errors to adjust actual color.
-----------------------------------------------------------------------------*/
    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane) {
        long int const s =
            intuple[plane] + fserrP->thiserr[plane][col+1] / FS_SCALE;
        outtuple[plane] = MIN(pamP->maxval, MAX(0,s));
    }
}



static void
floydPropagateErr(struct pam *   const pamP, 
                  struct fserr * const fserrP, 
                  int            const col, 
                  tuple          const oldtuple, 
                  tuple          const newtuple) {
/*----------------------------------------------------------------------------
  Propagate Floyd-Steinberg error terms.

  The error is due to substituting the tuple value 'newtuple' for the
  tuple value 'oldtuple' (both described by *pamP).  The error terms
  are meant to be used to introduce a compensating error into the
  future selection of tuples nearby in the image.
-----------------------------------------------------------------------------*/
    unsigned int plane;
    for (plane = 0; plane < pamP->depth; ++plane) {
        long const newSample = newtuple[plane];
        long const oldSample = oldtuple[plane];
        long const err = (oldSample - newSample) * FS_SCALE;
            
        if (fserrP->fsForward) {
            fserrP->thiserr[plane][col + 2] += ( err * 7 ) / 16;
            fserrP->nexterr[plane][col    ] += ( err * 3 ) / 16;
            fserrP->nexterr[plane][col + 1] += ( err * 5 ) / 16;
            fserrP->nexterr[plane][col + 2] += ( err     ) / 16;
        } else {
            fserrP->thiserr[plane][col    ] += ( err * 7 ) / 16;
            fserrP->nexterr[plane][col + 2] += ( err * 3 ) / 16;
            fserrP->nexterr[plane][col + 1] += ( err * 5 ) / 16;
            fserrP->nexterr[plane][col    ] += ( err     ) / 16;
        }
    }
}



static void
floydSwitchDir(struct pam * const pamP, struct fserr * const fserrP) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane) {
        long * const temperr = fserrP->thiserr[plane];
        fserrP->thiserr[plane] = fserrP->nexterr[plane];
        fserrP->nexterr[plane] = temperr;
    }

    if (fserrP->fsForward)
        fserrSetBackward(fserrP);
    else
        fserrSetForward(fserrP);
}



struct colormapFinder {
/*----------------------------------------------------------------------------
   This is an object that finds a color in a colormap.  The methods
   'searchColormapClose' and 'searchColormapExact' belong to it.

   This object ought to encompass the hash table as well some day and
   possibly encapsulate the color map altogether and just be an object
   that opaquely maps input colors to output colors.
-----------------------------------------------------------------------------*/
    tupletable colormap;
    unsigned int colors;
        /* Number of colors in 'colormap'.  At least 1 */
    unsigned int distanceDivider;
        /* The value by which our intermediate distance calculations
           have to be divided to make sure we don't overflow our
           unsigned int data structure.
           
           To the extent 'distanceDivider' is greater than 1, closest
           color results will be approximate -- there could
           conceivably be a closer one that we miss.
        */
};



static void
createColormapFinder(struct pam *             const pamP,
                     tupletable               const colormap,
                     unsigned int             const colors,
                     struct colormapFinder ** const colormapFinderPP) {

    struct colormapFinder * colormapFinderP;

    MALLOCVAR_NOFAIL(colormapFinderP);

    colormapFinderP->colormap = colormap;
    colormapFinderP->colors = colors;

    {
        unsigned int const maxHandleableSqrDiff = 
            (unsigned int)UINT_MAX / pamP->depth;
        
        if (SQR(pamP->maxval) > maxHandleableSqrDiff)
            colormapFinderP->distanceDivider = (unsigned int)
                (SQR(pamP->maxval) / maxHandleableSqrDiff + 0.1 + 1.0);
                /* The 0.1 is a fudge factor to keep us out of rounding 
                   trouble.  The 1.0 effects a round-up.
                */
        else
            colormapFinderP->distanceDivider = 1;
    }
    *colormapFinderPP = colormapFinderP;
}



static void
destroyColormapFinder(struct colormapFinder * const colormapFinderP) {

    free(colormapFinderP);
}



static void
searchColormapClose(struct pam *            const pamP,
                    tuple                   const tuple,
                    struct colormapFinder * const colorFinderP,
                    int *                   const colormapIndexP) {
/*----------------------------------------------------------------------------
   Search the colormap indicated by *colorFinderP for the color closest to
   that of tuple 'tuple'.  Return its index as *colormapIndexP.

   *pamP describes the tuple 'tuple' and *colorFinderP has to be
   compatible with it (i.e. the tuples in the color map must also be
   described by *pamP).

   We compute distance between colors simply as the cartesian distance
   between them in the RGB space.  An alternative would be to look at
   the chromaticities and luminosities of the colors.  In experiments
   in 2003, we found that this was actually worse in many cases.  One
   might think that two colors are closer if they have similar hues
   than when they are simply different brightnesses of the same hue.
   Human subjects asked to compare two colors normally say so.  But
   when replacing the color of a pixel in an image, the luminosity is
   much more important, because you need to retain the luminosity
   relationship between adjacent pixels.  If you replace a pixel with
   one that has the same chromaticity as the original, but much
   darker, it may stand out among its neighbors in a way the original
   pixel did not.  In fact, on an image with blurred edges, we saw
   ugly effects at the edges when we substituted colors using a
   chromaticity-first color closeness formula.
-----------------------------------------------------------------------------*/
    unsigned int i;
    unsigned int dist;
        /* The closest distance we've found so far between the value of
           tuple 'tuple' and a tuple in the colormap.  This is measured as
           the square of the cartesian distance between the tuples, except
           that it's divided by 'distanceDivider' to make sure it will fit
           in an unsigned int.
        */

    dist = UINT_MAX;  /* initial value */

    assert(colorFinderP->colors > 0);

    for (i = 0; i < colorFinderP->colors; ++i) {
        unsigned int newdist;  /* candidate for new 'dist' value */
        unsigned int plane;

        newdist = 0;

        for (plane=0; plane < pamP->depth; ++plane) {
            newdist += 
                SQR(tuple[plane] - colorFinderP->colormap[i]->tuple[plane]) 
                / colorFinderP->distanceDivider;
        }
        if (newdist < dist) {
            *colormapIndexP = i;
            dist = newdist;
        }
    }
}



static void
searchColormapExact(struct pam *            const pamP,
                    struct colormapFinder * const colorFinderP,
                    tuple                   const tuple,
                    int *                   const colormapIndexP,
                    bool *                  const foundP) {
/*----------------------------------------------------------------------------
   Search the colormap indicated by *colorFinderP for the color of
   tuple 'tuple'.  If it's in the map, return its index as
   *colormapIndexP and return *foundP == TRUE.  Otherwise, return
   *foundP = FALSE.

   *pamP describes the tuple 'tuple' and *colorFinderP has to be
   compatible with it (i.e. the tuples in the color map must also be
   described by *pamP).
-----------------------------------------------------------------------------*/
    unsigned int i;
    bool found;
    
    found = FALSE;  /* initial value */
    for (i = 0; i < colorFinderP->colors && !found; ++i) {
        unsigned int plane;
        found = TRUE;  /* initial assumption */
        for (plane=0; plane < pamP->depth; ++plane) 
            if (tuple[plane] != colorFinderP->colormap[i]->tuple[plane]) 
                found = FALSE;
        if (found) 
            *colormapIndexP = i;
    }
    *foundP = found;
}



static void
lookupThroughHash(struct pam *            const pamP, 
                  tuple                   const tuple, 
                  bool                    const needExactMatch,
                  struct colormapFinder * const colorFinderP,
                  tuplehash               const colorhash,       
                  int *                   const colormapIndexP,
                  bool *                  const usehashP) {
/*----------------------------------------------------------------------------
   Look up the color of tuple 'tuple' in the color map indicated by
   'colorFinderP' and, if it's in there, return its index as
   *colormapIndexP.  If not, return *colormapIndexP == -1.

   Both the tuple 'tuple' and the colors in color map 'colormap' are
   described by *pamP.

   If 'needExactMatch' isn't true, we find the closest color in the color map,
   and never return *colormapIndex == -1.

   lookaside at the hash table 'colorhash' to possibly avoid the cost of
   a full lookup.  If we do a full lookup, we add the result to 'colorhash'
   unless *usehashP is false, and if that makes 'colorhash' full, we set
   *usehashP false.
-----------------------------------------------------------------------------*/
    int found;

    /* Check hash table to see if we have already matched this color. */
    pnm_lookuptuple(pamP, colorhash, tuple, &found, colormapIndexP);
    if (!found) {
        /* No, have to do a full lookup */
        if (needExactMatch) {
            bool found;

            searchColormapExact(pamP, colorFinderP, tuple,
                                colormapIndexP, &found);
            if (!found)
                *colormapIndexP = -1;
        } else 
            searchColormapClose(pamP, tuple, colorFinderP, colormapIndexP);
        if (*usehashP) {
            bool fits;
            pnm_addtotuplehash(pamP, colorhash, tuple, *colormapIndexP, 
                               &fits);
            if (!fits) {
                pm_message("out of memory adding to hash table; "
                           "proceeding without it");
                *usehashP = FALSE;
            }
        }
    }
}



static void
mapTuple(struct pam *            const pamP,
         tuple                   const inTuple,
         tuple                   const defaultColor,
         tupletable              const colormap,
         struct colormapFinder * const colorFinderP,
         tuplehash               const colorhash, 
         bool *                  const usehashP,
         tuple                   const outTuple,
         bool *                  const missingP) {

    int colormapIndex;
        /* Index into the colormap of the replacement color, or -1 if
           there is no usable color in the color map.
        */

    lookupThroughHash(pamP, inTuple, !!defaultColor, colorFinderP, 
                      colorhash, &colormapIndex, usehashP);

    if (colormapIndex == -1) {
        *missingP = TRUE;

        assert(defaultColor);  /* Otherwise, lookup would have succeeded */

        pnm_assigntuple(pamP, outTuple, defaultColor);
    } else {
        *missingP = FALSE;
        pnm_assigntuple(pamP, outTuple, colormap[colormapIndex]->tuple);
    }
}



static void
convertRowStraight(struct pam *            const inpamP,
                   struct pam *            const outpamP, 
                   tuple                         inrow[],
                   depthAdjustment         const depthAdjustment,
                   tupletable              const colormap,
                   struct colormapFinder * const colorFinderP,
                   tuplehash               const colorhash, 
                   bool *                  const usehashP,
                   tuple                   const defaultColor,
                   tuple                         outrow[],
                   unsigned int *          const missingCountP) {
/*----------------------------------------------------------------------------
  Like convertRow(), compute outrow[] from inrow[], replacing each pixel with
  the new colors.  Do a straight pixel for pixel remap; no dithering.

  Return the number of pixels that were not matched in the color map as
  *missingCountP.

  *colorFinderP is a color finder based on 'colormap' -- it tells us what
  index of 'colormap' corresponds to a certain color.
-----------------------------------------------------------------------------*/
    unsigned int col;
    unsigned int missingCount;
    
    /* The following modify tuplerow, to make it consistent with
     *outpamP instead of *inpamP.
     */
    assert(outpamP->allocation_depth >= inpamP->depth);

    pnm_scaletuplerow(inpamP, outrow, inrow, outpamP->maxval);

    adjustDepthRow(outrow, outpamP->width, depthAdjustment);

    missingCount = 0;  /* initial value */
    
    for (col = 0; col < outpamP->width; ++col) {
        bool missing;
        mapTuple(outpamP, outrow[col], defaultColor,
                 colormap, colorFinderP,
                 colorhash, usehashP, outrow[col], &missing);

        if (missing)
            ++missingCount;
    }

    *missingCountP = missingCount;
}



static void
convertRowDither(struct pam *            const inpamP,
                 struct pam *            const outpamP,
                 tuple                   const inrow[],
                 depthAdjustment         const depthAdjustment,
                 tupletable              const colormap,
                 struct colormapFinder * const colorFinderP,
                 tuplehash               const colorhash, 
                 bool *                  const usehashP,
                 tuple                   const defaultColor,
                 struct fserr *          const fserrP,
                 tuple                         outrow[],
                 unsigned int *          const missingCountP) {
/*----------------------------------------------------------------------------
  Like convertRow(), compute outrow[] from inrow[], replacing each pixel with
  the new colors.  Do a Floyd-Steinberg dither, using and updating the error
  accumulator *fserrP.

  Return the number of pixels that were not matched in the color map as
  *missingCountP.

  *colorFinderP is a color finder based on 'colormap' -- it tells us what
  index of 'colormap' corresponds to a certain color.
-----------------------------------------------------------------------------*/
    tuple const ditheredTuple = pnm_allocpamtuple(inpamP);
        /* The input tuple we're converting, adjusted by the dither */
    tuple const normTuple = pnm_allocpamtuple(outpamP);
        /* Same as above, normalized to the maxval of the output file /
           colormap.
        */
    unsigned int missingCount;
    int col;

    floydInitRow(inpamP, fserrP);

    missingCount = 0;  /* initial value */
    
    for (col = fserrP->begCol; col != fserrP->endCol; col += fserrP->step) {
        bool missing;

        floydAdjustColor(inpamP, inrow[col], ditheredTuple, fserrP, col);

        /* Convert tuple to the form of those in the colormap */
        assert(outpamP->allocation_depth >= inpamP->depth);
        pnm_scaletuple(inpamP, normTuple, ditheredTuple, outpamP->maxval);
        adjustDepthTuple(normTuple, depthAdjustment);

        mapTuple(outpamP, normTuple, defaultColor,
                 colormap, colorFinderP,
                 colorhash, usehashP, outrow[col], &missing);

        if (missing)
            ++missingCount;

        /* Convert tuple back to the form of the input, where dithering
           takes place.
        */
        pnm_scaletuple(outpamP, normTuple, outrow[col], inpamP->maxval);
        inverseAdjustDepthTuple(normTuple, depthAdjustment);

        floydPropagateErr(inpamP, fserrP, col, inrow[col], normTuple);
    }

    floydSwitchDir(inpamP, fserrP);

    pnm_freepamtuple(normTuple);
    pnm_freepamtuple(ditheredTuple);

    *missingCountP = missingCount;
}



static void
convertRow(struct pam *            const inpamP,
           struct pam *            const outpamP,
           tuple                         inrow[],
           depthAdjustment               depthAdjustment,
           tupletable              const colormap,
           struct colormapFinder * const colorFinderP,
           tuplehash               const colorhash, 
           bool *                  const usehashP,
           bool                    const floyd, 
           tuple                   const defaultColor,
           struct fserr *          const fserrP,
           tuple                         outrow[],
           unsigned int *          const missingCountP) {
/*----------------------------------------------------------------------------
  Replace the colors in row tuplerow[] (described by *inpamP) with the
  new colors and convert so it is described by *outpamP.

  Use and update the Floyd-Steinberg state *fserrP.

  Return the number of pixels that were not matched in the color map as
  *missingCountP.

  *colorFinderP is a color finder based on 'colormap' -- it tells us what
  index of 'colormap' corresponds to a certain color.

  outrow[] doubles as a work space, so we require it to have an allocation
  depth at least as great as that of inrow[].
-----------------------------------------------------------------------------*/
    /* The following both consults and adds to 'colorhash' and
       its associated '*usehashP'.  It modifies 'tuplerow' too.
    */
    if (floyd)
        convertRowDither(inpamP, outpamP, inrow,
                         depthAdjustment, colormap, colorFinderP, colorhash,
                         usehashP, defaultColor,
                         fserrP, outrow, missingCountP);
    else 
        convertRowStraight(inpamP, outpamP, inrow,
                           depthAdjustment, colormap, colorFinderP, colorhash,
                           usehashP, defaultColor,
                           outrow, missingCountP);
}



static void
copyRaster(struct pam *       const inpamP, 
           struct pam *       const outpamP,
           tupletable         const colormap, 
           unsigned int       const colormapSize,
           bool               const floyd, 
           bool               const randomize,
           tuple              const defaultColor, 
           unsigned int *     const missingCountP) {

    tuplehash const colorhash = pnm_createtuplehash();

    tuple * inrow;
    tuple * outrow;
    struct pam workpam;
        /* This is for work space we use for building up the output
           pixels.  To save time and memory, we modify them in place in a
           buffer, which ultimately holds the output pixels.  This pam
           structure is thus the same as the *outpamP, but with a tuple
           allocation depth large enough to handle intermediate results.
        */
    depthAdjustment depthAdjustment;
    struct colormapFinder * colorFinderP;
    bool usehash;
    struct fserr fserr;
    int row;

    workpam = *outpamP;
    workpam.allocation_depth = MAX(workpam.depth, inpamP->depth);
    workpam.size             = sizeof(workpam);
    workpam.len              = PAM_STRUCT_SIZE(allocation_depth);

    inrow  = pnm_allocpamrow(inpamP);
    outrow = pnm_allocpamrow(&workpam);

    if (outpamP->maxval != inpamP->maxval && defaultColor)
        pm_error("The maxval of the colormap (%u) is not equal to the "
                 "maxval of the input image (%u).  This is allowable only "
                 "if you are doing an approximate mapping (i.e. you don't "
                 "specify -firstisdefault or -missingcolor)",
                 (unsigned int)outpamP->maxval, (unsigned int)inpamP->maxval);

    selectDepthAdjustment(inpamP, outpamP->depth, &depthAdjustment);

    usehash = TRUE;

    createColormapFinder(outpamP, colormap, colormapSize, &colorFinderP);

    if (floyd)
        initFserr(inpamP, &fserr, randomize);

    *missingCountP = 0;  /* initial value */

    for (row = 0; row < inpamP->height; ++row) {
        unsigned int missingCount;

        pnm_readpamrow(inpamP, inrow);

        convertRow(inpamP, &workpam, inrow,
                   depthAdjustment, colormap, colorFinderP, colorhash,
                   &usehash, floyd, defaultColor,
                   &fserr,  outrow, &missingCount);
        
        *missingCountP += missingCount;
        
        pnm_writepamrow(outpamP, outrow);
    }
    destroyColormapFinder(colorFinderP);
    pnm_freepamrow(inrow);
    pnm_freepamrow(outrow);
    pnm_destroytuplehash(colorhash);
}



static void
remap(FILE *             const ifP,
      const struct pam * const outpamCommonP,
      tupletable         const colormap, 
      unsigned int       const colormapSize,
      bool               const floyd,
      bool               const randomize,
      tuple              const defaultColor,
      bool               const verbose) {
/*----------------------------------------------------------------------------
   Remap the pixels from the raster on *ifP to the 'colormapSize' colors in
   'colormap'.

   Where the input pixel's color is in the map, just use that for the output.
   Where it isn't, use 'defaultColor', except if that is NULL, use the
   closest color in the map to the input color.

   But if 'floyd' is true and 'defaultColor' is NULL, also do Floyd-Steinberg
   dithering on the output so the aggregate color of a region is about the
   same as that of the input even though the individual pixels have different
   colors.
-----------------------------------------------------------------------------*/
    bool eof;
    eof = FALSE;
    while (!eof) {
        struct pam inpam, outpam;
        unsigned int missingCount;
            /* Number of pixels that were mapped to 'defaultColor' because
               they weren't present in the color map.
            */

        pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(allocation_depth));
    
        outpam = *outpamCommonP;
        outpam.width  = inpam.width;
        outpam.height = inpam.height;

        pnm_writepaminit(&outpam);

        /* Set up so input buffers have extra space as needed to
           convert the input to the output depth.
        */
        pnm_setminallocationdepth(&inpam, outpam.depth);
    
        copyRaster(&inpam, &outpam, colormap, colormapSize, floyd,
                   randomize, defaultColor, &missingCount);
        
        if (verbose)
            pm_message("%u pixels not matched in color map", missingCount);
        
        pnm_nextimage(ifP, &eof);
    }
}



static void
processMapFile(const char *   const mapFileName,
               struct pam *   const outpamCommonP,
               tupletable *   const colormapP,
               unsigned int * const colormapSizeP,
               tuple *        const firstColorP) {

    FILE * mapfile;
    struct pam mappam;
    tuple ** maptuples;
    tuple firstColor;

    mapfile = pm_openr(mapFileName);
    maptuples = pnm_readpam(mapfile, &mappam, PAM_STRUCT_SIZE(tuple_type));
    pm_close(mapfile);

    computeColorMapFromMap(&mappam, maptuples, colormapP, colormapSizeP);

    firstColor = pnm_allocpamtuple(&mappam);
    pnm_assigntuple(&mappam, firstColor, maptuples[0][0]);
    *firstColorP = firstColor;

    pnm_freepamarray(maptuples, &mappam);

    *outpamCommonP = mappam; 
    outpamCommonP->file = stdout;
}



static void
getSpecifiedMissingColor(struct pam * const pamP,
                         const char * const colorName,
                         tuple *      const specColorP) {

    tuple specColor;
                             
    specColor = pnm_allocpamtuple(pamP);

    if (colorName) {
        pixel const color = ppm_parsecolor(colorName, pamP->maxval);
        if (pamP->depth == 3) {
            specColor[PAM_RED_PLANE] = PPM_GETR(color);
            specColor[PAM_GRN_PLANE] = PPM_GETG(color);
            specColor[PAM_BLU_PLANE] = PPM_GETB(color);
        } else if (pamP->depth == 1) {
            specColor[0] = ppm_luminosity(color);
        } else {
            pm_error("You may not use -missing with a colormap that is not "
                     "of depth 1 or 3.  Yours has depth %u",
                     pamP->depth);
        }
    }
    *specColorP = specColor;
}



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

    struct cmdlineInfo cmdline;
    FILE * ifP;
    struct pam outpamCommon;
        /* Describes the output images.  Width and height fields are
           not meaningful, because different output images might have
           different dimensions.  The rest of the information is common
           across all output images.
        */
    tupletable colormap;
    unsigned int colormapSize;
    tuple specColor;
        /* A tuple of the color the user specified to use for input colors
           that are not in the colormap.  Arbitrary tuple if he didn't
           specify any.
        */
    tuple firstColor;
        /* A tuple of the first color present in the map file */
    tuple defaultColor;
        /* The color to which we will map an input color that is not in the
           colormap.  NULL if we are not to map such a color to a particular
           color (i.e. we'll choose an approximate match from the map).
        */

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    processMapFile(cmdline.mapFilespec, &outpamCommon,
                   &colormap, &colormapSize, &firstColor);

    getSpecifiedMissingColor(&outpamCommon, cmdline.missingcolor, &specColor);

    switch (cmdline.missingMethod) {
    case MISSING_CLOSE:
        defaultColor = NULL;
        break;
    case MISSING_FIRST:
        defaultColor = firstColor;
        break;
    case MISSING_SPECIFIED:
        defaultColor = specColor;
        break;
    }

    remap(ifP, &outpamCommon, colormap, colormapSize, 
          cmdline.floyd, !cmdline.norandom, defaultColor,
          cmdline.verbose);

    pnm_freepamtuple(firstColor);
    pnm_freepamtuple(specColor);

    pm_close(stdout);

    pm_close(ifP);

    return 0;
}