Better writer tests.

Fixed a bug in PICTWriter.
Minor changes in ImageReader/WriterBase classes.
This commit is contained in:
Harald Kuhr
2012-03-30 16:58:09 +02:00
parent 7431065519
commit 0307237852
7 changed files with 243 additions and 41 deletions
@@ -198,6 +198,7 @@ public class PICTImageWriter extends ImageWriterBase {
// Pixel type, 16 is allright for direct pixels
imageOutput.writeShort(16);
// TODO: Support others?
// Pixel size
imageOutput.writeShort(32);
@@ -257,20 +258,19 @@ public class PICTImageWriter extends ImageWriterBase {
// Treat the scanline.
for (int j = 0; j < w; j++) {
if (model instanceof ComponentColorModel && model.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
// TODO: Component order?
// NOTE: Assumes component order always (A)BGR
// TODO: Alpha support
scanlineBytes[x + j] = pixels[off + i * scansize * components + components * j + 2];
scanlineBytes[x + w + j] = pixels[off + i * scansize * components + components * j + 1];
scanlineBytes[x + 2 * w + j] = pixels[off + i * scansize * components + components * j];
scanlineBytes[x + j] = pixels[off + i * scansize * components + components * j + components - 1];
scanlineBytes[x + w + j] = pixels[off + i * scansize * components + components * j + components - 2];
scanlineBytes[x + 2 * w + j] = pixels[off + i * scansize * components + components * j + components - 3];
}
else {
int rgb = model.getRGB(pixels[off + i * scansize + j] & 0xFF);
// Set red, green and blue components.
scanlineBytes[x + j] = (byte) ((rgb >> 16) & 0xFF);
scanlineBytes[x + w + j] = (byte) ((rgb >> 8) & 0xFF);
scanlineBytes[x + 2 * w + j] = (byte) (rgb & 0xFF);
scanlineBytes[x + j] = (byte) ((rgb >> 16) & 0xFF);
scanlineBytes[x + w + j] = (byte) ((rgb >> 8) & 0xFF);
scanlineBytes[x + 2 * w + j] = (byte) ((rgb ) & 0xFF);
}
}
// If we have a complete scanline, then pack it and write it out.
@@ -288,7 +288,9 @@ public class PICTImageWriter extends ImageWriterBase {
imageOutput.writeByte(bytes.size());
}
bytes.writeTo(IIOUtil.createStreamAdapter(imageOutput));
OutputStream adapter = IIOUtil.createStreamAdapter(imageOutput);
bytes.writeTo(adapter);
adapter.flush();
scanWidthLeft = w;
}
@@ -313,9 +315,9 @@ public class PICTImageWriter extends ImageWriterBase {
int rgb = model.getRGB(pixels[off + i * scansize + j]);
// Set red, green and blue components.
scanlineBytes[x + j] = (byte) ((rgb >> 16) & 0xFF);
scanlineBytes[x + w + j] = (byte) ((rgb >> 8) & 0xFF);
scanlineBytes[x + 2 * w + j] = (byte) (rgb & 0xFF);
scanlineBytes[x + j] = (byte) ((rgb >> 16) & 0xFF);
scanlineBytes[x + w + j] = (byte) ((rgb >> 8) & 0xFF);
scanlineBytes[x + 2 * w + j] = (byte) ((rgb ) & 0xFF);
}
// If we have a complete scanline, then pack it and write it out.
@@ -333,7 +335,9 @@ public class PICTImageWriter extends ImageWriterBase {
imageOutput.writeByte(bytes.size());
}
bytes.writeTo(IIOUtil.createStreamAdapter(imageOutput));
OutputStream adapter = IIOUtil.createStreamAdapter(imageOutput);
bytes.writeTo(adapter);
adapter.flush();
scanWidthLeft = w;
}
@@ -29,10 +29,23 @@
package com.twelvemonkeys.imageio.plugins.pict;
import com.twelvemonkeys.imageio.util.ImageWriterAbstractTestCase;
import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* PICTImageWriterTest
@@ -50,8 +63,69 @@ public class PICTImageWriterTest extends ImageWriterAbstractTestCase {
}
@Override
protected RenderedImage getTestData() {
// TODO: Alpha support
return new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
protected List<? extends RenderedImage> getTestData() {
return Arrays.asList(
new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_BGR),
new BufferedImage(32, 20, BufferedImage.TYPE_INT_ARGB),
new BufferedImage(30, 20, BufferedImage.TYPE_3BYTE_BGR),
new BufferedImage(30, 20, BufferedImage.TYPE_4BYTE_ABGR),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_INDEXED),
new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_GRAY)
// new BufferedImage(32, 20, BufferedImage.TYPE_BYTE_BINARY) // Packed does not work
);
}
@Test
public void testWriteReadCompare() throws IOException {
ImageWriter writer = createImageWriter();
List<? extends RenderedImage> testData = getTestData();
for (int i = 0; i < testData.size(); i++) {
RenderedImage image = testData.get(i);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ImageOutputStream stream = ImageIO.createImageOutputStream(buffer);
writer.setOutput(stream);
BufferedImage original = drawSomething((BufferedImage) image);
try {
writer.write(original);
}
catch (IOException e) {
fail(e.getMessage());
}
finally {
stream.close(); // Force data to be written
}
assertTrue("No image data written", buffer.size() > 0);
ImageInputStream input = ImageIO.createImageInputStream(new ByteArrayInputStream(buffer.toByteArray()));
BufferedImage written = ImageIO.read(input);
assertNotNull(written);
assertEquals(original.getWidth(), written.getWidth());
assertEquals(original.getHeight(), written.getHeight());
for (int y = 0; y < original.getHeight(); y++) {
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);
}
}
}
}
}
}