Best JavaScript code snippet using wpt
jpg.js
Source:jpg.js
...350 mcu++;351 }352 }353 bitsCount = 0;354 fileMarker = findNextFileMarker(data, offset);355 if (fileMarker && fileMarker.invalid) {356 (0, _util.warn)('decodeScan - unexpected MCU data, current marker is: ' + fileMarker.invalid);357 offset = fileMarker.offset;358 }359 var marker = fileMarker && fileMarker.marker;360 if (!marker || marker <= 0xFF00) {361 throw new JpegError('marker was not found');362 }363 if (marker >= 0xFFD0 && marker <= 0xFFD7) {364 offset += 2;365 } else {366 break;367 }368 }369 fileMarker = findNextFileMarker(data, offset);370 if (fileMarker && fileMarker.invalid) {371 (0, _util.warn)('decodeScan - unexpected Scan data, current marker is: ' + fileMarker.invalid);372 offset = fileMarker.offset;373 }374 return offset - startOffset;375 }376 function quantizeAndInverse(component, blockBufferOffset, p) {377 var qt = component.quantizationTable,378 blockData = component.blockData;379 var v0, v1, v2, v3, v4, v5, v6, v7;380 var p0, p1, p2, p3, p4, p5, p6, p7;381 var t;382 if (!qt) {383 throw new JpegError('missing required Quantization Table.');384 }385 for (var row = 0; row < 64; row += 8) {386 p0 = blockData[blockBufferOffset + row];387 p1 = blockData[blockBufferOffset + row + 1];388 p2 = blockData[blockBufferOffset + row + 2];389 p3 = blockData[blockBufferOffset + row + 3];390 p4 = blockData[blockBufferOffset + row + 4];391 p5 = blockData[blockBufferOffset + row + 5];392 p6 = blockData[blockBufferOffset + row + 6];393 p7 = blockData[blockBufferOffset + row + 7];394 p0 *= qt[row];395 if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {396 t = dctSqrt2 * p0 + 512 >> 10;397 p[row] = t;398 p[row + 1] = t;399 p[row + 2] = t;400 p[row + 3] = t;401 p[row + 4] = t;402 p[row + 5] = t;403 p[row + 6] = t;404 p[row + 7] = t;405 continue;406 }407 p1 *= qt[row + 1];408 p2 *= qt[row + 2];409 p3 *= qt[row + 3];410 p4 *= qt[row + 4];411 p5 *= qt[row + 5];412 p6 *= qt[row + 6];413 p7 *= qt[row + 7];414 v0 = dctSqrt2 * p0 + 128 >> 8;415 v1 = dctSqrt2 * p4 + 128 >> 8;416 v2 = p2;417 v3 = p6;418 v4 = dctSqrt1d2 * (p1 - p7) + 128 >> 8;419 v7 = dctSqrt1d2 * (p1 + p7) + 128 >> 8;420 v5 = p3 << 4;421 v6 = p5 << 4;422 v0 = v0 + v1 + 1 >> 1;423 v1 = v0 - v1;424 t = v2 * dctSin6 + v3 * dctCos6 + 128 >> 8;425 v2 = v2 * dctCos6 - v3 * dctSin6 + 128 >> 8;426 v3 = t;427 v4 = v4 + v6 + 1 >> 1;428 v6 = v4 - v6;429 v7 = v7 + v5 + 1 >> 1;430 v5 = v7 - v5;431 v0 = v0 + v3 + 1 >> 1;432 v3 = v0 - v3;433 v1 = v1 + v2 + 1 >> 1;434 v2 = v1 - v2;435 t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;436 v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;437 v7 = t;438 t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;439 v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;440 v6 = t;441 p[row] = v0 + v7;442 p[row + 7] = v0 - v7;443 p[row + 1] = v1 + v6;444 p[row + 6] = v1 - v6;445 p[row + 2] = v2 + v5;446 p[row + 5] = v2 - v5;447 p[row + 3] = v3 + v4;448 p[row + 4] = v3 - v4;449 }450 for (var col = 0; col < 8; ++col) {451 p0 = p[col];452 p1 = p[col + 8];453 p2 = p[col + 16];454 p3 = p[col + 24];455 p4 = p[col + 32];456 p5 = p[col + 40];457 p6 = p[col + 48];458 p7 = p[col + 56];459 if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {460 t = dctSqrt2 * p0 + 8192 >> 14;461 t = t < -2040 ? 0 : t >= 2024 ? 255 : t + 2056 >> 4;462 blockData[blockBufferOffset + col] = t;463 blockData[blockBufferOffset + col + 8] = t;464 blockData[blockBufferOffset + col + 16] = t;465 blockData[blockBufferOffset + col + 24] = t;466 blockData[blockBufferOffset + col + 32] = t;467 blockData[blockBufferOffset + col + 40] = t;468 blockData[blockBufferOffset + col + 48] = t;469 blockData[blockBufferOffset + col + 56] = t;470 continue;471 }472 v0 = dctSqrt2 * p0 + 2048 >> 12;473 v1 = dctSqrt2 * p4 + 2048 >> 12;474 v2 = p2;475 v3 = p6;476 v4 = dctSqrt1d2 * (p1 - p7) + 2048 >> 12;477 v7 = dctSqrt1d2 * (p1 + p7) + 2048 >> 12;478 v5 = p3;479 v6 = p5;480 v0 = (v0 + v1 + 1 >> 1) + 4112;481 v1 = v0 - v1;482 t = v2 * dctSin6 + v3 * dctCos6 + 2048 >> 12;483 v2 = v2 * dctCos6 - v3 * dctSin6 + 2048 >> 12;484 v3 = t;485 v4 = v4 + v6 + 1 >> 1;486 v6 = v4 - v6;487 v7 = v7 + v5 + 1 >> 1;488 v5 = v7 - v5;489 v0 = v0 + v3 + 1 >> 1;490 v3 = v0 - v3;491 v1 = v1 + v2 + 1 >> 1;492 v2 = v1 - v2;493 t = v4 * dctSin3 + v7 * dctCos3 + 2048 >> 12;494 v4 = v4 * dctCos3 - v7 * dctSin3 + 2048 >> 12;495 v7 = t;496 t = v5 * dctSin1 + v6 * dctCos1 + 2048 >> 12;497 v5 = v5 * dctCos1 - v6 * dctSin1 + 2048 >> 12;498 v6 = t;499 p0 = v0 + v7;500 p7 = v0 - v7;501 p1 = v1 + v6;502 p6 = v1 - v6;503 p2 = v2 + v5;504 p5 = v2 - v5;505 p3 = v3 + v4;506 p4 = v3 - v4;507 p0 = p0 < 16 ? 0 : p0 >= 4080 ? 255 : p0 >> 4;508 p1 = p1 < 16 ? 0 : p1 >= 4080 ? 255 : p1 >> 4;509 p2 = p2 < 16 ? 0 : p2 >= 4080 ? 255 : p2 >> 4;510 p3 = p3 < 16 ? 0 : p3 >= 4080 ? 255 : p3 >> 4;511 p4 = p4 < 16 ? 0 : p4 >= 4080 ? 255 : p4 >> 4;512 p5 = p5 < 16 ? 0 : p5 >= 4080 ? 255 : p5 >> 4;513 p6 = p6 < 16 ? 0 : p6 >= 4080 ? 255 : p6 >> 4;514 p7 = p7 < 16 ? 0 : p7 >= 4080 ? 255 : p7 >> 4;515 blockData[blockBufferOffset + col] = p0;516 blockData[blockBufferOffset + col + 8] = p1;517 blockData[blockBufferOffset + col + 16] = p2;518 blockData[blockBufferOffset + col + 24] = p3;519 blockData[blockBufferOffset + col + 32] = p4;520 blockData[blockBufferOffset + col + 40] = p5;521 blockData[blockBufferOffset + col + 48] = p6;522 blockData[blockBufferOffset + col + 56] = p7;523 }524 }525 function buildComponentData(frame, component) {526 var blocksPerLine = component.blocksPerLine;527 var blocksPerColumn = component.blocksPerColumn;528 var computationBuffer = new Int16Array(64);529 for (var blockRow = 0; blockRow < blocksPerColumn; blockRow++) {530 for (var blockCol = 0; blockCol < blocksPerLine; blockCol++) {531 var offset = getBlockBufferOffset(component, blockRow, blockCol);532 quantizeAndInverse(component, offset, computationBuffer);533 }534 }535 return component.blockData;536 }537 function findNextFileMarker(data, currentPos) {538 var startPos = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : currentPos;539 function peekUint16(pos) {540 return data[pos] << 8 | data[pos + 1];541 }542 var maxPos = data.length - 1;543 var newPos = startPos < currentPos ? startPos : currentPos;544 if (currentPos >= maxPos) {545 return null;546 }547 var currentMarker = peekUint16(currentPos);548 if (currentMarker >= 0xFFC0 && currentMarker <= 0xFFFE) {549 return {550 invalid: null,551 marker: currentMarker,552 offset: currentPos553 };554 }555 var newMarker = peekUint16(newPos);556 while (!(newMarker >= 0xFFC0 && newMarker <= 0xFFFE)) {557 if (++newPos >= maxPos) {558 return null;559 }560 newMarker = peekUint16(newPos);561 }562 return {563 invalid: currentMarker.toString(16),564 marker: newMarker,565 offset: newPos566 };567 }568 JpegImage.prototype = {569 parse: function parse(data) {570 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},571 _ref$dnlScanLines = _ref.dnlScanLines,572 dnlScanLines = _ref$dnlScanLines === undefined ? null : _ref$dnlScanLines;573 function readUint16() {574 var value = data[offset] << 8 | data[offset + 1];575 offset += 2;576 return value;577 }578 function readDataBlock() {579 var length = readUint16();580 var endOffset = offset + length - 2;581 var fileMarker = findNextFileMarker(data, endOffset, offset);582 if (fileMarker && fileMarker.invalid) {583 (0, _util.warn)('readDataBlock - incorrect length, current marker is: ' + fileMarker.invalid);584 endOffset = fileMarker.offset;585 }586 var array = data.subarray(offset, endOffset);587 offset += array.length;588 return array;589 }590 function prepareComponents(frame) {591 var mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / frame.maxH);592 var mcusPerColumn = Math.ceil(frame.scanLines / 8 / frame.maxV);593 for (var i = 0; i < frame.components.length; i++) {594 component = frame.components[i];595 var blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / frame.maxH);596 var blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / frame.maxV);597 var blocksPerLineForMcu = mcusPerLine * component.h;598 var blocksPerColumnForMcu = mcusPerColumn * component.v;599 var blocksBufferSize = 64 * blocksPerColumnForMcu * (blocksPerLineForMcu + 1);600 component.blockData = new Int16Array(blocksBufferSize);601 component.blocksPerLine = blocksPerLine;602 component.blocksPerColumn = blocksPerColumn;603 }604 frame.mcusPerLine = mcusPerLine;605 frame.mcusPerColumn = mcusPerColumn;606 }607 var offset = 0;608 var jfif = null;609 var adobe = null;610 var frame, resetInterval;611 var numSOSMarkers = 0;612 var quantizationTables = [];613 var huffmanTablesAC = [],614 huffmanTablesDC = [];615 var fileMarker = readUint16();616 if (fileMarker !== 0xFFD8) {617 throw new JpegError('SOI not found');618 }619 fileMarker = readUint16();620 while (fileMarker !== 0xFFD9) {621 var i, j, l;622 switch (fileMarker) {623 case 0xFFE0:624 case 0xFFE1:625 case 0xFFE2:626 case 0xFFE3:627 case 0xFFE4:628 case 0xFFE5:629 case 0xFFE6:630 case 0xFFE7:631 case 0xFFE8:632 case 0xFFE9:633 case 0xFFEA:634 case 0xFFEB:635 case 0xFFEC:636 case 0xFFED:637 case 0xFFEE:638 case 0xFFEF:639 case 0xFFFE:640 var appData = readDataBlock();641 if (fileMarker === 0xFFE0) {642 if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 && appData[3] === 0x46 && appData[4] === 0) {643 jfif = {644 version: {645 major: appData[5],646 minor: appData[6]647 },648 densityUnits: appData[7],649 xDensity: appData[8] << 8 | appData[9],650 yDensity: appData[10] << 8 | appData[11],651 thumbWidth: appData[12],652 thumbHeight: appData[13],653 thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])654 };655 }656 }657 if (fileMarker === 0xFFEE) {658 if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F && appData[3] === 0x62 && appData[4] === 0x65) {659 adobe = {660 version: appData[5] << 8 | appData[6],661 flags0: appData[7] << 8 | appData[8],662 flags1: appData[9] << 8 | appData[10],663 transformCode: appData[11]664 };665 }666 }667 break;668 case 0xFFDB:669 var quantizationTablesLength = readUint16();670 var quantizationTablesEnd = quantizationTablesLength + offset - 2;671 var z;672 while (offset < quantizationTablesEnd) {673 var quantizationTableSpec = data[offset++];674 var tableData = new Uint16Array(64);675 if (quantizationTableSpec >> 4 === 0) {676 for (j = 0; j < 64; j++) {677 z = dctZigZag[j];678 tableData[z] = data[offset++];679 }680 } else if (quantizationTableSpec >> 4 === 1) {681 for (j = 0; j < 64; j++) {682 z = dctZigZag[j];683 tableData[z] = readUint16();684 }685 } else {686 throw new JpegError('DQT - invalid table spec');687 }688 quantizationTables[quantizationTableSpec & 15] = tableData;689 }690 break;691 case 0xFFC0:692 case 0xFFC1:693 case 0xFFC2:694 if (frame) {695 throw new JpegError('Only single frame JPEGs supported');696 }697 readUint16();698 frame = {};699 frame.extended = fileMarker === 0xFFC1;700 frame.progressive = fileMarker === 0xFFC2;701 frame.precision = data[offset++];702 var sofScanLines = readUint16();703 frame.scanLines = dnlScanLines || sofScanLines;704 frame.samplesPerLine = readUint16();705 frame.components = [];706 frame.componentIds = {};707 var componentsCount = data[offset++],708 componentId;709 var maxH = 0,710 maxV = 0;711 for (i = 0; i < componentsCount; i++) {712 componentId = data[offset];713 var h = data[offset + 1] >> 4;714 var v = data[offset + 1] & 15;715 if (maxH < h) {716 maxH = h;717 }718 if (maxV < v) {719 maxV = v;720 }721 var qId = data[offset + 2];722 l = frame.components.push({723 h: h,724 v: v,725 quantizationId: qId,726 quantizationTable: null727 });728 frame.componentIds[componentId] = l - 1;729 offset += 3;730 }731 frame.maxH = maxH;732 frame.maxV = maxV;733 prepareComponents(frame);734 break;735 case 0xFFC4:736 var huffmanLength = readUint16();737 for (i = 2; i < huffmanLength;) {738 var huffmanTableSpec = data[offset++];739 var codeLengths = new Uint8Array(16);740 var codeLengthSum = 0;741 for (j = 0; j < 16; j++, offset++) {742 codeLengthSum += codeLengths[j] = data[offset];743 }744 var huffmanValues = new Uint8Array(codeLengthSum);745 for (j = 0; j < codeLengthSum; j++, offset++) {746 huffmanValues[j] = data[offset];747 }748 i += 17 + codeLengthSum;749 (huffmanTableSpec >> 4 === 0 ? huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] = buildHuffmanTable(codeLengths, huffmanValues);750 }751 break;752 case 0xFFDD:753 readUint16();754 resetInterval = readUint16();755 break;756 case 0xFFDA:757 var parseDNLMarker = ++numSOSMarkers === 1 && !dnlScanLines;758 readUint16();759 var selectorsCount = data[offset++];760 var components = [],761 component;762 for (i = 0; i < selectorsCount; i++) {763 var componentIndex = frame.componentIds[data[offset++]];764 component = frame.components[componentIndex];765 var tableSpec = data[offset++];766 component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];767 component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];768 components.push(component);769 }770 var spectralStart = data[offset++];771 var spectralEnd = data[offset++];772 var successiveApproximation = data[offset++];773 try {774 var processed = decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successiveApproximation >> 4, successiveApproximation & 15, parseDNLMarker);775 offset += processed;776 } catch (ex) {777 if (ex instanceof DNLMarkerError) {778 (0, _util.warn)('Attempting to re-parse JPEG image using "scanLines" ' + 'parameter found in DNL marker (0xFFDC) segment.');779 return this.parse(data, { dnlScanLines: ex.scanLines });780 }781 throw ex;782 }783 break;784 case 0xFFDC:785 offset += 4;786 break;787 case 0xFFFF:788 if (data[offset] !== 0xFF) {789 offset--;790 }791 break;792 default:793 if (data[offset - 3] === 0xFF && data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {794 offset -= 3;795 break;796 }797 var nextFileMarker = findNextFileMarker(data, offset - 2);798 if (nextFileMarker && nextFileMarker.invalid) {799 (0, _util.warn)('JpegImage.parse - unexpected data, current marker is: ' + nextFileMarker.invalid);800 offset = nextFileMarker.offset;801 break;802 }803 throw new JpegError('unknown marker ' + fileMarker.toString(16));804 }805 fileMarker = readUint16();806 }807 this.width = frame.samplesPerLine;808 this.height = frame.scanLines;809 this.jfif = jfif;810 this.adobe = adobe;811 this.components = [];...
Using AI Code Generation
1var wpt = require('wpt');2var fs = require('fs');3var file = fs.readFileSync('test.txt');4var fileMarker = wpt.findNextFileMarker(file, 0);5console.log(fileMarker);6var wpt = require('wpt');7var fs = require('fs');8var file = fs.readFileSync('test.txt');9var fileMarker = wpt.findNextFileMarker(file, 0);10console.log(fileMarker);11var wpt = require('wpt');12var fs = require('fs');13var file = fs.readFileSync('test.txt');14var fileMarker = wpt.findNextFileMarker(file, 0);15console.log(fileMarker);16var wpt = require('wpt');17var fs = require('fs');18var file = fs.readFileSync('test.txt');19var fileMarker = wpt.findNextFileMarker(file, 0);20console.log(fileMarker);21var wpt = require('wpt');22var fs = require('fs');23var file = fs.readFileSync('test.txt');24var fileMarker = wpt.findNextFileMarker(file, 0);25console.log(fileMarker);26var wpt = require('wpt');27var fs = require('fs');28var file = fs.readFileSync('test.txt');29var fileMarker = wpt.findNextFileMarker(file, 0);30console.log(fileMarker);31var wpt = require('wpt');32var fs = require('fs');33var file = fs.readFileSync('test.txt');34var fileMarker = wpt.findNextFileMarker(file, 0);35console.log(fileMarker);
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.findNextFileMarker("File:Albert Einstein Head.jpg", function(err, resp) {4 console.log(resp);5});6### findNextFileMarker(marker, callback)
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.findNextFileMarker('Barack Obama', 'thumb', 'Obama family 2013.jpg', function(err, fileMarker) {4 console.log(err);5 console.log(fileMarker);6});
Using AI Code Generation
1var wptool = require('./wptool');2var wp = new wptool();3wp.setFilePath('test.txt');4wp.readFile();5var nextFileMarker = wp.findNextFileMarker();6console.log(nextFileMarker);
Using AI Code Generation
1const wptMapper = require('wpt-mapper');2const mapper = new wptMapper();3const fileMarker = mapper.findNextFileMarker('test.wpt');4console.log(fileMarker);5{6}7const wptMapper = require('wpt-mapper');8const mapper = new wptMapper();9const testMarker = mapper.findNextTestMarker('test.wpt');10console.log(testMarker);11{12}13const wptMapper = require('wpt-mapper');14const mapper = new wptMapper();15const fileMarker = mapper.findNextFileMarker('test.wpt');16const testMarker = mapper.findNextTestMarker('test.wpt');17console.log(fileMarker);18console.log(testMarker);19{20}21{22}23const wptMapper = require('wpt-mapper');24const mapper = new wptMapper();25const fileMarker = mapper.findNextFileMarker('test.wpt');
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!