Skip to content

Commit

Permalink
wip: Support fake alpha for BMP files
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Jan 11, 2023
1 parent 164cc11 commit 8d06409
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,14 @@ public ImageTypeSpecifier getRawImageType(int pImageIndex) throws IOException {
);
}

// TODO: use some heuristic to determine if we should treat the extra byte as an alpha channel
//
// According to https://entropymine.com/jason/bmpsuite/bmpsuite/html/bmpsuite.html some BMP decoders
// will treat the unused bits in a 32 bit representation as an alpha channel if any of the unused bits is non-zero
// but that potentially requires doing a full pass over all the pizels (in that case that it doesn't use fake alpha).

// Default if no mask
return ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
return ImageTypeSpecifiers.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);

case 0:
if (header.getCompression() == DIB.COMPRESSION_JPEG || header.getCompression() == DIB.COMPRESSION_PNG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,23 @@ public void testMetadataEqualsJRE() throws IOException {
}
}

@Test
public void testFakeAlpha() throws IOException {
final ImageReader reader = createReader();
TestData data = new TestData(getClassLoaderResource("/bmpsuite/q/rgb32fakealpha.bmp"), new Dimension(127, 64));
reader.setInput(data.getInputStream());
try {
reader.read(0);
}
catch (IOException e) {
fail("Could not read image");
}

ImageTypeSpecifier rawType = reader.getRawImageType(0);
assertNotNull(rawType);
assertTrue("BMP with fake alpha should support the alpha channel", rawType.getColorModel().hasAlpha());
}

private void assertNodeEquals(final String message, final Node expected, final Node actual) {
assertEquals(message + " class differs", expected.getClass(), actual.getClass());

Expand Down Expand Up @@ -473,4 +490,4 @@ private String toString(final NodeList list) {

return builder.toString();
}
}
}

0 comments on commit 8d06409

Please sign in to comment.