TMI-81: Support for 32 bit unsigned int color model.

Bonus: Cleaned up creation of ImageTypeSpecifiers and added tests.
This commit is contained in:
Harald Kuhr
2014-11-20 15:57:36 +01:00
parent 4b00945c9d
commit 654f7e7a70
21 changed files with 1363 additions and 156 deletions

View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 2014, Harald Kuhr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name "TwelveMonkeys" nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.imageio.color;
import java.awt.color.ColorSpace;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
/**
* ComponentColorModel subclass that correctly handles full 32 bit {@code TYPE_INT} unsigned integral samples.
*
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-6193686">
* ComponentColorModel.getNormalizedComponents() does not handle 32-bit TYPE_INT</a>
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: UInt32ColorModel.java,v 1.0 24.01.11 17.51 haraldk Exp$
*/
public final class UInt32ColorModel extends ComponentColorModel {
public UInt32ColorModel(final ColorSpace cs, final boolean hasAlpha, boolean isAlphaPremultiplied) {
super(cs, hasAlpha, isAlphaPremultiplied, hasAlpha ? TRANSLUCENT : OPAQUE, DataBuffer.TYPE_INT);
}
@Override
public float[] getNormalizedComponents(final Object pixel, float[] normComponents, final int normOffset) {
// Implementation borrowed from super class, with modifications to allow 32 bit shifts and unsigned values.
int numComponents = getNumComponents();
if (normComponents == null) {
normComponents = new float[numComponents + normOffset];
}
// This class only supports DataBuffer.TYPE_INT, cast is safe
int[] ipixel = (int[]) pixel;
for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
normComponents[nc] = ((float) (ipixel[c] & 0xffffffffl)) / ((float) ((1l << getComponentSize(c)) - 1));
}
int numColorComponents = getNumColorComponents();
if (hasAlpha() && isAlphaPremultiplied()) {
float alpha = normComponents[numColorComponents + normOffset];
if (alpha != 0.0f) {
float invAlpha = 1.0f / alpha;
for (int c = normOffset; c < numColorComponents + normOffset; c++) {
normComponents[c] *= invAlpha;
}
}
}
// TODO: We don't currently support color spaces that has min and max other than 0.0f and 1.0f respectively.
return normComponents;
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright (c) 2014, Harald Kuhr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name "TwelveMonkeys" nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.imageio.util;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
/**
* Factory class for creating {@code ImageTypeSpecifier}s.
* In most cases, this class will delegate to the corresponding methods in {@link ImageTypeSpecifier}.
*
* @see javax.imageio.ImageTypeSpecifier
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: ImageTypeSpecifiers.java,v 1.0 24.01.11 17.51 haraldk Exp$
*/
public final class ImageTypeSpecifiers {
private ImageTypeSpecifiers() {}
public static ImageTypeSpecifier createFromBufferedImageType(final int bufferedImageType) {
return ImageTypeSpecifier.createFromBufferedImageType(bufferedImageType);
}
public static ImageTypeSpecifier createPacked(final ColorSpace colorSpace,
final int redMask, final int greenMask,
final int blueMask, final int alphaMask,
final int transferType, boolean isAlphaPremultiplied) {
return ImageTypeSpecifier.createPacked(colorSpace, redMask, greenMask, blueMask, alphaMask, transferType, isAlphaPremultiplied);
}
public static ImageTypeSpecifier createInterleaved(final ColorSpace colorSpace,
final int[] bandOffsets,
final int dataType,
final boolean hasAlpha,
final boolean isAlphaPremultiplied) {
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
if (dataType == DataBuffer.TYPE_INT) {
return new UInt32ImageTypeSpecifier(colorSpace, bandOffsets, hasAlpha, isAlphaPremultiplied);
}
// ...or fall back to default for anything else
return ImageTypeSpecifier.createInterleaved(colorSpace, bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
}
public static ImageTypeSpecifier createBanded(final ColorSpace colorSpace,
final int[] bankIndices, final int[] bandOffsets,
final int dataType,
final boolean hasAlpha, final boolean isAlphaPremultiplied) {
return ImageTypeSpecifier.createBanded(colorSpace, bankIndices, bandOffsets, dataType, hasAlpha, isAlphaPremultiplied);
}
public static ImageTypeSpecifier createGrayscale(final int bits, final int dataType) {
if (bits == 32 && dataType == DataBuffer.TYPE_INT) {
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
return new UInt32ImageTypeSpecifier(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {0}, false, false);
}
// NOTE: The isSigned boolean is stored but *not used for anything* in the Grayscale ImageTypeSpecifier...
return ImageTypeSpecifier.createGrayscale(bits, dataType, false);
}
public static ImageTypeSpecifier createGrayscale(final int bits, final int dataType, final boolean isAlphaPremultiplied) {
if (bits == 32 && dataType == DataBuffer.TYPE_INT) {
// As the ComponentColorModel is broken for 32 bit unsigned int, we'll use our own version
return new UInt32ImageTypeSpecifier(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {0, 1}, true, isAlphaPremultiplied);
}
// NOTE: The isSigned boolean is stored but *not used for anything* in the Grayscale ImageTypeSpecifier...
return ImageTypeSpecifier.createGrayscale(bits, dataType, false, isAlphaPremultiplied);
}
public static ImageTypeSpecifier createIndexed(final byte[] redLUT, final byte[] greenLUT,
final byte[] blueLUT, final byte[] alphaLUT,
final int bits, final int dataType) {
return ImageTypeSpecifier.createIndexed(redLUT, greenLUT, blueLUT, alphaLUT, bits, dataType);
}
public static ImageTypeSpecifier createIndexed(final int[] colors, final boolean hasAlpha, final int transIndex,
final int bits, final int dataType) {
return createFromIndexColorModel(new IndexColorModel(bits, colors.length, colors, 0, hasAlpha, transIndex, dataType));
}
public static ImageTypeSpecifier createFromIndexColorModel(final IndexColorModel pColorModel) {
return new IndexedImageTypeSpecifier(pColorModel);
}
}

View File

@@ -1,11 +1,13 @@
package com.twelvemonkeys.imageio.util;
import javax.imageio.ImageTypeSpecifier;
import java.awt.image.IndexColorModel;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
import static com.twelvemonkeys.lang.Validate.notNull;
/**
* IndexedImageTypeSpecifier
*
@@ -13,18 +15,14 @@ import java.util.Hashtable;
* @author last modified by $Author: haraldk$
* @version $Id: IndexedImageTypeSpecifier.java,v 1.0 May 19, 2008 11:04:28 AM haraldk Exp$
*/
public class IndexedImageTypeSpecifier extends ImageTypeSpecifier {
IndexedImageTypeSpecifier(IndexColorModel pColorModel) {
final class IndexedImageTypeSpecifier extends ImageTypeSpecifier {
IndexedImageTypeSpecifier(final IndexColorModel pColorModel) {
// For some reason, we need a sample model
super(pColorModel, pColorModel.createCompatibleSampleModel(1, 1));
}
public static ImageTypeSpecifier createFromIndexColorModel(final IndexColorModel pColorModel) {
return new IndexedImageTypeSpecifier(pColorModel);
super(notNull(pColorModel, "colorModel"), pColorModel.createCompatibleSampleModel(1, 1));
}
@Override
public final BufferedImage createBufferedImage(int pWidth, int pHeight) {
public final BufferedImage createBufferedImage(final int pWidth, final int pHeight) {
try {
// This is a fix for the super-method, that first creates a sample model, and then
// creates a raster from it, using Raster.createWritableRaster. The problem with

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2014, Harald Kuhr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name "TwelveMonkeys" nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.imageio.color.UInt32ColorModel;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace;
import java.awt.image.DataBuffer;
import java.awt.image.PixelInterleavedSampleModel;
/**
* ImageTypeSpecifier for interleaved 32 bit unsigned integral samples.
*
* @see com.twelvemonkeys.imageio.color.UInt32ColorModel
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: UInt32ImageTypeSpecifier.java,v 1.0 24.01.11 17.51 haraldk Exp$
*/
final class UInt32ImageTypeSpecifier extends ImageTypeSpecifier {
UInt32ImageTypeSpecifier(final ColorSpace cs, int[] bandOffsets, final boolean hasAlpha, final boolean isAlphaPremultiplied) {
super(
new UInt32ColorModel(cs, hasAlpha, isAlphaPremultiplied),
new PixelInterleavedSampleModel(
DataBuffer.TYPE_INT, 1, 1,
cs.getNumComponents() + (hasAlpha ? 1 : 0),
cs.getNumComponents() + (hasAlpha ? 1 : 0),
bandOffsets
)
);
}
}

View File

@@ -0,0 +1,106 @@
package com.twelvemonkeys.imageio.color;
import org.junit.Test;
import java.awt.color.ColorSpace;
import java.awt.image.ComponentColorModel;
import static org.junit.Assert.assertEquals;
public class UInt32ColorModelTest {
private static final ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
private static final ColorSpace GRAY = ColorSpace.getInstance(ColorSpace.CS_GRAY);
@Test
public void testGetNormalizedComponentsRGBBlack() {
ComponentColorModel model = new UInt32ColorModel(sRGB, true, false);
float[] normalized = model.getNormalizedComponents(new int[]{0, 0, 0, 0}, null, 0);
for (float norm : normalized) {
assertEquals(0, norm, 0);
}
}
@Test
public void testGetNormalizedComponentsRGBGray() {
ComponentColorModel model = new UInt32ColorModel(sRGB, true, false);
float[] normalized = model.getNormalizedComponents(new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE}, null, 0);
for (float norm : normalized) {
assertEquals(0.5f, norm, 0);
}
}
@Test
public void testGetNormalizedComponentsRGBWhite() {
ComponentColorModel model = new UInt32ColorModel(sRGB, true, false);
float[] normalized = model.getNormalizedComponents(new int[]{-1, -1, -1, -1}, null, 0);
for (float norm : normalized) {
assertEquals(1, norm, 0);
}
}
@Test
public void testGetNormalizedComponentsRGB() {
ComponentColorModel model = new UInt32ColorModel(sRGB, true, false);
int[] pixel = new int[4];
float[] normalized = null;
for (long pix = 0; pix < 1l << 32; pix += Short.MAX_VALUE) {
float expected = ((float) (pix & 0xffffffffl)) / ((float) ((1l << 32) - 1));
for (int i = 0; i < pixel.length; i++) {
pixel[i] = (int) pix;
}
normalized = model.getNormalizedComponents(pixel, normalized, 0);
for (float norm : normalized) {
assertEquals(expected, norm, 0);
}
}
}
@Test
public void testGetNormalizedComponentsGrayBlack() {
ComponentColorModel model = new UInt32ColorModel(GRAY, false, false);
float[] normalized = model.getNormalizedComponents(new int[]{0}, null, 0);
for (float norm : normalized) {
assertEquals(0, norm, 0);
}
}
@Test
public void testGetNormalizedComponentsGrayGray() {
ComponentColorModel model = new UInt32ColorModel(GRAY, false, false);
float[] normalized = model.getNormalizedComponents(new int[]{Integer.MIN_VALUE}, null, 0);
for (float norm : normalized) {
assertEquals(0.5f, norm, 0);
}
}
@Test
public void testGetNormalizedComponentsGrayWhite() {
ComponentColorModel model = new UInt32ColorModel(GRAY, false, false);
float[] normalized = model.getNormalizedComponents(new int[]{-1}, null, 0);
for (float norm : normalized) {
assertEquals(1, norm, 0);
}
}
@Test
public void testGetNormalizedComponentsGray() {
ComponentColorModel model = new UInt32ColorModel(GRAY, false, false);
int[] pixel = new int[1];
float[] normalized = null;
for (long pix = 0; pix < 1l << 32; pix += Short.MAX_VALUE) {
float expected = ((float) (pix & 0xffffffffl)) / ((float) ((1l << 32) - 1));
pixel[0] = (int) pix;
normalized = model.getNormalizedComponents(pixel, normalized, 0);
assertEquals(expected, normalized[0], 0);
}
}
}

View File

@@ -0,0 +1,583 @@
package com.twelvemonkeys.imageio.util;
import org.junit.Test;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import static org.junit.Assert.assertEquals;
public class ImageTypeSpecifiersTest {
private static final ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
private static final ColorSpace GRAY = ColorSpace.getInstance(ColorSpace.CS_GRAY);
private static final int DCM_RED_MASK = 0x00ff0000;
private static final int DCM_GREEN_MASK = 0x0000ff00;
private static final int DCM_BLUE_MASK = 0x000000ff;
private static final int DCM_ALPHA_MASK = 0xff000000;
private static final int DCM_565_RED_MASK = 0xf800;
private static final int DCM_565_GRN_MASK = 0x07E0;
private static final int DCM_565_BLU_MASK = 0x001F;
private static final int DCM_555_RED_MASK = 0x7C00;
private static final int DCM_555_GRN_MASK = 0x03E0;
private static final int DCM_555_BLU_MASK = 0x001F;
private static final int DCM_BGR_RED_MASK = 0x0000ff;
private static final int DCM_BGR_GRN_MASK = 0x00ff00;
private static final int DCM_BGR_BLU_MASK = 0xff0000;
@Test
public void testCreateFromBufferedImageType() {
for (int type = BufferedImage.TYPE_INT_RGB; type < BufferedImage.TYPE_BYTE_INDEXED; type++) {
assertEquals(
ImageTypeSpecifier.createFromBufferedImageType(type),
ImageTypeSpecifiers.createFromBufferedImageType(type)
);
}
}
@Test
public void testCreatePacked() {
// TYPE_INT_RGB
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, DCM_RED_MASK, DCM_GREEN_MASK, DCM_BLUE_MASK, 0, DataBuffer.TYPE_INT, false),
ImageTypeSpecifiers.createPacked(sRGB, DCM_RED_MASK, DCM_GREEN_MASK, DCM_BLUE_MASK, 0, DataBuffer.TYPE_INT, false)
);
// TYPE_INT_ARGB
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, DCM_RED_MASK, DCM_GREEN_MASK, DCM_BLUE_MASK, DCM_ALPHA_MASK, DataBuffer.TYPE_INT, false),
ImageTypeSpecifiers.createPacked(sRGB, DCM_RED_MASK, DCM_GREEN_MASK, DCM_BLUE_MASK, DCM_ALPHA_MASK, DataBuffer.TYPE_INT, false)
);
// TYPE_INT_ARGB_PRE
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, DCM_RED_MASK, DCM_GREEN_MASK, DCM_BLUE_MASK, DCM_ALPHA_MASK, DataBuffer.TYPE_INT, true),
ImageTypeSpecifiers.createPacked(sRGB, DCM_RED_MASK, DCM_GREEN_MASK, DCM_BLUE_MASK, DCM_ALPHA_MASK, DataBuffer.TYPE_INT, true)
);
// TYPE_INT_BGR
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, DCM_BGR_RED_MASK, DCM_BGR_GRN_MASK, DCM_BGR_BLU_MASK, 0, DataBuffer.TYPE_INT, false),
ImageTypeSpecifiers.createPacked(sRGB, DCM_BGR_RED_MASK, DCM_BGR_GRN_MASK, DCM_BGR_BLU_MASK, 0, DataBuffer.TYPE_INT, false)
);
// TYPE_USHORT_555_RGB
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false),
ImageTypeSpecifiers.createPacked(sRGB, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false)
);
// "SHORT 555 RGB" (impossible for some reason)
// assertEquals(
// ImageTypeSpecifier.createPacked(sRGB, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK, 0, DataBuffer.TYPE_SHORT, false),
// ImageTypeSpecifiers.createPacked(sRGB, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK, 0, DataBuffer.TYPE_SHORT, false)
// );
// TYPE_USHORT_565_RGB
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false),
ImageTypeSpecifiers.createPacked(sRGB, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK, 0, DataBuffer.TYPE_USHORT, false)
);
// "USHORT 4444 ARGB"
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, false),
ImageTypeSpecifiers.createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, false)
);
// "USHORT 4444 ARGB PRE"
assertEquals(
ImageTypeSpecifier.createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, true),
ImageTypeSpecifiers.createPacked(sRGB, 0xf00, 0xf0, 0xf, 0xf000, DataBuffer.TYPE_USHORT, true)
);
}
@Test
public void testCreateInterleaved8() {
// 8 bits/sample
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_BYTE, false, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_BYTE, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_BYTE, true, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_BYTE, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_BYTE, false, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_BYTE, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_BYTE, true, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_BYTE, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_BYTE, true, true),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_BYTE, true, true)
);
}
@Test
public void testCreateInterleaved16() {
// 16 bits/sample
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_USHORT, false, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_USHORT, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_USHORT, true, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_USHORT, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_USHORT, false, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_USHORT, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_USHORT, true, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_USHORT, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_USHORT, true, true),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_USHORT, true, true)
);
}
@Test
public void testCreateInterleaved32() {
// 32 bits/sample
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0}, false, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_INT, false, false)
);
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_INT, true, false)
);
assertEquals(
new UInt32ImageTypeSpecifier(sRGB, new int[] {0, 1, 2}, false, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_INT, false, false)
);
assertEquals(
new UInt32ImageTypeSpecifier(sRGB, new int[] {0, 1, 2, 3}, true, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_INT, true, false)
);
assertEquals(
new UInt32ImageTypeSpecifier(sRGB, new int[] {0, 1, 2, 3}, true, true),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_INT, true, true)
);
}
@Test
public void testCreateInterleaved32fp() {
// 32 bits/sample
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_FLOAT, false, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_FLOAT, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_FLOAT, true, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_FLOAT, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_FLOAT, false, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_FLOAT, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_FLOAT, true, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_FLOAT, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_FLOAT, true, true),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_FLOAT, true, true)
);
}
@Test
public void testCreateInterleaved64fp() {
// 64 bits/sample
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_DOUBLE, false, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0}, DataBuffer.TYPE_DOUBLE, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_DOUBLE, true, false),
ImageTypeSpecifiers.createInterleaved(GRAY, new int[] {0, 1}, DataBuffer.TYPE_DOUBLE, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_DOUBLE, false, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2}, DataBuffer.TYPE_DOUBLE, false, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_DOUBLE, true, false),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_DOUBLE, true, false)
);
assertEquals(
ImageTypeSpecifier.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_DOUBLE, true, true),
ImageTypeSpecifiers.createInterleaved(sRGB, new int[] {0, 1, 2, 3}, DataBuffer.TYPE_DOUBLE, true, true)
);
}
@Test
public void testCreateBanded8() {
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_BYTE, false, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_BYTE, false, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_BYTE, true, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_BYTE, true, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_BYTE, true, true),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_BYTE, true, true)
);
}
@Test
public void testCreateBanded16() {
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_USHORT, false, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_USHORT, false, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_USHORT, true, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_USHORT, true, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_USHORT, true, true),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_USHORT, true, true)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_SHORT, false, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_SHORT, false, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_SHORT, true, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_SHORT, true, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_SHORT, true, true),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_SHORT, true, true)
);
}
@Test
public void testCreateBanded32() {
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_INT, false, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_INT, false, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_INT, true, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_INT, true, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_INT, true, true),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_INT, true, true)
);
}
@Test
public void testCreateBanded32fp() {
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_FLOAT, false, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_FLOAT, false, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_FLOAT, true, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_FLOAT, true, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_FLOAT, true, true),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_FLOAT, true, true)
);
}
@Test
public void testCreateBanded64fp() {
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_DOUBLE, false, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2}, new int[] {0, 0, 0}, DataBuffer.TYPE_DOUBLE, false, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_DOUBLE, true, false),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 0, 0, 0}, DataBuffer.TYPE_DOUBLE, true, false)
);
assertEquals(
ImageTypeSpecifier.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_DOUBLE, true, true),
ImageTypeSpecifiers.createBanded(sRGB, new int[] {0, 1, 2, 3}, new int[] {0, 1000, 2000, 3000}, DataBuffer.TYPE_DOUBLE, true, true)
);
}
@Test
public void testCreateGrayscale1to8() {
for (int bits = 1; bits <= 8; bits <<= 1) {
assertEquals(
ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, false),
ImageTypeSpecifiers.createGrayscale(bits, DataBuffer.TYPE_BYTE)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, true),
ImageTypeSpecifiers.createGrayscale(bits, DataBuffer.TYPE_BYTE)
);
}
}
@Test
public void testCreateGrayscale16() {
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, false),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_USHORT)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, true), // NOTE: Signed TYPE_USHORT makes no sense...
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_USHORT)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_SHORT, false), // NOTE: Unsigned TYPE_SHORT makes no sense...
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_SHORT)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_SHORT, true),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_SHORT)
);
}
@Test
public void testCreateGrayscale32() {
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0}, false, false),
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT)
);
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0}, false, false),
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT)
);
}
@Test
public void testCreateGrayscaleAlpha1to8() {
for (int bits = 1; bits <= 8; bits <<= 1) {
assertEquals(
ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, false, false),
ImageTypeSpecifiers.createGrayscale(bits, DataBuffer.TYPE_BYTE, false)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, false, true),
ImageTypeSpecifiers.createGrayscale(bits, DataBuffer.TYPE_BYTE, true)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, true, false),
ImageTypeSpecifiers.createGrayscale(bits, DataBuffer.TYPE_BYTE, false)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(bits, DataBuffer.TYPE_BYTE, true, true),
ImageTypeSpecifiers.createGrayscale(bits, DataBuffer.TYPE_BYTE, true)
);
}
}
@Test
public void testCreateGrayscaleAlpha16() {
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, false, false),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_USHORT, false)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, false, true),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_USHORT, true)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, true, false),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_USHORT, false)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, true, true),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_USHORT, true)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_SHORT, false, false),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_SHORT, false)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_SHORT, false, true),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_SHORT, true)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_SHORT, true, false),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_SHORT, false)
);
assertEquals(
ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_SHORT, true, true),
ImageTypeSpecifiers.createGrayscale(16, DataBuffer.TYPE_SHORT, true)
);
}
@Test
public void testCreateGrayscaleAlpha32() {
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, false),
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, false)
);
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, false),
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, false)
);
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, true),
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, true)
);
assertEquals(
new UInt32ImageTypeSpecifier(GRAY, new int[] {0, 1}, true, true),
ImageTypeSpecifiers.createGrayscale(32, DataBuffer.TYPE_INT, true)
);
}
@Test
public void testCreateIndexedByteArrays1to8() {
for (int bits = 1; bits <= 8; bits <<= 1) {
byte[] lut = createByteLut(1 << bits);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, null, bits, DataBuffer.TYPE_BYTE),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, null, bits, DataBuffer.TYPE_BYTE)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, lut, bits, DataBuffer.TYPE_BYTE),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, lut, bits, DataBuffer.TYPE_BYTE)
);
}
}
@Test
public void testCreateIndexedByteArrays16() {
for (int bits = 1; bits <= 8; bits <<= 1) {
byte[] lut = createByteLut(1 << bits);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, null, bits, DataBuffer.TYPE_USHORT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, null, bits, DataBuffer.TYPE_USHORT)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, lut, bits, DataBuffer.TYPE_USHORT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, lut, bits, DataBuffer.TYPE_USHORT)
);
// TYPE_SHORT is unsupported to MultiPixelPacked format (MultiPixelPackedSampleModel)
}
byte[] lut = createByteLut(1 << 16); // This is stupid, but ImageTypeSpecifier enforces lut.length == 1 << bits
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, null, 16, DataBuffer.TYPE_USHORT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, null, 16, DataBuffer.TYPE_USHORT)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, lut, 16, DataBuffer.TYPE_USHORT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, lut, 16, DataBuffer.TYPE_USHORT)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, null, 16, DataBuffer.TYPE_SHORT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, null, 16, DataBuffer.TYPE_SHORT)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, lut, 16, DataBuffer.TYPE_SHORT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, lut, 16, DataBuffer.TYPE_SHORT)
);
}
@Test
public void testCreateIndexedByteArrays32() {
for (int bits = 1; bits <= 8; bits <<= 1) {
byte[] lut = createByteLut(1 << bits);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, null, bits, DataBuffer.TYPE_INT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, null, bits, DataBuffer.TYPE_INT)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, lut, bits, DataBuffer.TYPE_INT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, lut, bits, DataBuffer.TYPE_INT)
);
}
byte[] lut = createByteLut(1 << 16); // This is stupid, but ImageTypeSpecifier enforces lut.length == 1 << bits
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, null, 16, DataBuffer.TYPE_INT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, null, 16, DataBuffer.TYPE_INT)
);
assertEquals(
ImageTypeSpecifier.createIndexed(lut, lut, lut, lut, 16, DataBuffer.TYPE_INT),
ImageTypeSpecifiers.createIndexed(lut, lut, lut, lut, 16, DataBuffer.TYPE_INT)
);
}
@Test
public void testCreateIndexedIntArray1to8() {
for (int bits = 1; bits <= 8; bits <<= 1) {
int[] colors = createIntLut(1 << bits);
assertEquals(
new IndexedImageTypeSpecifier(new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE)),
ImageTypeSpecifiers.createIndexed(colors, false, -1, bits, DataBuffer.TYPE_BYTE)
);
}
}
@Test
public void testCreateIndexedIntArray16() {
int[] colors = createIntLut(1 << 16);
assertEquals(
new IndexedImageTypeSpecifier(new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT)),
ImageTypeSpecifiers.createIndexed(colors, false, -1, 16, DataBuffer.TYPE_USHORT)
);
}
@Test
public void testCreateFromIndexedColorModel1to8() {
for (int bits = 1; bits <= 8; bits <<= 1) {
int[] colors = createIntLut(1 << bits);
IndexColorModel colorModel = new IndexColorModel(bits, colors.length, colors, 0, false, -1, DataBuffer.TYPE_BYTE);
assertEquals(
new IndexedImageTypeSpecifier(colorModel),
ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
);
}
}
@Test
public void testCreateFromIndexedColorModel16() {
int[] colors = createIntLut(1 << 16);
IndexColorModel colorModel = new IndexColorModel(16, colors.length, colors, 0, false, -1, DataBuffer.TYPE_USHORT);
assertEquals(
new IndexedImageTypeSpecifier(colorModel),
ImageTypeSpecifiers.createFromIndexColorModel(colorModel)
);
}
private static byte[] createByteLut(final int count) {
byte[] lut = new byte[count];
for (int i = 0; i < count; i++) {
lut[i] = (byte) count;
}
return lut;
}
private static int[] createIntLut(final int count) {
int[] lut = new int[count];
for (int i = 0; i < count; i++) {
lut[i] = 0xff000000 | count << 16 | count << 8 | count;
}
return lut;
}
}

View File

@@ -0,0 +1,82 @@
package com.twelvemonkeys.imageio.util;
import org.junit.Test;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import static org.junit.Assert.*;
/**
* IndexedImageTypeSpecifierTestCase
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: IndexedImageTypeSpecifierTestCase.java,v 1.0 Jun 9, 2008 2:42:03 PM haraldk Exp$
*/
public class IndexedImageTypeSpecifierTest {
@Test
public void testEquals() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
IndexedImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
IndexedImageTypeSpecifier different = new IndexedImageTypeSpecifier(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
assertEquals(spec, other);
assertEquals(other, spec);
assertEquals(spec.hashCode(), other.hashCode());
assertTrue(spec.equals(other));
assertTrue(other.equals(spec));
// TODO: There is still a problem that IndexColorModel does not override equals,
// so any model with the same number of bits, transparency, and transfer type will be treated as equal
assertFalse(other.equals(different));
}
@Test
public void testHashCode() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
IndexedImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
IndexedImageTypeSpecifier different = new IndexedImageTypeSpecifier(new IndexColorModel(2, 2, new int[]{0xff00ff, 0x00, 0xff00ff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE));
// TODO: There is still a problem that IndexColorModel does not override hashCode,
// so any model with the same number of bits, transparency, and transfer type will have same hash
assertEquals(spec.hashCode(), other.hashCode());
assertFalse(spec.hashCode() == different.hashCode());
}
@Test(expected = IllegalArgumentException.class)
public void testCreateNull() {
new IndexedImageTypeSpecifier(null);
}
@Test
public void testCreateBufferedImageBinary() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
BufferedImage image = spec.createBufferedImage(2, 2);
assertNotNull(image);
assertEquals(BufferedImage.TYPE_BYTE_BINARY, image.getType());
assertEquals(cm, image.getColorModel());
}
@Test
public void testCreateBufferedImageIndexed() {
IndexColorModel cm = new IndexColorModel(8, 256, new int[256], 0, false, -1, DataBuffer.TYPE_BYTE);
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
BufferedImage image = spec.createBufferedImage(2, 2);
assertNotNull(image);
assertEquals(BufferedImage.TYPE_BYTE_INDEXED, image.getType());
assertEquals(cm, image.getColorModel());
}
}

View File

@@ -1,28 +0,0 @@
package com.twelvemonkeys.imageio.util;
import junit.framework.TestCase;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
/**
* IndexedImageTypeSpecifierTestCase
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: IndexedImageTypeSpecifierTestCase.java,v 1.0 Jun 9, 2008 2:42:03 PM haraldk Exp$
*/
public class IndexedImageTypeSpecifierTestCase extends TestCase {
public void testEquals() {
IndexColorModel cm = new IndexColorModel(1, 2, new int[]{0xffffff, 0x00}, 0, false, -1, DataBuffer.TYPE_BYTE);
IndexedImageTypeSpecifier spec = new IndexedImageTypeSpecifier(cm);
IndexedImageTypeSpecifier other = new IndexedImageTypeSpecifier(cm);
assertEquals(spec, other);
assertEquals(other, spec);
assertTrue(spec.equals(other));
assertTrue(other.equals(spec));
}
}

View File

@@ -0,0 +1,199 @@
package com.twelvemonkeys.imageio.util;
import com.twelvemonkeys.imageio.color.ColorSpaces;
import org.junit.Test;
import javax.imageio.ImageTypeSpecifier;
import java.awt.color.ColorSpace;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.PixelInterleavedSampleModel;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class UInt32ImageTypeSpecifierTest {
private static final ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
private static final ColorSpace GRAY = ColorSpace.getInstance(ColorSpace.CS_GRAY);
private static final ColorSpace CMYK = ColorSpaces.getColorSpace(ColorSpaces.CS_GENERIC_CMYK);
@Test
public void testGray() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(GRAY, new int [] {0}, false, false);
assertEquals(1, spec.getNumBands());
assertEquals(1, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertFalse(spec.getColorModel().hasAlpha());
assertFalse(spec.getColorModel().isAlphaPremultiplied());
assertEquals(1, spec.getColorModel().getNumComponents());
assertEquals(1, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(1, spec.getSampleModel().getNumBands());
assertEquals(1, spec.getSampleModel().getNumDataElements());
}
@Test
public void testGrayAlpha() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(GRAY, new int [] {0, 1}, true, false);
assertEquals(2, spec.getNumBands());
assertEquals(2, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertEquals(32, spec.getBitsPerBand(1));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertTrue(spec.getColorModel().hasAlpha());
assertFalse(spec.getColorModel().isAlphaPremultiplied());
assertEquals(2, spec.getColorModel().getNumComponents());
assertEquals(1, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(2, spec.getSampleModel().getNumBands());
assertEquals(2, spec.getSampleModel().getNumDataElements());
}
@Test
public void testRGB() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
assertEquals(3, spec.getNumBands());
assertEquals(3, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertEquals(32, spec.getBitsPerBand(1));
assertEquals(32, spec.getBitsPerBand(2));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertFalse(spec.getColorModel().hasAlpha());
assertFalse(spec.getColorModel().isAlphaPremultiplied());
assertEquals(3, spec.getColorModel().getNumComponents());
assertEquals(3, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(3, spec.getSampleModel().getNumBands());
assertEquals(3, spec.getSampleModel().getNumDataElements());
}
@Test
public void testRGBAlpha() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, false);
assertEquals(4, spec.getNumBands());
assertEquals(4, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertEquals(32, spec.getBitsPerBand(1));
assertEquals(32, spec.getBitsPerBand(2));
assertEquals(32, spec.getBitsPerBand(3));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertTrue(spec.getColorModel().hasAlpha());
assertFalse(spec.getColorModel().isAlphaPremultiplied());
assertEquals(4, spec.getColorModel().getNumComponents());
assertEquals(3, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(4, spec.getSampleModel().getNumBands());
assertEquals(4, spec.getSampleModel().getNumDataElements());
}
@Test
public void testRGBAlphaPre() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, true);
assertEquals(4, spec.getNumBands());
assertEquals(4, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertEquals(32, spec.getBitsPerBand(1));
assertEquals(32, spec.getBitsPerBand(2));
assertEquals(32, spec.getBitsPerBand(3));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertTrue(spec.getColorModel().hasAlpha());
assertTrue(spec.getColorModel().isAlphaPremultiplied());
assertEquals(4, spec.getColorModel().getNumComponents());
assertEquals(3, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(4, spec.getSampleModel().getNumBands());
assertEquals(4, spec.getSampleModel().getNumDataElements());
}
@Test
public void testCMYK() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(CMYK, new int [] {0, 1, 2, 3}, false, false);
assertEquals(4, spec.getNumBands());
assertEquals(4, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertEquals(32, spec.getBitsPerBand(1));
assertEquals(32, spec.getBitsPerBand(2));
assertEquals(32, spec.getBitsPerBand(3));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertFalse(spec.getColorModel().hasAlpha());
assertFalse(spec.getColorModel().isAlphaPremultiplied());
assertEquals(4, spec.getColorModel().getNumComponents());
assertEquals(4, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(4, spec.getSampleModel().getNumBands());
assertEquals(4, spec.getSampleModel().getNumDataElements());
}
@Test
public void testCMYKAlpha() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(CMYK, new int [] {0, 1, 2, 3, 4}, true, false);
assertEquals(5, spec.getNumBands());
assertEquals(5, spec.getNumComponents());
assertEquals(32, spec.getBitsPerBand(0));
assertEquals(32, spec.getBitsPerBand(1));
assertEquals(32, spec.getBitsPerBand(2));
assertEquals(32, spec.getBitsPerBand(3));
assertEquals(32, spec.getBitsPerBand(4));
assertThat(spec.getColorModel(), is(ComponentColorModel.class));
assertTrue(spec.getColorModel().hasAlpha());
assertFalse(spec.getColorModel().isAlphaPremultiplied());
assertEquals(5, spec.getColorModel().getNumComponents());
assertEquals(4, spec.getColorModel().getNumColorComponents());
assertThat(spec.getSampleModel(), is(PixelInterleavedSampleModel.class));
assertEquals(5, spec.getSampleModel().getNumBands());
assertEquals(5, spec.getSampleModel().getNumDataElements());
}
@Test
public void testEquals() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
ImageTypeSpecifier other = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
ImageTypeSpecifier different = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, false);
// Equivalent, but broken, not equal
ImageTypeSpecifier broken =
ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, DataBuffer.TYPE_INT, false, false);
assertEquals(spec, other);
assertEquals(other, spec);
assertTrue(spec.equals(other));
assertTrue(other.equals(spec));
assertFalse(spec.equals(different));
assertFalse(different.equals(spec));
assertFalse(spec.equals(broken));
assertFalse(broken.equals(spec));
}
@Test
public void testHashCode() {
ImageTypeSpecifier spec = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
ImageTypeSpecifier other = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2}, false, false);
ImageTypeSpecifier different = new UInt32ImageTypeSpecifier(sRGB, new int [] {0, 1, 2, 3}, true, false);
// Equivalent, but broken, not equal
ImageTypeSpecifier broken =
ImageTypeSpecifier.createInterleaved(sRGB, new int [] {0, 1, 2}, DataBuffer.TYPE_INT, false, false);
assertEquals(spec.hashCode(), other.hashCode());
assertFalse(spec.hashCode() == different.hashCode());
assertFalse(spec.hashCode() == broken.hashCode());
}
}