Skip to content

Commit

Permalink
Fixes for small colour variations in tests
Browse files Browse the repository at this point in the history
* Most uses of clampTo8Bit actually didn't want 8-bit integers, but just a clamp
  between 0 and 255. There are now two functions. quantizeAndInverse says in its
  comments that these integers are needed, so small colour variations in the
  tests for this cases are expected
* The outputBuffer for quantizeAndInverse was changed back to Int32 to allow
  greater precision in the calculations
  • Loading branch information
Thorben Bochenek committed Mar 21, 2014
1 parent 9a0b2b0 commit 4b8e7c5
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions jpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ var JpegImage = (function jpegImage() {

// convert to 8-bit integers
for (i = 0; i < 64; ++i) {
p[i] = clampTo8bit((p[i] + 2056) >> 4);
p[i] = clampTo8bitInt((p[i] + 2056) >> 4);
}
}

Expand All @@ -515,7 +515,7 @@ var JpegImage = (function jpegImage() {
var blocksPerLine = component.blocksPerLine;
var blocksPerColumn = component.blocksPerColumn;
var samplesPerLine = blocksPerLine << 3;
var computationBuffer = new Int16Array(64);
var computationBuffer = new Int32Array(64);

var i, j, ll = 0;
for (var blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
Expand All @@ -541,10 +541,14 @@ var JpegImage = (function jpegImage() {
return lines;
}

function clampTo8bit(a) {
function clampTo8bitInt(a) {
return a <= 0 ? 0 : a >= 255 ? 255 : a | 0;
}

function clamp0to255(a) {
return a <= 0 ? 0 : a >= 255 ? 255 : a;
}

constructor.prototype = {
load: function load(path) {
var xhr = new XMLHttpRequest();
Expand Down Expand Up @@ -836,9 +840,9 @@ var JpegImage = (function jpegImage() {
Cb = data[i + 1];
Cr = data[i + 2];

R = clampTo8bit(Y + 1.402 * (Cr - 128));
G = clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
B = clampTo8bit(Y + 1.772 * (Cb - 128));
R = clamp0to255(Y + 1.402 * (Cr - 128));
G = clamp0to255(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
B = clamp0to255(Y + 1.772 * (Cb - 128));

data[i ] = R;
data[i + 1] = G;
Expand All @@ -863,9 +867,9 @@ var JpegImage = (function jpegImage() {
Cb = data[i + 1];
Cr = data[i + 2];

C = 255 - clampTo8bit(Y + 1.402 * (Cr - 128));
M = 255 - clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
Ye = 255 - clampTo8bit(Y + 1.772 * (Cb - 128));
C = 255 - clamp0to255(Y + 1.402 * (Cr - 128));
M = 255 - clamp0to255(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
Ye = 255 - clamp0to255(Y + 1.772 * (Cb - 128));

data[i ] = C;
data[i + 1] = M;
Expand Down Expand Up @@ -916,9 +920,9 @@ var JpegImage = (function jpegImage() {
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);
R = 255 - clamp0to255(C * (1 - K / 255) + K);
G = 255 - clamp0to255(M * (1 - K / 255) + K);
B = 255 - clamp0to255(Y * (1 - K / 255) + K);

imageDataArray[j++] = R;
imageDataArray[j++] = G;
Expand Down

0 comments on commit 4b8e7c5

Please sign in to comment.