コード例 #1
0
ファイル: ImageInfo.java プロジェクト: helma-next/helma-next
 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);
     }
   }
 }
コード例 #2
0
ファイル: ImageInfo.java プロジェクト: helma-next/helma-next
 private boolean checkPnm(int id) throws IOException {
   if (id < 1 || id > 6) {
     return false;
   }
   final int[] PNM_FORMATS = {FORMAT_PBM, FORMAT_PGM, FORMAT_PPM};
   this.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 (this.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 {
         this.width = Integer.parseInt(widthString);
         this.height = Integer.parseInt(heightString);
       } catch (NumberFormatException nfe) {
         return false;
       }
       if (this.width < 1 || this.height < 1) {
         return false;
       }
       if (this.format == FORMAT_PBM) {
         this.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))) {
           this.bitsPerPixel = i + 1;
           if (this.format == FORMAT_PPM) {
             this.bitsPerPixel *= 3;
           }
           return true;
         }
       }
       return false;
     }
   }
 }