FileFormatDetect.java 9.69 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.util.callbacks.Option
 *  com.scene7.is.util.collections.CollectionUtil
 *  com.scene7.is.util.collections.MapEntry
 *  org.jetbrains.annotations.NotNull
 */
package com.adobe.cq.dam.dm.process.image;

import com.adobe.cq.dam.dm.process.image.FileFormatEnum;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.collections.CollectionUtil;
import com.scene7.is.util.collections.MapEntry;
import java.util.Map;
import org.jetbrains.annotations.NotNull;

class FileFormatDetect {
    private static final FileFormat[] QUICK_FORMAT_TESTS = new FileFormat[]{new Bmp(), new Eps(), new Gif(), new Jpeg(), new Psd(), new Pict(), new Png(), new Tiff()};
    private static final Map<String, FileFormatEnum> QUICK_FORMAT_MIMETYPES = CollectionUtil.mapOf((MapEntry[])new MapEntry[]{MapEntry.mapEntry((Object)"image/bmp", (Object)((Object)FileFormatEnum.BMP)), MapEntry.mapEntry((Object)"image/eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"image/x-eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"application/postscript", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"application/eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"application/x-eps", (Object)((Object)FileFormatEnum.EPS)), MapEntry.mapEntry((Object)"image/gif", (Object)((Object)FileFormatEnum.GIF)), MapEntry.mapEntry((Object)"image/jpeg", (Object)((Object)FileFormatEnum.JPEG)), MapEntry.mapEntry((Object)"image/pjpeg", (Object)((Object)FileFormatEnum.JPEG)), MapEntry.mapEntry((Object)"image/photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/x-photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/psd", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"application/photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"application/psd", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/vnd.adobe.photoshop", (Object)((Object)FileFormatEnum.PSD)), MapEntry.mapEntry((Object)"image/pict", (Object)((Object)FileFormatEnum.PICT)), MapEntry.mapEntry((Object)"image/x-pict", (Object)((Object)FileFormatEnum.PICT)), MapEntry.mapEntry((Object)"image/png", (Object)((Object)FileFormatEnum.PNG)), MapEntry.mapEntry((Object)"image/tiff", (Object)((Object)FileFormatEnum.TIFF))});
    private static final int MAX_IMAGEFORMAT_HEADER_SIZE = FileFormatDetect.calcMaxHeaderSize();

    FileFormatDetect() {
    }

    public static int maxImageFormatHeaderSize() {
        return MAX_IMAGEFORMAT_HEADER_SIZE;
    }

    public static Option<FileFormatEnum> detectImageFormat(byte[] header, int size) {
        for (FileFormat t : QUICK_FORMAT_TESTS) {
            Option<FileFormatEnum> f = t.quickFormatTest(header, size);
            if (!f.isDefined()) continue;
            return f;
        }
        return Option.none();
    }

    public static Option<FileFormatEnum> detectImageFormat(@NotNull String mimeType) {
        return Option.some((Object)((Object)QUICK_FORMAT_MIMETYPES.get(mimeType)));
    }

    private static int calcMaxHeaderSize() {
        int maxsize = 0;
        for (FileFormat t : QUICK_FORMAT_TESTS) {
            maxsize = Math.max(maxsize, t.getMaxsize());
        }
        return maxsize;
    }

    private static int decodeU8(byte[] header, int offset) {
        return header[offset] & 255;
    }

    private static /* varargs */ boolean matches(byte[] header, int offset, int ... codes) {
        for (int i = 0; i < codes.length; ++i) {
            if (FileFormatDetect.decodeU8(header, offset + i) == codes[i]) continue;
            return false;
        }
        return true;
    }

    private static boolean matches(byte[] header, int offset, String s) {
        for (int i = 0; i < s.length(); ++i) {
            if (FileFormatDetect.decodeU8(header, offset + i) == s.charAt(i)) continue;
            return false;
        }
        return true;
    }

    private static class Tiff
    implements FileFormat {
        private static final int TIFF_HEADER_SIZE = 8;

        private Tiff() {
        }

        @Override
        public int getMaxsize() {
            return 8;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 8) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, new int[]{77, 77}) || FileFormatDetect.matches(header, 0, new int[]{73, 73})) {
                return Option.some((Object)((Object)FileFormatEnum.TIFF));
            }
            return Option.none();
        }
    }

    private static class Png
    implements FileFormat {
        private static final int PNG_HEADER_SIZE = 8;

        private Png() {
        }

        @Override
        public int getMaxsize() {
            return 8;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 8) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, new int[]{137, 80, 78, 71, 13, 10, 26, 10})) {
                return Option.some((Object)((Object)FileFormatEnum.PNG));
            }
            return Option.none();
        }
    }

    private static class Pict
    implements FileFormat {
        private static final int PICT_HEADER_SIZE = 532;

        private Pict() {
        }

        @Override
        public int getMaxsize() {
            return 532;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 532) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 522, new int[]{0, 17, 2, 255, 12, 0, 255})) {
                return Option.some((Object)((Object)FileFormatEnum.PICT));
            }
            return Option.none();
        }
    }

    private static class Psd
    implements FileFormat {
        private static final int PSD_HEADER_SIZE = 26;

        private Psd() {
        }

        @Override
        public int getMaxsize() {
            return 26;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 26) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, "8BPS")) {
                return Option.some((Object)((Object)FileFormatEnum.PSD));
            }
            return Option.none();
        }
    }

    private static class Jpeg
    implements FileFormat {
        private static final int JPEG_MIN_HEADER_SIZE = 5;
        private static final int JPEG_MAX_HEADER_SIZE = 256;

        private Jpeg() {
        }

        @Override
        public int getMaxsize() {
            return 256;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 5) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, new int[]{255, 216, 255})) {
                return Option.some((Object)((Object)FileFormatEnum.JPEG));
            }
            if (header[0] == 16 && header[4] == 16 ? FileFormatDetect.matches(header, 124, new int[]{255, 216, 255}) : FileFormatDetect.matches(header, 128, new int[]{255, 216, 255})) {
                return Option.some((Object)((Object)FileFormatEnum.JPEG));
            }
            return Option.none();
        }
    }

    private static class Gif
    implements FileFormat {
        private static final int GIF_HEADER_SIZE = 13;

        private Gif() {
        }

        @Override
        public int getMaxsize() {
            return 13;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 13) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, "GIF")) {
                return Option.some((Object)((Object)FileFormatEnum.GIF));
            }
            return Option.none();
        }
    }

    private static class Eps
    implements FileFormat {
        private static final int EPS_HEADER_SIZE = 11;
        private static final String EPS_HDR_MAGIC = "\u00c5\u00d0\u00d3\u00c6";

        private Eps() {
        }

        @Override
        public int getMaxsize() {
            return 11;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 11) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, "%!PS-Adobe-")) {
                return Option.some((Object)((Object)FileFormatEnum.EPS));
            }
            if (FileFormatDetect.matches(header, 0, "\u00c5\u00d0\u00d3\u00c6")) {
                return Option.some((Object)((Object)FileFormatEnum.EPS));
            }
            return Option.none();
        }
    }

    private static class Bmp
    implements FileFormat {
        private static final int BMP_HEADER_SIZE = 2;

        private Bmp() {
        }

        @Override
        public int getMaxsize() {
            return 2;
        }

        @Override
        public Option<FileFormatEnum> quickFormatTest(byte[] header, int size) {
            if (size < 2) {
                return Option.none();
            }
            if (FileFormatDetect.matches(header, 0, "BM")) {
                return Option.some((Object)((Object)FileFormatEnum.BMP));
            }
            return Option.none();
        }
    }

    private static interface FileFormat {
        public int getMaxsize();

        public Option<FileFormatEnum> quickFormatTest(byte[] var1, int var2);
    }

}