コード例 #1
0
ファイル: ImageInfo.java プロジェクト: johnnyshaw/util
 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);
     }
   }
 }
コード例 #2
0
ファイル: ImageInfo.java プロジェクト: johnnyshaw/util
 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;
     }
   }
 }