Open Source Repository

Home /itextpdf/itextpdf-5.1.2 | Repository Home



com/itextpdf/text/pdf/codec/CCITTG4Encoder.java
/*
 * $Id: CCITTG4Encoder.java 4784 2011-03-15 08:33:00Z blowagie $
 *
 * This file is part of the iText (R) project.
 * Copyright (c) 1998-2011 1T3XT BVBA
 * Authors: Bruno Lowagie, Paulo Soares, et al.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation with the addition of the
 * following permission added to Section 15 as permitted in Section 7(a):
 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
 * http://itextpdf.com/terms-of-use/
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License,
 * a covered work must retain the producer line in every PDF that is created
 * or manipulated using iText.
 *
 * You can be released from the requirements of the license by purchasing
 * a commercial license. Buying such a license is mandatory as soon as you
 * develop commercial activities involving the iText software without
 * disclosing the source code of your own applications.
 * These activities include: offering paid services to customers as an ASP,
 * serving PDFs on the fly in a web application, shipping iText with a closed
 * source product.
 *
 * For more information, please contact iText Software Corp. at this
 * address: [email protected]
 */
package com.itextpdf.text.pdf.codec;

import com.itextpdf.text.pdf.ByteBuffer;

/**
 * Encodes data in the CCITT G4 FAX format.
 */
public class CCITTG4Encoder {
    private int rowbytes;
    private int rowpixels;
    private int bit = 8;
    private int data;
    private byte[] refline;
    private ByteBuffer outBuf = new ByteBuffer(1024);
    private byte[] dataBp;
    private int offsetData;
    private int sizeData;
    
    /**
     * Creates a new encoder.
     @param width the line width
     */    
    public CCITTG4Encoder(int width) {
        rowpixels = width;
        rowbytes = (rowpixels + 78;
        refline = new byte[rowbytes];
    }
    
    /**
     * Encodes a number of lines.
     @param data the data to be encoded
     @param offset the offset into the data
     @param size the size of the data to be encoded
     */    
    public void fax4Encode(byte[] data, int offset, int size) {
        dataBp = data;
        offsetData = offset;
        sizeData = size;
        while (sizeData > 0) {
            Fax3Encode2DRow();
            System.arraycopy(dataBp, offsetData, refline, 0, rowbytes);
            offsetData += rowbytes;
            sizeData -= rowbytes;
        }
    }
    

    /**
     * Encodes a full image.
     @param data the data to encode
     @param width the image width
     @param height the image height
     @return the encoded image
     */    
    public static byte[] compress(byte[] data, int width, int height) {
        CCITTG4Encoder g4 = new CCITTG4Encoder(width);
        g4.fax4Encode(data, 0, g4.rowbytes * height);
        return g4.close();
    }
    
    /**
     * Encodes a number of lines.
     @param data the data to be encoded
     @param height the number of lines to encode
     */    
    public void fax4Encode(byte[] data, int height) {
        fax4Encode(data, 0, rowbytes * height);
    }

    private void putcode(int[] table) {
        putBits(table[CODE], table[LENGTH]);
    }
    
    private void putspan(int span, int[][] tab) {
        int code, length;
        
        while (span >= 2624) {
            int[] te = tab[63 (2560>>6)];
            code = te[CODE];
            length = te[LENGTH];
            putBits(code, length);
            span -= te[RUNLEN];
        }
        if (span >= 64) {
            int[] te = tab[63 (span>>6)];
            code = te[CODE];
            length = te[LENGTH];
            putBits(code, length);
            span -= te[RUNLEN];
        }
        code = tab[span][CODE];
        length = tab[span][LENGTH];
        putBits(code, length);
    }
    
    private void putBits(int bits, int length) {
        while (length > bit) {
            data |= bits >> (length - bit);
            length -= bit;
            outBuf.append((byte)data);
            data = 0;
            bit = 8;
        }
        data |= (bits & msbmask[length]) << (bit - length);
        bit -= length;
        if (bit == 0) {
            outBuf.append((byte)data);
            data = 0;
            bit = 8;
        }
    }
    
    private void Fax3Encode2DRow() {
        int a0 = 0;
        int a1 = (pixel(dataBp, offsetData, 0!= : finddiff(dataBp, offsetData, 0, rowpixels, 0));
        int b1 = (pixel(refline, 00!= : finddiff(refline, 00, rowpixels, 0));
        int a2, b2;
        
        for (;;) {
            b2 = finddiff2(refline, 0, b1, rowpixels, pixel(refline, 0,b1));
            if (b2 >= a1) {
                int d = b1 - a1;
                if (!(-<= d && d <= 3)) {  /* horizontal mode */
                    a2 = finddiff2(dataBp, offsetData, a1, rowpixels, pixel(dataBp, offsetData,a1));
                    putcode(horizcode);
                    if (a0+a1 == || pixel(dataBp, offsetData, a0== 0) {
                        putspan(a1-a0, TIFFFaxWhiteCodes);
                        putspan(a2-a1, TIFFFaxBlackCodes);
                    else {
                        putspan(a1-a0, TIFFFaxBlackCodes);
                        putspan(a2-a1, TIFFFaxWhiteCodes);
                    }
                    a0 = a2;
                else {      /* vertical mode */
                    putcode(vcodes[d+3]);
                    a0 = a1;
                }
            else {        /* pass mode */
                putcode(passcode);
                a0 = b2;
            }
            if (a0 >= rowpixels)
                break;
            a1 = finddiff(dataBp, offsetData, a0, rowpixels, pixel(dataBp, offsetData,a0));
            b1 = finddiff(refline, 0, a0, rowpixels, pixel(dataBp, offsetData,a01);
            b1 = finddiff(refline, 0, b1, rowpixels, pixel(dataBp, offsetData,a0));
        }
    }
    
    private void Fax4PostEncode() {
        putBits(EOL, 12);
        putBits(EOL, 12);
        if (bit != 8) {
            outBuf.append((byte)data);
            data = 0;
            bit = 8;
        }
    }
    
    /**
     * Closes the encoder and returns the encoded data.
     @return the encoded data
     */    
    public byte[] close() {
        Fax4PostEncode();
        return outBuf.toByteArray();
    }
    
    private int pixel(byte[] data, int offset, int bit) {
        if (bit >= rowpixels)
            return 0;
        return ((data[offset + (bit >> 3)] 0xff>> (7-((bit)&7))) 1;
    }
    
    private static int find1span(byte[] bp, int offset, int bs, int be) {
        int bits = be - bs;
        int n, span;
        
        int pos = offset + (bs >> 3);
        /*
         * Check partial byte on lhs.
         */
        if (bits > && (n = (bs & 7)) != 0) {
            span = oneruns[(bp[pos<< n0xff];
            if (span > 8-n)    /* table value too generous */
                span = 8-n;
            if (span > bits)  /* constrain span to bit range */
                span = bits;
            if (n+span < 8)    /* doesn't extend to edge of byte */
                return span;
            bits -= span;
            pos++;
        else
            span = 0;
        /*
         * Scan full bytes for all 1's.
         */
        while (bits >= 8) {
            if (bp[pos!= -1)  /* end of run */
                return (span + oneruns[bp[pos0xff]);
            span += 8;
            bits -= 8;
            pos++;
        }
        /*
         * Check partial byte on rhs.
         */
        if (bits > 0) {
            n = oneruns[bp[pos0xff];
            span += (n > bits ? bits : n);
        }
        return span;
    }
    
    private static int find0span(byte[] bp, int offset, int bs, int be) {
        int bits = be - bs;
        int n, span;
        
        int pos = offset + (bs >> 3);
        /*
         * Check partial byte on lhs.
         */
        if (bits > && (n = (bs & 7)) != 0) {
            span = zeroruns[(bp[pos<< n0xff];
            if (span > 8-n)    /* table value too generous */
                span = 8-n;
            if (span > bits)  /* constrain span to bit range */
                span = bits;
            if (n+span < 8)    /* doesn't extend to edge of byte */
                return span;
            bits -= span;
            pos++;
        else
            span = 0;
        /*
         * Scan full bytes for all 1's.
         */
        while (bits >= 8) {
            if (bp[pos!= 0)  /* end of run */
                return (span + zeroruns[bp[pos0xff]);
            span += 8;
            bits -= 8;
            pos++;
        }
        /*
         * Check partial byte on rhs.
         */
        if (bits > 0) {
            n = zeroruns[bp[pos0xff];
            span += (n > bits ? bits : n);
        }
        return span;
    }
    
    private static int finddiff(byte[] bp, int offset, int bs, int be, int color) {
        return bs + (color != ? find1span(bp, offset, bs, be: find0span(bp, offset, bs, be));
    }
    
    private static int finddiff2(byte[] bp, int offset, int bs, int be, int color) {
        return bs < be ? finddiff(bp, offset, bs, be, color: be;
    }
    
    private static byte zeroruns[] {
        8766555544444444,  /* 0x00 - 0x0f */
        3333333333333333,  /* 0x10 - 0x1f */
        2222222222222222,  /* 0x20 - 0x2f */
        2222222222222222,  /* 0x30 - 0x3f */
        1111111111111111,  /* 0x40 - 0x4f */
        1111111111111111,  /* 0x50 - 0x5f */
        1111111111111111,  /* 0x60 - 0x6f */
        1111111111111111,  /* 0x70 - 0x7f */
        0000000000000000,  /* 0x80 - 0x8f */
        0000000000000000,  /* 0x90 - 0x9f */
        0000000000000000,  /* 0xa0 - 0xaf */
        0000000000000000,  /* 0xb0 - 0xbf */
        0000000000000000,  /* 0xc0 - 0xcf */
        0000000000000000,  /* 0xd0 - 0xdf */
        0000000000000000,  /* 0xe0 - 0xef */
        0000000000000000  /* 0xf0 - 0xff */
    };
    
    private static byte oneruns[] {
        0000000000000000,  /* 0x00 - 0x0f */
        0000000000000000,  /* 0x10 - 0x1f */
        0000000000000000,  /* 0x20 - 0x2f */
        0000000000000000,  /* 0x30 - 0x3f */
        0000000000000000,  /* 0x40 - 0x4f */
        0000000000000000,  /* 0x50 - 0x5f */
        0000000000000000,  /* 0x60 - 0x6f */
        0000000000000000,  /* 0x70 - 0x7f */
        1111111111111111,  /* 0x80 - 0x8f */
        1111111111111111,  /* 0x90 - 0x9f */
        1111111111111111,  /* 0xa0 - 0xaf */
        1111111111111111,  /* 0xb0 - 0xbf */
        2222222222222222,  /* 0xc0 - 0xcf */
        2222222222222222,  /* 0xd0 - 0xdf */
        3333333333333333,  /* 0xe0 - 0xef */
        4444444455556678  /* 0xf0 - 0xff */
    };

    private static final int LENGTH = 0/* bit length of g3 code */
    private static final int CODE = 1;   /* g3 code */
    private static final int RUNLEN = 2/* run length in bits */

    private static final int EOL = 0x001/* EOL code value - 0000 0000 0000 1 */

    /* status values returned instead of a run length */
    private static final int G3CODE_EOL  = -1;     /* NB: ACT_EOL - ACT_WRUNT */
    private static final int G3CODE_INVALID = -2/* NB: ACT_INVALID - ACT_WRUNT */
    private static final int G3CODE_EOF = -3;     /* end of input data */
    private static final int G3CODE_INCOMP = -4;  /* incomplete run code */

    private int[][] TIFFFaxWhiteCodes = {
        80x35},  /* 0011 0101 */
        60x7},  /* 0001 11 */
        40x7},  /* 0111 */
        40x8},  /* 1000 */
        40xB},  /* 1011 */
        40xC},  /* 1100 */
        40xE},  /* 1110 */
        40xF},  /* 1111 */
        50x13},  /* 1001 1 */
        50x14},  /* 1010 0 */
        50x710 },  /* 0011 1 */
        50x811 },  /* 0100 0 */
        60x812 },  /* 0010 00 */
        60x313 },  /* 0000 11 */
        60x3414 },  /* 1101 00 */
        60x3515 },  /* 1101 01 */
        60x2A16 },  /* 1010 10 */
        60x2B17 },  /* 1010 11 */
        70x2718 },  /* 0100 111 */
        70xC19 },  /* 0001 100 */
        70x820 },  /* 0001 000 */
        70x1721 },  /* 0010 111 */
        70x322 },  /* 0000 011 */
        70x423 },  /* 0000 100 */
        70x2824 },  /* 0101 000 */
        70x2B25 },  /* 0101 011 */
        70x1326 },  /* 0010 011 */
        70x2427 },  /* 0100 100 */
        70x1828 },  /* 0011 000 */
        80x229 },  /* 0000 0010 */
        80x330 },  /* 0000 0011 */
        80x1A31 },  /* 0001 1010 */
        80x1B32 },  /* 0001 1011 */
        80x1233 },  /* 0001 0010 */
        80x1334 },  /* 0001 0011 */
        80x1435 },  /* 0001 0100 */
        80x1536 },  /* 0001 0101 */
        80x1637 },  /* 0001 0110 */
        80x1738 },  /* 0001 0111 */
        80x2839 },  /* 0010 1000 */
        80x2940 },  /* 0010 1001 */
        80x2A41 },  /* 0010 1010 */
        80x2B42 },  /* 0010 1011 */
        80x2C43 },  /* 0010 1100 */
        80x2D44 },  /* 0010 1101 */
        80x445 },  /* 0000 0100 */
        80x546 },  /* 0000 0101 */
        80xA47 },  /* 0000 1010 */
        80xB48 },  /* 0000 1011 */
        80x5249 },  /* 0101 0010 */
        80x5350 },  /* 0101 0011 */
        80x5451 },  /* 0101 0100 */
        80x5552 },  /* 0101 0101 */
        80x2453 },  /* 0010 0100 */
        80x2554 },  /* 0010 0101 */
        80x5855 },  /* 0101 1000 */
        80x5956 },  /* 0101 1001 */
        80x5A57 },  /* 0101 1010 */
        80x5B58 },  /* 0101 1011 */
        80x4A59 },  /* 0100 1010 */
        80x4B60 },  /* 0100 1011 */
        80x3261 },  /* 0011 0010 */
        80x3362 },  /* 0011 0011 */
        80x3463 },  /* 0011 0100 */
        50x1B64 },  /* 1101 1 */
        50x12128 },  /* 1001 0 */
        60x17192 },  /* 0101 11 */
        70x37256 },  /* 0110 111 */
        80x36320 },  /* 0011 0110 */
        80x37384 },  /* 0011 0111 */
        80x64448 },  /* 0110 0100 */
        80x65512 },  /* 0110 0101 */
        80x68576 },  /* 0110 1000 */
        80x67640 },  /* 0110 0111 */
        90xCC704 },  /* 0110 0110 0 */
        90xCD768 },  /* 0110 0110 1 */
        90xD2832 },  /* 0110 1001 0 */
        90xD3896 },  /* 0110 1001 1 */
        90xD4960 },  /* 0110 1010 0 */
        90xD51024 },  /* 0110 1010 1 */
        90xD61088 },  /* 0110 1011 0 */
        90xD71152 },  /* 0110 1011 1 */
        90xD81216 },  /* 0110 1100 0 */
        90xD91280 },  /* 0110 1100 1 */
        90xDA1344 },  /* 0110 1101 0 */
        90xDB1408 },  /* 0110 1101 1 */
        90x981472 },  /* 0100 1100 0 */
        90x991536 },  /* 0100 1100 1 */
        90x9A1600 },  /* 0100 1101 0 */
        60x181664 },  /* 0110 00 */
        90x9B1728 },  /* 0100 1101 1 */
        110x81792 },  /* 0000 0001 000 */
        110xC1856 },  /* 0000 0001 100 */
        110xD1920 },  /* 0000 0001 101 */
        120x121984 },  /* 0000 0001 0010 */
        120x132048 },  /* 0000 0001 0011 */
        120x142112 },  /* 0000 0001 0100 */
        120x152176 },  /* 0000 0001 0101 */
        120x162240 },  /* 0000 0001 0110 */
        120x172304 },  /* 0000 0001 0111 */
        120x1C2368 },  /* 0000 0001 1100 */
        120x1D2432 },  /* 0000 0001 1101 */
        120x1E2496 },  /* 0000 0001 1110 */
        120x1F2560 },  /* 0000 0001 1111 */
        120x1, G3CODE_EOL },  /* 0000 0000 0001 */
        90x1, G3CODE_INVALID },  /* 0000 0000 1 */
        100x1, G3CODE_INVALID },  /* 0000 0000 01 */
        110x1, G3CODE_INVALID },  /* 0000 0000 001 */
        120x0, G3CODE_INVALID }  /* 0000 0000 0000 */
    };

    private int[][] TIFFFaxBlackCodes = {
        100x37},  /* 0000 1101 11 */
        30x2},  /* 010 */
        20x3},  /* 11 */
        20x2},  /* 10 */
        30x3},  /* 011 */
        40x3},  /* 0011 */
        40x2},  /* 0010 */
        50x3},  /* 0001 1 */
        60x5},  /* 0001 01 */
        60x4},  /* 0001 00 */
        70x410 },  /* 0000 100 */
        70x511 },  /* 0000 101 */
        70x712 },  /* 0000 111 */
        80x413 },  /* 0000 0100 */
        80x714 },  /* 0000 0111 */
        90x1815 },  /* 0000 1100 0 */
        100x1716 },  /* 0000 0101 11 */
        100x1817 },  /* 0000 0110 00 */
        100x818 },  /* 0000 0010 00 */
        110x6719 },  /* 0000 1100 111 */
        110x6820 },  /* 0000 1101 000 */
        110x6C21 },  /* 0000 1101 100 */
        110x3722 },  /* 0000 0110 111 */
        110x2823 },  /* 0000 0101 000 */
        110x1724 },  /* 0000 0010 111 */
        110x1825 },  /* 0000 0011 000 */
        120xCA26 },  /* 0000 1100 1010 */
        120xCB27 },  /* 0000 1100 1011 */
        120xCC28 },  /* 0000 1100 1100 */
        120xCD29 },  /* 0000 1100 1101 */
        120x6830 },  /* 0000 0110 1000 */
        120x6931 },  /* 0000 0110 1001 */
        120x6A32 },  /* 0000 0110 1010 */
        120x6B33 },  /* 0000 0110 1011 */
        120xD234 },  /* 0000 1101 0010 */
        120xD335 },  /* 0000 1101 0011 */
        120xD436 },  /* 0000 1101 0100 */
        120xD537 },  /* 0000 1101 0101 */
        120xD638 },  /* 0000 1101 0110 */
        120xD739 },  /* 0000 1101 0111 */
        120x6C40 },  /* 0000 0110 1100 */
        120x6D41 },  /* 0000 0110 1101 */
        120xDA42 },  /* 0000 1101 1010 */
        120xDB43 },  /* 0000 1101 1011 */
        120x5444 },  /* 0000 0101 0100 */
        120x5545 },  /* 0000 0101 0101 */
        120x5646 },  /* 0000 0101 0110 */
        120x5747 },  /* 0000 0101 0111 */
        120x6448 },  /* 0000 0110 0100 */
        120x6549 },  /* 0000 0110 0101 */
        120x5250 },  /* 0000 0101 0010 */
        120x5351 },  /* 0000 0101 0011 */
        120x2452 },  /* 0000 0010 0100 */
        120x3753 },  /* 0000 0011 0111 */
        120x3854 },  /* 0000 0011 1000 */
        120x2755 },  /* 0000 0010 0111 */
        120x2856 },  /* 0000 0010 1000 */
        120x5857 },  /* 0000 0101 1000 */
        120x5958 },  /* 0000 0101 1001 */
        120x2B59 },  /* 0000 0010 1011 */
        120x2C60 },  /* 0000 0010 1100 */
        120x5A61 },  /* 0000 0101 1010 */
        120x6662 },  /* 0000 0110 0110 */
        120x6763 },  /* 0000 0110 0111 */
        100xF64 },  /* 0000 0011 11 */
        120xC8128 },  /* 0000 1100 1000 */
        120xC9192 },  /* 0000 1100 1001 */
        120x5B256 },  /* 0000 0101 1011 */
        120x33320 },  /* 0000 0011 0011 */
        120x34384 },  /* 0000 0011 0100 */
        120x35448 },  /* 0000 0011 0101 */
        130x6C512 },  /* 0000 0011 0110 0 */
        130x6D576 },  /* 0000 0011 0110 1 */
        130x4A640 },  /* 0000 0010 0101 0 */
        130x4B704 },  /* 0000 0010 0101 1 */
        130x4C768 },  /* 0000 0010 0110 0 */
        130x4D832 },  /* 0000 0010 0110 1 */
        130x72896 },  /* 0000 0011 1001 0 */
        130x73960 },  /* 0000 0011 1001 1 */
        130x741024 },  /* 0000 0011 1010 0 */
        130x751088 },  /* 0000 0011 1010 1 */
        130x761152 },  /* 0000 0011 1011 0 */
        130x771216 },  /* 0000 0011 1011 1 */
        130x521280 },  /* 0000 0010 1001 0 */
        130x531344 },  /* 0000 0010 1001 1 */
        130x541408 },  /* 0000 0010 1010 0 */
        130x551472 },  /* 0000 0010 1010 1 */
        130x5A1536 },  /* 0000 0010 1101 0 */
        130x5B1600 },  /* 0000 0010 1101 1 */
        130x641664 },  /* 0000 0011 0010 0 */
        130x651728 },  /* 0000 0011 0010 1 */
        110x81792 },  /* 0000 0001 000 */
        110xC1856 },  /* 0000 0001 100 */
        110xD1920 },  /* 0000 0001 101 */
        120x121984 },  /* 0000 0001 0010 */
        120x132048 },  /* 0000 0001 0011 */
        120x142112 },  /* 0000 0001 0100 */
        120x152176 },  /* 0000 0001 0101 */
        120x162240 },  /* 0000 0001 0110 */
        120x172304 },  /* 0000 0001 0111 */
        120x1C2368 },  /* 0000 0001 1100 */
        120x1D2432 },  /* 0000 0001 1101 */
        120x1E2496 },  /* 0000 0001 1110 */
        120x1F2560 },  /* 0000 0001 1111 */
        120x1, G3CODE_EOL },  /* 0000 0000 0001 */
        90x1, G3CODE_INVALID },  /* 0000 0000 1 */
        100x1, G3CODE_INVALID },  /* 0000 0000 01 */
        110x1, G3CODE_INVALID },  /* 0000 0000 001 */
        120x0, G3CODE_INVALID }  /* 0000 0000 0000 */
    };
    
    private int[] horizcode =
        30x1};    /* 001 */
    private int[] passcode =
        40x1};    /* 0001 */
    private int[][] vcodes = {
        70x03},  /* 0000 011 */
        60x03},  /* 0000 11 */
        30x03},  /* 011 */
        10x1},    /* 1 */
        30x2},    /* 010 */
        60x02},  /* 0000 10 */
        70x02}    /* 0000 010 */
    };
    private int[] msbmask =
    0x000x010x030x070x0f0x1f0x3f0x7f0xff };
}