001/* ***** BEGIN LICENSE BLOCK *****
002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003 *
004 * The contents of this file are subject to the Mozilla Public License Version
005 * 1.1 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 * http://www.mozilla.org/MPL/
008 *
009 * Software distributed under the License is distributed on an "AS IS" basis,
010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011 * for the specific language governing rights and limitations under the
012 * License.
013 *
014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in
015 * Java(TM), available at http://sourceforge.net/projects/dcm4che.
016 *
017 * The Initial Developer of the Original Code is
018 * Agfa HealthCare.
019 * Portions created by the Initial Developer are Copyright (C) 2013
020 * the Initial Developer. All Rights Reserved.
021 *
022 * Contributor(s):
023 * See @authors listed below.
024 *
025 * Alternatively, the contents of this file may be used under the terms of
026 * either the GNU General Public License Version 2 or later (the "GPL"), or
027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
028 * in which case the provisions of the GPL or the LGPL are applicable instead
029 * of those above. If you wish to allow use of your version of this file only
030 * under the terms of either the GPL or the LGPL, and not to allow others to
031 * use your version of this file under the terms of the MPL, indicate your
032 * decision by deleting the provisions above and replace them with the notice
033 * and other provisions required by the GPL or the LGPL. If you do not delete
034 * the provisions above, a recipient may use your version of this file under
035 * the terms of any one of the MPL, the GPL or the LGPL.
036 *
037 * ***** END LICENSE BLOCK ***** */
038
039package org.dcm4che3.imageio.codec.jpeg;
040
041import java.io.IOException;
042import java.util.Arrays;
043
044import javax.imageio.stream.ImageInputStream;
045import javax.imageio.stream.ImageInputStreamImpl;
046
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050/**
051 * @author Gunter Zeilinger <gunterze@gmail.com>
052 */
053public class PatchJPEGLSImageInputStream extends ImageInputStreamImpl {
054
055    private static final Logger LOG =
056            LoggerFactory.getLogger(PatchJPEGLSImageInputStream.class);
057
058    private final ImageInputStream iis;
059    private long patchPos;
060    private byte[] patch;
061
062    public PatchJPEGLSImageInputStream(ImageInputStream iis,
063            PatchJPEGLS patchJPEGLS) throws IOException {
064        if (iis == null)
065            throw new NullPointerException("iis");
066
067        super.streamPos = iis.getStreamPosition();
068        super.flushedPos = iis.getFlushedPosition();
069        this.iis = iis;
070        if (patchJPEGLS == null)
071            return;
072
073        JPEGLSCodingParam param = patchJPEGLS.createJPEGLSCodingParam(firstBytesOf(iis));
074        if (param != null) {
075            LOG.debug("Patch JPEG-LS with {}", param);
076            this.patchPos = streamPos + param.getOffset();
077            this.patch = param.getBytes();
078        }
079    }
080
081    private byte[] firstBytesOf(ImageInputStream iis) throws IOException {
082        byte[] b = new byte[256];
083        int n, off = 0, len = b.length;
084        iis.mark();
085        while (len > 0 && (n = iis.read(b, off, len)) > 0) {
086            off += n;
087            len -= n;
088        }
089        iis.reset();
090        return len > 0 ? Arrays.copyOf(b, b.length - len) : b;
091    }
092
093    private int readAvailable(byte[] b) throws IOException {
094        int nbytes;
095        int off = 0;
096        int len = b.length;
097        while (len > 0 && (nbytes = iis.read(b, off, len)) > 0) {
098            off += nbytes;
099            len -= nbytes;
100        }
101        return off;
102    }
103
104    public void close() throws IOException {
105        super.close();
106        iis.close();
107    }
108
109    public void flushBefore(long pos) throws IOException {
110        super.flushBefore(pos);
111        iis.flushBefore(adjustStreamPosition(pos));
112    }
113
114    private long adjustStreamPosition(long pos) {
115        if (patch == null)
116            return pos;
117        long index = pos - patchPos;
118        return index < 0 ? pos 
119                : index < patch.length ? patchPos 
120                        : pos - patch.length;
121    }
122
123    public boolean isCached() {
124        return iis.isCached();
125    }
126
127    public boolean isCachedFile() {
128        return iis.isCachedFile();
129    }
130
131    public boolean isCachedMemory() {
132        return iis.isCachedMemory();
133    }
134
135    public long length() {
136        try {
137            long len = iis.length();
138            return patch == null || len < 0 ? len : len + patch.length;
139        } catch (IOException e) {
140            return -1;
141        }
142    }
143
144    public int read() throws IOException {
145        int ch;
146        long index;
147        if (patch != null
148                && (index = streamPos - patchPos) >= 0 
149                && index < patch.length)
150            ch = patch[(int) index];
151        else
152            ch = iis.read();
153        if (ch >= 0)
154            streamPos++;
155        return ch;
156    }
157
158    public int read(byte[] b, int off, int len) throws IOException {
159        int r = 0;
160        if (patch != null && streamPos < patchPos + patch.length) {
161            if (streamPos < patchPos) {
162                r = iis.read(b, off, (int) Math.min(patchPos - streamPos, len));
163                if (r < 0)
164                    return r;
165                streamPos += r;
166                if (streamPos < patchPos)
167                    return r;
168                off += r;
169                len -= r;
170            }
171            int index = (int) (patchPos - streamPos);
172            int r2 = (int) Math.min(patch.length - index, len);
173            System.arraycopy(patch, index, b, off, r2);
174            streamPos += r2;
175            r += r2;
176            off += r2;
177            len -= r2;
178        }
179        if (len > 0) {
180            int r3 = iis.read(b, off, len);
181            if (r3 < 0)
182                return r3;
183            streamPos += r3;
184            r += r3;
185        }
186        return r;
187    }
188
189    public void mark() {
190        super.mark();
191        iis.mark();
192    }
193
194    public void reset() throws IOException {
195        super.reset();
196        iis.reset();
197    }
198
199    public void seek(long pos) throws IOException {
200        super.seek(pos);
201        iis.seek(adjustStreamPosition(pos));
202    }
203
204}