1 50 51 package com.lowagie.text; 52 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.net.URL ; 56 57 64 65 public class Jpeg extends Image { 66 67 69 70 public static final int NOT_A_MARKER = -1; 71 72 73 public static final int VALID_MARKER = 0; 74 75 76 public static final int[] VALID_MARKERS = {0xC0, 0xC1, 0xC2}; 77 78 79 public static final int UNSUPPORTED_MARKER = 1; 80 81 82 public static final int[] UNSUPPORTED_MARKERS = {0xC3, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF}; 83 84 85 public static final int NOPARAM_MARKER = 2; 86 87 88 public static final int[] NOPARAM_MARKERS = {0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0x01}; 89 90 91 public static final int M_APP0 = 0xE0; 92 93 public static final int M_APPE = 0xEE; 94 95 96 public static final byte JFIF_ID[] = {0x4A, 0x46, 0x49, 0x46, 0x00}; 97 99 Jpeg(Image image) { 100 super(image); 101 } 102 103 110 public Jpeg(URL url) throws BadElementException, IOException { 111 super(url); 112 processParameters(); 113 } 114 115 122 123 public Jpeg(byte[] img) throws BadElementException, IOException { 124 super((URL )null); 125 rawData = img; 126 originalData = img; 127 processParameters(); 128 } 129 130 139 140 public Jpeg(byte[] img, float width, float height) throws BadElementException, IOException { 141 this(img); 142 scaledWidth = width; 143 scaledHeight = height; 144 } 145 146 148 155 private static final int getShort(InputStream is) throws IOException { 156 return (is.read() << 8) + is.read(); 157 } 158 159 165 private static final int marker(int marker) { 166 for (int i = 0; i < VALID_MARKERS.length; i++) { 167 if (marker == VALID_MARKERS[i]) { 168 return VALID_MARKER; 169 } 170 } 171 for (int i = 0; i < NOPARAM_MARKERS.length; i++) { 172 if (marker == NOPARAM_MARKERS[i]) { 173 return NOPARAM_MARKER; 174 } 175 } 176 for (int i = 0; i < UNSUPPORTED_MARKERS.length; i++) { 177 if (marker == UNSUPPORTED_MARKERS[i]) { 178 return UNSUPPORTED_MARKER; 179 } 180 } 181 return NOT_A_MARKER; 182 } 183 184 186 191 private void processParameters() throws BadElementException, IOException { 192 type = JPEG; 193 originalType = ORIGINAL_JPEG; 194 InputStream is = null; 195 try { 196 String errorID; 197 if (rawData == null){ 198 is = url.openStream(); 199 errorID = url.toString(); 200 } 201 else{ 202 is = new java.io.ByteArrayInputStream (rawData); 203 errorID = "Byte array"; 204 } 205 if (is.read() != 0xFF || is.read() != 0xD8) { 206 throw new BadElementException(errorID + " is not a valid JPEG-file."); 207 } 208 boolean firstPass = true; 209 int len; 210 while (true) { 211 int v = is.read(); 212 if (v < 0) 213 throw new IOException ("Premature EOF while reading JPG."); 214 if (v == 0xFF) { 215 int marker = is.read(); 216 if (firstPass && marker == M_APP0) { 217 firstPass = false; 218 len = getShort(is); 219 if (len < 16) { 220 Utilities.skip(is, len - 2); 221 continue; 222 } 223 byte bcomp[] = new byte[JFIF_ID.length]; 224 int r = is.read(bcomp); 225 if (r != bcomp.length) 226 throw new BadElementException(errorID + " corrupted JFIF marker."); 227 boolean found = true; 228 for (int k = 0; k < bcomp.length; ++k) { 229 if (bcomp[k] != JFIF_ID[k]) { 230 found = false; 231 break; 232 } 233 } 234 if (!found) { 235 Utilities.skip(is, len - 2 - bcomp.length); 236 continue; 237 } 238 Utilities.skip(is, 2); 239 int units = is.read(); 240 int dx = getShort(is); 241 int dy = getShort(is); 242 if (units == 1) { 243 dpiX = dx; 244 dpiY = dy; 245 } 246 else if (units == 2) { 247 dpiX = (int)((float)dx * 2.54f + 0.5f); 248 dpiY = (int)((float)dy * 2.54f + 0.5f); 249 } 250 Utilities.skip(is, len - 2 - bcomp.length - 7); 251 continue; 252 } 253 if (marker == M_APPE) { 254 len = getShort(is) - 2; 255 byte[] byteappe = new byte[len]; 256 for (int k = 0; k < len; ++k) { 257 byteappe[k] = (byte)is.read(); 258 } 259 if (byteappe.length >= 12) { 260 String appe = new String (byteappe, 0, 5, "ISO-8859-1"); 261 if (appe.equals("Adobe")) { 262 invert = true; 263 } 264 } 265 continue; 266 } 267 firstPass = false; 268 int markertype = marker(marker); 269 if (markertype == VALID_MARKER) { 270 Utilities.skip(is, 2); 271 if (is.read() != 0x08) { 272 throw new BadElementException(errorID + " must have 8 bits per component."); 273 } 274 scaledHeight = getShort(is); 275 setTop(scaledHeight); 276 scaledWidth = getShort(is); 277 setRight(scaledWidth); 278 colorspace = is.read(); 279 bpc = 8; 280 break; 281 } 282 else if (markertype == UNSUPPORTED_MARKER) { 283 throw new BadElementException(errorID + ": unsupported JPEG marker: " + marker); 284 } 285 else if (markertype != NOPARAM_MARKER) { 286 Utilities.skip(is, getShort(is) - 2); 287 } 288 } 289 } 290 } 291 finally { 292 if (is != null) { 293 is.close(); 294 } 295 plainWidth = getWidth(); 296 plainHeight = getHeight(); 297 } 298 } 299 } 300 | Popular Tags |