| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952 | 
							- /* Copyright 2017 Mozilla Foundation
 
-  *
 
-  * Licensed under the Apache License, Version 2.0 (the "License");
 
-  * you may not use this file except in compliance with the License.
 
-  * You may obtain a copy of the License at
 
-  *
 
-  *     http://www.apache.org/licenses/LICENSE-2.0
 
-  *
 
-  * Unless required by applicable law or agreed to in writing, software
 
-  * distributed under the License is distributed on an "AS IS" BASIS,
 
-  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
-  * See the License for the specific language governing permissions and
 
-  * limitations under the License.
 
-  */
 
- 'use strict';
 
- Object.defineProperty(exports, "__esModule", {
 
-   value: true
 
- });
 
- exports.JpxImage = undefined;
 
- var _util = require('../shared/util');
 
- var _arithmetic_decoder = require('./arithmetic_decoder');
 
- var JpxError = function JpxErrorClosure() {
 
-   function JpxError(msg) {
 
-     this.message = 'JPX error: ' + msg;
 
-   }
 
-   JpxError.prototype = new Error();
 
-   JpxError.prototype.name = 'JpxError';
 
-   JpxError.constructor = JpxError;
 
-   return JpxError;
 
- }();
 
- var JpxImage = function JpxImageClosure() {
 
-   var SubbandsGainLog2 = {
 
-     'LL': 0,
 
-     'LH': 1,
 
-     'HL': 1,
 
-     'HH': 2
 
-   };
 
-   function JpxImage() {
 
-     this.failOnCorruptedImage = false;
 
-   }
 
-   JpxImage.prototype = {
 
-     parse: function JpxImage_parse(data) {
 
-       var head = (0, _util.readUint16)(data, 0);
 
-       if (head === 0xFF4F) {
 
-         this.parseCodestream(data, 0, data.length);
 
-         return;
 
-       }
 
-       var position = 0,
 
-           length = data.length;
 
-       while (position < length) {
 
-         var headerSize = 8;
 
-         var lbox = (0, _util.readUint32)(data, position);
 
-         var tbox = (0, _util.readUint32)(data, position + 4);
 
-         position += headerSize;
 
-         if (lbox === 1) {
 
-           lbox = (0, _util.readUint32)(data, position) * 4294967296 + (0, _util.readUint32)(data, position + 4);
 
-           position += 8;
 
-           headerSize += 8;
 
-         }
 
-         if (lbox === 0) {
 
-           lbox = length - position + headerSize;
 
-         }
 
-         if (lbox < headerSize) {
 
-           throw new JpxError('Invalid box field size');
 
-         }
 
-         var dataLength = lbox - headerSize;
 
-         var jumpDataLength = true;
 
-         switch (tbox) {
 
-           case 0x6A703268:
 
-             jumpDataLength = false;
 
-             break;
 
-           case 0x636F6C72:
 
-             var method = data[position];
 
-             if (method === 1) {
 
-               var colorspace = (0, _util.readUint32)(data, position + 3);
 
-               switch (colorspace) {
 
-                 case 16:
 
-                 case 17:
 
-                 case 18:
 
-                   break;
 
-                 default:
 
-                   (0, _util.warn)('Unknown colorspace ' + colorspace);
 
-                   break;
 
-               }
 
-             } else if (method === 2) {
 
-               (0, _util.info)('ICC profile not supported');
 
-             }
 
-             break;
 
-           case 0x6A703263:
 
-             this.parseCodestream(data, position, position + dataLength);
 
-             break;
 
-           case 0x6A502020:
 
-             if ((0, _util.readUint32)(data, position) !== 0x0d0a870a) {
 
-               (0, _util.warn)('Invalid JP2 signature');
 
-             }
 
-             break;
 
-           case 0x6A501A1A:
 
-           case 0x66747970:
 
-           case 0x72726571:
 
-           case 0x72657320:
 
-           case 0x69686472:
 
-             break;
 
-           default:
 
-             var headerType = String.fromCharCode(tbox >> 24 & 0xFF, tbox >> 16 & 0xFF, tbox >> 8 & 0xFF, tbox & 0xFF);
 
-             (0, _util.warn)('Unsupported header type ' + tbox + ' (' + headerType + ')');
 
-             break;
 
-         }
 
-         if (jumpDataLength) {
 
-           position += dataLength;
 
-         }
 
-       }
 
-     },
 
-     parseImageProperties: function JpxImage_parseImageProperties(stream) {
 
-       var newByte = stream.getByte();
 
-       while (newByte >= 0) {
 
-         var oldByte = newByte;
 
-         newByte = stream.getByte();
 
-         var code = oldByte << 8 | newByte;
 
-         if (code === 0xFF51) {
 
-           stream.skip(4);
 
-           var Xsiz = stream.getInt32() >>> 0;
 
-           var Ysiz = stream.getInt32() >>> 0;
 
-           var XOsiz = stream.getInt32() >>> 0;
 
-           var YOsiz = stream.getInt32() >>> 0;
 
-           stream.skip(16);
 
-           var Csiz = stream.getUint16();
 
-           this.width = Xsiz - XOsiz;
 
-           this.height = Ysiz - YOsiz;
 
-           this.componentsCount = Csiz;
 
-           this.bitsPerComponent = 8;
 
-           return;
 
-         }
 
-       }
 
-       throw new JpxError('No size marker found in JPX stream');
 
-     },
 
-     parseCodestream: function JpxImage_parseCodestream(data, start, end) {
 
-       var context = {};
 
-       var doNotRecover = false;
 
-       try {
 
-         var position = start;
 
-         while (position + 1 < end) {
 
-           var code = (0, _util.readUint16)(data, position);
 
-           position += 2;
 
-           var length = 0,
 
-               j,
 
-               sqcd,
 
-               spqcds,
 
-               spqcdSize,
 
-               scalarExpounded,
 
-               tile;
 
-           switch (code) {
 
-             case 0xFF4F:
 
-               context.mainHeader = true;
 
-               break;
 
-             case 0xFFD9:
 
-               break;
 
-             case 0xFF51:
 
-               length = (0, _util.readUint16)(data, position);
 
-               var siz = {};
 
-               siz.Xsiz = (0, _util.readUint32)(data, position + 4);
 
-               siz.Ysiz = (0, _util.readUint32)(data, position + 8);
 
-               siz.XOsiz = (0, _util.readUint32)(data, position + 12);
 
-               siz.YOsiz = (0, _util.readUint32)(data, position + 16);
 
-               siz.XTsiz = (0, _util.readUint32)(data, position + 20);
 
-               siz.YTsiz = (0, _util.readUint32)(data, position + 24);
 
-               siz.XTOsiz = (0, _util.readUint32)(data, position + 28);
 
-               siz.YTOsiz = (0, _util.readUint32)(data, position + 32);
 
-               var componentsCount = (0, _util.readUint16)(data, position + 36);
 
-               siz.Csiz = componentsCount;
 
-               var components = [];
 
-               j = position + 38;
 
-               for (var i = 0; i < componentsCount; i++) {
 
-                 var component = {
 
-                   precision: (data[j] & 0x7F) + 1,
 
-                   isSigned: !!(data[j] & 0x80),
 
-                   XRsiz: data[j + 1],
 
-                   YRsiz: data[j + 1]
 
-                 };
 
-                 calculateComponentDimensions(component, siz);
 
-                 components.push(component);
 
-               }
 
-               context.SIZ = siz;
 
-               context.components = components;
 
-               calculateTileGrids(context, components);
 
-               context.QCC = [];
 
-               context.COC = [];
 
-               break;
 
-             case 0xFF5C:
 
-               length = (0, _util.readUint16)(data, position);
 
-               var qcd = {};
 
-               j = position + 2;
 
-               sqcd = data[j++];
 
-               switch (sqcd & 0x1F) {
 
-                 case 0:
 
-                   spqcdSize = 8;
 
-                   scalarExpounded = true;
 
-                   break;
 
-                 case 1:
 
-                   spqcdSize = 16;
 
-                   scalarExpounded = false;
 
-                   break;
 
-                 case 2:
 
-                   spqcdSize = 16;
 
-                   scalarExpounded = true;
 
-                   break;
 
-                 default:
 
-                   throw new Error('Invalid SQcd value ' + sqcd);
 
-               }
 
-               qcd.noQuantization = spqcdSize === 8;
 
-               qcd.scalarExpounded = scalarExpounded;
 
-               qcd.guardBits = sqcd >> 5;
 
-               spqcds = [];
 
-               while (j < length + position) {
 
-                 var spqcd = {};
 
-                 if (spqcdSize === 8) {
 
-                   spqcd.epsilon = data[j++] >> 3;
 
-                   spqcd.mu = 0;
 
-                 } else {
 
-                   spqcd.epsilon = data[j] >> 3;
 
-                   spqcd.mu = (data[j] & 0x7) << 8 | data[j + 1];
 
-                   j += 2;
 
-                 }
 
-                 spqcds.push(spqcd);
 
-               }
 
-               qcd.SPqcds = spqcds;
 
-               if (context.mainHeader) {
 
-                 context.QCD = qcd;
 
-               } else {
 
-                 context.currentTile.QCD = qcd;
 
-                 context.currentTile.QCC = [];
 
-               }
 
-               break;
 
-             case 0xFF5D:
 
-               length = (0, _util.readUint16)(data, position);
 
-               var qcc = {};
 
-               j = position + 2;
 
-               var cqcc;
 
-               if (context.SIZ.Csiz < 257) {
 
-                 cqcc = data[j++];
 
-               } else {
 
-                 cqcc = (0, _util.readUint16)(data, j);
 
-                 j += 2;
 
-               }
 
-               sqcd = data[j++];
 
-               switch (sqcd & 0x1F) {
 
-                 case 0:
 
-                   spqcdSize = 8;
 
-                   scalarExpounded = true;
 
-                   break;
 
-                 case 1:
 
-                   spqcdSize = 16;
 
-                   scalarExpounded = false;
 
-                   break;
 
-                 case 2:
 
-                   spqcdSize = 16;
 
-                   scalarExpounded = true;
 
-                   break;
 
-                 default:
 
-                   throw new Error('Invalid SQcd value ' + sqcd);
 
-               }
 
-               qcc.noQuantization = spqcdSize === 8;
 
-               qcc.scalarExpounded = scalarExpounded;
 
-               qcc.guardBits = sqcd >> 5;
 
-               spqcds = [];
 
-               while (j < length + position) {
 
-                 spqcd = {};
 
-                 if (spqcdSize === 8) {
 
-                   spqcd.epsilon = data[j++] >> 3;
 
-                   spqcd.mu = 0;
 
-                 } else {
 
-                   spqcd.epsilon = data[j] >> 3;
 
-                   spqcd.mu = (data[j] & 0x7) << 8 | data[j + 1];
 
-                   j += 2;
 
-                 }
 
-                 spqcds.push(spqcd);
 
-               }
 
-               qcc.SPqcds = spqcds;
 
-               if (context.mainHeader) {
 
-                 context.QCC[cqcc] = qcc;
 
-               } else {
 
-                 context.currentTile.QCC[cqcc] = qcc;
 
-               }
 
-               break;
 
-             case 0xFF52:
 
-               length = (0, _util.readUint16)(data, position);
 
-               var cod = {};
 
-               j = position + 2;
 
-               var scod = data[j++];
 
-               cod.entropyCoderWithCustomPrecincts = !!(scod & 1);
 
-               cod.sopMarkerUsed = !!(scod & 2);
 
-               cod.ephMarkerUsed = !!(scod & 4);
 
-               cod.progressionOrder = data[j++];
 
-               cod.layersCount = (0, _util.readUint16)(data, j);
 
-               j += 2;
 
-               cod.multipleComponentTransform = data[j++];
 
-               cod.decompositionLevelsCount = data[j++];
 
-               cod.xcb = (data[j++] & 0xF) + 2;
 
-               cod.ycb = (data[j++] & 0xF) + 2;
 
-               var blockStyle = data[j++];
 
-               cod.selectiveArithmeticCodingBypass = !!(blockStyle & 1);
 
-               cod.resetContextProbabilities = !!(blockStyle & 2);
 
-               cod.terminationOnEachCodingPass = !!(blockStyle & 4);
 
-               cod.verticalyStripe = !!(blockStyle & 8);
 
-               cod.predictableTermination = !!(blockStyle & 16);
 
-               cod.segmentationSymbolUsed = !!(blockStyle & 32);
 
-               cod.reversibleTransformation = data[j++];
 
-               if (cod.entropyCoderWithCustomPrecincts) {
 
-                 var precinctsSizes = [];
 
-                 while (j < length + position) {
 
-                   var precinctsSize = data[j++];
 
-                   precinctsSizes.push({
 
-                     PPx: precinctsSize & 0xF,
 
-                     PPy: precinctsSize >> 4
 
-                   });
 
-                 }
 
-                 cod.precinctsSizes = precinctsSizes;
 
-               }
 
-               var unsupported = [];
 
-               if (cod.selectiveArithmeticCodingBypass) {
 
-                 unsupported.push('selectiveArithmeticCodingBypass');
 
-               }
 
-               if (cod.resetContextProbabilities) {
 
-                 unsupported.push('resetContextProbabilities');
 
-               }
 
-               if (cod.terminationOnEachCodingPass) {
 
-                 unsupported.push('terminationOnEachCodingPass');
 
-               }
 
-               if (cod.verticalyStripe) {
 
-                 unsupported.push('verticalyStripe');
 
-               }
 
-               if (cod.predictableTermination) {
 
-                 unsupported.push('predictableTermination');
 
-               }
 
-               if (unsupported.length > 0) {
 
-                 doNotRecover = true;
 
-                 throw new Error('Unsupported COD options (' + unsupported.join(', ') + ')');
 
-               }
 
-               if (context.mainHeader) {
 
-                 context.COD = cod;
 
-               } else {
 
-                 context.currentTile.COD = cod;
 
-                 context.currentTile.COC = [];
 
-               }
 
-               break;
 
-             case 0xFF90:
 
-               length = (0, _util.readUint16)(data, position);
 
-               tile = {};
 
-               tile.index = (0, _util.readUint16)(data, position + 2);
 
-               tile.length = (0, _util.readUint32)(data, position + 4);
 
-               tile.dataEnd = tile.length + position - 2;
 
-               tile.partIndex = data[position + 8];
 
-               tile.partsCount = data[position + 9];
 
-               context.mainHeader = false;
 
-               if (tile.partIndex === 0) {
 
-                 tile.COD = context.COD;
 
-                 tile.COC = context.COC.slice(0);
 
-                 tile.QCD = context.QCD;
 
-                 tile.QCC = context.QCC.slice(0);
 
-               }
 
-               context.currentTile = tile;
 
-               break;
 
-             case 0xFF93:
 
-               tile = context.currentTile;
 
-               if (tile.partIndex === 0) {
 
-                 initializeTile(context, tile.index);
 
-                 buildPackets(context);
 
-               }
 
-               length = tile.dataEnd - position;
 
-               parseTilePackets(context, data, position, length);
 
-               break;
 
-             case 0xFF55:
 
-             case 0xFF57:
 
-             case 0xFF58:
 
-             case 0xFF64:
 
-               length = (0, _util.readUint16)(data, position);
 
-               break;
 
-             case 0xFF53:
 
-               throw new Error('Codestream code 0xFF53 (COC) is ' + 'not implemented');
 
-             default:
 
-               throw new Error('Unknown codestream code: ' + code.toString(16));
 
-           }
 
-           position += length;
 
-         }
 
-       } catch (e) {
 
-         if (doNotRecover || this.failOnCorruptedImage) {
 
-           throw new JpxError(e.message);
 
-         } else {
 
-           (0, _util.warn)('JPX: Trying to recover from: ' + e.message);
 
-         }
 
-       }
 
-       this.tiles = transformComponents(context);
 
-       this.width = context.SIZ.Xsiz - context.SIZ.XOsiz;
 
-       this.height = context.SIZ.Ysiz - context.SIZ.YOsiz;
 
-       this.componentsCount = context.SIZ.Csiz;
 
-     }
 
-   };
 
-   function calculateComponentDimensions(component, siz) {
 
-     component.x0 = Math.ceil(siz.XOsiz / component.XRsiz);
 
-     component.x1 = Math.ceil(siz.Xsiz / component.XRsiz);
 
-     component.y0 = Math.ceil(siz.YOsiz / component.YRsiz);
 
-     component.y1 = Math.ceil(siz.Ysiz / component.YRsiz);
 
-     component.width = component.x1 - component.x0;
 
-     component.height = component.y1 - component.y0;
 
-   }
 
-   function calculateTileGrids(context, components) {
 
-     var siz = context.SIZ;
 
-     var tile,
 
-         tiles = [];
 
-     var numXtiles = Math.ceil((siz.Xsiz - siz.XTOsiz) / siz.XTsiz);
 
-     var numYtiles = Math.ceil((siz.Ysiz - siz.YTOsiz) / siz.YTsiz);
 
-     for (var q = 0; q < numYtiles; q++) {
 
-       for (var p = 0; p < numXtiles; p++) {
 
-         tile = {};
 
-         tile.tx0 = Math.max(siz.XTOsiz + p * siz.XTsiz, siz.XOsiz);
 
-         tile.ty0 = Math.max(siz.YTOsiz + q * siz.YTsiz, siz.YOsiz);
 
-         tile.tx1 = Math.min(siz.XTOsiz + (p + 1) * siz.XTsiz, siz.Xsiz);
 
-         tile.ty1 = Math.min(siz.YTOsiz + (q + 1) * siz.YTsiz, siz.Ysiz);
 
-         tile.width = tile.tx1 - tile.tx0;
 
-         tile.height = tile.ty1 - tile.ty0;
 
-         tile.components = [];
 
-         tiles.push(tile);
 
-       }
 
-     }
 
-     context.tiles = tiles;
 
-     var componentsCount = siz.Csiz;
 
-     for (var i = 0, ii = componentsCount; i < ii; i++) {
 
-       var component = components[i];
 
-       for (var j = 0, jj = tiles.length; j < jj; j++) {
 
-         var tileComponent = {};
 
-         tile = tiles[j];
 
-         tileComponent.tcx0 = Math.ceil(tile.tx0 / component.XRsiz);
 
-         tileComponent.tcy0 = Math.ceil(tile.ty0 / component.YRsiz);
 
-         tileComponent.tcx1 = Math.ceil(tile.tx1 / component.XRsiz);
 
-         tileComponent.tcy1 = Math.ceil(tile.ty1 / component.YRsiz);
 
-         tileComponent.width = tileComponent.tcx1 - tileComponent.tcx0;
 
-         tileComponent.height = tileComponent.tcy1 - tileComponent.tcy0;
 
-         tile.components[i] = tileComponent;
 
-       }
 
-     }
 
-   }
 
-   function getBlocksDimensions(context, component, r) {
 
-     var codOrCoc = component.codingStyleParameters;
 
-     var result = {};
 
-     if (!codOrCoc.entropyCoderWithCustomPrecincts) {
 
-       result.PPx = 15;
 
-       result.PPy = 15;
 
-     } else {
 
-       result.PPx = codOrCoc.precinctsSizes[r].PPx;
 
-       result.PPy = codOrCoc.precinctsSizes[r].PPy;
 
-     }
 
-     result.xcb_ = r > 0 ? Math.min(codOrCoc.xcb, result.PPx - 1) : Math.min(codOrCoc.xcb, result.PPx);
 
-     result.ycb_ = r > 0 ? Math.min(codOrCoc.ycb, result.PPy - 1) : Math.min(codOrCoc.ycb, result.PPy);
 
-     return result;
 
-   }
 
-   function buildPrecincts(context, resolution, dimensions) {
 
-     var precinctWidth = 1 << dimensions.PPx;
 
-     var precinctHeight = 1 << dimensions.PPy;
 
-     var isZeroRes = resolution.resLevel === 0;
 
-     var precinctWidthInSubband = 1 << dimensions.PPx + (isZeroRes ? 0 : -1);
 
-     var precinctHeightInSubband = 1 << dimensions.PPy + (isZeroRes ? 0 : -1);
 
-     var numprecinctswide = resolution.trx1 > resolution.trx0 ? Math.ceil(resolution.trx1 / precinctWidth) - Math.floor(resolution.trx0 / precinctWidth) : 0;
 
-     var numprecinctshigh = resolution.try1 > resolution.try0 ? Math.ceil(resolution.try1 / precinctHeight) - Math.floor(resolution.try0 / precinctHeight) : 0;
 
-     var numprecincts = numprecinctswide * numprecinctshigh;
 
-     resolution.precinctParameters = {
 
-       precinctWidth: precinctWidth,
 
-       precinctHeight: precinctHeight,
 
-       numprecinctswide: numprecinctswide,
 
-       numprecinctshigh: numprecinctshigh,
 
-       numprecincts: numprecincts,
 
-       precinctWidthInSubband: precinctWidthInSubband,
 
-       precinctHeightInSubband: precinctHeightInSubband
 
-     };
 
-   }
 
-   function buildCodeblocks(context, subband, dimensions) {
 
-     var xcb_ = dimensions.xcb_;
 
-     var ycb_ = dimensions.ycb_;
 
-     var codeblockWidth = 1 << xcb_;
 
-     var codeblockHeight = 1 << ycb_;
 
-     var cbx0 = subband.tbx0 >> xcb_;
 
-     var cby0 = subband.tby0 >> ycb_;
 
-     var cbx1 = subband.tbx1 + codeblockWidth - 1 >> xcb_;
 
-     var cby1 = subband.tby1 + codeblockHeight - 1 >> ycb_;
 
-     var precinctParameters = subband.resolution.precinctParameters;
 
-     var codeblocks = [];
 
-     var precincts = [];
 
-     var i, j, codeblock, precinctNumber;
 
-     for (j = cby0; j < cby1; j++) {
 
-       for (i = cbx0; i < cbx1; i++) {
 
-         codeblock = {
 
-           cbx: i,
 
-           cby: j,
 
-           tbx0: codeblockWidth * i,
 
-           tby0: codeblockHeight * j,
 
-           tbx1: codeblockWidth * (i + 1),
 
-           tby1: codeblockHeight * (j + 1)
 
-         };
 
-         codeblock.tbx0_ = Math.max(subband.tbx0, codeblock.tbx0);
 
-         codeblock.tby0_ = Math.max(subband.tby0, codeblock.tby0);
 
-         codeblock.tbx1_ = Math.min(subband.tbx1, codeblock.tbx1);
 
-         codeblock.tby1_ = Math.min(subband.tby1, codeblock.tby1);
 
-         var pi = Math.floor((codeblock.tbx0_ - subband.tbx0) / precinctParameters.precinctWidthInSubband);
 
-         var pj = Math.floor((codeblock.tby0_ - subband.tby0) / precinctParameters.precinctHeightInSubband);
 
-         precinctNumber = pi + pj * precinctParameters.numprecinctswide;
 
-         codeblock.precinctNumber = precinctNumber;
 
-         codeblock.subbandType = subband.type;
 
-         codeblock.Lblock = 3;
 
-         if (codeblock.tbx1_ <= codeblock.tbx0_ || codeblock.tby1_ <= codeblock.tby0_) {
 
-           continue;
 
-         }
 
-         codeblocks.push(codeblock);
 
-         var precinct = precincts[precinctNumber];
 
-         if (precinct !== undefined) {
 
-           if (i < precinct.cbxMin) {
 
-             precinct.cbxMin = i;
 
-           } else if (i > precinct.cbxMax) {
 
-             precinct.cbxMax = i;
 
-           }
 
-           if (j < precinct.cbyMin) {
 
-             precinct.cbxMin = j;
 
-           } else if (j > precinct.cbyMax) {
 
-             precinct.cbyMax = j;
 
-           }
 
-         } else {
 
-           precincts[precinctNumber] = precinct = {
 
-             cbxMin: i,
 
-             cbyMin: j,
 
-             cbxMax: i,
 
-             cbyMax: j
 
-           };
 
-         }
 
-         codeblock.precinct = precinct;
 
-       }
 
-     }
 
-     subband.codeblockParameters = {
 
-       codeblockWidth: xcb_,
 
-       codeblockHeight: ycb_,
 
-       numcodeblockwide: cbx1 - cbx0 + 1,
 
-       numcodeblockhigh: cby1 - cby0 + 1
 
-     };
 
-     subband.codeblocks = codeblocks;
 
-     subband.precincts = precincts;
 
-   }
 
-   function createPacket(resolution, precinctNumber, layerNumber) {
 
-     var precinctCodeblocks = [];
 
-     var subbands = resolution.subbands;
 
-     for (var i = 0, ii = subbands.length; i < ii; i++) {
 
-       var subband = subbands[i];
 
-       var codeblocks = subband.codeblocks;
 
-       for (var j = 0, jj = codeblocks.length; j < jj; j++) {
 
-         var codeblock = codeblocks[j];
 
-         if (codeblock.precinctNumber !== precinctNumber) {
 
-           continue;
 
-         }
 
-         precinctCodeblocks.push(codeblock);
 
-       }
 
-     }
 
-     return {
 
-       layerNumber: layerNumber,
 
-       codeblocks: precinctCodeblocks
 
-     };
 
-   }
 
-   function LayerResolutionComponentPositionIterator(context) {
 
-     var siz = context.SIZ;
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var layersCount = tile.codingStyleDefaultParameters.layersCount;
 
-     var componentsCount = siz.Csiz;
 
-     var maxDecompositionLevelsCount = 0;
 
-     for (var q = 0; q < componentsCount; q++) {
 
-       maxDecompositionLevelsCount = Math.max(maxDecompositionLevelsCount, tile.components[q].codingStyleParameters.decompositionLevelsCount);
 
-     }
 
-     var l = 0,
 
-         r = 0,
 
-         i = 0,
 
-         k = 0;
 
-     this.nextPacket = function JpxImage_nextPacket() {
 
-       for (; l < layersCount; l++) {
 
-         for (; r <= maxDecompositionLevelsCount; r++) {
 
-           for (; i < componentsCount; i++) {
 
-             var component = tile.components[i];
 
-             if (r > component.codingStyleParameters.decompositionLevelsCount) {
 
-               continue;
 
-             }
 
-             var resolution = component.resolutions[r];
 
-             var numprecincts = resolution.precinctParameters.numprecincts;
 
-             for (; k < numprecincts;) {
 
-               var packet = createPacket(resolution, k, l);
 
-               k++;
 
-               return packet;
 
-             }
 
-             k = 0;
 
-           }
 
-           i = 0;
 
-         }
 
-         r = 0;
 
-       }
 
-       throw new JpxError('Out of packets');
 
-     };
 
-   }
 
-   function ResolutionLayerComponentPositionIterator(context) {
 
-     var siz = context.SIZ;
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var layersCount = tile.codingStyleDefaultParameters.layersCount;
 
-     var componentsCount = siz.Csiz;
 
-     var maxDecompositionLevelsCount = 0;
 
-     for (var q = 0; q < componentsCount; q++) {
 
-       maxDecompositionLevelsCount = Math.max(maxDecompositionLevelsCount, tile.components[q].codingStyleParameters.decompositionLevelsCount);
 
-     }
 
-     var r = 0,
 
-         l = 0,
 
-         i = 0,
 
-         k = 0;
 
-     this.nextPacket = function JpxImage_nextPacket() {
 
-       for (; r <= maxDecompositionLevelsCount; r++) {
 
-         for (; l < layersCount; l++) {
 
-           for (; i < componentsCount; i++) {
 
-             var component = tile.components[i];
 
-             if (r > component.codingStyleParameters.decompositionLevelsCount) {
 
-               continue;
 
-             }
 
-             var resolution = component.resolutions[r];
 
-             var numprecincts = resolution.precinctParameters.numprecincts;
 
-             for (; k < numprecincts;) {
 
-               var packet = createPacket(resolution, k, l);
 
-               k++;
 
-               return packet;
 
-             }
 
-             k = 0;
 
-           }
 
-           i = 0;
 
-         }
 
-         l = 0;
 
-       }
 
-       throw new JpxError('Out of packets');
 
-     };
 
-   }
 
-   function ResolutionPositionComponentLayerIterator(context) {
 
-     var siz = context.SIZ;
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var layersCount = tile.codingStyleDefaultParameters.layersCount;
 
-     var componentsCount = siz.Csiz;
 
-     var l, r, c, p;
 
-     var maxDecompositionLevelsCount = 0;
 
-     for (c = 0; c < componentsCount; c++) {
 
-       var component = tile.components[c];
 
-       maxDecompositionLevelsCount = Math.max(maxDecompositionLevelsCount, component.codingStyleParameters.decompositionLevelsCount);
 
-     }
 
-     var maxNumPrecinctsInLevel = new Int32Array(maxDecompositionLevelsCount + 1);
 
-     for (r = 0; r <= maxDecompositionLevelsCount; ++r) {
 
-       var maxNumPrecincts = 0;
 
-       for (c = 0; c < componentsCount; ++c) {
 
-         var resolutions = tile.components[c].resolutions;
 
-         if (r < resolutions.length) {
 
-           maxNumPrecincts = Math.max(maxNumPrecincts, resolutions[r].precinctParameters.numprecincts);
 
-         }
 
-       }
 
-       maxNumPrecinctsInLevel[r] = maxNumPrecincts;
 
-     }
 
-     l = 0;
 
-     r = 0;
 
-     c = 0;
 
-     p = 0;
 
-     this.nextPacket = function JpxImage_nextPacket() {
 
-       for (; r <= maxDecompositionLevelsCount; r++) {
 
-         for (; p < maxNumPrecinctsInLevel[r]; p++) {
 
-           for (; c < componentsCount; c++) {
 
-             var component = tile.components[c];
 
-             if (r > component.codingStyleParameters.decompositionLevelsCount) {
 
-               continue;
 
-             }
 
-             var resolution = component.resolutions[r];
 
-             var numprecincts = resolution.precinctParameters.numprecincts;
 
-             if (p >= numprecincts) {
 
-               continue;
 
-             }
 
-             for (; l < layersCount;) {
 
-               var packet = createPacket(resolution, p, l);
 
-               l++;
 
-               return packet;
 
-             }
 
-             l = 0;
 
-           }
 
-           c = 0;
 
-         }
 
-         p = 0;
 
-       }
 
-       throw new JpxError('Out of packets');
 
-     };
 
-   }
 
-   function PositionComponentResolutionLayerIterator(context) {
 
-     var siz = context.SIZ;
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var layersCount = tile.codingStyleDefaultParameters.layersCount;
 
-     var componentsCount = siz.Csiz;
 
-     var precinctsSizes = getPrecinctSizesInImageScale(tile);
 
-     var precinctsIterationSizes = precinctsSizes;
 
-     var l = 0,
 
-         r = 0,
 
-         c = 0,
 
-         px = 0,
 
-         py = 0;
 
-     this.nextPacket = function JpxImage_nextPacket() {
 
-       for (; py < precinctsIterationSizes.maxNumHigh; py++) {
 
-         for (; px < precinctsIterationSizes.maxNumWide; px++) {
 
-           for (; c < componentsCount; c++) {
 
-             var component = tile.components[c];
 
-             var decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
 
-             for (; r <= decompositionLevelsCount; r++) {
 
-               var resolution = component.resolutions[r];
 
-               var sizeInImageScale = precinctsSizes.components[c].resolutions[r];
 
-               var k = getPrecinctIndexIfExist(px, py, sizeInImageScale, precinctsIterationSizes, resolution);
 
-               if (k === null) {
 
-                 continue;
 
-               }
 
-               for (; l < layersCount;) {
 
-                 var packet = createPacket(resolution, k, l);
 
-                 l++;
 
-                 return packet;
 
-               }
 
-               l = 0;
 
-             }
 
-             r = 0;
 
-           }
 
-           c = 0;
 
-         }
 
-         px = 0;
 
-       }
 
-       throw new JpxError('Out of packets');
 
-     };
 
-   }
 
-   function ComponentPositionResolutionLayerIterator(context) {
 
-     var siz = context.SIZ;
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var layersCount = tile.codingStyleDefaultParameters.layersCount;
 
-     var componentsCount = siz.Csiz;
 
-     var precinctsSizes = getPrecinctSizesInImageScale(tile);
 
-     var l = 0,
 
-         r = 0,
 
-         c = 0,
 
-         px = 0,
 
-         py = 0;
 
-     this.nextPacket = function JpxImage_nextPacket() {
 
-       for (; c < componentsCount; ++c) {
 
-         var component = tile.components[c];
 
-         var precinctsIterationSizes = precinctsSizes.components[c];
 
-         var decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
 
-         for (; py < precinctsIterationSizes.maxNumHigh; py++) {
 
-           for (; px < precinctsIterationSizes.maxNumWide; px++) {
 
-             for (; r <= decompositionLevelsCount; r++) {
 
-               var resolution = component.resolutions[r];
 
-               var sizeInImageScale = precinctsIterationSizes.resolutions[r];
 
-               var k = getPrecinctIndexIfExist(px, py, sizeInImageScale, precinctsIterationSizes, resolution);
 
-               if (k === null) {
 
-                 continue;
 
-               }
 
-               for (; l < layersCount;) {
 
-                 var packet = createPacket(resolution, k, l);
 
-                 l++;
 
-                 return packet;
 
-               }
 
-               l = 0;
 
-             }
 
-             r = 0;
 
-           }
 
-           px = 0;
 
-         }
 
-         py = 0;
 
-       }
 
-       throw new JpxError('Out of packets');
 
-     };
 
-   }
 
-   function getPrecinctIndexIfExist(pxIndex, pyIndex, sizeInImageScale, precinctIterationSizes, resolution) {
 
-     var posX = pxIndex * precinctIterationSizes.minWidth;
 
-     var posY = pyIndex * precinctIterationSizes.minHeight;
 
-     if (posX % sizeInImageScale.width !== 0 || posY % sizeInImageScale.height !== 0) {
 
-       return null;
 
-     }
 
-     var startPrecinctRowIndex = posY / sizeInImageScale.width * resolution.precinctParameters.numprecinctswide;
 
-     return posX / sizeInImageScale.height + startPrecinctRowIndex;
 
-   }
 
-   function getPrecinctSizesInImageScale(tile) {
 
-     var componentsCount = tile.components.length;
 
-     var minWidth = Number.MAX_VALUE;
 
-     var minHeight = Number.MAX_VALUE;
 
-     var maxNumWide = 0;
 
-     var maxNumHigh = 0;
 
-     var sizePerComponent = new Array(componentsCount);
 
-     for (var c = 0; c < componentsCount; c++) {
 
-       var component = tile.components[c];
 
-       var decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
 
-       var sizePerResolution = new Array(decompositionLevelsCount + 1);
 
-       var minWidthCurrentComponent = Number.MAX_VALUE;
 
-       var minHeightCurrentComponent = Number.MAX_VALUE;
 
-       var maxNumWideCurrentComponent = 0;
 
-       var maxNumHighCurrentComponent = 0;
 
-       var scale = 1;
 
-       for (var r = decompositionLevelsCount; r >= 0; --r) {
 
-         var resolution = component.resolutions[r];
 
-         var widthCurrentResolution = scale * resolution.precinctParameters.precinctWidth;
 
-         var heightCurrentResolution = scale * resolution.precinctParameters.precinctHeight;
 
-         minWidthCurrentComponent = Math.min(minWidthCurrentComponent, widthCurrentResolution);
 
-         minHeightCurrentComponent = Math.min(minHeightCurrentComponent, heightCurrentResolution);
 
-         maxNumWideCurrentComponent = Math.max(maxNumWideCurrentComponent, resolution.precinctParameters.numprecinctswide);
 
-         maxNumHighCurrentComponent = Math.max(maxNumHighCurrentComponent, resolution.precinctParameters.numprecinctshigh);
 
-         sizePerResolution[r] = {
 
-           width: widthCurrentResolution,
 
-           height: heightCurrentResolution
 
-         };
 
-         scale <<= 1;
 
-       }
 
-       minWidth = Math.min(minWidth, minWidthCurrentComponent);
 
-       minHeight = Math.min(minHeight, minHeightCurrentComponent);
 
-       maxNumWide = Math.max(maxNumWide, maxNumWideCurrentComponent);
 
-       maxNumHigh = Math.max(maxNumHigh, maxNumHighCurrentComponent);
 
-       sizePerComponent[c] = {
 
-         resolutions: sizePerResolution,
 
-         minWidth: minWidthCurrentComponent,
 
-         minHeight: minHeightCurrentComponent,
 
-         maxNumWide: maxNumWideCurrentComponent,
 
-         maxNumHigh: maxNumHighCurrentComponent
 
-       };
 
-     }
 
-     return {
 
-       components: sizePerComponent,
 
-       minWidth: minWidth,
 
-       minHeight: minHeight,
 
-       maxNumWide: maxNumWide,
 
-       maxNumHigh: maxNumHigh
 
-     };
 
-   }
 
-   function buildPackets(context) {
 
-     var siz = context.SIZ;
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var componentsCount = siz.Csiz;
 
-     for (var c = 0; c < componentsCount; c++) {
 
-       var component = tile.components[c];
 
-       var decompositionLevelsCount = component.codingStyleParameters.decompositionLevelsCount;
 
-       var resolutions = [];
 
-       var subbands = [];
 
-       for (var r = 0; r <= decompositionLevelsCount; r++) {
 
-         var blocksDimensions = getBlocksDimensions(context, component, r);
 
-         var resolution = {};
 
-         var scale = 1 << decompositionLevelsCount - r;
 
-         resolution.trx0 = Math.ceil(component.tcx0 / scale);
 
-         resolution.try0 = Math.ceil(component.tcy0 / scale);
 
-         resolution.trx1 = Math.ceil(component.tcx1 / scale);
 
-         resolution.try1 = Math.ceil(component.tcy1 / scale);
 
-         resolution.resLevel = r;
 
-         buildPrecincts(context, resolution, blocksDimensions);
 
-         resolutions.push(resolution);
 
-         var subband;
 
-         if (r === 0) {
 
-           subband = {};
 
-           subband.type = 'LL';
 
-           subband.tbx0 = Math.ceil(component.tcx0 / scale);
 
-           subband.tby0 = Math.ceil(component.tcy0 / scale);
 
-           subband.tbx1 = Math.ceil(component.tcx1 / scale);
 
-           subband.tby1 = Math.ceil(component.tcy1 / scale);
 
-           subband.resolution = resolution;
 
-           buildCodeblocks(context, subband, blocksDimensions);
 
-           subbands.push(subband);
 
-           resolution.subbands = [subband];
 
-         } else {
 
-           var bscale = 1 << decompositionLevelsCount - r + 1;
 
-           var resolutionSubbands = [];
 
-           subband = {};
 
-           subband.type = 'HL';
 
-           subband.tbx0 = Math.ceil(component.tcx0 / bscale - 0.5);
 
-           subband.tby0 = Math.ceil(component.tcy0 / bscale);
 
-           subband.tbx1 = Math.ceil(component.tcx1 / bscale - 0.5);
 
-           subband.tby1 = Math.ceil(component.tcy1 / bscale);
 
-           subband.resolution = resolution;
 
-           buildCodeblocks(context, subband, blocksDimensions);
 
-           subbands.push(subband);
 
-           resolutionSubbands.push(subband);
 
-           subband = {};
 
-           subband.type = 'LH';
 
-           subband.tbx0 = Math.ceil(component.tcx0 / bscale);
 
-           subband.tby0 = Math.ceil(component.tcy0 / bscale - 0.5);
 
-           subband.tbx1 = Math.ceil(component.tcx1 / bscale);
 
-           subband.tby1 = Math.ceil(component.tcy1 / bscale - 0.5);
 
-           subband.resolution = resolution;
 
-           buildCodeblocks(context, subband, blocksDimensions);
 
-           subbands.push(subband);
 
-           resolutionSubbands.push(subband);
 
-           subband = {};
 
-           subband.type = 'HH';
 
-           subband.tbx0 = Math.ceil(component.tcx0 / bscale - 0.5);
 
-           subband.tby0 = Math.ceil(component.tcy0 / bscale - 0.5);
 
-           subband.tbx1 = Math.ceil(component.tcx1 / bscale - 0.5);
 
-           subband.tby1 = Math.ceil(component.tcy1 / bscale - 0.5);
 
-           subband.resolution = resolution;
 
-           buildCodeblocks(context, subband, blocksDimensions);
 
-           subbands.push(subband);
 
-           resolutionSubbands.push(subband);
 
-           resolution.subbands = resolutionSubbands;
 
-         }
 
-       }
 
-       component.resolutions = resolutions;
 
-       component.subbands = subbands;
 
-     }
 
-     var progressionOrder = tile.codingStyleDefaultParameters.progressionOrder;
 
-     switch (progressionOrder) {
 
-       case 0:
 
-         tile.packetsIterator = new LayerResolutionComponentPositionIterator(context);
 
-         break;
 
-       case 1:
 
-         tile.packetsIterator = new ResolutionLayerComponentPositionIterator(context);
 
-         break;
 
-       case 2:
 
-         tile.packetsIterator = new ResolutionPositionComponentLayerIterator(context);
 
-         break;
 
-       case 3:
 
-         tile.packetsIterator = new PositionComponentResolutionLayerIterator(context);
 
-         break;
 
-       case 4:
 
-         tile.packetsIterator = new ComponentPositionResolutionLayerIterator(context);
 
-         break;
 
-       default:
 
-         throw new JpxError('Unsupported progression order ' + progressionOrder);
 
-     }
 
-   }
 
-   function parseTilePackets(context, data, offset, dataLength) {
 
-     var position = 0;
 
-     var buffer,
 
-         bufferSize = 0,
 
-         skipNextBit = false;
 
-     function readBits(count) {
 
-       while (bufferSize < count) {
 
-         var b = data[offset + position];
 
-         position++;
 
-         if (skipNextBit) {
 
-           buffer = buffer << 7 | b;
 
-           bufferSize += 7;
 
-           skipNextBit = false;
 
-         } else {
 
-           buffer = buffer << 8 | b;
 
-           bufferSize += 8;
 
-         }
 
-         if (b === 0xFF) {
 
-           skipNextBit = true;
 
-         }
 
-       }
 
-       bufferSize -= count;
 
-       return buffer >>> bufferSize & (1 << count) - 1;
 
-     }
 
-     function skipMarkerIfEqual(value) {
 
-       if (data[offset + position - 1] === 0xFF && data[offset + position] === value) {
 
-         skipBytes(1);
 
-         return true;
 
-       } else if (data[offset + position] === 0xFF && data[offset + position + 1] === value) {
 
-         skipBytes(2);
 
-         return true;
 
-       }
 
-       return false;
 
-     }
 
-     function skipBytes(count) {
 
-       position += count;
 
-     }
 
-     function alignToByte() {
 
-       bufferSize = 0;
 
-       if (skipNextBit) {
 
-         position++;
 
-         skipNextBit = false;
 
-       }
 
-     }
 
-     function readCodingpasses() {
 
-       if (readBits(1) === 0) {
 
-         return 1;
 
-       }
 
-       if (readBits(1) === 0) {
 
-         return 2;
 
-       }
 
-       var value = readBits(2);
 
-       if (value < 3) {
 
-         return value + 3;
 
-       }
 
-       value = readBits(5);
 
-       if (value < 31) {
 
-         return value + 6;
 
-       }
 
-       value = readBits(7);
 
-       return value + 37;
 
-     }
 
-     var tileIndex = context.currentTile.index;
 
-     var tile = context.tiles[tileIndex];
 
-     var sopMarkerUsed = context.COD.sopMarkerUsed;
 
-     var ephMarkerUsed = context.COD.ephMarkerUsed;
 
-     var packetsIterator = tile.packetsIterator;
 
-     while (position < dataLength) {
 
-       alignToByte();
 
-       if (sopMarkerUsed && skipMarkerIfEqual(0x91)) {
 
-         skipBytes(4);
 
-       }
 
-       var packet = packetsIterator.nextPacket();
 
-       if (!readBits(1)) {
 
-         continue;
 
-       }
 
-       var layerNumber = packet.layerNumber;
 
-       var queue = [],
 
-           codeblock;
 
-       for (var i = 0, ii = packet.codeblocks.length; i < ii; i++) {
 
-         codeblock = packet.codeblocks[i];
 
-         var precinct = codeblock.precinct;
 
-         var codeblockColumn = codeblock.cbx - precinct.cbxMin;
 
-         var codeblockRow = codeblock.cby - precinct.cbyMin;
 
-         var codeblockIncluded = false;
 
-         var firstTimeInclusion = false;
 
-         var valueReady;
 
-         if (codeblock['included'] !== undefined) {
 
-           codeblockIncluded = !!readBits(1);
 
-         } else {
 
-           precinct = codeblock.precinct;
 
-           var inclusionTree, zeroBitPlanesTree;
 
-           if (precinct['inclusionTree'] !== undefined) {
 
-             inclusionTree = precinct.inclusionTree;
 
-           } else {
 
-             var width = precinct.cbxMax - precinct.cbxMin + 1;
 
-             var height = precinct.cbyMax - precinct.cbyMin + 1;
 
-             inclusionTree = new InclusionTree(width, height, layerNumber);
 
-             zeroBitPlanesTree = new TagTree(width, height);
 
-             precinct.inclusionTree = inclusionTree;
 
-             precinct.zeroBitPlanesTree = zeroBitPlanesTree;
 
-           }
 
-           if (inclusionTree.reset(codeblockColumn, codeblockRow, layerNumber)) {
 
-             while (true) {
 
-               if (readBits(1)) {
 
-                 valueReady = !inclusionTree.nextLevel();
 
-                 if (valueReady) {
 
-                   codeblock.included = true;
 
-                   codeblockIncluded = firstTimeInclusion = true;
 
-                   break;
 
-                 }
 
-               } else {
 
-                 inclusionTree.incrementValue(layerNumber);
 
-                 break;
 
-               }
 
-             }
 
-           }
 
-         }
 
-         if (!codeblockIncluded) {
 
-           continue;
 
-         }
 
-         if (firstTimeInclusion) {
 
-           zeroBitPlanesTree = precinct.zeroBitPlanesTree;
 
-           zeroBitPlanesTree.reset(codeblockColumn, codeblockRow);
 
-           while (true) {
 
-             if (readBits(1)) {
 
-               valueReady = !zeroBitPlanesTree.nextLevel();
 
-               if (valueReady) {
 
-                 break;
 
-               }
 
-             } else {
 
-               zeroBitPlanesTree.incrementValue();
 
-             }
 
-           }
 
-           codeblock.zeroBitPlanes = zeroBitPlanesTree.value;
 
-         }
 
-         var codingpasses = readCodingpasses();
 
-         while (readBits(1)) {
 
-           codeblock.Lblock++;
 
-         }
 
-         var codingpassesLog2 = (0, _util.log2)(codingpasses);
 
-         var bits = (codingpasses < 1 << codingpassesLog2 ? codingpassesLog2 - 1 : codingpassesLog2) + codeblock.Lblock;
 
-         var codedDataLength = readBits(bits);
 
-         queue.push({
 
-           codeblock: codeblock,
 
-           codingpasses: codingpasses,
 
-           dataLength: codedDataLength
 
-         });
 
-       }
 
-       alignToByte();
 
-       if (ephMarkerUsed) {
 
-         skipMarkerIfEqual(0x92);
 
-       }
 
-       while (queue.length > 0) {
 
-         var packetItem = queue.shift();
 
-         codeblock = packetItem.codeblock;
 
-         if (codeblock['data'] === undefined) {
 
-           codeblock.data = [];
 
-         }
 
-         codeblock.data.push({
 
-           data: data,
 
-           start: offset + position,
 
-           end: offset + position + packetItem.dataLength,
 
-           codingpasses: packetItem.codingpasses
 
-         });
 
-         position += packetItem.dataLength;
 
-       }
 
-     }
 
-     return position;
 
-   }
 
-   function copyCoefficients(coefficients, levelWidth, levelHeight, subband, delta, mb, reversible, segmentationSymbolUsed) {
 
-     var x0 = subband.tbx0;
 
-     var y0 = subband.tby0;
 
-     var width = subband.tbx1 - subband.tbx0;
 
-     var codeblocks = subband.codeblocks;
 
-     var right = subband.type.charAt(0) === 'H' ? 1 : 0;
 
-     var bottom = subband.type.charAt(1) === 'H' ? levelWidth : 0;
 
-     for (var i = 0, ii = codeblocks.length; i < ii; ++i) {
 
-       var codeblock = codeblocks[i];
 
-       var blockWidth = codeblock.tbx1_ - codeblock.tbx0_;
 
-       var blockHeight = codeblock.tby1_ - codeblock.tby0_;
 
-       if (blockWidth === 0 || blockHeight === 0) {
 
-         continue;
 
-       }
 
-       if (codeblock['data'] === undefined) {
 
-         continue;
 
-       }
 
-       var bitModel, currentCodingpassType;
 
-       bitModel = new BitModel(blockWidth, blockHeight, codeblock.subbandType, codeblock.zeroBitPlanes, mb);
 
-       currentCodingpassType = 2;
 
-       var data = codeblock.data,
 
-           totalLength = 0,
 
-           codingpasses = 0;
 
-       var j, jj, dataItem;
 
-       for (j = 0, jj = data.length; j < jj; j++) {
 
-         dataItem = data[j];
 
-         totalLength += dataItem.end - dataItem.start;
 
-         codingpasses += dataItem.codingpasses;
 
-       }
 
-       var encodedData = new Uint8Array(totalLength);
 
-       var position = 0;
 
-       for (j = 0, jj = data.length; j < jj; j++) {
 
-         dataItem = data[j];
 
-         var chunk = dataItem.data.subarray(dataItem.start, dataItem.end);
 
-         encodedData.set(chunk, position);
 
-         position += chunk.length;
 
-       }
 
-       var decoder = new _arithmetic_decoder.ArithmeticDecoder(encodedData, 0, totalLength);
 
-       bitModel.setDecoder(decoder);
 
-       for (j = 0; j < codingpasses; j++) {
 
-         switch (currentCodingpassType) {
 
-           case 0:
 
-             bitModel.runSignificancePropagationPass();
 
-             break;
 
-           case 1:
 
-             bitModel.runMagnitudeRefinementPass();
 
-             break;
 
-           case 2:
 
-             bitModel.runCleanupPass();
 
-             if (segmentationSymbolUsed) {
 
-               bitModel.checkSegmentationSymbol();
 
-             }
 
-             break;
 
-         }
 
-         currentCodingpassType = (currentCodingpassType + 1) % 3;
 
-       }
 
-       var offset = codeblock.tbx0_ - x0 + (codeblock.tby0_ - y0) * width;
 
-       var sign = bitModel.coefficentsSign;
 
-       var magnitude = bitModel.coefficentsMagnitude;
 
-       var bitsDecoded = bitModel.bitsDecoded;
 
-       var magnitudeCorrection = reversible ? 0 : 0.5;
 
-       var k, n, nb;
 
-       position = 0;
 
-       var interleave = subband.type !== 'LL';
 
-       for (j = 0; j < blockHeight; j++) {
 
-         var row = offset / width | 0;
 
-         var levelOffset = 2 * row * (levelWidth - width) + right + bottom;
 
-         for (k = 0; k < blockWidth; k++) {
 
-           n = magnitude[position];
 
-           if (n !== 0) {
 
-             n = (n + magnitudeCorrection) * delta;
 
-             if (sign[position] !== 0) {
 
-               n = -n;
 
-             }
 
-             nb = bitsDecoded[position];
 
-             var pos = interleave ? levelOffset + (offset << 1) : offset;
 
-             if (reversible && nb >= mb) {
 
-               coefficients[pos] = n;
 
-             } else {
 
-               coefficients[pos] = n * (1 << mb - nb);
 
-             }
 
-           }
 
-           offset++;
 
-           position++;
 
-         }
 
-         offset += width - blockWidth;
 
-       }
 
-     }
 
-   }
 
-   function transformTile(context, tile, c) {
 
-     var component = tile.components[c];
 
-     var codingStyleParameters = component.codingStyleParameters;
 
-     var quantizationParameters = component.quantizationParameters;
 
-     var decompositionLevelsCount = codingStyleParameters.decompositionLevelsCount;
 
-     var spqcds = quantizationParameters.SPqcds;
 
-     var scalarExpounded = quantizationParameters.scalarExpounded;
 
-     var guardBits = quantizationParameters.guardBits;
 
-     var segmentationSymbolUsed = codingStyleParameters.segmentationSymbolUsed;
 
-     var precision = context.components[c].precision;
 
-     var reversible = codingStyleParameters.reversibleTransformation;
 
-     var transform = reversible ? new ReversibleTransform() : new IrreversibleTransform();
 
-     var subbandCoefficients = [];
 
-     var b = 0;
 
-     for (var i = 0; i <= decompositionLevelsCount; i++) {
 
-       var resolution = component.resolutions[i];
 
-       var width = resolution.trx1 - resolution.trx0;
 
-       var height = resolution.try1 - resolution.try0;
 
-       var coefficients = new Float32Array(width * height);
 
-       for (var j = 0, jj = resolution.subbands.length; j < jj; j++) {
 
-         var mu, epsilon;
 
-         if (!scalarExpounded) {
 
-           mu = spqcds[0].mu;
 
-           epsilon = spqcds[0].epsilon + (i > 0 ? 1 - i : 0);
 
-         } else {
 
-           mu = spqcds[b].mu;
 
-           epsilon = spqcds[b].epsilon;
 
-           b++;
 
-         }
 
-         var subband = resolution.subbands[j];
 
-         var gainLog2 = SubbandsGainLog2[subband.type];
 
-         var delta = reversible ? 1 : Math.pow(2, precision + gainLog2 - epsilon) * (1 + mu / 2048);
 
-         var mb = guardBits + epsilon - 1;
 
-         copyCoefficients(coefficients, width, height, subband, delta, mb, reversible, segmentationSymbolUsed);
 
-       }
 
-       subbandCoefficients.push({
 
-         width: width,
 
-         height: height,
 
-         items: coefficients
 
-       });
 
-     }
 
-     var result = transform.calculate(subbandCoefficients, component.tcx0, component.tcy0);
 
-     return {
 
-       left: component.tcx0,
 
-       top: component.tcy0,
 
-       width: result.width,
 
-       height: result.height,
 
-       items: result.items
 
-     };
 
-   }
 
-   function transformComponents(context) {
 
-     var siz = context.SIZ;
 
-     var components = context.components;
 
-     var componentsCount = siz.Csiz;
 
-     var resultImages = [];
 
-     for (var i = 0, ii = context.tiles.length; i < ii; i++) {
 
-       var tile = context.tiles[i];
 
-       var transformedTiles = [];
 
-       var c;
 
-       for (c = 0; c < componentsCount; c++) {
 
-         transformedTiles[c] = transformTile(context, tile, c);
 
-       }
 
-       var tile0 = transformedTiles[0];
 
-       var out = new Uint8Array(tile0.items.length * componentsCount);
 
-       var result = {
 
-         left: tile0.left,
 
-         top: tile0.top,
 
-         width: tile0.width,
 
-         height: tile0.height,
 
-         items: out
 
-       };
 
-       var shift, offset, max, min, maxK;
 
-       var pos = 0,
 
-           j,
 
-           jj,
 
-           y0,
 
-           y1,
 
-           y2,
 
-           r,
 
-           g,
 
-           b,
 
-           k,
 
-           val;
 
-       if (tile.codingStyleDefaultParameters.multipleComponentTransform) {
 
-         var fourComponents = componentsCount === 4;
 
-         var y0items = transformedTiles[0].items;
 
-         var y1items = transformedTiles[1].items;
 
-         var y2items = transformedTiles[2].items;
 
-         var y3items = fourComponents ? transformedTiles[3].items : null;
 
-         shift = components[0].precision - 8;
 
-         offset = (128 << shift) + 0.5;
 
-         max = 255 * (1 << shift);
 
-         maxK = max * 0.5;
 
-         min = -maxK;
 
-         var component0 = tile.components[0];
 
-         var alpha01 = componentsCount - 3;
 
-         jj = y0items.length;
 
-         if (!component0.codingStyleParameters.reversibleTransformation) {
 
-           for (j = 0; j < jj; j++, pos += alpha01) {
 
-             y0 = y0items[j] + offset;
 
-             y1 = y1items[j];
 
-             y2 = y2items[j];
 
-             r = y0 + 1.402 * y2;
 
-             g = y0 - 0.34413 * y1 - 0.71414 * y2;
 
-             b = y0 + 1.772 * y1;
 
-             out[pos++] = r <= 0 ? 0 : r >= max ? 255 : r >> shift;
 
-             out[pos++] = g <= 0 ? 0 : g >= max ? 255 : g >> shift;
 
-             out[pos++] = b <= 0 ? 0 : b >= max ? 255 : b >> shift;
 
-           }
 
-         } else {
 
-           for (j = 0; j < jj; j++, pos += alpha01) {
 
-             y0 = y0items[j] + offset;
 
-             y1 = y1items[j];
 
-             y2 = y2items[j];
 
-             g = y0 - (y2 + y1 >> 2);
 
-             r = g + y2;
 
-             b = g + y1;
 
-             out[pos++] = r <= 0 ? 0 : r >= max ? 255 : r >> shift;
 
-             out[pos++] = g <= 0 ? 0 : g >= max ? 255 : g >> shift;
 
-             out[pos++] = b <= 0 ? 0 : b >= max ? 255 : b >> shift;
 
-           }
 
-         }
 
-         if (fourComponents) {
 
-           for (j = 0, pos = 3; j < jj; j++, pos += 4) {
 
-             k = y3items[j];
 
-             out[pos] = k <= min ? 0 : k >= maxK ? 255 : k + offset >> shift;
 
-           }
 
-         }
 
-       } else {
 
-         for (c = 0; c < componentsCount; c++) {
 
-           var items = transformedTiles[c].items;
 
-           shift = components[c].precision - 8;
 
-           offset = (128 << shift) + 0.5;
 
-           max = 127.5 * (1 << shift);
 
-           min = -max;
 
-           for (pos = c, j = 0, jj = items.length; j < jj; j++) {
 
-             val = items[j];
 
-             out[pos] = val <= min ? 0 : val >= max ? 255 : val + offset >> shift;
 
-             pos += componentsCount;
 
-           }
 
-         }
 
-       }
 
-       resultImages.push(result);
 
-     }
 
-     return resultImages;
 
-   }
 
-   function initializeTile(context, tileIndex) {
 
-     var siz = context.SIZ;
 
-     var componentsCount = siz.Csiz;
 
-     var tile = context.tiles[tileIndex];
 
-     for (var c = 0; c < componentsCount; c++) {
 
-       var component = tile.components[c];
 
-       var qcdOrQcc = context.currentTile.QCC[c] !== undefined ? context.currentTile.QCC[c] : context.currentTile.QCD;
 
-       component.quantizationParameters = qcdOrQcc;
 
-       var codOrCoc = context.currentTile.COC[c] !== undefined ? context.currentTile.COC[c] : context.currentTile.COD;
 
-       component.codingStyleParameters = codOrCoc;
 
-     }
 
-     tile.codingStyleDefaultParameters = context.currentTile.COD;
 
-   }
 
-   var TagTree = function TagTreeClosure() {
 
-     function TagTree(width, height) {
 
-       var levelsLength = (0, _util.log2)(Math.max(width, height)) + 1;
 
-       this.levels = [];
 
-       for (var i = 0; i < levelsLength; i++) {
 
-         var level = {
 
-           width: width,
 
-           height: height,
 
-           items: []
 
-         };
 
-         this.levels.push(level);
 
-         width = Math.ceil(width / 2);
 
-         height = Math.ceil(height / 2);
 
-       }
 
-     }
 
-     TagTree.prototype = {
 
-       reset: function TagTree_reset(i, j) {
 
-         var currentLevel = 0,
 
-             value = 0,
 
-             level;
 
-         while (currentLevel < this.levels.length) {
 
-           level = this.levels[currentLevel];
 
-           var index = i + j * level.width;
 
-           if (level.items[index] !== undefined) {
 
-             value = level.items[index];
 
-             break;
 
-           }
 
-           level.index = index;
 
-           i >>= 1;
 
-           j >>= 1;
 
-           currentLevel++;
 
-         }
 
-         currentLevel--;
 
-         level = this.levels[currentLevel];
 
-         level.items[level.index] = value;
 
-         this.currentLevel = currentLevel;
 
-         delete this.value;
 
-       },
 
-       incrementValue: function TagTree_incrementValue() {
 
-         var level = this.levels[this.currentLevel];
 
-         level.items[level.index]++;
 
-       },
 
-       nextLevel: function TagTree_nextLevel() {
 
-         var currentLevel = this.currentLevel;
 
-         var level = this.levels[currentLevel];
 
-         var value = level.items[level.index];
 
-         currentLevel--;
 
-         if (currentLevel < 0) {
 
-           this.value = value;
 
-           return false;
 
-         }
 
-         this.currentLevel = currentLevel;
 
-         level = this.levels[currentLevel];
 
-         level.items[level.index] = value;
 
-         return true;
 
-       }
 
-     };
 
-     return TagTree;
 
-   }();
 
-   var InclusionTree = function InclusionTreeClosure() {
 
-     function InclusionTree(width, height, defaultValue) {
 
-       var levelsLength = (0, _util.log2)(Math.max(width, height)) + 1;
 
-       this.levels = [];
 
-       for (var i = 0; i < levelsLength; i++) {
 
-         var items = new Uint8Array(width * height);
 
-         for (var j = 0, jj = items.length; j < jj; j++) {
 
-           items[j] = defaultValue;
 
-         }
 
-         var level = {
 
-           width: width,
 
-           height: height,
 
-           items: items
 
-         };
 
-         this.levels.push(level);
 
-         width = Math.ceil(width / 2);
 
-         height = Math.ceil(height / 2);
 
-       }
 
-     }
 
-     InclusionTree.prototype = {
 
-       reset: function InclusionTree_reset(i, j, stopValue) {
 
-         var currentLevel = 0;
 
-         while (currentLevel < this.levels.length) {
 
-           var level = this.levels[currentLevel];
 
-           var index = i + j * level.width;
 
-           level.index = index;
 
-           var value = level.items[index];
 
-           if (value === 0xFF) {
 
-             break;
 
-           }
 
-           if (value > stopValue) {
 
-             this.currentLevel = currentLevel;
 
-             this.propagateValues();
 
-             return false;
 
-           }
 
-           i >>= 1;
 
-           j >>= 1;
 
-           currentLevel++;
 
-         }
 
-         this.currentLevel = currentLevel - 1;
 
-         return true;
 
-       },
 
-       incrementValue: function InclusionTree_incrementValue(stopValue) {
 
-         var level = this.levels[this.currentLevel];
 
-         level.items[level.index] = stopValue + 1;
 
-         this.propagateValues();
 
-       },
 
-       propagateValues: function InclusionTree_propagateValues() {
 
-         var levelIndex = this.currentLevel;
 
-         var level = this.levels[levelIndex];
 
-         var currentValue = level.items[level.index];
 
-         while (--levelIndex >= 0) {
 
-           level = this.levels[levelIndex];
 
-           level.items[level.index] = currentValue;
 
-         }
 
-       },
 
-       nextLevel: function InclusionTree_nextLevel() {
 
-         var currentLevel = this.currentLevel;
 
-         var level = this.levels[currentLevel];
 
-         var value = level.items[level.index];
 
-         level.items[level.index] = 0xFF;
 
-         currentLevel--;
 
-         if (currentLevel < 0) {
 
-           return false;
 
-         }
 
-         this.currentLevel = currentLevel;
 
-         level = this.levels[currentLevel];
 
-         level.items[level.index] = value;
 
-         return true;
 
-       }
 
-     };
 
-     return InclusionTree;
 
-   }();
 
-   var BitModel = function BitModelClosure() {
 
-     var UNIFORM_CONTEXT = 17;
 
-     var RUNLENGTH_CONTEXT = 18;
 
-     var LLAndLHContextsLabel = new Uint8Array([0, 5, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 1, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 2, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 2, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8, 0, 0, 0, 0, 0, 2, 6, 8, 0, 3, 7, 8, 0, 4, 7, 8]);
 
-     var HLContextLabel = new Uint8Array([0, 3, 4, 0, 5, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 1, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 2, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 2, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8, 0, 0, 0, 0, 0, 2, 3, 4, 0, 6, 7, 7, 0, 8, 8, 8]);
 
-     var HHContextLabel = new Uint8Array([0, 1, 2, 0, 1, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0, 3, 4, 5, 0, 4, 5, 5, 0, 5, 5, 5, 0, 0, 0, 0, 0, 6, 7, 7, 0, 7, 7, 7, 0, 7, 7, 7, 0, 0, 0, 0, 0, 8, 8, 8, 0, 8, 8, 8, 0, 8, 8, 8, 0, 0, 0, 0, 0, 8, 8, 8, 0, 8, 8, 8, 0, 8, 8, 8]);
 
-     function BitModel(width, height, subband, zeroBitPlanes, mb) {
 
-       this.width = width;
 
-       this.height = height;
 
-       this.contextLabelTable = subband === 'HH' ? HHContextLabel : subband === 'HL' ? HLContextLabel : LLAndLHContextsLabel;
 
-       var coefficientCount = width * height;
 
-       this.neighborsSignificance = new Uint8Array(coefficientCount);
 
-       this.coefficentsSign = new Uint8Array(coefficientCount);
 
-       this.coefficentsMagnitude = mb > 14 ? new Uint32Array(coefficientCount) : mb > 6 ? new Uint16Array(coefficientCount) : new Uint8Array(coefficientCount);
 
-       this.processingFlags = new Uint8Array(coefficientCount);
 
-       var bitsDecoded = new Uint8Array(coefficientCount);
 
-       if (zeroBitPlanes !== 0) {
 
-         for (var i = 0; i < coefficientCount; i++) {
 
-           bitsDecoded[i] = zeroBitPlanes;
 
-         }
 
-       }
 
-       this.bitsDecoded = bitsDecoded;
 
-       this.reset();
 
-     }
 
-     BitModel.prototype = {
 
-       setDecoder: function BitModel_setDecoder(decoder) {
 
-         this.decoder = decoder;
 
-       },
 
-       reset: function BitModel_reset() {
 
-         this.contexts = new Int8Array(19);
 
-         this.contexts[0] = 4 << 1 | 0;
 
-         this.contexts[UNIFORM_CONTEXT] = 46 << 1 | 0;
 
-         this.contexts[RUNLENGTH_CONTEXT] = 3 << 1 | 0;
 
-       },
 
-       setNeighborsSignificance: function BitModel_setNeighborsSignificance(row, column, index) {
 
-         var neighborsSignificance = this.neighborsSignificance;
 
-         var width = this.width,
 
-             height = this.height;
 
-         var left = column > 0;
 
-         var right = column + 1 < width;
 
-         var i;
 
-         if (row > 0) {
 
-           i = index - width;
 
-           if (left) {
 
-             neighborsSignificance[i - 1] += 0x10;
 
-           }
 
-           if (right) {
 
-             neighborsSignificance[i + 1] += 0x10;
 
-           }
 
-           neighborsSignificance[i] += 0x04;
 
-         }
 
-         if (row + 1 < height) {
 
-           i = index + width;
 
-           if (left) {
 
-             neighborsSignificance[i - 1] += 0x10;
 
-           }
 
-           if (right) {
 
-             neighborsSignificance[i + 1] += 0x10;
 
-           }
 
-           neighborsSignificance[i] += 0x04;
 
-         }
 
-         if (left) {
 
-           neighborsSignificance[index - 1] += 0x01;
 
-         }
 
-         if (right) {
 
-           neighborsSignificance[index + 1] += 0x01;
 
-         }
 
-         neighborsSignificance[index] |= 0x80;
 
-       },
 
-       runSignificancePropagationPass: function BitModel_runSignificancePropagationPass() {
 
-         var decoder = this.decoder;
 
-         var width = this.width,
 
-             height = this.height;
 
-         var coefficentsMagnitude = this.coefficentsMagnitude;
 
-         var coefficentsSign = this.coefficentsSign;
 
-         var neighborsSignificance = this.neighborsSignificance;
 
-         var processingFlags = this.processingFlags;
 
-         var contexts = this.contexts;
 
-         var labels = this.contextLabelTable;
 
-         var bitsDecoded = this.bitsDecoded;
 
-         var processedInverseMask = ~1;
 
-         var processedMask = 1;
 
-         var firstMagnitudeBitMask = 2;
 
-         for (var i0 = 0; i0 < height; i0 += 4) {
 
-           for (var j = 0; j < width; j++) {
 
-             var index = i0 * width + j;
 
-             for (var i1 = 0; i1 < 4; i1++, index += width) {
 
-               var i = i0 + i1;
 
-               if (i >= height) {
 
-                 break;
 
-               }
 
-               processingFlags[index] &= processedInverseMask;
 
-               if (coefficentsMagnitude[index] || !neighborsSignificance[index]) {
 
-                 continue;
 
-               }
 
-               var contextLabel = labels[neighborsSignificance[index]];
 
-               var decision = decoder.readBit(contexts, contextLabel);
 
-               if (decision) {
 
-                 var sign = this.decodeSignBit(i, j, index);
 
-                 coefficentsSign[index] = sign;
 
-                 coefficentsMagnitude[index] = 1;
 
-                 this.setNeighborsSignificance(i, j, index);
 
-                 processingFlags[index] |= firstMagnitudeBitMask;
 
-               }
 
-               bitsDecoded[index]++;
 
-               processingFlags[index] |= processedMask;
 
-             }
 
-           }
 
-         }
 
-       },
 
-       decodeSignBit: function BitModel_decodeSignBit(row, column, index) {
 
-         var width = this.width,
 
-             height = this.height;
 
-         var coefficentsMagnitude = this.coefficentsMagnitude;
 
-         var coefficentsSign = this.coefficentsSign;
 
-         var contribution, sign0, sign1, significance1;
 
-         var contextLabel, decoded;
 
-         significance1 = column > 0 && coefficentsMagnitude[index - 1] !== 0;
 
-         if (column + 1 < width && coefficentsMagnitude[index + 1] !== 0) {
 
-           sign1 = coefficentsSign[index + 1];
 
-           if (significance1) {
 
-             sign0 = coefficentsSign[index - 1];
 
-             contribution = 1 - sign1 - sign0;
 
-           } else {
 
-             contribution = 1 - sign1 - sign1;
 
-           }
 
-         } else if (significance1) {
 
-           sign0 = coefficentsSign[index - 1];
 
-           contribution = 1 - sign0 - sign0;
 
-         } else {
 
-           contribution = 0;
 
-         }
 
-         var horizontalContribution = 3 * contribution;
 
-         significance1 = row > 0 && coefficentsMagnitude[index - width] !== 0;
 
-         if (row + 1 < height && coefficentsMagnitude[index + width] !== 0) {
 
-           sign1 = coefficentsSign[index + width];
 
-           if (significance1) {
 
-             sign0 = coefficentsSign[index - width];
 
-             contribution = 1 - sign1 - sign0 + horizontalContribution;
 
-           } else {
 
-             contribution = 1 - sign1 - sign1 + horizontalContribution;
 
-           }
 
-         } else if (significance1) {
 
-           sign0 = coefficentsSign[index - width];
 
-           contribution = 1 - sign0 - sign0 + horizontalContribution;
 
-         } else {
 
-           contribution = horizontalContribution;
 
-         }
 
-         if (contribution >= 0) {
 
-           contextLabel = 9 + contribution;
 
-           decoded = this.decoder.readBit(this.contexts, contextLabel);
 
-         } else {
 
-           contextLabel = 9 - contribution;
 
-           decoded = this.decoder.readBit(this.contexts, contextLabel) ^ 1;
 
-         }
 
-         return decoded;
 
-       },
 
-       runMagnitudeRefinementPass: function BitModel_runMagnitudeRefinementPass() {
 
-         var decoder = this.decoder;
 
-         var width = this.width,
 
-             height = this.height;
 
-         var coefficentsMagnitude = this.coefficentsMagnitude;
 
-         var neighborsSignificance = this.neighborsSignificance;
 
-         var contexts = this.contexts;
 
-         var bitsDecoded = this.bitsDecoded;
 
-         var processingFlags = this.processingFlags;
 
-         var processedMask = 1;
 
-         var firstMagnitudeBitMask = 2;
 
-         var length = width * height;
 
-         var width4 = width * 4;
 
-         for (var index0 = 0, indexNext; index0 < length; index0 = indexNext) {
 
-           indexNext = Math.min(length, index0 + width4);
 
-           for (var j = 0; j < width; j++) {
 
-             for (var index = index0 + j; index < indexNext; index += width) {
 
-               if (!coefficentsMagnitude[index] || (processingFlags[index] & processedMask) !== 0) {
 
-                 continue;
 
-               }
 
-               var contextLabel = 16;
 
-               if ((processingFlags[index] & firstMagnitudeBitMask) !== 0) {
 
-                 processingFlags[index] ^= firstMagnitudeBitMask;
 
-                 var significance = neighborsSignificance[index] & 127;
 
-                 contextLabel = significance === 0 ? 15 : 14;
 
-               }
 
-               var bit = decoder.readBit(contexts, contextLabel);
 
-               coefficentsMagnitude[index] = coefficentsMagnitude[index] << 1 | bit;
 
-               bitsDecoded[index]++;
 
-               processingFlags[index] |= processedMask;
 
-             }
 
-           }
 
-         }
 
-       },
 
-       runCleanupPass: function BitModel_runCleanupPass() {
 
-         var decoder = this.decoder;
 
-         var width = this.width,
 
-             height = this.height;
 
-         var neighborsSignificance = this.neighborsSignificance;
 
-         var coefficentsMagnitude = this.coefficentsMagnitude;
 
-         var coefficentsSign = this.coefficentsSign;
 
-         var contexts = this.contexts;
 
-         var labels = this.contextLabelTable;
 
-         var bitsDecoded = this.bitsDecoded;
 
-         var processingFlags = this.processingFlags;
 
-         var processedMask = 1;
 
-         var firstMagnitudeBitMask = 2;
 
-         var oneRowDown = width;
 
-         var twoRowsDown = width * 2;
 
-         var threeRowsDown = width * 3;
 
-         var iNext;
 
-         for (var i0 = 0; i0 < height; i0 = iNext) {
 
-           iNext = Math.min(i0 + 4, height);
 
-           var indexBase = i0 * width;
 
-           var checkAllEmpty = i0 + 3 < height;
 
-           for (var j = 0; j < width; j++) {
 
-             var index0 = indexBase + j;
 
-             var allEmpty = checkAllEmpty && processingFlags[index0] === 0 && processingFlags[index0 + oneRowDown] === 0 && processingFlags[index0 + twoRowsDown] === 0 && processingFlags[index0 + threeRowsDown] === 0 && neighborsSignificance[index0] === 0 && neighborsSignificance[index0 + oneRowDown] === 0 && neighborsSignificance[index0 + twoRowsDown] === 0 && neighborsSignificance[index0 + threeRowsDown] === 0;
 
-             var i1 = 0,
 
-                 index = index0;
 
-             var i = i0,
 
-                 sign;
 
-             if (allEmpty) {
 
-               var hasSignificantCoefficent = decoder.readBit(contexts, RUNLENGTH_CONTEXT);
 
-               if (!hasSignificantCoefficent) {
 
-                 bitsDecoded[index0]++;
 
-                 bitsDecoded[index0 + oneRowDown]++;
 
-                 bitsDecoded[index0 + twoRowsDown]++;
 
-                 bitsDecoded[index0 + threeRowsDown]++;
 
-                 continue;
 
-               }
 
-               i1 = decoder.readBit(contexts, UNIFORM_CONTEXT) << 1 | decoder.readBit(contexts, UNIFORM_CONTEXT);
 
-               if (i1 !== 0) {
 
-                 i = i0 + i1;
 
-                 index += i1 * width;
 
-               }
 
-               sign = this.decodeSignBit(i, j, index);
 
-               coefficentsSign[index] = sign;
 
-               coefficentsMagnitude[index] = 1;
 
-               this.setNeighborsSignificance(i, j, index);
 
-               processingFlags[index] |= firstMagnitudeBitMask;
 
-               index = index0;
 
-               for (var i2 = i0; i2 <= i; i2++, index += width) {
 
-                 bitsDecoded[index]++;
 
-               }
 
-               i1++;
 
-             }
 
-             for (i = i0 + i1; i < iNext; i++, index += width) {
 
-               if (coefficentsMagnitude[index] || (processingFlags[index] & processedMask) !== 0) {
 
-                 continue;
 
-               }
 
-               var contextLabel = labels[neighborsSignificance[index]];
 
-               var decision = decoder.readBit(contexts, contextLabel);
 
-               if (decision === 1) {
 
-                 sign = this.decodeSignBit(i, j, index);
 
-                 coefficentsSign[index] = sign;
 
-                 coefficentsMagnitude[index] = 1;
 
-                 this.setNeighborsSignificance(i, j, index);
 
-                 processingFlags[index] |= firstMagnitudeBitMask;
 
-               }
 
-               bitsDecoded[index]++;
 
-             }
 
-           }
 
-         }
 
-       },
 
-       checkSegmentationSymbol: function BitModel_checkSegmentationSymbol() {
 
-         var decoder = this.decoder;
 
-         var contexts = this.contexts;
 
-         var symbol = decoder.readBit(contexts, UNIFORM_CONTEXT) << 3 | decoder.readBit(contexts, UNIFORM_CONTEXT) << 2 | decoder.readBit(contexts, UNIFORM_CONTEXT) << 1 | decoder.readBit(contexts, UNIFORM_CONTEXT);
 
-         if (symbol !== 0xA) {
 
-           throw new JpxError('Invalid segmentation symbol');
 
-         }
 
-       }
 
-     };
 
-     return BitModel;
 
-   }();
 
-   var Transform = function TransformClosure() {
 
-     function Transform() {}
 
-     Transform.prototype.calculate = function transformCalculate(subbands, u0, v0) {
 
-       var ll = subbands[0];
 
-       for (var i = 1, ii = subbands.length; i < ii; i++) {
 
-         ll = this.iterate(ll, subbands[i], u0, v0);
 
-       }
 
-       return ll;
 
-     };
 
-     Transform.prototype.extend = function extend(buffer, offset, size) {
 
-       var i1 = offset - 1,
 
-           j1 = offset + 1;
 
-       var i2 = offset + size - 2,
 
-           j2 = offset + size;
 
-       buffer[i1--] = buffer[j1++];
 
-       buffer[j2++] = buffer[i2--];
 
-       buffer[i1--] = buffer[j1++];
 
-       buffer[j2++] = buffer[i2--];
 
-       buffer[i1--] = buffer[j1++];
 
-       buffer[j2++] = buffer[i2--];
 
-       buffer[i1] = buffer[j1];
 
-       buffer[j2] = buffer[i2];
 
-     };
 
-     Transform.prototype.iterate = function Transform_iterate(ll, hl_lh_hh, u0, v0) {
 
-       var llWidth = ll.width,
 
-           llHeight = ll.height,
 
-           llItems = ll.items;
 
-       var width = hl_lh_hh.width;
 
-       var height = hl_lh_hh.height;
 
-       var items = hl_lh_hh.items;
 
-       var i, j, k, l, u, v;
 
-       for (k = 0, i = 0; i < llHeight; i++) {
 
-         l = i * 2 * width;
 
-         for (j = 0; j < llWidth; j++, k++, l += 2) {
 
-           items[l] = llItems[k];
 
-         }
 
-       }
 
-       llItems = ll.items = null;
 
-       var bufferPadding = 4;
 
-       var rowBuffer = new Float32Array(width + 2 * bufferPadding);
 
-       if (width === 1) {
 
-         if ((u0 & 1) !== 0) {
 
-           for (v = 0, k = 0; v < height; v++, k += width) {
 
-             items[k] *= 0.5;
 
-           }
 
-         }
 
-       } else {
 
-         for (v = 0, k = 0; v < height; v++, k += width) {
 
-           rowBuffer.set(items.subarray(k, k + width), bufferPadding);
 
-           this.extend(rowBuffer, bufferPadding, width);
 
-           this.filter(rowBuffer, bufferPadding, width);
 
-           items.set(rowBuffer.subarray(bufferPadding, bufferPadding + width), k);
 
-         }
 
-       }
 
-       var numBuffers = 16;
 
-       var colBuffers = [];
 
-       for (i = 0; i < numBuffers; i++) {
 
-         colBuffers.push(new Float32Array(height + 2 * bufferPadding));
 
-       }
 
-       var b,
 
-           currentBuffer = 0;
 
-       ll = bufferPadding + height;
 
-       if (height === 1) {
 
-         if ((v0 & 1) !== 0) {
 
-           for (u = 0; u < width; u++) {
 
-             items[u] *= 0.5;
 
-           }
 
-         }
 
-       } else {
 
-         for (u = 0; u < width; u++) {
 
-           if (currentBuffer === 0) {
 
-             numBuffers = Math.min(width - u, numBuffers);
 
-             for (k = u, l = bufferPadding; l < ll; k += width, l++) {
 
-               for (b = 0; b < numBuffers; b++) {
 
-                 colBuffers[b][l] = items[k + b];
 
-               }
 
-             }
 
-             currentBuffer = numBuffers;
 
-           }
 
-           currentBuffer--;
 
-           var buffer = colBuffers[currentBuffer];
 
-           this.extend(buffer, bufferPadding, height);
 
-           this.filter(buffer, bufferPadding, height);
 
-           if (currentBuffer === 0) {
 
-             k = u - numBuffers + 1;
 
-             for (l = bufferPadding; l < ll; k += width, l++) {
 
-               for (b = 0; b < numBuffers; b++) {
 
-                 items[k + b] = colBuffers[b][l];
 
-               }
 
-             }
 
-           }
 
-         }
 
-       }
 
-       return {
 
-         width: width,
 
-         height: height,
 
-         items: items
 
-       };
 
-     };
 
-     return Transform;
 
-   }();
 
-   var IrreversibleTransform = function IrreversibleTransformClosure() {
 
-     function IrreversibleTransform() {
 
-       Transform.call(this);
 
-     }
 
-     IrreversibleTransform.prototype = Object.create(Transform.prototype);
 
-     IrreversibleTransform.prototype.filter = function irreversibleTransformFilter(x, offset, length) {
 
-       var len = length >> 1;
 
-       offset = offset | 0;
 
-       var j, n, current, next;
 
-       var alpha = -1.586134342059924;
 
-       var beta = -0.052980118572961;
 
-       var gamma = 0.882911075530934;
 
-       var delta = 0.443506852043971;
 
-       var K = 1.230174104914001;
 
-       var K_ = 1 / K;
 
-       j = offset - 3;
 
-       for (n = len + 4; n--; j += 2) {
 
-         x[j] *= K_;
 
-       }
 
-       j = offset - 2;
 
-       current = delta * x[j - 1];
 
-       for (n = len + 3; n--; j += 2) {
 
-         next = delta * x[j + 1];
 
-         x[j] = K * x[j] - current - next;
 
-         if (n--) {
 
-           j += 2;
 
-           current = delta * x[j + 1];
 
-           x[j] = K * x[j] - current - next;
 
-         } else {
 
-           break;
 
-         }
 
-       }
 
-       j = offset - 1;
 
-       current = gamma * x[j - 1];
 
-       for (n = len + 2; n--; j += 2) {
 
-         next = gamma * x[j + 1];
 
-         x[j] -= current + next;
 
-         if (n--) {
 
-           j += 2;
 
-           current = gamma * x[j + 1];
 
-           x[j] -= current + next;
 
-         } else {
 
-           break;
 
-         }
 
-       }
 
-       j = offset;
 
-       current = beta * x[j - 1];
 
-       for (n = len + 1; n--; j += 2) {
 
-         next = beta * x[j + 1];
 
-         x[j] -= current + next;
 
-         if (n--) {
 
-           j += 2;
 
-           current = beta * x[j + 1];
 
-           x[j] -= current + next;
 
-         } else {
 
-           break;
 
-         }
 
-       }
 
-       if (len !== 0) {
 
-         j = offset + 1;
 
-         current = alpha * x[j - 1];
 
-         for (n = len; n--; j += 2) {
 
-           next = alpha * x[j + 1];
 
-           x[j] -= current + next;
 
-           if (n--) {
 
-             j += 2;
 
-             current = alpha * x[j + 1];
 
-             x[j] -= current + next;
 
-           } else {
 
-             break;
 
-           }
 
-         }
 
-       }
 
-     };
 
-     return IrreversibleTransform;
 
-   }();
 
-   var ReversibleTransform = function ReversibleTransformClosure() {
 
-     function ReversibleTransform() {
 
-       Transform.call(this);
 
-     }
 
-     ReversibleTransform.prototype = Object.create(Transform.prototype);
 
-     ReversibleTransform.prototype.filter = function reversibleTransformFilter(x, offset, length) {
 
-       var len = length >> 1;
 
-       offset = offset | 0;
 
-       var j, n;
 
-       for (j = offset, n = len + 1; n--; j += 2) {
 
-         x[j] -= x[j - 1] + x[j + 1] + 2 >> 2;
 
-       }
 
-       for (j = offset + 1, n = len; n--; j += 2) {
 
-         x[j] += x[j - 1] + x[j + 1] >> 1;
 
-       }
 
-     };
 
-     return ReversibleTransform;
 
-   }();
 
-   return JpxImage;
 
- }();
 
- exports.JpxImage = JpxImage;
 
 
  |