public void testCreateInterleaved() { ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] bandOffsets = new int[] {1, 2, 3}; int dataType = DataBuffer.TYPE_BYTE; ImageTypeSpecifier type = ImageTypeSpecifier.createInterleaved(colorSpace, bandOffsets, dataType, false, false); ColorModel colorModel = type.getColorModel(); PixelInterleavedSampleModel sampleModel = (PixelInterleavedSampleModel) type.getSampleModel(); // validate the colorModel assertEquals( "Failed to create with the correct colorspace type", ColorSpace.TYPE_RGB, colorModel.getColorSpace().getType()); assertEquals( "Failed to create with the correct transparency", Transparency.OPAQUE, colorModel.getTransparency()); assertEquals( "Failed to create with the correct transfer type", DataBuffer.TYPE_BYTE, colorModel.getTransferType()); // validate the sampleModel assertTrue( "The sampleModel and colorModel are not compatible", colorModel.isCompatibleSampleModel(sampleModel)); assertArrayEquals( "Failed to create with the correct bandOffsets", bandOffsets, sampleModel.getBandOffsets()); assertEquals("Failed to create with the correct pixel stride", 3, sampleModel.getPixelStride()); }
/** * Returns a width- and height-aligned image representation sharing data w/ {@link #image}. * * @param width * @param height * @return * @throws IllegalArgumentException if requested size exceeds image size */ public BufferedImage getAlignedImage(int width, int height) throws IllegalArgumentException { if (width * height > image.getWidth() * image.getHeight()) { throw new IllegalArgumentException( "Requested size exceeds image size: " + width + "x" + height + " > " + image.getWidth() + "x" + image.getHeight()); } if (width == image.getWidth() && height == image.getHeight()) { return image; } else { final ColorModel cm = image.getColorModel(); final WritableRaster raster0 = image.getRaster(); final DataBuffer dataBuffer = raster0.getDataBuffer(); final SinglePixelPackedSampleModel sppsm0 = (SinglePixelPackedSampleModel) raster0.getSampleModel(); final SinglePixelPackedSampleModel sppsm1 = new SinglePixelPackedSampleModel( dataBuffer.getDataType(), width, height, width /* scanLineStride */, sppsm0.getBitMasks()); final WritableRaster raster1 = WritableRaster.createWritableRaster(sppsm1, dataBuffer, null); return new BufferedImage(cm, raster1, cm.isAlphaPremultiplied(), null); } }
/** * Returns a copy of this EditableImage. * * @return A copy of this EditableImage. */ public EditableImage copy() { ColorModel colorModel = image.getColorModel(); boolean isAlphaPremultiplied = colorModel.isAlphaPremultiplied(); WritableRaster writeableRaster = image.copyData(null); return new EditableImage( new BufferedImage(colorModel, writeableRaster, isAlphaPremultiplied, null)); }
private GDIWindowSurfaceData(WComponentPeer peer, SurfaceType sType) { super(sType, peer.getDeviceColorModel()); ColorModel cm = peer.getDeviceColorModel(); this.peer = peer; int rMask = 0, gMask = 0, bMask = 0; int depth; switch (cm.getPixelSize()) { case 32: case 24: if (cm instanceof DirectColorModel) { depth = 32; } else { depth = 24; } break; default: depth = cm.getPixelSize(); } if (cm instanceof DirectColorModel) { DirectColorModel dcm = (DirectColorModel) cm; rMask = dcm.getRedMask(); gMask = dcm.getGreenMask(); bMask = dcm.getBlueMask(); } this.graphicsConfig = (Win32GraphicsConfig) peer.getGraphicsConfiguration(); this.solidloops = graphicsConfig.getSolidLoops(sType); Win32GraphicsDevice gd = graphicsConfig.getDevice(); scaleX = gd.getDefaultScaleX(); scaleY = gd.getDefaultScaleY(); initOps(peer, depth, rMask, gMask, bMask, gd.getScreen()); setBlitProxyKey(graphicsConfig.getProxyKey()); }
public static SurfaceType getSurfaceType(ColorModel cm) { switch (cm.getPixelSize()) { case 32: case 24: if (cm instanceof DirectColorModel) { if (((DirectColorModel) cm).getRedMask() == 0xff0000) { return IntRgbGdi; } else { return SurfaceType.IntRgbx; } } else { return ThreeByteBgrGdi; } case 15: return Ushort555RgbGdi; case 16: if ((cm instanceof DirectColorModel) && (((DirectColorModel) cm).getBlueMask() == 0x3e)) { return SurfaceType.Ushort555Rgbx; } else { return Ushort565RgbGdi; } case 8: if (cm.getColorSpace().getType() == ColorSpace.TYPE_GRAY && cm instanceof ComponentColorModel) { return SurfaceType.ByteGray; } else if (cm instanceof IndexColorModel && isOpaqueGray((IndexColorModel) cm)) { return SurfaceType.Index8Gray; } else { return SurfaceType.ByteIndexedOpaque; } default: throw new sun.java2d.InvalidPipeException( "Unsupported bit " + "depth: " + cm.getPixelSize()); } }
/* */ public void Blit( SurfaceData paramSurfaceData1, SurfaceData paramSurfaceData2, Composite paramComposite, Region paramRegion, int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6) /* */ { /* 120 */ Raster localRaster1 = paramSurfaceData1.getRaster(paramInt1, paramInt2, paramInt5, paramInt6); /* 121 */ ColorModel localColorModel = paramSurfaceData1.getColorModel(); /* */ /* 123 */ Raster localRaster2 = paramSurfaceData2.getRaster(paramInt3, paramInt4, paramInt5, paramInt6); /* 124 */ IntegerComponentRaster localIntegerComponentRaster = (IntegerComponentRaster) localRaster2; /* 125 */ int[] arrayOfInt1 = localIntegerComponentRaster.getDataStorage(); /* */ /* 127 */ Region localRegion = CustomComponent.getRegionOfInterest( paramSurfaceData1, paramSurfaceData2, paramRegion, paramInt1, paramInt2, paramInt3, paramInt4, paramInt5, paramInt6); /* */ /* 130 */ SpanIterator localSpanIterator = localRegion.getSpanIterator(); /* */ /* 132 */ Object localObject = null; /* */ /* 134 */ int i = localIntegerComponentRaster.getScanlineStride(); /* */ /* 136 */ paramInt1 -= paramInt3; /* 137 */ paramInt2 -= paramInt4; /* 138 */ int[] arrayOfInt2 = new int[4]; /* 139 */ while (localSpanIterator.nextSpan(arrayOfInt2)) { /* 140 */ int j = localIntegerComponentRaster.getDataOffset(0) + arrayOfInt2[1] * i + arrayOfInt2[0]; /* 141 */ for (int k = arrayOfInt2[1]; k < arrayOfInt2[3]; k++) { /* 142 */ int m = j; /* 143 */ for (int n = arrayOfInt2[0]; n < arrayOfInt2[2]; n++) { /* 144 */ localObject = localRaster1.getDataElements(n + paramInt1, k + paramInt2, localObject); /* 145 */ arrayOfInt1[(m++)] = localColorModel.getRGB(localObject); /* */ } /* 147 */ j += i; /* */ } /* */ /* */ } /* */ /* 152 */ localIntegerComponentRaster.markDirty(); /* */ }
/** * This function 'fixes' the source's color model. Right now it just selects if it should have one * or two bands based on if the source had an alpha channel. */ protected static ColorModel fixColorModel(CachableRed src) { ColorModel cm = src.getColorModel(); if (cm != null) { if (cm.hasAlpha()) return GraphicsUtil.Linear_sRGB_Unpre; return GraphicsUtil.Linear_sRGB; } else { // No ColorModel so try to make some intelligent // decisions based just on the number of bands... // 1 bands -> replicated into RGB // 2 bands -> Band 0 replicated into RGB & Band 1 -> alpha premult // 3 bands -> sRGB (not-linear?) // 4 bands -> sRGB premult (not-linear?) SampleModel sm = src.getSampleModel(); switch (sm.getNumBands()) { case 1: return GraphicsUtil.Linear_sRGB; case 2: return GraphicsUtil.Linear_sRGB_Unpre; case 3: return GraphicsUtil.Linear_sRGB; } return GraphicsUtil.Linear_sRGB_Unpre; } }
public BufferedImage filter(BufferedImage src, BufferedImage dst) { int width = src.getWidth(); int height = src.getHeight(); int type = src.getType(); WritableRaster srcRaster = src.getRaster(); originalSpace = new Rectangle(0, 0, width, height); transformedSpace = new Rectangle(0, 0, width, height); transformSpace(transformedSpace); if (dst == null) { ColorModel dstCM = src.getColorModel(); dst = new BufferedImage( dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null); } WritableRaster dstRaster = dst.getRaster(); int[] inPixels = getRGB(src, 0, 0, width, height, null); inPixels = filterPixels(width, height, inPixels, transformedSpace); setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels); return dst; }
/** * Construct a fully transparent image <tt>bounds</tt> size, will paint one tile with paint. Thus * paint should not be a pattered paint or gradient but should be a solid color. * * @param bounds the bounds of the image (in fact will respond with any request). */ public FloodRed(Rectangle bounds, Paint paint) { super(); // We _must_ call init... ColorModel cm = GraphicsUtil.sRGB_Unpre; int defSz = AbstractTiledRed.getDefaultTileSize(); int tw = bounds.width; if (tw > defSz) tw = defSz; int th = bounds.height; if (th > defSz) th = defSz; // fix my sample model so it makes sense given my size. SampleModel sm = cm.createCompatibleSampleModel(tw, th); // Finish initializing our base class... init((CachableRed) null, bounds, cm, sm, 0, 0, null); raster = Raster.createWritableRaster(sm, new Point(0, 0)); BufferedImage offScreen = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); Graphics2D g = GraphicsUtil.createGraphics(offScreen); g.setPaint(paint); g.fillRect(0, 0, bounds.width, bounds.height); g.dispose(); }
protected static ColorModel fixColorModel(CachableRed src) { ColorModel cm = src.getColorModel(); int b = src.getSampleModel().getNumBands(); int[] masks = new int[4]; switch (b) { case 1: masks[0] = 0xFF; break; case 2: masks[0] = 0x00FF; masks[3] = 0xFF00; break; case 3: masks[0] = 0xFF0000; masks[1] = 0x00FF00; masks[2] = 0x0000FF; break; case 4: masks[0] = 0x00FF0000; masks[1] = 0x0000FF00; masks[2] = 0x000000FF; masks[3] = 0xFF000000; break; default: throw new IllegalArgumentException( "GaussianBlurRed8Bit only supports one to four band images"); } ColorSpace cs = cm.getColorSpace(); return new DirectColorModel( cs, 8 * b, masks[0], masks[1], masks[2], masks[3], true, DataBuffer.TYPE_INT); }
private static byte[] getIndexedRGBPixels(BufferedImage img) { byte[] indices = ((java.awt.image.DataBufferByte) img.getRaster().getDataBuffer()).getData(); int numOfPixels = indices.length; ColorModel model = img.getColorModel(); byte[] rgbs = null; if (model.hasAlpha()) { rgbs = new byte[numOfPixels * 4]; int i = 0; for (int j = 0; j < numOfPixels; j++) { int argb = model.getRGB(indices[j]); rgbs[i++] = (byte) ((argb >> 24) & 0xff); rgbs[i++] = (byte) ((argb) & 0xff); rgbs[i++] = (byte) ((argb >> 8) & 0xff); rgbs[i++] = (byte) ((argb >> 16) & 0xff); } } else { rgbs = new byte[numOfPixels * 3]; int i = 0; for (int j = 0; j < numOfPixels; j++) { int argb = model.getRGB(indices[j]); rgbs[i++] = (byte) ((argb) & 0xff); rgbs[i++] = (byte) ((argb >> 8) & 0xff); rgbs[i++] = (byte) ((argb >> 16) & 0xff); } } return rgbs; }
void createBufferedImage() { // REMIND: Be careful! Is this called everytime there is a // startProduction? We only want to call it if it is new or // there is an error isDefaultBI = false; try { biRaster = cmodel.createCompatibleWritableRaster(width, height); bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null); } catch (Exception e) { // Create a default image cmodel = ColorModel.getRGBdefault(); biRaster = cmodel.createCompatibleWritableRaster(width, height); bimage = createImage(cmodel, biRaster, false, null); } int type = bimage.getType(); if ((cmodel == ColorModel.getRGBdefault()) || (type == BufferedImage.TYPE_INT_RGB) || (type == BufferedImage.TYPE_INT_ARGB_PRE)) { isDefaultBI = true; } else if (cmodel instanceof DirectColorModel) { DirectColorModel dcm = (DirectColorModel) cmodel; if (dcm.getRedMask() == 0xff0000 && dcm.getGreenMask() == 0xff00 && dcm.getBlueMask() == 0xff) { isDefaultBI = true; } } }
public void testCreateBanded() { int[] bankIndices = new int[] {0, 1, 2}; int[] bandOffsets = new int[] {1, 1, 1}; ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); int dataType = DataBuffer.TYPE_BYTE; boolean hasAlpha = false; ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createBanded( colorSpace, bankIndices, bandOffsets, dataType, hasAlpha, false); ColorModel colorModel = typeSpecifier.getColorModel(); assertEquals( "Failed to create with the correct colorspace type", ColorSpace.TYPE_RGB, colorModel.getColorSpace().getType()); assertEquals( "Failed to create with the correct transparency", Transparency.OPAQUE, colorModel.getTransparency()); assertEquals( "Failed to create with the correcttransfer type", DataBuffer.TYPE_BYTE, colorModel.getTransferType()); BandedSampleModel sampleModel = (BandedSampleModel) typeSpecifier.getSampleModel(); assertArrayEquals( "Failed to create with the correct bankIndices", bankIndices, sampleModel.getBankIndices()); assertArrayEquals( "Failed to create with the correct bankIndices", bandOffsets, sampleModel.getBandOffsets()); }
/** * This function 'fixes' the source's sample model. Right now it just selects if it should have 3 * or 4 bands based on if the source had an alpha channel. */ protected static SampleModel fixSampleModel(CachableRed src) { SampleModel sm = src.getSampleModel(); ColorModel cm = src.getColorModel(); int width = sm.getWidth(); int height = sm.getHeight(); boolean alpha = false; if (cm != null) alpha = cm.hasAlpha(); else { switch (sm.getNumBands()) { case 1: case 3: alpha = false; break; default: alpha = true; break; } } if (alpha) return new SinglePixelPackedSampleModel( DataBuffer.TYPE_INT, sm.getWidth(), sm.getHeight(), new int[] {0xFF0000, 0xFF00, 0xFF, 0xFF000000}); else return new SinglePixelPackedSampleModel( DataBuffer.TYPE_INT, sm.getWidth(), sm.getHeight(), new int[] {0xFF0000, 0xFF00, 0xFF}); }
protected static ColorModel fixColorModel(CachableRed src) { ColorModel cm = src.getColorModel(); if (cm.hasAlpha()) { if (!cm.isAlphaPremultiplied()) cm = GraphicsUtil.coerceColorModel(cm, true); return cm; } ColorSpace cs = cm.getColorSpace(); int b = src.getSampleModel().getNumBands() + 1; if (b == 4) { int[] masks = new int[4]; for (int i = 0; i < b - 1; i++) masks[i] = 0xFF0000 >> (8 * i); masks[3] = 0xFF << (8 * (b - 1)); return new DirectColorModel( cs, 8 * b, masks[0], masks[1], masks[2], masks[3], true, DataBuffer.TYPE_INT); } int[] bits = new int[b]; for (int i = 0; i < b; i++) bits[i] = 8; return new ComponentColorModel( cs, bits, true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_INT); }
@Override public String getName(GridCoverage2DReader reader, Map<String, String> map) { ImageLayout layout; String coverageName = null; try { layout = reader.getImageLayout(); ColorModel cm = layout.getColorModel(null); ColorSpace cs = cm.getColorSpace(); int type = cs.getType(); switch (type) { case ColorSpace.TYPE_GRAY: coverageName = GRAY; break; case ColorSpace.TYPE_RGB: coverageName = RGB; break; default: throw new IllegalArgumentException( "The specified ColorSpace's type is not supported: " + type); } } catch (IOException e) { throw new IllegalArgumentException(e); } return coverageName; }
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) { if (dstCM == null) dstCM = src.getColorModel(); return new BufferedImage( dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null); }
/** * Returns a new <code>BufferedImage</code> using the same color model as the image passed as a * parameter. The returned image is only compatible with the image passed as a parameter. This * does not mean the returned image is compatible with the hardware. * * @param image the reference image from which the color model of the new image is obtained * @return a new <code>BufferedImage</code>, compatible with the color model of <code>image</code> */ public static BufferedImage createColorModelCompatibleImage(BufferedImage image) { ColorModel cm = image.getColorModel(); return new BufferedImage( cm, cm.createCompatibleWritableRaster(image.getWidth(), image.getHeight()), cm.isAlphaPremultiplied(), null); }
public static ColorModel getInternedColorModel(ColorModel cm) { if (xrgbmodel == cm || xrgbmodel.equals(cm)) { return xrgbmodel; } if (argbmodel == cm || argbmodel.equals(cm)) { return argbmodel; } return cm; }
/** * Create a new ColorModel with it's alpha premultiplied state matching newAlphaPreMult. * * @param cm The ColorModel to change the alpha premult state of. * @param newAlphaPreMult The new state of alpha premult. * @return A new colorModel that has isAlphaPremultiplied() equal to newAlphaPreMult. */ public static ColorModel coerceColorModel(ColorModel cm, boolean newAlphaPreMult) { if (cm.isAlphaPremultiplied() == newAlphaPreMult) return cm; // Easiest way to build proper colormodel for new Alpha state... // Eventually this should switch on known ColorModel types and // only fall back on this hack when the CM type is unknown. WritableRaster wr = cm.createCompatibleWritableRaster(1, 1); return cm.coerceData(wr, newAlphaPreMult); }
private boolean needToCreateIndex(RenderedImage image) { SampleModel sampleModel = image.getSampleModel(); ColorModel colorModel = image.getColorModel(); return sampleModel.getNumBands() != 1 || sampleModel.getSampleSize()[0] > 8 || colorModel.getComponentSize()[0] > 8; }
/* (non-Javadoc) * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage) */ public BufferedImage filter(BufferedImage src, BufferedImage dst) { if (src == null) { throw new NullPointerException("src image is null"); } if (src == dst) { throw new IllegalArgumentException("src image cannot be the " + "same as the dst image"); } boolean needToConvert = false; ColorModel srcCM = src.getColorModel(); ColorModel dstCM; BufferedImage origDst = dst; if (srcCM instanceof IndexColorModel) { IndexColorModel icm = (IndexColorModel) srcCM; src = icm.convertToIntDiscrete(src.getRaster(), false); srcCM = src.getColorModel(); } if (dst == null) { dst = createCompatibleDestImage(src, null); dstCM = srcCM; origDst = dst; } else { dstCM = dst.getColorModel(); if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) { needToConvert = true; dst = createCompatibleDestImage(src, null); dstCM = dst.getColorModel(); } else if (dstCM instanceof IndexColorModel) { dst = createCompatibleDestImage(src, null); dstCM = dst.getColorModel(); } } java.awt.image.CropImageFilter cropfilter = new java.awt.image.CropImageFilter(region.x, region.y, region.width, region.height); Image returnImage = Toolkit.getDefaultToolkit() .createImage(new java.awt.image.FilteredImageSource(src.getSource(), cropfilter)); dst = ImageConverter.convertImage(returnImage); origDst = dst; if (needToConvert) { ColorConvertOp ccop = new ColorConvertOp(hints); ccop.filter(dst, origDst); } else if (origDst != dst) { java.awt.Graphics2D g2 = origDst.createGraphics(); try { g2.drawImage(dst, 0, 0, null); } finally { g2.dispose(); } } return origDst; }
/** * Converts an SWT ImageData to an AWT BufferedImage. * * @param bufferedImage * @return */ @Override public BufferedImage getBufferedImage(ImageData data) { ColorModel colorModel = null; PaletteData palette = data.palette; if (palette.isDirect) { colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask); BufferedImage bufferedImage = new BufferedImage( colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x, y); RGB rgb = palette.getRGB(pixel); bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue); } } return bufferedImage; } else { RGB[] rgbs = palette.getRGBs(); byte[] red = new byte[rgbs.length]; byte[] green = new byte[rgbs.length]; byte[] blue = new byte[rgbs.length]; for (int i = 0; i < rgbs.length; i++) { RGB rgb = rgbs[i]; red[i] = (byte) rgb.red; green[i] = (byte) rgb.green; blue[i] = (byte) rgb.blue; } if (data.transparentPixel != -1) { colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel); } else { colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue); } BufferedImage bufferedImage = new BufferedImage( colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[1]; for (int y = 0; y < data.height; y++) { for (int x = 0; x < data.width; x++) { int pixel = data.getPixel(x, y); pixelArray[0] = pixel; raster.setPixel(x, y, pixelArray); } } return bufferedImage; } }
public void testCreateGrayscale() { // create a 8-bit grayscale ImageTypeSpecifier ImageTypeSpecifier type = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, true); ColorModel model = type.getColorModel(); assertEquals( "Failed to return the colorspace type", ColorSpace.TYPE_GRAY, model.getColorSpace().getType()); assertEquals("Failed to return the transparency", Transparency.OPAQUE, model.getTransparency()); assertEquals( "Failed to return the transfer type", DataBuffer.TYPE_BYTE, model.getTransferType()); assertEquals("Failed to return the pixel size", 8, model.getPixelSize()); // create a 16-bit grayscale AlphaPremultiplied ImageTypeSpecifier type = ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, true, false); model = type.getColorModel(); assertEquals( "Failed to return the colorspace type", ColorSpace.TYPE_GRAY, model.getColorSpace().getType()); assertEquals( "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency()); assertEquals( "Failed to return the transfer type", DataBuffer.TYPE_USHORT, model.getTransferType()); assertEquals("Failed to return the pixel size", 32, model.getPixelSize()); }
public int filterRGB(int x, int y, int rgb) { int R, G, B, color; ColorModel cm = ColorModel.getRGBdefault(); if (x == -1) ; R = cm.getRed(rgb) & 0xff; G = cm.getGreen(rgb) & 0xff; B = cm.getBlue(rgb) & 0xff; if (R >= 100 && G >= 100 && B >= 100) color = 0xffffffff; else color = 0xff000000; return color; }
public vc_CompositeContextMisc( int rule, float alpha, ColorModel srcColorModel, ColorModel dstColorModel) { this.rule = rule; this.alpha = alpha; this.srcColorModel = srcColorModel; this.dstColorModel = dstColorModel; this.srcColorSpace = srcColorModel.getColorSpace(); this.dstColorSpace = dstColorModel.getColorSpace(); ColorModel srgbCM = ColorModel.getRGBdefault(); // srcNeedsConverting = !srcColorModel.equals(srgbCM); // dstNeedsConverting = !dstColorModel.equals(srgbCM); }
/** * Returns a <code>BufferedImage</code> that supports the specified transparency and has a data * layout and color model compatible with this <code>GraphicsConfiguration</code>. This method has * nothing to do with memory-mapping a device. The returned <code>BufferedImage</code> has a * layout and color model that can be optimally blitted to a device with this <code> * GraphicsConfiguration</code>. * * @param width the width of the returned <code>BufferedImage</code> * @param height the height of the returned <code>BufferedImage</code> * @param transparency the specified transparency mode * @return a <code>BufferedImage</code> whose data layout and color model is compatible with this * <code>GraphicsConfiguration</code> and also supports the specified transparency. * @throws IllegalArgumentException if the transparency is not a valid value * @see Transparency#OPAQUE * @see Transparency#BITMASK * @see Transparency#TRANSLUCENT */ public BufferedImage createCompatibleImage(int width, int height, int transparency) { if (getColorModel().getTransparency() == transparency) { return createCompatibleImage(width, height); } ColorModel cm = getColorModel(transparency); if (cm == null) { throw new IllegalArgumentException("Unknown transparency: " + transparency); } WritableRaster wr = cm.createCompatibleWritableRaster(width, height); return new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null); }
/** * Creates a buffered image from a raster in the color space specified by the given ICC_Profile. * * @param raster A raster. * @param profile An ICC_Profile specifying the color space of the raster. * @return a BufferedImage in the color space specified by the profile. */ public static BufferedImage createImageFromICCProfile(Raster raster, ICC_Profile profile) { ICC_ColorSpace cs = new ICC_ColorSpace(profile); WritableRaster r = (WritableRaster) raster; ColorModel cm; if (raster.getSampleModel() instanceof PixelInterleavedSampleModel) { cm = new ComponentColorModel(cs, false, false, ColorModel.OPAQUE, raster.getTransferType()); } else { cm = new ICCPackedColorModel(cs, raster); } return new BufferedImage(cm, (WritableRaster) raster, cm.isAlphaPremultiplied(), null); }
/** * Constructs a <code>ChannelDefinitionBox</code> based on the provided <code>ColorModel</code>. */ public ChannelDefinitionBox(ColorModel colorModel) { super(computeLength(colorModel), BOX_TYPE, null); // creates the buffers for the channel definitions. short length = (short) (colorModel.getComponentSize().length - 1); num = (short) (length * (colorModel.isAlphaPremultiplied() ? 3 : 2)); channels = new short[num]; types = new int[num]; associations = new short[num]; // fills the arrays. fillBasedOnBands(length, colorModel.isAlphaPremultiplied(), channels, types, associations); }
public static BufferedImage createImageFromRGB(Raster rgbRaster, ICC_Profile rgbProfile) { if (rgbProfile != null) { return createImageFromICCProfile(rgbRaster, rgbProfile); } else { BufferedImage image; int w = rgbRaster.getWidth(); int h = rgbRaster.getHeight(); try { CompletableFuture<int[]> cfR = CompletableFuture.supplyAsync(() -> rgbRaster.getSamples(0, 0, w, h, 0, (int[]) null)); CompletableFuture<int[]> cfG = CompletableFuture.supplyAsync(() -> rgbRaster.getSamples(0, 0, w, h, 1, (int[]) null)); CompletableFuture<int[]> cfB = CompletableFuture.supplyAsync(() -> rgbRaster.getSamples(0, 0, w, h, 2, (int[]) null)); int[] rgb = new int[w * h]; int[] R = cfR.get(); int[] G = cfG.get(); int[] B = cfB.get(); // Split the rgb array into bands and process each band in parallel. // for (int i=0;i<rgb.length;i++) { RangeStream.range(0, rgb.length) .parallel() .forEach( (lo, hi) -> { for (int i = lo; i < hi; i++) { rgb[i] = 0xff << 24 | R[i] << 16 | G[i] << 8 | B[i]; } }); WritableRaster packedRaster = Raster.createPackedRaster( new DataBufferInt(rgb, rgb.length), w, h, w, new int[] {0xff0000, 0xff00, 0xff, 0xff000000}, null); ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorModel cm = ColorModel .getRGBdefault(); // new DirectColorModel(cs, 32, 0xff0000, 0xff00, 0xff, // 0x0ff000000, false, DataBuffer.TYPE_INT); Hashtable<Object, Object> properties = new Hashtable<Object, Object>(); return new BufferedImage(cm, packedRaster, cm.isAlphaPremultiplied(), properties); } catch (ExecutionException | InterruptedException e) { throw new InternalError(e); } } }