001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    
033    package com.imagero.uio.io;
034    
035    import java.io.IOException;
036    
037    /**
038     * PackBits decoder
039     *
040     * @author Andrei Kouznetsov
041     */
042    public class RLE4InputStream extends RLEInputStream {
043    
044        byte[] value = new byte[2];
045    
046        BitInputStream bin;
047    
048        byte[] buffer = new byte[128];
049        int bufferStart;
050        int bufferLength;
051    
052        ByteArrayOutputStreamExt bout;
053        BitOutputStream bitOut;
054    
055        public RLE4InputStream(BitInputStream in) {
056            super(in);
057            bin = in;
058            bin.setBitsToRead(4);
059            bout = new ByteArrayOutputStreamExt();
060            bitOut = new BitOutputStream(bout);
061        }
062    
063        public int read() throws IOException {
064            if (bufferStart >= bufferLength) {
065                fillBuffer();
066            }
067            if (bufferStart >= bufferLength) {
068                return -1;
069            }
070            return buffer[bufferStart++];
071        }
072    
073        private void fillBuffer() throws IOException {
074            if (bout.size() == 0) {
075                fillBufferImpl();
076            }
077            bufferStart = 0;
078            bufferLength = bout.drain(buffer);
079        }
080    
081        private void fillBufferImpl() throws IOException {
082            int len = bin.read(8);
083            if (len == 0) {
084                int value = bin.read(8);
085                switch (value) {
086                    case 0:
087                        throw new EndOfLineException();
088                    case 1:
089                        finished = true;
090                        throw new EndOfBitmapException();
091                    case 2:
092                        int x = bin.read(8);
093                        int y = bin.read(8);
094                        throw new DeltaRecordException(x, y);
095                    default:
096                        int skipCount = 0;
097                        if ((value & 3) != 0) {
098                            skipCount = (value + 3) / 4 * 4 - value;
099                        }
100                        for (int i = 0; i < value; i++) {
101                            bitOut.write(bin.read(4), 4);
102                        }
103                        for (int i = 0; i < skipCount; i++) {
104                            /*int ignored = */bin.read(4);
105                        }
106                }
107            }
108            else {
109                value[0] = (byte) bin.read(4);
110                value[1] = (byte) bin.read(4);
111    
112                for (int i = 0; i < len; i++) {
113                    bitOut.write(value[i & 1], 4);
114                }
115            }
116        }
117    }