public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM) { if (destCM == null) { destCM = src.getColorModel(); if (destCM instanceof IndexColorModel) { destCM = ColorModel.getRGBdefault(); } } return new BufferedImage(destCM, destCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), destCM.isAlphaPremultiplied(), null); }
if (!crop && img.getWidth() > width) widthToUse = img.getWidth(); if (!crop && img.getHeight() > height) heightToUse = img.getHeight(); Graphics g = finalImage.getGraphics(); if (fillColor == null && !img.getColorModel().hasAlpha())
ColorModel srcCM = src.getColorModel(); ColorModel dstCM; BufferedImage origDst = dst; src = icm.convertToIntDiscrete(src.getRaster(), false); srcCM = src.getColorModel(); dstCM = dst.getColorModel(); if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) java.awt.Graphics2D g2 = origDst.createGraphics(); try { g2.drawImage(dst, 0, 0, null); } finally { g2.dispose();
private boolean hasAlphaBand(ColorModel cm) { // num components returns the alpha, num _color_ components does not return (cm.getNumComponents() == 2 && cm.getNumColorComponents() == 1) /* gray-alpha case */ || (cm.getNumComponents() == 4 && cm.getNumColorComponents() == 3) /* rgba case */; }
public static int getAlpha(int pixel) { ColorModel cm = ColorModel.getRGBdefault(); return cm.getAlpha(pixel); }
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM, int width, int height) { BufferedImage image; if (destCM == null) { destCM = src.getColorModel(); // Not much support for ICM if (destCM instanceof IndexColorModel) { destCM = ColorModel.getRGBdefault(); } } image = new BufferedImage (destCM, destCM.createCompatibleWritableRaster(width, height), destCM.isAlphaPremultiplied(), null); return image; }
void createRaster() { ColorModel cm = getColorModel(); raster = cm.createCompatibleWritableRaster((int)deviceBounds.getWidth(), (int)deviceBounds.getHeight()); BufferedImage img = new BufferedImage(cm, raster, false, null); Graphics2D graphics = img.createGraphics(); graphics.setRenderingHints(hints); graphics.translate(-deviceBounds.getX(), -deviceBounds.getY()); graphics.transform(xform); Raster img2 = pCtx.getRaster(0, 0, gradientSteps, 1); int[] rgb = new int[cm.getNumComponents()]; for (int i = gradientSteps-1; i>=0; i--) { img2.getPixel(i, 0, rgb); Color c = new Color(rgb[0],rgb[1],rgb[2]); if (rgb.length == 4) { // it doesn't work to use just a color with transparency ... graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, rgb[3]/255.0f)); } graphics.setStroke(new BasicStroke(i+1, capStyle, joinStyle)); graphics.setColor(c); graphics.draw(shape); } graphics.dispose(); } }
ColorModel srcCM = src.getColorModel(); ColorModel dstCM; BufferedImage origDst = dst; src = icm.convertToIntDiscrete(src.getRaster(), false); srcCM = src.getColorModel(); } else { dstCM = dst.getColorModel(); if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) Graphics2D g = dst.createGraphics(); g.translate((neww-w)/2, (newh-h)/2); g.rotate(Math.toRadians(amount), w/2, h/2); g.drawRenderedImage(src, null); g.dispose(); origDst = dst;
public Rect (BufferedImage source, int left, int top, int newWidth, int newHeight, boolean isPatch) { image = new BufferedImage(source.getColorModel(), source.getRaster().createWritableChild(left, top, newWidth, newHeight, 0, 0, null), source.getColorModel().isAlphaPremultiplied(), null); offsetX = left; offsetY = top; regionWidth = newWidth; regionHeight = newHeight; originalWidth = source.getWidth(); originalHeight = source.getHeight(); width = newWidth; height = newHeight; this.isPatch = isPatch; }
private void extractAndSetBackgroundColor(final BufferedImage pImage) { // TODO: bgColor request attribute instead of parameter? if (pImage.getColorModel().hasAlpha()) { String bgColor = originalRequest.getParameter("bg.color"); if (bgColor != null) { Color color = StringUtil.toColor(bgColor); Graphics2D g = pImage.createGraphics(); try { g.setColor(color); g.setComposite(AlphaComposite.DstOver); g.fillRect(0, 0, pImage.getWidth(), pImage.getHeight()); } finally { g.dispose(); } } } }
@Test public void testApplyClippingPath() throws IOException { BufferedImage source = new BufferedImage(20, 20, BufferedImage.TYPE_3BYTE_BGR); Path2D path = readExpectedPath("/ser/grape-path.ser"); BufferedImage image = Paths.applyClippingPath(path, source); assertNotNull(image); // Same dimensions as original assertEquals(source.getWidth(), image.getWidth()); assertEquals(source.getHeight(), image.getHeight()); // Transparent assertTrue(image.getColorModel().getTransparency() == Transparency.TRANSLUCENT); // Corners (at least) should be transparent assertEquals(0, image.getRGB(0, 0)); assertEquals(0, image.getRGB(source.getWidth() - 1, 0)); assertEquals(0, image.getRGB(0, source.getHeight() - 1)); assertEquals(0, image.getRGB(source.getWidth() - 1, source.getHeight() - 1)); // Center opaque assertEquals(0xff, image.getRGB(source.getWidth() / 2, source.getHeight() / 2) >>> 24); // TODO: Mor sophisticated test that tests all pixels outside path... }
@Override 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; }
public static BufferedImage convertImage(Image original) { ColorModel cm = ColorModel.getRGBdefault(); int width = original.getWidth(null); int height = original.getHeight(null); BufferedImage image = new BufferedImage (cm,cm.createCompatibleWritableRaster(width, height),cm.isAlphaPremultiplied(), null); Graphics2D bg = image.createGraphics(); bg.drawImage(original, 0, 0, width, height, null); bg.dispose(); return image; } }
@Override public void extract(BufferedImage image) { histogram = new double[18]; double[] directionality; ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), ColorSpace.getInstance(ColorSpace.CS_GRAY), new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY)); BufferedImage bimg = op.filter(image, null); bimg = ImageUtils.scaleImage(bimg, MAX_IMG_HEIGHT); Raster raster = bimg.getRaster(); int[] tmp = new int[3]; this.grayScales = new int[raster.getWidth()][raster.getHeight()]; for (int i = 0; i < raster.getWidth(); i++) { for (int j = 0; j < raster.getHeight(); j++) { raster.getPixel(i, j, tmp); this.grayScales[i][j] = tmp[0]; } } imgWidth = bimg.getWidth(); imgHeight = bimg.getHeight(); histogram[0] = this.coarseness(bimg.getWidth(), bimg.getHeight()); histogram[1] = this.contrast(); directionality = this.directionality(); for (int i = 2; i < histogram.length; i++) { histogram[i] = directionality[i - 2]; } }
ColorModel srcCM = src.getColorModel(); ColorModel dstCM; BufferedImage origDst = dst; src = icm.convertToIntDiscrete(src.getRaster(), false); srcCM = src.getColorModel(); dstCM = dst.getColorModel(); if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) java.awt.Graphics2D g2 = origDst.createGraphics(); try { g2.drawImage(dst, 0, 0, null); } finally { g2.dispose();
this.componentsPerPixel = image.getColorModel().getNumComponents(); this.transferType = image.getRaster().getTransferType(); this.bytesPerComponent = (transferType == DataBuffer.TYPE_SHORT || transferType == DataBuffer.TYPE_USHORT) ? 2 : 1; this.bytesPerPixel = image.getColorModel().getNumColorComponents() * bytesPerComponent; this.height = image.getHeight(); this.width = image.getWidth(); this.imageType = image.getType(); this.hasAlpha = image.getColorModel().getNumComponents() != image.getColorModel() .getNumColorComponents(); this.alphaImageData = hasAlpha ? new byte[width * height * bytesPerComponent] : null;
type = BufferedImage.TYPE_INT_ARGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); Graphics g = bimage.createGraphics(); return bimage.getColorModel().hasAlpha(); return cm.hasAlpha();
public BufferedImage filter(BufferedImage pSource, BufferedImage pDestination) { if (pSource == null) { throw new NullPointerException("source image is null"); } if (pSource == pDestination) { throw new IllegalArgumentException("source image cannot be the same as the destination image"); } int borderX = kernel.getWidth() / 2; int borderY = kernel.getHeight() / 2; BufferedImage original = addBorder(pSource, borderX, borderY); // Workaround for what seems to be a Java2D bug: // ConvolveOp needs explicit destination image type for some "uncommon" // image types. However, TYPE_3BYTE_BGR is what javax.imageio.ImageIO // normally returns for color JPEGs... :-/ BufferedImage destination = pDestination; if (original.getType() == BufferedImage.TYPE_3BYTE_BGR) { destination = ImageUtil.createBuffered( pSource.getWidth(), pSource.getHeight(), pSource.getType(), pSource.getColorModel().getTransparency(), null ); } // Do the filtering (if destination is null, a new image will be created) destination = convolve.filter(original, destination); if (pSource != original) { // Remove the border destination = destination.getSubimage(borderX, borderY, pSource.getWidth(), pSource.getHeight()); } return destination; }
private static BufferedImage deepCopy(BufferedImage bi) { final ColorModel cm = bi.getColorModel(); final boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); final WritableRaster raster = bi.copyData(bi.getRaster().createCompatibleWritableRaster()); return new BufferedImage(cm, raster, isAlphaPremultiplied, null); } }
WritableRaster output = getColorModel().createCompatibleWritableRaster(w, h); pixelInput = raster.getDataElements(x, y, pixelInput); pixelOutput[0] = rasterCM.getRed(pixelInput); pixelOutput[1] = rasterCM.getGreen(pixelInput); pixelOutput[2] = rasterCM.getBlue(pixelInput); pixelOutput[3] = rasterCM.getAlpha(pixelInput); if (x1 + x >= 0 && y1 + y >= 0 && x1 + x < mask.getWidth() && y1 + y < mask.getHeight()) mask.getRaster().getPixel(x1 + x, y1 + y, gray); int g = gray[0]; if (transferFunction != null) output.setPixel(x, y, pixelOutput);