001/*
002 * $RCSfile: BitFile.java,v $
003 *
004 * 
005 * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved.
006 * 
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions
009 * are met: 
010 * 
011 * - Redistribution of source code must retain the above copyright 
012 *   notice, this  list of conditions and the following disclaimer.
013 * 
014 * - Redistribution in binary form must reproduce the above copyright
015 *   notice, this list of conditions and the following disclaimer in 
016 *   the documentation and/or other materials provided with the
017 *   distribution.
018 * 
019 * Neither the name of Sun Microsystems, Inc. or the names of 
020 * contributors may be used to endorse or promote products derived 
021 * from this software without specific prior written permission.
022 * 
023 * This software is provided "AS IS," without a warranty of any 
024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 
025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 
028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 
029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 
031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035 * POSSIBILITY OF SUCH DAMAGES. 
036 * 
037 * You acknowledge that this software is not designed or intended for 
038 * use in the design, construction, operation or maintenance of any 
039 * nuclear facility. 
040 *
041 * $Revision: 1.1 $
042 * $Date: 2005/02/11 05:01:21 $
043 * $State: Exp $
044 */
045
046package com.github.jaiimageio.impl.common;
047
048import java.io.IOException;
049
050import javax.imageio.stream.ImageOutputStream;
051
052/**
053 * Came from GIFEncoder initially.
054 * Modified - to allow for output compressed data without the block counts
055 * which breakup the compressed data stream for GIF.
056 **/
057public class BitFile
058{
059    ImageOutputStream output_;
060    byte buffer_[];
061    int index_;
062    int bitsLeft_;      // bits left at current index that are avail.
063
064    /** note this also indicates gif format BITFile. **/
065    boolean blocks_ = false;
066
067    /**
068         * @param output destination for output data
069         * @param blocks GIF LZW requires block counts for output data
070         **/
071    public BitFile(ImageOutputStream    output, boolean blocks)
072    {
073        output_ = output;
074        blocks_ = blocks;
075        buffer_ = new byte[256];
076        index_ = 0;
077        bitsLeft_ =     8;
078    }
079
080    public void flush() throws IOException
081    {
082        int     numBytes = index_ +     (bitsLeft_ == 8 ? 0     : 1);
083        if (numBytes > 0)
084            {
085                if (blocks_)
086                    output_.write(numBytes);
087                output_.write(buffer_, 0, numBytes);
088                buffer_[0] = 0;
089                index_ = 0;
090                bitsLeft_ =     8;
091            }
092    }
093
094    public void writeBits(int bits,     int     numbits) throws IOException
095    {
096        int     bitsWritten     = 0;
097        int     numBytes = 255;         // gif block count
098        do
099            {
100                // This handles the GIF block count stuff
101                if ((index_     == 254 && bitsLeft_     == 0) || index_ > 254)
102                    {
103                        if (blocks_)
104                            output_.write(numBytes);
105
106                        output_.write(buffer_, 0, numBytes);
107
108                        buffer_[0] = 0;
109                        index_ = 0;
110                        bitsLeft_ =     8;
111                    }
112
113                if (numbits     <= bitsLeft_) // bits contents fit in current index byte
114                    {
115                        if (blocks_) // GIF
116                            {
117                                buffer_[index_] |= (bits & ((1 << numbits) - 1)) <<     (8 - bitsLeft_);
118                                bitsWritten     += numbits;
119                                bitsLeft_ -= numbits;
120                                numbits = 0;
121                            }
122                        else
123                            {
124                                buffer_[index_] |= (bits & ((1 << numbits) - 1)) <<     (bitsLeft_ - numbits);
125                                bitsWritten     += numbits;
126                                bitsLeft_ -= numbits;
127                                numbits = 0;
128
129                            }
130                    }
131                else    // bits overflow from current byte to next.
132                    {
133                        if (blocks_)    // GIF
134                            {
135                                // if bits  > space left in current byte then the lowest order bits
136                                // of code are taken and put in current byte and rest put in next.
137                                buffer_[index_] |= (bits & ((1 << bitsLeft_) - 1)) << (8 - bitsLeft_);
138                                bitsWritten     += bitsLeft_;
139                                bits >>= bitsLeft_;
140                                numbits -= bitsLeft_;
141                                buffer_[++index_] =     0;
142                                bitsLeft_ =     8;
143                            }
144                        else
145                            {
146                                // if bits  > space left in current byte then the highest order bits
147                                // of code are taken and put in current byte and rest put in next.
148                                // at highest order bit location !! 
149                                int topbits = (bits >>> (numbits - bitsLeft_)) & ((1 << bitsLeft_) - 1);
150                                buffer_[index_] |= topbits;
151                                numbits -= bitsLeft_;   // ok this many bits gone off the top
152                                bitsWritten     += bitsLeft_;
153                                buffer_[++index_] =     0;      // next index
154                                bitsLeft_ =     8;
155                            }
156                    }
157
158            } while     (numbits !=     0);
159    }
160}