1 17 18 package org.objectweb.jac.util; 19 20 import java.awt.Dimension ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Arrays ; 25 import org.apache.log4j.Logger; 26 27 30 31 public class Images { 32 static Logger logger = Logger.getLogger("images"); 33 34 protected static final byte[] 35 PNG_SIG = new byte[] {-119, 'P', 'N', 'G', 13, 10, 26, 10}; 36 protected static final byte[] 37 GIF87A_SIG = new byte[] {'G', 'I', 'F', '8', '7', 'a' }; 38 protected static final byte[] 39 GIF89A_SIG = new byte[] {'G', 'I', 'F', '8', '9', 'a' }; 40 protected static final byte[] 41 JFIF_SIG = new byte[] {-1, -40, -1, -32, 0, 0, 'J', 'F', 'I', 'F'}; 42 43 44 52 public static Dimension getImageFileSize(File img) 53 throws IOException 54 { 55 return getImageSize( 56 Images.class.getClassLoader().getResourceAsStream( 57 img.getPath())); 58 } 59 60 68 public static Dimension getImageSize(InputStream img) 69 throws IOException 70 { 71 72 int read; 73 byte [] buf = new byte[24]; 74 if ((read=img.read(buf,0,24))>=10) { 75 if (ExtArrays.equals(buf, 0, GIF87A_SIG, 0, 6) || 76 ExtArrays.equals(buf, 0, GIF89A_SIG, 0, 6)) { 77 return new Dimension ( 78 buf[7]*(2^8)+buf[6], 79 buf[9]*(2^8)+buf[8]); 80 } else if (ExtArrays.equals(buf,0,PNG_SIG,0,8)) { 81 if (read==24) { 82 return new Dimension ( 83 buf[16]*2^24+buf[17]*2^16+buf[18]*2^8+buf[19], 84 buf[20]*2^24+buf[21]*2^16+buf[22]*2^8+buf[23]); 85 } else { 86 throw new RuntimeException ( 87 "getImageSize: not enough data"); 88 } 89 } else if (ExtArrays.equals(buf,0,JFIF_SIG,0,4) && 90 ExtArrays.equals(buf,6,JFIF_SIG,6,4)) { 91 logger.debug("getImageSize: JPEG is not supported yet"); 92 return null; 93 } 94 logger.warn("Unrecognized signature"+ExtArrays.asList(buf)); 95 return null; 96 } else { 97 throw new RuntimeException ("getImageSize: not enough data"); 98 } 99 } 100 } 101 | Popular Tags |