1 /*
   2  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.imageio.plugins.png;
  27 
  28 import java.io.IOException;
  29 import java.util.Locale;
  30 import java.util.Iterator;
  31 import javax.imageio.ImageReader;
  32 import javax.imageio.spi.ImageReaderSpi;
  33 import javax.imageio.metadata.IIOMetadataFormat;
  34 import javax.imageio.metadata.IIOMetadataFormatImpl;
  35 import javax.imageio.stream.ImageInputStream;
  36 
  37 public class PNGImageReaderSpi extends ImageReaderSpi {
  38 
  39     private static final String vendorName = "Oracle Corporation";
  40 
  41     private static final String version = "1.0";
  42 
  43     private static final String[] names = { "png", "PNG" };
  44 
  45     private static final String[] suffixes = { "png" };
  46 
  47     private static final String[] MIMETypes = { "image/png", "image/x-png" };
  48 
  49     private static final String readerClassName =
  50         "com.sun.imageio.plugins.png.PNGImageReader";
  51 
  52     private static final String[] writerSpiNames = {
  53         "com.sun.imageio.plugins.png.PNGImageWriterSpi"
  54     };
  55 
  56     public PNGImageReaderSpi() {
  57         super(vendorName,
  58               version,
  59               names,
  60               suffixes,
  61               MIMETypes,
  62               readerClassName,
  63               new Class[] { ImageInputStream.class },
  64               writerSpiNames,
  65               false,
  66               null, null,
  67               null, null,
  68               true,
  69               PNGMetadata.nativeMetadataFormatName,
  70               "com.sun.imageio.plugins.png.PNGMetadataFormat",
  71               null, null
  72               );
  73     }
  74 
  75     public String getDescription(Locale locale) {
  76         return "Standard PNG image reader";
  77     }
  78 
  79     public boolean canDecodeInput(Object input) throws IOException {
  80         if (!(input instanceof ImageInputStream)) {
  81             return false;
  82         }
  83 
  84         ImageInputStream stream = (ImageInputStream)input;
  85         byte[] b = new byte[8];
  86         stream.mark();
  87         stream.readFully(b);
  88         stream.reset();
  89 
  90         return (b[0] == (byte)137 &&
  91                 b[1] == (byte)80 &&
  92                 b[2] == (byte)78 &&
  93                 b[3] == (byte)71 &&
  94                 b[4] == (byte)13 &&
  95                 b[5] == (byte)10 &&
  96                 b[6] == (byte)26 &&
  97                 b[7] == (byte)10);
  98     }
  99 
 100     public ImageReader createReaderInstance(Object extension) {
 101         return new PNGImageReader(this);
 102     }
 103 }