Ejemplo 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) {
         // 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);
     }
   }
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 private boolean checkBmp() throws IOException {
   byte[] a = new byte[44];
   if (read(a) != a.length) {
     return false;
   }
   width = getIntLittleEndian(a, 16);
   height = getIntLittleEndian(a, 20);
   if (width < 1 || height < 1) {
     return false;
   }
   bitsPerPixel = getShortLittleEndian(a, 26);
   if (bitsPerPixel != 1
       && bitsPerPixel != 4
       && bitsPerPixel != 8
       && bitsPerPixel != 16
       && bitsPerPixel != 24
       && bitsPerPixel != 32) {
     return false;
   }
   int x = (int) (getIntLittleEndian(a, 36) * 0.0254);
   if (x > 0) {
     setPhysicalWidthDpi(x);
   }
   int y = (int) (getIntLittleEndian(a, 40) * 0.0254);
   if (y > 0) {
     setPhysicalHeightDpi(y);
   }
   format = FORMAT_BMP;
   return true;
 }