How to use decodeAndClamp method in wpt

Best JavaScript code snippet using wpt

image.js

Source:image.js Github

copy

Full Screen

...25 /**26 * Decode and clamp a value. The formula is different from the spec because we27 * don't decode to float range [0,1], we decode it in the [0,max] range.28 */29 function decodeAndClamp(value, addend, coefficient, max) {30 value = addend + value * coefficient;31 // Clamp the value to the range32 return value < 0 ? 0 : value > max ? max : value;33 }34 function PDFImage(xref, res, image, inline, smask) {35 this.image = image;36 if (image.getParams) {37 // JPX/JPEG2000 streams directly contain bits per component38 // and color space mode information.39 TODO('get params from actual stream');40 // var bits = ...41 // var colorspace = ...42 }43 // TODO cache rendered images?44 var dict = image.dict;45 this.width = dict.get('Width', 'W');46 this.height = dict.get('Height', 'H');47 if (this.width < 1 || this.height < 1)48 error('Invalid image width: ' + this.width + ' or height: ' +49 this.height);50 this.interpolate = dict.get('Interpolate', 'I') || false;51 this.imageMask = dict.get('ImageMask', 'IM') || false;52 var bitsPerComponent = image.bitsPerComponent;53 if (!bitsPerComponent) {54 bitsPerComponent = dict.get('BitsPerComponent', 'BPC');55 if (!bitsPerComponent) {56 if (this.imageMask)57 bitsPerComponent = 1;58 else59 error('Bits per component missing in image: ' + this.imageMask);60 }61 }62 this.bpc = bitsPerComponent;63 if (!this.imageMask) {64 var colorSpace = dict.get('ColorSpace', 'CS');65 if (!colorSpace) {66 TODO('JPX images (which don"t require color spaces');67 colorSpace = new Name('DeviceRGB');68 }69 this.colorSpace = ColorSpace.parse(colorSpace, xref, res);70 this.numComps = this.colorSpace.numComps;71 }72 this.decode = dict.get('Decode', 'D');73 this.needsDecode = false;74 if (this.decode && this.colorSpace &&75 !this.colorSpace.isDefaultDecode(this.decode)) {76 this.needsDecode = true;77 // Do some preprocessing to avoid more math.78 var max = (1 << bitsPerComponent) - 1;79 this.decodeCoefficients = [];80 this.decodeAddends = [];81 for (var i = 0, j = 0; i < this.decode.length; i += 2, ++j) {82 var dmin = this.decode[i];83 var dmax = this.decode[i + 1];84 this.decodeCoefficients[j] = dmax - dmin;85 this.decodeAddends[j] = max * dmin;86 }87 }88 var mask = xref.fetchIfRef(dict.get('Mask'));89 if (mask) {90 TODO('masked images');91 } else if (smask) {92 this.smask = new PDFImage(xref, res, smask, false);93 }94 }95 /**96 * Handles processing of image data and calls the callback with an argument97 * of a PDFImage when the image is ready to be used.98 */99 PDFImage.buildImage = function buildImage(callback, handler, xref, res,100 image, inline) {101 var imageDataPromise = new Promise();102 var smaskPromise = new Promise();103 // The image data and smask data may not be ready yet, wait till both are104 // resolved.105 Promise.all([imageDataPromise, smaskPromise]).then(function(results) {106 var imageData = results[0], smaskData = results[1];107 var image = new PDFImage(xref, res, imageData, inline, smaskData);108 callback(image);109 });110 handleImageData(handler, xref, res, image, imageDataPromise);111 var smask = xref.fetchIfRef(image.dict.get('SMask'));112 if (smask)113 handleImageData(handler, xref, res, smask, smaskPromise);114 else115 smaskPromise.resolve(null);116 };117 /**118 * Resize an image using the nearest neighbor algorithm. Currently only119 * supports one and three component images.120 * @param {TypedArray} pixels The original image with one component.121 * @param {Number} bpc Number of bits per component.122 * @param {Number} components Number of color components, 1 or 3 is supported.123 * @param {Number} w1 Original width.124 * @param {Number} h1 Original height.125 * @param {Number} w2 New width.126 * @param {Number} h2 New height.127 * @return {TypedArray} Resized image data.128 */129 PDFImage.resize = function resize(pixels, bpc, components, w1, h1, w2, h2) {130 var length = w2 * h2 * components;131 var temp = bpc <= 8 ? new Uint8Array(length) :132 bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);133 var xRatio = w1 / w2;134 var yRatio = h1 / h2;135 var px, py, newIndex, oldIndex;136 for (var i = 0; i < h2; i++) {137 for (var j = 0; j < w2; j++) {138 px = Math.floor(j * xRatio);139 py = Math.floor(i * yRatio);140 newIndex = (i * w2) + j;141 oldIndex = ((py * w1) + px);142 if (components === 1) {143 temp[newIndex] = pixels[oldIndex];144 } else if (components === 3) {145 newIndex *= 3;146 oldIndex *= 3;147 temp[newIndex] = pixels[oldIndex];148 temp[newIndex + 1] = pixels[oldIndex + 1];149 temp[newIndex + 2] = pixels[oldIndex + 2];150 }151 }152 }153 return temp;154 };155 PDFImage.prototype = {156 get drawWidth() {157 if (!this.smask)158 return this.width;159 return Math.max(this.width, this.smask.width);160 },161 get drawHeight() {162 if (!this.smask)163 return this.height;164 return Math.max(this.height, this.smask.height);165 },166 getComponents: function getComponents(buffer) {167 var bpc = this.bpc;168 var needsDecode = this.needsDecode;169 var decodeMap = this.decode;170 // This image doesn't require any extra work.171 if (bpc == 8 && !needsDecode)172 return buffer;173 var bufferLength = buffer.length;174 var width = this.width;175 var height = this.height;176 var numComps = this.numComps;177 var length = width * height * numComps;178 var bufferPos = 0;179 var output = bpc <= 8 ? new Uint8Array(length) :180 bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);181 var rowComps = width * numComps;182 var decodeAddends, decodeCoefficients;183 if (needsDecode) {184 decodeAddends = this.decodeAddends;185 decodeCoefficients = this.decodeCoefficients;186 }187 var max = (1 << bpc) - 1;188 if (bpc == 8) {189 // Optimization for reading 8 bpc images that have a decode.190 for (var i = 0, ii = length; i < ii; ++i) {191 var compIndex = i % numComps;192 var value = buffer[i];193 value = decodeAndClamp(value, decodeAddends[compIndex],194 decodeCoefficients[compIndex], max);195 output[i] = value;196 }197 } else if (bpc == 1) {198 // Optimization for reading 1 bpc images.199 var valueZero = 0, valueOne = 1;200 if (decodeMap) {201 valueZero = decodeMap[0] ? 1 : 0;202 valueOne = decodeMap[1] ? 1 : 0;203 }204 var mask = 0;205 var buf = 0;206 for (var i = 0, ii = length; i < ii; ++i) {207 if (i % rowComps == 0) {208 mask = 0;209 buf = 0;210 } else {211 mask >>= 1;212 }213 if (mask <= 0) {214 buf = buffer[bufferPos++];215 mask = 128;216 }217 output[i] = !(buf & mask) ? valueZero : valueOne;218 }219 } else {220 // The general case that handles all other bpc values.221 var bits = 0, buf = 0;222 for (var i = 0, ii = length; i < ii; ++i) {223 if (i % rowComps == 0) {224 buf = 0;225 bits = 0;226 }227 while (bits < bpc) {228 buf = (buf << 8) | buffer[bufferPos++];229 bits += 8;230 }231 var remainingBits = bits - bpc;232 var value = buf >> remainingBits;233 if (needsDecode) {234 var compIndex = i % numComps;235 value = decodeAndClamp(value, decodeAddends[compIndex],236 decodeCoefficients[compIndex], max);237 }238 output[i] = value;239 buf = buf & ((1 << remainingBits) - 1);240 bits = remainingBits;241 }242 }243 return output;244 },245 getOpacity: function getOpacity(width, height) {246 var smask = this.smask;247 var originalWidth = this.width;248 var originalHeight = this.height;249 var buf;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var fs = require('fs');3var image = fs.readFileSync("test.jpg");4var decodedImage = wptools.decodeAndClamp(image);5fs.writeFileSync("decodedImage.jpg", decodedImage);6### wptools.decodeAndClamp(image, [callback])

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2 if(err) console.log(err);3 else console.log(res);4});5### decodeAndClamp(url, callback)6MIT © [Nathan Pannell](

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('./wptools.js');2var wiki = new wptools();3var page = process.argv[2];4var property = process.argv[3];5wiki.decodeAndClamp(page, property, function(err, result) {6 if (err) {7 console.log(err);8 } else {9 console.log(result);10 }11});

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