3 package com.pixelmed.imageio;
11 import java.io.ByteArrayOutputStream;
12 import java.io.InputStream;
13 import java.io.IOException;
15 import java.nio.ByteOrder;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
21 import java.awt.Point;
22 import java.awt.Transparency;
24 import java.awt.color.ColorSpace;
26 import java.awt.image.BufferedImage;
27 import java.awt.image.ComponentColorModel;
28 import java.awt.image.ComponentSampleModel;
29 import java.awt.image.DataBuffer;
30 import java.awt.image.DataBufferByte;
31 import java.awt.image.DataBufferUShort;
32 import java.awt.image.Raster;
33 import java.awt.image.WritableRaster;
35 import javax.imageio.ImageReader;
36 import javax.imageio.IIOException;
37 import javax.imageio.ImageReadParam;
38 import javax.imageio.ImageTypeSpecifier;
39 import javax.imageio.metadata.IIOMetadata;
40 import javax.imageio.spi.ImageReaderSpi;
41 import javax.imageio.stream.ImageInputStream;
45 private static final String identString =
"@(#) $Header: /userland/cvs/codec/com/pixelmed/imageio/JPEGLosslessImageReader.java,v 1.8 2015/10/19 15:34:42 dclunie Exp $";
47 ImageInputStream stream =
null;
55 boolean gotEverything =
false;
58 super(originatingProvider);
62 System.err.println(
"reset()");
65 gotEverything =
false;
66 decompressedOutput =
null;
69 public void setInput(Object input,
boolean isStreamable,
boolean ignoreMetadata) {
71 super.setInput(input,isStreamable,ignoreMetadata);
76 if (input instanceof ImageInputStream) {
77 this.stream = (ImageInputStream)input;
80 throw new IllegalArgumentException(
"bad input");
83 gotEverything =
false;
84 decompressedOutput =
null;
91 private void checkIndex(
int imageIndex) {
93 if (imageIndex != 0) {
94 throw new IndexOutOfBoundsException(
"bad index");
98 public int getWidth(
int imageIndex)
throws IIOException {
99 checkIndex(imageIndex);
104 public int getHeight(
int imageIndex)
throws IIOException {
105 checkIndex(imageIndex);
110 public Iterator<ImageTypeSpecifier>
getImageTypes(
int imageIndex)
throws IIOException {
111 checkIndex(imageIndex);
114 ImageTypeSpecifier imageType =
null;
115 List l =
new ArrayList<ImageTypeSpecifier>();
116 imageType = ImageTypeSpecifier.createGrayscale(
118 bitDepth <= 8 ? DataBuffer.TYPE_BYTE : DataBuffer.TYPE_USHORT,
124 private final class WrapImageInputStreamAsInputStream
extends InputStream {
125 private final ImageInputStream iis;
127 private WrapImageInputStreamAsInputStream() {
131 public WrapImageInputStreamAsInputStream(ImageInputStream iis) {
135 public final int available() {
return 0; }
137 public final void close() throws IOException { iis.close(); }
139 public final void mark(
int readlimit) { iis.mark(); }
141 public final boolean markSupported() {
return true; }
143 public final int read() throws IOException {
return iis.read(); }
145 public final int read(
byte[] b)
throws IOException {
return iis.read(b); }
147 public final int read(
byte[] b,
int off,
int len)
throws IOException {
return iis.read(b,off,len); }
149 public final void reset() throws IOException { iis.reset(); }
151 public final long skip(
long n)
throws IOException {
return iis.skipBytes(n); }
158 gotEverything =
true;
160 if (stream ==
null) {
161 throw new IllegalStateException(
"No input stream");
166 MarkerSegmentSOF sof = markerSegments !=
null ? markerSegments.getSOF() :
null;
169 throw new IIOException(
"Error reading JPEG stream - only single component (grayscale) or three component supported)");
176 throw new IIOException(
"Error reading JPEG stream - no SOS or SOF marker segment parsed");
179 catch (Exception e) {
180 throw new IIOException(
"Error reading JPEG stream",e);
184 public BufferedImage
read(
int imageIndex, ImageReadParam param)
throws IOException {
185 checkIndex(imageIndex);
188 BufferedImage image =
null;
190 OutputArrayOrStream[] decompressedOutputPerComponent = decompressedOutput.getDecompressedOutputPerComponent();
192 ComponentColorModel cm =
null;
193 ComponentSampleModel sm =
null;
194 DataBuffer buf =
null;
195 if (decompressedOutputPerComponent.length == 1) {
198 cm=
new ComponentColorModel(
199 ColorSpace.getInstance(ColorSpace.CS_GRAY),
206 sm =
new ComponentSampleModel(
207 DataBuffer.TYPE_BYTE,
214 buf =
new DataBufferByte(decompressedOutputPerComponent[0].getByteArray(),width,0);
218 cm=
new ComponentColorModel(
219 ColorSpace.getInstance(ColorSpace.CS_GRAY),
224 DataBuffer.TYPE_USHORT
226 sm =
new ComponentSampleModel(
227 DataBuffer.TYPE_USHORT,
234 buf =
new DataBufferUShort(decompressedOutputPerComponent[0].getShortArray(),width,0);
237 else if (decompressedOutputPerComponent.length == 3) {
241 cm=
new ComponentColorModel(
242 ColorSpace.getInstance(ColorSpace.CS_sRGB),
249 sm =
new ComponentSampleModel(
250 DataBuffer.TYPE_BYTE,
258 buf =
new DataBufferByte(
268 cm=
new ComponentColorModel(
269 ColorSpace.getInstance(ColorSpace.CS_sRGB),
270 new int[] {16,16,16},
274 DataBuffer.TYPE_USHORT
276 sm =
new ComponentSampleModel(
277 DataBuffer.TYPE_USHORT,
285 buf =
new DataBufferUShort(
296 WritableRaster wr = Raster.createWritableRaster(sm,buf,
new Point(0,0));
297 image =
new BufferedImage(cm,wr,
true,
null);
310 if (imageIndex != 0) {
311 throw new IndexOutOfBoundsException(
"imageIndex != 0!");
318 if (metadata !=
null) {