use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class PNGImageWriterSpi method canEncodeImage.
public boolean canEncodeImage(ImageTypeSpecifier type) {
SampleModel sampleModel = type.getSampleModel();
ColorModel colorModel = type.getColorModel();
// Find the maximum bit depth across all channels
int[] sampleSize = sampleModel.getSampleSize();
int bitDepth = sampleSize[0];
for (int i = 1; i < sampleSize.length; i++) {
if (sampleSize[i] > bitDepth) {
bitDepth = sampleSize[i];
}
}
// Ensure bitDepth is between 1 and 16
if (bitDepth < 1 || bitDepth > 16) {
return false;
}
// Check number of bands, alpha
int numBands = sampleModel.getNumBands();
if (numBands < 1 || numBands > 4) {
return false;
}
boolean hasAlpha = colorModel.hasAlpha();
// the check below to fail and return false.
if (colorModel instanceof IndexColorModel) {
return true;
}
if ((numBands == 1 || numBands == 3) && hasAlpha) {
return false;
}
if ((numBands == 2 || numBands == 4) && !hasAlpha) {
return false;
}
return true;
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class PNGMetadata method initialize.
/**
* Sets the IHDR_bitDepth and IHDR_colorType variables.
* The <code>numBands</code> parameter is necessary since
* we may only be writing a subset of the image bands.
*/
public void initialize(ImageTypeSpecifier imageType, int numBands) {
ColorModel colorModel = imageType.getColorModel();
SampleModel sampleModel = imageType.getSampleModel();
// Initialize IHDR_bitDepth
int[] sampleSize = sampleModel.getSampleSize();
int bitDepth = sampleSize[0];
// Fixes bug 4413109
for (int i = 1; i < sampleSize.length; i++) {
if (sampleSize[i] > bitDepth) {
bitDepth = sampleSize[i];
}
}
// Multi-channel images must have a bit depth of 8 or 16
if (sampleSize.length > 1 && bitDepth < 8) {
bitDepth = 8;
}
// Round bit depth up to a power of 2
if (bitDepth > 2 && bitDepth < 4) {
bitDepth = 4;
} else if (bitDepth > 4 && bitDepth < 8) {
bitDepth = 8;
} else if (bitDepth > 8 && bitDepth < 16) {
bitDepth = 16;
} else if (bitDepth > 16) {
throw new RuntimeException("bitDepth > 16!");
}
IHDR_bitDepth = bitDepth;
// Initialize IHDR_colorType
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel) colorModel;
int size = icm.getMapSize();
byte[] reds = new byte[size];
icm.getReds(reds);
byte[] greens = new byte[size];
icm.getGreens(greens);
byte[] blues = new byte[size];
icm.getBlues(blues);
// Determine whether the color tables are actually a gray ramp
// if the color type has not been set previously
boolean isGray = false;
if (!IHDR_present || (IHDR_colorType != PNGImageReader.PNG_COLOR_PALETTE)) {
isGray = true;
int scale = 255 / ((1 << IHDR_bitDepth) - 1);
for (int i = 0; i < size; i++) {
byte red = reds[i];
if ((red != (byte) (i * scale)) || (red != greens[i]) || (red != blues[i])) {
isGray = false;
break;
}
}
}
// Determine whether transparency exists
boolean hasAlpha = colorModel.hasAlpha();
byte[] alpha = null;
if (hasAlpha) {
alpha = new byte[size];
icm.getAlphas(alpha);
}
if (isGray && hasAlpha && (bitDepth == 8 || bitDepth == 16)) {
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
} else if (isGray && !hasAlpha) {
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
} else {
IHDR_colorType = PNGImageReader.PNG_COLOR_PALETTE;
PLTE_present = true;
PLTE_order = null;
PLTE_red = (byte[]) reds.clone();
PLTE_green = (byte[]) greens.clone();
PLTE_blue = (byte[]) blues.clone();
if (hasAlpha) {
tRNS_present = true;
tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE;
PLTE_order = new int[alpha.length];
// Reorder the palette so that non-opaque entries
// come first. Since the tRNS chunk does not have
// to store trailing 255's, this can save a
// considerable amount of space when encoding
// images with only one transparent pixel value,
// e.g., images from GIF sources.
byte[] newAlpha = new byte[alpha.length];
// Scan for non-opaque entries and assign them
// positions starting at 0.
int newIndex = 0;
for (int i = 0; i < alpha.length; i++) {
if (alpha[i] != (byte) 255) {
PLTE_order[i] = newIndex;
newAlpha[newIndex] = alpha[i];
++newIndex;
}
}
int numTransparent = newIndex;
// positions following the non-opaque entries.
for (int i = 0; i < alpha.length; i++) {
if (alpha[i] == (byte) 255) {
PLTE_order[i] = newIndex++;
}
}
// Reorder the palettes
byte[] oldRed = PLTE_red;
byte[] oldGreen = PLTE_green;
byte[] oldBlue = PLTE_blue;
// All have the same length
int len = oldRed.length;
PLTE_red = new byte[len];
PLTE_green = new byte[len];
PLTE_blue = new byte[len];
for (int i = 0; i < len; i++) {
PLTE_red[PLTE_order[i]] = oldRed[i];
PLTE_green[PLTE_order[i]] = oldGreen[i];
PLTE_blue[PLTE_order[i]] = oldBlue[i];
}
// Copy only the transparent entries into tRNS_alpha
tRNS_alpha = new byte[numTransparent];
System.arraycopy(newAlpha, 0, tRNS_alpha, 0, numTransparent);
}
}
} else {
if (numBands == 1) {
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY;
} else if (numBands == 2) {
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA;
} else if (numBands == 3) {
IHDR_colorType = PNGImageReader.PNG_COLOR_RGB;
} else if (numBands == 4) {
IHDR_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA;
} else {
throw new RuntimeException("Number of bands not 1-4!");
}
}
IHDR_present = true;
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class RenderableImageProducer method run.
/**
* The runnable method for this class. This will produce an image using
* the current RenderableImage and RenderContext and send it to all the
* ImageConsumer currently registered with this class.
*/
public void run() {
// First get the rendered image
RenderedImage rdrdImage;
if (rc != null) {
rdrdImage = rdblImage.createRendering(rc);
} else {
rdrdImage = rdblImage.createDefaultRendering();
}
// And its ColorModel
ColorModel colorModel = rdrdImage.getColorModel();
Raster raster = rdrdImage.getData();
SampleModel sampleModel = raster.getSampleModel();
DataBuffer dataBuffer = raster.getDataBuffer();
if (colorModel == null) {
colorModel = ColorModel.getRGBdefault();
}
int minX = raster.getMinX();
int minY = raster.getMinY();
int width = raster.getWidth();
int height = raster.getHeight();
Enumeration icList;
ImageConsumer ic;
// Set up the ImageConsumers
icList = ics.elements();
while (icList.hasMoreElements()) {
ic = (ImageConsumer) icList.nextElement();
ic.setDimensions(width, height);
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES | ImageConsumer.SINGLEPASS | ImageConsumer.SINGLEFRAME);
}
// Get RGB pixels from the raster scanline by scanline and
// send to consumers.
int[] pix = new int[width];
int i, j;
int numBands = sampleModel.getNumBands();
int[] tmpPixel = new int[numBands];
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
sampleModel.getPixel(i, j, tmpPixel, dataBuffer);
pix[i] = colorModel.getDataElement(tmpPixel, 0);
}
// Now send the scanline to the Consumers
icList = ics.elements();
while (icList.hasMoreElements()) {
ic = (ImageConsumer) icList.nextElement();
ic.setPixels(0, j, width, 1, colorModel, pix, 0, width);
}
}
// Now tell the consumers we're done.
icList = ics.elements();
while (icList.hasMoreElements()) {
ic = (ImageConsumer) icList.nextElement();
ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
}
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class XorPixelWriter method createXorPixelWriter.
static PixelWriter createXorPixelWriter(SunGraphics2D sg2d, SurfaceData sData) {
ColorModel dstCM = sData.getColorModel();
Object srcPixel = dstCM.getDataElements(sg2d.eargb, null);
XORComposite comp = (XORComposite) sg2d.getComposite();
int xorrgb = comp.getXorColor().getRGB();
Object xorPixel = dstCM.getDataElements(xorrgb, null);
switch(dstCM.getTransferType()) {
case DataBuffer.TYPE_BYTE:
return new XorPixelWriter.ByteData(srcPixel, xorPixel);
case DataBuffer.TYPE_SHORT:
case DataBuffer.TYPE_USHORT:
return new XorPixelWriter.ShortData(srcPixel, xorPixel);
case DataBuffer.TYPE_INT:
return new XorPixelWriter.IntData(srcPixel, xorPixel);
case DataBuffer.TYPE_FLOAT:
return new XorPixelWriter.FloatData(srcPixel, xorPixel);
case DataBuffer.TYPE_DOUBLE:
return new XorPixelWriter.DoubleData(srcPixel, xorPixel);
default:
throw new InternalError("Unsupported XOR pixel type");
}
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class Bug7049339 method main.
public static void main(String[] argv) {
int x = 100, y = 100;
BufferedImage src = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB);
BufferedImage dst = new BufferedImage(x, y, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D dstg2d = dst.createGraphics();
dstg2d.setComposite(new Composite() {
@Override
public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {
return new CompositeContext() {
@Override
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
// do nothing
}
@Override
public void dispose() {
}
};
}
});
Shape clip = new Ellipse2D.Double(x / 4, y / 4, x / 2, y / 2);
dstg2d.setClip(clip);
// This will throw a RasterFormatException if the bug is present.
dstg2d.drawImage(src, 0, 0, null);
}
Aggregations