Java Code Examples for java.awt.image.ColorModel

The following examples show how to use java.awt.image.ColorModel. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: SIMVA-SoS   Source File: FXGraphics2D.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts a rendered image to a {@code BufferedImage}.  This utility
 * method has come from a forum post by Jim Moore at:
 * <p>
 * <a href="http://www.jguru.com/faq/view.jsp?EID=114602">
 * http://www.jguru.com/faq/view.jsp?EID=114602</a>
 * 
 * @param img  the rendered image.
 * 
 * @return A buffered image. 
 */
private static BufferedImage convertRenderedImage(RenderedImage img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }
    ColorModel cm = img.getColorModel();
    int width = img.getWidth();
    int height = img.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    Hashtable properties = new Hashtable();
    String[] keys = img.getPropertyNames();
    if (keys != null) {
        for (int i = 0; i < keys.length; i++) {
            properties.put(keys[i], img.getProperty(keys[i]));
        }
    }
    BufferedImage result = new BufferedImage(cm, raster, 
            isAlphaPremultiplied, properties);
    img.copyData(raster);
    return result;
}
 
Example 2
Source Project: jdk8u60   Source File: GLXGraphicsConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public ColorModel getColorModel(int transparency) {
    switch (transparency) {
    case Transparency.OPAQUE:
        // REMIND: once the ColorModel spec is changed, this should be
        //         an opaque premultiplied DCM...
        return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    case Transparency.BITMASK:
        return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
    case Transparency.TRANSLUCENT:
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        return new DirectColorModel(cs, 32,
                                    0xff0000, 0xff00, 0xff, 0xff000000,
                                    true, DataBuffer.TYPE_INT);
    default:
        return null;
    }
}
 
Example 3
Source Project: openjdk-8   Source File: XWindow.java    License: GNU General Public License v2.0 6 votes vote down vote up
final public void xSetBackground(Color c) {
    XToolkit.awtLock();
    try {
        winBackground(c);
        // fix for 6558510: handle sun.awt.noerasebackground flag,
        // see doEraseBackground() and preInit() methods in XCanvasPeer
        if (!doEraseBackground()) {
            return;
        }
        // 6304250: XAWT: Items in choice show a blue border on OpenGL + Solaris10 when background color is set
        // Note: When OGL is enabled, surfaceData.pixelFor() will not
        // return a pixel value appropriate for passing to
        // XSetWindowBackground().  Therefore, we will use the ColorModel
        // for this component in order to calculate a pixel value from
        // the given RGB value.
        ColorModel cm = getColorModel();
        int pixel = PixelConverter.instance.rgbToPixel(c.getRGB(), cm);
        XlibWrapper.XSetWindowBackground(XToolkit.getDisplay(), getContentWindow(), pixel);
        XlibWrapper.XClearWindow(XToolkit.getDisplay(), getContentWindow());
    }
    finally {
        XToolkit.awtUnlock();
    }
}
 
Example 4
/**
 * Returns the color model associated with this configuration that
 * supports the specified transparency.
 */
public ColorModel getColorModel(int transparency) {

    if (model.getTransparency() == transparency) {
        return model;
    }
    switch (transparency) {
    case Transparency.OPAQUE:
        return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    case Transparency.BITMASK:
        return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
    case Transparency.TRANSLUCENT:
        return ColorModel.getRGBdefault();
    default:
        return null;
    }
}
 
Example 5
Source Project: pdfxtk   Source File: BinarizeDescriptor.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates an BinarizeOpImage with a given ParameterBlock */

  public RenderedImage create(ParameterBlock paramBlock, 
			      RenderingHints renderingHints)
  {
    RenderedImage img = paramBlock.getRenderedSource(0);

    ImageLayout il = new ImageLayout(img);
    ColorModel cm = new IndexColorModel(1, 2, bwColors, bwColors, bwColors);
    SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
						     img.getWidth(),
						     img.getHeight(),
						     1);

    il.setColorModel(cm);
    il.setSampleModel(sm);

    return new BinarizeOpImage(paramBlock.getRenderedSource(0),
			       renderingHints,
			       il,
			       (Integer)paramBlock.getObjectParameter(0));
  }
 
Example 6
/**
 * The setDimensions method is part of the ImageConsumer API which
 * this class must implement to retrieve the pixels.
 * <p>
 * Note: This method is intended to be called by the ImageProducer
 * of the Image whose pixels are being grabbed.  Developers using
 * this class to retrieve pixels from an image should avoid calling
 * this method directly since that operation could result in problems
 * with retrieving the requested pixels.
 * @param width the width of the dimension
 * @param height the height of the dimension
 */
public void setDimensions(int width, int height) {
    if (dstW < 0) {
        dstW = width - dstX;
    }
    if (dstH < 0) {
        dstH = height - dstY;
    }
    if (dstW <= 0 || dstH <= 0) {
        imageComplete(STATICIMAGEDONE);
    } else if (intPixels == null &&
               imageModel == ColorModel.getRGBdefault()) {
        intPixels = new int[dstW * dstH];
        dstScan = dstW;
        dstOff = 0;
    }
    flags |= (ImageObserver.WIDTH | ImageObserver.HEIGHT);
}
 
Example 7
Source Project: scipio-erp   Source File: ImageType.java    License: Apache License 2.0 6 votes vote down vote up
public static String printImageTypeInfo(BufferedImage image) {
    StringBuilder sb = new StringBuilder("[image pixel type: ");
    sb.append(ImagePixelType.getTypeConstantName(image.getType(), "(INVALID)"));
    ColorModel cm = image.getColorModel();
    sb.append("; bits per pixel: ");
    sb.append(cm.getPixelSize());

    if (image.getColorModel() instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        sb.append("; color array size: ");
        sb.append(icm.getMapSize());
    }

    sb.append("]");
    return sb.toString();
}
 
Example 8
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);
}
 
Example 9
Source Project: openjdk-jdk9   Source File: PixelGrabber.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * The setDimensions method is part of the ImageConsumer API which
 * this class must implement to retrieve the pixels.
 * <p>
 * Note: This method is intended to be called by the ImageProducer
 * of the Image whose pixels are being grabbed.  Developers using
 * this class to retrieve pixels from an image should avoid calling
 * this method directly since that operation could result in problems
 * with retrieving the requested pixels.
 * @param width the width of the dimension
 * @param height the height of the dimension
 */
public void setDimensions(int width, int height) {
    if (dstW < 0) {
        dstW = width - dstX;
    }
    if (dstH < 0) {
        dstH = height - dstY;
    }
    if (dstW <= 0 || dstH <= 0) {
        imageComplete(STATICIMAGEDONE);
    } else if (intPixels == null &&
               imageModel == ColorModel.getRGBdefault()) {
        intPixels = new int[dstW * dstH];
        dstScan = dstW;
        dstOff = 0;
    }
    flags |= (ImageObserver.WIDTH | ImageObserver.HEIGHT);
}
 
Example 10
public SunCompositeContext(AlphaComposite ac,
                           ColorModel s, ColorModel d)
{
    if (s == null) {
        throw new NullPointerException("Source color model cannot be null");
    }
    if (d == null) {
        throw new NullPointerException("Destination color model cannot be null");
    }
    srcCM = s;
    dstCM = d;
    this.composite = ac;
    this.comptype = CompositeType.forAlphaComposite(ac);
}
 
Example 11
@Override
public BufferedImage createCompatibleImage(int width, int height) {
    ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
    WritableRaster
        raster = model.createCompatibleWritableRaster(width, height);
    return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
                             null);
}
 
Example 12
public BufferedImage getOpaqueRGBImage() {
    if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) {
        int w = bimage.getWidth();
        int h = bimage.getHeight();
        int size = w * h;

        // Note that we steal the data array here, but only for reading...
        DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer();
        int[] pixels = SunWritableRaster.stealData(db, 0);

        for (int i = 0; i < size; i++) {
            if ((pixels[i] >>> 24) != 0xff) {
                return bimage;
            }
        }

        ColorModel opModel = new DirectColorModel(24,
                                                  0x00ff0000,
                                                  0x0000ff00,
                                                  0x000000ff);

        int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff};
        WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w,
                                                            bandmasks,
                                                            null);

        try {
            BufferedImage opImage = createImage(opModel, opRaster,
                                                false, null);
            return opImage;
        } catch (Exception e) {
            return bimage;
        }
    }
    return bimage;
}
 
Example 13
protected GLXSurfaceData(X11ComponentPeer peer, GLXGraphicsConfig gc,
                         ColorModel cm, int type)
{
    super(gc, cm, type);
    this.peer = peer;
    this.graphicsConfig = gc;
    initOps(gc, peer, graphicsConfig.getAData());
}
 
Example 14
Source Project: openjdk-8-source   Source File: Blit.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void Blit(SurfaceData srcData,
                 SurfaceData dstData,
                 Composite comp,
                 Region clip,
                 int srcx, int srcy,
                 int dstx, int dsty,
                 int width, int height)
{
    ColorModel srcCM = srcData.getColorModel();
    ColorModel dstCM = dstData.getColorModel();
    // REMIND: Should get RenderingHints from sg2d
    CompositeContext ctx = comp.createContext(srcCM, dstCM,
                                              new RenderingHints(null));
    Raster srcRas = srcData.getRaster(srcx, srcy, width, height);
    WritableRaster dstRas =
        (WritableRaster) dstData.getRaster(dstx, dsty, width, height);

    if (clip == null) {
        clip = Region.getInstanceXYWH(dstx, dsty, width, height);
    }
    int span[] = {dstx, dsty, dstx+width, dsty+height};
    SpanIterator si = clip.getSpanIterator(span);
    srcx -= dstx;
    srcy -= dsty;
    while (si.nextSpan(span)) {
        int w = span[2] - span[0];
        int h = span[3] - span[1];
        Raster tmpSrcRas = srcRas.createChild(srcx + span[0], srcy + span[1],
                                              w, h, 0, 0, null);
        WritableRaster tmpDstRas = dstRas.createWritableChild(span[0], span[1],
                                                              w, h, 0, 0, null);
        ctx.compose(tmpSrcRas, tmpDstRas, tmpDstRas);
    }
    ctx.dispose();
}
 
Example 15
Source Project: openjdk-jdk8u-backup   Source File: XWindow.java    License: GNU General Public License v2.0 5 votes vote down vote up
public ColorModel getColorModel() {
    if (graphicsConfig != null) {
        return graphicsConfig.getColorModel ();
    }
    else {
        return XToolkit.getStaticColorModel();
    }
}
 
Example 16
Source Project: jdk8u_jdk   Source File: BMPSubsamplingTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 17
/**
 * Returns the non-dynamic ColorModel associated with this device
 */
public ColorModel getColorModel() {
    if (colorModel == null)  {
        colorModel = makeColorModel(screen, false);
    }
    return colorModel;
}
 
Example 18
Source Project: TencentKona-8   Source File: D3DSurfaceData.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected D3DSurfaceData(WComponentPeer peer, D3DGraphicsConfig gc,
                         int width, int height, Image image,
                         ColorModel cm, int numBackBuffers,
                         int swapEffect, VSyncType vSyncType,
                         int type)
{
    super(getCustomSurfaceType(type), cm);
    this.graphicsDevice = gc.getD3DDevice();
    this.peer = peer;
    this.type = type;
    this.width = width;
    this.height = height;
    this.offscreenImage = image;
    this.backBuffersNum = numBackBuffers;
    this.swapEffect = swapEffect;
    this.syncType = vSyncType;

    initOps(graphicsDevice.getScreen(), width, height);
    if (type == WINDOW) {
        // we put the surface into the "lost"
        // state; it will be restored by the D3DScreenUpdateManager
        // prior to rendering to it for the first time. This is done
        // so that vram is not wasted for surfaces never rendered to
        setSurfaceLost(true);
    } else {
        initSurface();
    }
    setBlitProxyKey(gc.getProxyKey());
}
 
Example 19
Source Project: JDKSourceCode1.8   Source File: JPEGImageWriter.java    License: MIT License 5 votes vote down vote up
private int getDefaultDestCSType(ColorModel cm) {
    int retval = JPEG.JCS_UNKNOWN;
    if (cm != null) {
        boolean alpha = cm.hasAlpha();
        ColorSpace cs = cm.getColorSpace();
        switch (cs.getType()) {
        case ColorSpace.TYPE_GRAY:
            retval = JPEG.JCS_GRAYSCALE;
            break;
        case ColorSpace.TYPE_RGB:
            if (alpha) {
                retval = JPEG.JCS_YCbCrA;
            } else {
                retval = JPEG.JCS_YCbCr;
            }
            break;
        case ColorSpace.TYPE_YCbCr:
            if (alpha) {
                retval = JPEG.JCS_YCbCrA;
            } else {
                retval = JPEG.JCS_YCbCr;
            }
            break;
        case ColorSpace.TYPE_3CLR:
            if (cs == JPEG.JCS.getYCC()) {
                if (alpha) {
                    retval = JPEG.JCS_YCCA;
                } else {
                    retval = JPEG.JCS_YCC;
                }
            }
        case ColorSpace.TYPE_CMYK:
            retval = JPEG.JCS_YCCK;
            break;
        }
    }
    return retval;
}
 
Example 20
Source Project: Java8CN   Source File: GradientPaintContext.java    License: Apache License 2.0 5 votes vote down vote up
static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
    if (cm == cachedModel) {
        if (cached != null) {
            Raster ras = (Raster) cached.get();
            if (ras != null &&
                ras.getWidth() >= w &&
                ras.getHeight() >= h)
            {
                cached = null;
                return ras;
            }
        }
    }
    return cm.createCompatibleWritableRaster(w, h);
}
 
Example 21
/**
 * Choose which rows and columns of the delivered int pixels are
 * needed for the destination scaled image and pass through just
 * those rows and columns that are needed, replicated as necessary.
 * <p>
 * Note: This method is intended to be called by the
 * <code>ImageProducer</code> of the <code>Image</code> whose pixels
 * are being filtered. Developers using
 * this class to filter pixels from an image should avoid calling
 * this method directly since that operation could interfere
 * with the filtering operation.
 */
public void setPixels(int x, int y, int w, int h,
                      ColorModel model, int pixels[], int off,
                      int scansize) {
    if (srcrows == null || srccols == null) {
        calculateMaps();
    }
    int sx, sy;
    int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth);
    int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight);
    int outpix[];
    if (outpixbuf != null && outpixbuf instanceof int[]) {
        outpix = (int[]) outpixbuf;
    } else {
        outpix = new int[destWidth];
        outpixbuf = outpix;
    }
    for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) {
        int srcoff = off + scansize * (sy - y);
        int dx;
        for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) {
            outpix[dx] = pixels[srcoff + sx - x];
        }
        if (dx > dx1) {
            consumer.setPixels(dx1, dy, dx - dx1, 1,
                               model, outpix, dx1, destWidth);
        }
    }
}
 
Example 22
/**
 * Returns the color model associated with this configuration.
 */
public ColorModel getColorModel() {
    if (theModel == null) {
        BufferedImage bufImg =
            new BufferedImage(1,1, BufferedImage.TYPE_3BYTE_BGR);
        theModel = bufImg.getColorModel();
    }

    return theModel;
}
 
Example 23
public Any(WritableRaster srcRas, ColorModel cm,
           AffineTransform xform, int maxw, boolean filter)
{
    super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
    this.srcRas = srcRas;
    this.filter = filter;
}
 
Example 24
/**
 * Creates a new hidden-acceleration image of the given width and height
 * that is associated with the target Component.
 */
public Image createAcceleratedImage(Component target,
                                    int width, int height)
{
    // As of 1.7 we no longer create pmoffscreens here...
    ColorModel model = getColorModel(Transparency.OPAQUE);
    WritableRaster wr =
        model.createCompatibleWritableRaster(width, height);
    return new OffScreenImage(target, model, wr,
                              model.isAlphaPremultiplied());
}
 
Example 25
Source Project: TencentKona-8   Source File: GLXSurfaceData.java    License: GNU General Public License v2.0 5 votes vote down vote up
public GLXOffScreenSurfaceData(X11ComponentPeer peer,
                               GLXGraphicsConfig gc,
                               int width, int height,
                               Image image, ColorModel cm,
                               int type)
{
    super(peer, gc, cm, type);

    this.width = width;
    this.height = height;
    offscreenImage = image;

    initSurface(width, height);
}
 
Example 26
/**************************** RescaleOp support *****************************/

    public static boolean isRescaleOpValid(RescaleOp rop,
                                           BufferedImage srcImg)
    {
        int numFactors = rop.getNumFactors();
        ColorModel srcCM = srcImg.getColorModel();

        if (srcCM instanceof IndexColorModel) {
            throw new
                IllegalArgumentException("Rescaling cannot be "+
                                         "performed on an indexed image");
        }
        if (numFactors != 1 &&
            numFactors != srcCM.getNumColorComponents() &&
            numFactors != srcCM.getNumComponents())
        {
            throw new IllegalArgumentException("Number of scaling constants "+
                                               "does not equal the number of"+
                                               " of color or color/alpha "+
                                               " components");
        }

        int csType = srcCM.getColorSpace().getType();
        if (csType != ColorSpace.TYPE_RGB &&
            csType != ColorSpace.TYPE_GRAY)
        {
            // Not prepared to deal with other color spaces
            return false;
        }

        if (numFactors == 2 || numFactors > 4) {
            // Not really prepared to handle this at the native level, so...
            return false;
        }

        return true;
    }
 
Example 27
public BufferedImage getOpaqueRGBImage() {
    if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) {
        int w = bimage.getWidth();
        int h = bimage.getHeight();
        int size = w * h;

        // Note that we steal the data array here, but only for reading...
        DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer();
        int[] pixels = SunWritableRaster.stealData(db, 0);

        for (int i = 0; i < size; i++) {
            if ((pixels[i] >>> 24) != 0xff) {
                return bimage;
            }
        }

        ColorModel opModel = new DirectColorModel(24,
                                                  0x00ff0000,
                                                  0x0000ff00,
                                                  0x000000ff);

        int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff};
        WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w,
                                                            bandmasks,
                                                            null);

        try {
            BufferedImage opImage = createImage(opModel, opRaster,
                                                false, null);
            return opImage;
        } catch (Exception e) {
            return bimage;
        }
    }
    return bimage;
}
 
Example 28
Source Project: Bytecoder   Source File: GeneralCompositePipe.java    License: Apache License 2.0 5 votes vote down vote up
public TileContext(SunGraphics2D sg, PaintContext pCtx,
                   CompositeContext cCtx, ColorModel cModel) {
    sunG2D = sg;
    paintCtxt = pCtx;
    compCtxt = cCtx;
    compModel = cModel;
}
 
Example 29
Source Project: Bytecoder   Source File: SingleTileRenderedImage.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a SingleTileRenderedImage based on a Raster
 * and a ColorModel.
 *
 * @param ras A Raster that will define tile (0, 0) of the image.
 * @param colorModel A ColorModel that will serve as the image's
 *           ColorModel.
 */
public SingleTileRenderedImage(Raster ras, ColorModel colorModel) {
    this.ras = ras;

    this.tileGridXOffset = this.minX = ras.getMinX();
    this.tileGridYOffset = this.minY = ras.getMinY();
    this.tileWidth = this.width = ras.getWidth();
    this.tileHeight = this.height = ras.getHeight();
    this.sampleModel = ras.getSampleModel();
    this.colorModel = colorModel;
}
 
Example 30
Source Project: sambox   Source File: Type1ShadingPaint.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds,
                                  AffineTransform xform, RenderingHints hints)
{
    try
    {
        return new Type1ShadingContext(shading, cm, xform, matrix);
    }
    catch (IOException e)
    {
        LOG.error("An error occurred while painting", e);
        return new Color(0, 0, 0, 0).createContext(cm, deviceBounds, userBounds, xform, hints);
    }
}