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 * Medical Insight.
019 * Portions created by the Initial Developer are Copyright (C) 2011
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;
042
043import javax.imageio.stream.ImageOutputStream;
044import javax.imageio.stream.ImageOutputStreamImpl;
045
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049/**
050 * @author Jesper Bojesen <jbb@medical-insight.com>
051 * @author Gunter Zeilinger <gunterze@gmail.com>
052 */
053public class PatchJPEGLSImageOutputStream extends ImageOutputStreamImpl {
054
055    private static final Logger LOG =
056            LoggerFactory.getLogger(PatchJPEGLSImageOutputStream.class);
057
058    private final ImageOutputStream ios;
059    private final PatchJPEGLS patchJpegLS;
060    private byte[] jpegheader;
061    private int jpegheaderIndex;
062
063    public PatchJPEGLSImageOutputStream(ImageOutputStream ios,
064            PatchJPEGLS patchJpegLS) throws IOException {
065        if (ios == null)
066            throw new NullPointerException("ios");
067        super.streamPos = ios.getStreamPosition();
068        super.flushedPos = ios.getFlushedPosition();
069        this.ios = ios;
070        this.patchJpegLS = patchJpegLS;
071        this.jpegheader = patchJpegLS != null ? new byte[256] : null;
072    }
073
074    public void write(byte[] b, int off, int len) throws IOException {
075        if (jpegheader == null) {
076            ios.write(b, off, len);
077        } else {
078            int len0 = Math.min(jpegheader.length - jpegheaderIndex, len);
079            System.arraycopy(b, off, jpegheader, jpegheaderIndex, len0);
080            jpegheaderIndex += len0;
081            if (jpegheaderIndex >= jpegheader.length) {
082                JPEGLSCodingParam param =
083                        patchJpegLS.createJPEGLSCodingParam(jpegheader);
084                if (param == null)
085                    ios.write(jpegheader);
086                else {
087                    LOG.debug("Patch JPEG-LS with {}", param);
088                    int offset = param.getOffset();
089                    ios.write(jpegheader, 0, offset);
090                    ios.write(param.getBytes());
091                    ios.write(jpegheader, offset, jpegheader.length - offset);
092                }
093                ios.write(b, off + len0, len - len0);
094                jpegheader = null;
095            }
096        }
097        streamPos += len;
098    }
099
100    public void write(byte[] b) throws IOException {
101        write(b, 0, b.length);
102    }
103
104    public void write(int b) throws IOException {
105        if (jpegheader == null) {
106            ios.write(b);
107            streamPos++;
108        } else
109            write(new byte[]{(byte) b},0,1);
110    }
111
112    public int read() throws IOException {
113        return ios.read();
114    }
115
116    public int read(byte[] b, int off, int len) throws IOException {
117        return ios.read (b, off, len);
118    }
119}