示例#1
0
 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
 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);
 }
示例#3
0
 private boolean checkGif() throws IOException {
   final byte[] GIF_MAGIC_87A = {0x46, 0x38, 0x37, 0x61};
   final byte[] GIF_MAGIC_89A = {0x46, 0x38, 0x39, 0x61};
   byte[] a = new byte[11]; // 4 from the GIF signature + 7 from the global header
   if (read(a) != 11) {
     return false;
   }
   if ((!equals(a, 0, GIF_MAGIC_89A, 0, 4)) && (!equals(a, 0, GIF_MAGIC_87A, 0, 4))) {
     return false;
   }
   format = FORMAT_GIF;
   width = getShortLittleEndian(a, 4);
   height = getShortLittleEndian(a, 6);
   int flags = a[8] & 0xff;
   bitsPerPixel = ((flags >> 4) & 0x07) + 1;
   // progressive = (flags & 0x02) != 0;
   if (!determineNumberOfImages) {
     return true;
   }
   // skip global color palette
   if ((flags & 0x80) != 0) {
     int tableSize = (1 << ((flags & 7) + 1)) * 3;
     skip(tableSize);
   }
   numberOfImages = 0;
   int blockType;
   do {
     blockType = read();
     switch (blockType) {
       case (0x2c): // image separator
         {
           if (read(a, 0, 9) != 9) {
             return false;
           }
           flags = a[8] & 0xff;
           progressive = (flags & 0x40) != 0;
           /*int locWidth = getShortLittleEndian(a, 4);
           int locHeight = getShortLittleEndian(a, 6);
           System.out.println("LOCAL: " + locWidth + " x " + locHeight);*/
           int localBitsPerPixel = (flags & 0x07) + 1;
           if (localBitsPerPixel > bitsPerPixel) {
             bitsPerPixel = localBitsPerPixel;
           }
           if ((flags & 0x80) != 0) {
             skip((1 << localBitsPerPixel) * 3);
           }
           skip(1); // initial code length
           int n;
           do {
             n = read();
             if (n > 0) {
               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;
 }