From b67d335e84d2c23cabcab546b630f57ccce8d9fb Mon Sep 17 00:00:00 2001 From: Argent77 Date: Sat, 24 Jan 2015 21:49:54 +0100 Subject: [PATCH] Enhanced BMP reader to support a wider variety of formats --- .../resource/graphics/BmpResource.java | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/src/infinity/resource/graphics/BmpResource.java b/src/infinity/resource/graphics/BmpResource.java index e0a80496a..bc712161a 100644 --- a/src/infinity/resource/graphics/BmpResource.java +++ b/src/infinity/resource/graphics/BmpResource.java @@ -18,6 +18,7 @@ import java.awt.event.ActionListener; import java.awt.image.BufferedImage; +import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComponent; @@ -36,45 +37,53 @@ public final class BmpResource implements Resource, ActionListener public BmpResource(ResourceEntry entry) throws Exception { this.entry = entry; - byte[] data = entry.getResourceData(); - new String(data, 0, 2); // Signature - DynamicArray.getInt(data, 2); // Size - DynamicArray.getInt(data, 6); // Reserved - int rasteroff = DynamicArray.getInt(data, 10); - - DynamicArray.getInt(data, 14); // Headersize - int width = DynamicArray.getInt(data, 18); - int height = DynamicArray.getInt(data, 22); - DynamicArray.getShort(data, 26); // Planes - int bitcount = (int)DynamicArray.getShort(data, 28); - int compression = DynamicArray.getInt(data, 30); - if ((compression == 0 || compression == 3) && bitcount <= 32) { - DynamicArray.getInt(data, 34); // Comprsize - DynamicArray.getInt(data, 38); // Xpixprm - DynamicArray.getInt(data, 42); // Ypixprm - int colsUsed = DynamicArray.getInt(data, 46); // Colorsused - DynamicArray.getInt(data, 50); // Colorsimp - - if (bitcount <= 8) { - if (colsUsed == 0) - colsUsed = 1 << bitcount; - int palSize = 4 * colsUsed; - palette = new Palette(data, rasteroff - palSize, palSize); - } - - int bytesprline = bitcount * width / 8; - int padded = 4 - bytesprline % 4; - if (padded == 4) - padded = 0; - - image = ColorConvert.createCompatibleImage(width, height, bitcount >= 32); - int offset = rasteroff; - for (int y = height - 1; y >= 0; y--) { - setPixels(data, offset, bitcount, bytesprline, y, palette); - offset += bytesprline + padded; + try { + image = ImageIO.read(entry.getResourceDataAsStream()); + } catch (Exception e) { + image = null; + } + if (image == null) { + byte[] data = entry.getResourceData(); + new String(data, 0, 2); // Signature + DynamicArray.getInt(data, 2); // Size + DynamicArray.getInt(data, 6); // Reserved + int rasteroff = DynamicArray.getInt(data, 10); + + DynamicArray.getInt(data, 14); // Headersize + int width = DynamicArray.getInt(data, 18); + int height = DynamicArray.getInt(data, 22); + DynamicArray.getShort(data, 26); // Planes + int bitcount = (int)DynamicArray.getShort(data, 28); + int compression = DynamicArray.getInt(data, 30); + if ((compression == 0 || compression == 3) && bitcount <= 32) { + DynamicArray.getInt(data, 34); // Comprsize + DynamicArray.getInt(data, 38); // Xpixprm + DynamicArray.getInt(data, 42); // Ypixprm + int colsUsed = DynamicArray.getInt(data, 46); // Colorsused + DynamicArray.getInt(data, 50); // Colorsimp + + if (bitcount <= 8) { + if (colsUsed == 0) + colsUsed = 1 << bitcount; + int palSize = 4 * colsUsed; + palette = new Palette(data, rasteroff - palSize, palSize); + } + + int bytesprline = bitcount * width / 8; + int padded = 4 - bytesprline % 4; + if (padded == 4) + padded = 0; + + image = ColorConvert.createCompatibleImage(width, height, bitcount >= 32); + int offset = rasteroff; + for (int y = height - 1; y >= 0; y--) { + setPixels(data, offset, bitcount, bytesprline, y, palette); + offset += bytesprline + padded; + } + } else { + throw new Exception(String.format("%1$s: Unsupported BMP format", entry.getResourceName())); } - } else - throw new Exception("Unsupported BMP format"); + } } // --------------------- Begin Interface ActionListener ---------------------