How to use DecodingContext method in wpt

Best JavaScript code snippet using wpt

jbig2.js

Source:jbig2.js Github

copy

Full Screen

...27 }28 return (this[id] = new Int8Array(1 << 16));29 }30 };31 function DecodingContext(data, start, end) {32 this.data = data;33 this.start = start;34 this.end = end;35 }36 DecodingContext.prototype = {37 get decoder() {38 var decoder = new ArithmeticDecoder(this.data, this.start, this.end);39 return shadow(this, 'decoder', decoder);40 },41 get contextCache() {42 var cache = new ContextCache();43 return shadow(this, 'contextCache', cache);44 }45 };46 // Annex A. Arithmetic Integer Decoding Procedure47 // A.2 Procedure for decoding values48 function decodeInteger(contextCache, procedure, decoder) {49 var contexts = contextCache.getContexts(procedure);50 var prev = 1;51 function readBits(length) {52 var v = 0;53 for (var i = 0; i < length; i++) {54 var bit = decoder.readBit(contexts, prev);55 prev = (prev < 256 ? (prev << 1) | bit :56 (((prev << 1) | bit) & 511) | 256);57 v = (v << 1) | bit;58 }59 return v >>> 0;60 }61 var sign = readBits(1);62 var value = readBits(1) ?63 (readBits(1) ?64 (readBits(1) ?65 (readBits(1) ?66 (readBits(1) ?67 (readBits(32) + 4436) :68 readBits(12) + 340) :69 readBits(8) + 84) :70 readBits(6) + 20) :71 readBits(4) + 4) :72 readBits(2);73 return (sign === 0 ? value : (value > 0 ? -value : null));74 }75 // A.3 The IAID decoding procedure76 function decodeIAID(contextCache, decoder, codeLength) {77 var contexts = contextCache.getContexts('IAID');78 var prev = 1;79 for (var i = 0; i < codeLength; i++) {80 var bit = decoder.readBit(contexts, prev);81 prev = (prev << 1) | bit;82 }83 if (codeLength < 31) {84 return prev & ((1 << codeLength) - 1);85 }86 return prev & 0x7FFFFFFF;87 }88 // 7.3 Segment types89 var SegmentTypes = [90 'SymbolDictionary', null, null, null, 'IntermediateTextRegion', null,91 'ImmediateTextRegion', 'ImmediateLosslessTextRegion', null, null, null,92 null, null, null, null, null, 'patternDictionary', null, null, null,93 'IntermediateHalftoneRegion', null, 'ImmediateHalftoneRegion',94 'ImmediateLosslessHalftoneRegion', null, null, null, null, null, null, null,95 null, null, null, null, null, 'IntermediateGenericRegion', null,96 'ImmediateGenericRegion', 'ImmediateLosslessGenericRegion',97 'IntermediateGenericRefinementRegion', null,98 'ImmediateGenericRefinementRegion',99 'ImmediateLosslessGenericRefinementRegion', null, null, null, null,100 'PageInformation', 'EndOfPage', 'EndOfStripe', 'EndOfFile', 'Profiles',101 'Tables', null, null, null, null, null, null, null, null,102 'Extension'103 ];104 var CodingTemplates = [105 [{x: -1, y: -2}, {x: 0, y: -2}, {x: 1, y: -2}, {x: -2, y: -1},106 {x: -1, y: -1}, {x: 0, y: -1}, {x: 1, y: -1}, {x: 2, y: -1},107 {x: -4, y: 0}, {x: -3, y: 0}, {x: -2, y: 0}, {x: -1, y: 0}],108 [{x: -1, y: -2}, {x: 0, y: -2}, {x: 1, y: -2}, {x: 2, y: -2},109 {x: -2, y: -1}, {x: -1, y: -1}, {x: 0, y: -1}, {x: 1, y: -1},110 {x: 2, y: -1}, {x: -3, y: 0}, {x: -2, y: 0}, {x: -1, y: 0}],111 [{x: -1, y: -2}, {x: 0, y: -2}, {x: 1, y: -2}, {x: -2, y: -1},112 {x: -1, y: -1}, {x: 0, y: -1}, {x: 1, y: -1}, {x: -2, y: 0},113 {x: -1, y: 0}],114 [{x: -3, y: -1}, {x: -2, y: -1}, {x: -1, y: -1}, {x: 0, y: -1},115 {x: 1, y: -1}, {x: -4, y: 0}, {x: -3, y: 0}, {x: -2, y: 0}, {x: -1, y: 0}]116 ];117 var RefinementTemplates = [118 {119 coding: [{x: 0, y: -1}, {x: 1, y: -1}, {x: -1, y: 0}],120 reference: [{x: 0, y: -1}, {x: 1, y: -1}, {x: -1, y: 0}, {x: 0, y: 0},121 {x: 1, y: 0}, {x: -1, y: 1}, {x: 0, y: 1}, {x: 1, y: 1}]122 },123 {124 coding: [{x: -1, y: -1}, {x: 0, y: -1}, {x: 1, y: -1}, {x: -1, y: 0}],125 reference: [{x: 0, y: -1}, {x: -1, y: 0}, {x: 0, y: 0}, {x: 1, y: 0},126 {x: 0, y: 1}, {x: 1, y: 1}]127 }128 ];129 // See 6.2.5.7 Decoding the bitmap.130 var ReusedContexts = [131 0x9B25, // 10011 0110010 0101132 0x0795, // 0011 110010 101133 0x00E5, // 001 11001 01134 0x0195 // 011001 0101135 ];136 var RefinementReusedContexts = [137 0x0020, // '000' + '0' (coding) + '00010000' + '0' (reference)138 0x0008 // '0000' + '001000'139 ];140 function decodeBitmapTemplate0(width, height, decodingContext) {141 var decoder = decodingContext.decoder;142 var contexts = decodingContext.contextCache.getContexts('GB');143 var contextLabel, i, j, pixel, row, row1, row2, bitmap = [];144 // ...ooooo....145 // ..ooooooo... Context template for current pixel (X)146 // .ooooX...... (concatenate values of 'o'-pixels to get contextLabel)147 var OLD_PIXEL_MASK = 0x7BF7; // 01111 0111111 0111148 for (i = 0; i < height; i++) {149 row = bitmap[i] = new Uint8Array(width);150 row1 = (i < 1) ? row : bitmap[i - 1];151 row2 = (i < 2) ? row : bitmap[i - 2];152 // At the beginning of each row:153 // Fill contextLabel with pixels that are above/right of (X)154 contextLabel = (row2[0] << 13) | (row2[1] << 12) | (row2[2] << 11) |155 (row1[0] << 7) | (row1[1] << 6) | (row1[2] << 5) |156 (row1[3] << 4);157 for (j = 0; j < width; j++) {158 row[j] = pixel = decoder.readBit(contexts, contextLabel);159 // At each pixel: Clear contextLabel pixels that are shifted160 // out of the context, then add new ones.161 contextLabel = ((contextLabel & OLD_PIXEL_MASK) << 1) |162 (j + 3 < width ? row2[j + 3] << 11 : 0) |163 (j + 4 < width ? row1[j + 4] << 4 : 0) | pixel;164 }165 }166 return bitmap;167 }168 // 6.2 Generic Region Decoding Procedure169 function decodeBitmap(mmr, width, height, templateIndex, prediction, skip, at,170 decodingContext) {171 if (mmr) {172 error('JBIG2 error: MMR encoding is not supported');173 }174 // Use optimized version for the most common case175 if (templateIndex === 0 && !skip && !prediction && at.length === 4 &&176 at[0].x === 3 && at[0].y === -1 && at[1].x === -3 && at[1].y === -1 &&177 at[2].x === 2 && at[2].y === -2 && at[3].x === -2 && at[3].y === -2) {178 return decodeBitmapTemplate0(width, height, decodingContext);179 }180 var useskip = !!skip;181 var template = CodingTemplates[templateIndex].concat(at);182 // Sorting is non-standard, and it is not required. But sorting increases183 // the number of template bits that can be reused from the previous184 // contextLabel in the main loop.185 template.sort(function (a, b) {186 return (a.y - b.y) || (a.x - b.x);187 });188 var templateLength = template.length;189 var templateX = new Int8Array(templateLength);190 var templateY = new Int8Array(templateLength);191 var changingTemplateEntries = [];192 var reuseMask = 0, minX = 0, maxX = 0, minY = 0;193 var c, k;194 for (k = 0; k < templateLength; k++) {195 templateX[k] = template[k].x;196 templateY[k] = template[k].y;197 minX = Math.min(minX, template[k].x);198 maxX = Math.max(maxX, template[k].x);199 minY = Math.min(minY, template[k].y);200 // Check if the template pixel appears in two consecutive context labels,201 // so it can be reused. Otherwise, we add it to the list of changing202 // template entries.203 if (k < templateLength - 1 &&204 template[k].y === template[k + 1].y &&205 template[k].x === template[k + 1].x - 1) {206 reuseMask |= 1 << (templateLength - 1 - k);207 } else {208 changingTemplateEntries.push(k);209 }210 }211 var changingEntriesLength = changingTemplateEntries.length;212 var changingTemplateX = new Int8Array(changingEntriesLength);213 var changingTemplateY = new Int8Array(changingEntriesLength);214 var changingTemplateBit = new Uint16Array(changingEntriesLength);215 for (c = 0; c < changingEntriesLength; c++) {216 k = changingTemplateEntries[c];217 changingTemplateX[c] = template[k].x;218 changingTemplateY[c] = template[k].y;219 changingTemplateBit[c] = 1 << (templateLength - 1 - k);220 }221 // Get the safe bounding box edges from the width, height, minX, maxX, minY222 var sbb_left = -minX;223 var sbb_top = -minY;224 var sbb_right = width - maxX;225 var pseudoPixelContext = ReusedContexts[templateIndex];226 var row = new Uint8Array(width);227 var bitmap = [];228 var decoder = decodingContext.decoder;229 var contexts = decodingContext.contextCache.getContexts('GB');230 var ltp = 0, j, i0, j0, contextLabel = 0, bit, shift;231 for (var i = 0; i < height; i++) {232 if (prediction) {233 var sltp = decoder.readBit(contexts, pseudoPixelContext);234 ltp ^= sltp;235 if (ltp) {236 bitmap.push(row); // duplicate previous row237 continue;238 }239 }240 row = new Uint8Array(row);241 bitmap.push(row);242 for (j = 0; j < width; j++) {243 if (useskip && skip[i][j]) {244 row[j] = 0;245 continue;246 }247 // Are we in the middle of a scanline, so we can reuse contextLabel248 // bits?249 if (j >= sbb_left && j < sbb_right && i >= sbb_top) {250 // If yes, we can just shift the bits that are reusable and only251 // fetch the remaining ones.252 contextLabel = (contextLabel << 1) & reuseMask;253 for (k = 0; k < changingEntriesLength; k++) {254 i0 = i + changingTemplateY[k];255 j0 = j + changingTemplateX[k];256 bit = bitmap[i0][j0];257 if (bit) {258 bit = changingTemplateBit[k];259 contextLabel |= bit;260 }261 }262 } else {263 // compute the contextLabel from scratch264 contextLabel = 0;265 shift = templateLength - 1;266 for (k = 0; k < templateLength; k++, shift--) {267 j0 = j + templateX[k];268 if (j0 >= 0 && j0 < width) {269 i0 = i + templateY[k];270 if (i0 >= 0) {271 bit = bitmap[i0][j0];272 if (bit) {273 contextLabel |= bit << shift;274 }275 }276 }277 }278 }279 var pixel = decoder.readBit(contexts, contextLabel);280 row[j] = pixel;281 }282 }283 return bitmap;284 }285 // 6.3.2 Generic Refinement Region Decoding Procedure286 function decodeRefinement(width, height, templateIndex, referenceBitmap,287 offsetX, offsetY, prediction, at,288 decodingContext) {289 var codingTemplate = RefinementTemplates[templateIndex].coding;290 if (templateIndex === 0) {291 codingTemplate = codingTemplate.concat([at[0]]);292 }293 var codingTemplateLength = codingTemplate.length;294 var codingTemplateX = new Int32Array(codingTemplateLength);295 var codingTemplateY = new Int32Array(codingTemplateLength);296 var k;297 for (k = 0; k < codingTemplateLength; k++) {298 codingTemplateX[k] = codingTemplate[k].x;299 codingTemplateY[k] = codingTemplate[k].y;300 }301 var referenceTemplate = RefinementTemplates[templateIndex].reference;302 if (templateIndex === 0) {303 referenceTemplate = referenceTemplate.concat([at[1]]);304 }305 var referenceTemplateLength = referenceTemplate.length;306 var referenceTemplateX = new Int32Array(referenceTemplateLength);307 var referenceTemplateY = new Int32Array(referenceTemplateLength);308 for (k = 0; k < referenceTemplateLength; k++) {309 referenceTemplateX[k] = referenceTemplate[k].x;310 referenceTemplateY[k] = referenceTemplate[k].y;311 }312 var referenceWidth = referenceBitmap[0].length;313 var referenceHeight = referenceBitmap.length;314 var pseudoPixelContext = RefinementReusedContexts[templateIndex];315 var bitmap = [];316 var decoder = decodingContext.decoder;317 var contexts = decodingContext.contextCache.getContexts('GR');318 var ltp = 0;319 for (var i = 0; i < height; i++) {320 if (prediction) {321 var sltp = decoder.readBit(contexts, pseudoPixelContext);322 ltp ^= sltp;323 if (ltp) {324 error('JBIG2 error: prediction is not supported');325 }326 }327 var row = new Uint8Array(width);328 bitmap.push(row);329 for (var j = 0; j < width; j++) {330 var i0, j0;331 var contextLabel = 0;332 for (k = 0; k < codingTemplateLength; k++) {333 i0 = i + codingTemplateY[k];334 j0 = j + codingTemplateX[k];335 if (i0 < 0 || j0 < 0 || j0 >= width) {336 contextLabel <<= 1; // out of bound pixel337 } else {338 contextLabel = (contextLabel << 1) | bitmap[i0][j0];339 }340 }341 for (k = 0; k < referenceTemplateLength; k++) {342 i0 = i + referenceTemplateY[k] + offsetY;343 j0 = j + referenceTemplateX[k] + offsetX;344 if (i0 < 0 || i0 >= referenceHeight || j0 < 0 ||345 j0 >= referenceWidth) {346 contextLabel <<= 1; // out of bound pixel347 } else {348 contextLabel = (contextLabel << 1) | referenceBitmap[i0][j0];349 }350 }351 var pixel = decoder.readBit(contexts, contextLabel);352 row[j] = pixel;353 }354 }355 return bitmap;356 }357 // 6.5.5 Decoding the symbol dictionary358 function decodeSymbolDictionary(huffman, refinement, symbols,359 numberOfNewSymbols, numberOfExportedSymbols,360 huffmanTables, templateIndex, at,361 refinementTemplateIndex, refinementAt,362 decodingContext) {363 if (huffman) {364 error('JBIG2 error: huffman is not supported');365 }366 var newSymbols = [];367 var currentHeight = 0;368 var symbolCodeLength = log2(symbols.length + numberOfNewSymbols);369 var decoder = decodingContext.decoder;370 var contextCache = decodingContext.contextCache;371 while (newSymbols.length < numberOfNewSymbols) {372 var deltaHeight = decodeInteger(contextCache, 'IADH', decoder); // 6.5.6373 currentHeight += deltaHeight;374 var currentWidth = 0;375 var totalWidth = 0;376 while (true) {377 var deltaWidth = decodeInteger(contextCache, 'IADW', decoder); // 6.5.7378 if (deltaWidth === null) {379 break; // OOB380 }381 currentWidth += deltaWidth;382 totalWidth += currentWidth;383 var bitmap;384 if (refinement) {385 // 6.5.8.2 Refinement/aggregate-coded symbol bitmap386 var numberOfInstances = decodeInteger(contextCache, 'IAAI', decoder);387 if (numberOfInstances > 1) {388 bitmap = decodeTextRegion(huffman, refinement,389 currentWidth, currentHeight, 0,390 numberOfInstances, 1, //strip size391 symbols.concat(newSymbols),392 symbolCodeLength,393 0, //transposed394 0, //ds offset395 1, //top left 7.4.3.1.1396 0, //OR operator397 huffmanTables,398 refinementTemplateIndex, refinementAt,399 decodingContext);400 } else {401 var symbolId = decodeIAID(contextCache, decoder, symbolCodeLength);402 var rdx = decodeInteger(contextCache, 'IARDX', decoder); // 6.4.11.3403 var rdy = decodeInteger(contextCache, 'IARDY', decoder); // 6.4.11.4404 var symbol = (symbolId < symbols.length ? symbols[symbolId] :405 newSymbols[symbolId - symbols.length]);406 bitmap = decodeRefinement(currentWidth, currentHeight,407 refinementTemplateIndex, symbol, rdx, rdy, false, refinementAt,408 decodingContext);409 }410 } else {411 // 6.5.8.1 Direct-coded symbol bitmap412 bitmap = decodeBitmap(false, currentWidth, currentHeight,413 templateIndex, false, null, at, decodingContext);414 }415 newSymbols.push(bitmap);416 }417 }418 // 6.5.10 Exported symbols419 var exportedSymbols = [];420 var flags = [], currentFlag = false;421 var totalSymbolsLength = symbols.length + numberOfNewSymbols;422 while (flags.length < totalSymbolsLength) {423 var runLength = decodeInteger(contextCache, 'IAEX', decoder);424 while (runLength--) {425 flags.push(currentFlag);426 }427 currentFlag = !currentFlag;428 }429 for (var i = 0, ii = symbols.length; i < ii; i++) {430 if (flags[i]) {431 exportedSymbols.push(symbols[i]);432 }433 }434 for (var j = 0; j < numberOfNewSymbols; i++, j++) {435 if (flags[i]) {436 exportedSymbols.push(newSymbols[j]);437 }438 }439 return exportedSymbols;440 }441 function decodeTextRegion(huffman, refinement, width, height,442 defaultPixelValue, numberOfSymbolInstances,443 stripSize, inputSymbols, symbolCodeLength,444 transposed, dsOffset, referenceCorner,445 combinationOperator, huffmanTables,446 refinementTemplateIndex, refinementAt,447 decodingContext) {448 if (huffman) {449 error('JBIG2 error: huffman is not supported');450 }451 // Prepare bitmap452 var bitmap = [];453 var i, row;454 for (i = 0; i < height; i++) {455 row = new Uint8Array(width);456 if (defaultPixelValue) {457 for (var j = 0; j < width; j++) {458 row[j] = defaultPixelValue;459 }460 }461 bitmap.push(row);462 }463 var decoder = decodingContext.decoder;464 var contextCache = decodingContext.contextCache;465 var stripT = -decodeInteger(contextCache, 'IADT', decoder); // 6.4.6466 var firstS = 0;467 i = 0;468 while (i < numberOfSymbolInstances) {469 var deltaT = decodeInteger(contextCache, 'IADT', decoder); // 6.4.6470 stripT += deltaT;471 var deltaFirstS = decodeInteger(contextCache, 'IAFS', decoder); // 6.4.7472 firstS += deltaFirstS;473 var currentS = firstS;474 do {475 var currentT = (stripSize === 1 ? 0 :476 decodeInteger(contextCache, 'IAIT', decoder)); // 6.4.9477 var t = stripSize * stripT + currentT;478 var symbolId = decodeIAID(contextCache, decoder, symbolCodeLength);479 var applyRefinement = (refinement &&480 decodeInteger(contextCache, 'IARI', decoder));481 var symbolBitmap = inputSymbols[symbolId];482 var symbolWidth = symbolBitmap[0].length;483 var symbolHeight = symbolBitmap.length;484 if (applyRefinement) {485 var rdw = decodeInteger(contextCache, 'IARDW', decoder); // 6.4.11.1486 var rdh = decodeInteger(contextCache, 'IARDH', decoder); // 6.4.11.2487 var rdx = decodeInteger(contextCache, 'IARDX', decoder); // 6.4.11.3488 var rdy = decodeInteger(contextCache, 'IARDY', decoder); // 6.4.11.4489 symbolWidth += rdw;490 symbolHeight += rdh;491 symbolBitmap = decodeRefinement(symbolWidth, symbolHeight,492 refinementTemplateIndex, symbolBitmap, (rdw >> 1) + rdx,493 (rdh >> 1) + rdy, false, refinementAt,494 decodingContext);495 }496 var offsetT = t - ((referenceCorner & 1) ? 0 : symbolHeight);497 var offsetS = currentS - ((referenceCorner & 2) ? symbolWidth : 0);498 var s2, t2, symbolRow;499 if (transposed) {500 // Place Symbol Bitmap from T1,S1501 for (s2 = 0; s2 < symbolHeight; s2++) {502 row = bitmap[offsetS + s2];503 if (!row) {504 continue;505 }506 symbolRow = symbolBitmap[s2];507 // To ignore Parts of Symbol bitmap which goes508 // outside bitmap region509 var maxWidth = Math.min(width - offsetT, symbolWidth);510 switch (combinationOperator) {511 case 0: // OR512 for (t2 = 0; t2 < maxWidth; t2++) {513 row[offsetT + t2] |= symbolRow[t2];514 }515 break;516 case 2: // XOR517 for (t2 = 0; t2 < maxWidth; t2++) {518 row[offsetT + t2] ^= symbolRow[t2];519 }520 break;521 default:522 error('JBIG2 error: operator ' + combinationOperator +523 ' is not supported');524 }525 }526 currentS += symbolHeight - 1;527 } else {528 for (t2 = 0; t2 < symbolHeight; t2++) {529 row = bitmap[offsetT + t2];530 if (!row) {531 continue;532 }533 symbolRow = symbolBitmap[t2];534 switch (combinationOperator) {535 case 0: // OR536 for (s2 = 0; s2 < symbolWidth; s2++) {537 row[offsetS + s2] |= symbolRow[s2];538 }539 break;540 case 2: // XOR541 for (s2 = 0; s2 < symbolWidth; s2++) {542 row[offsetS + s2] ^= symbolRow[s2];543 }544 break;545 default:546 error('JBIG2 error: operator ' + combinationOperator +547 ' is not supported');548 }549 }550 currentS += symbolWidth - 1;551 }552 i++;553 var deltaS = decodeInteger(contextCache, 'IADS', decoder); // 6.4.8554 if (deltaS === null) {555 break; // OOB556 }557 currentS += deltaS + dsOffset;558 } while (true);559 }560 return bitmap;561 }562 function readSegmentHeader(data, start) {563 var segmentHeader = {};564 segmentHeader.number = readUint32(data, start);565 var flags = data[start + 4];566 var segmentType = flags & 0x3F;567 if (!SegmentTypes[segmentType]) {568 error('JBIG2 error: invalid segment type: ' + segmentType);569 }570 segmentHeader.type = segmentType;571 segmentHeader.typeName = SegmentTypes[segmentType];572 segmentHeader.deferredNonRetain = !!(flags & 0x80);573 var pageAssociationFieldSize = !!(flags & 0x40);574 var referredFlags = data[start + 5];575 var referredToCount = (referredFlags >> 5) & 7;576 var retainBits = [referredFlags & 31];577 var position = start + 6;578 if (referredFlags === 7) {579 referredToCount = readUint32(data, position - 1) & 0x1FFFFFFF;580 position += 3;581 var bytes = (referredToCount + 7) >> 3;582 retainBits[0] = data[position++];583 while (--bytes > 0) {584 retainBits.push(data[position++]);585 }586 } else if (referredFlags === 5 || referredFlags === 6) {587 error('JBIG2 error: invalid referred-to flags');588 }589 segmentHeader.retainBits = retainBits;590 var referredToSegmentNumberSize = (segmentHeader.number <= 256 ? 1 :591 (segmentHeader.number <= 65536 ? 2 : 4));592 var referredTo = [];593 var i, ii;594 for (i = 0; i < referredToCount; i++) {595 var number = (referredToSegmentNumberSize === 1 ? data[position] :596 (referredToSegmentNumberSize === 2 ? readUint16(data, position) :597 readUint32(data, position)));598 referredTo.push(number);599 position += referredToSegmentNumberSize;600 }601 segmentHeader.referredTo = referredTo;602 if (!pageAssociationFieldSize) {603 segmentHeader.pageAssociation = data[position++];604 } else {605 segmentHeader.pageAssociation = readUint32(data, position);606 position += 4;607 }608 segmentHeader.length = readUint32(data, position);609 position += 4;610 if (segmentHeader.length === 0xFFFFFFFF) {611 // 7.2.7 Segment data length, unknown segment length612 if (segmentType === 38) { // ImmediateGenericRegion613 var genericRegionInfo = readRegionSegmentInformation(data, position);614 var genericRegionSegmentFlags = data[position +615 RegionSegmentInformationFieldLength];616 var genericRegionMmr = !!(genericRegionSegmentFlags & 1);617 // searching for the segment end618 var searchPatternLength = 6;619 var searchPattern = new Uint8Array(searchPatternLength);620 if (!genericRegionMmr) {621 searchPattern[0] = 0xFF;622 searchPattern[1] = 0xAC;623 }624 searchPattern[2] = (genericRegionInfo.height >>> 24) & 0xFF;625 searchPattern[3] = (genericRegionInfo.height >> 16) & 0xFF;626 searchPattern[4] = (genericRegionInfo.height >> 8) & 0xFF;627 searchPattern[5] = genericRegionInfo.height & 0xFF;628 for (i = position, ii = data.length; i < ii; i++) {629 var j = 0;630 while (j < searchPatternLength && searchPattern[j] === data[i + j]) {631 j++;632 }633 if (j === searchPatternLength) {634 segmentHeader.length = i + searchPatternLength;635 break;636 }637 }638 if (segmentHeader.length === 0xFFFFFFFF) {639 error('JBIG2 error: segment end was not found');640 }641 } else {642 error('JBIG2 error: invalid unknown segment length');643 }644 }645 segmentHeader.headerEnd = position;646 return segmentHeader;647 }648 function readSegments(header, data, start, end) {649 var segments = [];650 var position = start;651 while (position < end) {652 var segmentHeader = readSegmentHeader(data, position);653 position = segmentHeader.headerEnd;654 var segment = {655 header: segmentHeader,656 data: data657 };658 if (!header.randomAccess) {659 segment.start = position;660 position += segmentHeader.length;661 segment.end = position;662 }663 segments.push(segment);664 if (segmentHeader.type === 51) {665 break; // end of file is found666 }667 }668 if (header.randomAccess) {669 for (var i = 0, ii = segments.length; i < ii; i++) {670 segments[i].start = position;671 position += segments[i].header.length;672 segments[i].end = position;673 }674 }675 return segments;676 }677 // 7.4.1 Region segment information field678 function readRegionSegmentInformation(data, start) {679 return {680 width: readUint32(data, start),681 height: readUint32(data, start + 4),682 x: readUint32(data, start + 8),683 y: readUint32(data, start + 12),684 combinationOperator: data[start + 16] & 7685 };686 }687 var RegionSegmentInformationFieldLength = 17;688 function processSegment(segment, visitor) {689 var header = segment.header;690 var data = segment.data, position = segment.start, end = segment.end;691 var args, at, i, atLength;692 switch (header.type) {693 case 0: // SymbolDictionary694 // 7.4.2 Symbol dictionary segment syntax695 var dictionary = {};696 var dictionaryFlags = readUint16(data, position); // 7.4.2.1.1697 dictionary.huffman = !!(dictionaryFlags & 1);698 dictionary.refinement = !!(dictionaryFlags & 2);699 dictionary.huffmanDHSelector = (dictionaryFlags >> 2) & 3;700 dictionary.huffmanDWSelector = (dictionaryFlags >> 4) & 3;701 dictionary.bitmapSizeSelector = (dictionaryFlags >> 6) & 1;702 dictionary.aggregationInstancesSelector = (dictionaryFlags >> 7) & 1;703 dictionary.bitmapCodingContextUsed = !!(dictionaryFlags & 256);704 dictionary.bitmapCodingContextRetained = !!(dictionaryFlags & 512);705 dictionary.template = (dictionaryFlags >> 10) & 3;706 dictionary.refinementTemplate = (dictionaryFlags >> 12) & 1;707 position += 2;708 if (!dictionary.huffman) {709 atLength = dictionary.template === 0 ? 4 : 1;710 at = [];711 for (i = 0; i < atLength; i++) {712 at.push({713 x: readInt8(data, position),714 y: readInt8(data, position + 1)715 });716 position += 2;717 }718 dictionary.at = at;719 }720 if (dictionary.refinement && !dictionary.refinementTemplate) {721 at = [];722 for (i = 0; i < 2; i++) {723 at.push({724 x: readInt8(data, position),725 y: readInt8(data, position + 1)726 });727 position += 2;728 }729 dictionary.refinementAt = at;730 }731 dictionary.numberOfExportedSymbols = readUint32(data, position);732 position += 4;733 dictionary.numberOfNewSymbols = readUint32(data, position);734 position += 4;735 args = [dictionary, header.number, header.referredTo,736 data, position, end];737 break;738 case 6: // ImmediateTextRegion739 case 7: // ImmediateLosslessTextRegion740 var textRegion = {};741 textRegion.info = readRegionSegmentInformation(data, position);742 position += RegionSegmentInformationFieldLength;743 var textRegionSegmentFlags = readUint16(data, position);744 position += 2;745 textRegion.huffman = !!(textRegionSegmentFlags & 1);746 textRegion.refinement = !!(textRegionSegmentFlags & 2);747 textRegion.stripSize = 1 << ((textRegionSegmentFlags >> 2) & 3);748 textRegion.referenceCorner = (textRegionSegmentFlags >> 4) & 3;749 textRegion.transposed = !!(textRegionSegmentFlags & 64);750 textRegion.combinationOperator = (textRegionSegmentFlags >> 7) & 3;751 textRegion.defaultPixelValue = (textRegionSegmentFlags >> 9) & 1;752 textRegion.dsOffset = (textRegionSegmentFlags << 17) >> 27;753 textRegion.refinementTemplate = (textRegionSegmentFlags >> 15) & 1;754 if (textRegion.huffman) {755 var textRegionHuffmanFlags = readUint16(data, position);756 position += 2;757 textRegion.huffmanFS = (textRegionHuffmanFlags) & 3;758 textRegion.huffmanDS = (textRegionHuffmanFlags >> 2) & 3;759 textRegion.huffmanDT = (textRegionHuffmanFlags >> 4) & 3;760 textRegion.huffmanRefinementDW = (textRegionHuffmanFlags >> 6) & 3;761 textRegion.huffmanRefinementDH = (textRegionHuffmanFlags >> 8) & 3;762 textRegion.huffmanRefinementDX = (textRegionHuffmanFlags >> 10) & 3;763 textRegion.huffmanRefinementDY = (textRegionHuffmanFlags >> 12) & 3;764 textRegion.huffmanRefinementSizeSelector =765 !!(textRegionHuffmanFlags & 14);766 }767 if (textRegion.refinement && !textRegion.refinementTemplate) {768 at = [];769 for (i = 0; i < 2; i++) {770 at.push({771 x: readInt8(data, position),772 y: readInt8(data, position + 1)773 });774 position += 2;775 }776 textRegion.refinementAt = at;777 }778 textRegion.numberOfSymbolInstances = readUint32(data, position);779 position += 4;780 // TODO 7.4.3.1.7 Symbol ID Huffman table decoding781 if (textRegion.huffman) {782 error('JBIG2 error: huffman is not supported');783 }784 args = [textRegion, header.referredTo, data, position, end];785 break;786 case 38: // ImmediateGenericRegion787 case 39: // ImmediateLosslessGenericRegion788 var genericRegion = {};789 genericRegion.info = readRegionSegmentInformation(data, position);790 position += RegionSegmentInformationFieldLength;791 var genericRegionSegmentFlags = data[position++];792 genericRegion.mmr = !!(genericRegionSegmentFlags & 1);793 genericRegion.template = (genericRegionSegmentFlags >> 1) & 3;794 genericRegion.prediction = !!(genericRegionSegmentFlags & 8);795 if (!genericRegion.mmr) {796 atLength = genericRegion.template === 0 ? 4 : 1;797 at = [];798 for (i = 0; i < atLength; i++) {799 at.push({800 x: readInt8(data, position),801 y: readInt8(data, position + 1)802 });803 position += 2;804 }805 genericRegion.at = at;806 }807 args = [genericRegion, data, position, end];808 break;809 case 48: // PageInformation810 var pageInfo = {811 width: readUint32(data, position),812 height: readUint32(data, position + 4),813 resolutionX: readUint32(data, position + 8),814 resolutionY: readUint32(data, position + 12)815 };816 if (pageInfo.height === 0xFFFFFFFF) {817 delete pageInfo.height;818 }819 var pageSegmentFlags = data[position + 16];820 var pageStripingInformatiom = readUint16(data, position + 17);821 pageInfo.lossless = !!(pageSegmentFlags & 1);822 pageInfo.refinement = !!(pageSegmentFlags & 2);823 pageInfo.defaultPixelValue = (pageSegmentFlags >> 2) & 1;824 pageInfo.combinationOperator = (pageSegmentFlags >> 3) & 3;825 pageInfo.requiresBuffer = !!(pageSegmentFlags & 32);826 pageInfo.combinationOperatorOverride = !!(pageSegmentFlags & 64);827 args = [pageInfo];828 break;829 case 49: // EndOfPage830 break;831 case 50: // EndOfStripe832 break;833 case 51: // EndOfFile834 break;835 case 62: // 7.4.15 defines 2 extension types which836 // are comments and can be ignored.837 break;838 default:839 error('JBIG2 error: segment type ' + header.typeName + '(' +840 header.type + ') is not implemented');841 }842 var callbackName = 'on' + header.typeName;843 if (callbackName in visitor) {844 visitor[callbackName].apply(visitor, args);845 }846 }847 function processSegments(segments, visitor) {848 for (var i = 0, ii = segments.length; i < ii; i++) {849 processSegment(segments[i], visitor);850 }851 }852 function parseJbig2(data, start, end) {853 var position = start;854 if (data[position] !== 0x97 || data[position + 1] !== 0x4A ||855 data[position + 2] !== 0x42 || data[position + 3] !== 0x32 ||856 data[position + 4] !== 0x0D || data[position + 5] !== 0x0A ||857 data[position + 6] !== 0x1A || data[position + 7] !== 0x0A) {858 error('JBIG2 error: invalid header');859 }860 var header = {};861 position += 8;862 var flags = data[position++];863 header.randomAccess = !(flags & 1);864 if (!(flags & 2)) {865 header.numberOfPages = readUint32(data, position);866 position += 4;867 }868 var segments = readSegments(header, data, position, end);869 error('Not implemented');870 // processSegments(segments, new SimpleSegmentVisitor());871 }872 function parseJbig2Chunks(chunks) {873 var visitor = new SimpleSegmentVisitor();874 for (var i = 0, ii = chunks.length; i < ii; i++) {875 var chunk = chunks[i];876 var segments = readSegments({}, chunk.data, chunk.start, chunk.end);877 processSegments(segments, visitor);878 }879 return visitor.buffer;880 }881 function SimpleSegmentVisitor() {}882 SimpleSegmentVisitor.prototype = {883 onPageInformation: function SimpleSegmentVisitor_onPageInformation(info) {884 this.currentPageInfo = info;885 var rowSize = (info.width + 7) >> 3;886 var buffer = new Uint8Array(rowSize * info.height);887 // The contents of ArrayBuffers are initialized to 0.888 // Fill the buffer with 0xFF only if info.defaultPixelValue is set889 if (info.defaultPixelValue) {890 for (var i = 0, ii = buffer.length; i < ii; i++) {891 buffer[i] = 0xFF;892 }893 }894 this.buffer = buffer;895 },896 drawBitmap: function SimpleSegmentVisitor_drawBitmap(regionInfo, bitmap) {897 var pageInfo = this.currentPageInfo;898 var width = regionInfo.width, height = regionInfo.height;899 var rowSize = (pageInfo.width + 7) >> 3;900 var combinationOperator = pageInfo.combinationOperatorOverride ?901 regionInfo.combinationOperator : pageInfo.combinationOperator;902 var buffer = this.buffer;903 var mask0 = 128 >> (regionInfo.x & 7);904 var offset0 = regionInfo.y * rowSize + (regionInfo.x >> 3);905 var i, j, mask, offset;906 switch (combinationOperator) {907 case 0: // OR908 for (i = 0; i < height; i++) {909 mask = mask0;910 offset = offset0;911 for (j = 0; j < width; j++) {912 if (bitmap[i][j]) {913 buffer[offset] |= mask;914 }915 mask >>= 1;916 if (!mask) {917 mask = 128;918 offset++;919 }920 }921 offset0 += rowSize;922 }923 break;924 case 2: // XOR925 for (i = 0; i < height; i++) {926 mask = mask0;927 offset = offset0;928 for (j = 0; j < width; j++) {929 if (bitmap[i][j]) {930 buffer[offset] ^= mask;931 }932 mask >>= 1;933 if (!mask) {934 mask = 128;935 offset++;936 }937 }938 offset0 += rowSize;939 }940 break;941 default:942 error('JBIG2 error: operator ' + combinationOperator +943 ' is not supported');944 }945 },946 onImmediateGenericRegion:947 function SimpleSegmentVisitor_onImmediateGenericRegion(region, data,948 start, end) {949 var regionInfo = region.info;950 var decodingContext = new DecodingContext(data, start, end);951 var bitmap = decodeBitmap(region.mmr, regionInfo.width, regionInfo.height,952 region.template, region.prediction, null,953 region.at, decodingContext);954 this.drawBitmap(regionInfo, bitmap);955 },956 onImmediateLosslessGenericRegion:957 function SimpleSegmentVisitor_onImmediateLosslessGenericRegion() {958 this.onImmediateGenericRegion.apply(this, arguments);959 },960 onSymbolDictionary:961 function SimpleSegmentVisitor_onSymbolDictionary(dictionary,962 currentSegment,963 referredSegments,964 data, start, end) {965 var huffmanTables;966 if (dictionary.huffman) {967 error('JBIG2 error: huffman is not supported');968 }969 // Combines exported symbols from all referred segments970 var symbols = this.symbols;971 if (!symbols) {972 this.symbols = symbols = {};973 }974 var inputSymbols = [];975 for (var i = 0, ii = referredSegments.length; i < ii; i++) {976 inputSymbols = inputSymbols.concat(symbols[referredSegments[i]]);977 }978 var decodingContext = new DecodingContext(data, start, end);979 symbols[currentSegment] = decodeSymbolDictionary(dictionary.huffman,980 dictionary.refinement, inputSymbols, dictionary.numberOfNewSymbols,981 dictionary.numberOfExportedSymbols, huffmanTables,982 dictionary.template, dictionary.at,983 dictionary.refinementTemplate, dictionary.refinementAt,984 decodingContext);985 },986 onImmediateTextRegion:987 function SimpleSegmentVisitor_onImmediateTextRegion(region,988 referredSegments,989 data, start, end) {990 var regionInfo = region.info;991 var huffmanTables;992 // Combines exported symbols from all referred segments993 var symbols = this.symbols;994 var inputSymbols = [];995 for (var i = 0, ii = referredSegments.length; i < ii; i++) {996 inputSymbols = inputSymbols.concat(symbols[referredSegments[i]]);997 }998 var symbolCodeLength = log2(inputSymbols.length);999 var decodingContext = new DecodingContext(data, start, end);1000 var bitmap = decodeTextRegion(region.huffman, region.refinement,1001 regionInfo.width, regionInfo.height, region.defaultPixelValue,1002 region.numberOfSymbolInstances, region.stripSize, inputSymbols,1003 symbolCodeLength, region.transposed, region.dsOffset,1004 region.referenceCorner, region.combinationOperator, huffmanTables,1005 region.refinementTemplate, region.refinementAt, decodingContext);1006 this.drawBitmap(regionInfo, bitmap);1007 },1008 onImmediateLosslessTextRegion:1009 function SimpleSegmentVisitor_onImmediateLosslessTextRegion() {1010 this.onImmediateTextRegion.apply(this, arguments);1011 }1012 };1013 function Jbig2Image() {}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var page = wptools.page('Barack Obama');8page.get(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var page = wptools.page('Barack Obama');13page.get(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var page = wptools.page('Barack Obama');18page.get(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22var page = wptools.page('Barack Obama');23page.get(function(err, resp) {24 console.log(resp);25});26var wptools = require('wptools');27var page = wptools.page('Barack Obama');28page.get(function(err, resp) {29 console.log(resp);30});31var wptools = require('wptools');32var page = wptools.page('Barack Obama');33page.get(function(err, resp) {34 console.log(resp);35});36var wptools = require('wptools');37var page = wptools.page('Barack Obama');38page.get(function(err, resp) {39 console.log(resp);40});41var wptools = require('wptools');42var page = wptools.page('Barack Obama');43page.get(function(err, resp) {44 console.log(resp);45});

Full Screen

Using AI Code Generation

copy

Full Screen

1const wpt_encoder = require('wpt-encoder');2let wpt_decoder = new wpt_encoder.DecodingContext();3let wpt_encoder = new wpt_encoder.EncodingContext();4let wpt_encoded = wpt_encoder.encode("string to encode");5let wpt_decoded = wpt_decoder.decode(wpt_encoded);6console.log(wpt_decoded);7var decoder = new TexDecoder();8var cntext = decder.decode();9var string = context.decode([0x61, 0x62, 0x63]);10console.log(string);11var encoder = new TextEncoder();12var context = encoder.encode();13var bytes = context.encode("abc");14console.og(bytes);15var decoder = new TextDecoderStream();16var readableStream = decoder.readable;17var writableStream = decoder.wrtable;18var reader = readableStrea.getReader();19var writer = writableStream.getWriter();20writer.write([0x61, 0x62, 0x63]);21writer.close();22reader.read().then(function(result) {23 console.log(result.value);24});25var encoder = new TextEncoderStream();26var readableStream = encoder.readable;27var wrtableStream = ecoder.writable;28var reader = readableStream.getReader();29var writer = writableStream.etWriter();30writer.write("abc");31writer.close();32reader.read().then(function(result){33 console.log(result.value);34});35var decoder = new TextDecoderStream();36var readableStream = decoder.readable;37var writableStream = decoder.writable;38var reader = readableStream.getReader();39var writer = writableStream.getWriter();40writer.write([0x61, 0x62, 0x63]);41writer.close();42reader.read().then(function(result) {43 console.log(result.value);44});45var encoder = new TextEncoderStream();46var readableStream = encoder.readable;47var writableStream = encoder.writable;48var reader = readableStream.getReader();49var writer = writableStream.getWriter();50writer.write("abc");51writer.close();52reader.read().then(function(result) {53 console.log(result.value);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var context = new wptools.DecodingContext();3var input = 'Manchester United F.C.';4var output = context.decode(input);5var wptools = require('wptools');6var context = new wptools.DecodingContext();7var input = 'Manchester United F.C.';8var output = context.decode(input);9var wptools = require('wptools');10var context = new wptools.DecodingContext();11var input = 'Manchester United F.C.';12var output = context.decode(input);13var wptools = require('wptools');14var context = new wptools.DecodingContext();15var input = 'Manchester United F.C.';16var output = context.decode(input);17var wptools = require('wptools');18var context = new wptools.DecodingContext();19var input = 'Manchester United F.C.';20var output = context.decode(input);21var wptools = require('wptools');22var context = new wptools.DecodingContext();23var input = 'Manchester United F.C.';24var output = context.decode(input);25var wptools = require('wptools'uire('

Full Screen

Using AI Code Generation

copy

Full Screen

1var text = "Im a \"string\" & I need to be 'encoded' < >";2var context = new DecodingContext();3var encoded = context.wptexturize(text);4console.log(encoded);5var text = "I'm a \"string\" & I need to be 'encoded' < >";6var context = new DecodingContext();7var encoded = context.wptexturize(text);8console.log(encoded);9var text = "I'm a \"string\" & I need to be 'encoded' < >";10var context = new DecodingContext();11var encoded = context.wptexturize(text);12console.log(encoded);13var text = "I'm a \"string\" & I need to be 'encoded' < >";14var context = new DecodingContext();15var encoded = context.wptexturize(text);16console.log(encoded);17var text = "I'm a \"string\" & I need to be 'encoded' < >";18var context = new DecodingContext();19var encoded = context.wptexturize(text);20console.log(encoded);21var text = "I'm a \"string\" & I need to be 'encoded' < >";22var context = new DecodingContext();23var encoded = context.wptexturize(text);24console.log(encoded);25var text = "I'm a \"string\" & I need to be 'encoded' < >";26var context = new DecodingContext();27var encoded = context.wptexturize(text);28console.log(encoded);29var text = "I'm a \"string\" & I need to be 'encoded' < >";30var context = new DecodingContext();31var encoded = context.wptexturize(text);32console.log(encoded);33var text = "I'm a \"string\" & I need to be 'encoded' < >";34var context = new wptools.DecodingContext();35var input = 'Manchester United F.C.';36var output = context.decode(input);37var wptools = require('wptools');38var context = new wptools.DecodingContext();39var input = 'Manchester United F.C.';40var output = context.decode(input);41console.log(output);

Full Screen

Using AI Code Generation

copy

Full Screen

1var decoder = new TextDecoder();2var context = decoder.decode();3var string = context.decode([0x61, 0x62, 0x63]);4console.log(string);5var encoder = new TextEncoder();6var context = encoder.encode();7var bytes = context.encode("abc");8console.log(bytes);9var decoder = new TextDecoderStream();10var readableStream = decoder.readable;11var writableStream = decoder.writable;12var reader = readableStream.getReader();13var writer = writableStream.getWriter();14writer.write([0x61, 0x62, 0x63]);15writer.close();16reader.read().then(function(result) {17 console.log(result.value);18});19var encoder = new TextEncoderStream();20var readableStream = encoder.readable;21var writableStream = encoder.writable;22var reader = readableStream.getReader();23var writer = writableStream.getWriter();24writer.write("abc");25writer.close();26reader.read().then(function(result) {27 console.log(result.value);28});29var decoder = new TextDecoderStream();30var readableStream = decoder.readable;31var writableStream = decoder.writable;32var reader = readableStream.getReader();33var writer = writableStream.getWriter();34writer.write([0x61, 0x62, 0x63]);35writer.close();36reader.read().then(function(result) {37 console.log(result.value);38});39var encoder = new TextEncoderStream();40var readableStream = encoder.readable;41var writableStream = encoder.writable;42var reader = readableStream.getReader();43var writer = writableStream.getWriter();44writer.write("abc");45writer.close();46reader.read().then(function(result) {47 console.log(result.value);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { DecodingContext } = require('wptools');2const dc = new DecodingContext();3console.log(url);4const { DecodingContext } = require('wptools');5const dc = new DecodingContext();6console.log(url);7const { DecodingContext } = require('wptools');8const dc = new DecodingContext();9console.log(url);10const { DecodingContext } = require('wptools');11const dc = new DecodingContext();12console.log(url);13const { DecodingContext } = require('wptools');14const dc = new DecodingContext();15console.log(url);16const { DecodingContext } = require('

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