001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.x.imageio.plugins;
018:
019: import java.awt.Image;
020: import java.awt.image.BufferedImage;
021: import java.awt.image.ColorModel;
022: import java.awt.image.ImageObserver;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.util.Arrays;
026: import java.util.Iterator;
027:
028: import javax.imageio.ImageReadParam;
029: import javax.imageio.ImageReader;
030: import javax.imageio.ImageTypeSpecifier;
031: import javax.imageio.metadata.IIOMetadata;
032: import javax.imageio.spi.ImageReaderSpi;
033: import javax.imageio.stream.ImageInputStream;
034:
035: import org.apache.harmony.awt.gl.image.DecodingImageSource;
036: import org.apache.harmony.awt.gl.image.OffscreenImage;
037: import org.apache.harmony.x.imageio.internal.nls.Messages;
038:
039: /**
040: * This implementation is based on the image loader from the AWT module. It
041: * supports all the image types supported by the loader.
042: */
043: public class AwtImageReader extends ImageReader {
044:
045: private ImageInputStream iis;
046: private OffscreenImage image;
047:
048: public AwtImageReader(final ImageReaderSpi imageReaderSpi) {
049: super (imageReaderSpi);
050: }
051:
052: @Override
053: public int getHeight(final int i) throws IOException {
054: return getImage(i).getHeight(new ImageObserver() {
055: public boolean imageUpdate(final Image img,
056: final int infoflags, final int x, final int y,
057: final int width, final int height) {
058: return (infoflags & HEIGHT) == 0;
059: }
060: });
061: }
062:
063: @Override
064: public int getWidth(final int i) throws IOException {
065: return getImage(i).getWidth(new ImageObserver() {
066: public boolean imageUpdate(final Image img,
067: final int infoflags, final int x, final int y,
068: final int width, final int height) {
069: return (infoflags & WIDTH) == 0;
070: }
071: });
072: }
073:
074: @Override
075: public int getNumImages(final boolean b) throws IOException {
076: return 1;
077: }
078:
079: @Override
080: public Iterator<ImageTypeSpecifier> getImageTypes(final int i)
081: throws IOException {
082: final ColorModel model = getImage(i).getColorModel();
083: final ImageTypeSpecifier[] spec = { new ImageTypeSpecifier(
084: model, model.createCompatibleSampleModel(1, 1)) };
085: return Arrays.asList(spec).iterator();
086: }
087:
088: @Override
089: public IIOMetadata getStreamMetadata() throws IOException {
090: return null;
091: }
092:
093: @Override
094: public IIOMetadata getImageMetadata(final int i) throws IOException {
095: return null;
096: }
097:
098: @Override
099: public BufferedImage read(final int i,
100: final ImageReadParam imageReadParam) throws IOException {
101: return getImage(i).getBufferedImage();
102: }
103:
104: @Override
105: public void setInput(final Object input,
106: final boolean seekForwardOnly, final boolean ignoreMetadata) {
107: super .setInput(input, seekForwardOnly, ignoreMetadata);
108: iis = (ImageInputStream) input;
109: image = null;
110: }
111:
112: @Override
113: public ImageReadParam getDefaultReadParam() {
114: return new ImageReadParam();
115: }
116:
117: private OffscreenImage getImage(final int index) throws IOException {
118: if (index >= getNumImages(false)) {
119: throw new IndexOutOfBoundsException(
120: "index >= getNumImages()"); //$NON-NLS-1$
121: }
122:
123: if (image == null) {
124: if (iis == null) {
125: throw new IllegalArgumentException(Messages.getString(
126: "imageio.2", //$NON-NLS-1$
127: "input")); //$NON-NLS-1$
128: }
129:
130: final DecodingImageSource source = new IISDecodingImageSource(
131: iis);
132: image = new OffscreenImage(source);
133: source.addConsumer(image);
134: source.load();
135: }
136:
137: return image;
138: }
139:
140: private static class IISDecodingImageSource extends
141: DecodingImageSource {
142:
143: private final InputStream is;
144:
145: IISDecodingImageSource(final ImageInputStream iis) {
146: is = PluginUtils.wrapIIS(iis);
147: }
148:
149: @Override
150: protected boolean checkConnection() {
151: return true;
152: }
153:
154: @Override
155: protected InputStream getInputStream() {
156: return is;
157: }
158: }
159: }
|