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 | 1x 1x 1x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 529x 1x 529x 1x 365x 365x 2867x 13x 13x 352x 1x 2461x 2461x 2461x 2461x 9844x 2461x 2461x 2461x 2461x 2461x 2205x 256x 256x 1x 256x 1x 2195x 1x 2195x 2195x 2195x 4x 4x 2191x 1855x 1x 351x 1x 351x 351x 351x 351x 351x 351x 351x 351x 351x 6x 6x 345x 4x 4x 341x 341x 341x 341x 341x 341x 341x 341x 1x 135x 1x 135x 135x 135x 7350x 135x 135x 1x 27x 27x 1x 27x 27x 15x 15x 15x 529x 15x 27x 6x 27x 6x 27x 1x 296x 1x 296x 296x 296x 1x 1233x 337x 337x 1233x 1x 1233x 1233x 1233x 1233x 1233x 173x 1060x 1x 336x 1x 336x 336x 336x 336x 173x | "use strict"; let constants = require("./constants"); let CrcCalculator = require("./crc"); let Parser = (module.exports = function (options, dependencies) { this._options = options; options.checkCRC = options.checkCRC !== false; this._hasIHDR = false; this._hasIEND = false; this._emittedHeadersFinished = 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.transColor = dependencies.transColor; this.palette = dependencies.palette; this.parsed = dependencies.parsed; this.inflateData = dependencies.inflateData; this.finished = dependencies.finished; this.simpleTransparency = dependencies.simpleTransparency; this.headersFinished = dependencies.headersFinished || function () {}; }); Parser.prototype.start = function () { this.read(constants.PNG_SIGNATURE.length, this._parseSignature.bind(this)); }; Parser.prototype._parseSignature = function (data) { let signature = constants.PNG_SIGNATURE; for (let 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 let length = data.readUInt32BE(0); // chunk type let type = data.readUInt32BE(4); let name = ""; for (let i = 4; i < 8; i++) { name += String.fromCharCode(data[i]); } //console.log('chunk ', name, length); // chunk flags let ancillary = Boolean(data[4] & 0x20); // or critical // priv = Boolean(data[5] & 0x20), // or public // safeToCopy = Boolean(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(Buffer.from(name)); if (this._chunks[type]) { return this._chunks[type](length); } Iif (!ancillary) { this.error(new Error("Unsupported critical chunk type " + name)); return; } 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) { let fileCrc = data.readInt32BE(0); let 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); let width = data.readUInt32BE(0); let height = data.readUInt32BE(4); let depth = data[8]; let colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha let compr = data[10]; let filter = data[11]; let 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 constants.COLORTYPE_TO_BPP_MAP)) { 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; let bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType]; this._hasIHDR = true; this.metadata({ width: width, height: height, depth: depth, interlace: Boolean(interlace), palette: Boolean(colorType & constants.COLORTYPE_PALETTE), color: Boolean(colorType & constants.COLORTYPE_COLOR), alpha: Boolean(colorType & constants.COLORTYPE_ALPHA), bpp: bpp, colorType: colorType, }); this._handleChunkEnd(); }; Parser.prototype._handlePLTE = function (length) { this.read(length, this._parsePLTE.bind(this)); }; Parser.prototype._parsePLTE = function (data) { this._crc.write(data); let entries = Math.floor(data.length / 3); // console.log('Palette:', entries); for (let i = 0; i < entries; i++) { this._palette.push([data[i * 3], data[i * 3 + 1], data[i * 3 + 2], 0xff]); } this.palette(this._palette); this._handleChunkEnd(); }; Parser.prototype._handleTRNS = function (length) { this.simpleTransparency(); this.read(length, this._parseTRNS.bind(this)); }; Parser.prototype._parseTRNS = function (data) { this._crc.write(data); // palette if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) { 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 (let i = 0; i < data.length; i++) { this._palette[i][3] = data[i]; } this.palette(this._palette); } // for colorType 0 (grayscale) and 2 (rgb) // there might be one gray/color defined as transparent if (this._colorType === constants.COLORTYPE_GRAYSCALE) { // grey, 2 bytes this.transColor([data.readUInt16BE(0)]); } if (this._colorType === constants.COLORTYPE_COLOR) { 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) / constants.GAMMA_DIVISION); this._handleChunkEnd(); }; Parser.prototype._handleIDAT = function (length) { if (!this._emittedHeadersFinished) { this._emittedHeadersFinished = true; this.headersFinished(); } this.read(-length, this._parseIDAT.bind(this, length)); }; Parser.prototype._parseIDAT = function (length, data) { this._crc.write(data); Iif ( this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0 ) { throw new Error("Expected palette not found"); } this.inflateData(data); let leftOverLength = length - data.length; if (leftOverLength > 0) { this._handleIDAT(leftOverLength); } 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(); if (this.finished) { this.finished(); } }; |