Code coverage report for lib\parser.js

Statements: 90.34% (159 / 176)      Branches: 86.11% (62 / 72)      Functions: 100% (20 / 20)      Lines: 90.34% (159 / 176)      Ignored: none     

All files » lib/ » parser.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 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                                              1       1   511 511   511 511     511 511   511 511 511 511 511 511 511   511 511 511 511 511 511 511 511     1               1 511         1   350   350 2754 12 12     338     1     2326     2326   2326 9304           2326       2326         2326 2326   2326 2088   238       238       1 238     1 2078     1   2078       2078 4 4     2074 1750       1 338   1   338   338                         338 6 6   332 4 4   328       328       328         328   328 328   328   328                   328       1 130   1   130   130     130 6828               130     1 22   1   22     22 12       12       12 2464           22   4   22 6     22     1 292   1   292 292   292     1 982   1   982   982     982 982   982     982       1 324   1   324   324 324   324     1   323   323     126   126 3204   3204 103332     103332       103332 413328       197   197 10 320   320 10240 10240   10240 4096 1834   6144 2718   10240 4552 18208           197 105 66     105 105   105 3360   3360 107520 107520 430080         323      
// 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 constants = require('./constants'),
  CrcCalculator = require('./crc');
 
 
var Parser = module.exports = function(options, dependencies) {
 
  this._options = options;
  options.checkCRC = options.checkCRC !== false;
 
  this._hasIHDR = false;
  this._hasIEND = false;
 
  // input flags/metadata
  this._palette = [];
  this._colorType = 0;
 
  this._chunks = {};
  this._chunks[constants.TYPE_IHDR] = this._handleIHDR.bind(this);
  this._chunks[constants.TYPE_IEND] = this._handleIEND.bind(this);
  this._chunks[constants.TYPE_IDAT] = this._handleIDAT.bind(this);
  this._chunks[constants.TYPE_PLTE] = this._handlePLTE.bind(this);
  this._chunks[constants.TYPE_tRNS] = this._handleTRNS.bind(this);
  this._chunks[constants.TYPE_gAMA] = this._handleGAMA.bind(this);
 
  this.read = dependencies.read;
  this.error = dependencies.error;
  this.metadata = dependencies.metadata;
  this.gamma = dependencies.gamma;
  this.parsed = dependencies.parsed;
  this.createData = dependencies.createData;
  this.inflateData = dependencies.inflateData;
    this.finished = dependencies.finished;
};
 
var colorTypeToBppMap = {
  0: 1,
  2: 3,
  3: 1,
  4: 2,
  6: 4
};
 
Parser.prototype.start = function() {
  this.read(constants.PNG_SIGNATURE.length,
    this._parseSignature.bind(this)
  );
};
 
Parser.prototype._parseSignature = function(data) {
 
  var signature = constants.PNG_SIGNATURE;
 
  for (var i = 0; i < signature.length; i++) {
    if (data[i] != signature[i]) {
      this.error(new Error('Invalid file signature'));
      return;
    }
  }
  this.read(8, this._parseChunkBegin.bind(this));
};
 
Parser.prototype._parseChunkBegin = function(data) {
 
  // chunk content length
  var length = data.readUInt32BE(0);
 
  // chunk type
  var type = data.readUInt32BE(4),
    name = '';
  for (var i = 4; i < 8; i++) {
    name += String.fromCharCode(data[i]);
  }
 
  //console.log('chunk ', name, length);
 
  // chunk flags
  var ancillary  = !!(data[4] & 0x20),  // or critical
    priv       = !!(data[5] & 0x20),  // or public
    safeToCopy = !!(data[7] & 0x20);  // or unsafe
 
  Iif (!this._hasIHDR && type != constants.TYPE_IHDR) {
    this.error(new Error('Expected IHDR on beggining'));
    return;
  }
 
  this._crc = new CrcCalculator();
  this._crc.write(new Buffer(name));
 
  if (this._chunks[type]) {
    return this._chunks[type](length);
 
  } else Iif (!ancillary) {
    this.error(new Error('Unsupported critical chunk type ' + name));
    return;
  } else {
    this.read(length + 4, this._skipChunk.bind(this));
  }
};
 
Parser.prototype._skipChunk = function(data) {
  this.read(8, this._parseChunkBegin.bind(this));
};
 
Parser.prototype._handleChunkEnd = function() {
  this.read(4, this._parseChunkEnd.bind(this));
};
 
Parser.prototype._parseChunkEnd = function(data) {
 
  var fileCrc = data.readInt32BE(0),
    calcCrc = this._crc.crc32();
 
  // check CRC
  if (this._options.checkCRC && calcCrc != fileCrc) {
    this.error(new Error('Crc error - ' + fileCrc + " - " + calcCrc));
    return;
  }
 
  if (!this._hasIEND) {
    this.read(8, this._parseChunkBegin.bind(this));
  }
};
 
Parser.prototype._handleIHDR = function(length) {
  this.read(length, this._parseIHDR.bind(this));
};
Parser.prototype._parseIHDR = function(data) {
 
  this._crc.write(data);
 
  var width = data.readUInt32BE(0),
    height = data.readUInt32BE(4),
    depth = data[8],
    colorType = data[9], // bits: 1 palette, 2 color, 4 alpha
    compr = data[10],
    filter = data[11],
    interlace = data[12];
 
  // console.log('    width', width, 'height', height,
  //     'depth', depth, 'colorType', colorType,
  //     'compr', compr, 'filter', filter, 'interlace', interlace
  // );
 
  if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1 && depth !== 16) {
    this.error(new Error('Unsupported bit depth ' + depth));
    return;
  }
  if (!(colorType in colorTypeToBppMap)) {
    this.error(new Error('Unsupported color type'));
    return;
  }
  Iif (compr != 0) {
    this.error(new Error('Unsupported compression method'));
    return;
  }
  Iif (filter != 0) {
    this.error(new Error('Unsupported filter method'));
    return;
  }
  Iif (interlace != 0 && interlace !== 1) {
    this.error(new Error('Unsupported interlace method'));
    return;
  }
 
  this._colorType = colorType;
 
  var bpp = colorTypeToBppMap[this._colorType];
  this.createData(width, height, bpp, depth, interlace);
 
  this._hasIHDR = true;
 
  this.metadata({
    width: width,
    height: height,
    depth: depth,
    interlace: interlace,
    palette: !!(colorType & constants.COLOR_PALETTE),
    color: !!(colorType & constants.COLOR_COLOR),
    alpha: !!(colorType & constants.COLOR_ALPHA)
  });
 
  this._handleChunkEnd();
};
 
 
Parser.prototype._handlePLTE = function(length) {
  this.read(length, this._parsePLTE.bind(this));
};
Parser.prototype._parsePLTE = function(data) {
 
  this._crc.write(data);
 
  var entries = Math.floor(data.length / 3);
  // console.log('Palette:', entries);
 
  for (var i = 0; i < entries; i++) {
    this._palette.push([
      data.readUInt8(i * 3),
      data.readUInt8(i * 3 + 1),
      data.readUInt8(i * 3 + 2 ),
      0xff
    ]);
  }
 
  this._handleChunkEnd();
};
 
Parser.prototype._handleTRNS = function(length) {
  this.read(length, this._parseTRNS.bind(this));
};
Parser.prototype._parseTRNS = function(data) {
 
  this._crc.write(data);
 
  // palette
  if (this._colorType == 3) {
    Iif (this._palette.length == 0) {
      this.error(new Error('Transparency chunk must be after palette'));
      return;
    }
    Iif (data.length > this._palette.length) {
      this.error(new Error('More transparent colors than palette size'));
      return;
    }
    for (var i = 0; i < this._palette.length; i++) {
      this._palette[i][3] = i < data.length ? data.readUInt8(i) : 0xff;
    }
  }
 
  // for colorType 0 (grayscale) and 2 (rgb)
  // there might be one gray/color defined as transparent
  if (this._colorType === 0) {
    // grey, 2 bytes
    this._transColor = [data.readUInt16BE(0)];
  }
  if (this._colorType === 2) {
    this._transColor = [data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)];
  }
 
  this._handleChunkEnd();
};
 
Parser.prototype._handleGAMA = function(length) {
  this.read(length, this._parseGAMA.bind(this));
};
Parser.prototype._parseGAMA = function(data) {
 
  this._crc.write(data);
  this.gamma(data.readUInt32BE(0) / 100000);
 
  this._handleChunkEnd();
};
 
Parser.prototype._handleIDAT = function(length) {
  this.read(-length, this._parseIDAT.bind(this, length));
};
Parser.prototype._parseIDAT = function(length, data) {
 
  this._crc.write(data);
 
  Iif (this._colorType == 3 && this._palette.length == 0)
    throw new Error('Expected palette not found');
 
  this.inflateData(data);
  length -= data.length;
 
  Iif (length > 0)
    this._handleIDAT(length);
  else
    this._handleChunkEnd();
};
 
 
Parser.prototype._handleIEND = function(length) {
  this.read(length, this._parseIEND.bind(this));
};
Parser.prototype._parseIEND = function(data) {
 
  this._crc.write(data);
 
  this._hasIEND = true;
  this._handleChunkEnd();
 
  this.finished();
};
 
Parser.prototype.reverseFiltered = function(indata, depth, width, height) {
 
  var outdata = indata; // only different for 16 bits
 
  if (this._colorType == 3) { // paletted
    //console.log("paletted");
    // use values from palette
    var pxLineLength = width << 2;
 
    for (var y = 0; y < height; y++) {
      var pxRowPos = y * pxLineLength;
 
      for (var x = 0; x < width; x++) {
        var pxPos = pxRowPos + (x << 2),
          color = this._palette[indata[pxPos]];
 
        Iif (!color) {
          throw new Error("index " + indata[pxPos] + " not in palette");
        }
 
        for (var i = 0; i < 4; i++)
          indata[pxPos + i] = color[i];
      }
    }
  } else {
    var pxLineLength = width << 2;
 
    if (this._transColor) {
      for (var y = 0; y < height; y++) {
        var pxRowPos = y * pxLineLength;
 
        for (var x = 0; x < width; x++) {
          var pxPos = pxRowPos + (x << 2);
          var makeTrans = false;
          //console.log(pxPos);
          if (this._transColor.length === 1) {
            if (this._transColor[0] === indata[pxPos]) {
              makeTrans = true;
            }
          } else if (this._transColor[0] === indata[pxPos] && this._transColor[1] === indata[pxPos + 1] && this._transColor[2] === indata[pxPos + 2]) {
            makeTrans = true;
          }
          if (makeTrans) {
            for (var i = 0; i < 4; i++) {
              indata[pxPos + i] = 0;
            }
          }
        }
      }
    }
    if (depth !== 8) {
      if (depth === 16) {
        outdata = new Buffer(width * height * 4);
      }
      //console.log("adjusting");
      var maxOutSample = 255;
      var maxInSample = Math.pow(2, depth) - 1;
 
      for (var y = 0; y < height; y++) {
        var pxRowPos = y * pxLineLength;
 
        for (var x = 0; x < width; x++) {
          var pxPos = pxRowPos + (x << 2);
          for (var i = 0; i < 4; i++)
            outdata[pxPos + i] = Math.floor((indata[pxPos + i] * maxOutSample) / maxInSample + 0.5);
        }
      }
    }
  }
  return outdata;
};