Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.digitalcollections.openjpeg.lib.enums.COLOR_SPACE;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.IOException;
import java.util.Iterator;
import java.util.stream.Stream;
Expand Down Expand Up @@ -228,6 +229,21 @@ public BufferedImage readTile(int imageIndex, int tileX, int tileY) throws IOExc
return this.read(imageIndex, param);
}

@Override
public boolean canReadRaster() {
return true;
}

@Override
public Raster readRaster(int imageIndex, ImageReadParam param) throws IOException {
return this.read(imageIndex, param).getData();
}

@Override
public Raster readTileRaster(int imageIndex, int tileX, int tileY) throws IOException {
return this.readTile(imageIndex,tileX,tileY).getData();
}

@Override
public IIOMetadata getStreamMetadata() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -152,29 +153,79 @@ private void assertImageEquals(String expectedImageName, String actualImageName)
assertImageEquals(expectedImg, actualImage);
}

public static void compareImagesByRaster(BufferedImage expectedImage, BufferedImage actualImage,
Raster actualRaster, int tolerance) {
int width = expectedImage.getWidth();
assertEquals(width, actualImage.getWidth());
int height = expectedImage.getHeight();
assertEquals(height, actualImage.getHeight());

Raster expectedRaster = expectedImage.getData();
int bands = expectedRaster.getNumBands();

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int totalDiff = 0;
for (int b = 0; b < bands; b++) {
int expectedSample = expectedRaster.getSample(x, y, b);
int actualSample = actualRaster.getSample(x, y, b);
totalDiff += Math.abs(expectedSample - actualSample);
}
if (totalDiff > tolerance) {
assertEquals(totalDiff, tolerance, "Tolerance of Raster " + x + "," + y);
}
}
}
}

private void assertRasterEquals(String expectedImageName, String actualImageName)
throws IOException {
assertRasterEquals(expectedImageName, actualImageName, 5);
}

private void assertRasterEquals(String expectedImageName, String actualImageName, int tolerance)
throws IOException {
OpenJp2ImageReader reader = getReader(actualImageName);
BufferedImage actualImage = reader.read(0, null);
reader = getReader(actualImageName);
Raster actualRaster = reader.readRaster(0, null);
InputStream input = ClassLoader.getSystemResourceAsStream(expectedImageName);
assertThat(input).isNotNull();
BufferedImage expectedImg = ImageIO.read(input);

compareImagesByRaster(expectedImg, actualImage, actualRaster, tolerance);
}

@Test
public void testReadRGBA() throws Exception {
assertImageEquals("rgba.png", "rgba.jp2");
assertRasterEquals("rgba.png", "rgba.jp2");
}

@Test
public void testReadCMYK() throws Exception {
assertImageEquals("cmyk.png", "cmyk.jp2");
// need higher tolerance due to lower image quality which will appear in raster sample
assertRasterEquals("cmyk.png", "cmyk.jp2", 1500);
}

@Test
public void testReadCMYK_withAlpha() throws Exception {
assertImageEquals("cmykWithAlpha.png", "cmykWithAlpha.jp2");
// need higher tolerance due to lower image quality which will appear in raster sample
assertRasterEquals("cmykWithAlpha.png", "cmykWithAlpha.jp2",1500);
}

@Test
public void testReadGrayWithAlpha() throws Exception {
assertImageEquals("grayWithAlpha.png", "grayWithAlpha.jp2");
assertRasterEquals("grayWithAlpha.png", "grayWithAlpha.jp2");
}

@Test
public void testReadGray16bitWithoutAlpha() throws Exception {
assertImageEquals("gray16bitWithoutAlpha.png", "gray16bitWithoutAlpha.jp2");
assertRasterEquals("gray16bitWithoutAlpha.png", "gray16bitWithoutAlpha.jp2");
}

@Test
Expand All @@ -190,5 +241,6 @@ public void testReadYUV() throws Exception {
@Test
public void testReadWeirdGrayscale() throws IOException {
assertImageEquals("gray2_control.png", "gray2.jp2");
assertRasterEquals("gray2_control.png", "gray2.jp2");
}
}