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;
049import javax.imageio.stream.ImageOutputStream;
050
051/**
052 * Came from GIFEncoder initially.
053 * Modified - to allow for output compressed data without the block counts
054 * which breakup the compressed data stream for GIF.
055 **/
056public class BitFile
057{
058    ImageOutputStream output_;
059    byte buffer_[];
060    int index_;
061    int bitsLeft_;      // bits left at current index that are avail.
062
063    /** note this also indicates gif format BITFile. **/
064    boolean blocks_ = false;
065
066    /**
067         * @param output destination for output data
068         * @param blocks GIF LZW requires block counts for output data
069         **/
070    public BitFile(ImageOutputStream    output, boolean blocks)
071    {
072        output_ = output;
073        blocks_ = blocks;
074        buffer_ = new byte[256];
075        index_ = 0;
076        bitsLeft_ =     8;
077    }
078
079    public void flush() throws IOException
080    {
081        int     numBytes = index_ +     (bitsLeft_ == 8 ? 0     : 1);
082        if (numBytes > 0)
083            {
084                if (blocks_)
085                    output_.write(numBytes);
086                output_.write(buffer_, 0, numBytes);
087                buffer_[0] = 0;
088                index_ = 0;
089                bitsLeft_ =     8;
090            }
091    }
092
093    public void writeBits(int bits,     int     numbits) throws IOException
094    {
095        int     bitsWritten     = 0;
096        int     numBytes = 255;         // gif block count
097        do
098            {
099                // This handles the GIF block count stuff
100                if ((index_     == 254 && bitsLeft_     == 0) || index_ > 254)
101                    {
102                        if (blocks_)
103                            output_.write(numBytes);
104
105                        output_.write(buffer_, 0, numBytes);
106
107                        buffer_[0] = 0;
108                        index_ = 0;
109                        bitsLeft_ =     8;
110                    }
111
112                if (numbits     <= bitsLeft_) // bits contents fit in current index byte
113                    {
114                        if (blocks_) // GIF
115                            {
116                                buffer_[index_] |= (bits & ((1 << numbits) - 1)) <<     (8 - bitsLeft_);
117                                bitsWritten     += numbits;
118                                bitsLeft_ -= numbits;
119                                numbits = 0;
120                            }
121                        else
122                            {
123                                buffer_[index_] |= (bits & ((1 << numbits) - 1)) <<     (bitsLeft_ - numbits);
124                                bitsWritten     += numbits;
125                                bitsLeft_ -= numbits;
126                                numbits = 0;
127
128                            }
129                    }
130                else    // bits overflow from current byte to next.
131                    {
132                        if (blocks_)    // GIF
133                            {
134                                // if bits  > space left in current byte then the lowest order bits
135                                // of code are taken and put in current byte and rest put in next.
136                                buffer_[index_] |= (bits & ((1 << bitsLeft_) - 1)) << (8 - bitsLeft_);
137                                bitsWritten     += bitsLeft_;
138                                bits >>= bitsLeft_;
139                                numbits -= bitsLeft_;
140                                buffer_[++index_] =     0;
141                                bitsLeft_ =     8;
142                            }
143                        else
144                            {
145                                // if bits  > space left in current byte then the highest order bits
146                                // of code are taken and put in current byte and rest put in next.
147                                // at highest order bit location !! 
148                                int topbits = (bits >>> (numbits - bitsLeft_)) & ((1 << bitsLeft_) - 1);
149                                buffer_[index_] |= topbits;
150                                numbits -= bitsLeft_;   // ok this many bits gone off the top
151                                bitsWritten     += bitsLeft_;
152                                buffer_[++index_] =     0;      // next index
153                                bitsLeft_ =     8;
154                            }
155                    }
156
157            } while     (numbits !=     0);
158    }
159}