1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Rustem V. Rafikov
19 * @version $Revision: 1.4 $
20 */
21package org.apache.harmony.x.imageio.plugins.jpeg;
22
23
24import javax.imageio.ImageReader;
25import javax.imageio.ImageReadParam;
26import javax.imageio.ImageTypeSpecifier;
27import javax.imageio.plugins.jpeg.JPEGImageReadParam;
28import javax.imageio.stream.ImageInputStream;
29import javax.imageio.metadata.IIOMetadata;
30import javax.imageio.spi.ImageReaderSpi;
31
32import org.apache.harmony.awt.gl.image.DecodingImageSource;
33import org.apache.harmony.awt.gl.image.OffscreenImage;
34
35import java.io.IOException;
36import java.util.Iterator;
37import java.awt.image.BufferedImage;
38
39/**
40 * This implementation uses org.apache.harmony.awt.gl.image.JpegDecoder to read
41 * an image. The only implemented method is read(..);
42 *
43 * TODO: Implements generic decoder to be used by javad2 and imageio
44 *
45 * @see org.apache.harmony.awt.gl.image.JpegDecoder
46 * @see org.apache.harmony.x.imageio.plugins.jpeg.IISDecodingImageSource
47 */
48public class JPEGImageReader extends ImageReader {
49
50    ImageInputStream iis;
51
52    public JPEGImageReader(ImageReaderSpi imageReaderSpi) {
53        super(imageReaderSpi);
54    }
55
56    @Override
57    public int getHeight(int i) throws IOException {
58        //-- TODO imlement
59        throw new UnsupportedOperationException("not implemented yet");
60    }
61
62    @Override
63    public int getWidth(int i) throws IOException {
64        //-- TODO imlement
65        throw new UnsupportedOperationException("not implemented yet");
66    }
67
68    @Override
69    public int getNumImages(boolean b) throws IOException {
70        //-- TODO imlement
71        throw new UnsupportedOperationException("not implemented yet");
72    }
73
74    @Override
75    public Iterator<ImageTypeSpecifier> getImageTypes(int i) throws IOException {
76        //-- TODO imlement
77        throw new UnsupportedOperationException("not implemented yet");
78    }
79
80    @Override
81    public IIOMetadata getStreamMetadata() throws IOException {
82        //-- TODO imlement
83        throw new UnsupportedOperationException("not implemented yet");
84    }
85
86    @Override
87    public IIOMetadata getImageMetadata(int i) throws IOException {
88        //-- TODO imlement
89        throw new UnsupportedOperationException("not implemented yet");
90    }
91
92    @Override
93    public BufferedImage read(int i, ImageReadParam imageReadParam) throws IOException {
94        if (iis == null) {
95            throw new IllegalArgumentException("input stream == null");
96        }
97
98        DecodingImageSource source = new IISDecodingImageSource(iis);
99        OffscreenImage image = new OffscreenImage(source);
100        source.addConsumer(image);
101        source.load();
102        // The interrupted flag should be cleared because ImageDecoder interrupts
103        // current thread while decoding. The same technique is used in
104        // ImageLoader#run(). Another solution can be to create
105        // a separate decoding thread. However, decoder keeps its own pool
106        // of threads so creating a new thread will be just a waste of resources.
107        Thread.interrupted();
108        return image.getBufferedImage();
109    }
110
111    @Override
112    public BufferedImage read(int i) throws IOException {
113        return read(i, null);
114    }
115
116    @Override
117    public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
118        super.setInput(input, seekForwardOnly, ignoreMetadata);
119        iis = (ImageInputStream) input;
120    }
121
122    @Override
123    public ImageReadParam getDefaultReadParam() {
124        return new JPEGImageReadParam();
125    }
126}
127