Skip to content

Commit 1f53fe1

Browse files
committed
fix for Unable to parse the attached JPEG Lossless file correctly #5
1 parent 28cbc78 commit 1f53fe1

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

src/decoder.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ jpeg.lossless.Decoder = jpeg.lossless.Decoder || function (buffer, numBytes) {
6767
this.yDim = 0;
6868
this.xLoc = 0;
6969
this.yLoc = 0;
70-
this.numBytes = 0;
71-
this.outputData = null;
70+
this.numBytes = 0;
71+
this.outputData = null;
72+
this.restarting = false;
7273

7374
if (typeof numBytes !== "undefined") {
7475
this.numBytes = numBytes;
@@ -84,7 +85,8 @@ jpeg.lossless.Decoder.TABLE = [0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26,
8485
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];
8586
jpeg.lossless.Decoder.MAX_HUFFMAN_SUBTREE = 50;
8687
jpeg.lossless.Decoder.MSB = 0x80000000;
87-
88+
jpeg.lossless.Decoder.RESTART_MARKER_BEGIN = 0xFFD0;
89+
jpeg.lossless.Decoder.RESTART_MARKER_END = 0xFFD7;
8890

8991
/*** Prototype Methods ***/
9092

@@ -302,6 +304,7 @@ jpeg.lossless.Decoder.prototype.decode = function (buffer, offset, length, numBy
302304
}
303305

304306
for (mcuNum = 0; mcuNum < this.restartInterval; mcuNum+=1) {
307+
this.restarting = (mcuNum == 0);
305308
current = this.decodeUnit(pred, temp, index);
306309
this.output(pred);
307310

@@ -319,7 +322,8 @@ jpeg.lossless.Decoder.prototype.decode = function (buffer, offset, length, numBy
319322
}
320323
}
321324

322-
if (!((current >= 0xFFD0) && (current <= 0xFFD7))) {
325+
if (!((current >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN) &&
326+
(current <= jpeg.lossless.Decoder.RESTART_MARKER_END))) {
323327
break; //current=MARKER
324328
}
325329
}
@@ -444,17 +448,29 @@ jpeg.lossless.Decoder.prototype.decodeRGB = function (prev, temp, index) {
444448
jpeg.lossless.Decoder.prototype.decodeSingle = function (prev, temp, index) {
445449
/*jslint bitwise: true */
446450

447-
var value, i;
451+
var value, i, n, nRestart;
448452

449-
prev[0] = this.selector();
453+
if (this.restarting) {
454+
this.restarting = false;
455+
prev[0] = (1 << (this.frame.precision - 1));
456+
} else {
457+
prev[0] = this.selector();
458+
}
450459

451460
for (i = 0; i < this.nBlock[0]; i+=1) {
452461
value = this.getHuffmanValue(this.dcTab[0], temp, index);
453462
if (value >= 0xFF00) {
454463
return value;
455464
}
456465

457-
prev[0] += this.getn(prev, value, temp, index);
466+
n = this.getn(prev, value, temp, index);
467+
nRestart = (n >> 8);
468+
469+
if ((nRestart >= jpeg.lossless.Decoder.RESTART_MARKER_BEGIN) && (nRestart <= jpeg.lossless.Decoder.RESTART_MARKER_END)) {
470+
return nRestart;
471+
}
472+
473+
prev[0] += n;
458474
}
459475

460476
return 0;

0 commit comments

Comments
 (0)