⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tifffaxdecoder.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    long tiffT4Options) {
        this.data = compData;
        compression = 3;
        
        bitPointer = 0;
        bytePointer = 0;
        
        int scanlineStride = (w + 7)/8;
        
        int a0, a1, b1, b2;
        int[] b = new int[2];
        int entry, code, bits;
        boolean isWhite;
        int currIndex = 0;
        int temp[];
        
        // fillBits - dealt with this in readEOL
        // 1D/2D encoding - dealt with this in readEOL
        
        // uncompressedMode - haven't dealt with this yet.
        
        
        oneD = (int)(tiffT4Options & 0x01);
        uncompressedMode = (int)((tiffT4Options & 0x02) >> 1);
        fillBits = (int)((tiffT4Options & 0x04) >> 2);
        
        // The data must start with an EOL code
        if (readEOL(true) != 1) {
            throw new RuntimeException("First scanline must be 1D encoded.");
        }
        
        int lineOffset = 0;
        int bitOffset;
        
        // Then the 1D encoded scanline data will occur, changing elements
        // array gets set.
        decodeNextScanline(buffer, lineOffset, startX);
        lineOffset += scanlineStride;
        
        for (int lines = 1; lines < height; lines++) {
            
            // Every line must begin with an EOL followed by a bit which
            // indicates whether the following scanline is 1D or 2D encoded.
            if (readEOL(false) == 0) {
                // 2D encoded scanline follows
                
                // Initialize previous scanlines changing elements, and
                // initialize current scanline's changing elements array
                temp = prevChangingElems;
                prevChangingElems = currChangingElems;
                currChangingElems = temp;
                currIndex = 0;
                
                // a0 has to be set just before the start of this scanline.
                a0 = -1;
                isWhite = true;
                bitOffset = startX;
                
                lastChangingElement = 0;
                
                while (bitOffset < w) {
                    // Get the next changing element
                    getNextChangingElement(a0, isWhite, b);
                    
                    b1 = b[0];
                    b2 = b[1];
                    
                    // Get the next seven bits
                    entry = nextLesserThan8Bits(7);
                    
                    // Run these through the 2DCodes table
                    entry = (int)(twoDCodes[entry] & 0xff);
                    
                    // Get the code and the number of bits used up
                    code = (entry & 0x78) >>> 3;
                    bits = entry & 0x07;
                    
                    if (code == 0) {
                        if (!isWhite) {
                            setToBlack(buffer, lineOffset, bitOffset,
                            b2 - bitOffset);
                        }
                        bitOffset = a0 = b2;
                        
                        // Set pointer to consume the correct number of bits.
                        updatePointer(7 - bits);
                    } else if (code == 1) {
                        // Horizontal
                        updatePointer(7 - bits);
                        
                        // identify the next 2 codes.
                        int number;
                        if (isWhite) {
                            number = decodeWhiteCodeWord();
                            bitOffset += number;
                            currChangingElems[currIndex++] = bitOffset;
                            
                            number = decodeBlackCodeWord();
                            setToBlack(buffer, lineOffset, bitOffset, number);
                            bitOffset += number;
                            currChangingElems[currIndex++] = bitOffset;
                        } else {
                            number = decodeBlackCodeWord();
                            setToBlack(buffer, lineOffset, bitOffset, number);
                            bitOffset += number;
                            currChangingElems[currIndex++] = bitOffset;
                            
                            number = decodeWhiteCodeWord();
                            bitOffset += number;
                            currChangingElems[currIndex++] = bitOffset;
                        }
                        
                        a0 = bitOffset;
                    } else if (code <= 8) {
                        // Vertical
                        a1 = b1 + (code - 5);
                        
                        currChangingElems[currIndex++] = a1;
                        
                        // We write the current color till a1 - 1 pos,
                        // since a1 is where the next color starts
                        if (!isWhite) {
                            setToBlack(buffer, lineOffset, bitOffset,
                            a1 - bitOffset);
                        }
                        bitOffset = a0 = a1;
                        isWhite = !isWhite;
                        
                        updatePointer(7 - bits);
                    } else {
                        throw new RuntimeException("Invalid code encountered while decoding 2D group 3 compressed data.");
                    }
                }
                
                // Add the changing element beyond the current scanline for the
                // other color too
                currChangingElems[currIndex++] = bitOffset;
                changingElemSize = currIndex;
            } else {
                // 1D encoded scanline follows
                decodeNextScanline(buffer, lineOffset, startX);
            }
            
            lineOffset += scanlineStride;
        }
    }
    
    public synchronized void decodeT6(byte[] buffer,
    byte[] compData,
    int startX,
    int height,
    long tiffT6Options) {
        this.data = compData;
        compression = 4;
        
        bitPointer = 0;
        bytePointer = 0;
        
        int scanlineStride = (w + 7)/8;
        
        int a0, a1, b1, b2;
        int entry, code, bits;
        boolean isWhite;
        int currIndex;
        int temp[];
        
        // Return values from getNextChangingElement
        int[] b = new int[2];
        
        // uncompressedMode - have written some code for this, but this
        // has not been tested due to lack of test images using this optional
        
        uncompressedMode = (int)((tiffT6Options & 0x02) >> 1);
        
        // Local cached reference
        int[] cce = currChangingElems;
        
        // Assume invisible preceding row of all white pixels and insert
        // both black and white changing elements beyond the end of this
        // imaginary scanline.
        changingElemSize = 0;
        cce[changingElemSize++] = w;
        cce[changingElemSize++] = w;
        
        int lineOffset = 0;
        int bitOffset;
        
        for (int lines = 0; lines < height; lines++) {
            // a0 has to be set just before the start of the scanline.
            a0 = -1;
            isWhite = true;
            
            // Assign the changing elements of the previous scanline to
            // prevChangingElems and start putting this new scanline's
            // changing elements into the currChangingElems.
            temp = prevChangingElems;
            prevChangingElems = currChangingElems;
            cce = currChangingElems = temp;
            currIndex = 0;
            
            // Start decoding the scanline at startX in the raster
            bitOffset = startX;
            
            // Reset search start position for getNextChangingElement
            lastChangingElement = 0;
            
            // Till one whole scanline is decoded
            while (bitOffset < w) {
                // Get the next changing element
                getNextChangingElement(a0, isWhite, b);
                b1 = b[0];
                b2 = b[1];
                
                // Get the next seven bits
                entry = nextLesserThan8Bits(7);
                // Run these through the 2DCodes table
                entry = (int)(twoDCodes[entry] & 0xff);
                
                // Get the code and the number of bits used up
                code = (entry & 0x78) >>> 3;
                bits = entry & 0x07;
                
                if (code == 0) { // Pass
                    // We always assume WhiteIsZero format for fax.
                    if (!isWhite) {
                        setToBlack(buffer, lineOffset, bitOffset,
                        b2 - bitOffset);
                    }
                    bitOffset = a0 = b2;
                    
                    // Set pointer to only consume the correct number of bits.
                    updatePointer(7 - bits);
                } else if (code == 1) { // Horizontal
                    // Set pointer to only consume the correct number of bits.
                    updatePointer(7 - bits);
                    
                    // identify the next 2 alternating color codes.
                    int number;
                    if (isWhite) {
                        // Following are white and black runs
                        number = decodeWhiteCodeWord();
                        bitOffset += number;
                        cce[currIndex++] = bitOffset;
                        
                        number = decodeBlackCodeWord();
                        setToBlack(buffer, lineOffset, bitOffset, number);
                        bitOffset += number;
                        cce[currIndex++] = bitOffset;
                    } else {
                        // First a black run and then a white run follows
                        number = decodeBlackCodeWord();
                        setToBlack(buffer, lineOffset, bitOffset, number);
                        bitOffset += number;
                        cce[currIndex++] = bitOffset;
                        
                        number = decodeWhiteCodeWord();
                        bitOffset += number;
                        cce[currIndex++] = bitOffset;
                    }
                    
                    a0 = bitOffset;
                } else if (code <= 8) { // Vertical
                    a1 = b1 + (code - 5);
                    cce[currIndex++] = a1;
                    
                    // We write the current color till a1 - 1 pos,
                    // since a1 is where the next color starts
                    if (!isWhite) {
                        setToBlack(buffer, lineOffset, bitOffset,
                        a1 - bitOffset);
                    }
                    bitOffset = a0 = a1;
                    isWhite = !isWhite;
                    
                    updatePointer(7 - bits);
                } else if (code == 11) {
                    if (nextLesserThan8Bits(3) != 7) {
                        throw new RuntimeException("Invalid code encountered while decoding 2D group 4 compressed data.");
                    }
                    
                    int zeros = 0;
                    boolean exit = false;
                    
                    while (!exit) {
                        while (nextLesserThan8Bits(1) != 1) {
                            zeros++;
                        }
                        
                        if (zeros > 5) {
                            // Exit code
                            
                            // Zeros before exit code
                            zeros = zeros - 6;
                            
                            if (!isWhite && (zeros > 0)) {
                                cce[currIndex++] = bitOffset;
                            }
                            
                            // Zeros before the exit code
                            bitOffset += zeros;
                            if (zeros > 0) {
                                // Some zeros have been written
                                isWhite = true;
                            }
                            
                            // Read in the bit which specifies the color of
                            // the following run
                            if (nextLesserThan8Bits(1) == 0) {
                                if (!isWhite) {
                                    cce[currIndex++] = bitOffset;
                                }
                                isWhite = true;
                            } else {
                                if (isWhite) {
                                    cce[currIndex++] = bitOffset;
                                }
                                isWhite = false;
                            }
                            
                            exit = true;
                        }
                        
                        if (zeros == 5) {
                            if (!isWhite) {
                                cce[currIndex++] = bitOffset;
                            }
                            bitOffset += zeros;
                            
                            // Last thing written was white
                            isWhite = true;
                        } else {
                            bitOffset += zeros;
                            
                            cce[currIndex++] = bitOffset;
                            setToBlack(buffer, lineOffset, bitOffset, 1);
                            ++bitOffset;
                            
                            // Last thing written was black
                            isWhite = false;
                        }
                        
                    }
                } else {
                    throw new RuntimeException("Invalid code encountered while decoding 2D group 4 compressed data.");
                }
            }
            
            // Add the changing element beyond the current scanline for the
            // other color too
            //make sure that the index does not exceed the bounds of the array
            if(currIndex < cce.length) 
            cce[currIndex++] = bitOffset;
            
            // Number of changing elements in this scanline.
            changingElemSize = currIndex;
            
            lineOffset += scanlineStride;
        }
    }
    
    private void setToBlack(byte[] buffer,
    int lineOffset, int bitOffset,
    int numBits) {
        int bitNum = 8*lineOffset + bitOffset;
        int lastBit = bitNum + numBits;
        
        int byteNum = bitNum >> 3;
        
        // Handle bits in first byte
        int shift = bitNum & 0x7;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -