ami.js
Version:
<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>
1,311 lines (1,276 loc) • 137 kB
JavaScript
/*! image-JPEG2000 - v0.3.1 - 2015-08-26 | https://github.com/OHIF/image-JPEG2000 */
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* Copyright 2012 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.
*/
/* globals ArithmeticDecoder, globalScope, log2, readUint16, readUint32,
info, warn */
'use strict';
var JpxImage = (function JpxImageClosure() {
// Table E.1
var SubbandsGainLog2 = {
'LL': 0,
'LH': 1,
'HL': 1,
'HH': 2
};
function JpxImage() {
this.failOnCorruptedImage = false;
}
JpxImage.prototype = {
parse: function JpxImage_parse(data) {
var head = readUint16(data, 0);
// No box header, immediate start of codestream (SOC)
if (head === 0xFF4F) {
this.parseCodestream(data, 0, data.length);
return;
}
var position = 0, length = data.length;
while (position < length) {
var headerSize = 8;
var lbox = readUint32(data, position);
var tbox = readUint32(data, position + 4);
position += headerSize;
if (lbox === 1) {
// XLBox: read UInt64 according to spec.
// JavaScript's int precision of 53 bit should be sufficient here.
lbox = readUint32(data, position) * 4294967296 +
readUint32(data, position + 4);
position += 8;
headerSize += 8;
}
if (lbox === 0) {
lbox = length - position + headerSize;
}
if (lbox < headerSize) {
throw new Error('JPX Error: Invalid box field size');
}
var dataLength = lbox - headerSize;
var jumpDataLength = true;
switch (tbox) {
case 0x6A703268: // 'jp2h'
jumpDataLength = false; // parsing child boxes
break;
case 0x636F6C72: // 'colr'
// Colorspaces are not used, the CS from the PDF is used.
var method = data[position];
var precedence = data[position + 1];
var approximation = data[position + 2];
if (method === 1) {
// enumerated colorspace
var colorspace = readUint32(data, position + 3);
switch (colorspace) {
case 16: // this indicates a sRGB colorspace
case 17: // this indicates a grayscale colorspace
case 18: // this indicates a YUV colorspace
break;
default:
warn('Unknown colorspace ' + colorspace);
break;
}
} else if (method === 2) {
info('ICC profile not supported');
}
break;
case 0x6A703263: // 'jp2c'
this.parseCodestream(data, position, position + dataLength);
break;
case 0x6A502020: // 'jP\024\024'
if (0x0d0a870a !== readUint32(data, position)) {
warn('Invalid JP2 signature');
}
break;
// The following header types are valid but currently not used:
case 0x6A501A1A: // 'jP\032\032'
case 0x66747970: // 'ftyp'
case 0x72726571: // 'rreq'
case 0x72657320: // 'res '
case 0x69686472: // 'ihdr'
break;
default:
var headerType = String.fromCharCode((tbox >> 24) & 0xFF,
(tbox >> 16) & 0xFF,
(tbox >> 8) & 0xFF,
tbox & 0xFF);
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;
// Image and tile size (SIZ)
if (code === 0xFF51) {
stream.skip(4);
var Xsiz = stream.getInt32() >>> 0; // Byte 4
var Ysiz = stream.getInt32() >>> 0; // Byte 8
var XOsiz = stream.getInt32() >>> 0; // Byte 12
var YOsiz = stream.getInt32() >>> 0; // Byte 16
stream.skip(16);
var Csiz = stream.getUint16(); // Byte 36
this.width = Xsiz - XOsiz;
this.height = Ysiz - YOsiz;
this.componentsCount = Csiz;
// Results are always returned as Uint8Arrays
this.bitsPerComponent = 8;
return;
}
}
throw new Error('JPX Error: No size marker found in JPX stream');
},
parseCodestream: function JpxImage_parseCodestream(data, start, end) {
var context = {};
try {
var doNotRecover = false;
var position = start;
while (position + 1 < end) {
var code = readUint16(data, position);
position += 2;
var length = 0, j, sqcd, spqcds, spqcdSize, scalarExpounded, tile;
switch (code) {
case 0xFF4F: // Start of codestream (SOC)
context.mainHeader = true;
break;
case 0xFFD9: // End of codestream (EOC)
break;
case 0xFF51: // Image and tile size (SIZ)
length = readUint16(data, position);
var siz = {};
siz.Xsiz = readUint32(data, position + 4);
siz.Ysiz = readUint32(data, position + 8);
siz.XOsiz = readUint32(data, position + 12);
siz.YOsiz = readUint32(data, position + 16);
siz.XTsiz = readUint32(data, position + 20);
siz.YTsiz = readUint32(data, position + 24);
siz.XTOsiz = readUint32(data, position + 28);
siz.YTOsiz = readUint32(data, position + 32);
var componentsCount = 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: // Quantization default (QCD)
length = 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('JPX 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: // Quantization component (QCC)
length = readUint16(data, position);
var qcc = {};
j = position + 2;
var cqcc;
if (context.SIZ.Csiz < 257) {
cqcc = data[j++];
} else {
cqcc = 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('JPX 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: // Coding style default (COD)
length = 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 = 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('JPX Error: Unsupported COD options (' +
unsupported.join(', ') + ')');
}
if (context.mainHeader) {
context.COD = cod;
} else {
context.currentTile.COD = cod;
context.currentTile.COC = [];
}
break;
case 0xFF90: // Start of tile-part (SOT)
length = readUint16(data, position);
tile = {};
tile.index = readUint16(data, position + 2);
tile.length = 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) {
// reset component specific settings
tile.COD = context.COD;
tile.COC = context.COC.slice(0); // clone of the global COC
tile.QCD = context.QCD;
tile.QCC = context.QCC.slice(0); // clone of the global COC
}
context.currentTile = tile;
break;
case 0xFF93: // Start of data (SOD)
tile = context.currentTile;
if (tile.partIndex === 0) {
initializeTile(context, tile.index);
buildPackets(context);
}
// moving to the end of the data
length = tile.dataEnd - position;
parseTilePackets(context, data, position, length);
break;
case 0xFF55: // Tile-part lengths, main header (TLM)
case 0xFF57: // Packet length, main header (PLM)
case 0xFF58: // Packet length, tile-part header (PLT)
case 0xFF64: // Comment (COM)
length = readUint16(data, position);
// skipping content
break;
case 0xFF53: // Coding style component (COC)
throw new Error('JPX Error: Codestream code 0xFF53 (COC) is ' +
'not implemented');
default:
throw new Error('JPX Error: Unknown codestream code: ' +
code.toString(16));
}
position += length;
}
} catch (e) {
if (doNotRecover || this.failOnCorruptedImage) {
throw e;
} else {
warn('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) {
// Section B.2 Component mapping
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;
// Section B.3 Division into tile and tile-components
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;
}
// calculate codeblock size as described in section B.7
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) {
// Section B.6 Division resolution to precincts
var precinctWidth = 1 << dimensions.PPx;
var precinctHeight = 1 << dimensions.PPy;
// Jasper introduces codeblock groups for mapping each subband codeblocks
// to precincts. Precinct partition divides a resolution according to width
// and height parameters. The subband that belongs to the resolution level
// has a different size than the level, unless it is the zero resolution.
// From Jasper documentation: jpeg2000.pdf, section K: Tier-2 coding:
// The precinct partitioning for a particular subband is derived from a
// partitioning of its parent LL band (i.e., the LL band at the next higher
// resolution level)... The LL band associated with each resolution level is
// divided into precincts... Each of the resulting precinct regions is then
// mapped into its child subbands (if any) at the next lower resolution
// level. This is accomplished by using the coordinate transformation
// (u, v) = (ceil(x/2), ceil(y/2)) where (x, y) and (u, v) are the
// coordinates of a point in the LL band and child subband, respectively.
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) {
// Section B.7 Division sub-band into code-blocks
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);
// Calculate precinct number for this codeblock, codeblock position
// should be relative to its subband, use actual dimension and position
// See comment about codeblock group width and height
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);
// building precinct for the sub-band
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 = [];
// Section B.10.8 Order of info in packet
var subbands = resolution.subbands;
// sub-bands already ordered in 'LL', 'HL', 'LH', and 'HH' sequence
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() {
// Section B.12.1.1 Layer-resolution-component-position
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;
}
};
}
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() {
// Section B.12.1.2 Resolution-layer-component-position
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;
}
};
}
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() {
// Section B.12.1.3 Resolution-position-component-layer
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;
}
};
}
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() {
// Section B.12.1.4 Position-component-resolution-layer
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;
}
};
}
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() {
// Section B.12.1.5 Component-position-resolution-layer
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;
}
};
}
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;
// Creating resolutions and sub-bands for each component
for (var c = 0; c < componentsCount; c++) {
var component = tile.components[c];
var decompositionLevelsCount =
component.codingStyleParameters.decompositionLevelsCount;
// Section B.5 Resolution levels and sub-bands
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) {
// one sub-band (LL) with last decomposition
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 = [];
// three sub-bands (HL, LH and HH) with rest of decompositions
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;
}
// Generate the packets sequence
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 Error('JPX Error: 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) {
if(offset + position >= data.length){
throw new Error("Unexpected EOF");
}
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) {
try{
alignToByte();
if (sopMarkerUsed && skipMarkerIfEqual(0x91)) {
// Skip also marker segment length and packet sequence ID
skipBytes(4);
}
var packet = packetsIterator.nextPacket();
if (packet === undefined) {
//No more packets. Stream is probably truncated.
return;
}
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 {
// reading inclusion tree
precinct = codeblock.precinct;
var inclusionTree, zeroBitPlanesTree;
if (precinct['inclusionTree'] !== undefined) {
inclusionTree = precinct.inclusionTree;
} else {
// building inclusion and zero bit-planes trees
var width = precinct.cbxMax - precinct.cbxMin + 1;
var height = precinct.cbyMax - precinct.cbyMin + 1;
inclusionTree = new InclusionTree(width, height);
zeroBitPlanesTree = new TagTree(width, height);
precinct.inclusionTree = inclusionTree;
precinct.zeroBitPlanesTree = zeroBitPlanesTree;
}
inclusionTree.reset(codeblockColumn, codeblockRow, layerNumber);
while (true) {
if (position >= data.length) {
return;
}
if (inclusionTree.isAboveThreshold()){
break;
}
if (inclusionTree.isKnown()) {
inclusionTree.nextLevel();
continue;
}
if (readBits(1)) {
inclusionTree.setKnown();
if (inclusionTree.isLeaf()) {
codeblock.included = true;
codeblockIncluded = firstTimeInclusion = true;
break;
} else {
inclusionTree.nextLevel();
}
} else {
inclusionTree.incrementValue();
}
}
}
if (!codeblockIncluded) {
continue;
}
if (firstTimeInclusion) {
zeroBitPlanesTree = precinct.zeroBitPlanesTree;
zeroBitPlanesTree.reset(codeblockColumn, codeblockRow);
while (true) {
if (position >= data.length) {
return;
}
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 = log2(codingpasses);
// rounding down log2
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;
}
} catch (e) {
return;
}
}
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; // first bit plane starts from cleanup
// collect data
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 Int16Array(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;
}
// decoding the item
var decoder = new ArithmeticDecoder(encodedData, 0, totalLength);
bitModel.setDecoder(decoder);
for (j = 0; j < codingpasses; j++) {
switch (currentCodingpassType) {
case 0:
bitModel.runSignificancePropogationPass();
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;
// Do the interleaving of Section F.3.3 here, so we do not need
// to copy later. LL level is not interleaved, just copied.
var interleave = (subband.type !== 'LL');
for (j = 0; j < blockHeight; j++) {
var row = (offset / width) | 0; // row in the non-interleaved subband
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));
}
}