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
19package org.apache.harmony.x.imageio.plugins.png;
20
21import org.apache.harmony.x.imageio.plugins.jpeg.JPEGSpiConsts;
22
23import javax.imageio.spi.ImageReaderSpi;
24import javax.imageio.spi.ServiceRegistry;
25import javax.imageio.ImageReader;
26import javax.imageio.stream.ImageInputStream;
27import java.io.IOException;
28import java.util.Locale;
29
30public class PNGImageReaderSpi extends ImageReaderSpi {
31    static final String PNG_NAMES[] = new String[] {"png", "PNG"};
32    static final String PNG_SUFFIXES[] = new String[] {"png"};
33    static final String PNG_MIME_TYPES[] = new String[] {"image/png"};
34    static final String PNG_READER_CLASS_NAME = "org.apache.harmony.x.imageio.plugins.png.PNGImageReader";
35    static final String PNG_READER_SPI_NAMES[] = {"org.apache.harmony.x.imageio.plugins.png.PNGImageReaderSpi"};
36
37    public PNGImageReaderSpi() {
38        super(
39                JPEGSpiConsts.vendorName, JPEGSpiConsts.version,
40                PNG_NAMES, PNG_SUFFIXES,
41                PNG_MIME_TYPES, PNG_READER_CLASS_NAME,
42                STANDARD_INPUT_TYPE, null,
43                false, null,
44                null, null,
45                null, false,
46                null, null,
47                null, null
48        );
49    }
50
51    @Override
52    public boolean canDecodeInput(Object source) throws IOException {
53        ImageInputStream markable = (ImageInputStream) source;
54        markable.mark();
55
56        byte[] signature = new byte[8];
57        markable.seek(0);
58
59        int nBytes = markable.read(signature, 0, 8);
60        if(nBytes != 8) markable.read(signature, nBytes, 8-nBytes);
61        markable.reset();
62
63        // PNG signature: 137 80 78 71 13 10 26 10
64        return  (signature[0] & 0xFF) == 137 &&
65                (signature[1] & 0xFF) == 80 &&
66                (signature[2] & 0xFF) == 78 &&
67                (signature[3] & 0xFF) == 71 &&
68                (signature[4] & 0xFF) == 13 &&
69                (signature[5] & 0xFF) == 10 &&
70                (signature[6] & 0xFF) == 26 &&
71                (signature[7] & 0xFF) == 10;
72    }
73
74    @Override
75    public ImageReader createReaderInstance(Object extension) throws IOException {
76        return new PNGImageReader(this);
77    }
78
79    @Override
80    public String getDescription(Locale locale) {
81        return "DRL PNG decoder";
82    }
83
84    @Override
85    public void onRegistration(ServiceRegistry registry, Class<?> category) {
86        super.onRegistration(registry, category);
87    }
88}
89