1 16 package org.apache.cocoon.util; 17 18 import java.io.File ; 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 import java.io.BufferedInputStream ; 22 import java.io.FileInputStream ; 23 24 29 final public class ImageUtils { 30 31 final public static ImageProperties getImageProperties(File file) throws FileNotFoundException , IOException , FileFormatException { 32 String type = MIMEUtils.getMIMEType(file); 33 34 if ("image/gif".equals(type)) { 35 return (getGifProperties(file)); 36 } 37 else if ("image/jpeg".equals(type)) { 38 return (getJpegProperties(file)); 39 } 40 else { 41 return (null); 42 } 43 } 44 45 final public static ImageProperties getJpegProperties(File file) throws FileNotFoundException , IOException , FileFormatException { 46 BufferedInputStream in = null; 47 try { 48 in = new BufferedInputStream (new FileInputStream (file)); 49 50 byte[] buf = new byte[2]; 52 int count = in.read(buf, 0, 2); 53 if (count < 2) { 54 throw new FileFormatException("Not a valid Jpeg file!"); 55 } 56 if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) { 57 throw new FileFormatException("Not a valid Jpeg file!"); 58 } 59 60 int width = 0; 61 int height = 0; 62 char[] comment = null; 63 64 boolean hasDims = false; 65 boolean hasComment = false; 66 int ch = 0; 67 68 while (ch != 0xDA && !(hasDims && hasComment)) { 69 70 while (ch != 0xFF) { 71 ch = in.read(); 72 } 73 74 while (ch == 0xFF) { 75 ch = in.read(); 76 } 77 78 79 int length = 256 * in.read(); 80 length += in.read(); 81 if (length < 2) { 82 throw new FileFormatException("Not a valid Jpeg file!"); 83 } 84 85 86 if (ch >= 0xC0 && ch <= 0xC3) { 87 in.read(); 88 height = 256 * in.read(); 89 height += in.read(); 90 width = 256 * in.read(); 91 width += in.read(); 92 for (int foo = 0; foo < length - 2 - 5; foo++) { 93 in.read(); 94 } 95 hasDims = true; 96 } 97 else if (ch == 0xFE) { 98 comment = new char[length-2]; 100 for (int foo = 0; foo < length - 2; foo++) 101 comment[foo] = (char) in.read(); 102 hasComment = true; 103 } 104 else { 105 for (int foo = 0; foo < length - 2; foo++) { 107 in.read(); 108 } 109 } 110 } 111 return (new ImageProperties(width, height, comment, "jpeg")); 112 113 } 114 finally { 115 if (in != null) { 116 try { 117 in.close(); 118 } 119 catch (IOException e) { 120 } 121 } 122 } 123 } 124 125 final public static ImageProperties getGifProperties(File file) throws FileNotFoundException , IOException , FileFormatException { 126 BufferedInputStream in = null; 127 try { 128 in = new BufferedInputStream (new FileInputStream (file)); 129 byte[] buf = new byte[10]; 130 int count = in.read(buf, 0, 10); 131 if (count < 10) { 132 throw new FileFormatException("Not a valid Gif file!"); 133 } 134 if ((buf[0]) != (byte) 'G' || (buf[1]) != (byte) 'I' || (buf[2]) != (byte) 'F') { 135 throw new FileFormatException("Not a valid Gif file!"); 136 } 137 138 int w1 = (buf[6] & 0xff) | (buf[6] & 0x80); 139 int w2 = (buf[7] & 0xff) | (buf[7] & 0x80); 140 int h1 = (buf[8] & 0xff) | (buf[8] & 0x80); 141 int h2 = (buf[9] & 0xff) | (buf[9] & 0x80); 142 143 int width = w1 + (w2 << 8); 144 int height = h1 + (h2 << 8); 145 146 return (new ImageProperties(width, height, null,"gif")); 147 148 } 149 finally { 150 if (in != null) { 151 try { 152 in.close(); 153 } 154 catch (IOException e) { 155 } 156 } 157 } 158 } 159 } 160 | Popular Tags |