Code coverage report for bitmapper.js

# All files bitmapper.js

94.37% Statements 134/142
72.41% Branches 21/29
89.47% Functions 17/19
94.16% Lines 129/137

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

    1x

    1x

    313632x

    313632x
    313632x
    313632x
    313632x
    313632x

    8192x

    8192x
    8192x
    8192x
    8192x
    8192x

    1228160x

    1228160x
    1228160x
    1228160x
    1228160x

    272640x

    272640x
    272640x
    272640x
    272640x

    1x

    1193380x
    1193380x
    1193380x
    1193380x
    1193380x

    8192x
    8192x
    8192x
    8192x
    8192x

    24576x
    24576x
    24576x
    24576x

    8192x
    8192x
    8192x
    8192x

    209x
    209x

    334250x

    334250x
    334250x

    334250x

    149504x
    149504x
    149504x
    149504x

    49612x
    49612x
    49612x
    49612x

    2968x
    2968x
    2968x
    2968x
    2968x
    2968x

    132166x
    132166x
    132166x
    132166x
    132166x
    132166x
    132166x
    132166x
    132166x
    132166x

    209x

    1234340x
    334250x

    1234340x
    1234340x
    1234340x

    8106x

    209x

    210x
    210x
    210x
    210x
    7288x
    1822624x
    1822624x
    1822624x

    210x

    529x
    529x
    529x
    529x
    8106x
    1234340x
    1234340x
    1234340x

    8106x

    1x
    335x
    335x
    335x
    335x
    335x

    335x
    209x

    335x
    269x

    66x

    335x
    335x

    335x
    72x
    72x

    263x
    263x
    2732114x
    2732114x
    2732114x

    263x

    335x
    739x
    210x

    529x

    335x
    126x

    209x

    335x

     "use strict";

    let interlaceUtils = require("./interlace");

    let pixelBppMapper = [
      // 0 - dummy entry
      function () {},

      // 1 - L
      // 0: 0, 1: 0, 2: 0, 3: 0xff
      function (pxData, data, pxPos, rawPos) {
        Iif (rawPos === data.length) {
          throw new Error("Ran out of data");
        }

        let pixel = data[rawPos];
        pxData[pxPos] = pixel;
        pxData[pxPos + 1] = pixel;
        pxData[pxPos + 2] = pixel;
        pxData[pxPos + 3] = 0xff;
      },

      // 2 - LA
      // 0: 0, 1: 0, 2: 0, 3: 1
      function (pxData, data, pxPos, rawPos) {
        Iif (rawPos + 1 >= data.length) {
          throw new Error("Ran out of data");
        }

        let pixel = data[rawPos];
        pxData[pxPos] = pixel;
        pxData[pxPos + 1] = pixel;
        pxData[pxPos + 2] = pixel;
        pxData[pxPos + 3] = data[rawPos + 1];
      },

      // 3 - RGB
      // 0: 0, 1: 1, 2: 2, 3: 0xff
      function (pxData, data, pxPos, rawPos) {
        Iif (rawPos + 2 >= data.length) {
          throw new Error("Ran out of data");
        }

        pxData[pxPos] = data[rawPos];
        pxData[pxPos + 1] = data[rawPos + 1];
        pxData[pxPos + 2] = data[rawPos + 2];
        pxData[pxPos + 3] = 0xff;
      },

      // 4 - RGBA
      // 0: 0, 1: 1, 2: 2, 3: 3
      function (pxData, data, pxPos, rawPos) {
        Iif (rawPos + 3 >= data.length) {
          throw new Error("Ran out of data");
        }

        pxData[pxPos] = data[rawPos];
        pxData[pxPos + 1] = data[rawPos + 1];
        pxData[pxPos + 2] = data[rawPos + 2];
        pxData[pxPos + 3] = data[rawPos + 3];
      },
    ];

    let pixelBppCustomMapper = [
      // 0 - dummy entry
      function () {},

      // 1 - L
      // 0: 0, 1: 0, 2: 0, 3: 0xff
      function (pxData, pixelData, pxPos, maxBit) {
        let pixel = pixelData[0];
        pxData[pxPos] = pixel;
        pxData[pxPos + 1] = pixel;
        pxData[pxPos + 2] = pixel;
        pxData[pxPos + 3] = maxBit;
      },

      // 2 - LA
      // 0: 0, 1: 0, 2: 0, 3: 1
      function (pxData, pixelData, pxPos) {
        let pixel = pixelData[0];
        pxData[pxPos] = pixel;
        pxData[pxPos + 1] = pixel;
        pxData[pxPos + 2] = pixel;
        pxData[pxPos + 3] = pixelData[1];
      },

      // 3 - RGB
      // 0: 0, 1: 1, 2: 2, 3: 0xff
      function (pxData, pixelData, pxPos, maxBit) {
        pxData[pxPos] = pixelData[0];
        pxData[pxPos + 1] = pixelData[1];
        pxData[pxPos + 2] = pixelData[2];
        pxData[pxPos + 3] = maxBit;
      },

      // 4 - RGBA
      // 0: 0, 1: 1, 2: 2, 3: 3
      function (pxData, pixelData, pxPos) {
        pxData[pxPos] = pixelData[0];
        pxData[pxPos + 1] = pixelData[1];
        pxData[pxPos + 2] = pixelData[2];
        pxData[pxPos + 3] = pixelData[3];
      },
    ];

    function bitRetriever(data, depth) {
      let leftOver = [];
      let i = 0;

      function split() {
        Iif (i === data.length) {
          throw new Error("Ran out of data");
        }
        let byte = data[i];
        i++;
        let byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1;
        switch (depth) {
          default:
            throw new Error("unrecognised depth");
          case 16:
            byte2 = data[i];
            i++;
            leftOver.push((byte << 8) + byte2);
            break;
          case 4:
            byte2 = byte & 0x0f;
            byte1 = byte >> 4;
            leftOver.push(byte1, byte2);
            break;
          case 2:
            byte4 = byte & 3;
            byte3 = (byte >> 2) & 3;
            byte2 = (byte >> 4) & 3;
            byte1 = (byte >> 6) & 3;
            leftOver.push(byte1, byte2, byte3, byte4);
            break;
          case 1:
            byte8 = byte & 1;
            byte7 = (byte >> 1) & 1;
            byte6 = (byte >> 2) & 1;
            byte5 = (byte >> 3) & 1;
            byte4 = (byte >> 4) & 1;
            byte3 = (byte >> 5) & 1;
            byte2 = (byte >> 6) & 1;
            byte1 = (byte >> 7) & 1;
            leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8);
            break;
        }
      }

      return {
        get: function (count) {
          while (leftOver.length < count) {
            split();
          }
          let returner = leftOver.slice(0, count);
          leftOver = leftOver.slice(count);
          return returner;
        },
        resetAfterLine: function () {
          leftOver.length = 0;
        },
        end: function () {
          Iif (i !== data.length) {
            throw new Error("extra data found");
          }
        },
      };
    }

    function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) {
      // eslint-disable-line max-params
      let imageWidth = image.width;
      let imageHeight = image.height;
      let imagePass = image.index;
      for (let y = 0; y < imageHeight; y++) {
        for (let x = 0; x < imageWidth; x++) {
          let pxPos = getPxPos(x, y, imagePass);
          pixelBppMapper[bpp](pxData, data, pxPos, rawPos);
          rawPos += bpp; //eslint-disable-line no-param-reassign
        }
      }
      return rawPos;
    }

    function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) {
      // eslint-disable-line max-params
      let imageWidth = image.width;
      let imageHeight = image.height;
      let imagePass = image.index;
      for (let y = 0; y < imageHeight; y++) {
        for (let x = 0; x < imageWidth; x++) {
          let pixelData = bits.get(bpp);
          let pxPos = getPxPos(x, y, imagePass);
          pixelBppCustomMapper[bpp](pxData, pixelData, pxPos, maxBit);
        }
        bits.resetAfterLine();
      }
    }

    exports.dataToBitMap = function (data, bitmapInfo) {
      let width = bitmapInfo.width;
      let height = bitmapInfo.height;
      let depth = bitmapInfo.depth;
      let bpp = bitmapInfo.bpp;
      let interlace = bitmapInfo.interlace;
      let bits;

      if (depth !== 8) {
        bits = bitRetriever(data, depth);
      }
      let pxData;
      if (depth <= 8) {
        pxData = Buffer.alloc(width * height * 4);
      } else {
        pxData = new Uint16Array(width * height * 4);
      }
      let maxBit = Math.pow(2, depth) - 1;
      let rawPos = 0;
      let images;
      let getPxPos;

      if (interlace) {
        images = interlaceUtils.getImagePasses(width, height);
        getPxPos = interlaceUtils.getInterlaceIterator(width, height);
      } else {
        let nonInterlacedPxPos = 0;
        getPxPos = function () {
          let returner = nonInterlacedPxPos;
          nonInterlacedPxPos += 4;
          return returner;
        };
        images = [{ width: width, height: height }];
      }

      for (let imageIndex = 0; imageIndex < images.length; imageIndex++) {
        if (depth === 8) {
          rawPos = mapImage8Bit(
            images[imageIndex],
            pxData,
            getPxPos,
            bpp,
            data,
            rawPos
          );
        } else {
          mapImageCustomBit(
            images[imageIndex],
            pxData,
            getPxPos,
            bpp,
            bits,
            maxBit
          );
        }
      }
      if (depth === 8) {
        Iif (rawPos !== data.length) {
          throw new Error("extra data found");
        }
      } else {
        bits.end();
      }

      return pxData;
    };