use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class SunGraphics2D method drawTranslatedRenderedImage.
/**
* Draw a portion of a RenderedImage tile-by-tile with a given
* integer image to user space translation. The user to
* device transform must also be an integer translation.
*/
private void drawTranslatedRenderedImage(RenderedImage img, Rectangle region, int i2uTransX, int i2uTransY) {
// Cache tile grid info
int tileGridXOffset = img.getTileGridXOffset();
int tileGridYOffset = img.getTileGridYOffset();
int tileWidth = img.getTileWidth();
int tileHeight = img.getTileHeight();
// Determine the tile index extrema in each direction
int minTileX = getTileIndex(region.x, tileGridXOffset, tileWidth);
int minTileY = getTileIndex(region.y, tileGridYOffset, tileHeight);
int maxTileX = getTileIndex(region.x + region.width - 1, tileGridXOffset, tileWidth);
int maxTileY = getTileIndex(region.y + region.height - 1, tileGridYOffset, tileHeight);
// Create a single ColorModel to use for all BufferedImages
ColorModel colorModel = img.getColorModel();
// Reuse the same Rectangle for each iteration
Rectangle tileRect = new Rectangle();
for (int ty = minTileY; ty <= maxTileY; ty++) {
for (int tx = minTileX; tx <= maxTileX; tx++) {
// Get the current tile.
Raster raster = img.getTile(tx, ty);
// Fill in tileRect with the tile bounds
tileRect.x = tx * tileWidth + tileGridXOffset;
tileRect.y = ty * tileHeight + tileGridYOffset;
tileRect.width = tileWidth;
tileRect.height = tileHeight;
// Clip the tile against the image bounds and
// backwards mapped clip region
// The result can't be empty
clipTo(tileRect, region);
// Create a WritableRaster containing the tile
WritableRaster wRaster = null;
if (raster instanceof WritableRaster) {
wRaster = (WritableRaster) raster;
} else {
// Create a WritableRaster in the same coordinate system
// as the original raster.
wRaster = Raster.createWritableRaster(raster.getSampleModel(), raster.getDataBuffer(), null);
}
// Translate wRaster to start at (0, 0) and to contain
// only the relevent portion of the tile
wRaster = wRaster.createWritableChild(tileRect.x, tileRect.y, tileRect.width, tileRect.height, 0, 0, null);
// Wrap wRaster in a BufferedImage
BufferedImage bufImg = new BufferedImage(colorModel, wRaster, colorModel.isAlphaPremultiplied(), null);
// Now we have a BufferedImage starting at (0, 0) that
// represents data from a Raster starting at
// (tileRect.x, tileRect.y). Additionally, it needs
// to be translated by (i2uTransX, i2uTransY). We call
// copyImage to draw just the region of interest
// without needing to create a child image.
copyImage(bufImg, tileRect.x + i2uTransX, tileRect.y + i2uTransY, 0, 0, tileRect.width, tileRect.height, null, null);
}
}
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class CGLVolatileSurfaceManager method initAcceleratedSurface.
/**
* Create a pbuffer-based SurfaceData object (or init the backbuffer
* of an existing window if this is a double buffered GraphicsConfig)
*/
protected SurfaceData initAcceleratedSurface() {
SurfaceData sData = null;
Component comp = vImg.getComponent();
final ComponentPeer peer = (comp != null) ? comp.getPeer() : null;
try {
boolean createVSynced = false;
boolean forceback = false;
if (context instanceof Boolean) {
forceback = ((Boolean) context).booleanValue();
if (forceback && peer instanceof BackBufferCapsProvider) {
BackBufferCapsProvider provider = (BackBufferCapsProvider) peer;
BufferCapabilities caps = provider.getBackBufferCaps();
if (caps instanceof ExtendedBufferCapabilities) {
ExtendedBufferCapabilities ebc = (ExtendedBufferCapabilities) caps;
if (ebc.getVSync() == VSYNC_ON && ebc.getFlipContents() == COPIED) {
createVSynced = true;
forceback = false;
}
}
}
}
if (forceback) {
// peer must be non-null in this case
// TODO: modify parameter to delegate
// sData = CGLSurfaceData.createData(peer, vImg, FLIP_BACKBUFFER);
} else {
CGLGraphicsConfig gc = (CGLGraphicsConfig) vImg.getGraphicsConfig();
ColorModel cm = gc.getColorModel(vImg.getTransparency());
int type = vImg.getForcedAccelSurfaceType();
// use the forced type, otherwise choose one based on caps
if (type == OGLSurfaceData.UNDEFINED) {
type = gc.isCapPresent(CAPS_EXT_FBOBJECT) ? OGLSurfaceData.FBOBJECT : OGLSurfaceData.PBUFFER;
}
if (createVSynced) {
// TODO: modify parameter to delegate
// sData = CGLSurfaceData.createData(peer, vImg, type);
} else {
sData = CGLSurfaceData.createData(gc, vImg.getWidth(), vImg.getHeight(), cm, vImg, type);
}
}
} catch (NullPointerException ex) {
sData = null;
} catch (OutOfMemoryError er) {
sData = null;
}
return sData;
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class GIFImageWriteParam method writeImage.
/**
* Writes any extension blocks, the Image Descriptor, and the image data
*
* @param iioimage The image and image metadata.
* @param param The write parameters.
* @param globalColorTable The Global Color Table.
* @param sourceBounds The source region.
* @param destSize The destination dimensions.
*/
private void writeImage(RenderedImage image, GIFWritableImageMetadata imageMetadata, ImageWriteParam param, byte[] globalColorTable, Rectangle sourceBounds, Dimension destSize) throws IOException {
ColorModel colorModel = image.getColorModel();
SampleModel sampleModel = image.getSampleModel();
boolean writeGraphicsControlExtension;
if (imageMetadata == null) {
// Create default metadata.
imageMetadata = (GIFWritableImageMetadata) getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
// Set GraphicControlExtension flag only if there is
// transparency.
writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
} else {
// Check for GraphicControlExtension element.
NodeList list = null;
try {
IIOMetadataNode root = (IIOMetadataNode) imageMetadata.getAsTree(IMAGE_METADATA_NAME);
list = root.getElementsByTagName("GraphicControlExtension");
} catch (IllegalArgumentException iae) {
// Should never happen.
}
// Set GraphicControlExtension flag if element present.
writeGraphicsControlExtension = list != null && list.getLength() > 0;
// the interlacing is set per the ImageWriteParam mode setting.
if (param != null && param.canWriteProgressive()) {
if (param.getProgressiveMode() == ImageWriteParam.MODE_DISABLED) {
imageMetadata.interlaceFlag = false;
} else if (param.getProgressiveMode() == ImageWriteParam.MODE_DEFAULT) {
imageMetadata.interlaceFlag = true;
}
}
}
// Unset local color table if equal to global color table.
if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
imageMetadata.localColorTable = null;
}
// Override dimensions
imageMetadata.imageWidth = destSize.width;
imageMetadata.imageHeight = destSize.height;
// Write Graphics Control Extension.
if (writeGraphicsControlExtension) {
writeGraphicControlExtension(imageMetadata);
}
// Write extension blocks.
writePlainTextExtension(imageMetadata);
writeApplicationExtension(imageMetadata);
writeCommentExtension(imageMetadata);
// Write Image Descriptor
int bitsPerPixel = getNumBits(imageMetadata.localColorTable == null ? (globalColorTable == null ? sampleModel.getSampleSize(0) : globalColorTable.length / 3) : imageMetadata.localColorTable.length / 3);
writeImageDescriptor(imageMetadata, bitsPerPixel);
// Write image data
writeRasterData(image, sourceBounds, destSize, param, imageMetadata.interlaceFlag);
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class GIFImageWriterSpi method canEncodeImage.
public boolean canEncodeImage(ImageTypeSpecifier type) {
if (type == null) {
throw new IllegalArgumentException("type == null!");
}
SampleModel sm = type.getSampleModel();
ColorModel cm = type.getColorModel();
boolean canEncode = sm.getNumBands() == 1 && sm.getSampleSize(0) <= 8 && sm.getWidth() <= 65535 && sm.getHeight() <= 65535 && (cm == null || cm.getComponentSize()[0] <= 8);
if (canEncode) {
return true;
} else {
return PaletteBuilder.canCreatePalette(type);
}
}
use of java.awt.image.ColorModel in project jdk8u_jdk by JetBrains.
the class GIFImageReader method createIndexed.
// We don't check all parameters as ImageTypeSpecifier.createIndexed do
// since this method is private and we pass consistent data here
private ImageTypeSpecifier createIndexed(byte[] r, byte[] g, byte[] b, int bits) {
ColorModel colorModel;
if (imageMetadata.transparentColorFlag) {
// Some files erroneously have a transparent color index
// of 255 even though there are fewer than 256 colors.
int idx = Math.min(imageMetadata.transparentColorIndex, r.length - 1);
colorModel = new IndexColorModel(bits, r.length, r, g, b, idx);
} else {
colorModel = new IndexColorModel(bits, r.length, r, g, b);
}
SampleModel sampleModel;
if (bits == 8) {
int[] bandOffsets = { 0 };
sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, 1, 1, bandOffsets);
} else {
sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, bits);
}
return new ImageTypeSpecifier(colorModel, sampleModel);
}
Aggregations