From d4721919268851e0fd4ad302ae7e48f9715a8f7e Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Wed, 19 Jul 2023 14:04:27 +0200 Subject: [PATCH] BMP cleanup --- .../imageio/plugins/bmp/BMPImageReader.java | 27 +- .../plugins/bmp/BMPImageReaderSpi.java | 16 +- .../imageio/plugins/bmp/BMPImageWriter.java | 2 +- .../imageio/plugins/bmp/BMPMetadata.java | 3 +- .../imageio/plugins/bmp/BitmapDescriptor.java | 14 +- .../imageio/plugins/bmp/BitmapIndexed.java | 36 +- .../imageio/plugins/bmp/BitmapMask.java | 16 +- .../imageio/plugins/bmp/BitmapRGB.java | 11 +- .../plugins/bmp/BitmapUnsupported.java | 15 +- .../imageio/plugins/bmp/CURImageReader.java | 10 +- .../plugins/bmp/CURImageReaderSpi.java | 9 +- .../imageio/plugins/bmp/DIBHeader.java | 274 ++++++++++------ .../imageio/plugins/bmp/DIBImageReader.java | 309 +++++++++--------- .../imageio/plugins/bmp/Directory.java | 19 +- .../imageio/plugins/bmp/DirectoryEntry.java | 43 ++- .../imageio/plugins/bmp/ICOImageReader.java | 4 +- .../plugins/bmp/ICOImageReaderSpi.java | 21 +- .../imageio/plugins/bmp/ICOImageWriter.java | 16 +- .../plugins/bmp/BMPImageReaderTest.java | 55 ++-- .../imageio/util/ImageReaderAbstractTest.java | 83 ++--- .../imageio/util/ImageWriterAbstractTest.java | 12 +- 21 files changed, 527 insertions(+), 468 deletions(-) diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReader.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReader.java index 0dd70fef..26cb1e3c 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReader.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReader.java @@ -81,8 +81,8 @@ public final class BMPImageReader extends ImageReaderBase { super(new BMPImageReaderSpi()); } - BMPImageReader(final ImageReaderSpi pProvider) { - super(pProvider); + BMPImageReader(final ImageReaderSpi provider) { + super(provider); } @Override @@ -129,6 +129,7 @@ public final class BMPImageReader extends ImageReaderBase { // Read DIB header header = DIBHeader.read(imageInput); +// System.out.println("header = " + header); if (pixelOffset < header.size + DIB.BMP_FILE_HEADER_SIZE) { throw new IIOException("Invalid pixel offset: " + pixelOffset); @@ -186,30 +187,30 @@ public final class BMPImageReader extends ImageReaderBase { } @Override - public int getWidth(int pImageIndex) throws IOException { - checkBounds(pImageIndex); + public int getWidth(int imageIndex) throws IOException { + checkBounds(imageIndex); return header.getWidth(); } @Override - public int getHeight(int pImageIndex) throws IOException { - checkBounds(pImageIndex); + public int getHeight(int imageIndex) throws IOException { + checkBounds(imageIndex); return header.getHeight(); } @Override - public Iterator getImageTypes(int pImageIndex) throws IOException { - checkBounds(pImageIndex); + public Iterator getImageTypes(int imageIndex) throws IOException { + checkBounds(imageIndex); // TODO: Better implementation, include INT_RGB types for 3BYTE_BGR and 4BYTE_ABGR for INT_ARGB - return Collections.singletonList(getRawImageType(pImageIndex)).iterator(); + return Collections.singletonList(getRawImageType(imageIndex)).iterator(); } @Override - public ImageTypeSpecifier getRawImageType(int pImageIndex) throws IOException { - checkBounds(pImageIndex); + public ImageTypeSpecifier getRawImageType(int imageIndex) throws IOException { + checkBounds(imageIndex); if (header.getPlanes() != 1) { throw new IIOException("Multiple planes not supported"); @@ -685,8 +686,8 @@ public final class BMPImageReader extends ImageReaderBase { } @SuppressWarnings({ "unchecked", "UnusedDeclaration", "SameParameterValue" }) - static void throwAs(final Class pType, final Throwable pThrowable) throws T { - throw (T) pThrowable; + static void throwAs(final Class type, final Throwable throwable) throws T { + throw (T) throwable; } private class ListenerDelegator extends ProgressListenerBase implements IIOReadUpdateListener, IIOReadWarningListener { diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderSpi.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderSpi.java index 159eedbc..7463fae6 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderSpi.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderSpi.java @@ -65,16 +65,16 @@ public final class BMPImageReaderSpi extends ImageReaderSpiBase { } } - public boolean canDecodeInput(final Object pSource) throws IOException { - return pSource instanceof ImageInputStream && canDecode((ImageInputStream) pSource); + public boolean canDecodeInput(final Object source) throws IOException { + return source instanceof ImageInputStream && canDecode((ImageInputStream) source); } - private static boolean canDecode(final ImageInputStream pInput) throws IOException { + private static boolean canDecode(final ImageInputStream input) throws IOException { byte[] fileHeader = new byte[18]; // Strictly: file header (14 bytes) + BMP header size field (4 bytes) try { - pInput.mark(); - pInput.readFully(fileHeader); + input.mark(); + input.readFully(fileHeader); // Magic: BM if (fileHeader[0] != 'B' || fileHeader[1] != 'M') { @@ -112,15 +112,15 @@ public final class BMPImageReaderSpi extends ImageReaderSpiBase { } } finally { - pInput.reset(); + input.reset(); } } - public ImageReader createReaderInstance(final Object pExtension) { + public ImageReader createReaderInstance(final Object extension) { return new BMPImageReader(this); } - public String getDescription(final Locale pLocale) { + public String getDescription(final Locale locale) { return "Windows Device Independent Bitmap Format (BMP) Reader"; } } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageWriter.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageWriter.java index 3a2b9961..1eb21941 100644 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageWriter.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageWriter.java @@ -47,7 +47,7 @@ import java.nio.ByteOrder; * BMPImageWriter */ public final class BMPImageWriter extends DIBImageWriter { - protected BMPImageWriter(ImageWriterSpi provider) { + BMPImageWriter(ImageWriterSpi provider) { super(provider); } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPMetadata.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPMetadata.java index 8f7bb80a..fd98179d 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPMetadata.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BMPMetadata.java @@ -32,6 +32,7 @@ package com.twelvemonkeys.imageio.plugins.bmp; import com.twelvemonkeys.imageio.AbstractMetadata; import com.twelvemonkeys.lang.Validate; + import org.w3c.dom.Node; import javax.imageio.metadata.IIOMetadataNode; @@ -141,7 +142,7 @@ final class BMPMetadata extends AbstractMetadata { @Override protected IIOMetadataNode getStandardChromaNode() { // NOTE: BMP files may contain a color map, even if true color... - // Not sure if this is a good idea to expose to the meta data, + // Not sure if this is a good idea to expose to the metadata, // as it might be unexpected... Then again... if (colorMap != null) { IIOMetadataNode chroma = new IIOMetadataNode("Chroma"); diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapDescriptor.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapDescriptor.java index ff8e80b7..862cea3e 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapDescriptor.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapDescriptor.java @@ -29,11 +29,11 @@ */ package com.twelvemonkeys.imageio.plugins.bmp; -import static com.twelvemonkeys.lang.Validate.notNull; - -import java.awt.image.BufferedImage; +import java.awt.image.*; import java.io.IOException; +import static com.twelvemonkeys.lang.Validate.notNull; + /** * Describes a bitmap structure. * @@ -47,9 +47,9 @@ abstract class BitmapDescriptor { protected BufferedImage image; protected BitmapMask mask; - public BitmapDescriptor(final DirectoryEntry pEntry, final DIBHeader pHeader) { - entry = notNull(pEntry, "entry");; - header = notNull(pHeader, "header"); + public BitmapDescriptor(final DirectoryEntry entry, final DIBHeader header) { + this.entry = notNull(entry, "entry"); + this.header = notNull(header, "header"); } abstract public BufferedImage getImage() throws IOException; @@ -75,7 +75,7 @@ abstract class BitmapDescriptor { return getClass().getSimpleName() + "[" + entry + ", " + header + "]"; } - public final void setMask(final BitmapMask mask) { + final void setMask(final BitmapMask mask) { this.mask = mask; } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapIndexed.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapIndexed.java index 0d6417af..70a320f4 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapIndexed.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapIndexed.java @@ -29,10 +29,7 @@ */ package com.twelvemonkeys.imageio.plugins.bmp; -import java.awt.image.BufferedImage; -import java.awt.image.DataBuffer; -import java.awt.image.IndexColorModel; -import java.awt.image.WritableRaster; +import java.awt.image.*; import java.util.Hashtable; /** @@ -41,12 +38,13 @@ import java.util.Hashtable; * @author Harald Kuhr * @version $Id: BitmapIndexed.java,v 1.0 25.feb.2006 00:29:44 haku Exp$ */ -class BitmapIndexed extends BitmapDescriptor { - protected final int[] bits; - protected final int[] colors; +final class BitmapIndexed extends BitmapDescriptor { + final int[] bits; + final int[] colors; + + public BitmapIndexed(final DirectoryEntry entry, final DIBHeader header) { + super(entry, header); - public BitmapIndexed(final DirectoryEntry pEntry, final DIBHeader pHeader) { - super(pEntry, pHeader); bits = new int[getWidth() * getHeight()]; // NOTE: We're adding space for one extra color, for transparency @@ -59,20 +57,16 @@ class BitmapIndexed extends BitmapDescriptor { IndexColorModel icm = createColorModel(); - // This is slightly obscure, and should probably be moved.. + // We add cursor hotspot as a property to images created from CUR format. + // This is slightly obscure, and should probably be moved... Hashtable properties = null; if (entry instanceof DirectoryEntry.CUREntry) { properties = new Hashtable<>(1); properties.put("cursor_hotspot", ((DirectoryEntry.CUREntry) this.entry).getHotspot()); } - BufferedImage image = new BufferedImage( - icm, - icm.createCompatibleWritableRaster(getWidth(), getHeight()), - icm.isAlphaPremultiplied(), properties - ); - - WritableRaster raster = image.getRaster(); + WritableRaster raster = icm.createCompatibleWritableRaster(getWidth(), getHeight()); + BufferedImage image = new BufferedImage(icm, raster, icm.isAlphaPremultiplied(), properties); // Make pixels transparent according to mask final int trans = icm.getTransparentPixel(); @@ -105,7 +99,7 @@ class BitmapIndexed extends BitmapDescriptor { int index = findTransparentIndexMaybeRemap(this.colors, this.bits); if (index == -1) { - // No duplicate found, increase bitcount + // No duplicate found, increase bit count bits++; transparent = this.colors.length - 1; } @@ -117,10 +111,8 @@ class BitmapIndexed extends BitmapDescriptor { } // NOTE: Setting hasAlpha to true, makes things work on 1.2 - return new IndexColorModel( - bits, colors, this.colors, 0, true, transparent, - bits <= 8 ? DataBuffer.TYPE_BYTE : DataBuffer.TYPE_USHORT - ); + return new IndexColorModel(bits, colors, this.colors, 0, true, transparent, + bits <= 8 ? DataBuffer.TYPE_BYTE : DataBuffer.TYPE_USHORT); } private static int findTransparentIndexMaybeRemap(final int[] colors, final int[] bits) { diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapMask.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapMask.java index 526c3dab..24fadbb7 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapMask.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapMask.java @@ -30,7 +30,7 @@ package com.twelvemonkeys.imageio.plugins.bmp; -import java.awt.image.BufferedImage; +import java.awt.image.*; /** @@ -39,17 +39,17 @@ import java.awt.image.BufferedImage; * @author Harald Kuhr * @version $Id: BitmapMask.java,v 1.0 25.feb.2006 00:29:44 haku Exp$ */ -class BitmapMask extends BitmapDescriptor { - protected final BitmapIndexed bitMask; +final class BitmapMask extends BitmapDescriptor { + final BitmapIndexed bitMask; - public BitmapMask(final DirectoryEntry pParent, final DIBHeader pHeader) { - super(pParent, pHeader); - bitMask = new BitmapIndexed(pParent, pHeader); + public BitmapMask(final DirectoryEntry parent, final DIBHeader header) { + super(parent, header); + bitMask = new BitmapIndexed(parent, header); } - boolean isTransparent(final int pX, final int pY) { + boolean isTransparent(final int x, final int y) { // NOTE: 1: Fully transparent, 0: Opaque... - return bitMask.bits[pX + pY * getWidth()] != 0; + return bitMask.bits[x + y * getWidth()] != 0; } public BufferedImage getImage() { diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapRGB.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapRGB.java index d3f74d7c..a1226b4a 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapRGB.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapRGB.java @@ -31,8 +31,7 @@ package com.twelvemonkeys.imageio.plugins.bmp; import java.awt.*; -import java.awt.image.BufferedImage; -import java.awt.image.WritableRaster; +import java.awt.image.*; /** * Describes an RGB/true color bitmap structure (16, 24 and 32 bits per pixel). @@ -40,10 +39,10 @@ import java.awt.image.WritableRaster; * @author Harald Kuhr * @version $Id: BitmapRGB.java,v 1.0 25.feb.2006 00:29:44 haku Exp$ */ -class BitmapRGB extends BitmapDescriptor { +final class BitmapRGB extends BitmapDescriptor { - public BitmapRGB(final DirectoryEntry pEntry, final DIBHeader pHeader) { - super(pEntry, pHeader); + public BitmapRGB(final DirectoryEntry entry, final DIBHeader header) { + super(entry, header); } @Override @@ -71,7 +70,7 @@ class BitmapRGB extends BitmapDescriptor { WritableRaster alphaRaster = masked.getAlphaRaster(); - byte[] trans = {0x0}; + byte[] trans = {0x00}; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < getWidth(); x++) { if (mask.isTransparent(x, y)) { diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapUnsupported.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapUnsupported.java index 2c070378..6ff81b7c 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapUnsupported.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/BitmapUnsupported.java @@ -30,10 +30,9 @@ package com.twelvemonkeys.imageio.plugins.bmp; -import java.awt.image.BufferedImage; -import java.io.IOException; - import javax.imageio.IIOException; +import java.awt.image.*; +import java.io.IOException; /** * Represents bitmap structures we can't read. @@ -42,13 +41,13 @@ import javax.imageio.IIOException; * @author Harald Kuhr * @version $Id: BitmapUnsupported.java,v 1.0 25.feb.2006 00:29:44 haku Exp$ */ -class BitmapUnsupported extends BitmapDescriptor { - private String message; +final class BitmapUnsupported extends BitmapDescriptor { + private final String message; - public BitmapUnsupported(final DirectoryEntry pEntry, DIBHeader header, final String pMessage) { - super(pEntry, header); + public BitmapUnsupported(final DirectoryEntry entry, DIBHeader header, final String message) { + super(entry, header); - message = pMessage; + this.message = message; } @Override diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReader.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReader.java index 1cb5dd65..aeb3bd4b 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReader.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReader.java @@ -48,22 +48,22 @@ public final class CURImageReader extends DIBImageReader { super(new CURImageReaderSpi()); } - protected CURImageReader(final ImageReaderSpi pProvider) { - super(pProvider); + CURImageReader(final ImageReaderSpi provider) { + super(provider); } /** * Returns the hot spot location for the cursor. * - * @param pImageIndex the index of the cursor in the current input. + * @param imageIndex the index of the cursor in the current input. * @return the hot spot location for the cursor * * @throws java.io.IOException if an I/O exception occurs during reading of image meta data * @throws IndexOutOfBoundsException if {@code pImageIndex} is less than {@code 0} or greater than/equal to * the number of cursors in the file */ - public final Point getHotSpot(final int pImageIndex) throws IOException { - DirectoryEntry.CUREntry entry = (DirectoryEntry.CUREntry) getEntry(pImageIndex); + public Point getHotSpot(final int imageIndex) throws IOException { + DirectoryEntry.CUREntry entry = (DirectoryEntry.CUREntry) getEntry(imageIndex); return entry.getHotspot(); } } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReaderSpi.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReaderSpi.java index b3693a4a..9ebe2418 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReaderSpi.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/CURImageReaderSpi.java @@ -32,7 +32,6 @@ package com.twelvemonkeys.imageio.plugins.bmp; import com.twelvemonkeys.imageio.spi.ImageReaderSpiBase; -import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import java.io.IOException; import java.util.Locale; @@ -49,15 +48,15 @@ public final class CURImageReaderSpi extends ImageReaderSpiBase { super(new CURProviderInfo()); } - public boolean canDecodeInput(final Object pSource) throws IOException { - return pSource instanceof ImageInputStream && ICOImageReaderSpi.canDecode((ImageInputStream) pSource, DIB.TYPE_CUR); + public boolean canDecodeInput(final Object source) throws IOException { + return source instanceof ImageInputStream && ICOImageReaderSpi.canDecode((ImageInputStream) source, DIB.TYPE_CUR); } - public ImageReader createReaderInstance(final Object pExtension) throws IOException { + public CURImageReader createReaderInstance(final Object extension) { return new CURImageReader(this); } - public String getDescription(final Locale pLocale) { + public String getDescription(final Locale locale) { return "Windows Cursor Format (CUR) Reader"; } } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBHeader.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBHeader.java index 04c3e4cd..483a7517 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBHeader.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBHeader.java @@ -30,12 +30,11 @@ package com.twelvemonkeys.imageio.plugins.bmp; +import javax.imageio.IIOException; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; -import javax.imageio.IIOException; - /** * Represents the DIB (Device Independent Bitmap) Information header structure. * @@ -91,17 +90,17 @@ abstract class DIBHeader { protected DIBHeader() { } - public static DIBHeader read(final DataInput pStream) throws IOException { - int size = pStream.readInt(); + public static DIBHeader read(final DataInput stream) throws IOException { + int size = stream.readInt(); DIBHeader header = createHeader(size); - header.read(size, pStream); + header.read(size, stream); return header; } - private static DIBHeader createHeader(final int pSize) throws IOException { - switch (pSize) { + private static DIBHeader createHeader(final int size) throws IOException { + switch (size) { case DIB.BITMAP_CORE_HEADER_SIZE: return new BitmapCoreHeader(); case DIB.OS2_V2_HEADER_16_SIZE: @@ -118,11 +117,12 @@ abstract class DIBHeader { case DIB.BITMAP_V5_INFO_HEADER_SIZE: return new BitmapV5InfoHeader(); default: - throw new IIOException(String.format("Unknown Bitmap Information Header (size: %s)", pSize)); + throw new IIOException(String.format("Unknown Bitmap Information Header (size: %s)", size)); } } - protected abstract void read(int pSize, DataInput pStream) throws IOException; + protected abstract void read(int size, DataInput stream) throws IOException; + protected abstract void write(final DataOutput stream) throws IOException; public final int getSize() { return size; @@ -189,12 +189,12 @@ abstract class DIBHeader { ); } - private static int[] readMasks(final DataInput pStream, final boolean hasAlphaMask) throws IOException { + private static int[] readMasks(final DataInput stream, final boolean hasAlphaMask) throws IOException { int maskCount = hasAlphaMask ? 4 : 3; int[] masks = new int[4]; for (int i = 0; i < maskCount; i++) { - masks[i] = pStream.readInt(); + masks[i] = stream.readInt(); } return masks; @@ -205,24 +205,30 @@ abstract class DIBHeader { // TODO: Get rid of code duplication below... static final class BitmapCoreHeader extends DIBHeader { - protected void read(final int pSize, final DataInput pStream) throws IOException { - if (pSize != DIB.BITMAP_CORE_HEADER_SIZE) { - throw new IIOException(String.format("Size: %s !=: %s", pSize, DIB.BITMAP_CORE_HEADER_SIZE)); + @Override + protected void read(final int size, final DataInput stream) throws IOException { + if (size != DIB.BITMAP_CORE_HEADER_SIZE) { + throw new IIOException(String.format("Size: %s !=: %s", size, DIB.BITMAP_CORE_HEADER_SIZE)); } - size = pSize; + this.size = size; // NOTE: Unlike all other headers, width and height are unsigned SHORT values (16 bit)! - width = pStream.readUnsignedShort(); - height = pStream.readShort(); + width = stream.readUnsignedShort(); + height = stream.readShort(); if (height < 0) { height = -height; topDown = true; } - planes = pStream.readUnsignedShort(); - bitCount = pStream.readUnsignedShort(); + planes = stream.readUnsignedShort(); + bitCount = stream.readUnsignedShort(); + } + + @Override + protected void write(DataOutput stream) { + throw new UnsupportedOperationException(); } public String getBMPVersion() { @@ -242,45 +248,51 @@ abstract class DIBHeader { */ static final class BitmapCoreHeaderV2 extends DIBHeader { @SuppressWarnings("unused") - protected void read(final int pSize, final DataInput pStream) throws IOException { - if (pSize != DIB.OS2_V2_HEADER_SIZE && pSize != DIB.OS2_V2_HEADER_16_SIZE) { - throw new IIOException(String.format("Size: %s !=: %s", pSize, DIB.OS2_V2_HEADER_SIZE)); + @Override + protected void read(final int size, final DataInput stream) throws IOException { + if (size != DIB.OS2_V2_HEADER_SIZE && size != DIB.OS2_V2_HEADER_16_SIZE) { + throw new IIOException(String.format("Size: %s !=: %s", size, DIB.OS2_V2_HEADER_SIZE)); } - size = pSize; + this.size = size; - width = pStream.readInt(); - height = pStream.readInt(); + width = stream.readInt(); + height = stream.readInt(); if (height < 0) { height = -height; topDown = true; } - planes = pStream.readUnsignedShort(); - bitCount = pStream.readUnsignedShort(); + planes = stream.readUnsignedShort(); + bitCount = stream.readUnsignedShort(); - if (pSize != DIB.OS2_V2_HEADER_16_SIZE) { - compression = pStream.readInt(); + if (size != DIB.OS2_V2_HEADER_16_SIZE) { + compression = stream.readInt(); - imageSize = pStream.readInt(); + imageSize = stream.readInt(); - xPixelsPerMeter = pStream.readInt(); - yPixelsPerMeter = pStream.readInt(); + xPixelsPerMeter = stream.readInt(); + yPixelsPerMeter = stream.readInt(); - colorsUsed = pStream.readInt(); - colorsImportant = pStream.readInt(); + colorsUsed = stream.readInt(); + colorsImportant = stream.readInt(); } // TODO: Use? These fields are not reflected in metadata as per now... - int units = pStream.readShort(); - int reserved = pStream.readShort(); - int recording = pStream.readShort(); // Recording algorithm - int rendering = pStream.readShort(); // Halftoning algorithm - int size1 = pStream.readInt(); // Reserved for halftoning use - int size2 = pStream.readInt(); // Reserved for halftoning use - int colorEncoding = pStream.readInt(); // Color model used in bitmap - int identifier = pStream.readInt(); // Reserved for application use + int units = stream.readShort(); + int reserved = stream.readShort(); + int recording = stream.readShort(); // Recording algorithm + int rendering = stream.readShort(); // Halftoning algorithm + int size1 = stream.readInt(); // Reserved for halftoning use + int size2 = stream.readInt(); // Reserved for halftoning use + int colorEncoding = stream.readInt(); // Color model used in bitmap + int identifier = stream.readInt(); // Reserved for application use + } + + @Override + protected void write(DataOutput stream) { + throw new UnsupportedOperationException(); } public String getBMPVersion() { @@ -288,7 +300,6 @@ abstract class DIBHeader { } } - /** * Represents the DIB (Device Independent Bitmap) Windows 3.0 Bitmap Information header structure. * This is the common format for persistent DIB structures, even if Windows @@ -304,44 +315,46 @@ abstract class DIBHeader { * @see BITMAPV3INFOHEADER. */ static final class BitmapInfoHeader extends DIBHeader { - protected void read(final int pSize, final DataInput pStream) throws IOException { - if (!(pSize == DIB.BITMAP_INFO_HEADER_SIZE || pSize == DIB.BITMAP_V2_INFO_HEADER_SIZE || pSize == DIB.BITMAP_V3_INFO_HEADER_SIZE)) { - throw new IIOException(String.format("Size: %s !=: %s", pSize, DIB.BITMAP_INFO_HEADER_SIZE)); + @Override + protected void read(final int size, final DataInput stream) throws IOException { + if (!(size == DIB.BITMAP_INFO_HEADER_SIZE || size == DIB.BITMAP_V2_INFO_HEADER_SIZE || size == DIB.BITMAP_V3_INFO_HEADER_SIZE)) { + throw new IIOException(String.format("Size: %s !=: %s", size, DIB.BITMAP_INFO_HEADER_SIZE)); } - size = pSize; + this.size = size; - width = pStream.readInt(); - height = pStream.readInt(); + width = stream.readInt(); + height = stream.readInt(); if (height < 0) { height = -height; topDown = true; } - planes = pStream.readUnsignedShort(); - bitCount = pStream.readUnsignedShort(); - compression = pStream.readInt(); + planes = stream.readUnsignedShort(); + bitCount = stream.readUnsignedShort(); + compression = stream.readInt(); - imageSize = pStream.readInt(); + imageSize = stream.readInt(); - xPixelsPerMeter = pStream.readInt(); - yPixelsPerMeter = pStream.readInt(); + xPixelsPerMeter = stream.readInt(); + yPixelsPerMeter = stream.readInt(); - colorsUsed = pStream.readInt(); - colorsImportant = pStream.readInt(); + colorsUsed = stream.readInt(); + colorsImportant = stream.readInt(); // Read masks if we have V2 or V3 // or if we have compression BITFIELDS or ALPHA_BITFIELDS - if (size == DIB.BITMAP_V2_INFO_HEADER_SIZE || compression == DIB.COMPRESSION_BITFIELDS) { - masks = readMasks(pStream, false); + if (this.size == DIB.BITMAP_V2_INFO_HEADER_SIZE || compression == DIB.COMPRESSION_BITFIELDS) { + masks = readMasks(stream, false); } - else if (size == DIB.BITMAP_V3_INFO_HEADER_SIZE || compression == DIB.COMPRESSION_ALPHA_BITFIELDS) { - masks = readMasks(pStream, true); + else if (this.size == DIB.BITMAP_V3_INFO_HEADER_SIZE || compression == DIB.COMPRESSION_ALPHA_BITFIELDS) { + masks = readMasks(stream, true); } } - void write(final DataOutput stream) throws IOException { + @Override + protected void write(final DataOutput stream) throws IOException { stream.writeInt(DIB.BITMAP_INFO_HEADER_SIZE); stream.writeInt(width); @@ -359,7 +372,7 @@ abstract class DIBHeader { stream.writeInt(colorsUsed); stream.writeInt(colorsImportant); - // TODO: Write masks, if bitfields + // TODO: Write masks, if COMPRESSION_BITFIELDS/COMPRESSION_ALPHA_BITFIELDS } public String getBMPVersion() { @@ -376,105 +389,160 @@ abstract class DIBHeader { * Represents the BITMAPV4INFOHEADER structure. */ static final class BitmapV4InfoHeader extends DIBHeader { - protected void read(final int pSize, final DataInput pStream) throws IOException { - if (pSize != DIB.BITMAP_V4_INFO_HEADER_SIZE) { - throw new IIOException(String.format("Size: %s !=: %s", pSize, DIB.BITMAP_V4_INFO_HEADER_SIZE)); + @Override + protected void read(final int size, final DataInput stream) throws IOException { + if (size != DIB.BITMAP_V4_INFO_HEADER_SIZE) { + throw new IIOException(String.format("Size: %s !=: %s", size, DIB.BITMAP_V4_INFO_HEADER_SIZE)); } - size = pSize; + this.size = size; - width = pStream.readInt(); - height = pStream.readInt(); + width = stream.readInt(); + height = stream.readInt(); if (height < 0) { height = -height; topDown = true; } - planes = pStream.readUnsignedShort(); - bitCount = pStream.readUnsignedShort(); - compression = pStream.readInt(); + planes = stream.readUnsignedShort(); + bitCount = stream.readUnsignedShort(); + compression = stream.readInt(); - imageSize = pStream.readInt(); + imageSize = stream.readInt(); - xPixelsPerMeter = pStream.readInt(); - yPixelsPerMeter = pStream.readInt(); + xPixelsPerMeter = stream.readInt(); + yPixelsPerMeter = stream.readInt(); - colorsUsed = pStream.readInt(); - colorsImportant = pStream.readInt(); + colorsUsed = stream.readInt(); + colorsImportant = stream.readInt(); - masks = readMasks(pStream, true); + masks = readMasks(stream, true); - colorSpaceType = pStream.readInt(); // Should be 0 for V4 + colorSpaceType = stream.readInt(); // Should be 0 for V4 cieXYZEndpoints = new double[9]; for (int i = 0; i < cieXYZEndpoints.length; i++) { - cieXYZEndpoints[i] = pStream.readInt(); // TODO: Hmmm...? + cieXYZEndpoints[i] = stream.readInt(); // TODO: Hmmm...? } gamma = new int[3]; for (int i = 0; i < gamma.length; i++) { - gamma[i] = pStream.readInt(); + gamma[i] = stream.readInt(); } } public String getBMPVersion() { return "BMP v. 4.x"; } + + @Override + protected void write(DataOutput stream) throws IOException { + stream.writeInt(DIB.BITMAP_V4_INFO_HEADER_SIZE); + + stream.writeInt(width); + stream.writeInt(topDown ? -height : height); + + stream.writeShort(planes); + stream.writeShort(bitCount); + stream.writeInt(compression); + + stream.writeInt(imageSize); + + stream.writeInt(xPixelsPerMeter); + stream.writeInt(yPixelsPerMeter); + + stream.writeInt(colorsUsed); + stream.writeInt(colorsImportant); + + // Red, Green, Blue, Alpha masks + stream.writeInt(masks[0]); + stream.writeInt(masks[1]); + stream.writeInt(masks[2]); + stream.writeInt(masks[3]); + + // color space ("sRGB" LITTLE endian) + stream.writeInt(DIB.LCS_sRGB); + + // 36 bytes CIE XYZ triples, unused for sRGB + stream.writeInt(0); + stream.writeInt(0); + stream.writeInt(0); + + stream.writeInt(0); + stream.writeInt(0); + stream.writeInt(0); + + stream.writeInt(0); + stream.writeInt(0); + stream.writeInt(0); + + // Red gamma, unused for sRGB + // Green gamma, unused for sRGB + // Blue gamma, unused for sRGB + stream.writeInt(0); + stream.writeInt(0); + stream.writeInt(0); + } } /** * Represents the BITMAPV5INFOHEADER structure. */ static final class BitmapV5InfoHeader extends DIBHeader { - protected void read(final int pSize, final DataInput pStream) throws IOException { - if (pSize != DIB.BITMAP_V5_INFO_HEADER_SIZE) { - throw new IIOException(String.format("Size: %s !=: %s", pSize, DIB.BITMAP_V5_INFO_HEADER_SIZE)); + protected void read(final int size, final DataInput stream) throws IOException { + if (size != DIB.BITMAP_V5_INFO_HEADER_SIZE) { + throw new IIOException(String.format("Size: %s !=: %s", size, DIB.BITMAP_V5_INFO_HEADER_SIZE)); } - size = pSize; + this.size = size; - width = pStream.readInt(); - height = pStream.readInt(); + width = stream.readInt(); + height = stream.readInt(); if (height < 0) { height = -height; topDown = true; } - planes = pStream.readUnsignedShort(); - bitCount = pStream.readUnsignedShort(); - compression = pStream.readInt(); + planes = stream.readUnsignedShort(); + bitCount = stream.readUnsignedShort(); + compression = stream.readInt(); - imageSize = pStream.readInt(); + imageSize = stream.readInt(); - xPixelsPerMeter = pStream.readInt(); - yPixelsPerMeter = pStream.readInt(); + xPixelsPerMeter = stream.readInt(); + yPixelsPerMeter = stream.readInt(); - colorsUsed = pStream.readInt(); - colorsImportant = pStream.readInt(); + colorsUsed = stream.readInt(); + colorsImportant = stream.readInt(); - masks = readMasks(pStream, true); + masks = readMasks(stream, true); - colorSpaceType = pStream.readInt(); + colorSpaceType = stream.readInt(); cieXYZEndpoints = new double[9]; for (int i = 0; i < cieXYZEndpoints.length; i++) { - cieXYZEndpoints[i] = pStream.readInt(); // TODO: Hmmm...? + cieXYZEndpoints[i] = stream.readInt(); // TODO: Hmmm...? } gamma = new int[3]; for (int i = 0; i < gamma.length; i++) { - gamma[i] = pStream.readInt(); + gamma[i] = stream.readInt(); } - intent = pStream.readInt(); // TODO: Verify if this is same as ICC intent - profileData = pStream.readInt() & 0xffffffffL; - profileSize = pStream.readInt() & 0xffffffffL; - pStream.readInt(); // Reserved + intent = stream.readInt(); // TODO: Verify if this is same as ICC intent + profileData = stream.readInt() & 0xffffffffL; + profileSize = stream.readInt() & 0xffffffffL; + stream.readInt(); // Reserved + } + + @Override + protected void write(DataOutput stream) { + throw new UnsupportedOperationException(); } public String getBMPVersion() { diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBImageReader.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBImageReader.java index 6591b35d..f0d8dbaf 100644 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBImageReader.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DIBImageReader.java @@ -30,19 +30,11 @@ package com.twelvemonkeys.imageio.plugins.bmp; -import java.awt.*; -import java.awt.color.ColorSpace; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.awt.image.*; -import java.io.File; -import java.io.IOException; -import java.nio.ByteOrder; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.WeakHashMap; +import com.twelvemonkeys.image.ImageUtil; +import com.twelvemonkeys.imageio.ImageReaderBase; +import com.twelvemonkeys.imageio.stream.SubImageInputStream; +import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers; +import com.twelvemonkeys.util.WeakWeakMap; import javax.imageio.IIOException; import javax.imageio.ImageIO; @@ -52,12 +44,18 @@ import javax.imageio.ImageTypeSpecifier; import javax.imageio.spi.ImageReaderSpi; import javax.imageio.stream.ImageInputStream; import javax.swing.*; - -import com.twelvemonkeys.image.ImageUtil; -import com.twelvemonkeys.imageio.ImageReaderBase; -import com.twelvemonkeys.imageio.stream.SubImageInputStream; -import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers; -import com.twelvemonkeys.util.WeakWeakMap; +import java.awt.*; +import java.awt.color.*; +import java.awt.event.*; +import java.awt.image.*; +import java.io.File; +import java.io.IOException; +import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; /** * ImageReader for Microsoft Windows ICO (icon) format. @@ -81,13 +79,13 @@ abstract class DIBImageReader extends ImageReaderBase { private Directory directory; // TODO: Review these, make sure we don't have a memory leak - private Map headers = new WeakHashMap<>(); - private Map descriptors = new WeakWeakMap<>(); + private final Map headers = new WeakHashMap<>(); + private final Map descriptors = new WeakWeakMap<>(); private ImageReader pngImageReader; - protected DIBImageReader(final ImageReaderSpi pProvider) { - super(pProvider); + protected DIBImageReader(final ImageReaderSpi provider) { + super(provider); } protected void resetMembers() { @@ -102,8 +100,8 @@ abstract class DIBImageReader extends ImageReaderBase { } } - public Iterator getImageTypes(final int pImageIndex) throws IOException { - DirectoryEntry entry = getEntry(pImageIndex); + public Iterator getImageTypes(final int imageIndex) throws IOException { + DirectoryEntry entry = getEntry(imageIndex); // NOTE: Delegate to PNG reader if (isPNG(entry)) { @@ -155,39 +153,39 @@ abstract class DIBImageReader extends ImageReaderBase { return getDirectory().count(); } - public int getWidth(final int pImageIndex) throws IOException { - return getEntry(pImageIndex).getWidth(); + public int getWidth(final int imageIndex) throws IOException { + return getEntry(imageIndex).getWidth(); } - public int getHeight(final int pImageIndex) throws IOException { - return getEntry(pImageIndex).getHeight(); + public int getHeight(final int imageIndex) throws IOException { + return getEntry(imageIndex).getHeight(); } - public BufferedImage read(final int pImageIndex, final ImageReadParam pParam) throws IOException { - checkBounds(pImageIndex); + public BufferedImage read(final int imageIndex, final ImageReadParam param) throws IOException { + checkBounds(imageIndex); - processImageStarted(pImageIndex); + processImageStarted(imageIndex); - DirectoryEntry entry = getEntry(pImageIndex); + DirectoryEntry entry = getEntry(imageIndex); BufferedImage destination; if (isPNG(entry)) { // NOTE: Special case for Windows Vista, 256x256 PNG encoded images, with no DIB header... - destination = readPNG(entry, pParam); + destination = readPNG(entry, param); } else { // NOTE: If param does not have explicit destination, we'll try to create a BufferedImage later, // to allow for storing the cursor hotspot for CUR images - destination = hasExplicitDestination(pParam) ? - getDestination(pParam, getImageTypes(pImageIndex), getWidth(pImageIndex), getHeight(pImageIndex)) : null; + destination = hasExplicitDestination(param) ? + getDestination(param, getImageTypes(imageIndex), getWidth(imageIndex), getHeight(imageIndex)) : null; BufferedImage image = readBitmap(entry); // TODO: Handle AOI and subsampling inline, probably not of big importance... - if (pParam != null) { - image = fakeAOI(image, pParam); - image = ImageUtil.toBuffered(fakeSubsampling(image, pParam)); + if (param != null) { + image = fakeAOI(image, param); + image = ImageUtil.toBuffered(fakeSubsampling(image, param)); } if (destination == null) { @@ -213,10 +211,10 @@ abstract class DIBImageReader extends ImageReaderBase { return destination; } - private boolean isPNG(final DirectoryEntry pEntry) throws IOException { + private boolean isPNG(final DirectoryEntry entry) throws IOException { long magic; - imageInput.seek(pEntry.getOffset()); + imageInput.seek(entry.getOffset()); imageInput.setByteOrder(ByteOrder.BIG_ENDIAN); try { @@ -229,22 +227,20 @@ abstract class DIBImageReader extends ImageReaderBase { return magic == DIB.PNG_MAGIC; } - private BufferedImage readPNG(final DirectoryEntry pEntry, final ImageReadParam pParam) throws IOException { + private BufferedImage readPNG(final DirectoryEntry entry, final ImageReadParam param) throws IOException { // TODO: Consider delegating listener calls - return initPNGReader(pEntry).read(0, pParam); + return initPNGReader(entry).read(0, param); } - private Iterator getImageTypesPNG(final DirectoryEntry pEntry) throws IOException { - return initPNGReader(pEntry).getImageTypes(0); + private Iterator getImageTypesPNG(final DirectoryEntry entry) throws IOException { + return initPNGReader(entry).getImageTypes(0); } - private ImageReader initPNGReader(final DirectoryEntry pEntry) throws IOException { + private ImageReader initPNGReader(final DirectoryEntry entry) throws IOException { ImageReader pngReader = getPNGReader(); - imageInput.seek(pEntry.getOffset()); -// InputStream inputStream = IIOUtil.createStreamAdapter(imageInput, pEntry.getSize()); -// ImageInputStream stream = ImageIO.createImageInputStream(inputStream); - ImageInputStream stream = new SubImageInputStream(imageInput, pEntry.getSize()); + imageInput.seek(entry.getOffset()); + ImageInputStream stream = new SubImageInputStream(imageInput, entry.getSize()); // NOTE: Will throw IOException on later reads if input is not PNG pngReader.setInput(stream); @@ -271,31 +267,31 @@ abstract class DIBImageReader extends ImageReaderBase { return pngImageReader; } - private DIBHeader getHeader(final DirectoryEntry pEntry) throws IOException { - if (!headers.containsKey(pEntry)) { - imageInput.seek(pEntry.getOffset()); + private DIBHeader getHeader(final DirectoryEntry entry) throws IOException { + if (!headers.containsKey(entry)) { + imageInput.seek(entry.getOffset()); DIBHeader header = DIBHeader.read(imageInput); - headers.put(pEntry, header); + headers.put(entry, header); } - return headers.get(pEntry); + return headers.get(entry); } - private BufferedImage readBitmap(final DirectoryEntry pEntry) throws IOException { + private BufferedImage readBitmap(final DirectoryEntry entry) throws IOException { // TODO: Get rid of the caching, as the images are mutable - BitmapDescriptor descriptor = descriptors.get(pEntry); + BitmapDescriptor descriptor = descriptors.get(entry); - if (descriptor == null || !descriptors.containsKey(pEntry)) { - DIBHeader header = getHeader(pEntry); + if (descriptor == null || !descriptors.containsKey(entry)) { + DIBHeader header = getHeader(entry); - int offset = pEntry.getOffset() + header.getSize(); + int offset = entry.getOffset() + header.getSize(); if (offset != imageInput.getStreamPosition()) { imageInput.seek(offset); } // TODO: Support this, it's already in the BMP reader, spec allows RLE4 and RLE8 if (header.getCompression() != DIB.COMPRESSION_RGB) { - descriptor = new BitmapUnsupported(pEntry, header, String.format("Unsupported compression: %d", header.getCompression())); + descriptor = new BitmapUnsupported(entry, header, String.format("Unsupported compression: %d", header.getCompression())); } else { int bitCount = header.getBitCount(); @@ -305,75 +301,75 @@ abstract class DIBImageReader extends ImageReaderBase { case 1: case 4: case 8: // TODO: Gray! - descriptor = new BitmapIndexed(pEntry, header); + descriptor = new BitmapIndexed(entry, header); readBitmapIndexed((BitmapIndexed) descriptor); break; // RGB style case 16: - descriptor = new BitmapRGB(pEntry, header); + descriptor = new BitmapRGB(entry, header); readBitmap16(descriptor); break; case 24: - descriptor = new BitmapRGB(pEntry, header); + descriptor = new BitmapRGB(entry, header); readBitmap24(descriptor); break; case 32: - descriptor = new BitmapRGB(pEntry, header); + descriptor = new BitmapRGB(entry, header); readBitmap32(descriptor); break; default: - descriptor = new BitmapUnsupported(pEntry, header, String.format("Unsupported bit count %d", bitCount)); + descriptor = new BitmapUnsupported(entry, header, String.format("Unsupported bit count %d", bitCount)); } } - descriptors.put(pEntry, descriptor); + descriptors.put(entry, descriptor); } return descriptor.getImage(); } - private void readBitmapIndexed(final BitmapIndexed pBitmap) throws IOException { - readColorMap(pBitmap); + private void readBitmapIndexed(final BitmapIndexed bitmap) throws IOException { + readColorMap(bitmap); - switch (pBitmap.getBitCount()) { + switch (bitmap.getBitCount()) { case 1: - readBitmapIndexed1(pBitmap, false); + readBitmapIndexed1(bitmap, false); break; case 4: - readBitmapIndexed4(pBitmap); + readBitmapIndexed4(bitmap); break; case 8: - readBitmapIndexed8(pBitmap); + readBitmapIndexed8(bitmap); break; } - BitmapMask mask = new BitmapMask(pBitmap.entry, pBitmap.header); + BitmapMask mask = new BitmapMask(bitmap.entry, bitmap.header); readBitmapIndexed1(mask.bitMask, true); - pBitmap.setMask(mask); + bitmap.setMask(mask); } - private void readColorMap(final BitmapIndexed pBitmap) throws IOException { - int colorCount = pBitmap.getColorCount(); + private void readColorMap(final BitmapIndexed bitmap) throws IOException { + int colorCount = bitmap.getColorCount(); for (int i = 0; i < colorCount; i++) { // aRGB (a is "Reserved") - pBitmap.colors[i] = (imageInput.readInt() & 0xffffff) | 0xff000000; + bitmap.colors[i] = (imageInput.readInt() & 0xffffff) | 0xff000000; } } - private void readBitmapIndexed1(final BitmapIndexed pBitmap, final boolean pAsMask) throws IOException { - int width = adjustToPadding((pBitmap.getWidth() + 7) >> 3); + private void readBitmapIndexed1(final BitmapIndexed bitmap, final boolean asMask) throws IOException { + int width = adjustToPadding((bitmap.getWidth() + 7) >> 3); byte[] row = new byte[width]; - for (int y = 0; y < pBitmap.getHeight(); y++) { + for (int y = 0; y < bitmap.getHeight(); y++) { imageInput.readFully(row, 0, width); int rowPos = 0; int xOrVal = 0x80; - int pos = (pBitmap.getHeight() - y - 1) * pBitmap.getWidth(); + int pos = (bitmap.getHeight() - y - 1) * bitmap.getWidth(); - for (int x = 0; x < pBitmap.getWidth(); x++) { - pBitmap.bits[pos++] = ((row[rowPos] & xOrVal) / xOrVal) & 0xFF; + for (int x = 0; x < bitmap.getWidth(); x++) { + bitmap.bits[pos++] = ((row[rowPos] & xOrVal) / xOrVal) & 0xFF; if (xOrVal == 1) { xOrVal = 0x80; @@ -384,29 +380,29 @@ abstract class DIBImageReader extends ImageReaderBase { } } - // NOTE: If we are reading the mask, we don't abort or report progress - if (!pAsMask) { + // NOTE: If we are reading the mask, we can't abort or report progress + if (!asMask) { if (abortRequested()) { processReadAborted(); break; } - processImageProgress(100 * y / (float) pBitmap.getHeight()); + processImageProgress(100 * y / (float) bitmap.getHeight()); } } } - private void readBitmapIndexed4(final BitmapIndexed pBitmap) throws IOException { - int width = adjustToPadding((pBitmap.getWidth() + 1) >> 1); + private void readBitmapIndexed4(final BitmapIndexed bitmap) throws IOException { + int width = adjustToPadding((bitmap.getWidth() + 1) >> 1); byte[] row = new byte[width]; - for (int y = 0; y < pBitmap.getHeight(); y++) { + for (int y = 0; y < bitmap.getHeight(); y++) { imageInput.readFully(row, 0, width); int rowPos = 0; boolean high4 = true; - int pos = (pBitmap.getHeight() - y - 1) * pBitmap.getWidth(); + int pos = (bitmap.getHeight() - y - 1) * bitmap.getWidth(); - for (int x = 0; x < pBitmap.getWidth(); x++) { + for (int x = 0; x < bitmap.getWidth(); x++) { int value; if (high4) { @@ -417,7 +413,7 @@ abstract class DIBImageReader extends ImageReaderBase { rowPos++; } - pBitmap.bits[pos++] = value & 0xFF; + bitmap.bits[pos++] = value & 0xFF; high4 = !high4; } @@ -426,22 +422,22 @@ abstract class DIBImageReader extends ImageReaderBase { break; } - processImageProgress(100 * y / (float) pBitmap.getHeight()); + processImageProgress(100 * y / (float) bitmap.getHeight()); } } - private void readBitmapIndexed8(final BitmapIndexed pBitmap) throws IOException { - int width = adjustToPadding(pBitmap.getWidth()); + private void readBitmapIndexed8(final BitmapIndexed bitmap) throws IOException { + int width = adjustToPadding(bitmap.getWidth()); byte[] row = new byte[width]; - for (int y = 0; y < pBitmap.getHeight(); y++) { + for (int y = 0; y < bitmap.getHeight(); y++) { imageInput.readFully(row, 0, width); int rowPos = 0; - int pos = (pBitmap.getHeight() - y - 1) * pBitmap.getWidth(); + int pos = (bitmap.getHeight() - y - 1) * bitmap.getWidth(); - for (int x = 0; x < pBitmap.getWidth(); x++) { - pBitmap.bits[pos++] = row[rowPos++] & 0xFF; + for (int x = 0; x < bitmap.getWidth(); x++) { + bitmap.bits[pos++] = row[rowPos++] & 0xFF; } if (abortRequested()) { @@ -449,40 +445,41 @@ abstract class DIBImageReader extends ImageReaderBase { break; } - processImageProgress(100 * y / (float) pBitmap.getHeight()); + processImageProgress(100 * y / (float) bitmap.getHeight()); } } /** - * @param pWidth Bytes per scan line (i.e., 1BPP, width = 9 -> bytes = 1) + * @param width Bytes per scan line (i.e., 1BPP, width = 9 -> bytes = 2) * @return padded width */ - private static int adjustToPadding(final int pWidth) { - if ((pWidth & 0x03) != 0) { - return (pWidth & ~0x03) + 4; + private static int adjustToPadding(final int width) { + if ((width & 0x03) != 0) { + return (width & ~0x03) + 4; } - return pWidth; + + return width; } - private void readBitmap16(final BitmapDescriptor pBitmap) throws IOException { - short[] pixels = new short[pBitmap.getWidth() * pBitmap.getHeight()]; + private void readBitmap16(final BitmapDescriptor bitmap) throws IOException { + short[] pixels = new short[bitmap.getWidth() * bitmap.getHeight()]; // TODO: Support TYPE_USHORT_565 and the RGB 444/ARGB 4444 layouts // Will create TYPE_USHORT_555 DirectColorModel cm = new DirectColorModel(16, 0x7C00, 0x03E0, 0x001F); DataBuffer buffer = new DataBufferUShort(pixels, pixels.length); WritableRaster raster = Raster.createPackedRaster( - buffer, pBitmap.getWidth(), pBitmap.getHeight(), pBitmap.getWidth(), cm.getMasks(), null + buffer, bitmap.getWidth(), bitmap.getHeight(), bitmap.getWidth(), cm.getMasks(), null ); - pBitmap.image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); + bitmap.image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); - for (int y = 0; y < pBitmap.getHeight(); y++) { - int offset = (pBitmap.getHeight() - y - 1) * pBitmap.getWidth(); - imageInput.readFully(pixels, offset, pBitmap.getWidth()); + for (int y = 0; y < bitmap.getHeight(); y++) { + int offset = (bitmap.getHeight() - y - 1) * bitmap.getWidth(); + imageInput.readFully(pixels, offset, bitmap.getWidth()); // Skip to 32 bit boundary - if (pBitmap.getWidth() % 2 != 0) { + if (bitmap.getWidth() % 2 != 0) { imageInput.readShort(); } @@ -491,14 +488,14 @@ abstract class DIBImageReader extends ImageReaderBase { break; } - processImageProgress(100 * y / (float) pBitmap.getHeight()); + processImageProgress(100 * y / (float) bitmap.getHeight()); } // TODO: Might be mask!? } - private void readBitmap24(final BitmapDescriptor pBitmap) throws IOException { - byte[] pixels = new byte[pBitmap.getWidth() * pBitmap.getHeight() * 3]; + private void readBitmap24(final BitmapDescriptor bitmap) throws IOException { + byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight() * 3]; // Create TYPE_3BYTE_BGR DataBuffer buffer = new DataBufferByte(pixels, pixels.length); @@ -509,17 +506,17 @@ abstract class DIBImageReader extends ImageReaderBase { cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE ); - int scanlineStride = pBitmap.getWidth() * 3; + int scanlineStride = bitmap.getWidth() * 3; // BMP rows are padded to 4 byte boundary int rowSizeBytes = ((8 * scanlineStride + 31) / 32) * 4; WritableRaster raster = Raster.createInterleavedRaster( - buffer, pBitmap.getWidth(), pBitmap.getHeight(), scanlineStride, 3, bOffs, null + buffer, bitmap.getWidth(), bitmap.getHeight(), scanlineStride, 3, bOffs, null ); - pBitmap.image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); + bitmap.image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); - for (int y = 0; y < pBitmap.getHeight(); y++) { - int offset = (pBitmap.getHeight() - y - 1) * scanlineStride; + for (int y = 0; y < bitmap.getHeight(); y++) { + int offset = (bitmap.getHeight() - y - 1) * scanlineStride; imageInput.readFully(pixels, offset, scanlineStride); imageInput.skipBytes(rowSizeBytes - scanlineStride); @@ -528,38 +525,38 @@ abstract class DIBImageReader extends ImageReaderBase { break; } - processImageProgress(100 * y / (float) pBitmap.getHeight()); + processImageProgress(100 * y / (float) bitmap.getHeight()); } // 24 bit icons usually have a bit mask - if (pBitmap.hasMask()) { - BitmapMask mask = new BitmapMask(pBitmap.entry, pBitmap.header); + if (bitmap.hasMask()) { + BitmapMask mask = new BitmapMask(bitmap.entry, bitmap.header); readBitmapIndexed1(mask.bitMask, true); - pBitmap.setMask(mask); + bitmap.setMask(mask); } } - private void readBitmap32(final BitmapDescriptor pBitmap) throws IOException { - int[] pixels = new int[pBitmap.getWidth() * pBitmap.getHeight()]; + private void readBitmap32(final BitmapDescriptor bitmap) throws IOException { + int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()]; // Will create TYPE_INT_ARGB DirectColorModel cm = (DirectColorModel) ColorModel.getRGBdefault(); DataBuffer buffer = new DataBufferInt(pixels, pixels.length); WritableRaster raster = Raster.createPackedRaster( - buffer, pBitmap.getWidth(), pBitmap.getHeight(), pBitmap.getWidth(), cm.getMasks(), null + buffer, bitmap.getWidth(), bitmap.getHeight(), bitmap.getWidth(), cm.getMasks(), null ); - pBitmap.image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); + bitmap.image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); - for (int y = 0; y < pBitmap.getHeight(); y++) { - int offset = (pBitmap.getHeight() - y - 1) * pBitmap.getWidth(); - imageInput.readFully(pixels, offset, pBitmap.getWidth()); + for (int y = 0; y < bitmap.getHeight(); y++) { + int offset = (bitmap.getHeight() - y - 1) * bitmap.getWidth(); + imageInput.readFully(pixels, offset, bitmap.getWidth()); if (abortRequested()) { processReadAborted(); break; } - processImageProgress(100 * y / (float) pBitmap.getHeight()); + processImageProgress(100 * y / (float) bitmap.getHeight()); } // There might be a mask here as well, but we'll ignore it, @@ -590,18 +587,18 @@ abstract class DIBImageReader extends ImageReaderBase { directory = Directory.read(type, imageCount, imageInput); } - final DirectoryEntry getEntry(final int pImageIndex) throws IOException { + final DirectoryEntry getEntry(final int imageIndex) throws IOException { Directory directory = getDirectory(); - if (pImageIndex < 0 || pImageIndex >= directory.count()) { - throw new IndexOutOfBoundsException(String.format("Index: %d, ImageCount: %d", pImageIndex, directory.count())); + if (imageIndex < 0 || imageIndex >= directory.count()) { + throw new IndexOutOfBoundsException(String.format("Index: %d, ImageCount: %d", imageIndex, directory.count())); } - return directory.getEntry(pImageIndex); + return directory.getEntry(imageIndex); } /// Test code below, ignore.. :-) - public static void main(final String[] pArgs) throws IOException { - if (pArgs.length == 0) { + public static void main(final String[] args) throws IOException { + if (args.length == 0) { System.err.println("Please specify the icon file name"); System.exit(1); } @@ -613,7 +610,7 @@ abstract class DIBImageReader extends ImageReaderBase { // Ignore } - String title = new File(pArgs[0]).getName(); + String title = new File(args[0]).getName(); JFrame frame = createWindow(title); JPanel root = new JPanel(new FlowLayout()); JScrollPane scroll = @@ -629,7 +626,7 @@ abstract class DIBImageReader extends ImageReaderBase { ImageReader reader = readers.next(); - for (String arg : pArgs) { + for (String arg : args) { JPanel panel = new JPanel(null); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); readImagesInFile(arg, reader, panel); @@ -640,28 +637,28 @@ abstract class DIBImageReader extends ImageReaderBase { frame.setVisible(true); } - private static void readImagesInFile(String pFileName, ImageReader pReader, final Container pContainer) throws IOException { - File file = new File(pFileName); + private static void readImagesInFile(String fileName, ImageReader reader, final Container container) throws IOException { + File file = new File(fileName); if (!file.isFile()) { - System.err.println(pFileName + " not found, or is no file"); + System.err.println(fileName + " not found, or is no file"); } - pReader.setInput(ImageIO.createImageInputStream(file)); - int imageCount = pReader.getNumImages(true); + reader.setInput(ImageIO.createImageInputStream(file)); + int imageCount = reader.getNumImages(true); for (int i = 0; i < imageCount; i++) { try { - addImage(pContainer, pReader, i); + addImage(container, reader, i); } catch (Exception e) { - System.err.println("FileName: " + pFileName); + System.err.println("FileName: " + fileName); System.err.println("Icon: " + i); e.printStackTrace(); } } } - private static JFrame createWindow(final String pTitle) { - JFrame frame = new JFrame(pTitle); + private static JFrame createWindow(final String title) { + JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { @@ -671,15 +668,15 @@ abstract class DIBImageReader extends ImageReaderBase { return frame; } - private static void addImage(final Container pParent, final ImageReader pReader, final int pImageNo) throws IOException { + private static void addImage(final Container parent, final ImageReader reader, final int imageNo) throws IOException { final JButton button = new JButton(); - BufferedImage image = pReader.read(pImageNo); + BufferedImage image = reader.read(imageNo); button.setIcon(new ImageIcon(image) { TexturePaint texture; - private void createTexture(final GraphicsConfiguration pGraphicsConfiguration) { - BufferedImage pattern = pGraphicsConfiguration.createCompatibleImage(20, 20); + private void createTexture(final GraphicsConfiguration graphicsConfiguration) { + BufferedImage pattern = graphicsConfiguration.createCompatibleImage(20, 20); Graphics2D g = pattern.createGraphics(); try { g.setColor(Color.LIGHT_GRAY); @@ -714,6 +711,6 @@ abstract class DIBImageReader extends ImageReaderBase { String.valueOf(((IndexColorModel) image.getColorModel()).getMapSize()) : "TrueColor")); - pParent.add(button); + parent.add(button); } } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/Directory.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/Directory.java index 988b65f7..e1574df3 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/Directory.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/Directory.java @@ -44,24 +44,25 @@ import java.util.List; class Directory { private final List entries; - private Directory(int pImageCount) { - entries = Arrays.asList(new DirectoryEntry[pImageCount]); + private Directory(int imageCount) { + entries = Arrays.asList(new DirectoryEntry[imageCount]); } - public static Directory read(final int pType, final int pImageCount, final DataInput pStream) throws IOException { - Directory directory = new Directory(pImageCount); - directory.readEntries(pType, pStream); + public static Directory read(final int type, final int imageCount, final DataInput stream) throws IOException { + Directory directory = new Directory(imageCount); + directory.readEntries(type, stream); + return directory; } - private void readEntries(final int pType, final DataInput pStream) throws IOException { + private void readEntries(final int type, final DataInput stream) throws IOException { for (int i = 0; i < entries.size(); i++) { - entries.set(i, DirectoryEntry.read(pType, pStream)); + entries.set(i, DirectoryEntry.read(type, stream)); } } - public DirectoryEntry getEntry(final int pEntryIndex) { - return entries.get(pEntryIndex); + public DirectoryEntry getEntry(final int entryIndex) { + return entries.get(entryIndex); } public int count() { diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DirectoryEntry.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DirectoryEntry.java index 47210114..9299d214 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DirectoryEntry.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/DirectoryEntry.java @@ -32,8 +32,7 @@ package com.twelvemonkeys.imageio.plugins.bmp; import javax.imageio.IIOException; import java.awt.*; -import java.awt.image.ColorModel; -import java.awt.image.IndexColorModel; +import java.awt.image.*; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; @@ -58,47 +57,43 @@ abstract class DirectoryEntry { DirectoryEntry() { } - public static DirectoryEntry read(final int pType, final DataInput pStream) throws IOException { - DirectoryEntry entry = createEntry(pType); - entry.read(pStream); + public static DirectoryEntry read(final int type, final DataInput stream) throws IOException { + DirectoryEntry entry = createEntry(type); + entry.read(stream); + return entry; } - private static DirectoryEntry createEntry(int pType) throws IIOException { - switch (pType) { + private static DirectoryEntry createEntry(int type) throws IIOException { + switch (type) { case DIB.TYPE_ICO: return new ICOEntry(); case DIB.TYPE_CUR: return new CUREntry(); default: - throw new IIOException( - String.format( - "Unknown DIB type: %s, expected: %s (ICO) or %s (CUR)", - pType, DIB.TYPE_ICO, DIB.TYPE_CUR - ) - ); + throw new IIOException(String.format("Unknown DIB type: %s, expected: %s (ICO) or %s (CUR)", type, DIB.TYPE_ICO, DIB.TYPE_CUR)); } } - protected void read(final DataInput pStream) throws IOException { + protected void read(final DataInput stream) throws IOException { // Width/height = 0, means 256 - int w = pStream.readUnsignedByte(); + int w = stream.readUnsignedByte(); width = w == 0 ? 256 : w; - int h = pStream.readUnsignedByte(); + int h = stream.readUnsignedByte(); height = h == 0 ? 256 : h; // Color count = 0, means 256 or more colors - colorCount = pStream.readUnsignedByte(); + colorCount = stream.readUnsignedByte(); // Ignore. Should be 0, but .NET (System.Drawing.Icon.Save) sets this value to 255, according to Wikipedia - pStream.readUnsignedByte(); + stream.readUnsignedByte(); - planes = pStream.readUnsignedShort(); // Should be 0 or 1 for ICO, x hotspot for CUR - bitCount = pStream.readUnsignedShort(); // bit count for ICO, y hotspot for CUR + planes = stream.readUnsignedShort(); // Should be 0 or 1 for ICO, x hotspot for CUR + bitCount = stream.readUnsignedShort(); // bit count for ICO, y hotspot for CUR // Size of bitmap in bytes - size = pStream.readInt(); - offset = pStream.readInt(); + size = stream.readInt(); + offset = stream.readInt(); } void write(final DataOutput output) throws IOException { @@ -157,8 +152,8 @@ abstract class DirectoryEntry { private int yHotspot; @Override - protected void read(final DataInput pStream) throws IOException { - super.read(pStream); + protected void read(final DataInput stream) throws IOException { + super.read(stream); // NOTE: This is a hack... xHotspot = planes; diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReader.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReader.java index 88657bf1..500390e2 100644 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReader.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReader.java @@ -46,7 +46,7 @@ public final class ICOImageReader extends DIBImageReader { super(new ICOImageReaderSpi()); } - protected ICOImageReader(final ImageReaderSpi pProvider) { - super(pProvider); + ICOImageReader(final ImageReaderSpi provider) { + super(provider); } } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReaderSpi.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReaderSpi.java index e21f1937..749888fe 100755 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReaderSpi.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageReaderSpi.java @@ -32,7 +32,6 @@ package com.twelvemonkeys.imageio.plugins.bmp; import com.twelvemonkeys.imageio.spi.ImageReaderSpiBase; -import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import java.io.IOException; import java.util.Locale; @@ -49,32 +48,32 @@ public final class ICOImageReaderSpi extends ImageReaderSpiBase { super(new ICOProviderInfo()); } - public boolean canDecodeInput(final Object pSource) throws IOException { - return pSource instanceof ImageInputStream && canDecode((ImageInputStream) pSource, DIB.TYPE_ICO); + public boolean canDecodeInput(final Object source) throws IOException { + return source instanceof ImageInputStream && canDecode((ImageInputStream) source, DIB.TYPE_ICO); } - static boolean canDecode(final ImageInputStream pInput, final int pType) throws IOException { + static boolean canDecode(final ImageInputStream input, final int type) throws IOException { byte[] signature = new byte[4]; try { - pInput.mark(); - pInput.readFully(signature); + input.mark(); + input.readFully(signature); - int count = pInput.readByte() + (pInput.readByte() << 8); + int count = input.readByte() + (input.readByte() << 8); - return (signature[0] == 0x0 && signature[1] == 0x0 && signature[2] == pType + return (signature[0] == 0x0 && signature[1] == 0x0 && signature[2] == type && signature[3] == 0x0 && count > 0); } finally { - pInput.reset(); + input.reset(); } } - public ImageReader createReaderInstance(final Object pExtension) throws IOException { + public ICOImageReader createReaderInstance(final Object extension) { return new ICOImageReader(this); } - public String getDescription(final Locale pLocale) { + public String getDescription(final Locale locale) { return "Windows Icon Format (ICO) Reader"; } } diff --git a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageWriter.java b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageWriter.java index 1f8709c3..ea809460 100644 --- a/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageWriter.java +++ b/imageio/imageio-bmp/src/main/java/com/twelvemonkeys/imageio/plugins/bmp/ICOImageWriter.java @@ -33,15 +33,19 @@ package com.twelvemonkeys.imageio.plugins.bmp; import com.twelvemonkeys.imageio.stream.SubImageOutputStream; import com.twelvemonkeys.imageio.util.ProgressListenerBase; -import javax.imageio.*; +import javax.imageio.IIOException; +import javax.imageio.IIOImage; +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; import javax.imageio.event.IIOWriteWarningListener; import javax.imageio.metadata.IIOMetadata; import javax.imageio.spi.ImageWriterSpi; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; -import java.awt.image.BufferedImage; -import java.awt.image.ColorModel; -import java.awt.image.RenderedImage; +import java.awt.image.*; import java.io.File; import java.io.IOException; import java.util.Iterator; @@ -64,7 +68,7 @@ public final class ICOImageWriter extends DIBImageWriter { private ImageWriter pngDelegate; - protected ICOImageWriter(final ImageWriterSpi provider) { + ICOImageWriter(final ImageWriterSpi provider) { super(provider); } @@ -124,7 +128,7 @@ public final class ICOImageWriter extends DIBImageWriter { } @Override - public void endWriteSequence() throws IOException { + public void endWriteSequence() { assertOutput(); if (sequenceIndex < 0) { diff --git a/imageio/imageio-bmp/src/test/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderTest.java b/imageio/imageio-bmp/src/test/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderTest.java index 6138e0ff..ea491e9c 100755 --- a/imageio/imageio-bmp/src/test/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderTest.java +++ b/imageio/imageio-bmp/src/test/java/com/twelvemonkeys/imageio/plugins/bmp/BMPImageReaderTest.java @@ -30,24 +30,14 @@ package com.twelvemonkeys.imageio.plugins.bmp; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeNoException; -import static org.mockito.ArgumentMatchers.anyFloat; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.mock; +import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest; +import com.twelvemonkeys.xml.XMLSerializer; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.InOrder; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import javax.imageio.IIOException; import javax.imageio.ImageIO; @@ -57,16 +47,25 @@ import javax.imageio.ImageTypeSpecifier; import javax.imageio.event.IIOReadProgressListener; import javax.imageio.metadata.IIOMetadata; import javax.imageio.metadata.IIOMetadataNode; +import javax.imageio.spi.IIORegistry; import javax.imageio.spi.ImageReaderSpi; +import java.awt.*; +import java.awt.image.*; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; -import org.junit.Ignore; -import org.junit.Test; -import org.mockito.InOrder; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest; -import com.twelvemonkeys.xml.XMLSerializer; +import static org.junit.Assert.*; +import static org.junit.Assume.assumeNoException; +import static org.mockito.ArgumentMatchers.anyFloat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; /** * BMPImageReaderTest @@ -326,10 +325,8 @@ public class BMPImageReaderTest extends ImageReaderAbstractTest public void testMetadataEqualsJRE() throws IOException { ImageReader jreReader; try { - @SuppressWarnings("unchecked") - Class jreReaderClass = (Class) Class.forName("com.sun.imageio.plugins.bmp.BMPImageReader"); - Constructor constructor = jreReaderClass.getConstructor(ImageReaderSpi.class); - jreReader = constructor.newInstance(new Object[] {null}); + ImageReaderSpi provider = (ImageReaderSpi) IIORegistry.getDefaultInstance().getServiceProviderByClass(Class.forName("com.sun.imageio.plugins.bmp.BMPImageReaderSpi")); + jreReader = provider.createReaderInstance(); } catch (Exception e) { e.printStackTrace(); diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageReaderAbstractTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageReaderAbstractTest.java index 9e55d83b..75b77683 100644 --- a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageReaderAbstractTest.java +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageReaderAbstractTest.java @@ -31,6 +31,7 @@ package com.twelvemonkeys.imageio.util; import com.twelvemonkeys.imageio.stream.URLImageInputStreamSpi; +import com.twelvemonkeys.lang.Validate; import org.junit.Ignore; import org.junit.Test; @@ -38,14 +39,18 @@ import org.mockito.InOrder; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import javax.imageio.*; +import javax.imageio.IIOException; +import javax.imageio.ImageIO; +import javax.imageio.ImageReadParam; +import javax.imageio.ImageReader; +import javax.imageio.ImageTypeSpecifier; import javax.imageio.event.IIOReadProgressListener; import javax.imageio.metadata.IIOMetadata; import javax.imageio.spi.IIORegistry; import javax.imageio.spi.ImageReaderSpi; import javax.imageio.stream.ImageInputStream; import java.awt.*; -import java.awt.geom.AffineTransform; +import java.awt.geom.*; import java.awt.image.*; import java.io.File; import java.io.IOException; @@ -53,6 +58,7 @@ import java.lang.reflect.ParameterizedType; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -602,7 +608,7 @@ public abstract class ImageReaderAbstractTest { assertReadWithSourceRegionParamEqualImage(new Rectangle(3, 3, 9, 9), getTestData().get(0), 0); } - protected void assertReadWithSourceRegionParamEqualImage(final Rectangle r, final TestData data, final int imageIndex) throws IOException { + protected void assertReadWithSourceRegionParamEqualImage(final Rectangle r, final TestData data, @SuppressWarnings("SameParameterValue") final int imageIndex) throws IOException { ImageReader reader = createReader(); try (ImageInputStream inputStream = data.getInputStream()) { reader.setInput(inputStream); @@ -1828,52 +1834,51 @@ public abstract class ImageReaderAbstractTest { private final List sizes; private final List images; - public TestData(final Object pInput, final Dimension... pSizes) { - this(pInput, Arrays.asList(pSizes), null); + public TestData(final Object input, final Dimension... dimensions) { + this(input, Arrays.asList(dimensions), null); } - public TestData(final Object pInput, final BufferedImage... pImages) { - this(pInput, null, Arrays.asList(pImages)); + public TestData(final Object input, final BufferedImage... images) { + this(input, null, Arrays.asList(images)); } - public TestData(final Object pInput, final List pSizes, final List pImages) { - if (pInput == null) { - throw new IllegalArgumentException("input == null"); - } + public TestData(final Object input, final List dimensions, final List images) { + Validate.notNull(input, "input"); + Validate.isTrue(dimensions != null || images != null, "Need either dimensions or image"); - sizes = new ArrayList<>(); - images = new ArrayList<>(); + List combinedDimensions; + if (dimensions == null) { + // Copy dimensions from images + combinedDimensions = new ArrayList<>(images.size()); - List sizes = pSizes; - if (sizes == null) { - sizes = new ArrayList<>(); - if (pImages != null) { - for (BufferedImage image : pImages) { - sizes.add(new Dimension(image.getWidth(), image.getHeight())); - } - } - else { - throw new IllegalArgumentException("Need either size or image"); + for (BufferedImage image : images) { + combinedDimensions.add(new Dimension(image.getWidth(), image.getHeight())); } } - else if (pImages != null) { - if (pImages.size() != pSizes.size()) { - throw new IllegalArgumentException("Size parameter and image size differs"); - } - for (int i = 0; i < sizes.size(); i++) { - if (!new Dimension(pImages.get(i).getWidth(), pImages.get(i).getHeight()).equals(sizes.get(i))) { - throw new IllegalArgumentException("Size parameter and image size differs"); + else { + // Validate equal dimensions + if (images != null) { + if (images.size() != dimensions.size()) { + throw new IllegalArgumentException("Dimensions and images parameter's size differs"); } + for (int i = 0; i < dimensions.size(); i++) { + if (!new Dimension(images.get(i).getWidth(), images.get(i).getHeight()).equals(dimensions.get(i))) { + throw new IllegalArgumentException("Dimensions and images parameter's dimensions differ"); + } + } } + + combinedDimensions = new ArrayList<>(dimensions); } - this.sizes.addAll(sizes); - if (pImages != null) { - images.addAll(pImages); - } + this.sizes = Collections.unmodifiableList(combinedDimensions); - input = pInput; + this.images = images != null + ? Collections.unmodifiableList(new ArrayList<>(images)) + : Collections.emptyList(); + + this.input = input; } public Object getInput() { @@ -1898,13 +1903,13 @@ public abstract class ImageReaderAbstractTest { return sizes.size(); } - public Dimension getDimension(final int pIndex) { - return sizes.get(pIndex); + public Dimension getDimension(final int index) { + return sizes.get(index); } @SuppressWarnings("unused") - public BufferedImage getImage(final int pIndex) { - return images.get(pIndex); + public BufferedImage getImage(final int index) { + return images.get(index); } @Override diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageWriterAbstractTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageWriterAbstractTest.java index 3f3f3d4b..523b21ae 100755 --- a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageWriterAbstractTest.java +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/util/ImageWriterAbstractTest.java @@ -43,15 +43,17 @@ import javax.imageio.spi.IIORegistry; import javax.imageio.spi.ImageWriterSpi; import javax.imageio.stream.ImageOutputStream; import java.awt.*; -import java.awt.image.BufferedImage; -import java.awt.image.RenderedImage; +import java.awt.image.*; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.reflect.ParameterizedType; import java.net.URL; import java.util.List; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Mockito.*; /** @@ -108,8 +110,8 @@ public abstract class ImageWriterAbstractTest { return getTestData().get(index); } - protected URL getClassLoaderResource(final String pName) { - return getClass().getResource(pName); + protected URL getClassLoaderResource(final String name) { + return getClass().getResource(name); } @Test