# Code coverage report for lib\filter.js

## Statements: 99.25% (132 / 133) Branches: 98.77% (80 / 81) Functions: 100% (11 / 11) Lines: 99.21% (126 / 127) Ignored: none

All files » lib/ » filter.js

    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

    1

    1
    1053
    1053
    539

    1053

    1

    661
    661
    661
    661
    661

    661
    500
    161

    661

    661
    661
    661

    661
    661
    661
    70
    70
    462

    591

    1
    339

    1

    11514
    11514

    11514

    11514
    11514

    11514
    994154
    994154

    132202
    132202

    51184
    51184
    51184

    39440
    39440
    39440

    9952
    9952
    9952
    9952
    9952

    761376
    761376
    761376

    761376
    761376
    761376

    11514

    11514
    11514
    716
    716
    716

    10798

    11514
    11190

    324

    1

    322

    322

    9828

    9828
    49140
    49140
    32968
    32968

    9828

    322

    1

    9872

    9872
    9828
    1712272

    44
    44

    9872

    1

    10448

    10448
    620

    10448

    1793152

    1793152
    80880

    10448

    1

    12330

    12330
    2502

    12330

    2032176

    2032176
    319904

    12330

    1

    9918

    9918
    90

    9918

    1723792

    1723792
    11520

    9918

    1

    16400

    16400
    6572

    16400

    3007104

    3007104
    1294832

    16400

    1

    3768480

    3768480
    540962
    97510

     // Copyright (c) 2012 Kuba Niegowski
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    // in the Software without restriction, including without limitation the rights
    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    // copies of the Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall be included in
    // all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    // THE SOFTWARE.

    'use strict';

    var interlaceUtils = require('./interlace');

    function getByteWidth(width, bpp, depth) {
        var byteWidth = width * bpp;
        if (depth !== 8) {
            byteWidth = Math.ceil(byteWidth / (8 / depth));
        }
        return byteWidth;
    }

    var Filter = module.exports = function(width, height, Bpp, depth, interlace, options, dependencies) {

        this._width = width;
        this._height = height;
        this._Bpp = Bpp;
        this._depth = depth;
        this._options = options;

        if (!('filterType' in options) || options.filterType == -1) {
            options.filterType = [0, 1, 2, 3, 4];
        } else Iif (typeof options.filterType == 'number') {
            options.filterType = [options.filterType];
        }

        this._filters = {
            0: this._filterNone.bind(this),
            1: this._filterSub.bind(this),
            2: this._filterUp.bind(this),
            3: this._filterAvg.bind(this),
            4: this._filterPaeth.bind(this)
        };

        this.read = dependencies.read;
        this.write = dependencies.write;
        this.complete = dependencies.complete;

        this._imageIndex = 0;
        this._images = [];
        if (interlace) {
            var passes = interlaceUtils.getImagePasses(width, height);
            for(var i = 0; i < passes.length; i++) {
                this._images.push({
                    byteWidth: getByteWidth(passes[i].width, Bpp, depth),
                    height: passes[i].height,
                    lineIndex: 0
                });
            }
        } else {
            this._images.push({
                byteWidth: getByteWidth(width, Bpp, depth),
                height: height,
                lineIndex: 0
            });
        }
    };

    Filter.prototype.start = function() {
        this.read(this._images[this._imageIndex].byteWidth + 1, this._reverseFilterLine.bind(this));
    };

    Filter.prototype._reverseFilterLine = function(rawData) {

        var currentImage = this._images[this._imageIndex];
        var line = new Buffer(currentImage.byteWidth);

        var filter = rawData[0];

        var xComparison = this._depth >= 8 ? ((this._depth === 16) ? this._Bpp * 2 : this._Bpp) : 1;
        var xBiggerThan = xComparison - 1;

        for (var x = 0; x < currentImage.byteWidth; x++) {
            var rawByte = rawData[1 + x];
            switch(filter) {
                case 0:
                    line[x] = rawByte;
                    break;
                case 1:
                    var left = x > xBiggerThan ? line[x - xComparison] : 0;
                    line[x] = rawByte + left;
                    break;
                case 2:
                    var up = this._lastLine ? this._lastLine[x] : 0;
                    line[x] = rawByte + up;
                    break;
                case 3:
                    var up = this._lastLine ? this._lastLine[x] : 0;
                    var left = x > xBiggerThan ? line[x - xComparison] : 0;
                    var add = Math.floor((left + up) / 2);
                    line[x] = rawByte + add;
                    break;
                case 4:
                    var up = this._lastLine ? this._lastLine[x] : 0;
                    var left = x > xBiggerThan ? line[x - xComparison] : 0;
                    var upLeft = x > xBiggerThan && this._lastLine
                            ? this._lastLine[x - xComparison] : 0;
                    var add = PaethPredictor(left, up, upLeft);
                    line[x] = rawByte + add;
                    break;
            }

            //if (x === 5) {
            //    console.log("R", line[3], "G", line[4], "B", line[5]);
            //}
        }

        this.write(line);

        currentImage.lineIndex++;
        if (currentImage.lineIndex >= currentImage.height) {
            this._lastLine = null;
            this._imageIndex++;
            currentImage = this._images[this._imageIndex];
        } else {
            this._lastLine = line;
        }

        if (currentImage) {
            this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this));
        } else {
            this.complete(this._width, this._height);
        }
    };

    Filter.prototype.filter = function(pxData) {

        var rawData = new Buffer(((this._width << 2) + 1) * this._height);

        for (var y = 0; y < this._height; y++) {

            // find best filter for this line (with lowest sum of values)
            var filterTypes = this._options.filterType,
                min = Infinity,
                sel = 0;

            for (var i = 0; i < filterTypes.length; i++) {
                var sum = this._filters[filterTypes[i]](pxData, y, null);
                if (sum < min) {
                    sel = filterTypes[i];
                    min = sum;
                }
            }

            this._filters[sel](pxData, y, rawData);
        }
        return rawData;
    };

    Filter.prototype._filterNone = function(pxData, y, rawData) {

        var pxRowLength = this._width << 2,
            rawRowLength = pxRowLength + 1,
            sum = 0;

        if (!rawData) {
            for (var x = 0; x < pxRowLength; x++)
                sum += Math.abs(pxData[y * pxRowLength + x]);

        } else {
            rawData[y * rawRowLength] = 0;
            pxData.copy(rawData, rawRowLength * y + 1, pxRowLength * y, pxRowLength * (y + 1));
        }

        return sum;
    };

    Filter.prototype._filterSub = function(pxData, y, rawData) {

        var pxRowLength = this._width << 2,
            rawRowLength = pxRowLength + 1,
            sum = 0;

        if (rawData)
            rawData[y * rawRowLength] = 1;

        for (var x = 0; x < pxRowLength; x++) {

            var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0,
                val = pxData[y * pxRowLength + x] - left;

            if (!rawData) sum += Math.abs(val);
            else rawData[y * rawRowLength + 1 + x] = val;
        }
        return sum;
    };

    Filter.prototype._filterUp = function(pxData, y, rawData) {

        var pxRowLength = this._width << 2,
            rawRowLength = pxRowLength + 1,
            sum = 0;

        if (rawData)
            rawData[y * rawRowLength] = 2;

        for (var x = 0; x < pxRowLength; x++) {

            var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0,
                val = pxData[y * pxRowLength + x] - up;

            if (!rawData) sum += Math.abs(val);
            else rawData[y * rawRowLength + 1 + x] = val;
        }
        return sum;
    };

    Filter.prototype._filterAvg = function(pxData, y, rawData) {

        var pxRowLength = this._width << 2,
            rawRowLength = pxRowLength + 1,
            sum = 0;

        if (rawData)
            rawData[y * rawRowLength] = 3;

        for (var x = 0; x < pxRowLength; x++) {

            var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0,
                up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0,
                val = pxData[y * pxRowLength + x] - ((left + up) >> 1);

            if (!rawData) sum += Math.abs(val);
            else rawData[y * rawRowLength + 1 + x] = val;
        }
        return sum;
    };

    Filter.prototype._filterPaeth = function(pxData, y, rawData) {

        var pxRowLength = this._width << 2,
            rawRowLength = pxRowLength + 1,
            sum = 0;

        if (rawData)
            rawData[y * rawRowLength] = 4;

        for (var x = 0; x < pxRowLength; x++) {

            var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0,
                up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0,
                upLeft = x >= 4 && y > 0 ? pxData[(y - 1) * pxRowLength + x - 4] : 0,
                val = pxData[y * pxRowLength + x] - PaethPredictor(left, up, upLeft);

            if (!rawData) sum += Math.abs(val);
            else rawData[y * rawRowLength + 1 + x] = val;
        }
        return sum;
    };

    var PaethPredictor = function(left, above, upLeft) {

        var p = left + above - upLeft,
            pLeft = Math.abs(p - left),
            pAbove = Math.abs(p - above),
            pUpLeft = Math.abs(p - upLeft);

        if (pLeft <= pAbove && pLeft <= pUpLeft) return left;
        else if (pAbove <= pUpLeft) return above;
        else return upLeft;
    };