/** * Save onscreen image to file - suffix must be png, jpg, or gif. * * @param filename the name of the file with one of the required suffixes */ public static void save(String filename) { File file = new File(filename); String suffix = filename.substring(filename.lastIndexOf('.') + 1); // png files if (suffix.toLowerCase().equals("png")) { try { ImageIO.write(onscreenImage, suffix, file); } catch (IOException e) { e.printStackTrace(); } } // need to change from ARGB to RGB for jpeg // reference: // http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&L=java2d-interest&D=0&P=2727 else if (suffix.toLowerCase().equals("jpg")) { WritableRaster raster = onscreenImage.getRaster(); WritableRaster newRaster; newRaster = raster.createWritableChild(0, 0, width, height, 0, 0, new int[] {0, 1, 2}); DirectColorModel cm = (DirectColorModel) onscreenImage.getColorModel(); DirectColorModel newCM = new DirectColorModel( cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask()); BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null); try { ImageIO.write(rgbBuffer, suffix, file); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Invalid image file type: " + suffix); } }
/** * Makes the Mandelbrot image. * * @param width the width * @parah height the height * @return the image */ public BufferedImage makeMandelbrot(int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); WritableRaster raster = image.getRaster(); ColorModel model = image.getColorModel(); Color fractalColor = Color.red; int argb = fractalColor.getRGB(); Object colorData = model.getDataElements(argb, null); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { double a = XMIN + i * (XMAX - XMIN) / width; double b = YMIN + j * (YMAX - YMIN) / height; if (!escapesToInfinity(a, b)) raster.setDataElements(i, j, colorData); } return image; }
public static BufferedImage toGrayScale(BufferedImage image) { BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Raster raster = image.getData(); WritableRaster grayRaster = result.getRaster(); int height = raster.getHeight(); int sample = 0; for (int x = 0; x < raster.getWidth(); ++x) { for (int y = 0; y < height; ++y) { sample = raster.getSample(x, y, 0) + raster.getSample(x, y, 1) + raster.getSample(x, y, 2); grayRaster.setSample(x, y, 0, (int) ((float) sample / 3.0)); } } return result; }
public DisplayImage16(int w, int h) { this(); iWidth = w; iHeight = h; iSize = iWidth * iHeight; imageBufferArray = new short[iSize]; for (int i = 0; i < iSize; i++) { imageBufferArray[i] = 0; } bImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_USHORT_GRAY); bRaster = bImage.getRaster(); bRaster.setDataElements(0, 0, iWidth, iHeight, imageBufferArray); // buffGraphics = (Graphics2D) bImage.createGraphics(); // buffGraphics.setFont(font); this.setPreferredSize(new Dimension(iWidth, iHeight)); setVisible(true); // this.setBorder(BorderFactory.createLoweredBevelBorder()); }
public void updateImage(short[] imageData) { bRaster.setDataElements(0, 0, iWidth, iHeight, imageData); repaint(); }
public static BufferedImage getImageFromArray(int[] pixels, int width, int height) { BufferedImage imageBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); WritableRaster raster = (WritableRaster) imageBI.getData(); raster.setPixels(0, 0, width, height, pixels); return imageBI; }