Esempio n. 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) {
         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);
     }
   }
 }
Esempio n. 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;
       }
       this.format = FORMAT_IFF;
       this.width = getShortBigEndian(a, 0);
       this.height = getShortBigEndian(a, 2);
       this.bitsPerPixel = a[8] & 0xff;
       return (this.width > 0
           && this.height > 0
           && this.bitsPerPixel > 0
           && this.bitsPerPixel < 33);
     }
     skip(size);
   } while (true);
 }
Esempio n. 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;
   }
   this.format = FORMAT_GIF;
   this.width = getShortLittleEndian(a, 4);
   this.height = getShortLittleEndian(a, 6);
   int flags = a[8] & 0xff;
   this.bitsPerPixel = ((flags >> 4) & 0x07) + 1;
   this.progressive = (flags & 0x02) != 0;
   // skip global color palette
   if ((flags & 0x80) != 0) {
     this.numColors = (1 << ((flags & 7) + 1));
     skip(this.numColors * 3);
   }
   if (!this.determineNumberOfImages) {
     return true;
   }
   this.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;
           int localBitsPerPixel = (flags & 0x07) + 1;
           if (localBitsPerPixel > this.bitsPerPixel) {
             this.bitsPerPixel = localBitsPerPixel;
           }
           if ((flags & 0x80) != 0) {
             int localNumColors = 1 << localBitsPerPixel;
             skip(localNumColors * 3);
             if (localNumColors > this.numColors) {
               this.numColors = localNumColors;
             }
           }
           skip(1); // initial code length
           int n;
           do {
             n = read();
             if (n > 0) {
               skip(n);
             } else if (n == -1) {
               return false;
             }
           } while (n > 0);
           this.numberOfImages++;
           break;
         }
       case (0x21): // extension
         {
           int extensionType = read();
           if (this.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;
 }