summaryrefslogtreecommitdiffstats
path: root/awt/org/apache/harmony/awt/gl/image/PngDecoderJava.java
blob: 46545f9c40d94d0002763f3181494c6d81e0c0bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package org.apache.harmony.awt.gl.image;

// A simple PNG decoder source code in Java.
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.CRC32;
import java.util.zip.InflaterInputStream;

//import javax.swing.JFrame;

public class PngDecoderJava {
 
/*
  public static void main(String[] args) throws Exception {
    String name = "logo.png";
    if (args.length > 0)
      name = args[0];
    InputStream in = PngDecoderJava.class.getResourceAsStream(name);
    final BufferedImage image = PngDecoderJava.decode(in);
    in.close();

    JFrame f = new JFrame() {
      public void paint(Graphics g) {
        Insets insets = getInsets();
        g.drawImage(image, insets.left, insets.top, null);
      }
    };
    f.setVisible(true);
    Insets insets = f.getInsets();
    f.setSize(image.getWidth() + insets.left + insets.right, image
        .getHeight()
        + insets.top + insets.bottom);
  }
  */

  public static BufferedImage decode(InputStream in) throws IOException {
    DataInputStream dataIn = new DataInputStream(in);
    readSignature(dataIn);
    PNGData chunks = readChunks(dataIn);

    long widthLong = chunks.getWidth();
    long heightLong = chunks.getHeight();
    if (widthLong > Integer.MAX_VALUE || heightLong > Integer.MAX_VALUE)
      throw new IOException("That image is too wide or tall.");
    int width = (int) widthLong;
    int height = (int) heightLong;

    ColorModel cm = chunks.getColorModel();
    WritableRaster raster = chunks.getRaster();

    BufferedImage image = new BufferedImage(cm, raster, false, null);

    return image;
  }

  protected static void readSignature(DataInputStream in) throws IOException {
    long signature = in.readLong();
    if (signature != 0x89504e470d0a1a0aL)
      throw new IOException("PNG signature not found!");
  }

  protected static PNGData readChunks(DataInputStream in) throws IOException {
    PNGData chunks = new PNGData();

    boolean trucking = true;
    while (trucking) {
      try {
        // Read the length.
        int length = in.readInt();
        if (length < 0)
          throw new IOException("Sorry, that file is too long.");
        // Read the type.
        byte[] typeBytes = new byte[4];
        in.readFully(typeBytes);
        // Read the data.
        byte[] data = new byte[length];
        in.readFully(data);
        // Read the CRC.
        long crc = in.readInt() & 0x00000000ffffffffL; // Make it
        // unsigned.
        if (verifyCRC(typeBytes, data, crc) == false)
          throw new IOException("That file appears to be corrupted.");

        PNGChunk chunk = new PNGChunk(typeBytes, data);
        chunks.add(chunk);
      } catch (EOFException eofe) {
        trucking = false;
      }
    }
    return chunks;
  }

  protected static boolean verifyCRC(byte[] typeBytes, byte[] data, long crc) {
    CRC32 crc32 = new CRC32();
    crc32.update(typeBytes);
    crc32.update(data);
    long calculated = crc32.getValue();
    return (calculated == crc);
  }
}

class PNGData {
  private int mNumberOfChunks;

  private PNGChunk[] mChunks;

  public PNGData() {
    mNumberOfChunks = 0;
    mChunks = new PNGChunk[10];
  }

  public void add(PNGChunk chunk) {
    mChunks[mNumberOfChunks++] = chunk;
    if (mNumberOfChunks >= mChunks.length) {
      PNGChunk[] largerArray = new PNGChunk[mChunks.length + 10];
      System.arraycopy(mChunks, 0, largerArray, 0, mChunks.length);
      mChunks = largerArray;
    }
  }

  public long getWidth() {
    return getChunk("IHDR").getUnsignedInt(0);
  }

  public long getHeight() {    return getChunk("IHDR").getUnsignedInt(4);
  }

  public short getBitsPerPixel() {
    return getChunk("IHDR").getUnsignedByte(8);
  }

  public short getColorType() {
    return getChunk("IHDR").getUnsignedByte(9);
  }

  public short getCompression() {
    return getChunk("IHDR").getUnsignedByte(10);
  }

  public short getFilter() {
    return getChunk("IHDR").getUnsignedByte(11);
  }

  public short getInterlace() {
    return getChunk("IHDR").getUnsignedByte(12);
  }

  public ColorModel getColorModel() {
    short colorType = getColorType();
    int bitsPerPixel = getBitsPerPixel();

    if (colorType == 3) {
      byte[] paletteData = getChunk("PLTE").getData();
      int paletteLength = paletteData.length / 3;
      return new IndexColorModel(bitsPerPixel, paletteLength,
          paletteData, 0, false);
    }
    System.out.println("Unsupported color type: " + colorType);
    return null;
  }

  public WritableRaster getRaster() {
    int width = (int) getWidth();
    int height = (int) getHeight();
    int bitsPerPixel = getBitsPerPixel();
    short colorType = getColorType();

    if (colorType == 3) {
      byte[] imageData = getImageData();
      //Orig: DataBuffer db = new DataBufferByte(imageData, imageData.length);
      int len = Math.max(imageData.length, (width - 1) * (height -1));
      DataBuffer db = new DataBufferByte(imageData, len);
      WritableRaster raster = Raster.createPackedRaster(db, width,
          height, bitsPerPixel, null);
      return raster;
    } else
      System.out.println("Unsupported color type!");
    return null;
  }

  public byte[] getImageData() {
    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      // Write all the IDAT data into the array.
      for (int i = 0; i < mNumberOfChunks; i++) {
        PNGChunk chunk = mChunks[i];
        if (chunk.getTypeString().equals("IDAT")) {
          out.write(chunk.getData());
        }
      }
      out.flush();
      // Now deflate the data.
      InflaterInputStream in = new InflaterInputStream(
          new ByteArrayInputStream(out.toByteArray()));
      ByteArrayOutputStream inflatedOut = new ByteArrayOutputStream();
      int readLength;
      byte[] block = new byte[8192];
      while ((readLength = in.read(block)) != -1)
        inflatedOut.write(block, 0, readLength);
      inflatedOut.flush();
      byte[] imageData = inflatedOut.toByteArray();
      // Compute the real length.
      int width = (int) getWidth();
      int height = (int) getHeight();
      int bitsPerPixel = getBitsPerPixel();
      int length = width * height * bitsPerPixel / 8;

      byte[] prunedData = new byte[length];

      // We can only deal with non-interlaced images.
      if (getInterlace() == 0) {
        int index = 0;
        for (int i = 0; i < length; i++) {
          if ((i * 8 / bitsPerPixel) % width == 0) {
            index++; // Skip the filter byte.
          }
          prunedData[i] = imageData[index++];
        }
      } else
        System.out.println("Couldn't undo interlacing.");

      return prunedData;
    } catch (IOException ioe) {
    }
    return null;
  }

  public PNGChunk getChunk(String type) {
    for (int i = 0; i < mNumberOfChunks; i++)
      if (mChunks[i].getTypeString().equals(type))
        return mChunks[i];
    return null;
  }
}

class PNGChunk {
  private byte[] mType;

  private byte[] mData;

  public PNGChunk(byte[] type, byte[] data) {
    mType = type;
    mData = data;
  }

  public String getTypeString() {
    try {
      return new String(mType, "UTF8");
    } catch (UnsupportedEncodingException uee) {
      return "";
    }
  }

  public byte[] getData() {
    return mData;
  }

  public long getUnsignedInt(int offset) {
    long value = 0;
    for (int i = 0; i < 4; i++)
      value += (mData[offset + i] & 0xff) << ((3 - i) * 8);
    return value;
  }

  public short getUnsignedByte(int offset) {
    return (short) (mData[offset] & 0x00ff);
  }
}