📄 tifffaxdecoder.java
字号:
232, 232, 232, 232, 232, 232, 232, 232,
// 1008 - 1015
232, 232, 232, 232, 232, 232, 232, 232,
// 1016 - 1023
232, 232, 232, 232, 232, 232, 232, 232,
};
// Additional make up codes for both White and Black runs
static short additionalMakeup[] = {
28679, 28679, 31752, (short)32777,
(short)33801, (short)34825, (short)35849, (short)36873,
(short)29703, (short)29703, (short)30727, (short)30727,
(short)37897, (short)38921, (short)39945, (short)40969
};
// Initial black run look up table, uses the first 4 bits of a code
static short initBlack[] = {
// 0 - 7
3226, 6412, 200, 168, 38, 38, 134, 134,
// 8 - 15
100, 100, 100, 100, 68, 68, 68, 68
};
//
static short twoBitBlack[] = {292, 260, 226, 226}; // 0 - 3
// Main black run table, using the last 9 bits of possible 13 bit code
static short black[] = {
// 0 - 7
62, 62, 30, 30, 0, 0, 0, 0,
// 8 - 15
0, 0, 0, 0, 0, 0, 0, 0,
// 16 - 23
0, 0, 0, 0, 0, 0, 0, 0,
// 24 - 31
0, 0, 0, 0, 0, 0, 0, 0,
// 32 - 39
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
// 40 - 47
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
// 48 - 55
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
// 56 - 63
3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
// 64 - 71
588, 588, 588, 588, 588, 588, 588, 588,
// 72 - 79
1680, 1680, 20499, 22547, 24595, 26643, 1776, 1776,
// 80 - 87
1808, 1808, -24557, -22509, -20461, -18413, 1904, 1904,
// 88 - 95
1936, 1936, -16365, -14317, 782, 782, 782, 782,
// 96 - 103
814, 814, 814, 814, -12269, -10221, 10257, 10257,
// 104 - 111
12305, 12305, 14353, 14353, 16403, 18451, 1712, 1712,
// 112 - 119
1744, 1744, 28691, 30739, -32749, -30701, -28653, -26605,
// 120 - 127
2061, 2061, 2061, 2061, 2061, 2061, 2061, 2061,
// 128 - 135
424, 424, 424, 424, 424, 424, 424, 424,
// 136 - 143
424, 424, 424, 424, 424, 424, 424, 424,
// 144 - 151
424, 424, 424, 424, 424, 424, 424, 424,
// 152 - 159
424, 424, 424, 424, 424, 424, 424, 424,
// 160 - 167
750, 750, 750, 750, 1616, 1616, 1648, 1648,
// 168 - 175
1424, 1424, 1456, 1456, 1488, 1488, 1520, 1520,
// 176 - 183
1840, 1840, 1872, 1872, 1968, 1968, 8209, 8209,
// 184 - 191
524, 524, 524, 524, 524, 524, 524, 524,
// 192 - 199
556, 556, 556, 556, 556, 556, 556, 556,
// 200 - 207
1552, 1552, 1584, 1584, 2000, 2000, 2032, 2032,
// 208 - 215
976, 976, 1008, 1008, 1040, 1040, 1072, 1072,
// 216 - 223
1296, 1296, 1328, 1328, 718, 718, 718, 718,
// 224 - 231
456, 456, 456, 456, 456, 456, 456, 456,
// 232 - 239
456, 456, 456, 456, 456, 456, 456, 456,
// 240 - 247
456, 456, 456, 456, 456, 456, 456, 456,
// 248 - 255
456, 456, 456, 456, 456, 456, 456, 456,
// 256 - 263
326, 326, 326, 326, 326, 326, 326, 326,
// 264 - 271
326, 326, 326, 326, 326, 326, 326, 326,
// 272 - 279
326, 326, 326, 326, 326, 326, 326, 326,
// 280 - 287
326, 326, 326, 326, 326, 326, 326, 326,
// 288 - 295
326, 326, 326, 326, 326, 326, 326, 326,
// 296 - 303
326, 326, 326, 326, 326, 326, 326, 326,
// 304 - 311
326, 326, 326, 326, 326, 326, 326, 326,
// 312 - 319
326, 326, 326, 326, 326, 326, 326, 326,
// 320 - 327
358, 358, 358, 358, 358, 358, 358, 358,
// 328 - 335
358, 358, 358, 358, 358, 358, 358, 358,
// 336 - 343
358, 358, 358, 358, 358, 358, 358, 358,
// 344 - 351
358, 358, 358, 358, 358, 358, 358, 358,
// 352 - 359
358, 358, 358, 358, 358, 358, 358, 358,
// 360 - 367
358, 358, 358, 358, 358, 358, 358, 358,
// 368 - 375
358, 358, 358, 358, 358, 358, 358, 358,
// 376 - 383
358, 358, 358, 358, 358, 358, 358, 358,
// 384 - 391
490, 490, 490, 490, 490, 490, 490, 490,
// 392 - 399
490, 490, 490, 490, 490, 490, 490, 490,
// 400 - 407
4113, 4113, 6161, 6161, 848, 848, 880, 880,
// 408 - 415
912, 912, 944, 944, 622, 622, 622, 622,
// 416 - 423
654, 654, 654, 654, 1104, 1104, 1136, 1136,
// 424 - 431
1168, 1168, 1200, 1200, 1232, 1232, 1264, 1264,
// 432 - 439
686, 686, 686, 686, 1360, 1360, 1392, 1392,
// 440 - 447
12, 12, 12, 12, 12, 12, 12, 12,
// 448 - 455
390, 390, 390, 390, 390, 390, 390, 390,
// 456 - 463
390, 390, 390, 390, 390, 390, 390, 390,
// 464 - 471
390, 390, 390, 390, 390, 390, 390, 390,
// 472 - 479
390, 390, 390, 390, 390, 390, 390, 390,
// 480 - 487
390, 390, 390, 390, 390, 390, 390, 390,
// 488 - 495
390, 390, 390, 390, 390, 390, 390, 390,
// 496 - 503
390, 390, 390, 390, 390, 390, 390, 390,
// 504 - 511
390, 390, 390, 390, 390, 390, 390, 390,
};
static byte twoDCodes[] = {
// 0 - 7
80, 88, 23, 71, 30, 30, 62, 62,
// 8 - 15
4, 4, 4, 4, 4, 4, 4, 4,
// 16 - 23
11, 11, 11, 11, 11, 11, 11, 11,
// 24 - 31
11, 11, 11, 11, 11, 11, 11, 11,
// 32 - 39
35, 35, 35, 35, 35, 35, 35, 35,
// 40 - 47
35, 35, 35, 35, 35, 35, 35, 35,
// 48 - 55
51, 51, 51, 51, 51, 51, 51, 51,
// 56 - 63
51, 51, 51, 51, 51, 51, 51, 51,
// 64 - 71
41, 41, 41, 41, 41, 41, 41, 41,
// 72 - 79
41, 41, 41, 41, 41, 41, 41, 41,
// 80 - 87
41, 41, 41, 41, 41, 41, 41, 41,
// 88 - 95
41, 41, 41, 41, 41, 41, 41, 41,
// 96 - 103
41, 41, 41, 41, 41, 41, 41, 41,
// 104 - 111
41, 41, 41, 41, 41, 41, 41, 41,
// 112 - 119
41, 41, 41, 41, 41, 41, 41, 41,
// 120 - 127
41, 41, 41, 41, 41, 41, 41, 41,
};
/**
* @param fillOrder The fill order of the compressed data bytes.
* @param w
* @param h
*/
public TIFFFaxDecoder(int fillOrder, int w, int h) {
this.fillOrder = fillOrder;
this.w = w;
this.h = h;
this.bitPointer = 0;
this.bytePointer = 0;
this.prevChangingElems = new int[w];
this.currChangingElems = new int[w];
}
// One-dimensional decoding methods
public void decode1D(byte[] buffer, byte[] compData,
int startX, int height) {
this.data = compData;
int lineOffset = 0;
int scanlineStride = (w + 7)/8;
bitPointer = 0;
bytePointer = 0;
for (int i = 0; i < height; i++) {
decodeNextScanline(buffer, lineOffset, startX);
lineOffset += scanlineStride;
}
}
public void decodeNextScanline(byte[] buffer,
int lineOffset, int bitOffset) {
int bits = 0, code = 0, isT = 0;
int current, entry, twoBits;
boolean isWhite = true;
// Initialize starting of the changing elements array
changingElemSize = 0;
// While scanline not complete
while (bitOffset < w) {
while (isWhite) {
// White run
current = nextNBits(10);
entry = white[current];
// Get the 3 fields from the entry
isT = entry & 0x0001;
bits = (entry >>> 1) & 0x0f;
if (bits == 12) { // Additional Make up code
// Get the next 2 bits
twoBits = nextLesserThan8Bits(2);
// Consolidate the 2 new bits and last 2 bits into 4 bits
current = ((current << 2) & 0x000c) | twoBits;
entry = additionalMakeup[current];
bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
code = (entry >>> 4) & 0x0fff; // 12 bits
bitOffset += code; // Skip white run
updatePointer(4 - bits);
} else if (bits == 0) { // ERROR
throw new RuntimeException("Invalid code encountered.");
} else if (bits == 15) { // EOL
throw new RuntimeException("EOL code word encountered in White run.");
} else {
// 11 bits - 0000 0111 1111 1111 = 0x07ff
code = (entry >>> 5) & 0x07ff;
bitOffset += code;
updatePointer(10 - bits);
if (isT == 0) {
isWhite = false;
currChangingElems[changingElemSize++] = bitOffset;
}
}
}
// Check whether this run completed one width, if so
// advance to next byte boundary for compression = 2.
if (bitOffset == w) {
if (compression == 2) {
advancePointer();
}
break;
}
while (!isWhite) {
// Black run
current = nextLesserThan8Bits(4);
entry = initBlack[current];
// Get the 3 fields from the entry
isT = entry & 0x0001;
bits = (entry >>> 1) & 0x000f;
code = (entry >>> 5) & 0x07ff;
if (code == 100) {
current = nextNBits(9);
entry = black[current];
// Get the 3 fields from the entry
isT = entry & 0x0001;
bits = (entry >>> 1) & 0x000f;
code = (entry >>> 5) & 0x07ff;
if (bits == 12) {
// Additional makeup codes
updatePointer(5);
current = nextLesserThan8Bits(4);
entry = additionalMakeup[current];
bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
code = (entry >>> 4) & 0x0fff; // 12 bits
setToBlack(buffer, lineOffset, bitOffset, code);
bitOffset += code;
updatePointer(4 - bits);
} else if (bits == 15) {
// EOL code
throw new RuntimeException("EOL code word encountered in Black run.");
} else {
setToBlack(buffer, lineOffset, bitOffset, code);
bitOffset += code;
updatePointer(9 - bits);
if (isT == 0) {
isWhite = true;
currChangingElems[changingElemSize++] = bitOffset;
}
}
} else if (code == 200) {
// Is a Terminating code
current = nextLesserThan8Bits(2);
entry = twoBitBlack[current];
code = (entry >>> 5) & 0x07ff;
bits = (entry >>> 1) & 0x0f;
setToBlack(buffer, lineOffset, bitOffset, code);
bitOffset += code;
updatePointer(2 - bits);
isWhite = true;
currChangingElems[changingElemSize++] = bitOffset;
} else {
// Is a Terminating code
setToBlack(buffer, lineOffset, bitOffset, code);
bitOffset += code;
updatePointer(4 - bits);
isWhite = true;
currChangingElems[changingElemSize++] = bitOffset;
}
}
// Check whether this run completed one width
if (bitOffset == w) {
if (compression == 2) {
advancePointer();
}
break;
}
}
currChangingElems[changingElemSize++] = bitOffset;
}
// Two-dimensional decoding methods
public void decode2D(byte[] buffer,
byte compData[],
int startX,
int height,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -