protected BufferedImage scalingIfNeed( BufferedImage image, int dstWidth, int dstHeight, boolean needAlpha) { if (dstHeight > 0 && dstWidth > 0 && (image.getHeight() != dstHeight || image.getWidth() != dstWidth)) { Image scaled = image.getScaledInstance(dstWidth, dstHeight, Image.SCALE_SMOOTH); if (needAlpha && image.getColorModel().hasAlpha()) { return toBufferedImage(scaled, BufferedImage.TYPE_4BYTE_ABGR); } else { if (image.getRaster().getNumDataElements() == 1) return toBufferedImage(scaled, BufferedImage.TYPE_BYTE_GRAY); else return toBufferedImage(scaled, BufferedImage.TYPE_3BYTE_BGR); } } else { if (image.getType() == BufferedImage.TYPE_4BYTE_ABGR || image.getType() == BufferedImage.TYPE_3BYTE_BGR) { return image; } else if (needAlpha && image.getColorModel().hasAlpha()) { return toBufferedImage(image, BufferedImage.TYPE_4BYTE_ABGR); } else { if (image.getRaster().getNumDataElements() == 1) return toBufferedImage(image, BufferedImage.TYPE_BYTE_GRAY); else return toBufferedImage(image, BufferedImage.TYPE_3BYTE_BGR); } } }
/** * 画像のリサイズ * * @param srcImage 元画像 * @param destWidth 横幅 * @param destHeight 高さ * @return */ public BufferedImage rescaleImage(BufferedImage srcImage, int dstWidth, int dstHeight) { BufferedImage dstImage = null; if (srcImage.getColorModel() instanceof IndexColorModel) { dstImage = new BufferedImage( dstWidth, dstHeight, srcImage.getType(), (IndexColorModel) srcImage.getColorModel()); } else { if (srcImage.getType() == 0) { dstImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_4BYTE_ABGR_PRE); } else { dstImage = new BufferedImage(dstWidth, dstHeight, srcImage.getType()); } } double x = (double) dstWidth / srcImage.getWidth(); double y = (double) dstHeight / srcImage.getHeight(); AffineTransform af = AffineTransform.getScaleInstance(x, y); if (dstImage.getColorModel().hasAlpha() && dstImage.getColorModel() instanceof IndexColorModel) { int pixel = ((IndexColorModel) dstImage.getColorModel()).getTransparentPixel(); for (int i = 0; i < dstImage.getWidth(); ++i) { for (int j = 0; j < dstImage.getHeight(); ++j) { dstImage.setRGB(i, j, pixel); } } } Graphics2D g2 = (Graphics2D) dstImage.createGraphics(); g2.drawImage(srcImage, af, null); g2.dispose(); return dstImage; }
public byte[] resize(BufferedImage imgSrc, String imgFormat, int targetHeight, int targetWidth) throws Exception { int height = imgSrc.getHeight(); int width = imgSrc.getWidth(); int filterWidth = getFilterWidth(); int filterHeight = getFilterHeight(); if (width <= filterWidth || height <= filterHeight) { return new byte[4]; } if (height > targetHeight || width > targetWidth) { float resizeMultiple; if ((float) height / targetHeight > (float) width / targetWidth) { resizeMultiple = (float) height / targetHeight; } else { resizeMultiple = (float) width / targetWidth; } targetHeight = (int) (height / resizeMultiple); targetWidth = (int) (width / resizeMultiple); } else { targetHeight = height; targetWidth = width; } Image image = imgSrc.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH); int imageType = (imgSrc.getType() == BufferedImage.TYPE_CUSTOM ? BufferedImage.TYPE_INT_RGB : imgSrc.getType()); return resize(imgFormat, targetHeight, targetWidth, image, imageType); }
/** Creates a new instance of IDEJRManFramebufferImpl */ public IDEJRManFramebufferImpl(String name, BufferedImage image) { super("JRMan rendered: " + name, true, true, true, true); this.name = name; save.setEnabled(false); imagePanel.setImage(image); imagePanel.addToolbarAction(save); if (image.getType() == BufferedImage.TYPE_INT_ARGB || image.getType() == BufferedImage.TYPE_INT_ARGB_PRE) { imagePanel.setShowTransparencyPattern(true); } getRootPane().setDoubleBuffered(false); getContentPane().add(imagePanel); pack(); ImageResource images = ImageResource.getInstance(); // set the frame icon setFrameIcon(images.getJrMan()); // add this to the IDE desktop MainMenuEventHandlers.getInstance(null) .getIdeInstance() .getWorkspaceDesktop() .addInternalFrame(this, true); }
public static Boolean resizeImage( InputStream inputImage, OutputStream outputImage, Integer Width, Integer Height) { BufferedImage origImage; try { origImage = ImageIO.read(inputImage); int type = origImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : origImage.getType(); // *Special* if the width or height is 0 use image src dimensions if (Width == 0) { Width = origImage.getWidth(); } if (Height == 0) { Height = origImage.getHeight(); } int fHeight = Height; int fWidth = Width; // Work out the resized width/height if (origImage.getHeight() > Height || origImage.getWidth() > Width) { fHeight = Height; int wid = Width; float sum = (float) origImage.getWidth() / (float) origImage.getHeight(); fWidth = Math.round(fHeight * sum); if (fWidth > wid) { // rezise again for the width this time fHeight = Math.round(wid / sum); fWidth = wid; } } BufferedImage resizedImage = new BufferedImage(fWidth, fHeight, type); Graphics2D g = resizedImage.createGraphics(); g.setComposite(AlphaComposite.Src); g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(origImage, 0, 0, fWidth, fHeight, null); g.dispose(); ImageIO.write(resizedImage, PHOTO_TYPE, outputImage); } catch (IOException e) { logger.error("IO error while resizing an image", e); return false; } return true; }
JPanelWithBackground(String path, int width, int height) { try { image = ImageIO.read(getClass().getClassLoader().getResource(path)); Type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType(); IMG_WIDTH = width; IMG_HEIGHT = height; image = resizeImage(image, Type); } catch (IOException ex) { // handle exception... } }
public void setImage(File image) { try { BufferedImage originalImage = ImageIO.read(image); int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); imageDisplay.add(resizeImageWithHint(originalImage, type)); } catch (IOException e) { JOptionPane.showMessageDialog( this, "De afbeelding kon niet worden ingeladen. Probeer opnieuw"); } catch (NullPointerException e) { JOptionPane.showMessageDialog(this, "Dit is geen geldig type. Probeer opnieuw"); } }
public void write(IIOMetadata streamMetadata, IIOImage img, ImageWriteParam param) throws IOException { ImageOutputStream out = (ImageOutputStream) getOutput(); if (!(img.getRenderedImage() instanceof BufferedImage)) { throw new IOException(getClass().getName() + "write:\nCan only write BufferedImage objects"); } BufferedImage image = (BufferedImage) img.getRenderedImage(); int width = image.getWidth(); int height = image.getHeight(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JFIFOutputStream os; if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) { // one component; grey scale os = new JFIFOutputStream(baos, false, height, width); // SOF:start of frame WritableRaster raster = image.getRaster(); DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer(); byte[] imgdata = (byte[]) buffer.getData(); os.write(imgdata); os.close(); // EOF: end of frame } else if (image.getType() == BufferedImage.TYPE_INT_RGB) { // three components; YCbCr os = new JFIFOutputStream(baos, true, height, width); // SOF:start of frame WritableRaster raster = image.getRaster(); DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer(); int[] imgdata = (int[]) buffer.getData(); os.write(imgdata); os.close(); // EOF: end of frame } else { // three components; YCbCr os = new JFIFOutputStream(baos, true, height, width); // SOF:start of frame for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { os.write(image.getRGB(x, y)); } } os.close(); // EOF: end of frame } out.write(baos.toByteArray()); // write to image stream } catch (Exception e) { e.printStackTrace(); throw new IOException( getClass().getName() + ".write:\n\tCould not write image due to :\n\t" + e.getMessage()); } }
public byte[] getThumbNail() { byte[] img = this.getImage(); byte[] out = null; ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); try { BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(img)); int w = bufferedImage.getWidth() / 10; int h = bufferedImage.getHeight() / 10; BufferedImage after = new BufferedImage(w, h, bufferedImage.getType()); AffineTransform at = new AffineTransform(); at.scale(0.1, 0.1); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); after = scaleOp.filter(bufferedImage, after); ImageIO.write(after, "jpg", byteArrayOut); out = byteArrayOut.toByteArray(); } catch (IOException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } return out; }
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 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; }
protected java.awt.image.BufferedImage extractSelectedPixels( java.awt.image.BufferedImage image, int[] deltaMatrix, int noDifferenceMarker) { int type = image.getType(); WritableRaster imageRaster = image.getRaster(); switch (type) { case java.awt.image.BufferedImage.TYPE_INT_ARGB: case java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE: case java.awt.image.BufferedImage.TYPE_4BYTE_ABGR: case java.awt.image.BufferedImage.TYPE_4BYTE_ABGR_PRE: imageRaster.setSamples(0, 0, imageWidth, imageHeight, 3, deltaMatrix); break; default: int vectorLength = imageWidth * imageHeight; int[] imagePixels = new int[vectorLength]; int bandNumber = imageRaster.getNumBands(); for (int k = 0; k < bandNumber; k++) { imagePixels = imageRaster.getSamples(0, 0, imageWidth, imageHeight, k, imagePixels); for (int n = 0; n < vectorLength; n++) { if (deltaMatrix[n] == noDifferenceMarker) { imagePixels[n] = spaceColor; } } ; imageRaster.setSamples(0, 0, imageWidth, imageHeight, k, imagePixels); } } ; return image; }
/** * returns the resized and cliped img. * * @param img * @param newW * @param newH * @return */ private static BufferedImage resizeclip(BufferedImage img, int newW, int newH) { int startx, width, starty, height; int w = img.getWidth(); int h = img.getHeight(); double sourceVer = 1.0 * h / w; double targetVer = 1.0 * newH / newW; if (sourceVer < targetVer) { starty = 0; height = h; startx = (int) (((1.0 * newH / h * w - newW) / 2) * 1.0 * h / newH); width = startx + (int) (newW * 1.0 * h / newH); } else { startx = 0; width = w; starty = (int) (((1.0 * newW / w * h - newH) / 2) * 1.0 * w / newW); height = starty + (int) (newH * 1.0 * w / newW); } BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); Graphics2D g = dimg.createGraphics(); g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(img, 0, 0, newW, newH, startx, starty, width, height, null); g.dispose(); return dimg; }
/** * Simple image flipper. Can flip image either vertically, horizontally or both directions * * @param source _ * @param flipType _ * @return BufferedImage * @throws NullPointerException _ * @throws IllegalArgumentException _ */ public static <T> BufferedImage flipImage(final T source, final FlipType flipType) throws NullPointerException, IllegalArgumentException, IOException { if (verifyNotNull(source, flipType)) { BufferedImage sourceImage = convertToBufferedImage(source); BufferedImage target = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), sourceImage.getType()); AffineTransform affineTransform; AffineTransformOp affineTransformOp; if (flipType.equals(FlipType.HORIZONTAL) || flipType.equals(FlipType.BOTH)) { affineTransform = AffineTransform.getScaleInstance(1, -1); affineTransform.translate(-sourceImage.getWidth(), 0); affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); target = affineTransformOp.filter(sourceImage, target); } if (flipType.equals(FlipType.VERTICAL) || flipType.equals(FlipType.BOTH)) { affineTransform = AffineTransform.getScaleInstance(1, -1); affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); affineTransform.translate(0, -sourceImage.getHeight()); target = affineTransformOp.filter(sourceImage, target); } return target; } throw new NullPointerException(E_OBJECT_WAS_NULL); }
public BufferedImage copyImage(BufferedImage source) { BufferedImage b = new BufferedImage(source.getWidth(), source.getHeight(), source.getType()); Graphics g = b.getGraphics(); g.drawImage(source, 0, 0, null); g.dispose(); return b; }
// Button Event Edited Image Zoom Out (-) private void jButton4ActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton4ActionPerformed zoomEdit = zoomEdit - 1; if (gambarEdit == null) { return; } if (zoomEdit > 0 && iEdit > 0) { iEdit = iEdit - 1; AffineTransform transform = new AffineTransform(); transform.setToScale(zoomEdit, zoomEdit); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); BufferedImage filteredImage = new BufferedImage( gambarEdit.getWidth() / 2, gambarEdit.getHeight() / 2, gambarEdit.getType()); op.filter(gambarEditZoomOut[iEdit], filteredImage); gambarEditZoomOut[iEdit] = filteredImage; gambarEdit = gambarEditZoomOut[iEdit]; jLabel2.setIcon(null); jLabel2.setIcon(new ImageIcon(gambarEditZoomOut[iEdit])); } else if (iEdit <= 0) { iEdit = 0; zoomEdit = 1; } } // GEN-LAST:event_jButton4ActionPerformed
public BufferedImage filter(BufferedImage src, BufferedImage dst) { int width = src.getWidth(); int height = src.getHeight(); int type = src.getType(); WritableRaster srcRaster = src.getRaster(); if (dst == null) dst = createCompatibleDestImage(src, null); WritableRaster dstRaster = dst.getRaster(); setDimensions(width, height); int[] inPixels = new int[width]; for (int y = 0; y < height; y++) { // We try to avoid calling getRGB on images as it causes them to become unmanaged, causing // horrible performance problems. if (type == BufferedImage.TYPE_INT_ARGB) { srcRaster.getDataElements(0, y, width, 1, inPixels); for (int x = 0; x < width; x++) inPixels[x] = filterRGB(x, y, inPixels[x]); dstRaster.setDataElements(0, y, width, 1, inPixels); } else { src.getRGB(0, y, width, 1, inPixels, 0, width); for (int x = 0; x < width; x++) inPixels[x] = filterRGB(x, y, inPixels[x]); dst.setRGB(0, y, width, 1, inPixels, 0, width); } } return dst; }
/** * Loads an image from a given URL into a BufferedImage. The image is returned in the format * defined by the imageType parameter. Note that this is special cased for JPEG images where * loading is performed outside the standard media tracker, for efficiency reasons. * * @param url URL where the image file is located. * @param imageType one of the image type defined in the BufferedImage class. * @return loaded image at path or url * @see java.awt.image.BufferedImage */ public static synchronized BufferedImage loadBufferedImage(URL url, int imageType) { BufferedImage image = null; // Special handling for JPEG images to avoid extra processing if possible. if (url == null || !url.toString().toLowerCase().endsWith(".jpg")) { Image tmpImage = loadImage(url); if (tmpImage != null) { image = new BufferedImage(tmpImage.getWidth(null), tmpImage.getHeight(null), imageType); Graphics2D g = image.createGraphics(); g.drawImage(tmpImage, 0, 0, null); g.dispose(); } } else { BufferedImage tmpImage = loadBufferedJPEGImage(url); if (tmpImage != null) { if (tmpImage.getType() != imageType) { log.config("Incompatible JPEG image type: creating new buffer image"); image = new BufferedImage(tmpImage.getWidth(null), tmpImage.getHeight(null), imageType); Graphics2D g = image.createGraphics(); g.drawImage(tmpImage, 0, 0, null); g.dispose(); } else image = tmpImage; } } return image; } // loadBufferedImage
public void testAcquireWhenNoneExists() throws Exception { BufferedImage image = pool.acquire(dimension); assertEquals(100, image.getWidth()); assertEquals(200, image.getHeight()); assertEquals(BufferedImage.TYPE_INT_ARGB, image.getType()); }
public RenderedImage scale(RenderedImage renderedImage, int width) { if (width <= 0) { return renderedImage; } int imageHeight = renderedImage.getHeight(); int imageWidth = renderedImage.getWidth(); double factor = (double) width / imageWidth; int scaledHeight = (int) (factor * imageHeight); int scaledWidth = width; BufferedImage bufferedImage = getBufferedImage(renderedImage); int type = bufferedImage.getType(); if (type == 0) { type = BufferedImage.TYPE_INT_ARGB; } BufferedImage scaledBufferedImage = new BufferedImage(scaledWidth, scaledHeight, type); Graphics graphics = scaledBufferedImage.getGraphics(); Image scaledImage = bufferedImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); graphics.drawImage(scaledImage, 0, 0, null); return scaledBufferedImage; }
public ArrayList<BufferedImage> Split(BufferedImage orgBilde) throws IOException { ArrayList<BufferedImage> bilder = new ArrayList<>(); int rader = 3; int kolonner = 2; int bildeBit = rader * kolonner; int splittetBredde = orgBilde.getWidth() / kolonner; int splittetHøyde = orgBilde.getHeight() / rader; int count = 0; BufferedImage imgs[] = new BufferedImage[bildeBit]; for (int x = 0; x < rader; x++) { for (int y = 0; y < kolonner; y++) { imgs[count] = new BufferedImage(splittetBredde, splittetHøyde, orgBilde.getType()); Graphics2D gr = imgs[count++].createGraphics(); gr.drawImage( orgBilde, 0, 0, splittetBredde, splittetHøyde, splittetBredde * y, splittetHøyde * x, splittetBredde * y + splittetBredde, splittetHøyde * x + splittetHøyde, null); gr.dispose(); } } bilder.addAll(Arrays.asList(imgs)); return bilder; }
/** * 伺か用PNA(アルファチャネルのグレースケール表現)に変換する. * * @param img 変換元の透過画像(TYPE_INT_ARGB専用) * @return 伺か用PNA画像(TYPE_INT_RGB, アルファチャネルのグレースケール表現) */ public BufferedImage createUkagakaPNA(BufferedImage img) { if (img == null) { throw new IllegalArgumentException("引数にnullは指定できません。"); } if (img.getType() != BufferedImage.TYPE_INT_ARGB) { throw new IllegalArgumentException("TYPE_INT_ARGB専用です."); } int w = img.getWidth(); int h = img.getHeight(); Raster raster = img.getData(); BufferedImage outimg = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); // アルファ値をグレースケール表現に変換 int[] argb = null; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { argb = raster.getPixel(x, y, argb); int a = argb[3]; // alpha int o = a << 16 | a << 8 | a; outimg.setRGB(x, y, o); } } return outimg; }
public static BufferedImage read(InputStream input) throws IOException { BufferedImage in; byte[] data = Helper.readStream(input); try (ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(data))) { CMYKJPEGImageReader r = new CMYKJPEGImageReader(new CMYKJPEGImageReaderSpi()); r.setInput(iis); in = r.read(0); } catch (IOException | ArrayIndexOutOfBoundsException ex) { try { in = ImageIO.read(ImageIO.createImageInputStream(new ByteArrayInputStream(data))); } catch (IOException ex1) { return null; } } int type = in.getType(); if (type != BufferedImage.TYPE_INT_ARGB_PRE && type != BufferedImage.TYPE_INT_RGB) { // convert to ARGB int width = in.getWidth(); int height = in.getHeight(); int[] imgData = in.getRGB(0, 0, width, height, null, 0, width); BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); newImage.getRaster().setDataElements(0, 0, width, height, imgData); return newImage; } return in; }
static BufferedImage addMagnitudes(BufferedImage img1, BufferedImage img2) { BufferedImage dst = new BufferedImage(img1.getWidth(), img1.getHeight(), img1.getType()); for (int y = 0; y < img1.getHeight(); ++y) { for (int x = 0; x < img1.getWidth(); ++x) { int rgb1 = img1.getRGB(x, y); int rgb2 = img2.getRGB(x, y); int r1 = (rgb1 & 0x00FF0000) >> 16; int r2 = (rgb2 & 0x00FF0000) >> 16; int r = (int) (Math.sqrt(r1 * r1 + r2 * r2) / Math.sqrt(2.0)); if (r > 255) r = 255; if (r < 0) r = 0; int g1 = (rgb1 & 0x0000FF00) >> 8; int g2 = (rgb2 & 0x0000FF00) >> 8; int g = (int) (Math.sqrt(g1 * g1 + g2 * g2) / Math.sqrt(2.0)); if (g > 255) g = 255; if (g < 0) g = 0; int b1 = rgb1 & 0x000000FF; int b2 = rgb2 & 0x000000FF; int b = (int) (Math.sqrt(b1 * b1 + b2 * b2) / Math.sqrt(2.0)); if (b > 255) b = 255; if (b < 0) b = 0; int rgb = b + (g << 8) + (r << 16); dst.setRGB(x, y, rgb); } } return dst; }
private BufferedImage aplicaFiltroMatrizConvolucion( double[][] filter, double factor, double bias, BufferedImage img) { imgTmp = new BufferedImage(img.getWidth(), img.getHeight(), img.getType()); for (int x = 0; x < img.getWidth(); x++) { for (int y = 0; y < img.getHeight(); y++) { int r = 0, g = 0, b = 0; for (int filterX = 0; filterX < filter.length; filterX++) for (int filterY = 0; filterY < filter.length; filterY++) { int imageX = (x - filter.length / 2 + filterX + img.getWidth()) % img.getWidth(); int imageY = (y - filter.length / 2 + filterY + img.getHeight()) % img.getHeight(); Color c = new Color(img.getRGB(imageX, imageY)); r += c.getRed() * filter[filterX][filterY]; g += c.getGreen() * filter[filterX][filterY]; b += c.getBlue() * filter[filterX][filterY]; } imgTmp.setRGB( x, y, new Color( Math.min(Math.max((int) (factor * r + bias), 0), 255), Math.min(Math.max((int) (factor * g + bias), 0), 255), Math.min(Math.max((int) (factor * b + bias), 0), 255)) .getRGB()); } } return imgTmp; }
public static BufferedImage getTexture(Point p) { BufferedImage b = new BufferedImage(60, 60, terrainMap.getType()); Graphics g = b.getGraphics(); g.drawImage(terrainMap, 0, 0, 60, 60, 60 * p.x, 60 * p.y, 60 * p.x + 60, 60 * p.y + 60, null); return b; }
@Override public BufferedImage processImage(BufferedImage image) { originalImage = image; width = originalImage.getWidth(); height = originalImage.getHeight(); filteredImage = new BufferedImage(width, height, originalImage.getType()); kernel = createKernel(); int white = 255; int black = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int color = new Color(originalImage.getRGB(i, j)).getRed(); if (color == black) { convolve(i, j); } else { int alpha = new Color(originalImage.getRGB(i, j)).getAlpha(); int rgb = ImageUtilities.colorToRGB(alpha, white, white, white); filteredImage.setRGB(i, j, rgb); } } } return filteredImage; }
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; } } }
/** * returns the resized img with black bars. * * @param img * @param newW * @param newH * @return */ private static BufferedImage resize(BufferedImage img, int newW, int newH) { int startx, wide, starty, hight; int w = img.getWidth(); int h = img.getHeight(); double sourceVer = 1.0 * h / w; double targetVer = 1.0 * newH / newW; if (sourceVer < targetVer) { startx = 0; wide = newW; hight = (int) 1.0 * newW / w * h; starty = (int) (newH - hight) / 2; hight += starty; } else { starty = 0; hight = newH; wide = (int) 1.0 * newH / h * w; startx = (int) (newW - wide) / 2; wide += startx; } BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType()); Graphics2D g = dimg.createGraphics(); g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); Color color = new Color(0, 0, 0); g.setColor(color); g.fillRect(0, 0, startx == 0 ? newW : startx, starty == 0 ? newH : starty); g.drawImage(img, startx, starty, wide, hight, 0, 0, w, h, null); g.fillRect( startx == 0 ? 0 : wide, starty == 0 ? 0 : hight, startx == 0 ? newW : startx, starty == 0 ? newH : starty); g.dispose(); return dimg; }
/** * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance * penalty of BufferedImage.getRGB unmanaging the image. */ public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) { int type = image.getType(); if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) { return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels); } return image.getRGB(x, y, width, height, pixels, 0, width); }