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; }
private void convertToRGB() { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; DataBufferInt dbi = new DataBufferInt(size); // Note that stealData() requires a markDirty() afterwards // since we modify the data in it. int newpixels[] = SunWritableRaster.stealData(dbi, 0); if (cmodel instanceof IndexColorModel && biRaster instanceof ByteComponentRaster && biRaster.getNumDataElements() == 1) { ByteComponentRaster bct = (ByteComponentRaster) biRaster; byte[] data = bct.getDataStorage(); int coff = bct.getDataOffset(0); for (int i = 0; i < size; i++) { newpixels[i] = srcLUT[data[coff + i] & 0xff]; } } else { Object srcpixels = null; int off = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { srcpixels = biRaster.getDataElements(x, y, srcpixels); newpixels[off++] = cmodel.getRGB(srcpixels); } } } // We modified the data array directly above so mark it as dirty now... SunWritableRaster.markDirty(dbi); isSameCM = false; cmodel = ColorModel.getRGBdefault(); int bandMasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}; biRaster = Raster.createPackedRaster(dbi, w, h, w, bandMasks, null); bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null); srcLUT = null; isDefaultBI = true; }
/** * Stores the Raster data at the specified location. An ArrayIndexOutOfBounds exception will be * thrown at runtime if the pixel coordinates are out of bounds. * * @param x The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param inRaster Raster of data to place at x,y location. */ public void setDataElements(int x, int y, Raster inRaster) { // Check if we can use fast code if (!(inRaster instanceof BytePackedRaster) || ((BytePackedRaster) inRaster).pixelBitStride != pixelBitStride) { super.setDataElements(x, y, inRaster); return; } int srcOffX = inRaster.getMinX(); int srcOffY = inRaster.getMinY(); int dstOffX = srcOffX + x; int dstOffY = srcOffY + y; int width = inRaster.getWidth(); int height = inRaster.getHeight(); if ((dstOffX < this.minX) || (dstOffY < this.minY) || (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) { throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!"); } setDataElements(dstOffX, dstOffY, srcOffX, srcOffY, width, height, (BytePackedRaster) inRaster); }
/** * Copies pixels from Raster srcRaster to this WritableRaster. For each (x, y) address in * srcRaster, the corresponding pixel is copied to address (x+dx, y+dy) in this WritableRaster, * unless (x+dx, y+dy) falls outside the bounds of this raster. srcRaster must have the same * number of bands as this WritableRaster. The copy is a simple copy of source samples to the * corresponding destination samples. For details, see {@link WritableRaster#setRect(Raster)}. * * @param dx The X translation factor from src space to dst space of the copy. * @param dy The Y translation factor from src space to dst space of the copy. * @param srcRaster The Raster from which to copy pixels. */ public void setRect(int dx, int dy, Raster srcRaster) { // Check if we can use fast code if (!(srcRaster instanceof BytePackedRaster) || ((BytePackedRaster) srcRaster).pixelBitStride != pixelBitStride) { super.setRect(dx, dy, srcRaster); return; } int width = srcRaster.getWidth(); int height = srcRaster.getHeight(); int srcOffX = srcRaster.getMinX(); int srcOffY = srcRaster.getMinY(); int dstOffX = dx + srcOffX; int dstOffY = dy + srcOffY; // Clip to this raster if (dstOffX < this.minX) { int skipX = this.minX - dstOffX; width -= skipX; srcOffX += skipX; dstOffX = this.minX; } if (dstOffY < this.minY) { int skipY = this.minY - dstOffY; height -= skipY; srcOffY += skipY; dstOffY = this.minY; } if (dstOffX + width > this.maxX) { width = this.maxX - dstOffX; } if (dstOffY + height > this.maxY) { height = this.maxY - dstOffY; } setDataElements( dstOffX, dstOffY, srcOffX, srcOffY, width, height, (BytePackedRaster) srcRaster); }