下面列出了java.awt.image.ImagingOpException#java.awt.image.ComponentColorModel 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
/**
* Constructs IntegerRaster with the given size.
* @param _nrow the number of rows
* @param _ncol the number of columns
*/
public IntegerRaster(int _nrow, int _ncol) {
nrow = _nrow;
ncol = _ncol;
dimension = new Dimension(ncol, nrow);
int size = nrow*ncol;
ComponentColorModel ccm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8, 8, 8}, false, // hasAlpha
false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
BandedSampleModel csm = new BandedSampleModel(DataBuffer.TYPE_BYTE, ncol, nrow, ncol, new int[] {0, 1, 2}, new int[] {0, 0, 0});
rgbData = new byte[3][size];
DataBuffer databuffer = new DataBufferByte(rgbData, size);
WritableRaster raster = Raster.createWritableRaster(csm, databuffer, new Point(0, 0));
image = new BufferedImage(ccm, raster, false, null);
// col in x direction, row in y direction
xmin = 0;
xmax = ncol;
ymin = nrow;
ymax = 0; // zero is on top
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
/**
* Returns the (A)RGB equivalent of the given raster, using the given AWT color space to perform the conversion.
*
* @param raster the source raster
* @param colorSpace the AWT
* @return an (A)RGB buffered image
*/
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace)
{
//
// WARNING: this method is performance sensitive, modify with care!
//
// ICC Profile color transforms are only fast when performed using ColorConvertOp
ColorModel colorModel = new ComponentColorModel(colorSpace, false, false,
Transparency.OPAQUE, raster.getDataBuffer().getDataType());
BufferedImage src = new BufferedImage(colorModel, raster, false, null);
BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(),
BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dest);
return dest;
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
public SignedColorModel(final int pixelBits, final int dataType,
final int nChannels)
{
super(pixelBits, makeBitArray(nChannels, pixelBits), AWTImageTools
.makeColorSpace(nChannels), nChannels == 4, false,
Transparency.TRANSLUCENT, dataType);
int type = dataType;
if (type == DataBuffer.TYPE_SHORT) {
type = DataBuffer.TYPE_USHORT;
}
helper = new ComponentColorModel(AWTImageTools.makeColorSpace(nChannels),
nChannels == 4, false, Transparency.TRANSLUCENT, type);
this.pixelBits = pixelBits;
this.nChannels = nChannels;
max = (int) Math.pow(2, pixelBits) - 1;
}
public void testCreateLosslessFrom16Bit() throws IOException
{
PDDocument document = new PDDocument();
BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
ColorSpace targetCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int dataBufferType = DataBuffer.TYPE_USHORT;
final ColorModel colorModel = new ComponentColorModel(targetCS, false, false,
ColorModel.OPAQUE, dataBufferType);
WritableRaster targetRaster = Raster.createInterleavedRaster(dataBufferType,
image.getWidth(), image.getHeight(), targetCS.getNumComponents(), new Point(0, 0));
BufferedImage img16Bit = new BufferedImage(colorModel, targetRaster, false,
new Hashtable());
ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS,
null);
op.filter(image, img16Bit);
PDImageXObject ximage = LosslessFactory.createFromImage(img16Bit);
validate(ximage, 16, img16Bit.getWidth(), img16Bit.getHeight(), "png",
PDDeviceRGB.INSTANCE.getName());
checkIdent(image, ximage.getImage());
doWritePDF(document, ximage, testResultsDir, "misc-16bit.pdf");
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
/**
* Returns the custom buffered image, which mostly identical to
* BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride.
* This means that the raster will have gaps, between the rows.
*/
private static BufferedImage makeCustomManagedBI() {
int w = 511, h = 255;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
int[] bOffs = {2, 1, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
w * 3 + 2, 3, bOffs, null);
BufferedImage bi = new BufferedImage(colorModel, raster, true, null);
SunWritableRaster.makeTrackable(raster.getDataBuffer());
SunWritableRaster.markDirty(bi);
return bi;
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
/**
* Constructor.
*
* @param shading the shading type to be used
* @param cm the color model to be used
* @param xform transformation for user to device space
* @param matrix the pattern matrix concatenated with that of the parent content stream
* @throws java.io.IOException if there is an error getting the color space
* or doing background color conversion.
*/
public ShadingContext(PDShading shading, ColorModel cm, AffineTransform xform,
Matrix matrix) throws IOException
{
this.shading = shading;
shadingColorSpace = shading.getColorSpace();
// create the output color model using RGB+alpha as color space
ColorSpace outputCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
outputColorModel = new ComponentColorModel(outputCS, true, false, Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
// get background values if available
COSArray bg = shading.getBackground();
if (bg != null)
{
background = bg.toFloatArray();
rgbBackground = convertToRGB(background);
}
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
private BufferedImage readTrueColorPcx(InputStream is) throws Exception {
byte brgb[] = IOUtils.readFully(is, 4096);
byte pixels[] = new byte[bytesPerLine*NPlanes*height];
/**
* A BufferedInputStream could have been constructed from the InputStream,
* but for maximum decoding speed, one time reading of the image data
* into memory is ideal though this is memory consuming.
*/
LOGGER.info("true color pcx image!");
readScanLines(brgb, brgb.length, pixels);
is.close();
DataBuffer db = new DataBufferByte(pixels, pixels.length);
int trans = Transparency.OPAQUE;
int[] nBits = {8, 8, 8};
WritableRaster raster = Raster.createBandedRaster(db, width, height, bytesPerLine*3,
new int[]{0, 0, 0}, new int[] {0, bytesPerLine, bytesPerLine*2}, null);
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), nBits, false, false,
trans, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, raster, false, null);
}
static ColorModel createComponentCM(ColorSpace colorSpace,
int numBands,
int dataType,
boolean hasAlpha,
boolean isAlphaPremultiplied) {
int transparency =
hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
int[] numBits = new int[numBands];
int bits = DataBuffer.getDataTypeSize(dataType);
for (int i = 0; i < numBands; i++) {
numBits[i] = bits;
}
return new ComponentColorModel(colorSpace,
numBits,
hasAlpha,
isAlphaPremultiplied,
transparency,
dataType);
}
BufferedImage getThumbnail(ImageInputStream iis,
JPEGImageReader reader)
throws IOException {
iis.mark();
iis.seek(streamPos);
DataBufferByte buffer = new DataBufferByte(getLength());
readByteBuffer(iis,
buffer.getData(),
reader,
1.0F,
0.0F);
iis.reset();
WritableRaster raster =
Raster.createInterleavedRaster(buffer,
thumbWidth,
thumbHeight,
thumbWidth*3,
3,
new int [] {0, 1, 2},
null);
ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB,
false,
false,
ColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
return new BufferedImage(cm,
raster,
false,
null);
}
private static BufferedImage createCustomBuffer() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
return new BufferedImage(cm, wr, false, null);
}
BufferedImage getThumbnail(ImageInputStream iis,
JPEGImageReader reader)
throws IOException {
iis.mark();
iis.seek(streamPos);
DataBufferByte buffer = new DataBufferByte(getLength());
readByteBuffer(iis,
buffer.getData(),
reader,
1.0F,
0.0F);
iis.reset();
WritableRaster raster =
Raster.createInterleavedRaster(buffer,
thumbWidth,
thumbHeight,
thumbWidth*3,
3,
new int [] {0, 1, 2},
null);
ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB,
false,
false,
ColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
return new BufferedImage(cm,
raster,
false,
null);
}
public static ComponentColorModel createABGRCCM() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
int[] bOffs = {3, 2, 1, 0};
return new ComponentColorModel(cs, nBits, true, true,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
}
BufferedImage getThumbnail(ImageInputStream iis,
JPEGImageReader reader)
throws IOException {
iis.mark();
iis.seek(streamPos);
DataBufferByte buffer = new DataBufferByte(getLength());
readByteBuffer(iis,
buffer.getData(),
reader,
1.0F,
0.0F);
iis.reset();
WritableRaster raster =
Raster.createInterleavedRaster(buffer,
thumbWidth,
thumbHeight,
thumbWidth*3,
3,
new int [] {0, 1, 2},
null);
ColorModel cm = new ComponentColorModel(JPEG.JCS.sRGB,
false,
false,
ColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
return new BufferedImage(cm,
raster,
false,
null);
}
public static ComponentColorModel createABGRCCM() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
int[] bOffs = {3, 2, 1, 0};
return new ComponentColorModel(cs, nBits, true, true,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
}
@Override
protected void onCursorUpdate(CursorPacket cursorPacket) {
super.onCursorUpdate(cursorPacket);
if(cursorPacket.isEmpty()) {
return;
}
int width = cursorPacket.width;
int height = cursorPacket.height;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
int[] bOffs = {1, 2, 3, 0};
ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
width, height,
width*4, 4,
bOffs, null);
BufferedImage img = new BufferedImage(colorModel, raster, true, null);
img.getRaster().setDataElements(0, 0, width, height, cursorPacket.pixels);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension size = toolkit.getBestCursorSize(width, height);
Point hotspot = new Point(cursorPacket.xHotspot, cursorPacket.yHotspot);
Image outputImg = img;
if(size.width != width || size.height != height) {
outputImg = img.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH);
hotspot.x = hotspot.x * size.width / width;
hotspot.y = hotspot.y * size.height / height;
}
Cursor c = toolkit.createCustomCursor(outputImg, hotspot, "test");
SwingFrame window = (SwingFrame) getWindow(1);
window.window.setCursor(c);
}
@Override
public boolean canEncodeImage( ImageTypeSpecifier type ) {
ColorModel colorModel = type.getColorModel();
SampleModel sampleModel = type.getSampleModel();
int transferType = sampleModel.getTransferType();
if ( colorModel instanceof ComponentColorModel ) {
if ( !( sampleModel instanceof ComponentSampleModel ) ) {
return false;
}
if ( transferType != DataBuffer.TYPE_BYTE && transferType != DataBuffer.TYPE_INT ) {
return false;
}
}
else if ( colorModel instanceof DirectColorModel ) {
if ( !( sampleModel instanceof SinglePixelPackedSampleModel ) ) {
return false;
}
if ( transferType != DataBuffer.TYPE_INT ) {
return false;
}
}
ColorSpace colorSpace = colorModel.getColorSpace();
if ( !( colorSpace.isCS_sRGB() ) ) {
return false;
}
int[] sampleSize = sampleModel.getSampleSize();
for ( int i = 0; i < sampleSize.length; i++ ) {
if ( sampleSize[ i ] > 8 ) {
return false;
}
}
return true;
}
public static void main(String args[]) {
BufferedImage src
= new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
// Set src pixel values
Color pelColor = new Color(100, 100, 100, 128);
for (int i = 0; i < 10; i++) {
src.setRGB(0, i, pelColor.getRGB());
}
ColorModel cm = new ComponentColorModel
(ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int [] {8,8}, true,
src.getColorModel().isAlphaPremultiplied(),
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
SampleModel sm = new PixelInterleavedSampleModel
(DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
new int [] { 0, 1 });
WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));
BufferedImage dst =
new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
dst = dst.getSubimage(0, 0, 1, 10);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dst);
for (int i = 0; i < 10; i++) {
if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
throw new RuntimeException(
"Incorrect destination alpha value.");
}
}
}
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel colorModel =
new ComponentColorModel(cs, nBits,
false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
w, h,
w*3, 3,
bOffs, null);
return new BufferedImage(colorModel, raster, false, null);
}
private static BufferedImage createCustomBuffer() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
return new BufferedImage(cm, wr, false, null);
}