in src/main/java/org/apache/commons/imaging/Imaging.java [703:813]
public static ImageFormat guessFormat(final ByteSource byteSource) throws IOException {
if (byteSource == null) {
return ImageFormats.UNKNOWN;
}
try (InputStream is = byteSource.getInputStream()) {
final int i1 = is.read();
final int i2 = is.read();
if (i1 < 0 || i2 < 0) {
throw new IllegalArgumentException("Couldn't read magic numbers to guess format.");
}
final int b1 = i1 & 0xff;
final int b2 = i2 & 0xff;
final int[] bytePair = { b1, b2, };
if (compareBytePair(MAGIC_NUMBERS_GIF, bytePair)) {
return ImageFormats.GIF;
// } else if (b1 == 0x00 && b2 == 0x00) // too similar to TGA
// {
// return ImageFormat.IMAGE_FORMAT_ICO;
}
if (compareBytePair(MAGIC_NUMBERS_PNG, bytePair)) {
return ImageFormats.PNG;
}
if (compareBytePair(MAGIC_NUMBERS_JPEG, bytePair)) {
return ImageFormats.JPEG;
}
if (compareBytePair(MAGIC_NUMBERS_BMP, bytePair)) {
return ImageFormats.BMP;
}
if (compareBytePair(MAGIC_NUMBERS_TIFF_MOTOROLA, bytePair)) {
return ImageFormats.TIFF;
}
if (compareBytePair(MAGIC_NUMBERS_TIFF_INTEL, bytePair)) {
return ImageFormats.TIFF;
}
if (compareBytePair(MAGIC_NUMBERS_PSD, bytePair)) {
return ImageFormats.PSD;
}
if (compareBytePair(MAGIC_NUMBERS_PAM, bytePair)) {
return ImageFormats.PAM;
}
if (compareBytePair(MAGIC_NUMBERS_PBM_A, bytePair)) {
return ImageFormats.PBM;
}
if (compareBytePair(MAGIC_NUMBERS_PBM_B, bytePair)) {
return ImageFormats.PBM;
}
if (compareBytePair(MAGIC_NUMBERS_PGM_A, bytePair)) {
return ImageFormats.PGM;
}
if (compareBytePair(MAGIC_NUMBERS_PGM_B, bytePair)) {
return ImageFormats.PGM;
}
if (compareBytePair(MAGIC_NUMBERS_PPM_A, bytePair)) {
return ImageFormats.PPM;
}
if (compareBytePair(MAGIC_NUMBERS_PPM_B, bytePair)) {
return ImageFormats.PPM;
}
if (compareBytePair(MAGIC_NUMBERS_JBIG2_1, bytePair)) {
final int i3 = is.read();
final int i4 = is.read();
if (i3 < 0 || i4 < 0) {
throw new IllegalArgumentException("Couldn't read magic numbers to guess format.");
}
final int b3 = i3 & 0xff;
final int b4 = i4 & 0xff;
final int[] bytePair2 = { b3, b4, };
if (compareBytePair(MAGIC_NUMBERS_JBIG2_2, bytePair2)) {
return ImageFormats.JBIG2;
}
} else if (compareBytePair(MAGIC_NUMBERS_ICNS, bytePair)) {
return ImageFormats.ICNS;
} else if (compareBytePair(MAGIC_NUMBERS_DCX, bytePair)) {
return ImageFormats.DCX;
} else if (compareBytePair(MAGIC_NUMBERS_RGBE, bytePair)) {
return ImageFormats.RGBE;
} else if (compareBytePair(MAGIC_NUMBERS_RIFF_1, bytePair)) {
final int i3 = is.read();
final int i4 = is.read();
if (i3 < 0 || i4 < 0) {
throw new IllegalArgumentException("Couldn't read magic numbers to guess format.");
}
final int b3 = i3 & 0xff;
final int b4 = i4 & 0xff;
final int[] bytePair2 = { b3, b4, };
if (compareBytePair(MAGIC_NUMBERS_RIFF_2, bytePair2)) {
final byte[] bytes = new byte[4];
if (is.read(bytes) < 4) { // Skip file size
throw new IllegalArgumentException("Couldn't read magic numbers to guess format.");
}
if (is.read(bytes) == 4 && Arrays.equals(MAGIC_NUMBERS_WEBP, bytes)) {
return ImageFormats.WEBP;
}
}
}
return Stream.of(ImageFormats.values()).filter(imageFormat -> Stream.of(imageFormat.getExtensions()).anyMatch(extension -> {
final String fileName = byteSource.getFileName();
if (StringUtils.isEmpty(fileName)) {
return false;
}
final String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
return fileExtension.equalsIgnoreCase(extension);
})).findFirst().orElse(ImageFormats.UNKNOWN);
}
}