Best JavaScript code snippet using wpt
pdfjs.js
Source:pdfjs.js
1const util = require('pdfjs-dist/lib/shared/util');2const isNodeJS = require('pdfjs-dist/lib/shared/is_node');3const zlib = require('zlib');4const ImageKind = util.ImageKind;5/**6 * See pdf.js/src/display/svg.js7 */8const convertImgDataToPng = (() => {9 const PNG_HEADER =10 new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);11 const CHUNK_WRAPPER_SIZE = 12;12 const crcTable = new Int32Array(256);13 for (let i = 0; i < 256; i++) {14 let c = i;15 for (let h = 0; h < 8; h++) {16 if (c & 1) {17 c = 0xedB88320 ^ ((c >> 1) & 0x7fffffff);18 } else {19 c = (c >> 1) & 0x7fffffff;20 }21 }22 crcTable[i] = c;23 }24 function crc32 (data, start, end) {25 let crc = -1;26 for (let i = start; i < end; i++) {27 const a = (crc ^ data[i]) & 0xff;28 const b = crcTable[a];29 crc = (crc >>> 8) ^ b;30 }31 return crc ^ -1;32 }33 function writePngChunk (type, body, data, offset) {34 let p = offset;35 const len = body.length;36 data[p] = len >> 24 & 0xff;37 data[p + 1] = len >> 16 & 0xff;38 data[p + 2] = len >> 8 & 0xff;39 data[p + 3] = len & 0xff;40 p += 4;41 data[p] = type.charCodeAt(0) & 0xff;42 data[p + 1] = type.charCodeAt(1) & 0xff;43 data[p + 2] = type.charCodeAt(2) & 0xff;44 data[p + 3] = type.charCodeAt(3) & 0xff;45 p += 4;46 data.set(body, p);47 p += body.length;48 const crc = crc32(data, offset + 4, p);49 data[p] = crc >> 24 & 0xff;50 data[p + 1] = crc >> 16 & 0xff;51 data[p + 2] = crc >> 8 & 0xff;52 data[p + 3] = crc & 0xff;53 }54 function adler32 (data, start, end) {55 let a = 1;56 let b = 0;57 for (let i = start; i < end; ++i) {58 a = (a + (data[i] & 0xff)) % 65521;59 b = (b + a) % 65521;60 }61 return (b << 16) | a;62 }63 /**64 * @param {Uint8Array} literals The input data.65 * @returns {Uint8Array} The DEFLATE-compressed data stream in zlib format.66 * This is the required format for compressed streams in the PNG format:67 * http://www.libpng.org/pub/png/spec/1.2/PNG-Compression.html68 */69 function deflateSync (literals) {70 if (!isNodeJS()) {71 // zlib is certainly not available outside of Node.js. We can either use72 // the pako library for client-side DEFLATE compression, or use the canvas73 // API of the browser to obtain a more optimal PNG file.74 return deflateSyncUncompressed(literals);75 }76 try {77 // NOTE: This implementation is far from perfect, but already way better78 // than not applying any compression.79 //80 // A better algorithm will try to choose a good predictor/filter and81 // then choose a suitable zlib compression strategy (e.g. 3,Z_RLE).82 //83 // Node v0.11.12 zlib.deflateSync is introduced (and returns a Buffer).84 // Node v3.0.0 Buffer inherits from Uint8Array.85 // Node v8.0.0 zlib.deflateSync accepts Uint8Array as input.86 let input;87 // eslint-disable-next-line no-undef88 if (parseInt(process.versions.node) >= 8) {89 input = literals;90 } else {91 // eslint-disable-next-line no-undef92 // input = new Buffer(literals);93 input = Buffer.from(literals);94 }95 // const output = __non_webpack_require__('zlib')96 const output = zlib97 .deflateSync(input, { level: 9 });98 return output instanceof Uint8Array ? output : new Uint8Array(output);99 } catch (e) {100 console.warn('Not compressing PNG because zlib.deflateSync is unavailable: ' + e);101 }102 return deflateSyncUncompressed(literals);103 }104 // An implementation of DEFLATE with compression level 0 (Z_NO_COMPRESSION).105 function deflateSyncUncompressed (literals) {106 let len = literals.length;107 const maxBlockLength = 0xFFFF;108 const deflateBlocks = Math.ceil(len / maxBlockLength);109 const idat = new Uint8Array(2 + len + deflateBlocks * 5 + 4);110 let pi = 0;111 idat[pi++] = 0x78; // compression method and flags112 idat[pi++] = 0x9c; // flags113 let pos = 0;114 while (len > maxBlockLength) {115 // writing non-final DEFLATE blocks type 0 and length of 65535116 idat[pi++] = 0x00;117 idat[pi++] = 0xff;118 idat[pi++] = 0xff;119 idat[pi++] = 0x00;120 idat[pi++] = 0x00;121 idat.set(literals.subarray(pos, pos + maxBlockLength), pi);122 pi += maxBlockLength;123 pos += maxBlockLength;124 len -= maxBlockLength;125 }126 // writing non-final DEFLATE blocks type 0127 idat[pi++] = 0x01;128 idat[pi++] = len & 0xff;129 idat[pi++] = len >> 8 & 0xff;130 idat[pi++] = (~len & 0xffff) & 0xff;131 idat[pi++] = (~len & 0xffff) >> 8 & 0xff;132 idat.set(literals.subarray(pos), pi);133 pi += literals.length - pos;134 const adler = adler32(literals, 0, literals.length); // checksum135 idat[pi++] = adler >> 24 & 0xff;136 idat[pi++] = adler >> 16 & 0xff;137 idat[pi++] = adler >> 8 & 0xff;138 idat[pi++] = adler & 0xff;139 return idat;140 }141 function encode (imgData, kind, forceDataSchema, isMask) {142 const width = imgData.width;143 const height = imgData.height;144 let bitDepth, colorType, lineSize;145 const bytes = imgData.data;146 switch (kind) {147 case ImageKind.GRAYSCALE_1BPP:148 colorType = 0;149 bitDepth = 1;150 lineSize = (width + 7) >> 3;151 break;152 case ImageKind.RGB_24BPP:153 colorType = 2;154 bitDepth = 8;155 lineSize = width * 3;156 break;157 case ImageKind.RGBA_32BPP:158 colorType = 6;159 bitDepth = 8;160 lineSize = width * 4;161 break;162 default:163 throw new Error('invalid format');164 }165 // prefix every row with predictor 0166 const literals = new Uint8Array((1 + lineSize) * height);167 let offsetLiterals = 0;168 let offsetBytes = 0;169 for (let y = 0; y < height; ++y) {170 literals[offsetLiterals++] = 0; // no prediction171 literals.set(bytes.subarray(offsetBytes, offsetBytes + lineSize),172 offsetLiterals);173 offsetBytes += lineSize;174 offsetLiterals += lineSize;175 }176 if (kind === ImageKind.GRAYSCALE_1BPP && isMask) {177 // inverting for image masks178 offsetLiterals = 0;179 for (let y = 0; y < height; y++) {180 offsetLiterals++; // skipping predictor181 for (let i = 0; i < lineSize; i++) {182 literals[offsetLiterals++] ^= 0xFF;183 }184 }185 }186 const ihdr = new Uint8Array([187 width >> 24 & 0xff,188 width >> 16 & 0xff,189 width >> 8 & 0xff,190 width & 0xff,191 height >> 24 & 0xff,192 height >> 16 & 0xff,193 height >> 8 & 0xff,194 height & 0xff,195 bitDepth, // bit depth196 colorType, // color type197 0x00, // compression method198 0x00, // filter method199 0x00, // interlace method200 ]);201 const idat = deflateSync(literals);202 // PNG consists of: header, IHDR+data, IDAT+data, and IEND.203 const pngLength = PNG_HEADER.length + (CHUNK_WRAPPER_SIZE * 3) +204 ihdr.length + idat.length;205 const data = new Uint8Array(pngLength);206 let offset = 0;207 data.set(PNG_HEADER, offset);208 offset += PNG_HEADER.length;209 writePngChunk('IHDR', ihdr, data, offset);210 offset += CHUNK_WRAPPER_SIZE + ihdr.length;211 writePngChunk('IDATA', idat, data, offset);212 offset += CHUNK_WRAPPER_SIZE + idat.length;213 writePngChunk('IEND', new Uint8Array(0), data, offset);214 return data;215 // return util.createObjectURL(data, 'image/png', forceDataSchema);216 }217 return function convertImgDataToPng (imgData, forceDataSchema, isMask) {218 const kind = (imgData.kind === undefined ? ImageKind.GRAYSCALE_1BPP : imgData.kind);219 return encode(imgData, kind, forceDataSchema, isMask);220 };221})();222module.exports = {223 convertImgDataToPng,...
helpers.js
Source:helpers.js
...70 default:71 return { colorType: 0, bitDepth: 1, lineSize: (width + 7) >> 3 };72 }73}74function convertImgDataToPng(imgData, isMask) {75 const kind = imgData.kind || 1;76 const { width, height } = imgData;77 const bytes = imgData.data;78 const { bitDepth, colorType, lineSize } = getKindParams(kind, width);79 const literals = new Uint8Array((1 + lineSize) * height);80 let offsetLiterals = 0;81 let offsetBytes = 0;82 for (let y = 0; y < height; y += 1) {83 literals[offsetLiterals] = 0;84 offsetLiterals += 1;85 literals.set(86 bytes.subarray(offsetBytes, offsetBytes + lineSize),87 offsetLiterals88 );...
paintImageXObject.ts
Source:paintImageXObject.ts
...32 logger.warn(`Image ${objectId} was not previously loaded`);33 return null;34 }35 const { height, width } = imgData;36 const imageData = convertImgDataToPng(imgData, false);37 return {38 type: 'image',39 data: {40 imageData,41 objectId,42 height,43 width,44 transform: OperationState.state.transformMatrix,45 },46 };47 },...
Using AI Code Generation
1var convertImgDataToPng = require('wptools').convertImgDataToPng;2var fs = require('fs');3var image = fs.readFileSync('test.jpg');4convertImgDataToPng(image, function(err, image) {5 if(err) {6 console.log(err);7 } else {8 fs.writeFileSync('test.png', image);9 }10});11var convertImgDataToJpg = require('wptools').convertImgDataToJpg;12var fs = require('fs');13var image = fs.readFileSync('test.png');14convertImgDataToJpg(image, function(err, image) {15 if(err) {16 console.log(err);17 } else {18 fs.writeFileSync('test.jpg', image);19 }20});21var convertImgDataToGif = require('wptools').convertImgDataToGif;22var fs = require('fs');23var image = fs.readFileSync('test.jpg');24convertImgDataToGif(image, function(err, image) {25 if(err) {26 console.log(err);27 } else {28 fs.writeFileSync('test.gif', image);29 }30});31var convertImgDataToBmp = require('wptools').convertImgDataToBmp;32var fs = require('fs');33var image = fs.readFileSync('test.jpg');34convertImgDataToBmp(image, function(err, image) {35 if(err) {36 console.log(err);37 } else {38 fs.writeFileSync('test.bmp', image);39 }40});41var convertImgDataToTiff = require('wptools').convertImgDataToTiff;42var fs = require('fs');43var image = fs.readFileSync('test.jpg');44convertImgDataToTiff(image, function(err, image) {45 if(err) {46 console.log(err);47 } else {48 fs.writeFileSync('test.tiff', image);49 }50});
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var imgData = fs.readFileSync('test.png', 'base64');4wptools.convertImgDataToPng(imgData, function (err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});
Using AI Code Generation
1const wptools = require('wptools')2import {convertImgDataToPng} from 'wptools'3const {convertImgDataToPng} = require('wptools')4const wptools = require('wptools')5const {convertImgDataToPng} = wptools6import wptools from 'wptools'7const {convertImgDataToPng} = wptools8const wptools = require('wptools')9const {convertImgDataToPng} = wptools10const wptools = require('wptools')11const {convertImgDataToPng} = wptools12const wptools = require('wptools')13const {convertImgDataToPng} = wptools14const wptools = require('wptools')15const {convertImgDataToPng} = wptools16const wptools = require('wptools')17const {convertImgData
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var fs = require('fs');3var imgData = fs.readFileSync('input.png');4var pngData = wptoolkit.convertImgDataToPng(imgData, 300, 300, 96, 96);5fs.writeFileSync('output.png', pngData);6convertImgDataToPng(imgData, width, height, dpiX, dpiY, callback)7function(err, pngData) { }8var wptoolkit = require('wptoolkit');9var fs = require('fs');10var imgData = fs.readFileSync('input.png');11wptoolkit.convertImgDataToPng(imgData, 300, 300, 96, 96, function(err, pngData) {12 if (err) {13 console.log('Error: ' + err.message);14 }15 else {16 fs.writeFileSync('output.png', pngData);17 }18});
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var data = fs.readFileSync('test.png');4var img = wptools.convertImgDataToPng(data);5fs.writeFileSync('test2.png', img);6var wptools = require('wptools');7var fs = require('fs');8var data = fs.readFileSync('test2.png');9var img = wptools.convertImgDataToPng(data);10fs.writeFileSync('test3.png', img);11var wptools = require('wptools');12var fs = require('fs');13var data = fs.readFileSync('test3.png');14var img = wptools.convertImgDataToPng(data);15fs.writeFileSync('test4.png', img);16var wptools = require('wptools');17var fs = require('fs');18var data = fs.readFileSync('test4.png');19var img = wptools.convertImgDataToPng(data);20fs.writeFileSync('test5.png', img);21var wptools = require('wptools');22var fs = require('fs');23var data = fs.readFileSync('test5.png');24var img = wptools.convertImgDataToPng(data);25fs.writeFileSync('test6.png', img);26var wptools = require('wptools');27var fs = require('fs');28var data = fs.readFileSync('test6.png');29var img = wptools.convertImgDataToPng(data);30fs.writeFileSync('test7.png', img);31var wptools = require('wptools');32var fs = require('fs');33var data = fs.readFileSync('test7.png');34var img = wptools.convertImgDataToPng(data);35fs.writeFileSync('test8.png', img);
Using AI Code Generation
1const wptools = require('wptools');2const fs = require('fs');3const convertImgDataToPng = wptools.convertImgDataToPng;4const imgData = fs.readFileSync('test.jpg', 'base64');5convertImgDataToPng(imgData, (err, pngData) => {6 if (err) {7 console.log(err);8 } else {9 console.log(pngData);10 }11});
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var fs = require('fs');3var imgData = fs.readFileSync('test.png');4var pngImgData = wptoolkit.convertImgDataToPng(imgData);5fs.writeFileSync('test.png', pngImgData);6var wptoolkit = require('wptoolkit');7var fs = require('fs');8var imgData = fs.readFileSync('test.png');9var jpegImgData = wptoolkit.convertImgDataToJpeg(imgData);10fs.writeFileSync('test.jpg', jpegImgData);11var wptoolkit = require('wptoolkit');12var fs = require('fs');13var imgData = fs.readFileSync('test.png');14var webpImgData = wptoolkit.convertImgDataToWebp(imgData);15fs.writeFileSync('test.webp', webpImgData);16var wptoolkit = require('wptoolkit');17var fs = require('fs');18var imgData = fs.readFileSync('test.png');19var tiffImgData = wptoolkit.convertImgDataToTiff(imgData);20fs.writeFileSync('test.tiff', tiffImgData);21var wptoolkit = require('wptoolkit');22var fs = require('fs');23var imgData = fs.readFileSync('test.png');24var bmpImgData = wptoolkit.convertImgDataToBmp(imgData);25fs.writeFileSync('test.bmp', bmpImgData);26var wptoolkit = require('wptool
Using AI Code Generation
1I have tried to use the convertImgDataToPng and convertImgDataToJpg methods of wptools. It works fine when I use it in a single file. But when I try to use it in a different file, it throws an error saying "ReferenceError: convertImgDataToPng is not defined". I have tried to import the module using require and import but it still throws the same error. What am I doing wrong?2async function getImage(url) {3 const page = await wptools.page(url).get()4 const image = await page.getImage()5}6"engines": {7 }
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!!