mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-05-01 00:00:02 -04:00
CITT Group 3/4 Support - Pass Mode / 2D Change referencing in work
This commit is contained in:
+128
-91
@@ -45,122 +45,159 @@ import static org.junit.Assert.*;
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haraldk$
|
||||
* @version $Id: CCITTFaxDecoderStreamTest.java,v 1.0 09.03.13 14:44 haraldk Exp$
|
||||
* @version $Id: CCITTFaxDecoderStreamTest.java,v 1.0 09.03.13 14:44 haraldk
|
||||
* Exp$
|
||||
*/
|
||||
public class CCITTFaxDecoderStreamTest {
|
||||
|
||||
// TODO: Better tests (full A4 width scan lines?)
|
||||
static final byte[] DATA_G3_1D = { 0x00, 0x18, // 000000000001|1000| EOL|3W|
|
||||
0x4E, 0x00, // 010|0111|000000000 1B|2W|EOL
|
||||
0x30, (byte) 0x9C, // 001|1000|010|0111|00 |3W|1B|2W|EOL
|
||||
0x00, 0x61, // 0000000001|1000|01 |3W|1B
|
||||
0x38, 0x00, // 0|0111|00000000000 |2W|EOL
|
||||
(byte) 0xBE, (byte) 0xE0 }; // 1|0111|11|0111|00000 |2W|2B|2W|5F
|
||||
|
||||
// From http://www.mikekohn.net/file_formats/tiff.php
|
||||
static final byte[] DATA_TYPE_2 = {
|
||||
(byte) 0x84, (byte) 0xe0, // 10000100 11100000
|
||||
(byte) 0x84, (byte) 0xe0, // 10000100 11100000
|
||||
(byte) 0x84, (byte) 0xe0, // 10000100 11100000
|
||||
(byte) 0x7d, (byte) 0xc0, // 01111101 11000000
|
||||
};
|
||||
static final byte[] DATA_G3_1D_FILL = { 0x00, 0x01, (byte) 0x84, (byte) 0xE0, 0x01, (byte) 0x84, (byte) 0xE0, 0x01,
|
||||
(byte) 0x84, (byte) 0xE0, 0x1, 0x7D, (byte) 0xC0 };
|
||||
|
||||
static final byte[] DATA_TYPE_3 = {
|
||||
0x00, 0x01, (byte) 0xc2, 0x70,
|
||||
0x00, 0x01, 0x70,
|
||||
0x01,
|
||||
static final byte[] DATA_G3_2D = {
|
||||
0x00, 0x1C, // 000000000001|1|100 EOL|1|3W
|
||||
0x27, 0x00, // 0|010|0111|00000000 |1B|2W|EOL
|
||||
0x17, 0x00, // 0001|0|1|1|1|00000000 |0|V|V|V|EOL
|
||||
0x1C, 0x27, // 0001|1|1000|010|0111| |1|3W|1B|2W|
|
||||
0x00, 0x12, // 000000000001|0|010| EOL|0|V-1|
|
||||
(byte) 0xC0 }; // 1|1|000000 V|V|6F
|
||||
|
||||
};
|
||||
static final byte[] DATA_G3_2D_FILL = { 0x00, 0x01, (byte) 0xC2, 0x70, 0x01, 0x70, 0x01, (byte) 0xC2, 0x70, 0x01,
|
||||
0x2C };
|
||||
|
||||
static final byte[] DATA_TYPE_4 = {
|
||||
0x26, (byte) 0xb0, 95, (byte) 0xfa, (byte) 0xc0
|
||||
};
|
||||
// EOF exception, not sure
|
||||
static final byte[] DATA_G3_2D_lsb2msb = { 0x00, 0x38, (byte) 0xE4, 0x00, (byte) 0xE8, 0x00, 0x38, (byte) 0xE4,
|
||||
0x00, 0x48, 0x03 };
|
||||
|
||||
// Image should be (6 x 4):
|
||||
// 1 1 1 0 1 1 x x
|
||||
// 1 1 1 0 1 1 x x
|
||||
// 1 1 1 0 1 1 x x
|
||||
// 1 1 0 0 1 1 x x
|
||||
BufferedImage image;
|
||||
static final byte[] DATA_G4 = {
|
||||
0x04, 0x17, // 0000 0100 0001 01|11
|
||||
(byte) 0xF5, (byte) 0x80, // 1|111| 0101 1000 0000
|
||||
0x08, 0x00, // 0000 1000 0000 0000
|
||||
(byte) 0x80 }; // 1000 0000
|
||||
// Line 1: V-3, V-2, V0
|
||||
// Line 2: V0 V0 V0
|
||||
// Line 3: V0 V0 V0
|
||||
// Line 4: V-1, V0, V0 EOL EOL
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
image = new BufferedImage(6, 4, BufferedImage.TYPE_BYTE_BINARY);
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 6; x++) {
|
||||
image.setRGB(x, y, x == 3 ? 0xff000000 : 0xffffffff);
|
||||
}
|
||||
}
|
||||
// TODO: Better tests (full A4 width scan lines?)
|
||||
|
||||
image.setRGB(2, 3, 0xff000000);
|
||||
}
|
||||
// From http://www.mikekohn.net/file_formats/tiff.php
|
||||
static final byte[] DATA_TYPE_2 = { (byte) 0x84, (byte) 0xe0, // 10000100
|
||||
// 11100000
|
||||
(byte) 0x84, (byte) 0xe0, // 10000100 11100000
|
||||
(byte) 0x84, (byte) 0xe0, // 10000100 11100000
|
||||
(byte) 0x7d, (byte) 0xc0, // 01111101 11000000
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testReadCountType2() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_TYPE_2), 6, TIFFBaseline.COMPRESSION_CCITT_MODIFIED_HUFFMAN_RLE, 1);
|
||||
static final byte[] DATA_TYPE_3 = { 0x00, 0x01, (byte) 0xc2, 0x70, // 00000000
|
||||
// 00000001
|
||||
// 11000010
|
||||
// 01110000
|
||||
0x00, 0x01, 0x78, // 00000000 00000001 01111000
|
||||
0x00, 0x01, 0x78, // 00000000 00000001 01110000
|
||||
0x00, 0x01, 0x56, // 00000000 00000001 01010110
|
||||
// 0x01, // 00000001
|
||||
|
||||
int count = 0;
|
||||
int read;
|
||||
while ((read = stream.read()) >= 0) {
|
||||
count++;
|
||||
}
|
||||
};
|
||||
|
||||
// Just make sure we'll have 4 bytes
|
||||
assertEquals(4, count);
|
||||
// 001 00110101 10 000010 1 1 1 1 1 1 1 1 1 1 010 11 (000000 padding)
|
||||
static final byte[] DATA_TYPE_4 = { 0x26, // 001 00110
|
||||
(byte) 0xb0, // 101 10 000
|
||||
0x5f, // 010 1 1 1 1 1
|
||||
(byte) 0xfa, // 1 1 1 1 1 010
|
||||
(byte) 0xc0 // 11 (000000 padding)
|
||||
};
|
||||
|
||||
// Verify that we don't return arbitrary values
|
||||
assertEquals(-1, read);
|
||||
}
|
||||
// Image should be (6 x 4):
|
||||
// 1 1 1 0 1 1 x x
|
||||
// 1 1 1 0 1 1 x x
|
||||
// 1 1 1 0 1 1 x x
|
||||
// 1 1 0 0 1 1 x x
|
||||
BufferedImage image;
|
||||
|
||||
@Test
|
||||
public void testDecodeType2() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_TYPE_2), 6, TIFFBaseline.COMPRESSION_CCITT_MODIFIED_HUFFMAN_RLE, 1);
|
||||
@Before
|
||||
public void init() {
|
||||
image = new BufferedImage(6, 4, BufferedImage.TYPE_BYTE_BINARY);
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 6; x++) {
|
||||
image.setRGB(x, y, x == 3 ? 0xff000000 : 0xffffffff);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
image.setRGB(2, 3, 0xff000000);
|
||||
}
|
||||
|
||||
// JPanel panel = new JPanel();
|
||||
// panel.add(new JLabel("Expected", new BufferedImageIcon(image, 300, 300, true), JLabel.CENTER));
|
||||
// panel.add(new JLabel("Actual", new BufferedImageIcon(new BufferedImage(image.getColorModel(), Raster.createPackedRaster(new DataBufferByte(bytes, bytes.length), 6, 4, 1, null), false, null), 300, 300, true), JLabel.CENTER));
|
||||
// JOptionPane.showConfirmDialog(null, panel);
|
||||
@Test
|
||||
public void testDecodeType2() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_TYPE_2), 6,
|
||||
TIFFBaseline.COMPRESSION_CCITT_MODIFIED_HUFFMAN_RLE, 1, 0L);
|
||||
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDecodeType3() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_TYPE_3), 6, TIFFExtension.COMPRESSION_CCITT_T4, 1);
|
||||
@Test
|
||||
public void testDecodeType3_1D() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_G3_1D), 6,
|
||||
TIFFExtension.COMPRESSION_CCITT_T4, 1, 0L);
|
||||
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
DataInputStream dataInput = new DataInputStream(stream);
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
|
||||
for (int y = 0; y < image.getHeight(); y++) {
|
||||
System.err.println("y: " + y);
|
||||
dataInput.readFully(bytes, y * image.getWidth(), image.getWidth());
|
||||
}
|
||||
@Test
|
||||
public void testDecodeType3_1D_FILL() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_G3_1D_FILL), 6,
|
||||
TIFFExtension.COMPRESSION_CCITT_T4, 1, TIFFExtension.GROUP3OPT_FILLBITS);
|
||||
|
||||
// JPanel panel = new JPanel();
|
||||
// panel.add(new JLabel("Expected", new BufferedImageIcon(image, 300, 300, true), JLabel.CENTER));
|
||||
// panel.add(new JLabel("Actual", new BufferedImageIcon(new BufferedImage(image.getColorModel(), Raster.createPackedRaster(new DataBufferByte(bytes, bytes.length), 6, 4, 1, null), false, null), 300, 300, true), JLabel.CENTER));
|
||||
// JOptionPane.showConfirmDialog(null, panel);
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
@Test
|
||||
public void testDecodeType3_2D() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_G3_2D), 6,
|
||||
TIFFExtension.COMPRESSION_CCITT_T4, 1, TIFFExtension.GROUP3OPT_2DENCODING);
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDecodeType4() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_TYPE_4), 6, TIFFExtension.COMPRESSION_CCITT_T6, 1);
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
DataInputStream dataInput = new DataInputStream(stream);
|
||||
@Test
|
||||
public void testDecodeType3_2D_FILL() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_G3_2D_FILL), 6,
|
||||
TIFFExtension.COMPRESSION_CCITT_T4, 1,
|
||||
TIFFExtension.GROUP3OPT_2DENCODING | TIFFExtension.GROUP3OPT_FILLBITS);
|
||||
|
||||
for (int y = 0; y < image.getHeight(); y++) {
|
||||
System.err.println("y: " + y);
|
||||
dataInput.readFully(bytes, y * image.getWidth(), image.getWidth());
|
||||
}
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeType4() throws IOException {
|
||||
InputStream stream = new CCITTFaxDecoderStream(new ByteArrayInputStream(DATA_G4), 6,
|
||||
TIFFExtension.COMPRESSION_CCITT_T6, 1,
|
||||
0L);
|
||||
|
||||
// JPanel panel = new JPanel();
|
||||
// panel.add(new JLabel("Expected", new BufferedImageIcon(image, 300, 300, true), JLabel.CENTER));
|
||||
// panel.add(new JLabel("Actual", new BufferedImageIcon(new BufferedImage(image.getColorModel(), Raster.createPackedRaster(new DataBufferByte(bytes, bytes.length), 6, 4, 1, null), false, null), 300, 300, true), JLabel.CENTER));
|
||||
// JOptionPane.showConfirmDialog(null, panel);
|
||||
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
byte[] imageData = ((DataBufferByte) image.getData().getDataBuffer()).getData();
|
||||
byte[] bytes = new byte[imageData.length];
|
||||
new DataInputStream(stream).readFully(bytes);
|
||||
assertArrayEquals(imageData, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user