Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.56 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class SimpleImageInfo {
  4.     private final int _width;
  5.     private final int _height;
  6.     private final String type;
  7.  
  8.     private SimpleImageInfo(final int width, final int height, final String type) {
  9.         _width = width;
  10.         _height = height;
  11.         this.type = type;
  12.     }
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return "SimpleImageInfo{" + "_width=" + _width + ", _height=" + _height + ", type='" + type + '\'' + '}';
  17.     }
  18.  
  19.     @Override
  20.     public boolean equals(final Object o) {
  21.         if (this == o) return true;
  22.         if (!(o instanceof SimpleImageInfo)) return false;
  23.  
  24.         final SimpleImageInfo that = (SimpleImageInfo) o;
  25.  
  26.         if (_width != that._width) return false;
  27.         if (_height != that._height) return false;
  28.         return type != null ? type.equals(that.type) : that.type == null;
  29.  
  30.     }
  31.  
  32.     @Override
  33.     public int hashCode() {
  34.         int result = _width;
  35.         result = 31 * result + _height;
  36.         result = 31 * result + (type != null ? type.hashCode() : 0);
  37.         return result;
  38.     }
  39.  
  40.     public int getWidth() {
  41.         return _width;
  42.     }
  43.  
  44.     public int getHeight() {
  45.         return _height;
  46.     }
  47.  
  48.     public String getType() {
  49.         return type;
  50.     }
  51.  
  52.     private interface StreamSupplier {
  53.         int read() throws IOException;
  54.  
  55.         long skip(long skip) throws IOException;
  56.     }
  57.  
  58.     public static SimpleImageInfo getInfo(File file, File target) throws IOException {
  59.         final InputStream is = new FileInputStream(file);
  60.         try {
  61.             return getInfo(is, target);
  62.         } finally {
  63.             is.close();
  64.         }
  65.     }
  66.  
  67.     public static SimpleImageInfo getInfo(InputStream inputStream, File target) throws IOException {
  68.         final OutputStream outputStream = new FileOutputStream(target);
  69.         try {
  70.             return getInfo(inputStream, outputStream);
  71.         } finally {
  72.             outputStream.close();
  73.         }
  74.     }
  75.  
  76.     public static SimpleImageInfo getInfo(byte[] bytes, OutputStream outputStream) throws IOException {
  77.         final InputStream is = new ByteArrayInputStream(bytes);
  78.         try {
  79.             return getInfo(is, outputStream);
  80.         } finally {
  81.             is.close();
  82.         }
  83.     }
  84.  
  85.     public static SimpleImageInfo getInfo(final InputStream is, final OutputStream outputStream) throws IOException {
  86.         final SimpleImageInfo info = getInfo(new StreamSupplier() {
  87.             @Override
  88.             public int read() throws IOException {
  89.                 final int read = is.read();
  90.                 outputStream.write(read);
  91.                 return read;
  92.             }
  93.  
  94.             @Override
  95.             public long skip(final long n) throws IOException {
  96.  
  97.                 if (n <= 0) {
  98.                     return 0;
  99.                 }
  100.  
  101.                 long remaining = n;
  102.                 final int size = (int) Math.min(2048, remaining);
  103.                 final byte[] skipBuffer = new byte[size];
  104.                 while (remaining > 0) {
  105.                     final int length = (int) Math.min(size, remaining);
  106.                     final int nr = is.read(skipBuffer, 0, length);
  107.  
  108.                     if (nr < 0) {
  109.                         break;
  110.                     }
  111.                     outputStream.write(skipBuffer, 0, nr);
  112.                     remaining -= nr;
  113.                 }
  114.  
  115.                 return n - remaining;
  116.             }
  117.         });
  118.  
  119.         final byte[] buffer = new byte[2048];
  120.         int n;
  121.         while (-1 != (n = is.read(buffer))) {
  122.             outputStream.write(buffer, 0, n);
  123.         }
  124.         return info;
  125.     }
  126.  
  127.     public static SimpleImageInfo getInfo(File file) throws IOException {
  128.         final InputStream is = new FileInputStream(file);
  129.         try {
  130.             return getInfo(is);
  131.         } finally {
  132.             is.close();
  133.         }
  134.     }
  135.  
  136.     public static SimpleImageInfo getInfo(byte[] bytes) throws IOException {
  137.         final InputStream is = new ByteArrayInputStream(bytes);
  138.         try {
  139.             return getInfo(is);
  140.         } finally {
  141.             is.close();
  142.         }
  143.     }
  144.  
  145.  
  146.     /**
  147.      * Based on SimplaImageInfo class from  Jaimon Mathew <http://www.jaimon.co.uk>
  148.      * http://blog.jaimon.co.uk/simpleimageinfo/SimpleImageInfo.java.html
  149.      */
  150.     public static SimpleImageInfo getInfo(final InputStream is) throws IOException {
  151.         return getInfo(new StreamSupplier() {
  152.             @Override
  153.             public int read() throws IOException {
  154.                 return is.read();
  155.             }
  156.  
  157.             @Override
  158.             public long skip(final long skip) throws IOException {
  159.                 return is.skip(skip);
  160.             }
  161.         });
  162.     }
  163.  
  164.  
  165.     private static SimpleImageInfo getInfo(StreamSupplier is) throws IOException {
  166.         final int h1 = is.read();
  167.         final int h2 = is.read();
  168.         if (h1 == 0xFF && h2 == 0xD8) {   //JPG
  169.             return getJPG(is);
  170.         } else if (h1 == 66 && h2 == 77) { // BMP
  171.             return getBMP(is);
  172.         } else {
  173.             final int h3 = is.read();
  174.             if (h1 == 137 && h2 == 80 && h3 == 78) { // PNG
  175.                 return getPNG(is);
  176.             } else if (h1 == 'G' && h2 == 'I' && h3 == 'F') { // GIF
  177.                 return getGIF(is);
  178.             } else {
  179.                 final int c4 = is.read();
  180.                 if ((h1 == 'M' && h2 == 'M' && h3 == 0 && c4 == 42) || (h1 == 'I' && h2 == 'I' && h3 == 42 && c4 == 0)) { //TIFF
  181.                     final boolean bigEndian = h1 == 'M';
  182.                     return getTIFF(is, bigEndian);
  183.                 } else {
  184.                     throw new IOException("Unsupported Image Type");
  185.                 }
  186.             }
  187.         }
  188.     }
  189.  
  190.  
  191.     private static SimpleImageInfo getJPG(final StreamSupplier is) throws IOException {
  192.         int currentRead = is.read();
  193.         while (currentRead == 255) {
  194.             final int marker = is.read();
  195.             final int len = readInt(is, 2, true);
  196.             if (marker == 192 || marker == 193 || marker == 194) {
  197.                 is.skip(1);
  198.                 final int height = readInt(is, 2, true);
  199.                 final int width = readInt(is, 2, true);
  200.                 return new SimpleImageInfo(width, height, "JPG");
  201.             } else {
  202.                 is.skip(len - 2);
  203.                 currentRead = is.read();
  204.             }
  205.         }
  206.         throw new IOException("Unsupported Jpg format");
  207.     }
  208.  
  209.     private static SimpleImageInfo getTIFF(final StreamSupplier is, final boolean bigEndian) throws IOException {
  210.         int height = -1;
  211.         int width = -1;
  212.         final int ifd = readInt(is, 4, bigEndian);
  213.         is.skip(ifd - 8);
  214.         final int entries = readInt(is, 2, bigEndian);
  215.         for (int i = 1; i <= entries; i++) {
  216.             final int tag = readInt(is, 2, bigEndian);
  217.             final int fieldType = readInt(is, 2, bigEndian);
  218.             final long count = readInt(is, 4, bigEndian);
  219.             final int valOffset;
  220.             if ((fieldType == 3 || fieldType == 8)) {
  221.                 valOffset = readInt(is, 2, bigEndian);
  222.                 is.skip(2);
  223.             } else {
  224.                 valOffset = readInt(is, 4, bigEndian);
  225.             }
  226.  
  227.             if (tag == 256) {
  228.                 width = valOffset;
  229.             } else if (tag == 257) {
  230.                 height = valOffset;
  231.             }
  232.             if (width != -1 && height != -1) {
  233.                 return new SimpleImageInfo(width, height, "TIF");
  234.             }
  235.         }
  236.         throw new IOException("Unsupported Tiff Format");
  237.     }
  238.  
  239.     private static SimpleImageInfo getBMP(final StreamSupplier is) throws IOException {
  240.         is.skip(16);
  241.         final int width = readInt(is, 2, false);
  242.         is.skip(2);
  243.         final int height = readInt(is, 2, false);
  244.         return new SimpleImageInfo(width, height, "BMP");
  245.     }
  246.  
  247.     private static SimpleImageInfo getPNG(final StreamSupplier is) throws IOException {
  248.         is.skip(15);
  249.         final int width = readInt(is, 2, true);
  250.         is.skip(2);
  251.         final int height = readInt(is, 2, true);
  252.         return new SimpleImageInfo(width, height, "PNG");
  253.     }
  254.  
  255.     private static SimpleImageInfo getGIF(final StreamSupplier is) throws IOException {
  256.         is.skip(3);
  257.         final int width = readInt(is, 2, false);
  258.         final int height = readInt(is, 2, false);
  259.         return new SimpleImageInfo(width, height, "GIF");
  260.     }
  261.  
  262.  
  263.     private static int readInt(StreamSupplier is, int noOfBytes, boolean bigEndian) throws IOException {
  264.         int result = 0;
  265.         int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0;
  266.         final int cnt = bigEndian ? -8 : 8;
  267.         for (int i = 0; i < noOfBytes; i++) {
  268.             result |= is.read() << sv;
  269.             sv += cnt;
  270.         }
  271.         return result;
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement