List of usage examples for java.awt.image ColorModel createCompatibleWritableRaster
public WritableRaster createCompatibleWritableRaster(int w, int h)
From source file:ImageUtil.java
/** * create new image from source image/*from w w w .j a v a 2 s .c o m*/ * @param srcImg source image * @param targetWidth target image width * @param targetHeight target image height * @return new image with specify width and height */ public static BufferedImage createNewImage(BufferedImage srcImg, int targetWidth, int targetHeight) { BufferedImage targetImg = null; int type = srcImg.getType(); if (type == BufferedImage.TYPE_CUSTOM) { ColorModel cm = srcImg.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetWidth, targetHeight); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); targetImg = new BufferedImage(cm, raster, alphaPremultiplied, null); } else { targetImg = new BufferedImage(targetWidth, targetHeight, type); } return targetImg; }
From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java
/** * ?/*from w w w. jav a 2 s . c o m*/ * * @param source * @param targetW * @param targetH * @param ifScaling ? * @return */ public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) { // targetWtargetH int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); // targetWtargetH??,?if else??? if (ifScaling) { if (sx > sy) { sx = sy; targetW = (int) (sx * source.getWidth()); } else { sy = sx; targetH = (int) (sy * source.getHeight()); } } if (type == BufferedImage.TYPE_CUSTOM) { // handmade ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, alphaPremultiplied, null); } else target = new BufferedImage(targetW, targetH, type); Graphics2D g = target.createGraphics(); // smoother than exlax: g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; }
From source file:org.eclipse.swt.snippets.Snippet156.java
static BufferedImage convertToAWT(ImageData data) { ColorModel colorModel = null; PaletteData palette = data.palette;/* ww w .ja va 2 s. co m*/ 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; } }
From source file:Snippet156.java
static BufferedImage convertToAWT(ImageData data) { ColorModel colorModel = null; PaletteData palette = data.palette;//from w w w.j ava2 s.c o m 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); WritableRaster raster = bufferedImage.getRaster(); int[] pixelArray = new int[3]; 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); pixelArray[0] = rgb.red; pixelArray[1] = rgb.green; pixelArray[2] = rgb.blue; raster.setPixels(x, y, 1, 1, pixelArray); } } 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; } }
From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java
/** * Convert a SWT Image to a {@link BufferedImage} * //from w ww .j ava 2s . c o m * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"} * * @param data the SWT {@link ImageData} * @param applyAlphaMask true if the image data's alpha mask should be * applied to the result image (if there is any). This method * calls {@link #applyTransparencyMask(BufferedImage, ImageData)} * for that purpose. * @return the AWT {@link BufferedImage} */ public static BufferedImage convertToAWT(ImageData data, boolean applyAlphaMask) { ColorModel colorModel = null; PaletteData palette = data.palette; BufferedImage result; 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); } } result = 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); } } result = bufferedImage; } if (data.getTransparencyType() == SWT.TRANSPARENCY_MASK && applyAlphaMask) { result = applyTransparencyMask(result, data.getTransparencyMask()); } return result; }
From source file:GraphicsUtil.java
/** * Constructs a BufferedImage with a linear sRGB colorModel, and alpha. * @param width The desired width of the BufferedImage * @param height The desired height of the BufferedImage * @param premult The desired state of alpha premultiplied * @return The requested BufferedImage. *//*from w w w . ja v a 2s . c o m*/ public static BufferedImage makeLinearBufferedImage(int width, int height, boolean premult) { ColorModel cm = makeLinear_sRGBCM(premult); WritableRaster wr = cm.createCompatibleWritableRaster(width, height); return new BufferedImage(cm, wr, premult, null); }
From source file:GraphicsUtil.java
/** * Create a new ColorModel with it's alpha premultiplied state matching * newAlphaPreMult./*from w w w . j a v a 2s . c o m*/ * @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); }
From source file:ImageOpByRomain.java
/** * <p>// w w w. ja v a 2 s .c om * 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. * </p> * * @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); }
From source file:com.dianping.imcaptcha.strategy.WaveFilter.java
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);/*from w ww. j a v a 2s.c o m*/ 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); if (interpolation == NEAREST_NEIGHBOUR) return filterPixelsNN(dst, width, height, inPixels, transformedSpace); int srcWidth = width; int srcHeight = height; int srcWidth1 = width - 1; int srcHeight1 = height - 1; int outWidth = transformedSpace.width; int outHeight = transformedSpace.height; int outX, outY; int index = 0; int[] outPixels = new int[outWidth]; float radius = srcHeight * 1.0f / 2 / (float) Math.PI; outX = transformedSpace.x; outY = transformedSpace.y; float[] out = new float[2]; for (int y = 0; y < outHeight; y++) { for (int x = 0; x < outWidth; x++) { transformInverse(outX + x, outY + y, out, radius); int srcX = (int) Math.floor(out[0]); int srcY = (int) Math.floor(out[1]); float xWeight = out[0] - srcX; float yWeight = out[1] - srcY; int nw, ne, sw, se; if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) { // Easy case, all corners are in the image int i = srcWidth * srcY + srcX; nw = inPixels[i]; ne = inPixels[i + 1]; sw = inPixels[i + srcWidth]; se = inPixels[i + srcWidth + 1]; } else { // Some of the corners are off the image nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight); ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight); sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight); se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight); } outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se); } setRGB(dst, 0, y, transformedSpace.width, 1, outPixels); } return dst; }
From source file:com.github.dactiv.fear.service.service.account.AccountService.java
/** * ??/*from w ww. j a va 2 s . c o m*/ * * @param sourcePath ? * @param targetPath ?? * @param portraitSize ? * * @throws IOException */ private String scaleImage(String sourcePath, String targetPath, PortraitSize portraitSize) throws IOException { InputStream inputStream = new FileInputStream(sourcePath); BufferedImage source = ImageIO.read(inputStream); ColorModel targetColorModel = source.getColorModel(); inputStream.close(); int width = portraitSize.getWidth(); int height = portraitSize.getHeight(); BufferedImage target = new BufferedImage(targetColorModel, targetColorModel.createCompatibleWritableRaster(width, height), targetColorModel.isAlphaPremultiplied(), null); Image scaleImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH); Graphics2D g = target.createGraphics(); g.drawImage(scaleImage, 0, 0, width, height, null); g.dispose(); String result = targetPath + portraitSize.getName(); FileOutputStream fileOutputStream = new FileOutputStream(result); ImageIO.write(target, PORTRAIT_PIC_TYPE, fileOutputStream); fileOutputStream.close(); return result; }