160 lines
8.8 KiB
Java
160 lines
8.8 KiB
Java
package org.atriasoft.pngencoder;
|
|
|
|
import org.atriasoft.egami.Image;
|
|
|
|
public class PngEncoderImageInterfaceConverter {
|
|
private static final int[] BAND_MASKS_INT_ARGB = { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 };
|
|
private static final int[] BAND_MASKS_INT_ARGB_PRE = { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 };
|
|
|
|
private static final int[] BAND_MASKS_INT_BGR = { 0x000000ff, 0x0000ff00, 0x00ff0000 };
|
|
private static final int[] BAND_MASKS_INT_RGB = { 0x00ff0000, 0x0000ff00, 0x000000ff };
|
|
|
|
private static final int[] BAND_MASKS_USHORT_555_RGB = { 0x7C00, 0x03E0, 0x001F };
|
|
private static final int[] BAND_MASKS_USHORT_565_RGB = { 0xf800, 0x07E0, 0x001F };
|
|
|
|
private static final ColorModel COLOR_MODEL_INT_ARGB = ColorModel.getRGBdefault();
|
|
private static final ColorModel COLOR_MODEL_INT_ARGB_PRE = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, true,
|
|
DataBuffer.TYPE_INT);
|
|
|
|
private static final ColorModel COLOR_MODEL_INT_BGR = new DirectColorModel(24, 0x000000ff, 0x0000ff00, 0x00ff0000);
|
|
|
|
private static final ColorModel COLOR_MODEL_INT_RGB = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
|
|
|
|
public static Image copyType(final Image ImageInterface, final PngEncoderImageInterfaceType type) {
|
|
final int width = ImageInterface.getWidth();
|
|
final int height = ImageInterface.getHeight();
|
|
final Image convertedImageInterface = new Image(width, height, type.ordinal());
|
|
final Graphics graphics = convertedImageInterface.getGraphics();
|
|
if (!convertedImageInterface.hasAlpha()) {
|
|
graphics.setColor(Color.WHITE);
|
|
graphics.fillRect(0, 0, width, height);
|
|
}
|
|
graphics.drawImage(ImageInterface, 0, 0, null);
|
|
graphics.dispose();
|
|
return convertedImageInterface;
|
|
}
|
|
|
|
public static Image createFrom3ByteBgr(final byte[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferByte(data, data.length);
|
|
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
|
|
int[] nBits = { 8, 8, 8 };
|
|
int[] bOffs = { 2, 1, 0 };
|
|
ColorModel colorModel = new ComponentColorModel(colorSpace, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
|
|
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width * 3, 3, bOffs, null);
|
|
return new Image(colorModel, raster, false, null);
|
|
}
|
|
|
|
public static Image createFrom4ByteAbgr(final byte[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferByte(data, data.length);
|
|
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
|
|
int[] nBits = { 8, 8, 8, 8 };
|
|
int[] bOffs = { 3, 2, 1, 0 };
|
|
ColorModel colorModel = new ComponentColorModel(colorSpace, nBits, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
|
|
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width * 4, 4, bOffs, null);
|
|
return new Image(colorModel, raster, false, null);
|
|
}
|
|
|
|
public static Image createFrom4ByteAbgrPre(final byte[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferByte(data, data.length);
|
|
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
|
|
int[] nBits = { 8, 8, 8, 8 };
|
|
int[] bOffs = { 3, 2, 1, 0 };
|
|
ColorModel colorModel = new ComponentColorModel(colorSpace, nBits, true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
|
|
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width * 4, 4, bOffs, null);
|
|
return new Image(colorModel, raster, true, null);
|
|
}
|
|
|
|
public static Image createFromByteBinary(final byte[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferByte(data, data.length);
|
|
byte[] arr = { (byte) 0, (byte) 0xff };
|
|
IndexColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, 1, null);
|
|
return new Image(colorModel, raster, false, null);
|
|
}
|
|
|
|
public static Image createFromByteGray(final byte[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferByte(data, data.length);
|
|
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
|
|
int[] nBits = { 8 };
|
|
ColorModel colorModel = new ComponentColorModel(colorSpace, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
|
|
int[] bandOffsets = { 0 };
|
|
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width, 1, bandOffsets, null);
|
|
return new Image(colorModel, raster, true, null);
|
|
}
|
|
|
|
public static Image createFromIntArgb(final int[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferInt(data, data.length);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, width, PngEncoderImageInterfaceConverter.BAND_MASKS_INT_ARGB, null);
|
|
return new Image(PngEncoderImageInterfaceConverter.COLOR_MODEL_INT_ARGB, raster, false, null);
|
|
}
|
|
|
|
public static Image createFromIntArgbPre(final int[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferInt(data, data.length);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, width, PngEncoderImageInterfaceConverter.BAND_MASKS_INT_ARGB_PRE, null);
|
|
return new Image(PngEncoderImageInterfaceConverter.COLOR_MODEL_INT_ARGB_PRE, raster, true, null);
|
|
}
|
|
|
|
public static Image createFromIntBgr(final int[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferInt(data, data.length);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, width, PngEncoderImageInterfaceConverter.BAND_MASKS_INT_BGR, null);
|
|
return new Image(PngEncoderImageInterfaceConverter.COLOR_MODEL_INT_BGR, raster, false, null);
|
|
}
|
|
|
|
public static Image createFromIntRgb(final int[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferInt(data, data.length);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, width, PngEncoderImageInterfaceConverter.BAND_MASKS_INT_RGB, null);
|
|
return new Image(PngEncoderImageInterfaceConverter.COLOR_MODEL_INT_RGB, raster, false, null);
|
|
}
|
|
|
|
public static Image createFromUshort555Rgb(final short[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferUShort(data, data.length);
|
|
ColorModel colorModel = new DirectColorModel(15, PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_555_RGB[0], PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_555_RGB[1],
|
|
PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_555_RGB[2]);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, width, PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_555_RGB, null);
|
|
return new Image(colorModel, raster, false, null);
|
|
}
|
|
|
|
public static Image createFromUshort565Rgb(final short[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferUShort(data, data.length);
|
|
ColorModel colorModel = new DirectColorModel(16, PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_565_RGB[0], PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_565_RGB[1],
|
|
PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_565_RGB[2]);
|
|
WritableRaster raster = Raster.createPackedRaster(dataBuffer, width, height, width, PngEncoderImageInterfaceConverter.BAND_MASKS_USHORT_565_RGB, null);
|
|
return new Image(colorModel, raster, false, null);
|
|
}
|
|
|
|
public static Image createFromUshortGray(final short[] data, final int width, final int height) {
|
|
DataBuffer dataBuffer = new DataBufferUShort(data, data.length);
|
|
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
|
|
int[] nBits = { 16 };
|
|
ColorModel colorModel = new ComponentColorModel(colorSpace, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
|
|
int[] bandOffsets = { 0 };
|
|
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width, 1, bandOffsets, null);
|
|
return new Image(colorModel, raster, false, null);
|
|
}
|
|
|
|
public static Image ensureType(final Image ImageInterface, final PngEncoderImageInterfaceType type) {
|
|
if (PngEncoderImageInterfaceType.valueOf(ImageInterface) == type) {
|
|
return ImageInterface;
|
|
}
|
|
return PngEncoderImageInterfaceConverter.copyType(ImageInterface, type);
|
|
}
|
|
|
|
public static DataBuffer getDataBuffer(final Image ImageInterface) {
|
|
return ImageInterface.getRaster().getDataBuffer();
|
|
}
|
|
|
|
public static DataBufferByte getDataBufferByte(final Image ImageInterface) {
|
|
return (DataBufferByte) PngEncoderImageInterfaceConverter.getDataBuffer(ImageInterface);
|
|
}
|
|
|
|
public static DataBufferInt getDataBufferInt(final Image ImageInterface) {
|
|
return (DataBufferInt) PngEncoderImageInterfaceConverter.getDataBuffer(ImageInterface);
|
|
}
|
|
|
|
public static DataBufferUShort getDataBufferUShort(final Image ImageInterface) {
|
|
return (DataBufferUShort) PngEncoderImageInterfaceConverter.getDataBuffer(ImageInterface);
|
|
}
|
|
|
|
private PngEncoderImageInterfaceConverter() {}
|
|
}
|