Search in sources :

Example 36 with ColorModel

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);
        }
    }
}
Also used : ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) GradientPaint(java.awt.GradientPaint) BufferedImage(java.awt.image.BufferedImage)

Example 37 with ColorModel

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;
}
Also used : ComponentPeer(java.awt.peer.ComponentPeer) ExtendedBufferCapabilities(sun.java2d.pipe.hw.ExtendedBufferCapabilities) SurfaceData(sun.java2d.SurfaceData) ExtendedBufferCapabilities(sun.java2d.pipe.hw.ExtendedBufferCapabilities) BufferCapabilities(java.awt.BufferCapabilities) ColorModel(java.awt.image.ColorModel) BackBufferCapsProvider(sun.java2d.BackBufferCapsProvider) Component(java.awt.Component)

Example 38 with ColorModel

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);
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) NodeList(org.w3c.dom.NodeList) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 39 with ColorModel

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);
    }
}
Also used : SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel)

Example 40 with ColorModel

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);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

ColorModel (java.awt.image.ColorModel)104 IndexColorModel (java.awt.image.IndexColorModel)44 BufferedImage (java.awt.image.BufferedImage)43 WritableRaster (java.awt.image.WritableRaster)34 DirectColorModel (java.awt.image.DirectColorModel)31 ComponentColorModel (java.awt.image.ComponentColorModel)24 SampleModel (java.awt.image.SampleModel)17 Raster (java.awt.image.Raster)16 ColorSpace (java.awt.color.ColorSpace)12 Graphics2D (java.awt.Graphics2D)11 Rectangle (java.awt.Rectangle)9 SurfaceData (sun.java2d.SurfaceData)9 Point (java.awt.Point)8 ComponentSampleModel (java.awt.image.ComponentSampleModel)8 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)7 PackedColorModel (java.awt.image.PackedColorModel)6 Component (java.awt.Component)4 Paint (java.awt.Paint)4 RenderingHints (java.awt.RenderingHints)4 AffineTransform (java.awt.geom.AffineTransform)4