Fixed a bug in the IFFImageWriter, caused by buffered data not being written to the stream. Adapter streams are now properly flushed/closed.

Test clean-up.
This commit is contained in:
Harald Kuhr 2012-04-02 14:25:28 +02:00
parent 36a05272a5
commit 13a4646ae4
2 changed files with 63 additions and 31 deletions

View File

@ -102,9 +102,15 @@ public class IFFImageWriter extends ImageWriterBase {
imageOutput.writeInt(IFF.CHUNK_BODY); imageOutput.writeInt(IFF.CHUNK_BODY);
imageOutput.writeInt(pImageData.size()); imageOutput.writeInt(pImageData.size());
// NOTE: This is much faster than mOutput.write(pImageData.toByteArray()) // NOTE: This is much faster than imageOutput.write(pImageData.toByteArray())
// as the data array is not duplicated // as the data array is not duplicated
pImageData.writeTo(IIOUtil.createStreamAdapter(imageOutput)); OutputStream adapter = IIOUtil.createStreamAdapter(imageOutput);
try {
pImageData.writeTo(adapter);
}
finally {
adapter.close();
}
if (pImageData.size() % 2 == 0) { if (pImageData.size() % 2 == 0) {
imageOutput.writeByte(0); // PAD imageOutput.writeByte(0); // PAD
@ -163,6 +169,8 @@ public class IFFImageWriter extends ImageWriterBase {
} }
} }
output.flush();
processImageProgress(y * 100f / height); processImageProgress(y * 100f / height);
} }

View File

@ -28,6 +28,7 @@
package com.twelvemonkeys.imageio.plugins.iff; package com.twelvemonkeys.imageio.plugins.iff;
import com.twelvemonkeys.image.MonochromeColorModel;
import com.twelvemonkeys.imageio.util.ImageWriterAbstractTestCase; import com.twelvemonkeys.imageio.util.ImageWriterAbstractTestCase;
import org.junit.Test; import org.junit.Test;
@ -64,15 +65,15 @@ public class IFFImageWriterTest extends ImageWriterAbstractTestCase {
@Override @Override
protected List<? extends RenderedImage> getTestData() { protected List<? extends RenderedImage> getTestData() {
return Arrays.asList( return Arrays.asList(
new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB)/*, new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_RGB), new BufferedImage(33, 20, BufferedImage.TYPE_BYTE_GRAY),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_BGR), new BufferedImage(31, 23, BufferedImage.TYPE_BYTE_INDEXED),
new BufferedImage(32, 20, BufferedImage.TYPE_3BYTE_BGR), new BufferedImage(30, 27, BufferedImage.TYPE_BYTE_BINARY),
new BufferedImage(32, 20, BufferedImage.TYPE_4BYTE_ABGR), new BufferedImage(29, 29, BufferedImage.TYPE_BYTE_INDEXED, MonochromeColorModel.getInstance()),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_GRAY), new BufferedImage(28, 31, BufferedImage.TYPE_INT_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_INDEXED), new BufferedImage(27, 33, BufferedImage.TYPE_INT_RGB),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_BINARY), new BufferedImage(24, 37, BufferedImage.TYPE_3BYTE_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_INDEXED, MonochromeColorModel.getInstance())*/ new BufferedImage(23, 41, BufferedImage.TYPE_4BYTE_ABGR)
); );
} }
@ -109,29 +110,52 @@ public class IFFImageWriterTest extends ImageWriterAbstractTestCase {
assertNotNull(written); assertNotNull(written);
assertEquals(original.getWidth(), written.getWidth()); assertEquals(original.getWidth(), written.getWidth());
assertEquals(original.getHeight(), written.getHeight()); assertEquals(original.getHeight(), written.getHeight());
assertSameType(original, written);
for (int y = 0; y < original.getHeight(); y++) { assertSameData(original, written);
for (int x = 0; x < original.getWidth(); x++) {
int originalRGB = original.getRGB(x, y);
int writtenRGB = written.getRGB(x, y);
if (original.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_GRAY) {
// NOTE: For some reason, gray data seems to be one step off...
assertEquals("Test data " + i + " R(" + x + "," + y + ")", originalRGB & 0xff0000, writtenRGB & 0xff0000, 0x10000);
assertEquals("Test data " + i + " G(" + x + "," + y + ")", originalRGB & 0x00ff00, writtenRGB & 0x00ff00, 0x100);
assertEquals("Test data " + i + " B(" + x + "," + y + ")", originalRGB & 0x0000ff, writtenRGB & 0x0000ff, 0x1);
}
else {
assertEquals("Test data " + i + " R(" + x + "," + y + ")", originalRGB & 0xff0000, writtenRGB & 0xff0000);
assertEquals("Test data " + i + " G(" + x + "," + y + ")", originalRGB & 0x00ff00, writtenRGB & 0x00ff00);
assertEquals("Test data " + i + " B(" + x + "," + y + ")", originalRGB & 0x0000ff, writtenRGB & 0x0000ff);
}
}
}
} }
catch (IOException e) { catch (IOException e) {
fail(i + " " + e); AssertionError fail = new AssertionError("Failure writing test data " + i + " " + e);
fail.initCause(e);
throw fail;
} }
} }
} }
private static void assertSameData(BufferedImage expected, BufferedImage actual) {
for (int y = 0; y < expected.getHeight(); y++) {
for (int x = 0; x < expected.getWidth(); x++) {
int expectedRGB = expected.getRGB(x, y);
int actualRGB = actual.getRGB(x, y);
if (expected.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_GRAY) {
// NOTE: For some reason, gray data seems to be one step off...
assertEquals("R(" + x + "," + y + ")", expectedRGB & 0xff0000, actualRGB & 0xff0000, 0x10000);
assertEquals("G(" + x + "," + y + ")", expectedRGB & 0x00ff00, actualRGB & 0x00ff00, 0x100);
assertEquals("B(" + x + "," + y + ")", expectedRGB & 0x0000ff, actualRGB & 0x0000ff, 0x1);
}
else {
assertEquals("R(" + x + "," + y + ")", expectedRGB & 0xff0000, actualRGB & 0xff0000);
assertEquals("G(" + x + "," + y + ")", expectedRGB & 0x00ff00, actualRGB & 0x00ff00);
assertEquals("B(" + x + "," + y + ")", expectedRGB & 0x0000ff, actualRGB & 0x0000ff);
}
}
}
}
private static void assertSameType(BufferedImage expected, BufferedImage actual) {
if (expected.getType() != actual.getType()) {
if (expected.getType() == BufferedImage.TYPE_INT_RGB || expected.getType() == BufferedImage.TYPE_INT_BGR) {
assertEquals(BufferedImage.TYPE_3BYTE_BGR, actual.getType());
}
else if (expected.getType() == BufferedImage.TYPE_INT_ARGB || expected.getType() == BufferedImage.TYPE_INT_ARGB_PRE) {
assertEquals(BufferedImage.TYPE_4BYTE_ABGR, actual.getType());
}
else if (expected.getType() == BufferedImage.TYPE_BYTE_INDEXED && expected.getColorModel().getPixelSize() <= 16) {
assertEquals(BufferedImage.TYPE_BYTE_BINARY, actual.getType());
}
// NOTE: Actually, TYPE_GRAY may be converted to TYPE_BYTE_INDEXED with linear gray color-map,
// without being a problem (just a waste of time and space).
}
}
} }