mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-05 04:25:29 -04:00
#651: Fix ExtraSamplesColorModel equals + hashcode to behave nicely with ImageTypeSpecifier comparison.
This commit is contained in:
parent
aa4b5db054
commit
98e4b76206
@ -35,6 +35,7 @@ import com.twelvemonkeys.lang.Validate;
|
||||
import java.awt.*;
|
||||
import java.awt.color.ColorSpace;
|
||||
import java.awt.image.*;
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.awt.image.DataBuffer.getDataTypeSize;
|
||||
|
||||
@ -119,4 +120,44 @@ final class ExtraSamplesColorModel extends ComponentColorModel {
|
||||
|
||||
throw new IllegalArgumentException("This method has not been implemented for transferType " + transferType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other == null || getClass() != other.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ExtraSamplesColorModel that = (ExtraSamplesColorModel) other;
|
||||
|
||||
if (hasAlpha() != that.hasAlpha() ||
|
||||
isAlphaPremultiplied() != that.isAlphaPremultiplied() ||
|
||||
getPixelSize() != that.getPixelSize() ||
|
||||
getTransparency() != that.getTransparency() ||
|
||||
numComponents != that.numComponents) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int[] nBits = getComponentSize();
|
||||
int[] nb = that.getComponentSize();
|
||||
|
||||
if ((nBits == null) || (nb == null)) {
|
||||
return ((nBits == null) && (nb == null));
|
||||
}
|
||||
|
||||
for (int i = 0; i < nBits.length; i++) {
|
||||
if (nBits[i] != nb[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), numComponents, componentSize);
|
||||
}
|
||||
}
|
||||
|
@ -40,15 +40,14 @@ import java.awt.color.ColorSpace;
|
||||
import java.awt.image.*;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ExtraSamplesColorModelTest {
|
||||
|
||||
private BufferedImage createExtraSamplesImage(int w, int h, ColorSpace cs, boolean hasAlpha, int extraComponents) {
|
||||
int samplesPerPixel = cs.getNumComponents() + (hasAlpha ? 1 : 0) + extraComponents;
|
||||
|
||||
ExtraSamplesColorModel colorModel = new ExtraSamplesColorModel(cs, hasAlpha, true, DataBuffer.TYPE_BYTE, extraComponents);
|
||||
ExtraSamplesColorModel colorModel = createExtraSamplesColorModel(cs, hasAlpha, extraComponents);
|
||||
SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, w, h, samplesPerPixel, samplesPerPixel * w, createOffsets(samplesPerPixel));
|
||||
|
||||
WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
|
||||
@ -56,6 +55,10 @@ public class ExtraSamplesColorModelTest {
|
||||
return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), new Hashtable<>());
|
||||
}
|
||||
|
||||
private ExtraSamplesColorModel createExtraSamplesColorModel(ColorSpace cs, boolean hasAlpha, int extraComponents) {
|
||||
return new ExtraSamplesColorModel(cs, hasAlpha, true, DataBuffer.TYPE_BYTE, extraComponents);
|
||||
}
|
||||
|
||||
private static int[] createOffsets(int samplesPerPixel) {
|
||||
int[] offsets = new int[samplesPerPixel];
|
||||
for (int i = 0; i < samplesPerPixel; i++) {
|
||||
@ -151,4 +154,19 @@ public class ExtraSamplesColorModelTest {
|
||||
assertEquals(Color.BLACK.getRGB(), image.getRGB(0, 0));
|
||||
assertEquals(Color.WHITE.getRGB(), image.getRGB(1, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
ExtraSamplesColorModel original = createExtraSamplesColorModel(ColorSpaces.getColorSpace(ColorSpace.CS_sRGB), true, 1);
|
||||
ExtraSamplesColorModel equal = createExtraSamplesColorModel(ColorSpaces.getColorSpace(ColorSpace.CS_sRGB), true, 1);
|
||||
|
||||
assertEquals(original, equal);
|
||||
assertEquals(equal, original);
|
||||
|
||||
ExtraSamplesColorModel different = createExtraSamplesColorModel(ColorSpaces.getColorSpace(ColorSpace.CS_sRGB), true, 2);
|
||||
ExtraSamplesColorModel differentToo = createExtraSamplesColorModel(ColorSpaces.getColorSpace(ColorSpace.CS_sRGB), false, 1);
|
||||
|
||||
assertNotEquals(original, different);
|
||||
assertNotEquals(original, differentToo);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user