Use the updated jpgjs with faster transform.

This commit is contained in:
Brendan Dahl 2011-11-22 09:32:20 -08:00
parent 7a5556ca91
commit b64b7d55b1
3 changed files with 261 additions and 68 deletions

View File

@ -1,6 +1,9 @@
Copyright (C) 2011 by notmasteryet
Contributors: Yury Delendik
Brendan Dahl <bdahl@mozilla.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including

323
external/jpgjs/jpg.js vendored
View File

@ -11,44 +11,36 @@
var JpegImage = (function jpegImage() {
"use strict";
var dctZigZag = new Int32Array([
0,
1, 8,
16, 9, 2,
3, 10, 17, 24,
32, 25, 18, 11, 4,
5, 12, 19, 26, 33, 40,
48, 41, 34, 27, 20, 13, 6,
7, 14, 21, 28, 35, 42, 49, 56,
57, 50, 43, 36, 29, 22, 15,
23, 30, 37, 44, 51, 58,
59, 52, 45, 38, 31,
39, 46, 53, 60,
61, 54, 47,
55, 62,
63
]);
function constructor(colorTransform) {
this.colorTransform = typeof colorTransform !== 'undefined' ? colorTransform : -1;
var dctCos1 = 4017 // cos(pi/16)
var dctSin1 = 799 // sin(pi/16)
var dctCos3 = 3406 // cos(3*pi/16)
var dctSin3 = 2276 // sin(3*pi/16)
var dctCos6 = 1567 // cos(6*pi/16)
var dctSin6 = 3784 // sin(6*pi/16)
var dctSqrt2 = 5793 // sqrt(2)
var dctSqrt1d2 = 2896 // sqrt(2) / 2
function constructor() {
}
var iDCTTables = (function initDCTTables() {
var cosTables = [], i, j;
for (i = 0; i < 8; i++) {
cosTables.push(new Float32Array(8));
for (j = 0; j < 8; j++)
cosTables[i][j] = Math.cos((2 * i + 1) * j * Math.PI / 16) *
(j > 0 ? 1 : 1/Math.sqrt(2));
}
var zzTransform = new Int32Array([
0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12,
17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 40, 44, 53, 10, 19, 23, 32,
39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34, 37, 47,
50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63]);
var x, y, u, v;
var tables = [];
for (y = 0; y < 8; y++) {
var cosTable_y = cosTables[y];
for (x = 0; x < 8; x++) {
var cosTable_x = cosTables[x];
var table = new Float32Array(64);
i = 0;
for (v = 0; v < 8; v++) {
for (u = 0; u < 8; u++)
table[zzTransform[i++]] = cosTable_x[u] * cosTable_y[v];
}
tables.push(table);
}
}
return tables;
})();
function buildHuffmanTable(codeLengths, values) {
var k = 0, code = [], i, j, length = 16;
while (length > 0 && !codeLengths[length - 1])
@ -151,7 +143,8 @@ var JpegImage = (function jpegImage() {
continue;
}
k += r;
zz[k] = receiveAndExtend(s);
var z = dctZigZag[k];
zz[z] = receiveAndExtend(s);
k++;
}
}
@ -182,7 +175,8 @@ var JpegImage = (function jpegImage() {
continue;
}
k += r;
zz[k] = receiveAndExtend(s) * (1 << successive);
var z = dctZigZag[k];
zz[z] = receiveAndExtend(s) * (1 << successive);
k++;
}
}
@ -190,6 +184,7 @@ var JpegImage = (function jpegImage() {
function decodeACSuccessive(component, zz) {
var k = spectralStart, e = spectralEnd, r = 0;
while (k <= e) {
var z = dctZigZag[k];
switch (successiveACState) {
case 0: // initial state
var rs = decodeHuffman(component.huffmanTableAC);
@ -211,8 +206,8 @@ var JpegImage = (function jpegImage() {
continue;
case 1: // skipping r zero items
case 2:
if (zz[k])
zz[k] += (readBit() << successive);
if (zz[z])
zz[z] += (readBit() << successive);
else {
r--;
if (r === 0)
@ -220,16 +215,16 @@ var JpegImage = (function jpegImage() {
}
break;
case 3: // set value for a zero item
if (zz[k])
zz[k] += (readBit() << successive);
if (zz[z])
zz[z] += (readBit() << successive);
else {
zz[k] = successiveACNextValue << successive;
zz[z] = successiveACNextValue << successive;
successiveACState = 0;
}
break;
case 4: // eob
if (zz[k])
zz[k] += (readBit() << successive);
if (zz[z])
zz[z] += (readBit() << successive);
break;
}
k++;
@ -325,27 +320,166 @@ var JpegImage = (function jpegImage() {
var blocksPerLine = component.blocksPerLine;
var blocksPerColumn = component.blocksPerColumn;
var samplesPerLine = blocksPerLine << 3;
var R = new Int32Array(64), r = new Uint8Array(64);
function quantizeAndInverse(zz) {
// A port of poppler's IDCT method which in turn is taken from:
// Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
// "Practical Fast 1-D DCT Algorithms with 11 Multiplications",
// IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
// 988-991.
function quantizeAndInverse(zz, dataOut, dataIn) {
var qt = component.quantizationTable;
var precisionShift = frame.precision - 8;
var v0, v1, v2, v3, v4, v5, v6, v7, t;
var p = dataIn;
var i;
var R = new Int32Array(64);
// dequant
for (i = 0; i < 64; i++)
R[i] = zz[i] * qt[i];
p[i] = zz[i] * qt[i];
var r = new Uint8Array(64), i, j;
for (i = 0; i < 64; i++) {
var sum = 0;
var table = iDCTTables[i];
for (j = 0; j < 64; j++)
sum += table[j] * R[j];
// TODO loosing precision?
var sample = 128 + ((sum / 4) >> precisionShift);
// clamping
r[i] = sample < 0 ? 0 : sample > 0xFF ? 0xFF : sample;
// inverse DCT on rows
for (i = 0; i < 8; ++i) {
var row = 8 * i;
// check for all-zero AC coefficients
if (p[1 + row] == 0 && p[2 + row] == 0 && p[3 + row] == 0 &&
p[4 + row] == 0 && p[5 + row] == 0 && p[6 + row] == 0 &&
p[7 + row] == 0) {
t = (dctSqrt2 * p[0 + row] + 512) >> 10;
p[0 + row] = t;
p[1 + row] = t;
p[2 + row] = t;
p[3 + row] = t;
p[4 + row] = t;
p[5 + row] = t;
p[6 + row] = t;
p[7 + row] = t;
continue;
}
// stage 4
v0 = (dctSqrt2 * p[0 + row] + 128) >> 8;
v1 = (dctSqrt2 * p[4 + row] + 128) >> 8;
v2 = p[2 + row];
v3 = p[6 + row];
v4 = (dctSqrt1d2 * (p[1 + row] - p[7 + row]) + 128) >> 8;
v7 = (dctSqrt1d2 * (p[1 + row] + p[7 + row]) + 128) >> 8;
v5 = p[3 + row] << 4;
v6 = p[5 + row] << 4;
// stage 3
t = (v0 - v1+ 1) >> 1;
v0 = (v0 + v1 + 1) >> 1;
v1 = t;
t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
v3 = t;
t = (v4 - v6 + 1) >> 1;
v4 = (v4 + v6 + 1) >> 1;
v6 = t;
t = (v7 + v5 + 1) >> 1;
v5 = (v7 - v5 + 1) >> 1;
v7 = t;
// stage 2
t = (v0 - v3 + 1) >> 1;
v0 = (v0 + v3 + 1) >> 1;
v3 = t;
t = (v1 - v2 + 1) >> 1;
v1 = (v1 + v2 + 1) >> 1;
v2 = t;
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
v7 = t;
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
v6 = t;
// stage 1
p[0 + row] = v0 + v7;
p[7 + row] = v0 - v7;
p[1 + row] = v1 + v6;
p[6 + row] = v1 - v6;
p[2 + row] = v2 + v5;
p[5 + row] = v2 - v5;
p[3 + row] = v3 + v4;
p[4 + row] = v3 - v4;
}
// inverse DCT on columns
for (i = 0; i < 8; ++i) {
var col = i;
// check for all-zero AC coefficients
if (p[1*8 + col] == 0 && p[2*8 + col] == 0 && p[3*8 + col] == 0 &&
p[4*8 + col] == 0 && p[5*8 + col] == 0 && p[6*8 + col] == 0 &&
p[7*8 + col] == 0) {
t = (dctSqrt2 * dataIn[i+0] + 8192) >> 14;
p[0*8 + col] = t;
p[1*8 + col] = t;
p[2*8 + col] = t;
p[3*8 + col] = t;
p[4*8 + col] = t;
p[5*8 + col] = t;
p[6*8 + col] = t;
p[7*8 + col] = t;
continue;
}
// stage 4
v0 = (dctSqrt2 * p[0*8 + col] + 2048) >> 12;
v1 = (dctSqrt2 * p[4*8 + col] + 2048) >> 12;
v2 = p[2*8 + col];
v3 = p[6*8 + col];
v4 = (dctSqrt1d2 * (p[1*8 + col] - p[7*8 + col]) + 2048) >> 12;
v7 = (dctSqrt1d2 * (p[1*8 + col] + p[7*8 + col]) + 2048) >> 12;
v5 = p[3*8 + col];
v6 = p[5*8 + col];
// stage 3
t = (v0 - v1 + 1) >> 1;
v0 = (v0 + v1 + 1) >> 1;
v1 = t;
t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
v3 = t;
t = (v4 - v6 + 1) >> 1;
v4 = (v4 + v6 + 1) >> 1;
v6 = t;
t = (v7 + v5 + 1) >> 1;
v5 = (v7 - v5 + 1) >> 1;
v7 = t;
// stage 2
t = (v0 - v3 + 1) >> 1;
v0 = (v0 + v3 + 1) >> 1;
v3 = t;
t = (v1 - v2 + 1) >> 1;
v1 = (v1 + v2 + 1) >> 1;
v2 = t;
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
v7 = t;
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
v6 = t;
// stage 1
p[0*8 + col] = v0 + v7;
p[7*8 + col] = v0 - v7;
p[1*8 + col] = v1 + v6;
p[6*8 + col] = v1 - v6;
p[2*8 + col] = v2 + v5;
p[5*8 + col] = v2 - v5;
p[3*8 + col] = v3 + v4;
p[4*8 + col] = v3 - v4;
}
// convert to 8-bit integers
for (i = 0; i < 64; ++i) {
var sample = 128 + ((p[i] + 8) >> 4);
dataOut[i] = sample < 0 ? 0 : sample > 0xFF ? 0xFF : sample;
}
return r;
}
var i, j;
@ -354,7 +488,7 @@ var JpegImage = (function jpegImage() {
for (i = 0; i < 8; i++)
lines.push(new Uint8Array(samplesPerLine));
for (var blockCol = 0; blockCol < blocksPerLine; blockCol++) {
var r = quantizeAndInverse(component.blocks[blockRow][blockCol]);
quantizeAndInverse(component.blocks[blockRow][blockCol], r, R);
var offset = 0, sample = blockCol << 3;
for (j = 0; j < 8; j++) {
@ -498,8 +632,10 @@ var JpegImage = (function jpegImage() {
var quantizationTableSpec = data[offset++];
var tableData = new Int32Array(64);
if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
for (j = 0; j < 64; j++)
tableData[j] = data[offset++];
for (j = 0; j < 64; j++) {
var z = dctZigZag[j];
tableData[z] = data[offset++];
}
} else if ((quantizationTableSpec >> 4) === 1) { //16 bit
tableData[j] = readUint16();
} else
@ -634,8 +770,8 @@ var JpegImage = (function jpegImage() {
// The adobe transform marker overrides any previous setting
if (this.adobe && this.adobe.transformCode)
colorTransform = true;
else if (typeof this.colorTransform != -1)
colorTransform = this.colorTransform == true;
else if (typeof this.colorTransform !== 'undefined')
colorTransform = !!this.colorTransform;
component1 = this.components[0];
component2 = this.components[1];
@ -673,8 +809,8 @@ var JpegImage = (function jpegImage() {
// The adobe transform marker overrides any previous setting
if (this.adobe && this.adobe.transformCode)
colorTransform = true;
else if (typeof this.colorTransform != -1)
colorTransform = this.colorTransform == true;
else if (typeof this.colorTransform !== 'undefined')
colorTransform = !!this.colorTransform;
component1 = this.components[0];
component2 = this.components[1];
@ -714,7 +850,60 @@ var JpegImage = (function jpegImage() {
return data;
},
copyToImageData: function copyToImageData(imageData) {
this.getData(imageData.data, imageData.width, imageData.height);
var width = imageData.width, height = imageData.height;
var imageDataArray = imageData.data;
var data = this.getData(width, height);
var i = 0, j = 0, x, y;
var Y, K, C, M, R, G, B;
switch (this.components.length) {
case 1:
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
Y = data[i++];
imageDataArray[j++] = Y;
imageDataArray[j++] = Y;
imageDataArray[j++] = Y;
imageDataArray[j++] = 255;
}
}
break;
case 3:
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
R = data[i++];
G = data[i++];
B = data[i++];
imageDataArray[j++] = R;
imageDataArray[j++] = G;
imageDataArray[j++] = B;
imageDataArray[j++] = 255;
}
}
break;
case 4:
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
C = data[i++];
M = data[i++];
Y = data[i++];
K = data[i++];
R = 255 - clampTo8bit(C * (1 - K / 255) + K);
G = 255 - clampTo8bit(M * (1 - K / 255) + K);
B = 255 - clampTo8bit(Y * (1 - K / 255) + K);
imageDataArray[j++] = R;
imageDataArray[j++] = G;
imageDataArray[j++] = B;
imageDataArray[j++] = 255;
}
}
break;
default:
throw 'Unsupported color mode';
}
}
};

View File

@ -830,7 +830,8 @@ var JpegStream = (function jpegStream() {
constructor.prototype.ensureBuffer = function jpegStreamEnsureBuffer(req) {
if (this.bufferLength)
return;
var jpegImage = new JpegImage(this.colorTransform);
var jpegImage = new JpegImage();
jpegImage.colorTransform = this.colorTransform;
jpegImage.parse(this.bytes);
var width = jpegImage.width;
var height = jpegImage.height;