Optimized JPX's transformComponents

This commit is contained in:
p01 2014-04-28 17:24:22 +02:00
parent 27d532efeb
commit 7067409e00

View File

@ -1076,7 +1076,7 @@ var JpxImage = (function JpxImageClosure() {
};
// Section G.2.2 Inverse multi component transform
var shift, offset, max, min;
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;
@ -1090,45 +1090,44 @@ var JpxImage = (function JpxImageClosure() {
// compute shift and offset only once.
shift = components[0].precision - 8;
offset = (128 << shift) + 0.5;
max = (127.5 * (1 << shift));
min = -max;
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) {
// inverse irreversible multiple component transform
for (j = 0, jj = y0items.length; j < jj; ++j) {
y0 = y0items[j];
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 <= min ? 0 : r >= max ? 255 : (r + offset) >> shift;
out[pos++] = g <= min ? 0 : g >= max ? 255 : (g + offset) >> shift;
out[pos++] = b <= min ? 0 : b >= max ? 255 : (b + offset) >> shift;
if (fourComponents) {
k = y3items[j];
out[pos++] =
k <= min ? 0 : k >= max ? 255 : (k + offset) >> shift;
}
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 {
// inverse reversible multiple component transform
for (j = 0, jj = y0items.length; j < jj; ++j) {
y0 = y0items[j];
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 <= min ? 0 : r >= max ? 255 : (r + offset) >> shift;
out[pos++] = g <= min ? 0 : g >= max ? 255 : (g + offset) >> shift;
out[pos++] = b <= min ? 0 : b >= max ? 255 : (b + offset) >> shift;
if (fourComponents) {
k = y3items[j];
out[pos++] =
k <= min ? 0 : k >= max ? 255 : (k + offset) >> shift;
}
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 { // no multi-component transform