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    import java.io.InputStream;
037    
038    /**
039     * PackBits decoder
040     *
041     * @author Andrei Kouznetsov
042     */
043    public class RLE8InputStream extends RLEInputStream {
044    
045        int numSamples, value;
046        boolean copyLiter;
047        boolean ignoreByte;
048    
049        public RLE8InputStream(InputStream in) {
050            super(in);
051        }
052    
053        public int read() throws IOException {
054            if (numSamples == 0) {
055                if (ignoreByte) {
056                    ignoreByte = false;
057                    /*int ignored = */in.read();
058                }
059                int l = in.read();
060                if (l == 0) {
061                    value = in.read();
062                    switch (value) {
063                        case 0:
064                            throw new EndOfLineException();
065                        case 1:
066                            finished = true;
067                            throw new EndOfBitmapException();
068                        case 2:
069                            int x = in.read();
070                            int y = in.read();
071                            throw new DeltaRecordException(x, y);
072                        default:
073                            copyLiter = true;
074                            numSamples = value;
075                            if ((numSamples & 1) != 0) {
076                                ignoreByte = true;
077                            }
078                    }
079                }
080                else {
081                    numSamples = l;
082                    copyLiter = false;
083                    value = in.read();
084                }
085            }
086            numSamples--;
087            if (copyLiter) {
088                return in.read();
089            }
090            else {
091                return value;
092            }
093        }
094    }