How to use JpegError method in wpt

Best JavaScript code snippet using wpt

jpg.js

Source:jpg.js Github

copy

Full Screen

...26exports.JpegImage = undefined;27var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };28var _util = require('../shared/util');29var JpegError = function JpegErrorClosure() {30 function JpegError(msg) {31 this.message = 'JPEG error: ' + msg;32 }33 JpegError.prototype = new Error();34 JpegError.prototype.name = 'JpegError';35 JpegError.constructor = JpegError;36 return JpegError;37}();38var JpegImage = function JpegImageClosure() {39 var dctZigZag = new Uint8Array([0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63]);40 var dctCos1 = 4017;41 var dctSin1 = 799;42 var dctCos3 = 3406;43 var dctSin3 = 2276;44 var dctCos6 = 1567;45 var dctSin6 = 3784;46 var dctSqrt2 = 5793;47 var dctSqrt1d2 = 2896;48 function JpegImage() {49 this.decodeTransform = null;50 this.colorTransform = -1;51 }52 function buildHuffmanTable(codeLengths, values) {53 var k = 0,54 code = [],55 i,56 j,57 length = 16;58 while (length > 0 && !codeLengths[length - 1]) {59 length--;60 }61 code.push({62 children: [],63 index: 064 });65 var p = code[0],66 q;67 for (i = 0; i < length; i++) {68 for (j = 0; j < codeLengths[i]; j++) {69 p = code.pop();70 p.children[p.index] = values[k];71 while (p.index > 0) {72 p = code.pop();73 }74 p.index++;75 code.push(p);76 while (code.length <= i) {77 code.push(q = {78 children: [],79 index: 080 });81 p.children[p.index] = q.children;82 p = q;83 }84 k++;85 }86 if (i + 1 < length) {87 code.push(q = {88 children: [],89 index: 090 });91 p.children[p.index] = q.children;92 p = q;93 }94 }95 return code[0].children;96 }97 function getBlockBufferOffset(component, row, col) {98 return 64 * ((component.blocksPerLine + 1) * row + col);99 }100 function decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successivePrev, successive) {101 var mcusPerLine = frame.mcusPerLine;102 var progressive = frame.progressive;103 var startOffset = offset,104 bitsData = 0,105 bitsCount = 0;106 function readBit() {107 if (bitsCount > 0) {108 bitsCount--;109 return bitsData >> bitsCount & 1;110 }111 bitsData = data[offset++];112 if (bitsData === 0xFF) {113 var nextByte = data[offset++];114 if (nextByte) {115 throw new JpegError('unexpected marker ' + (bitsData << 8 | nextByte).toString(16));116 }117 }118 bitsCount = 7;119 return bitsData >>> 7;120 }121 function decodeHuffman(tree) {122 var node = tree;123 while (true) {124 node = node[readBit()];125 if (typeof node === 'number') {126 return node;127 }128 if ((typeof node === 'undefined' ? 'undefined' : _typeof(node)) !== 'object') {129 throw new JpegError('invalid huffman sequence');130 }131 }132 }133 function receive(length) {134 var n = 0;135 while (length > 0) {136 n = n << 1 | readBit();137 length--;138 }139 return n;140 }141 function receiveAndExtend(length) {142 if (length === 1) {143 return readBit() === 1 ? 1 : -1;144 }145 var n = receive(length);146 if (n >= 1 << length - 1) {147 return n;148 }149 return n + (-1 << length) + 1;150 }151 function decodeBaseline(component, offset) {152 var t = decodeHuffman(component.huffmanTableDC);153 var diff = t === 0 ? 0 : receiveAndExtend(t);154 component.blockData[offset] = component.pred += diff;155 var k = 1;156 while (k < 64) {157 var rs = decodeHuffman(component.huffmanTableAC);158 var s = rs & 15,159 r = rs >> 4;160 if (s === 0) {161 if (r < 15) {162 break;163 }164 k += 16;165 continue;166 }167 k += r;168 var z = dctZigZag[k];169 component.blockData[offset + z] = receiveAndExtend(s);170 k++;171 }172 }173 function decodeDCFirst(component, offset) {174 var t = decodeHuffman(component.huffmanTableDC);175 var diff = t === 0 ? 0 : receiveAndExtend(t) << successive;176 component.blockData[offset] = component.pred += diff;177 }178 function decodeDCSuccessive(component, offset) {179 component.blockData[offset] |= readBit() << successive;180 }181 var eobrun = 0;182 function decodeACFirst(component, offset) {183 if (eobrun > 0) {184 eobrun--;185 return;186 }187 var k = spectralStart,188 e = spectralEnd;189 while (k <= e) {190 var rs = decodeHuffman(component.huffmanTableAC);191 var s = rs & 15,192 r = rs >> 4;193 if (s === 0) {194 if (r < 15) {195 eobrun = receive(r) + (1 << r) - 1;196 break;197 }198 k += 16;199 continue;200 }201 k += r;202 var z = dctZigZag[k];203 component.blockData[offset + z] = receiveAndExtend(s) * (1 << successive);204 k++;205 }206 }207 var successiveACState = 0,208 successiveACNextValue;209 function decodeACSuccessive(component, offset) {210 var k = spectralStart;211 var e = spectralEnd;212 var r = 0;213 var s;214 var rs;215 while (k <= e) {216 var offsetZ = offset + dctZigZag[k];217 var sign = component.blockData[offsetZ] < 0 ? -1 : 1;218 switch (successiveACState) {219 case 0:220 rs = decodeHuffman(component.huffmanTableAC);221 s = rs & 15;222 r = rs >> 4;223 if (s === 0) {224 if (r < 15) {225 eobrun = receive(r) + (1 << r);226 successiveACState = 4;227 } else {228 r = 16;229 successiveACState = 1;230 }231 } else {232 if (s !== 1) {233 throw new JpegError('invalid ACn encoding');234 }235 successiveACNextValue = receiveAndExtend(s);236 successiveACState = r ? 2 : 3;237 }238 continue;239 case 1:240 case 2:241 if (component.blockData[offsetZ]) {242 component.blockData[offsetZ] += sign * (readBit() << successive);243 } else {244 r--;245 if (r === 0) {246 successiveACState = successiveACState === 2 ? 3 : 0;247 }248 }249 break;250 case 3:251 if (component.blockData[offsetZ]) {252 component.blockData[offsetZ] += sign * (readBit() << successive);253 } else {254 component.blockData[offsetZ] = successiveACNextValue << successive;255 successiveACState = 0;256 }257 break;258 case 4:259 if (component.blockData[offsetZ]) {260 component.blockData[offsetZ] += sign * (readBit() << successive);261 }262 break;263 }264 k++;265 }266 if (successiveACState === 4) {267 eobrun--;268 if (eobrun === 0) {269 successiveACState = 0;270 }271 }272 }273 function decodeMcu(component, decode, mcu, row, col) {274 var mcuRow = mcu / mcusPerLine | 0;275 var mcuCol = mcu % mcusPerLine;276 var blockRow = mcuRow * component.v + row;277 var blockCol = mcuCol * component.h + col;278 var offset = getBlockBufferOffset(component, blockRow, blockCol);279 decode(component, offset);280 }281 function decodeBlock(component, decode, mcu) {282 var blockRow = mcu / component.blocksPerLine | 0;283 var blockCol = mcu % component.blocksPerLine;284 var offset = getBlockBufferOffset(component, blockRow, blockCol);285 decode(component, offset);286 }287 var componentsLength = components.length;288 var component, i, j, k, n;289 var decodeFn;290 if (progressive) {291 if (spectralStart === 0) {292 decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;293 } else {294 decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;295 }296 } else {297 decodeFn = decodeBaseline;298 }299 var mcu = 0,300 fileMarker;301 var mcuExpected;302 if (componentsLength === 1) {303 mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;304 } else {305 mcuExpected = mcusPerLine * frame.mcusPerColumn;306 }307 var h, v;308 while (mcu < mcuExpected) {309 var mcuToRead = resetInterval ? Math.min(mcuExpected - mcu, resetInterval) : mcuExpected;310 for (i = 0; i < componentsLength; i++) {311 components[i].pred = 0;312 }313 eobrun = 0;314 if (componentsLength === 1) {315 component = components[0];316 for (n = 0; n < mcuToRead; n++) {317 decodeBlock(component, decodeFn, mcu);318 mcu++;319 }320 } else {321 for (n = 0; n < mcuToRead; n++) {322 for (i = 0; i < componentsLength; i++) {323 component = components[i];324 h = component.h;325 v = component.v;326 for (j = 0; j < v; j++) {327 for (k = 0; k < h; k++) {328 decodeMcu(component, decodeFn, mcu, j, k);329 }330 }331 }332 mcu++;333 }334 }335 bitsCount = 0;336 fileMarker = findNextFileMarker(data, offset);337 if (fileMarker && fileMarker.invalid) {338 (0, _util.warn)('decodeScan - unexpected MCU data, next marker is: ' + fileMarker.invalid);339 offset = fileMarker.offset;340 }341 var marker = fileMarker && fileMarker.marker;342 if (!marker || marker <= 0xFF00) {343 throw new JpegError('marker was not found');344 }345 if (marker >= 0xFFD0 && marker <= 0xFFD7) {346 offset += 2;347 } else {348 break;349 }350 }351 fileMarker = findNextFileMarker(data, offset);352 if (fileMarker && fileMarker.invalid) {353 (0, _util.warn)('decodeScan - unexpected Scan data, next marker is: ' + fileMarker.invalid);354 offset = fileMarker.offset;355 }356 return offset - startOffset;357 }358 function quantizeAndInverse(component, blockBufferOffset, p) {359 var qt = component.quantizationTable,360 blockData = component.blockData;361 var v0, v1, v2, v3, v4, v5, v6, v7;362 var p0, p1, p2, p3, p4, p5, p6, p7;363 var t;364 if (!qt) {365 throw new JpegError('missing required Quantization Table.');366 }367 for (var row = 0; row < 64; row += 8) {368 p0 = blockData[blockBufferOffset + row];369 p1 = blockData[blockBufferOffset + row + 1];370 p2 = blockData[blockBufferOffset + row + 2];371 p3 = blockData[blockBufferOffset + row + 3];372 p4 = blockData[blockBufferOffset + row + 4];373 p5 = blockData[blockBufferOffset + row + 5];374 p6 = blockData[blockBufferOffset + row + 6];375 p7 = blockData[blockBufferOffset + row + 7];376 p0 *= qt[row];377 if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {378 t = dctSqrt2 * p0 + 512 >> 10;379 p[row] = t;380 p[row + 1] = t;381 p[row + 2] = t;382 p[row + 3] = t;383 p[row + 4] = t;384 p[row + 5] = t;385 p[row + 6] = t;386 p[row + 7] = t;387 continue;388 }389 p1 *= qt[row + 1];390 p2 *= qt[row + 2];391 p3 *= qt[row + 3];392 p4 *= qt[row + 4];393 p5 *= qt[row + 5];394 p6 *= qt[row + 6];395 p7 *= qt[row + 7];396 v0 = dctSqrt2 * p0 + 128 >> 8;397 v1 = dctSqrt2 * p4 + 128 >> 8;398 v2 = p2;399 v3 = p6;400 v4 = dctSqrt1d2 * (p1 - p7) + 128 >> 8;401 v7 = dctSqrt1d2 * (p1 + p7) + 128 >> 8;402 v5 = p3 << 4;403 v6 = p5 << 4;404 v0 = v0 + v1 + 1 >> 1;405 v1 = v0 - v1;406 t = v2 * dctSin6 + v3 * dctCos6 + 128 >> 8;407 v2 = v2 * dctCos6 - v3 * dctSin6 + 128 >> 8;408 v3 = t;409 v4 = v4 + v6 + 1 >> 1;410 v6 = v4 - v6;411 v7 = v7 + v5 + 1 >> 1;412 v5 = v7 - v5;413 v0 = v0 + v3 + 1 >> 1;414 v3 = v0 - v3;415 v1 = v1 + v2 + 1 >> 1;416 v2 = v1 - v2;417 t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;418 v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;419 v7 = t;420 t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;421 v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;422 v6 = t;423 p[row] = v0 + v7;424 p[row + 7] = v0 - v7;425 p[row + 1] = v1 + v6;426 p[row + 6] = v1 - v6;427 p[row + 2] = v2 + v5;428 p[row + 5] = v2 - v5;429 p[row + 3] = v3 + v4;430 p[row + 4] = v3 - v4;431 }432 for (var col = 0; col < 8; ++col) {433 p0 = p[col];434 p1 = p[col + 8];435 p2 = p[col + 16];436 p3 = p[col + 24];437 p4 = p[col + 32];438 p5 = p[col + 40];439 p6 = p[col + 48];440 p7 = p[col + 56];441 if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {442 t = dctSqrt2 * p0 + 8192 >> 14;443 t = t < -2040 ? 0 : t >= 2024 ? 255 : t + 2056 >> 4;444 blockData[blockBufferOffset + col] = t;445 blockData[blockBufferOffset + col + 8] = t;446 blockData[blockBufferOffset + col + 16] = t;447 blockData[blockBufferOffset + col + 24] = t;448 blockData[blockBufferOffset + col + 32] = t;449 blockData[blockBufferOffset + col + 40] = t;450 blockData[blockBufferOffset + col + 48] = t;451 blockData[blockBufferOffset + col + 56] = t;452 continue;453 }454 v0 = dctSqrt2 * p0 + 2048 >> 12;455 v1 = dctSqrt2 * p4 + 2048 >> 12;456 v2 = p2;457 v3 = p6;458 v4 = dctSqrt1d2 * (p1 - p7) + 2048 >> 12;459 v7 = dctSqrt1d2 * (p1 + p7) + 2048 >> 12;460 v5 = p3;461 v6 = p5;462 v0 = (v0 + v1 + 1 >> 1) + 4112;463 v1 = v0 - v1;464 t = v2 * dctSin6 + v3 * dctCos6 + 2048 >> 12;465 v2 = v2 * dctCos6 - v3 * dctSin6 + 2048 >> 12;466 v3 = t;467 v4 = v4 + v6 + 1 >> 1;468 v6 = v4 - v6;469 v7 = v7 + v5 + 1 >> 1;470 v5 = v7 - v5;471 v0 = v0 + v3 + 1 >> 1;472 v3 = v0 - v3;473 v1 = v1 + v2 + 1 >> 1;474 v2 = v1 - v2;475 t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;476 v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;477 v7 = t;478 t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;479 v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;480 v6 = t;481 p0 = v0 + v7;482 p7 = v0 - v7;483 p1 = v1 + v6;484 p6 = v1 - v6;485 p2 = v2 + v5;486 p5 = v2 - v5;487 p3 = v3 + v4;488 p4 = v3 - v4;489 p0 = p0 < 16 ? 0 : p0 >= 4080 ? 255 : p0 >> 4;490 p1 = p1 < 16 ? 0 : p1 >= 4080 ? 255 : p1 >> 4;491 p2 = p2 < 16 ? 0 : p2 >= 4080 ? 255 : p2 >> 4;492 p3 = p3 < 16 ? 0 : p3 >= 4080 ? 255 : p3 >> 4;493 p4 = p4 < 16 ? 0 : p4 >= 4080 ? 255 : p4 >> 4;494 p5 = p5 < 16 ? 0 : p5 >= 4080 ? 255 : p5 >> 4;495 p6 = p6 < 16 ? 0 : p6 >= 4080 ? 255 : p6 >> 4;496 p7 = p7 < 16 ? 0 : p7 >= 4080 ? 255 : p7 >> 4;497 blockData[blockBufferOffset + col] = p0;498 blockData[blockBufferOffset + col + 8] = p1;499 blockData[blockBufferOffset + col + 16] = p2;500 blockData[blockBufferOffset + col + 24] = p3;501 blockData[blockBufferOffset + col + 32] = p4;502 blockData[blockBufferOffset + col + 40] = p5;503 blockData[blockBufferOffset + col + 48] = p6;504 blockData[blockBufferOffset + col + 56] = p7;505 }506 }507 function buildComponentData(frame, component) {508 var blocksPerLine = component.blocksPerLine;509 var blocksPerColumn = component.blocksPerColumn;510 var computationBuffer = new Int16Array(64);511 for (var blockRow = 0; blockRow < blocksPerColumn; blockRow++) {512 for (var blockCol = 0; blockCol < blocksPerLine; blockCol++) {513 var offset = getBlockBufferOffset(component, blockRow, blockCol);514 quantizeAndInverse(component, offset, computationBuffer);515 }516 }517 return component.blockData;518 }519 function findNextFileMarker(data, currentPos, startPos) {520 function peekUint16(pos) {521 return data[pos] << 8 | data[pos + 1];522 }523 var maxPos = data.length - 1;524 var newPos = startPos < currentPos ? startPos : currentPos;525 if (currentPos >= maxPos) {526 return null;527 }528 var currentMarker = peekUint16(currentPos);529 if (currentMarker >= 0xFFC0 && currentMarker <= 0xFFFE) {530 return {531 invalid: null,532 marker: currentMarker,533 offset: currentPos534 };535 }536 var newMarker = peekUint16(newPos);537 while (!(newMarker >= 0xFFC0 && newMarker <= 0xFFFE)) {538 if (++newPos >= maxPos) {539 return null;540 }541 newMarker = peekUint16(newPos);542 }543 return {544 invalid: currentMarker.toString(16),545 marker: newMarker,546 offset: newPos547 };548 }549 JpegImage.prototype = {550 parse: function parse(data) {551 function readUint16() {552 var value = data[offset] << 8 | data[offset + 1];553 offset += 2;554 return value;555 }556 function readDataBlock() {557 var length = readUint16();558 var endOffset = offset + length - 2;559 var fileMarker = findNextFileMarker(data, endOffset, offset);560 if (fileMarker && fileMarker.invalid) {561 (0, _util.warn)('readDataBlock - incorrect length, next marker is: ' + fileMarker.invalid);562 endOffset = fileMarker.offset;563 }564 var array = data.subarray(offset, endOffset);565 offset += array.length;566 return array;567 }568 function prepareComponents(frame) {569 var mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / frame.maxH);570 var mcusPerColumn = Math.ceil(frame.scanLines / 8 / frame.maxV);571 for (var i = 0; i < frame.components.length; i++) {572 component = frame.components[i];573 var blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / frame.maxH);574 var blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / frame.maxV);575 var blocksPerLineForMcu = mcusPerLine * component.h;576 var blocksPerColumnForMcu = mcusPerColumn * component.v;577 var blocksBufferSize = 64 * blocksPerColumnForMcu * (blocksPerLineForMcu + 1);578 component.blockData = new Int16Array(blocksBufferSize);579 component.blocksPerLine = blocksPerLine;580 component.blocksPerColumn = blocksPerColumn;581 }582 frame.mcusPerLine = mcusPerLine;583 frame.mcusPerColumn = mcusPerColumn;584 }585 var offset = 0;586 var jfif = null;587 var adobe = null;588 var frame, resetInterval;589 var quantizationTables = [];590 var huffmanTablesAC = [],591 huffmanTablesDC = [];592 var fileMarker = readUint16();593 if (fileMarker !== 0xFFD8) {594 throw new JpegError('SOI not found');595 }596 fileMarker = readUint16();597 while (fileMarker !== 0xFFD9) {598 var i, j, l;599 switch (fileMarker) {600 case 0xFFE0:601 case 0xFFE1:602 case 0xFFE2:603 case 0xFFE3:604 case 0xFFE4:605 case 0xFFE5:606 case 0xFFE6:607 case 0xFFE7:608 case 0xFFE8:609 case 0xFFE9:610 case 0xFFEA:611 case 0xFFEB:612 case 0xFFEC:613 case 0xFFED:614 case 0xFFEE:615 case 0xFFEF:616 case 0xFFFE:617 var appData = readDataBlock();618 if (fileMarker === 0xFFE0) {619 if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 && appData[3] === 0x46 && appData[4] === 0) {620 jfif = {621 version: {622 major: appData[5],623 minor: appData[6]624 },625 densityUnits: appData[7],626 xDensity: appData[8] << 8 | appData[9],627 yDensity: appData[10] << 8 | appData[11],628 thumbWidth: appData[12],629 thumbHeight: appData[13],630 thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])631 };632 }633 }634 if (fileMarker === 0xFFEE) {635 if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F && appData[3] === 0x62 && appData[4] === 0x65) {636 adobe = {637 version: appData[5] << 8 | appData[6],638 flags0: appData[7] << 8 | appData[8],639 flags1: appData[9] << 8 | appData[10],640 transformCode: appData[11]641 };642 }643 }644 break;645 case 0xFFDB:646 var quantizationTablesLength = readUint16();647 var quantizationTablesEnd = quantizationTablesLength + offset - 2;648 var z;649 while (offset < quantizationTablesEnd) {650 var quantizationTableSpec = data[offset++];651 var tableData = new Uint16Array(64);652 if (quantizationTableSpec >> 4 === 0) {653 for (j = 0; j < 64; j++) {654 z = dctZigZag[j];655 tableData[z] = data[offset++];656 }657 } else if (quantizationTableSpec >> 4 === 1) {658 for (j = 0; j < 64; j++) {659 z = dctZigZag[j];660 tableData[z] = readUint16();661 }662 } else {663 throw new JpegError('DQT - invalid table spec');664 }665 quantizationTables[quantizationTableSpec & 15] = tableData;666 }667 break;668 case 0xFFC0:669 case 0xFFC1:670 case 0xFFC2:671 if (frame) {672 throw new JpegError('Only single frame JPEGs supported');673 }674 readUint16();675 frame = {};676 frame.extended = fileMarker === 0xFFC1;677 frame.progressive = fileMarker === 0xFFC2;678 frame.precision = data[offset++];679 frame.scanLines = readUint16();680 frame.samplesPerLine = readUint16();681 frame.components = [];682 frame.componentIds = {};683 var componentsCount = data[offset++],684 componentId;685 var maxH = 0,686 maxV = 0;687 for (i = 0; i < componentsCount; i++) {688 componentId = data[offset];689 var h = data[offset + 1] >> 4;690 var v = data[offset + 1] & 15;691 if (maxH < h) {692 maxH = h;693 }694 if (maxV < v) {695 maxV = v;696 }697 var qId = data[offset + 2];698 l = frame.components.push({699 h: h,700 v: v,701 quantizationId: qId,702 quantizationTable: null703 });704 frame.componentIds[componentId] = l - 1;705 offset += 3;706 }707 frame.maxH = maxH;708 frame.maxV = maxV;709 prepareComponents(frame);710 break;711 case 0xFFC4:712 var huffmanLength = readUint16();713 for (i = 2; i < huffmanLength;) {714 var huffmanTableSpec = data[offset++];715 var codeLengths = new Uint8Array(16);716 var codeLengthSum = 0;717 for (j = 0; j < 16; j++, offset++) {718 codeLengthSum += codeLengths[j] = data[offset];719 }720 var huffmanValues = new Uint8Array(codeLengthSum);721 for (j = 0; j < codeLengthSum; j++, offset++) {722 huffmanValues[j] = data[offset];723 }724 i += 17 + codeLengthSum;725 (huffmanTableSpec >> 4 === 0 ? huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] = buildHuffmanTable(codeLengths, huffmanValues);726 }727 break;728 case 0xFFDD:729 readUint16();730 resetInterval = readUint16();731 break;732 case 0xFFDA:733 readUint16();734 var selectorsCount = data[offset++];735 var components = [],736 component;737 for (i = 0; i < selectorsCount; i++) {738 var componentIndex = frame.componentIds[data[offset++]];739 component = frame.components[componentIndex];740 var tableSpec = data[offset++];741 component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];742 component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];743 components.push(component);744 }745 var spectralStart = data[offset++];746 var spectralEnd = data[offset++];747 var successiveApproximation = data[offset++];748 var processed = decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successiveApproximation >> 4, successiveApproximation & 15);749 offset += processed;750 break;751 case 0xFFFF:752 if (data[offset] !== 0xFF) {753 offset--;754 }755 break;756 default:757 if (data[offset - 3] === 0xFF && data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {758 offset -= 3;759 break;760 }761 throw new JpegError('unknown marker ' + fileMarker.toString(16));762 }763 fileMarker = readUint16();764 }765 this.width = frame.samplesPerLine;766 this.height = frame.scanLines;767 this.jfif = jfif;768 this.adobe = adobe;769 this.components = [];770 for (i = 0; i < frame.components.length; i++) {771 component = frame.components[i];772 var quantizationTable = quantizationTables[component.quantizationId];773 if (quantizationTable) {774 component.quantizationTable = quantizationTable;775 }776 this.components.push({777 output: buildComponentData(frame, component),778 scaleX: component.h / frame.maxH,779 scaleY: component.v / frame.maxV,780 blocksPerLine: component.blocksPerLine,781 blocksPerColumn: component.blocksPerColumn782 });783 }784 this.numComponents = this.components.length;785 },786 _getLinearizedBlockData: function getLinearizedBlockData(width, height) {787 var scaleX = this.width / width,788 scaleY = this.height / height;789 var component, componentScaleX, componentScaleY, blocksPerScanline;790 var x, y, i, j, k;791 var index;792 var offset = 0;793 var output;794 var numComponents = this.components.length;795 var dataLength = width * height * numComponents;796 var data = new Uint8ClampedArray(dataLength);797 var xScaleBlockOffset = new Uint32Array(width);798 var mask3LSB = 0xfffffff8;799 for (i = 0; i < numComponents; i++) {800 component = this.components[i];801 componentScaleX = component.scaleX * scaleX;802 componentScaleY = component.scaleY * scaleY;803 offset = i;804 output = component.output;805 blocksPerScanline = component.blocksPerLine + 1 << 3;806 for (x = 0; x < width; x++) {807 j = 0 | x * componentScaleX;808 xScaleBlockOffset[x] = (j & mask3LSB) << 3 | j & 7;809 }810 for (y = 0; y < height; y++) {811 j = 0 | y * componentScaleY;812 index = blocksPerScanline * (j & mask3LSB) | (j & 7) << 3;813 for (x = 0; x < width; x++) {814 data[offset] = output[index + xScaleBlockOffset[x]];815 offset += numComponents;816 }817 }818 }819 var transform = this.decodeTransform;820 if (transform) {821 for (i = 0; i < dataLength;) {822 for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {823 data[i] = (data[i] * transform[k] >> 8) + transform[k + 1];824 }825 }826 }827 return data;828 },829 _isColorConversionNeeded: function _isColorConversionNeeded() {830 if (this.adobe) {831 return !!this.adobe.transformCode;832 }833 if (this.numComponents === 3) {834 if (this.colorTransform === 0) {835 return false;836 }837 return true;838 }839 if (this.colorTransform === 1) {840 return true;841 }842 return false;843 },844 _convertYccToRgb: function convertYccToRgb(data) {845 var Y, Cb, Cr;846 for (var i = 0, length = data.length; i < length; i += 3) {847 Y = data[i];848 Cb = data[i + 1];849 Cr = data[i + 2];850 data[i] = Y - 179.456 + 1.402 * Cr;851 data[i + 1] = Y + 135.459 - 0.344 * Cb - 0.714 * Cr;852 data[i + 2] = Y - 226.816 + 1.772 * Cb;853 }854 return data;855 },856 _convertYcckToRgb: function convertYcckToRgb(data) {857 var Y, Cb, Cr, k;858 var offset = 0;859 for (var i = 0, length = data.length; i < length; i += 4) {860 Y = data[i];861 Cb = data[i + 1];862 Cr = data[i + 2];863 k = data[i + 3];864 data[offset++] = -122.67195406894 + Cb * (-6.60635669420364e-5 * Cb + 0.000437130475926232 * Cr - 5.4080610064599e-5 * Y + 0.00048449797120281 * k - 0.154362151871126) + Cr * (-0.000957964378445773 * Cr + 0.000817076911346625 * Y - 0.00477271405408747 * k + 1.53380253221734) + Y * (0.000961250184130688 * Y - 0.00266257332283933 * k + 0.48357088451265) + k * (-0.000336197177618394 * k + 0.484791561490776);865 data[offset++] = 107.268039397724 + Cb * (2.19927104525741e-5 * Cb - 0.000640992018297945 * Cr + 0.000659397001245577 * Y + 0.000426105652938837 * k - 0.176491792462875) + Cr * (-0.000778269941513683 * Cr + 0.00130872261408275 * Y + 0.000770482631801132 * k - 0.151051492775562) + Y * (0.00126935368114843 * Y - 0.00265090189010898 * k + 0.25802910206845) + k * (-0.000318913117588328 * k - 0.213742400323665);866 data[offset++] = -20.810012546947 + Cb * (-0.000570115196973677 * Cb - 2.63409051004589e-5 * Cr + 0.0020741088115012 * Y - 0.00288260236853442 * k + 0.814272968359295) + Cr * (-1.53496057440975e-5 * Cr - 0.000132689043961446 * Y + 0.000560833691242812 * k - 0.195152027534049) + Y * (0.00174418132927582 * Y - 0.00255243321439347 * k + 0.116935020465145) + k * (-0.000343531996510555 * k + 0.24165260232407);867 }868 return data;869 },870 _convertYcckToCmyk: function convertYcckToCmyk(data) {871 var Y, Cb, Cr;872 for (var i = 0, length = data.length; i < length; i += 4) {873 Y = data[i];874 Cb = data[i + 1];875 Cr = data[i + 2];876 data[i] = 434.456 - Y - 1.402 * Cr;877 data[i + 1] = 119.541 - Y + 0.344 * Cb + 0.714 * Cr;878 data[i + 2] = 481.816 - Y - 1.772 * Cb;879 }880 return data;881 },882 _convertCmykToRgb: function convertCmykToRgb(data) {883 var c, m, y, k;884 var offset = 0;885 var scale = 1 / 255;886 for (var i = 0, length = data.length; i < length; i += 4) {887 c = data[i] * scale;888 m = data[i + 1] * scale;889 y = data[i + 2] * scale;890 k = data[i + 3] * scale;891 data[offset++] = 255 + c * (-4.387332384609988 * c + 54.48615194189176 * m + 18.82290502165302 * y + 212.25662451639585 * k - 285.2331026137004) + m * (1.7149763477362134 * m - 5.6096736904047315 * y - 17.873870861415444 * k - 5.497006427196366) + y * (-2.5217340131683033 * y - 21.248923337353073 * k + 17.5119270841813) - k * (21.86122147463605 * k + 189.48180835922747);892 data[offset++] = 255 + c * (8.841041422036149 * c + 60.118027045597366 * m + 6.871425592049007 * y + 31.159100130055922 * k - 79.2970844816548) + m * (-15.310361306967817 * m + 17.575251261109482 * y + 131.35250912493976 * k - 190.9453302588951) + y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) - k * (20.737325471181034 * k + 187.80453709719578);893 data[offset++] = 255 + c * (0.8842522430003296 * c + 8.078677503112928 * m + 30.89978309703729 * y - 0.23883238689178934 * k - 14.183576799673286) + m * (10.49593273432072 * m + 63.02378494754052 * y + 50.606957656360734 * k - 112.23884253719248) + y * (0.03296041114873217 * y + 115.60384449646641 * k - 193.58209356861505) - k * (22.33816807309886 * k + 180.12613974708367);894 }895 return data;896 },897 getData: function getData(width, height, forceRGBoutput) {898 if (this.numComponents > 4) {899 throw new JpegError('Unsupported color mode');900 }901 var data = this._getLinearizedBlockData(width, height);902 if (this.numComponents === 1 && forceRGBoutput) {903 var dataLength = data.length;904 var rgbData = new Uint8ClampedArray(dataLength * 3);905 var offset = 0;906 for (var i = 0; i < dataLength; i++) {907 var grayColor = data[i];908 rgbData[offset++] = grayColor;909 rgbData[offset++] = grayColor;910 rgbData[offset++] = grayColor;911 }912 return rgbData;913 } else if (this.numComponents === 3 && this._isColorConversionNeeded()) {...

Full Screen

Full Screen

jpg.ts

Source:jpg.ts Github

copy

Full Screen

...19} : function(obj) {20 return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype ? 'symbol' : typeof obj;21};22let JpegError = function JpegErrorClosure() {23 function JpegError(msg) {24 this.message = 'JPEG error: ' + msg;25 }26 JpegError.prototype = new Error();27 JpegError.prototype.name = 'JpegError';28 JpegError.constructor = JpegError;29 return JpegError;30}();31let JpegImage = function JpegImageClosure() {32 let dctZigZag = new Uint8Array([0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63]);33 let dctCos1 = 4017;34 let dctSin1 = 799;35 let dctCos3 = 3406;36 let dctSin3 = 2276;37 let dctCos6 = 1567;38 let dctSin6 = 3784;39 let dctSqrt2 = 5793;40 let dctSqrt1d2 = 2896;41 function JpegImage() {42 this.decodeTransform = null;43 this.colorTransform = -1;44 }45 function buildHuffmanTable(codeLengths, values) {46 let k = 0,47 code = [],48 i,49 j,50 length = 16;51 while (length > 0 && !codeLengths[length - 1]) {52 length--;53 }54 code.push({55 children: [],56 index: 0,57 });58 let p = code[0],59 q;60 for (i = 0; i < length; i++) {61 for (j = 0; j < codeLengths[i]; j++) {62 p = code.pop();63 p.children[p.index] = values[k];64 while (p.index > 0) {65 p = code.pop();66 }67 p.index++;68 code.push(p);69 while (code.length <= i) {70 code.push(q = {71 children: [],72 index: 0,73 });74 p.children[p.index] = q.children;75 p = q;76 }77 k++;78 }79 if (i + 1 < length) {80 code.push(q = {81 children: [],82 index: 0,83 });84 p.children[p.index] = q.children;85 p = q;86 }87 }88 return code[0].children;89 }90 function getBlockBufferOffset(component, row, col) {91 return 64 * ((component.blocksPerLine + 1) * row + col);92 }93 function decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successivePrev, successive) {94 let mcusPerLine = frame.mcusPerLine;95 let progressive = frame.progressive;96 let startOffset = offset,97 bitsData = 0,98 bitsCount = 0;99 function readBit() {100 if (bitsCount > 0) {101 bitsCount--;102 return bitsData >> bitsCount & 1;103 }104 bitsData = data[offset++];105 if (bitsData === 0xFF) {106 let nextByte = data[offset++];107 if (nextByte) {108 throw new JpegError('unexpected marker ' + (bitsData << 8 | nextByte).toString(16));109 }110 }111 bitsCount = 7;112 return bitsData >>> 7;113 }114 function decodeHuffman(tree) {115 let node = tree;116 while (true) {117 node = node[readBit()];118 if (typeof node === 'number') {119 return node;120 }121 if ((typeof node === 'undefined' ? 'undefined' : _typeof(node)) !== 'object') {122 throw new JpegError('invalid huffman sequence');123 }124 }125 }126 function receive(length) {127 let n = 0;128 while (length > 0) {129 n = n << 1 | readBit();130 length--;131 }132 return n;133 }134 function receiveAndExtend(length) {135 if (length === 1) {136 return readBit() === 1 ? 1 : -1;137 }138 let n = receive(length);139 if (n >= 1 << length - 1) {140 return n;141 }142 return n + (-1 << length) + 1;143 }144 function decodeBaseline(component, offset) {145 let t = decodeHuffman(component.huffmanTableDC);146 let diff = t === 0 ? 0 : receiveAndExtend(t);147 component.blockData[offset] = component.pred += diff;148 let k = 1;149 while (k < 64) {150 let rs = decodeHuffman(component.huffmanTableAC);151 let s = rs & 15,152 r = rs >> 4;153 if (s === 0) {154 if (r < 15) {155 break;156 }157 k += 16;158 continue;159 }160 k += r;161 let z = dctZigZag[k];162 component.blockData[offset + z] = receiveAndExtend(s);163 k++;164 }165 }166 function decodeDCFirst(component, offset) {167 let t = decodeHuffman(component.huffmanTableDC);168 let diff = t === 0 ? 0 : receiveAndExtend(t) << successive;169 component.blockData[offset] = component.pred += diff;170 }171 function decodeDCSuccessive(component, offset) {172 component.blockData[offset] |= readBit() << successive;173 }174 let eobrun = 0;175 function decodeACFirst(component, offset) {176 if (eobrun > 0) {177 eobrun--;178 return;179 }180 let k = spectralStart,181 e = spectralEnd;182 while (k <= e) {183 let rs = decodeHuffman(component.huffmanTableAC);184 let s = rs & 15,185 r = rs >> 4;186 if (s === 0) {187 if (r < 15) {188 eobrun = receive(r) + (1 << r) - 1;189 break;190 }191 k += 16;192 continue;193 }194 k += r;195 let z = dctZigZag[k];196 component.blockData[offset + z] = receiveAndExtend(s) * (1 << successive);197 k++;198 }199 }200 let successiveACState = 0,201 successiveACNextValue;202 function decodeACSuccessive(component, offset) {203 let k = spectralStart;204 let e = spectralEnd;205 let r = 0;206 let s;207 let rs;208 while (k <= e) {209 let z = dctZigZag[k];210 switch (successiveACState) {211 case 0:212 rs = decodeHuffman(component.huffmanTableAC);213 s = rs & 15;214 r = rs >> 4;215 if (s === 0) {216 if (r < 15) {217 eobrun = receive(r) + (1 << r);218 successiveACState = 4;219 } else {220 r = 16;221 successiveACState = 1;222 }223 } else {224 if (s !== 1) {225 throw new JpegError('invalid ACn encoding');226 }227 successiveACNextValue = receiveAndExtend(s);228 successiveACState = r ? 2 : 3;229 }230 continue;231 case 1:232 case 2:233 if (component.blockData[offset + z]) {234 component.blockData[offset + z] += readBit() << successive;235 } else {236 r--;237 if (r === 0) {238 successiveACState = successiveACState === 2 ? 3 : 0;239 }240 }241 break;242 case 3:243 if (component.blockData[offset + z]) {244 component.blockData[offset + z] += readBit() << successive;245 } else {246 component.blockData[offset + z] = successiveACNextValue << successive;247 successiveACState = 0;248 }249 break;250 case 4:251 if (component.blockData[offset + z]) {252 component.blockData[offset + z] += readBit() << successive;253 }254 break;255 }256 k++;257 }258 if (successiveACState === 4) {259 eobrun--;260 if (eobrun === 0) {261 successiveACState = 0;262 }263 }264 }265 function decodeMcu(component, decode, mcu, row, col) {266 let mcuRow = mcu / mcusPerLine | 0;267 let mcuCol = mcu % mcusPerLine;268 let blockRow = mcuRow * component.v + row;269 let blockCol = mcuCol * component.h + col;270 let offset = getBlockBufferOffset(component, blockRow, blockCol);271 decode(component, offset);272 }273 function decodeBlock(component, decode, mcu) {274 let blockRow = mcu / component.blocksPerLine | 0;275 let blockCol = mcu % component.blocksPerLine;276 let offset = getBlockBufferOffset(component, blockRow, blockCol);277 decode(component, offset);278 }279 let componentsLength = components.length;280 let component, i, j, k, n;281 let decodeFn;282 if (progressive) {283 if (spectralStart === 0) {284 decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;285 } else {286 decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;287 }288 } else {289 decodeFn = decodeBaseline;290 }291 let mcu = 0,292 fileMarker;293 let mcuExpected;294 if (componentsLength === 1) {295 mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;296 } else {297 mcuExpected = mcusPerLine * frame.mcusPerColumn;298 }299 let h, v;300 while (mcu < mcuExpected) {301 let mcuToRead = resetInterval ? Math.min(mcuExpected - mcu, resetInterval) : mcuExpected;302 for (i = 0; i < componentsLength; i++) {303 components[i].pred = 0;304 }305 eobrun = 0;306 if (componentsLength === 1) {307 component = components[0];308 for (n = 0; n < mcuToRead; n++) {309 decodeBlock(component, decodeFn, mcu);310 mcu++;311 }312 } else {313 for (n = 0; n < mcuToRead; n++) {314 for (i = 0; i < componentsLength; i++) {315 component = components[i];316 h = component.h;317 v = component.v;318 for (j = 0; j < v; j++) {319 for (k = 0; k < h; k++) {320 decodeMcu(component, decodeFn, mcu, j, k);321 }322 }323 }324 mcu++;325 }326 }327 bitsCount = 0;328 fileMarker = findNextFileMarker(data, offset, undefined);329 if (fileMarker && fileMarker.invalid) {330 // (0, _util.warn)('decodeScan - unexpected MCU data, next marker is: ' + fileMarker.invalid);331 offset = fileMarker.offset;332 }333 let marker = fileMarker && fileMarker.marker;334 if (!marker || marker <= 0xFF00) {335 throw new JpegError('marker was not found');336 }337 if (marker >= 0xFFD0 && marker <= 0xFFD7) {338 offset += 2;339 } else {340 break;341 }342 }343 fileMarker = findNextFileMarker(data, offset, undefined);344 if (fileMarker && fileMarker.invalid) {345 // (0, _util.warn)('decodeScan - unexpected Scan data, next marker is: ' + fileMarker.invalid);346 offset = fileMarker.offset;347 }348 return offset - startOffset;349 }350 function quantizeAndInverse(component, blockBufferOffset, p) {351 let qt = component.quantizationTable,352 blockData = component.blockData;353 let v0, v1, v2, v3, v4, v5, v6, v7;354 let p0, p1, p2, p3, p4, p5, p6, p7;355 let t;356 if (!qt) {357 throw new JpegError('missing required Quantization Table.');358 }359 for (let row = 0; row < 64; row += 8) {360 p0 = blockData[blockBufferOffset + row];361 p1 = blockData[blockBufferOffset + row + 1];362 p2 = blockData[blockBufferOffset + row + 2];363 p3 = blockData[blockBufferOffset + row + 3];364 p4 = blockData[blockBufferOffset + row + 4];365 p5 = blockData[blockBufferOffset + row + 5];366 p6 = blockData[blockBufferOffset + row + 6];367 p7 = blockData[blockBufferOffset + row + 7];368 p0 *= qt[row];369 if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {370 t = dctSqrt2 * p0 + 512 >> 10;371 p[row] = t;372 p[row + 1] = t;373 p[row + 2] = t;374 p[row + 3] = t;375 p[row + 4] = t;376 p[row + 5] = t;377 p[row + 6] = t;378 p[row + 7] = t;379 continue;380 }381 p1 *= qt[row + 1];382 p2 *= qt[row + 2];383 p3 *= qt[row + 3];384 p4 *= qt[row + 4];385 p5 *= qt[row + 5];386 p6 *= qt[row + 6];387 p7 *= qt[row + 7];388 v0 = dctSqrt2 * p0 + 128 >> 8;389 v1 = dctSqrt2 * p4 + 128 >> 8;390 v2 = p2;391 v3 = p6;392 v4 = dctSqrt1d2 * (p1 - p7) + 128 >> 8;393 v7 = dctSqrt1d2 * (p1 + p7) + 128 >> 8;394 v5 = p3 << 4;395 v6 = p5 << 4;396 v0 = v0 + v1 + 1 >> 1;397 v1 = v0 - v1;398 t = v2 * dctSin6 + v3 * dctCos6 + 128 >> 8;399 v2 = v2 * dctCos6 - v3 * dctSin6 + 128 >> 8;400 v3 = t;401 v4 = v4 + v6 + 1 >> 1;402 v6 = v4 - v6;403 v7 = v7 + v5 + 1 >> 1;404 v5 = v7 - v5;405 v0 = v0 + v3 + 1 >> 1;406 v3 = v0 - v3;407 v1 = v1 + v2 + 1 >> 1;408 v2 = v1 - v2;409 t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;410 v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;411 v7 = t;412 t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;413 v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;414 v6 = t;415 p[row] = v0 + v7;416 p[row + 7] = v0 - v7;417 p[row + 1] = v1 + v6;418 p[row + 6] = v1 - v6;419 p[row + 2] = v2 + v5;420 p[row + 5] = v2 - v5;421 p[row + 3] = v3 + v4;422 p[row + 4] = v3 - v4;423 }424 for (let col = 0; col < 8; ++col) {425 p0 = p[col];426 p1 = p[col + 8];427 p2 = p[col + 16];428 p3 = p[col + 24];429 p4 = p[col + 32];430 p5 = p[col + 40];431 p6 = p[col + 48];432 p7 = p[col + 56];433 if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {434 t = dctSqrt2 * p0 + 8192 >> 14;435 t = t < -2040 ? 0 : t >= 2024 ? 255 : t + 2056 >> 4;436 blockData[blockBufferOffset + col] = t;437 blockData[blockBufferOffset + col + 8] = t;438 blockData[blockBufferOffset + col + 16] = t;439 blockData[blockBufferOffset + col + 24] = t;440 blockData[blockBufferOffset + col + 32] = t;441 blockData[blockBufferOffset + col + 40] = t;442 blockData[blockBufferOffset + col + 48] = t;443 blockData[blockBufferOffset + col + 56] = t;444 continue;445 }446 v0 = dctSqrt2 * p0 + 2048 >> 12;447 v1 = dctSqrt2 * p4 + 2048 >> 12;448 v2 = p2;449 v3 = p6;450 v4 = dctSqrt1d2 * (p1 - p7) + 2048 >> 12;451 v7 = dctSqrt1d2 * (p1 + p7) + 2048 >> 12;452 v5 = p3;453 v6 = p5;454 v0 = (v0 + v1 + 1 >> 1) + 4112;455 v1 = v0 - v1;456 t = v2 * dctSin6 + v3 * dctCos6 + 2048 >> 12;457 v2 = v2 * dctCos6 - v3 * dctSin6 + 2048 >> 12;458 v3 = t;459 v4 = v4 + v6 + 1 >> 1;460 v6 = v4 - v6;461 v7 = v7 + v5 + 1 >> 1;462 v5 = v7 - v5;463 v0 = v0 + v3 + 1 >> 1;464 v3 = v0 - v3;465 v1 = v1 + v2 + 1 >> 1;466 v2 = v1 - v2;467 t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;468 v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;469 v7 = t;470 t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;471 v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;472 v6 = t;473 p0 = v0 + v7;474 p7 = v0 - v7;475 p1 = v1 + v6;476 p6 = v1 - v6;477 p2 = v2 + v5;478 p5 = v2 - v5;479 p3 = v3 + v4;480 p4 = v3 - v4;481 p0 = p0 < 16 ? 0 : p0 >= 4080 ? 255 : p0 >> 4;482 p1 = p1 < 16 ? 0 : p1 >= 4080 ? 255 : p1 >> 4;483 p2 = p2 < 16 ? 0 : p2 >= 4080 ? 255 : p2 >> 4;484 p3 = p3 < 16 ? 0 : p3 >= 4080 ? 255 : p3 >> 4;485 p4 = p4 < 16 ? 0 : p4 >= 4080 ? 255 : p4 >> 4;486 p5 = p5 < 16 ? 0 : p5 >= 4080 ? 255 : p5 >> 4;487 p6 = p6 < 16 ? 0 : p6 >= 4080 ? 255 : p6 >> 4;488 p7 = p7 < 16 ? 0 : p7 >= 4080 ? 255 : p7 >> 4;489 blockData[blockBufferOffset + col] = p0;490 blockData[blockBufferOffset + col + 8] = p1;491 blockData[blockBufferOffset + col + 16] = p2;492 blockData[blockBufferOffset + col + 24] = p3;493 blockData[blockBufferOffset + col + 32] = p4;494 blockData[blockBufferOffset + col + 40] = p5;495 blockData[blockBufferOffset + col + 48] = p6;496 blockData[blockBufferOffset + col + 56] = p7;497 }498 }499 function buildComponentData(frame, component) {500 let blocksPerLine = component.blocksPerLine;501 let blocksPerColumn = component.blocksPerColumn;502 let computationBuffer = new Int16Array(64);503 for (let blockRow = 0; blockRow < blocksPerColumn; blockRow++) {504 for (let blockCol = 0; blockCol < blocksPerLine; blockCol++) {505 let offset = getBlockBufferOffset(component, blockRow, blockCol);506 quantizeAndInverse(component, offset, computationBuffer);507 }508 }509 return component.blockData;510 }511 function clamp0to255(a) {512 return a <= 0 ? 0 : a >= 255 ? 255 : a;513 }514 function findNextFileMarker(data, currentPos, startPos) {515 function peekUint16(pos) {516 return data[pos] << 8 | data[pos + 1];517 }518 let maxPos = data.length - 1;519 let newPos = startPos < currentPos ? startPos : currentPos;520 if (currentPos >= maxPos) {521 return null;522 }523 let currentMarker = peekUint16(currentPos);524 if (currentMarker >= 0xFFC0 && currentMarker <= 0xFFFE) {525 return {526 invalid: null,527 marker: currentMarker,528 offset: currentPos,529 };530 }531 let newMarker = peekUint16(newPos);532 while (!(newMarker >= 0xFFC0 && newMarker <= 0xFFFE)) {533 if (++newPos >= maxPos) {534 return null;535 }536 newMarker = peekUint16(newPos);537 }538 return {539 invalid: currentMarker.toString(16),540 marker: newMarker,541 offset: newPos,542 };543 }544 JpegImage.prototype = {545 parse: function parse(data) {546 function readUint16() {547 let value = data[offset] << 8 | data[offset + 1];548 offset += 2;549 return value;550 }551 function readDataBlock() {552 let length = readUint16();553 let endOffset = offset + length - 2;554 let fileMarker = findNextFileMarker(data, endOffset, offset);555 if (fileMarker && fileMarker.invalid) {556 // (0, _util.warn)('readDataBlock - incorrect length, next marker is: ' + fileMarker.invalid);557 endOffset = fileMarker.offset;558 }559 let array = data.subarray(offset, endOffset);560 offset += array.length;561 return array;562 }563 function prepareComponents(frame) {564 let mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / frame.maxH);565 let mcusPerColumn = Math.ceil(frame.scanLines / 8 / frame.maxV);566 for (let i = 0; i < frame.components.length; i++) {567 component = frame.components[i];568 let blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / frame.maxH);569 let blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / frame.maxV);570 let blocksPerLineForMcu = mcusPerLine * component.h;571 let blocksPerColumnForMcu = mcusPerColumn * component.v;572 let blocksBufferSize = 64 * blocksPerColumnForMcu * (blocksPerLineForMcu + 1);573 component.blockData = new Int16Array(blocksBufferSize);574 component.blocksPerLine = blocksPerLine;575 component.blocksPerColumn = blocksPerColumn;576 }577 frame.mcusPerLine = mcusPerLine;578 frame.mcusPerColumn = mcusPerColumn;579 }580 var offset = 0;581 let jfif = null;582 let adobe = null;583 let frame, resetInterval;584 let quantizationTables = [];585 let huffmanTablesAC = [],586 huffmanTablesDC = [];587 let fileMarker = readUint16();588 if (fileMarker !== 0xFFD8) {589 throw new JpegError('SOI not found');590 }591 fileMarker = readUint16();592 while (fileMarker !== 0xFFD9) {593 var i, j, l;594 switch (fileMarker) {595 case 0xFFE0:596 case 0xFFE1:597 case 0xFFE2:598 case 0xFFE3:599 case 0xFFE4:600 case 0xFFE5:601 case 0xFFE6:602 case 0xFFE7:603 case 0xFFE8:604 case 0xFFE9:605 case 0xFFEA:606 case 0xFFEB:607 case 0xFFEC:608 case 0xFFED:609 case 0xFFEE:610 case 0xFFEF:611 case 0xFFFE:612 var appData = readDataBlock();613 if (fileMarker === 0xFFE0) {614 if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 && appData[3] === 0x46 && appData[4] === 0) {615 jfif = {616 version: {617 major: appData[5],618 minor: appData[6],619 },620 densityUnits: appData[7],621 xDensity: appData[8] << 8 | appData[9],622 yDensity: appData[10] << 8 | appData[11],623 thumbWidth: appData[12],624 thumbHeight: appData[13],625 thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13]),626 };627 }628 }629 if (fileMarker === 0xFFEE) {630 if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F && appData[3] === 0x62 && appData[4] === 0x65) {631 adobe = {632 version: appData[5] << 8 | appData[6],633 flags0: appData[7] << 8 | appData[8],634 flags1: appData[9] << 8 | appData[10],635 transformCode: appData[11],636 };637 }638 }639 break;640 case 0xFFDB:641 var quantizationTablesLength = readUint16();642 var quantizationTablesEnd = quantizationTablesLength + offset - 2;643 var z;644 while (offset < quantizationTablesEnd) {645 let quantizationTableSpec = data[offset++];646 let tableData = new Uint16Array(64);647 if (quantizationTableSpec >> 4 === 0) {648 for (j = 0; j < 64; j++) {649 z = dctZigZag[j];650 tableData[z] = data[offset++];651 }652 } else if (quantizationTableSpec >> 4 === 1) {653 for (j = 0; j < 64; j++) {654 z = dctZigZag[j];655 tableData[z] = readUint16();656 }657 } else {658 throw new JpegError('DQT - invalid table spec');659 }660 quantizationTables[quantizationTableSpec & 15] = tableData;661 }662 break;663 case 0xFFC0:664 case 0xFFC1:665 case 0xFFC2:666 if (frame) {667 throw new JpegError('Only single frame JPEGs supported');668 }669 readUint16();670 frame = {};671 frame.extended = fileMarker === 0xFFC1;672 frame.progressive = fileMarker === 0xFFC2;673 frame.precision = data[offset++];674 frame.scanLines = readUint16();675 frame.samplesPerLine = readUint16();676 frame.components = [];677 frame.componentIds = {};678 var componentsCount = data[offset++],679 componentId;680 var maxH = 0,681 maxV = 0;682 for (i = 0; i < componentsCount; i++) {683 componentId = data[offset];684 let h = data[offset + 1] >> 4;685 let v = data[offset + 1] & 15;686 if (maxH < h) {687 maxH = h;688 }689 if (maxV < v) {690 maxV = v;691 }692 let qId = data[offset + 2];693 l = frame.components.push({694 h: h,695 v: v,696 quantizationId: qId,697 quantizationTable: null,698 });699 frame.componentIds[componentId] = l - 1;700 offset += 3;701 }702 frame.maxH = maxH;703 frame.maxV = maxV;704 prepareComponents(frame);705 break;706 case 0xFFC4:707 var huffmanLength = readUint16();708 for (i = 2; i < huffmanLength;) {709 let huffmanTableSpec = data[offset++];710 let codeLengths = new Uint8Array(16);711 let codeLengthSum = 0;712 for (j = 0; j < 16; j++ , offset++) {713 codeLengthSum += codeLengths[j] = data[offset];714 }715 let huffmanValues = new Uint8Array(codeLengthSum);716 for (j = 0; j < codeLengthSum; j++ , offset++) {717 huffmanValues[j] = data[offset];718 }719 i += 17 + codeLengthSum;720 (huffmanTableSpec >> 4 === 0 ? huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] = buildHuffmanTable(codeLengths, huffmanValues);721 }722 break;723 case 0xFFDD:724 readUint16();725 resetInterval = readUint16();726 break;727 case 0xFFDA:728 readUint16();729 var selectorsCount = data[offset++];730 var components = [],731 component;732 for (i = 0; i < selectorsCount; i++) {733 let componentIndex = frame.componentIds[data[offset++]];734 component = frame.components[componentIndex];735 let tableSpec = data[offset++];736 component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];737 component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];738 components.push(component);739 }740 var spectralStart = data[offset++];741 var spectralEnd = data[offset++];742 var successiveApproximation = data[offset++];743 var processed = decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successiveApproximation >> 4, successiveApproximation & 15);744 offset += processed;745 break;746 case 0xFFFF:747 if (data[offset] !== 0xFF) {748 offset--;749 }750 break;751 default:752 if (data[offset - 3] === 0xFF && data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {753 offset -= 3;754 break;755 }756 throw new JpegError('unknown marker ' + fileMarker.toString(16));757 }758 fileMarker = readUint16();759 }760 this.width = frame.samplesPerLine;761 this.height = frame.scanLines;762 this.jfif = jfif;763 this.adobe = adobe;764 this.components = [];765 for (i = 0; i < frame.components.length; i++) {766 component = frame.components[i];767 let quantizationTable = quantizationTables[component.quantizationId];768 if (quantizationTable) {769 component.quantizationTable = quantizationTable;770 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.JpegError('test.jpg', function (err, data) {3 if (err) {4 console.log(err);5 }6 else {7 console.log(data);8 }9});10{ "Error": "Unsupported format or corrupted file" }11{ "Error": "File not found" }12function(err, data) {13 if (err) {14 console.log(err);15 }16 else {17 console.log(data);18 }19}20JpegInfo(image, callback)21var wpt = require('wpt');22wpt.JpegInfo('test.jpg', function (err, data) {23 if (err) {24 console.log(err);25 }26 else {27 console.log(data);28 }29});30{31}32{ "Error": "File not found" }33function(err, data) {34 if (err) {35 console.log(err);36 }37 else {38 console.log(data);39 }40}41JpegThumbnail(image, width, height, callback)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.on('error', function(err) {3 console.log('Error: ' + err);4});5wpt.on('result', function(result) {6 console.log('Result: ' + result);7});8 if (err) {9 console.log('Error: ' + err);10 } else {11 console.log('Data: ' + data);12 }13});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful