# All files / fast-colors/src pixel-box.ts

100% Statements 163/163
100% Branches 56/56
100% Functions 4/4
100% Lines 144/144

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    5x

    5x

    4524x
    4x
    4x

    4520x

    4520x
    3890x
    3890x

    630x
    176x
    176x

    454x
    454x
    7414x
    454x
    454x

    454x

    5x

    724x
    724x
    724x
    724x
    724x
    724x
    724x
    724x

    724x
    724x
    724x
    724x

    724x
    724x
    724x
    724x

    724x
    4082x
    37642x
    491508x

    491508x
    491508x
    491508x
    491508x

    724x
    154x

    570x

    724x
    4164x

    3804x

    360x
    360x
    360x
    360x

    360x
    124x
    124x
    236x

    122x
    122x

    114x
    114x

    360x
    360x

    360x
    360x
    360x

    360x

    124x
    1100x
    1100x
    10788x
    153440x

    1100x
    1100x

    124x
    1100x

    124x
    650x
    124x
    124x

    124x
    70x

    54x

    124x
    144x

    124x
    124x

    36x
    36x

    124x

    124x

    124x

    236x

    122x
    1262x
    1262x
    10484x
    130318x

    1262x
    1262x

    122x
    1262x

    122x
    774x
    122x
    122x

    122x
    58x

    64x

    122x

    218x

    122x
    122x

    56x
    56x

    122x

    122x

    122x

    114x
    1300x
    1300x
    10600x
    129908x

    1300x
    1300x

    114x
    1300x

    114x
    830x
    114x
    114x

    114x
    52x

    62x

    114x

    258x

    114x
    114x

    44x
    44x

    114x

    114x

    114x

    360x

     import { Histogram } from "./histogram";
    import { ColorRGBA64 } from "./color-rgba-64";

    /**
     * Adds a newItem to an already sorted list without needing to do a full re-sort.
     * Higher sort priority puts the newItem closer to the start (index 0) of the list.
     */
    export function insertIntoSortedList(
        list: PixelBox[],
        newItem: PixelBox,
        sortPriority: (box: PixelBox) => number
    ): void {
        if (list.length === 0) {
            list.push(newItem);
            return;
        }
        const newItemPriority: number = sortPriority(newItem);
        // The new item being either first or last happens often enough that it is worth special casing
        // In cases of a tie the new item should be inserted after existing items of the same priority
        if (newItemPriority > sortPriority(list[0])) {
            list.unshift(newItem);
            return;
        }
        if (newItemPriority <= sortPriority(list[list.length - 1])) {
            list.push(newItem);
            return;
        }
        let newIndex: number = 0;
        for (let i: number = 0; i < list.length; i++) {
            if (newItemPriority > sortPriority(list[i])) {
                newIndex = i;
                break;
            }
        }
        list.splice(newIndex, 0, newItem);
    }

    /**
     * Represents a range of colors in RGB color space.
     */
    export class PixelBox {
        constructor(
            globalHistogram: Histogram,
            minRed: number,
            maxRed: number,
            minGreen: number,
            maxGreen: number,
            minBlue: number,
            maxBlue: number
        ) {
            this.pixelCount = 0;
            this.globalHistogram = globalHistogram;
            this.minRed = minRed;
            this.maxRed = maxRed;
            this.minGreen = minGreen;
            this.maxGreen = maxGreen;
            this.minBlue = minBlue;
            this.maxBlue = maxBlue;

            this.rangeRed = this.maxRed - this.minRed + 1;
            this.rangeGreen = this.maxGreen - this.minGreen + 1;
            this.rangeBlue = this.maxBlue - this.minBlue + 1;
            this.colorVolume = this.rangeRed * this.rangeGreen * this.rangeBlue;

            let redSum: number = 0;
            let greenSum: number = 0;
            let blueSum: number = 0;
            const factor: number = 1 << (8 - this.globalHistogram.significantBits);

            for (let r: number = minRed; r <= maxRed; r++) {
                for (let g: number = minGreen; g <= maxGreen; g++) {
                    for (let b: number = minBlue; b <= maxBlue; b++) {
                        const histoValue: number = this.globalHistogram.getHistogramValue(
                            r,
                            g,
                            b
                        );
                        this.pixelCount += histoValue;
                        redSum += histoValue * (r + 0.5) * factor;
                        greenSum += histoValue * (g + 0.5) * factor;
                        blueSum += histoValue * (b + 0.5) * factor;
                    }
                }
            }

            if (this.pixelCount === 0) {
                this.averageColor = new ColorRGBA64(
                    (factor * ((minRed + maxRed + 1) / 2)) / 255,
                    (factor * ((minGreen + maxGreen + 1) / 2)) / 255,
                    (factor * ((minBlue + maxBlue + 1) / 2)) / 255,
                    1
                );
            } else {
                this.averageColor = new ColorRGBA64(
                    redSum / this.pixelCount / 255,
                    greenSum / this.pixelCount / 255,
                    blueSum / this.pixelCount / 255,
                    1
                );
            }
        }

        public readonly globalHistogram: Histogram;
        public readonly pixelCount: number;

        public readonly minRed: number;
        public readonly maxRed: number;
        public readonly rangeRed: number;
        public readonly minGreen: number;
        public readonly maxGreen: number;
        public readonly rangeGreen: number;
        public readonly minBlue: number;
        public readonly maxBlue: number;
        public readonly rangeBlue: number;
        public readonly colorVolume: number;

        public readonly averageColor: ColorRGBA64;

        /**
         * Attempts to divide the range of colors represented by this PixelBox into two smaller PixelBox objects.
         * This does not actually cut directly at the median, rather it finds the median then cuts halfway through the larger box on either side of that median. The result is that small areas of color are better represented in the final output.
         * Based on the Modified Median Cut Quantization implementation from https://github.com/DanBloomberg/leptonica/blob/master/src/colorquant2.c
         */
        public modifiedMedianCut = (): [PixelBox | null, PixelBox | null] => {
            if (this.rangeRed === 1 && this.rangeGreen === 1 && this.rangeBlue === 1) {
                // This box is already sliced as finely as possible
                return [this, null];
            }

            enum CutAxis {
                Red,
                Green,
                Blue,
            }
            let axis: CutAxis;
            let axisRange: number;
            if (this.rangeRed >= this.rangeGreen && this.rangeRed >= this.rangeBlue) {
                axis = CutAxis.Red;
                axisRange = this.rangeRed;
            } else if (
                this.rangeGreen >= this.rangeRed &&
                this.rangeGreen >= this.rangeBlue
            ) {
                axis = CutAxis.Green;
                axisRange = this.rangeGreen;
            } else {
                axis = CutAxis.Blue;
                axisRange = this.rangeBlue;
            }
            const partialSum: number[] = new Array(axisRange);
            const lookAheadSum: number[] = new Array(axisRange);

            let retLeft: PixelBox | null = null;
            let retRight: PixelBox | null = null;
            let axisTotal: number = 0;
            // This does not actually cut directly at the median, rather it finds the median then
            // cuts halfway through the larger box on either side of that median
            // The result is that small areas of color are better represented in the final output
            if (axis === CutAxis.Red) {
                // Calculate partial sums
                for (let r: number = this.minRed; r <= this.maxRed; r++) {
                    let sum: number = 0;
                    for (let g: number = this.minGreen; g <= this.maxGreen; g++) {
                        for (let b: number = this.minBlue; b <= this.maxBlue; b++) {
                            sum += this.globalHistogram.getHistogramValue(r, g, b);
                        }
                    }
                    axisTotal += sum;
                    partialSum[r - this.minRed] = axisTotal;
                }
                for (let i: number = 0; i < partialSum.length; i++) {
                    lookAheadSum[i] = axisTotal - partialSum[i];
                }

                // Find the cut point based on partial sums vs total
                for (let r: number = this.minRed; r <= this.maxRed; r++) {
                    if (partialSum[r - this.minRed] >= axisTotal / 2) {
                        const left: number = r - this.minRed;
                        const right: number = this.maxRed - r;
                        let cut: number;
                        if (left <= right) {
                            cut = Math.min(this.maxRed - 1, Math.floor(r + right / 2));
                        } else {
                            cut = Math.max(this.minRed, Math.floor(r - 1 - left / 2));
                        }
                        // Adjust the cut point if either side has 0 pixelCount
                        while (partialSum[cut - this.minRed] <= 0 && cut < this.maxRed - 1) {
                            cut++;
                        }
                        let lookAhead: number = lookAheadSum[cut - this.minRed];
                        while (
                            lookAhead === 0 &&
                            cut > this.minRed &&
                            partialSum[cut - this.minRed - 1] !== 0
                        ) {
                            cut--;
                            lookAhead = lookAheadSum[cut - this.minRed];
                        }

                        retLeft = new PixelBox(
                            this.globalHistogram,
                            this.minRed,
                            cut,
                            this.minGreen,
                            this.maxGreen,
                            this.minBlue,
                            this.maxBlue
                        );
                        retRight = new PixelBox(
                            this.globalHistogram,
                            cut + 1,
                            this.maxRed,
                            this.minGreen,
                            this.maxGreen,
                            this.minBlue,
                            this.maxBlue
                        );
                        break;
                    }
                }
            } else if (axis === CutAxis.Green) {
                // Calculate partial sums
                for (let g: number = this.minGreen; g <= this.maxGreen; g++) {
                    let sum: number = 0;
                    for (let r: number = this.minRed; r <= this.maxRed; r++) {
                        for (let b: number = this.minBlue; b <= this.maxBlue; b++) {
                            sum += this.globalHistogram.getHistogramValue(r, g, b);
                        }
                    }
                    axisTotal += sum;
                    partialSum[g - this.minGreen] = axisTotal;
                }
                for (let i: number = 0; i < partialSum.length; i++) {
                    lookAheadSum[i] = axisTotal - partialSum[i];
                }

                // Find the cut point based on partial sums vs total
                for (let g: number = this.minGreen; g <= this.maxGreen; g++) {
                    if (partialSum[g - this.minGreen] >= axisTotal / 2) {
                        const left: number = g - this.minGreen;
                        const right: number = this.maxGreen - g;
                        let cut: number;
                        if (left <= right) {
                            cut = Math.min(this.maxGreen - 1, Math.floor(g + right / 2));
                        } else {
                            cut = Math.max(this.minGreen, Math.floor(g - 1 - left / 2));
                        }
                        // Adjust the cut point if either side has 0 pixelCount
                        while (
                            partialSum[cut - this.minGreen] <= 0 &&
                            cut < this.maxGreen - 1
                        ) {
                            cut++;
                        }
                        let lookAhead: number = lookAheadSum[cut - this.minGreen];
                        while (
                            lookAhead === 0 &&
                            cut > this.minGreen &&
                            partialSum[cut - this.minGreen - 1] !== 0
                        ) {
                            cut--;
                            lookAhead = lookAheadSum[cut - this.minGreen];
                        }

                        retLeft = new PixelBox(
                            this.globalHistogram,
                            this.minRed,
                            this.maxRed,
                            this.minGreen,
                            cut,
                            this.minBlue,
                            this.maxBlue
                        );
                        retRight = new PixelBox(
                            this.globalHistogram,
                            this.minRed,
                            this.maxRed,
                            cut + 1,
                            this.maxGreen,
                            this.minBlue,
                            this.maxBlue
                        );
                        break;
                    }
                }
            } else {
                // Calculate partial sums
                for (let b: number = this.minBlue; b <= this.maxBlue; b++) {
                    let sum: number = 0;
                    for (let r: number = this.minRed; r <= this.maxRed; r++) {
                        for (let g: number = this.minGreen; g <= this.maxGreen; g++) {
                            sum += this.globalHistogram.getHistogramValue(r, g, b);
                        }
                    }
                    axisTotal += sum;
                    partialSum[b - this.minBlue] = axisTotal;
                }
                for (let i: number = 0; i < partialSum.length; i++) {
                    lookAheadSum[i] = axisTotal - partialSum[i];
                }

                // Find the cut point based on partial sums vs total
                for (let b: number = this.minBlue; b <= this.maxBlue; b++) {
                    if (partialSum[b - this.minBlue] >= axisTotal / 2) {
                        const left: number = b - this.minBlue;
                        const right: number = this.maxBlue - b;
                        let cut: number;
                        if (left <= right) {
                            cut = Math.min(this.maxBlue - 1, Math.floor(b + right / 2));
                        } else {
                            cut = Math.max(this.minBlue, Math.floor(b - 1 - left / 2));
                        }
                        // Adjust the cut point if either side has 0 pixelCount
                        while (
                            partialSum[cut - this.minBlue] <= 0 &&
                            cut < this.maxBlue - 1
                        ) {
                            cut++;
                        }
                        let lookAhead: number = lookAheadSum[cut - this.minBlue];
                        while (
                            lookAhead === 0 &&
                            cut > this.minBlue &&
                            partialSum[cut - this.minBlue - 1] !== 0
                        ) {
                            cut--;
                            lookAhead = lookAheadSum[cut - this.minBlue];
                        }

                        retLeft = new PixelBox(
                            this.globalHistogram,
                            this.minRed,
                            this.maxRed,
                            this.minGreen,
                            this.maxGreen,
                            this.minBlue,
                            cut
                        );
                        retRight = new PixelBox(
                            this.globalHistogram,
                            this.minRed,
                            this.maxRed,
                            this.minGreen,
                            this.maxGreen,
                            cut + 1,
                            this.maxBlue
                        );
                        break;
                    }
                }
            }

            return [retLeft, retRight];
        };
    }