Skip to content

Commit

Permalink
jpeg decoder cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed May 1, 2015
1 parent 7624339 commit 41c211b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
10 changes: 0 additions & 10 deletions lib/src/formats/jpeg/jpeg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ part of image;

class Jpeg {
static const List<int> dctZigZag = const [
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 ];

static const List<int> dctNaturalOrder = const [
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/formats/jpeg/jpeg_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of image;
class JpegComponent {
int h;
int v;
final List<Int32List> quantizationTableList;
final List<Int16List> quantizationTableList;
int quantizationIndex;
int blocksPerLine;
int blocksPerColumn;
Expand All @@ -15,6 +15,6 @@ class JpegComponent {
JpegComponent(this.h, this.v, this.quantizationTableList,
this.quantizationIndex);

Int32List get quantizationTable =>
Int16List get quantizationTable =>
quantizationTableList[quantizationIndex];
}
18 changes: 14 additions & 4 deletions lib/src/formats/jpeg/jpeg_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ class JpegData {
}

if (quantizationTables[n] == null) {
quantizationTables[n] = new Int32List(64);;
quantizationTables[n] = new Int16List(64);
}

Int32List tableData = quantizationTables[n];
Int16List tableData = quantizationTables[n];
for (int i = 0; i < Jpeg.DCTSIZE2; i++) {
int tmp;
if (prec != 0) {
Expand All @@ -454,7 +454,7 @@ class JpegData {
tmp = block.readByte();
}

tableData[Jpeg.dctNaturalOrder[i]] = tmp;
tableData[Jpeg.dctZigZag[i]] = tmp;
}
}

Expand Down Expand Up @@ -661,14 +661,24 @@ class JpegData {
return lines;
}

static int toFix(double val) {
const int FIXED_POINT = 20;
const int ONE = 1 << FIXED_POINT;

return (val * ONE).toInt() & 0xffffffff;
}

static Uint8List dctClip;
/**
* 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.
*
* TODO I'm pretty sure the rounding errors in some pixels are coming from
* here.
*/
void _quantizeAndInverse(Int32List quantizationTable,
void _quantizeAndInverse(Int16List quantizationTable,
Int32List coefBlock,
Uint8List dataOut,
Int32List dataIn) {
Expand Down
16 changes: 11 additions & 5 deletions lib/src/formats/jpeg/jpeg_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,26 @@ class JpegScan {
int diff = t == 0 ? 0 : _receiveAndExtend(t);
component.pred += diff;
zz[0] = component.pred;

int k = 1;
while (k < 64) {
var rs = _decodeHuffman(component.huffmanTableAC);
int s = rs & 15;
int r = rs >> 4;
if (s == 0) {
if (r < 15)
if (r < 15) {
break;
}
k += 16;
continue;
}

k += r;
int z = Jpeg.dctNaturalOrder[k];
zz[z] = _receiveAndExtend(s);

s = _receiveAndExtend(s);

int z = Jpeg.dctZigZag[k];
zz[z] = s;
k++;
}
}
Expand Down Expand Up @@ -215,7 +221,7 @@ class JpegScan {
continue;
}
k += r;
int z = Jpeg.dctNaturalOrder[k];
int z = Jpeg.dctZigZag[k];
zz[z] = (_receiveAndExtend(s) * (1 << successive));
k++;
}
Expand All @@ -226,7 +232,7 @@ class JpegScan {
int e = spectralEnd;
int r = 0;
while (k <= e) {
int z = Jpeg.dctNaturalOrder[k];
int z = Jpeg.dctZigZag[k];
switch (successiveACState) {
case 0: // initial state
int rs = _decodeHuffman(component.huffmanTableAC);
Expand Down
1 change: 1 addition & 0 deletions test/jpeg_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void defineJpegTests() {
expect(image.width, equals(227));
expect(image.height, equals(149));

// This test identifies the rounding error in some pixels.
/*for (int y = 0; y < image.height; ++y) {
for (int x = 0; x < image.width; ++x) {
expect(image.getPixel(x, y), equals(png.getPixel(x, y)),
Expand Down

0 comments on commit 41c211b

Please sign in to comment.