private boolean checkJpeg() throws IOException { byte[] data = new byte[12]; while (true) { if (read(data, 0, 4) != 4) { return false; } int marker = getShortBigEndian(data, 0); int size = getShortBigEndian(data, 2); if ((marker & 0xff00) != 0xff00) { return false; // not a valid marker } if (marker == 0xffe0) { // APPx if (size < 14) { return false; // APPx header must be >= 14 bytes } if (read(data, 0, 12) != 12) { return false; } final byte[] APP0_ID = {0x4a, 0x46, 0x49, 0x46, 0x00}; if (equals(APP0_ID, 0, data, 0, 5)) { // System.out.println("data 7=" + data[7]); if (data[7] == 1) { setPhysicalWidthDpi(getShortBigEndian(data, 8)); setPhysicalHeightDpi(getShortBigEndian(data, 10)); } else if (data[7] == 2) { int x = getShortBigEndian(data, 8); int y = getShortBigEndian(data, 10); setPhysicalWidthDpi((int) (x * 2.54f)); setPhysicalHeightDpi((int) (y * 2.54f)); } } skip(size - 14); } else if (this.collectComments && size > 2 && marker == 0xfffe) { // comment size -= 2; byte[] chars = new byte[size]; if (read(chars, 0, size) != size) { return false; } String comment = new String(chars, "iso-8859-1"); // $NON-NLS-1$ comment = comment.trim(); addComment(comment); } else if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4 && marker != 0xffc8) { if (read(data, 0, 6) != 6) { return false; } this.format = FORMAT_JPEG; this.bitsPerPixel = (data[0] & 0xff) * (data[5] & 0xff); this.progressive = marker == 0xffc2 || marker == 0xffc6 || marker == 0xffca || marker == 0xffce; this.width = getShortBigEndian(data, 3); this.height = getShortBigEndian(data, 1); return true; } else { skip(size - 2); } } }
private boolean checkPcx() throws IOException { byte[] a = new byte[64]; if (read(a) != a.length) { return false; } if (a[0] != 1) { // encoding, 1=RLE is only valid value return false; } // width / height int x1 = getShortLittleEndian(a, 2); int y1 = getShortLittleEndian(a, 4); int x2 = getShortLittleEndian(a, 6); int y2 = getShortLittleEndian(a, 8); if (x1 < 0 || x2 < x1 || y1 < 0 || y2 < y1) { return false; } this.width = x2 - x1 + 1; this.height = y2 - y1 + 1; // color depth int bits = a[1]; int planes = a[63]; if (planes == 1 && (bits == 1 || bits == 2 || bits == 4 || bits == 8)) { // paletted this.bitsPerPixel = bits; } else if (planes == 3 && bits == 8) { // RGB truecolor this.bitsPerPixel = 24; } else { return false; } setPhysicalWidthDpi(getShortLittleEndian(a, 10)); setPhysicalHeightDpi(getShortLittleEndian(a, 10)); this.format = FORMAT_PCX; return true; }
private boolean checkBmp() throws IOException { byte[] a = new byte[44]; if (read(a) != a.length) { return false; } this.width = getIntLittleEndian(a, 16); this.height = getIntLittleEndian(a, 20); if (this.width < 1 || this.height < 1) { return false; } this.bitsPerPixel = getShortLittleEndian(a, 26); if (this.bitsPerPixel != 1 && this.bitsPerPixel != 4 && this.bitsPerPixel != 8 && this.bitsPerPixel != 16 && this.bitsPerPixel != 24 && this.bitsPerPixel != 32) { return false; } int x = (int) (getIntLittleEndian(a, 36) * 0.0254); if (x > 0) { setPhysicalWidthDpi(x); } int y = (int) (getIntLittleEndian(a, 40) * 0.0254); if (y > 0) { setPhysicalHeightDpi(y); } this.format = FORMAT_BMP; return true; }
// Written by Michael Aird. private boolean checkSwf() throws IOException { // get rid of the last byte of the signature, the byte of the version and 4 bytes of the size byte[] a = new byte[6]; if (read(a) != a.length) { return false; } format = FORMAT_SWF; int bitSize = (int) readUBits(5); int minX = (int) readSBits(bitSize); int maxX = (int) readSBits(bitSize); int minY = (int) readSBits(bitSize); int maxY = (int) readSBits(bitSize); width = maxX / 20; // cause we're in twips height = maxY / 20; // cause we're in twips setPhysicalWidthDpi(72); setPhysicalHeightDpi(72); return (width > 0 && height > 0); }