?? ccittg4encoder.java
字號:
bitOffset = (next-base)*8 + byteTable[testbyte]; return ((bitOffset < maxOffset) ? bitOffset : maxOffset); } } } bitOffset = (next-base)*8 + byteTable[testbyte]; return ((bitOffset < maxOffset) ? bitOffset : maxOffset); } /** * Initialize bit buffer machinery. */ private void initBitBuf() { ndex = 0; bits = 0x00000000; } /** * Get code for run and add to compressed bitstream. */ private int add1DBits(byte[] buf, int where, // byte offs int count, // #pixels in run int color) // color of run { int sixtyfours; int mask; int len = where; sixtyfours = count >>> 6; // count / 64; count = count & 0x3f; // count % 64 if (sixtyfours != 0) { for ( ; sixtyfours > 40; sixtyfours -= 40) { mask = makeupCodes[color][40]; bits |= (mask & 0xfff80000) >>> ndex; ndex += (int)(mask & 0x0000ffff); while (ndex > 7) { buf[len++] = (byte)(bits >>> 24); bits <<= 8; ndex -= 8; } } mask = makeupCodes[color][sixtyfours]; bits |= (mask & 0xfff80000) >>> ndex; ndex += (int)(mask & 0x0000ffff); while (ndex > 7) { buf[len++] = (byte)(bits >>> 24); bits <<= 8; ndex -= 8; } } mask = termCodes[color][count]; bits |= (mask & 0xfff80000) >>> ndex; ndex += (int)(mask & 0x0000ffff); while (ndex > 7) { buf[len++] = (byte)(bits >>> 24); bits <<= 8; ndex -= 8; } return(len - where); } /** * Place entry from mode table into compressed bitstream. */ private int add2DBits(byte[] buf, // compressed buffer int where, // byte offset into compressed buffer int[][] mode, // 2-D mode to be encoded int entry) // mode entry (0 unless vertical) { int mask; int len = where; int color = 0; mask = mode[color][entry]; bits |= (mask & 0xfff80000) >>> ndex; ndex += (int)(mask & 0x0000ffff); while (ndex > 7) { buf[len++] = (byte)(bits >>> 24); bits <<= 8; ndex -= 8; } return(len - where); } /** * Add an End-of-Line (EOL == 0x001) to the compressed bitstream * with optional byte alignment. */ private int addEOL(boolean is1DMode,// 1D encoding boolean addFill, // byte aligned EOLs boolean add1, // add1 ? EOL+1 : EOL+0 byte[] buf, // compressed buffer address int where) // current byte offset into buffer { int len = where; // // Add zero-valued fill bits such that the EOL is aligned as // // xxxx 0000 0000 0001 // if(addFill) { // // Simply increment the bit count. No need to feed bits into // the output buffer at this point as there are at most 7 bits // in the bit buffer, at most 7 are added here, and at most // 13 below making the total 7+7+13 = 27 before the bit feed // at the end of this routine. // ndex += ((ndex <= 4) ? 4 - ndex : 12 - ndex); } // // Write EOL into buffer // if(is1DMode) { bits |= 0x00100000 >>> ndex; ndex += 12; } else { bits |= (add1 ? 0x00180000 : 0x00100000) >>> ndex; ndex += 13; } while (ndex > 7) { buf[len++] = (byte)(bits >>> 24); bits <<= 8; ndex -= 8; } return(len - where); } /** * Add an End-of-Facsimile-Block (EOFB == 0x001001) to the compressed * bitstream. */ private int addEOFB(byte[] buf, // compressed buffer int where) // byte offset into compressed buffer { int len = where; // // eofb code // bits |= 0x00100100 >>> ndex; // // eofb code length // ndex += 24; // // flush all pending bits // while(ndex > 0) { buf[len++] = (byte)(bits >>> 24); bits <<= 8; ndex -= 8; } return(len - where); } /** * Encode a buffer of data using CCITT T.6 Compression also known as * Group 4 facsimile compression. * * @param data The row of data to compress. * @param width Number of bits in the row. * @param height Number of rows in the buffer. * * @return The number of bytes saved in the compressed data array. */ public byte[] encodeT6(byte[] data, int width, int height) { int bufSize = (int)Math.ceil((((width + 1)/2)*9 + 2)/8.0); bufSize = height*(bufSize + 2) + 12; byte[] compData = new byte[bufSize]; int lineStride = (width+7)/8; int colOffset = 0; // // ao, a1, a2 are bit indices in the current line // b1 and b2 are bit indices in the reference line (line above) // color is the current color (WHITE or BLACK) // byte[] refData = null; int refAddr = 0; int lineAddr = 0; int outIndex = 0; initBitBuf(); // // Iterate over all lines // while(height-- != 0) { int a0 = colOffset; int last = a0 + width; int testbit = ((data[lineAddr + (a0>>>3)]&0xff) >>> (7-(a0 & 0x7))) & 0x1; int a1 = testbit != 0 ? a0 : nextState(data, lineAddr, a0, last); testbit = refData == null ? 0: ((refData[refAddr + (a0>>>3)]&0xff) >>> (7-(a0 & 0x7))) & 0x1; int b1 = testbit != 0 ? a0 : nextState(refData, refAddr, a0, last); // // The current color is set to WHITE at line start // int color = WHITE; while(true) { int b2 = nextState(refData, refAddr, b1, last); if(b2 < a1) { // pass mode outIndex += add2DBits(compData, outIndex, pass, 0); a0 = b2; } else { int tmp = b1 - a1 + 3; if((tmp <= 6) && (tmp >= 0)) { // vertical mode outIndex += add2DBits(compData, outIndex, vert, tmp); a0 = a1; } else { // horizontal mode int a2 = nextState(data, lineAddr, a1, last); outIndex += add2DBits(compData, outIndex, horz, 0); outIndex += add1DBits(compData, outIndex, a1-a0, color); outIndex += add1DBits(compData, outIndex, a2-a1, color^1); a0 = a2; } } if(a0 >= last) { break; } color = ((data[lineAddr + (a0>>>3)]&0xff) >>> (7-(a0 & 0x7))) & 0x1; a1 = nextState(data, lineAddr, a0, last); b1 = nextState(refData, refAddr, a0, last); testbit = refData == null ? 0: ((refData[refAddr + (b1>>>3)]&0xff) >>> (7-(b1 & 0x7))) & 0x1; if(testbit == color) { b1 = nextState(refData, refAddr, b1, last); } } refData = data; refAddr = lineAddr; lineAddr += lineStride; } // End while(height--) // // append eofb // outIndex += addEOFB(compData, outIndex); byte out[] = new byte[outIndex]; System.arraycopy(compData, 0, out, 0, outIndex); return out; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -