about summary refs log tree commit diff
path: root/other/pnmcolormap.c
blob: f59a1a0ddd81c601653058ddb45f0b1dc7b4ca5c (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
/*=============================================================================
                               pnmcolormap
===============================================================================
  Create a colormap file (a PPM image containing one pixel of each of a set
  of colors).  Base the set of colors on an input image.

  For PGM input, do the equivalent for grayscale and produce a PGM graymap.

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

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

enum MethodForLargest {LARGE_NORM, LARGE_LUM};

enum MethodForRep {REP_CENTER_BOX, REP_AVERAGE_COLORS, REP_AVERAGE_PIXELS};

enum MethodForSplit {SPLIT_MAX_PIXELS, SPLIT_MAX_COLORS, SPLIT_MAX_SPREAD};

struct Box {
/*----------------------------------------------------------------------------
   A box contains an extent of a color frequency table, i.e. the colors
   with some consecutive index values in the color frequency table.
-----------------------------------------------------------------------------*/
    unsigned int serialNum;
        /* Unique identifier of this box; sequence number of creation. */
    unsigned int startIndex;
        /* First index in the extent */
    unsigned int colorCt;
        /* Size of the extent (Number of colors in it -- at least 1) */
    unsigned int sum;
        /* Number of pixels of all colors in the extent */
    unsigned int maxdim;
        /* Which dimension has the largest spread.  RGB plane number. */
        /* Meaningless if box contains only 1 color */
    sample       spread;
        /* spread in dimension 'maxdim' */
        /* Meaningless if box contains only 1 color */
};

struct BoxVector {
    tupletable2 colorFreqTable;
        /* The colors and their frequencies (number of pixels in the image of
           that color), ordered into consecutive boxes, as defined by 'box'.
        */
    unsigned int colorDepth;
        /* Number of planes in the tuples of 'colorFreqTable' */
    struct Box * box;  /* malloc'ed array */
        /* An array of boxes that contain consecutive extents of
           'colorFreqTable'.  The list covers the entire table.
        */
    unsigned int boxCt;
        /* Number of boxes in the above list */
    unsigned int capacity;
        /* Number of boxes the array is capable of containing */
};

struct CmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFileNm;  /* Name of input file */
    unsigned int allcolors;  /* boolean: select all colors from the input */
    unsigned int newColorCt;
        /* Number of colors argument; meaningless if allcolors true */
    enum MethodForLargest methodForLargest;
        /* -spreadintensity/-spreadluminosity options */
    enum MethodForRep methodForRep;
        /* -center/-meancolor/-meanpixel options */
    enum MethodForSplit methodForSplit;
        /* -splitpixelct/-splitcolorct/-splitspread options */
    unsigned int sort;
    unsigned int square;
    unsigned int verbose;
    unsigned int debug;
};



static void
parseCommandLine (int argc, const char ** argv,
                  struct CmdlineInfo * const 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;
    optStruct3 opt;

    unsigned int option_def_index;

    unsigned int spreadbrightness, spreadluminosity;
    unsigned int center, meancolor, meanpixel;
    unsigned int splitpixelct, splitcolorct, splitspread;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0,   "spreadbrightness", OPT_FLAG,
            NULL,                       &spreadbrightness, 0);
    OPTENT3(0,   "spreadluminosity", OPT_FLAG,
            NULL,                       &spreadluminosity, 0);
    OPTENT3(0,   "center",           OPT_FLAG,
            NULL,                       &center,           0);
    OPTENT3(0,   "meancolor",        OPT_FLAG,
            NULL,                       &meancolor,        0);
    OPTENT3(0,   "meanpixel",        OPT_FLAG,
            NULL,                       &meanpixel,        0);
    OPTENT3(0,   "splitpixelct",     OPT_FLAG,
            NULL,                       &splitpixelct,     0);
    OPTENT3(0,   "splitcolorct",     OPT_FLAG,
            NULL,                       &splitcolorct,     0);
    OPTENT3(0,   "splitspread",      OPT_FLAG,
            NULL,                       &splitspread,      0);
    OPTENT3(0, "sort",               OPT_FLAG,   NULL,
            &cmdlineP->sort,                               0);
    OPTENT3(0, "square",             OPT_FLAG,   NULL,
            &cmdlineP->square,                             0);
    OPTENT3(0, "verbose",            OPT_FLAG,   NULL,
            &cmdlineP->verbose,                            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_optParseOptions4( &argc, argv, opt, sizeof(opt), 0 );
        /* Uses and sets argc, argv, and some of *cmdline_p and others. */


    if (spreadbrightness && spreadluminosity)
        pm_error("You cannot specify both -spreadbrightness and "
                 "spreadluminosity.");
    if (spreadluminosity)
        cmdlineP->methodForLargest = LARGE_LUM;
    else
        cmdlineP->methodForLargest = LARGE_NORM;

    if (center + meancolor + meanpixel > 1)
        pm_error("You can specify only one of -center, -meancolor, and "
                 "-meanpixel.");
    if (meancolor)
        cmdlineP->methodForRep = REP_AVERAGE_COLORS;
    else if (meanpixel)
        cmdlineP->methodForRep = REP_AVERAGE_PIXELS;
    else
        cmdlineP->methodForRep = REP_CENTER_BOX;

    if (splitpixelct)
        cmdlineP->methodForSplit = SPLIT_MAX_PIXELS;
    else if (splitcolorct)
        cmdlineP->methodForSplit = SPLIT_MAX_COLORS;
    else if (splitspread)
        cmdlineP->methodForSplit = SPLIT_MAX_SPREAD;
    else
        cmdlineP->methodForSplit = SPLIT_MAX_PIXELS;

    if (argc-1 > 2)
        pm_error("Program takes at most two arguments: number of colors "
                 "and input file specification.  "
                 "You specified %d arguments.", argc-1);
    else {
        if (argc-1 < 2)
            cmdlineP->inputFileNm = "-";
        else
            cmdlineP->inputFileNm = argv[2];

        if (argc-1 < 1)
            pm_error("You must specify the number of colors in the "
                     "output as an argument.");
        else {
            if (strcmp(argv[1], "all") == 0)
                cmdlineP->allcolors = true;
            else {
                const char * error;
                cmdlineP->allcolors = false;
                pm_string_to_uint(argv[1], &cmdlineP->newColorCt, &error);

                if (error) {
                    pm_error("The number of colors argument '%s' is not "
                             "an unsigned number or 'all'.  %s",
                             argv[1], error);
                } else if (cmdlineP->newColorCt == 0)
                    pm_error("The number of colors must be positive");
            }
        }
    }
}



#ifndef LITERAL_FN_DEF_MATCH
static qsort_comparison_fn compareColor;
#endif

static struct {
/*----------------------------------------------------------------------------
  This is a parameter to compareColor().  We use this global variable
  so that compareColor() can be called by qsort(), to compare two
  tuples.  qsort() doesn't pass any arguments except the two tuples.
-----------------------------------------------------------------------------*/
    unsigned int comparePlane;
        /* The number of the plane to compare between the tuples */
    unsigned int colorDepth;
        /* Depth (number of planes) of the tuples */
} compareColorParm;

static int
compareColor(const void * const arg1,
             const void * const arg2) {

    const struct tupleint * const * const comparandPP  = arg1;
    const struct tupleint * const * const comparatorPP = arg2;

    sample const comparandSample  =
        (*comparandPP) ->tuple[compareColorParm.comparePlane];
    sample const comparatorSample =
        (*comparatorPP)->tuple[compareColorParm.comparePlane];

    int retval;

    if (comparandSample < comparatorSample)
        retval = -1;
    else if (comparandSample > comparatorSample)
        retval = +1;
    else {
        /* In the plane that matters, samples are equal, but we're going to
           try to differentiate the colors anyway so as to make qsort put the
           colors in a deterministic order so the boxes are deterministic.
        */
        unsigned int plane;
        int bestDiffSoFar;  /* -1, 0, or 1, like our return value */
        for (plane = 0, bestDiffSoFar = 0;
             plane < compareColorParm.colorDepth && bestDiffSoFar == 0;
             ++plane) {

            sample const comparandSample  =
                (*comparandPP) ->tuple[compareColorParm.comparePlane];
            sample const comparatorSample =
                (*comparatorPP)->tuple[compareColorParm.comparePlane];

            if (comparandSample < comparatorSample)
                bestDiffSoFar = -1;
            else if (comparandSample > comparatorSample)
                bestDiffSoFar = +1;
        }
        retval = bestDiffSoFar;
    }
    return retval;
}



#ifndef LITERAL_FN_DEF_MATCH
static qsort_comparison_fn sumcompare;
#endif

static int
sumcompare(const void * const arg1,
           const void * const arg2) {

    struct Box * const comparandP  = (struct Box *)arg1;
    struct Box * const comparatorP = (struct Box *)arg2;

    return
        comparatorP->sum < comparandP->sum ? -1 :
        comparatorP->sum > comparandP->sum ? +1 :
        comparatorP->serialNum < comparandP->serialNum ? -1 :
        comparatorP->serialNum > comparandP->serialNum ? +1 :
        0;
}



#ifndef LITERAL_FN_DEF_MATCH
static qsort_comparison_fn colcompare;
#endif

static int
colcompare(const void * const arg1,
           const void * const arg2) {

    struct Box * const comparandP  = (struct Box *)arg1;
    struct Box * const comparatorP = (struct Box *)arg2;

    return
        comparatorP->colorCt < comparandP->colorCt ? -1 :
        comparatorP->colorCt > comparandP->colorCt ? 1 :
        comparatorP->serialNum < comparandP->serialNum ? -1 :
        comparatorP->serialNum > comparandP->serialNum ? +1 :
        0;
}



#ifndef LITERAL_FN_DEF_MATCH
static qsort_comparison_fn spreadcompare;
#endif

static int
spreadcompare(const void * const arg1,
              const void * const arg2) {

    struct Box * const comparandP  = (struct Box *)arg1;
    struct Box * const comparatorP = (struct Box *)arg2;

    return
        comparatorP->spread < comparandP->spread ? -1 :
        comparatorP->spread > comparandP->spread ? 1 :
        comparatorP->serialNum < comparandP->serialNum ? -1 :
        comparatorP->serialNum > comparandP->serialNum ? +1 :
        0;
}



static void
sortBoxes(struct BoxVector *  const boxVectorP,
          enum MethodForSplit const methodForSplit) {

    qsort_comparison_fn * comparisonFn;

    switch (methodForSplit){
    case SPLIT_MAX_PIXELS: comparisonFn = &sumcompare;    break;
    case SPLIT_MAX_COLORS: comparisonFn = &colcompare;    break;
    case SPLIT_MAX_SPREAD: comparisonFn = &spreadcompare; break;
    }

    qsort((char*) &boxVectorP->box[0], boxVectorP->boxCt, sizeof(struct Box),
          comparisonFn);
}



/*
** Here is the fun part, the median-cut colormap generator.  This is based
** on Paul Heckbert's paper "Color Image Quantization for Frame Buffer
** Display", SIGGRAPH '82 Proceedings, page 297.
*/

static void
findBoxBoundaries(tupletable2  const colorFreqTable,
                  unsigned int const depth,
                  unsigned int const boxStart,
                  unsigned int const boxSize,
                  sample             minval[],
                  sample             maxval[]) {
/*----------------------------------------------------------------------------
  Go through the box finding the minimum and maximum of each component - the
  boundaries of the box.
-----------------------------------------------------------------------------*/
    unsigned int plane;
    unsigned int i;

    for (plane = 0; plane < depth; ++plane) {
        minval[plane] = colorFreqTable.table[boxStart]->tuple[plane];
        maxval[plane] = minval[plane];
    }

    for (i = 1; i < boxSize; ++i) {
        unsigned int plane;
        for (plane = 0; plane < depth; ++plane) {
            sample const v = colorFreqTable.table[boxStart + i]->tuple[plane];
            if (v < minval[plane]) minval[plane] = v;
            if (v > maxval[plane]) maxval[plane] = v;
        }
    }
}



static void
findPlaneWithLargestSpreadByNorm(sample         const minval[],
                                 sample         const maxval[],
                                 unsigned int   const depth,
                                 unsigned int * const planeP,
                                 sample *       const spreadP) {

    unsigned int planeWithLargest;
    sample       largestSpreadSoFar;
    unsigned int plane;

    for (plane = 0, largestSpreadSoFar = 0; plane < depth; ++plane) {

        sample const spread = maxval[plane]-minval[plane];
        if (spread > largestSpreadSoFar) {
            largestSpreadSoFar = spread;
            planeWithLargest   = plane;
        }
    }
    *planeP  = planeWithLargest;
    *spreadP = largestSpreadSoFar;
}



static void
findPlaneWithLargestSpreadByLuminosity(sample         const minval[],
                                       sample         const maxval[],
                                       unsigned int   const depth,
                                       unsigned int * const planeP,
                                       sample *       const spreadP) {
/*----------------------------------------------------------------------------
   This subroutine presumes that the tuple type is either
   BLACKANDWHITE, GRAYSCALE, or RGB (which implies pamP->depth is 1 or 3).
   To save time, we don't actually check it.
-----------------------------------------------------------------------------*/
    if (depth == 1){
        *planeP  = 0;
        *spreadP = 0;
    } else {
        /* An RGB tuple */
        unsigned int planeWithLargest;
        sample       largestSpreadSoFar;
        unsigned int plane;

        assert(depth >= 3);

        for (plane = 0, largestSpreadSoFar = 0; plane < 3; ++plane) {
            double const spread =
                pnm_lumin_factor[plane] * (maxval[plane]-minval[plane]);
            if (spread > largestSpreadSoFar) {
                largestSpreadSoFar = spread;
                planeWithLargest   = plane;
            }
        }
        *planeP  = planeWithLargest;
        *spreadP = largestSpreadSoFar;
    }
}



static void
computeBoxSpread(const struct Box *    const boxP,
                 tupletable2           const colorFreqTable,
                 unsigned int          const depth,
                 enum MethodForLargest const methodForLargest,
                 unsigned int *        const planeWithLargestP,
                 sample *              const spreadP
                 ) {
/*----------------------------------------------------------------------------
  Find the spread in the dimension in which it is greatest.

  Return as *planeWithLargestP the number of that plane and as *spreadP the
  spread in that plane.
-----------------------------------------------------------------------------*/
    sample * minval;  /* malloc'ed array */
    sample * maxval;  /* malloc'ed array */

    MALLOCARRAY_NOFAIL(minval, depth);
    MALLOCARRAY_NOFAIL(maxval, depth);

    findBoxBoundaries(colorFreqTable, depth, boxP->startIndex, boxP->colorCt,
                      minval, maxval);

    switch (methodForLargest) {
    case LARGE_NORM:
        findPlaneWithLargestSpreadByNorm(minval, maxval, depth,
                                         planeWithLargestP, spreadP);
        break;
    case LARGE_LUM:
        findPlaneWithLargestSpreadByLuminosity(minval, maxval, depth,
                                               planeWithLargestP, spreadP);
        break;
    }
    free(minval); free(maxval);
}



static unsigned int
freqTotal(tupletable2 const freqTable) {

    unsigned int i;
    unsigned int sum;

    for (i = 0, sum = 0; i < freqTable.size; ++i)
        sum += freqTable.table[i]->value;

    return sum;
}



static struct BoxVector
newBoxVector(tupletable2           const colorFreqTable,
             unsigned int          const capacity,
             unsigned int          const depth,
             enum MethodForLargest const methodForLargest) {

    unsigned int const colorCt = colorFreqTable.size;
    unsigned int const sum     = freqTotal(colorFreqTable);

    struct BoxVector boxVector;

    boxVector.colorFreqTable = colorFreqTable;
    boxVector.colorDepth     = depth;

    MALLOCARRAY(boxVector.box, capacity);

    if (!boxVector.box)
        pm_error("out of memory allocating box vector table");

    /* Set up the initial box. */
    boxVector.box[0].startIndex = 0;
    boxVector.box[0].colorCt    = colorCt;
    boxVector.box[0].sum        = sum;

    computeBoxSpread(&boxVector.box[0], colorFreqTable, depth,
                     methodForLargest,
                     &boxVector.box[0].maxdim,
                     &boxVector.box[0].spread);

    boxVector.boxCt    = 1;
    boxVector.capacity = capacity;

    return boxVector;
}



static void
destroyBoxVector(struct BoxVector const boxVector) {

    free(boxVector.box);
}



static void
centerBox(int          const boxStart,
          int          const boxSize,
          tupletable2  const colorFreqTable,
          unsigned int const depth,
          tuple        const newTuple) {

    unsigned int plane;

    for (plane = 0; plane < depth; ++plane) {
        int minval, maxval;
        unsigned int i;

        minval = maxval = colorFreqTable.table[boxStart]->tuple[plane];

        for (i = 1; i < boxSize; ++i) {
            int const v = colorFreqTable.table[boxStart + i]->tuple[plane];
            minval = MIN( minval, v);
            maxval = MAX( maxval, v);
        }
        newTuple[plane] = (minval + maxval) / 2;
    }
}



static tupletable2
newColorMap(unsigned int const colorCt,
            unsigned int const depth) {

    tupletable2 colormap;
    unsigned int i;
    struct pam pam;

    pam.depth = depth;

    colormap.table = pnm_alloctupletable(&pam, colorCt);

    for (i = 0; i < colorCt; ++i) {
        unsigned int plane;
        for (plane = 0; plane < depth; ++plane)
            colormap.table[i]->tuple[plane] = 0;
    }
    colormap.size = colorCt;

    return colormap;
}



static void
averageColors(int          const boxStart,
              int          const boxSize,
              tupletable2  const colorFreqTable,
              unsigned int const depth,
              tuple        const newTuple) {

    unsigned int plane;

    for (plane = 0; plane < depth; ++plane) {
        sample sum;
        int i;

        sum = 0;

        for (i = 0; i < boxSize; ++i)
            sum += colorFreqTable.table[boxStart+i]->tuple[plane];

        newTuple[plane] = ROUNDDIV(sum, boxSize);
    }
}



static void
averagePixels(int          const boxStart,
              int          const boxSize,
              tupletable2  const colorFreqTable,
              unsigned int const depth,
              tuple        const newTuple) {

    unsigned int n;
        /* Number of tuples represented by the box */
    unsigned int plane;
    unsigned int i;

    /* Count the tuples in question */
    n = 0;  /* initial value */
    for (i = 0; i < boxSize; ++i)
        n += colorFreqTable.table[boxStart + i]->value;


    for (plane = 0; plane < depth; ++plane) {
        sample sum;
        int i;

        sum = 0;

        for (i = 0; i < boxSize; ++i)
            sum += colorFreqTable.table[boxStart+i]->tuple[plane]
                * colorFreqTable.table[boxStart+i]->value;

        newTuple[plane] = ROUNDDIV(sum, n);
    }
}



static tupletable2
colormapFromBv(unsigned int      const colorCt,
               struct BoxVector  const boxVector,
               enum MethodForRep const methodForRep) {
    /*
    ** Ok, we've got enough boxes.  Now choose a representative color for
    ** each box.  There are a number of possible ways to make this choice.
    ** One would be to choose the center of the box; this ignores any structure
    ** within the boxes.  Another method would be to average all the colors in
    ** the box - this is the method specified in Heckbert's paper.  A third
    ** method is to average all the pixels in the box.
    */
    tupletable2 colormap;
    unsigned int boxIdx;

    colormap = newColorMap(colorCt, boxVector.colorDepth);

    for (boxIdx = 0; boxIdx < boxVector.boxCt; ++boxIdx) {
        switch (methodForRep) {
        case REP_CENTER_BOX:
            centerBox(boxVector.box[boxIdx].startIndex,
                      boxVector.box[boxIdx].colorCt,
                      boxVector.colorFreqTable, boxVector.colorDepth,
                      colormap.table[boxIdx]->tuple);
            break;
        case REP_AVERAGE_COLORS:
            averageColors(boxVector.box[boxIdx].startIndex,
                          boxVector.box[boxIdx].colorCt,
                          boxVector.colorFreqTable, boxVector.colorDepth,
                          colormap.table[boxIdx]->tuple);
            break;
        case REP_AVERAGE_PIXELS:
            averagePixels(boxVector.box[boxIdx].startIndex,
                          boxVector.box[boxIdx].colorCt,
                          boxVector.colorFreqTable, boxVector.colorDepth,
                          colormap.table[boxIdx]->tuple);
            break;
        default:
            pm_error("Internal error: invalid value of methodForRep: %d",
                     methodForRep);
        }
    }
    return colormap;
}



static void
setBox(struct Box *          const boxP,
       unsigned int          const startIndex,
       unsigned int          const colorCt,
       unsigned int          const sum,
       struct BoxVector *    const boxVectorP,
       enum MethodForLargest const methodForLargest
    ) {

    boxP->startIndex = startIndex;
    boxP->colorCt    = colorCt;
    boxP->sum        = sum;

    computeBoxSpread(boxP, boxVectorP->colorFreqTable,
                     boxVectorP->colorDepth, methodForLargest,
                     &boxP->maxdim, &boxP->spread);
}



static void
makeNewBox(struct BoxVector *    const boxVectorP,
           unsigned int          const startIndex,
           unsigned int          const colorCt,
           unsigned int          const sum,
           enum MethodForLargest const methodForLargest) {

    struct Box * const boxP = &boxVectorP->box[boxVectorP->boxCt++];

    assert(boxVectorP->boxCt <= boxVectorP->capacity);

    boxP->serialNum = boxVectorP->boxCt;

    setBox(boxP, startIndex, colorCt, sum, boxVectorP, methodForLargest);
}



static void
splitBox(struct BoxVector *    const boxVectorP,
         unsigned int          const boxIdx,
         enum MethodForLargest const methodForLargest,
         enum MethodForSplit   const methodForSplit) {
/*----------------------------------------------------------------------------
   Split Box 'boxIdx' in the box vector 'boxVector' (so that 'boxVector'
   contains one more box than it did as input).  Split it so that each new box
   represents about half of the pixels in the image for the colors in the
   original box, but with distinct colors in each of the two new boxes.

   Assume the box contains at least two colors.
-----------------------------------------------------------------------------*/
    unsigned int const boxStart = boxVectorP->box[boxIdx].startIndex;
    unsigned int const boxSize  = boxVectorP->box[boxIdx].colorCt;
    unsigned int const sum      = boxVectorP->box[boxIdx].sum;

    unsigned int medianIndex;
    unsigned int lowerSum;
        /* Number of pixels whose value is "less than" the median */

    /* Set the gross global variable 'compareColorParm' as a
       parameter to compareColor(), which is called by qsort().
    */
    compareColorParm.comparePlane    = boxVectorP->box[boxIdx].maxdim;
    compareColorParm.colorDepth      = boxVectorP->colorDepth;
    qsort((char*) &boxVectorP->colorFreqTable.table[boxStart], boxSize,
          sizeof(boxVectorP->colorFreqTable.table[boxStart]),
          compareColor);

    {
        /* Find the median based on the counts, so that about half the pixels
           (not colors, pixels) are in each subdivision.
        */
        unsigned int i;

        lowerSum = boxVectorP->colorFreqTable.table[boxStart]->value;
            /* initial value */
        for (i = 1; i < boxSize - 1 && lowerSum < sum/2; ++i) {
            lowerSum += boxVectorP->colorFreqTable.table[boxStart + i]->value;
        }
        medianIndex = i;
    }
    /* Split the box, and sort to bring the biggest boxes to the top.  The old
       box becomes the lower half; we make a new box for the upper half.
    */
    setBox(&boxVectorP->box[boxIdx],
           boxStart, medianIndex,
           lowerSum,
           boxVectorP, methodForLargest);

    makeNewBox(boxVectorP,
               boxStart + medianIndex, boxSize - medianIndex,
               sum - lowerSum,
               methodForLargest);

    sortBoxes(boxVectorP, methodForSplit);
}



static void
reportBoxVector(struct BoxVector const boxVector) {

    unsigned int i;

    pm_message("All colors of image, sorted into %u boxes:", boxVector.boxCt);

    for (i = 0; i < boxVector.boxCt; ++i) {
        const struct Box * const boxP = &boxVector.box[i];

        unsigned int j;

        pm_message("Box %u, %u colors starting with index %u (%u pixels):",
                   i, boxP->colorCt, boxP->startIndex, boxP->sum);
        if (boxP->colorCt > 1)
            pm_message("Largest spread is %lu, in plane %u",
                       boxP->spread, boxP->maxdim);

        for (j = 0; j < boxP->colorCt; ++j) {
            unsigned int colorIdx = boxP->startIndex + j;

            assert(colorIdx < boxVector.colorFreqTable.size);

            tuple const color =
                boxVector.colorFreqTable.table[colorIdx]->tuple;

            pm_message("(%lu, %lu, %lu)",
                       color[PAM_RED_PLANE],
                       color[PAM_GRN_PLANE],
                       color[PAM_BLU_PLANE]);
        }
    }
}



static void
mediancut(tupletable2           const colorFreqTable,
          unsigned int          const depth,
          unsigned int          const newColorCt,
          enum MethodForLargest const methodForLargest,
          enum MethodForRep     const methodForRep,
          enum MethodForSplit   const methodForSplit,
          bool                  const wantBvReport,
          tupletable2 *         const colormapP) {
/*----------------------------------------------------------------------------
   Compute a set of only 'newColorCt' colors that best represent an
   image whose pixels are summarized by the histogram
   'colorFreqTable'.  Each tuple in that table has depth 'depth'.
   colorFreqTable.table[i] tells the number of pixels in the subject image
   that have a particular color.

   As a side effect, sort 'colorFreqTable'.
-----------------------------------------------------------------------------*/
    struct BoxVector boxVector;
    bool multicolorBoxesExist;
        /* There is at least one box that contains at least 2 colors; ergo,
           there is more splitting we can do.
        */

    boxVector = newBoxVector(colorFreqTable, newColorCt, depth,
                             methodForLargest);

    multicolorBoxesExist = (colorFreqTable.size > 1);

    /* Split boxes until we have enough. */
    while (boxVector.boxCt < newColorCt && multicolorBoxesExist) {
        unsigned int boxIdx;

        for (boxIdx = 0;
             boxIdx < boxVector.boxCt && boxVector.box[boxIdx].colorCt < 2;
             ++boxIdx);
            /* Find the first splittable box. */

        if (boxIdx >= boxVector.boxCt)
            multicolorBoxesExist = false;
        else
            splitBox(&boxVector, boxIdx, methodForLargest, methodForSplit);
                /* Side effect: sorts the extent of 'colorfreqTable' that is
                   in the box
                */
    }

    if (wantBvReport)
        reportBoxVector(boxVector);

    *colormapP = colormapFromBv(newColorCt, boxVector, methodForRep);

    destroyBoxVector(boxVector);
}




static void
validateCompatibleImage(struct pam * const inpamP,
                        struct pam * const firstPamP,
                        unsigned int const imageSeq) {

    if (inpamP->depth != firstPamP->depth)
        pm_error("Image %u depth (%u) is not the same as Image 0 (%u)",
                 imageSeq, inpamP->depth, firstPamP->depth);
    if (inpamP->maxval != firstPamP->maxval)
        pm_error("Image %u maxval (%u) is not the same as Image 0 (%u)",
                 imageSeq,
                 (unsigned)inpamP->maxval, (unsigned)firstPamP->maxval);
    if (inpamP->format != firstPamP->format)
        pm_error("Image %u format (%d) is not the same as Image 0 (%d)",
                 imageSeq, inpamP->format, firstPamP->format);
    if (!streq(inpamP->tuple_type, firstPamP->tuple_type))
        pm_error("Image %u tuple type (%s) is not the same as Image 0 (%s)",
                 imageSeq, inpamP->tuple_type, firstPamP->tuple_type);
}



static void
addImageColorsToHash(struct pam *   const pamP,
                     tuplehash      const tuplehash,
                     unsigned int * const colorCountP) {

    tuple * tuplerow;
    unsigned int row;

    tuplerow = pnm_allocpamrow(pamP);

    for (row = 0; row < pamP->height; ++row) {
        unsigned int col;

        pnm_readpamrow(pamP, tuplerow);

        for (col = 0; col < pamP->width; ++col) {
            int firstOccurrence;

            pnm_addtuplefreqoccurrence(pamP, tuplerow[col], tuplehash,
                                       &firstOccurrence);

            if (firstOccurrence)
                ++(*colorCountP);
        }
    }
    pnm_freepamrow(tuplerow);
}



static void
computeHistogram(FILE *         const ifP,
                 int *          const formatP,
                 struct pam *   const freqPamP,
                 tupletable2 *  const colorFreqTableP) {
/*----------------------------------------------------------------------------
  Make a histogram of the colors in the image stream in the file '*ifP'.

  Return as *freqPamP a description of the tuple values in the histogram.
  Only the fields of *freqPamP that describe individual tuples are
  meaningful (depth, maxval, tuple type);

  As a fringe benefit, also return the format of the input file as
  *formatP.
----------------------------------------------------------------------------*/
    unsigned int imageSeq;
    struct pam firstPam;
    tuplehash tuplehash;
    unsigned int colorCount;
    int eof;

    pm_message("making histogram...");

    tuplehash = pnm_createtuplehash();
    colorCount = 0;

    eof = false;

    for (imageSeq = 0; !eof; ++imageSeq) {
        struct pam inpam;

        pm_message("Scanning image %u", imageSeq);

        pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

        if (imageSeq == 0)
            firstPam = inpam;
        else
            validateCompatibleImage(&inpam, &firstPam, imageSeq);

        addImageColorsToHash(&inpam, tuplehash, &colorCount);

        pm_message("%u colors so far", colorCount);

        pnm_nextimage(ifP, &eof);
    }
    colorFreqTableP->table =
        pnm_tuplehashtotable(&firstPam, tuplehash, colorCount);
    colorFreqTableP->size = colorCount;

    pnm_destroytuplehash(tuplehash);

    pm_message("%u colors found", colorFreqTableP->size);

    freqPamP->size   = sizeof(*freqPamP);
    freqPamP->len    = PAM_STRUCT_SIZE(tuple_type);
    freqPamP->maxval = firstPam.maxval;
    freqPamP->bytes_per_sample = pnm_bytespersample(freqPamP->maxval);
    freqPamP->depth  = firstPam.depth;
    STRSCPY(freqPamP->tuple_type, firstPam.tuple_type);

    *formatP = firstPam.format;
}



static void
computeColorMapFromInput(FILE *                const ifP,
                         bool                  const allColors,
                         unsigned int          const reqColorCt,
                         enum MethodForLargest const methodForLargest,
                         enum MethodForRep     const methodForRep,
                         enum MethodForSplit   const methodForSplit,
                         bool                  const wantBvReport,
                         int *                 const formatP,
                         struct pam *          const freqPamP,
                         tupletable2 *         const colormapP) {
/*----------------------------------------------------------------------------
   Produce a colormap containing the best colors to represent the
   image stream in file 'ifP'.  Figure it out using the median cut
   technique.

   The colormap will have 'reqcolorCt' or fewer colors in it, unless
   'allcolors' is true, in which case it will have all the colors that
   are in the input.

   The colormap has the same maxval as the input.

   Put the colormap in newly allocated storage as a tupletable2
   and return its address as *colormapP.  Return the number of colors in
   it as *colorsP and its maxval as *colormapMaxvalP.

   Return the characteristics of the input file as
   *formatP and *freqPamP.  (This information is not really
   relevant to our colormap mission; just a fringe benefit).
-----------------------------------------------------------------------------*/
    tupletable2 colorFreqTable;
        /* Table of all colors in the image, with the number of pixels of
           each color.
        */

    computeHistogram(ifP, formatP, freqPamP, &colorFreqTable);

    if (allColors) {
        *colormapP = colorFreqTable;
    } else {
        if (colorFreqTable.size <= reqColorCt) {
            pm_message("Image already has few enough colors (<=%u).  "
                       "Keeping same colors.", reqColorCt);
            *colormapP = colorFreqTable;
        } else {
            pm_message("choosing %u colors...", reqColorCt);
            mediancut(colorFreqTable, freqPamP->depth,
                      reqColorCt, methodForLargest, methodForRep,
                      methodForSplit, wantBvReport, colormapP);
            pnm_freetupletable2(freqPamP, colorFreqTable);
        }
    }
}



static void
sortColormap(tupletable2  const colormap,
             sample       const depth) {
/*----------------------------------------------------------------------------
   Sort the colormap in place, in order of ascending Plane 0 value,
   the Plane 1 value, etc.

   Use insertion sort.
-----------------------------------------------------------------------------*/
    int i;

    pm_message("Sorting %u colors...", colormap.size);

    for (i = 0; i < colormap.size; ++i) {
        int j;
        for (j = i+1; j < colormap.size; ++j) {
            unsigned int plane;
            bool iIsGreater, iIsLess;

            iIsGreater = false; iIsLess = false;
            for (plane = 0;
                 plane < depth && !iIsGreater && !iIsLess;
                 ++plane) {
                if (colormap.table[i]->tuple[plane] >
                    colormap.table[j]->tuple[plane])
                    iIsGreater = true;
                else if (colormap.table[i]->tuple[plane] <
                         colormap.table[j]->tuple[plane])
                    iIsLess = true;
            }
            if (iIsGreater) {
                for (plane = 0; plane < depth; ++plane) {
                    sample const temp = colormap.table[i]->tuple[plane];
                    colormap.table[i]->tuple[plane] =
                        colormap.table[j]->tuple[plane];
                    colormap.table[j]->tuple[plane] = temp;
                }
            }
        }
    }
}



static void
colormapToSquare(struct pam * const pamP,
                 tupletable2  const colormap,
                 tuple ***    const outputRasterP) {
    {
        unsigned int const intsqrt = (int)sqrt((float)colormap.size);
        if (SQR(intsqrt) == colormap.size)
            pamP->width = intsqrt;
        else
            pamP->width = intsqrt + 1;
    }
    {
        unsigned int const intQuotient = colormap.size / pamP->width;
        if (pamP->width * intQuotient == colormap.size)
            pamP->height = intQuotient;
        else
            pamP->height = intQuotient + 1;
    }
    {
        tuple ** outputRaster;
        unsigned int row;
        unsigned int colormapIndex;

        outputRaster = pnm_allocpamarray(pamP);

        colormapIndex = 0;  /* initial value */

        for (row = 0; row < pamP->height; ++row) {
            unsigned int col;
            for (col = 0; col < pamP->width; ++col) {
                unsigned int plane;
                for (plane = 0; plane < pamP->depth; ++plane) {
                    outputRaster[row][col][plane] =
                        colormap.table[colormapIndex]->tuple[plane];
                }
                if (colormapIndex < colormap.size-1)
                    ++colormapIndex;
            }
        }
        *outputRasterP = outputRaster;
    }
}



static void
colormapToSingleRow(struct pam * const pamP,
                    tupletable2  const colormap,
                    tuple ***    const outputRasterP) {

    tuple ** outputRaster;
    unsigned int col;

    pamP->width = colormap.size;
    pamP->height = 1;

    outputRaster = pnm_allocpamarray(pamP);

    for (col = 0; col < pamP->width; ++col) {
        int plane;
        for (plane = 0; plane < pamP->depth; ++plane)
            outputRaster[0][col][plane] = colormap.table[col]->tuple[plane];
    }
    *outputRasterP = outputRaster;
}



static void
colormapToImage(int                const format,
                const struct pam * const colormapPamP,
                tupletable2        const colormap,
                bool               const sort,
                bool               const square,
                struct pam *       const outpamP,
                tuple ***          const outputRasterP) {
/*----------------------------------------------------------------------------
   Create a tuple array and pam structure for an image which includes
   one pixel of each of the colors in the colormap 'colormap'.

   May rearrange the contents of 'colormap'.
-----------------------------------------------------------------------------*/
    outpamP->size             = sizeof(*outpamP);
    outpamP->len              = PAM_STRUCT_SIZE(tuple_type);
    outpamP->format           = format,
    outpamP->plainformat      = false;
    outpamP->depth            = colormapPamP->depth;
    outpamP->maxval           = colormapPamP->maxval;
    outpamP->bytes_per_sample = pnm_bytespersample(outpamP->maxval);
    STRSCPY(outpamP->tuple_type, colormapPamP->tuple_type);

    if (sort)
        sortColormap(colormap, outpamP->depth);

    if (square)
        colormapToSquare(outpamP, colormap, outputRasterP);
    else
        colormapToSingleRow(outpamP, colormap, outputRasterP);
}



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

    struct CmdlineInfo cmdline;
    FILE * ifP;
    int format;
    struct pam colormapPam;
    struct pam outpam;
    tuple ** colormapRaster;
    tupletable2 colormap;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileNm);

    computeColorMapFromInput(ifP,
                             cmdline.allcolors, cmdline.newColorCt,
                             cmdline.methodForLargest,
                             cmdline.methodForRep,
                             cmdline.methodForSplit,
                             !!cmdline.debug,
                             &format, &colormapPam, &colormap);

    pm_close(ifP);

    colormapToImage(format, &colormapPam, colormap,
                    cmdline.sort, cmdline.square, &outpam, &colormapRaster);

    if (cmdline.verbose)
        pm_message("Generating %u x %u image", outpam.width, outpam.height);

    outpam.file = stdout;

    pnm_writepam(&outpam, colormapRaster);

    pnm_freetupletable2(&colormapPam, colormap);

    pnm_freepamarray(colormapRaster, &outpam);

    pm_close(stdout);

    return 0;
}