📄 imageinfo.java
字号:
skip(n); } else if (n == -1) { return false; } } while (n > 0); numberOfImages++; break; } case(0x21): // extension { int extensionType = read(); if (collectComments && extensionType == 0xfe) { StringBuffer sb = new StringBuffer(); int n; do { n = read(); if (n == -1) { return false; } if (n > 0) { for (int i = 0; i < n; i++) { int ch = read(); if (ch == -1) { return false; } sb.append((char)ch); } } } while (n > 0); } else { int n; do { n = read(); if (n > 0) { skip(n); } else if (n == -1) { return false; } } while (n > 0); } break; } case(0x3b): // end of file { break; } default: { return false; } } } while (blockType != 0x3b); return true; } private boolean checkIff() throws IOException { byte[] a = new byte[10]; // read remaining 2 bytes of file id, 4 bytes file size // and 4 bytes IFF subformat if (read(a, 0, 10) != 10) { return false; } final byte[] IFF_RM = {0x52, 0x4d}; if (!equals(a, 0, IFF_RM, 0, 2)) { return false; } int type = getIntBigEndian(a, 6); if (type != 0x494c424d && // type must be ILBM... type != 0x50424d20) { // ...or PBM return false; } // loop chunks to find BMHD chunk do { if (read(a, 0, 8) != 8) { return false; } int chunkId = getIntBigEndian(a, 0); int size = getIntBigEndian(a, 4); if ((size & 1) == 1) { size++; } if (chunkId == 0x424d4844) { // BMHD chunk if (read(a, 0, 9) != 9) { return false; } format = FORMAT_IFF; width = getShortBigEndian(a, 0); height = getShortBigEndian(a, 2); bitsPerPixel = a[8] & 0xff; return (width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel < 33); } else { skip(size); } } while (true); } 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) { // not an APPx header as we know it, skip skip(size - 2); continue; } 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 (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"); comment = comment.trim(); addComment(comment); } else if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4 && marker != 0xffc8) { if (read(data, 0, 6) != 6) { return false; } format = FORMAT_JPEG; bitsPerPixel = (data[0] & 0xff) * (data[5] & 0xff); progressive = marker == 0xffc2 || marker == 0xffc6 || marker == 0xffca || marker == 0xffce; width = getShortBigEndian(data, 3); 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; } width = x2 - x1 + 1; 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 bitsPerPixel = bits; } else if (planes == 3 && bits == 8) { // RGB truecolor bitsPerPixel = 24; } else { return false; } setPhysicalWidthDpi(getShortLittleEndian(a, 10)); setPhysicalHeightDpi(getShortLittleEndian(a, 10)); format = FORMAT_PCX; return true; } private boolean checkPng() throws IOException { final byte[] PNG_MAGIC = {0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; byte[] a = new byte[27]; if (read(a) != 27) { return false; } if (!equals(a, 0, PNG_MAGIC, 0, 6)) { return false; } format = FORMAT_PNG; width = getIntBigEndian(a, 14); height = getIntBigEndian(a, 18); bitsPerPixel = a[22] & 0xff; int colorType = a[23] & 0xff; if (colorType == 2 || colorType == 6) { bitsPerPixel *= 3; } progressive = (a[26] & 0xff) != 0; return true; } private boolean checkPnm(int id) throws IOException { if (id < 1 || id > 6) { return false; } final int[] PNM_FORMATS = {FORMAT_PBM, FORMAT_PGM, FORMAT_PPM}; format = PNM_FORMATS[(id - 1) % 3]; boolean hasPixelResolution = false; String s; while (true) { s = readLine(); if (s != null) { s = s.trim(); } if (s == null || s.length() < 1) { continue; } if (s.charAt(0) == '#') { // comment if (collectComments && s.length() > 1) { addComment(s.substring(1)); } continue; } if (!hasPixelResolution) { // split "343 966" into width=343, height=966 int spaceIndex = s.indexOf(' '); if (spaceIndex == -1) { return false; } String widthString = s.substring(0, spaceIndex); spaceIndex = s.lastIndexOf(' '); if (spaceIndex == -1) { return false; } String heightString = s.substring(spaceIndex + 1); try { width = Integer.parseInt(widthString); height = Integer.parseInt(heightString); } catch (NumberFormatException nfe) { return false; } if (width < 1 || height < 1) { return false; } if (format == FORMAT_PBM) { bitsPerPixel = 1; return true; } hasPixelResolution = true; } else { int maxSample; try { maxSample = Integer.parseInt(s); } catch (NumberFormatException nfe) { return false; } if (maxSample < 0) { return false; } for (int i = 0; i < 25; i++) { if (maxSample < (1 << (i + 1))) { bitsPerPixel = i + 1; if (format == FORMAT_PPM) { bitsPerPixel *= 3; } return true; } } return false; } } } private boolean checkPsd() throws IOException { byte[] a = new byte[24]; if (read(a) != a.length) { return false; } final byte[] PSD_MAGIC = {0x50, 0x53}; if (!equals(a, 0, PSD_MAGIC, 0, 2)) { return false; } format = FORMAT_PSD; width = getIntBigEndian(a, 16); height = getIntBigEndian(a, 12); int channels = getShortBigEndian(a, 10); int depth = getShortBigEndian(a, 20); bitsPerPixel = channels * depth; return (width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel <= 64); } private boolean checkRas() throws IOException { byte[] a = new byte[14]; if (read(a) != a.length) { return false; } final byte[] RAS_MAGIC = {0x6a, (byte)0x95}; if (!equals(a, 0, RAS_MAGIC, 0, 2)) { return false; } format = FORMAT_RAS; width = getIntBigEndian(a, 2); height = getIntBigEndian(a, 6); bitsPerPixel = getIntBigEndian(a, 10); return (width > 0 && height > 0 && bitsPerPixel > 0 && bitsPerPixel <= 24); } /** * Run over String list, return false iff at least one of the arguments * equals <code>-c</code>. * @param args string list to check */ private static boolean determineVerbosity(String[] args) { if (args != null && args.length > 0) { for (int i = 0; i < args.length; i++) { if ("-c".equals(args[i])) { return false; } } } return true; } private static boolean equals(byte[] a1, int offs1, byte[] a2, int offs2, int num) { while (num-- > 0) { if (a1[offs1++] != a2[offs2++]) { return false; } } return true; } /** * If {@link #check()} was successful, returns the image's number of bits per pixel. * Does not include transparency information like the alpha channel. * @return number of bits per image pixel */ public int getBitsPerPixel() { return bitsPerPixel; } /** * Returns the index'th comment retrieved from the file. * @param index int index of comment to return * @throws IllegalArgumentException if index is smaller than 0 or larger than or equal * to the number of comments retrieved * @see #getNumberOfComments */ public String getComment(int index) { if (comments == null || index < 0 || index >= comments.size()) { throw new IllegalArgumentException("Not a valid comment index: " + index); } return (String)comments.elementAt(index); } /** * If {@link #check()} was successful, returns the image format as one * of the FORMAT_xyz constants from this class. * Use {@link #getFormatName()} to get a textual description of the file format. * @return file format as a FORMAT_xyz constant */ public int getFormat() { return format; } /** * If {@link #check()} was successful, returns the image format's name. * Use {@link #getFormat()} to get a unique number. * @return file format name */ public String getFormatName() { if (format >= 0 && format < FORMAT_NAMES.length) { return FORMAT_NAMES[format]; } else { return "?"; } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -