You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ap...@apache.org on 2008/01/21 10:20:32 UTC

svn commit: r613806 - /harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/org/apache/harmony/x/imageio/plugins/jpeg/JPEGImageReader.java

Author: apavlenko
Date: Mon Jan 21 01:20:30 2008
New Revision: 613806

URL: http://svn.apache.org/viewvc?rev=613806&view=rev
Log:
Implemented unimplemented methods of JPEGImageReader.

Modified:
    harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/org/apache/harmony/x/imageio/plugins/jpeg/JPEGImageReader.java

Modified: harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/org/apache/harmony/x/imageio/plugins/jpeg/JPEGImageReader.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/org/apache/harmony/x/imageio/plugins/jpeg/JPEGImageReader.java?rev=613806&r1=613805&r2=613806&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/org/apache/harmony/x/imageio/plugins/jpeg/JPEGImageReader.java (original)
+++ harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/org/apache/harmony/x/imageio/plugins/jpeg/JPEGImageReader.java Mon Jan 21 01:20:30 2008
@@ -14,114 +14,129 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-/**
- * @author Rustem V. Rafikov
- * @version $Revision: 1.4 $
- */
 package org.apache.harmony.x.imageio.plugins.jpeg;
 
+import java.awt.Image;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.ImageObserver;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Iterator;
 
-import javax.imageio.ImageReader;
 import javax.imageio.ImageReadParam;
+import javax.imageio.ImageReader;
 import javax.imageio.ImageTypeSpecifier;
-import javax.imageio.plugins.jpeg.JPEGImageReadParam;
-import javax.imageio.stream.ImageInputStream;
 import javax.imageio.metadata.IIOMetadata;
+import javax.imageio.plugins.jpeg.JPEGImageReadParam;
 import javax.imageio.spi.ImageReaderSpi;
+import javax.imageio.stream.ImageInputStream;
 
 import org.apache.harmony.awt.gl.image.DecodingImageSource;
 import org.apache.harmony.awt.gl.image.OffscreenImage;
-import org.apache.harmony.luni.util.NotImplementedException;
-
-import java.io.IOException;
-import java.util.Iterator;
-import java.awt.image.BufferedImage;
+import org.apache.harmony.x.imageio.internal.nls.Messages;
 
 /**
  * This implementation uses org.apache.harmony.awt.gl.image.JpegDecoder to read
- * an image. The only implemented method is read(..);
- *
- * TODO: Implements generic decoder to be used by javad2 and imageio
- *
+ * an image. 
+ * 
+ * TODO: Implement generic decoder to be used by javad2 and imageio
+ * 
  * @see org.apache.harmony.awt.gl.image.JpegDecoder
  * @see org.apache.harmony.x.imageio.plugins.jpeg.IISDecodingImageSource
  */
 public class JPEGImageReader extends ImageReader {
 
-    ImageInputStream iis;
+    private ImageInputStream iis;
+    private OffscreenImage   image;
 
-    public JPEGImageReader(ImageReaderSpi imageReaderSpi) {
+    public JPEGImageReader(final ImageReaderSpi imageReaderSpi) {
         super(imageReaderSpi);
     }
 
     @Override
-    public int getHeight(int i) throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public int getHeight(final int i) throws IOException {
+        return getImage(i).getHeight(new ImageObserver() {
+            public boolean imageUpdate(final Image img, final int infoflags,
+                            final int x, final int y, final int width,
+                            final int height) {
+                return (infoflags & HEIGHT) == 0;
+            }
+        });
     }
 
     @Override
-    public int getWidth(int i) throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public int getWidth(final int i) throws IOException {
+        return getImage(i).getWidth(new ImageObserver() {
+            public boolean imageUpdate(final Image img, final int infoflags,
+                            final int x, final int y, final int width,
+                            final int height) {
+                return (infoflags & WIDTH) == 0;
+            }
+        });
     }
 
     @Override
-    public int getNumImages(boolean b) throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public int getNumImages(final boolean b) throws IOException {
+        return 1;
     }
 
     @Override
-    public Iterator<ImageTypeSpecifier> getImageTypes(int i) throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public Iterator<ImageTypeSpecifier> getImageTypes(final int i)
+                    throws IOException {
+        final ColorModel model = getImage(i).getColorModel();
+        final ImageTypeSpecifier[] spec = { new ImageTypeSpecifier(model,
+                        model.createCompatibleSampleModel(1, 1)) };
+        return Arrays.asList(spec).iterator();
     }
 
     @Override
-    public IIOMetadata getStreamMetadata() throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public IIOMetadata getStreamMetadata() throws IOException {
+        return null;
     }
 
     @Override
-    public IIOMetadata getImageMetadata(int i) throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
-    }
-
-    @Override
-    public BufferedImage read(int i, ImageReadParam imageReadParam) throws IOException {
-        if (iis == null) {
-            throw new IllegalArgumentException("input stream == null");
-        }
-
-        DecodingImageSource source = new IISDecodingImageSource(iis);
-        OffscreenImage image = new OffscreenImage(source);
-        source.addConsumer(image);
-        source.load();
-        // The interrupted flag should be cleared because ImageDecoder interrupts
-        // current thread while decoding. The same technique is used in
-        // ImageLoader#run(). Another solution can be to create
-        // a separate decoding thread. However, decoder keeps its own pool
-        // of threads so creating a new thread will be just a waste of resources.
-        Thread.interrupted();
-        return image.getBufferedImage();
+    public IIOMetadata getImageMetadata(final int i) throws IOException {
+        return null;
     }
 
     @Override
-    public BufferedImage read(int i) throws IOException {
-        return read(i, null);
+    public BufferedImage read(final int i, final ImageReadParam imageReadParam)
+                    throws IOException {
+        return getImage(i).getBufferedImage();
     }
 
     @Override
-    public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
+    public void setInput(final Object input, final boolean seekForwardOnly,
+                    final boolean ignoreMetadata) {
         super.setInput(input, seekForwardOnly, ignoreMetadata);
         iis = (ImageInputStream) input;
+        image = null;
     }
 
     @Override
     public ImageReadParam getDefaultReadParam() {
         return new JPEGImageReadParam();
+    }
+
+    private OffscreenImage getImage(final int index) throws IOException {
+        if (index >= getNumImages(false)) {
+            throw new IndexOutOfBoundsException("index >= getNumImages()"); //$NON-NLS-1$
+        }
+
+        if (image == null) {
+            if (iis == null) {
+                throw new IllegalArgumentException(Messages.getString(
+                    "imageio.2", //$NON-NLS-1$
+                    "input")); //$NON-NLS-1$
+            }
+
+            final DecodingImageSource source = new IISDecodingImageSource(iis);
+            image = new OffscreenImage(source);
+            source.addConsumer(image);
+            source.load();
+        }
+
+        return image;
     }
 }