1 51 package org.apache.fop.image.analyser; 52 53 import java.io.BufferedInputStream ; 55 import java.io.IOException ; 56 57 61 public class PNGReader extends AbstractImageReader { 62 protected static final int PNG_SIG_LENGTH = 24; 63 protected byte[] header; 64 65 public boolean verifySignature(String uri, BufferedInputStream fis) 66 throws IOException { 67 this.imageStream = fis; 68 this.setDefaultHeader(); 69 boolean supported = ((header[0] == (byte)0x89) 70 && (header[1] == (byte)0x50) 71 && (header[2] == (byte)0x4e) 72 && (header[3] == (byte)0x47) 73 && (header[4] == (byte)0x0d) 74 && (header[5] == (byte)0x0a) 75 && (header[6] == (byte)0x1a) 76 && (header[7] == (byte)0x0a)); 77 if (supported) { 78 setDimension(); 79 return true; 80 } else 81 return false; 82 } 83 84 public String getMimeType() { 85 return "image/png"; 86 } 87 88 protected void setDimension() { 89 int byte1 = header[16] & 0xff; 91 int byte2 = header[17] & 0xff; 92 int byte3 = header[18] & 0xff; 93 int byte4 = header[19] & 0xff; 94 long l = (long)((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4); 95 this.width = (int)(l); 96 97 byte1 = header[20] & 0xff; 98 byte2 = header[21] & 0xff; 99 byte3 = header[22] & 0xff; 100 byte4 = header[23] & 0xff; 101 l = (long)((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4); 102 this.height = (int)(l); 103 104 } 105 106 protected void setDefaultHeader() throws IOException { 107 this.header = new byte[PNG_SIG_LENGTH]; 108 try { 109 this.imageStream.mark(PNG_SIG_LENGTH + 1); 110 this.imageStream.read(header); 111 this.imageStream.reset(); 112 } catch (IOException ex) { 113 try { 114 this.imageStream.reset(); 115 } catch (IOException exbis) {} 116 throw ex; 117 } 118 } 119 120 } 121 122 | Popular Tags |