public void set(int x, int y, int argb) { if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) return; // ((BufferedImage) image).setRGB(x, y, argb); getset[0] = argb; WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster(); raster.setDataElements(x, y, getset); }
/** * 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); } }
public int get(int x, int y) { if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) return 0; // return ((BufferedImage) image).getRGB(x, y); WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster(); raster.getDataElements(x, y, getset); return getset[0]; }
public void backgroundImpl() { if (backgroundAlpha) { // Create a small array that can be used to set the pixels several times. // Using a single-pixel line of length 'width' is a tradeoff between // speed (setting each pixel individually is too slow) and memory // (an array for width*height would waste lots of memory if it stayed // resident, and would terrify the gc if it were re-created on each trip // to background(). WritableRaster raster = ((BufferedImage) image).getRaster(); if ((clearPixels == null) || (clearPixels.length < width)) { clearPixels = new int[width]; } java.util.Arrays.fill(clearPixels, backgroundColor); for (int i = 0; i < height; i++) { raster.setDataElements(0, i, width, 1, clearPixels); } } else { // new Exception().printStackTrace(System.out); // in case people do transformations before background(), // need to handle this with a push/reset/pop pushMatrix(); resetMatrix(); g2.setColor(new Color(backgroundColor)); // , backgroundAlpha)); g2.fillRect(0, 0, width, height); popMatrix(); } }
public void loadPixels() { if ((pixels == null) || (pixels.length != width * height)) { pixels = new int[width * height]; } // ((BufferedImage) image).getRGB(0, 0, width, height, pixels, 0, width); WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster(); raster.getDataElements(0, 0, width, height, pixels); }
protected void setImpl(int dx, int dy, int sx, int sy, int sw, int sh, PImage src) { WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster(); if ((sx == 0) && (sy == 0) && (sw == src.width) && (sh == src.height)) { raster.setDataElements(dx, dy, src.width, src.height, src.pixels); } else { // TODO Optimize, incredibly inefficient to reallocate this much memory PImage temp = src.get(sx, sy, sw, sh); raster.setDataElements(dx, dy, temp.width, temp.height, temp.pixels); } }
public PImage getImpl(int x, int y, int w, int h) { PImage output = new PImage(w, h); output.parent = parent; // oops, the last parameter is the scan size of the *target* buffer // ((BufferedImage) image).getRGB(x, y, w, h, output.pixels, 0, w); WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster(); raster.getDataElements(x, y, w, h, output.pixels); return output; }
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(); }
/** * Update the pixels of the cache image. Already determined that the tint has changed, or the * pixels have changed, so should just go through with the update without further checks. */ public void update(boolean tint, int tintColor) { int bufferType = BufferedImage.TYPE_INT_ARGB; boolean opaque = (tintColor & 0xFF000000) == 0xFF000000; if (source.format == RGB) { if (!tint || (tint && opaque)) { bufferType = BufferedImage.TYPE_INT_RGB; } } boolean wrongType = (image != null) && (image.getType() != bufferType); if ((image == null) || wrongType) { image = new BufferedImage(source.width, source.height, bufferType); } WritableRaster wr = image.getRaster(); if (tint) { if (tintedPixels == null || tintedPixels.length != source.width) { tintedPixels = new int[source.width]; } int a2 = (tintColor >> 24) & 0xff; int r2 = (tintColor >> 16) & 0xff; int g2 = (tintColor >> 8) & 0xff; int b2 = (tintColor) & 0xff; if (bufferType == BufferedImage.TYPE_INT_RGB) { // int alpha = tintColor & 0xFF000000; int index = 0; for (int y = 0; y < source.height; y++) { for (int x = 0; x < source.width; x++) { int argb1 = source.pixels[index++]; int r1 = (argb1 >> 16) & 0xff; int g1 = (argb1 >> 8) & 0xff; int b1 = (argb1) & 0xff; tintedPixels[x] = // 0xFF000000 | (((r2 * r1) & 0xff00) << 8) | ((g2 * g1) & 0xff00) | (((b2 * b1) & 0xff00) >> 8); } wr.setDataElements(0, y, source.width, 1, tintedPixels); } // could this be any slower? // float[] scales = { tintR, tintG, tintB }; // float[] offsets = new float[3]; // RescaleOp op = new RescaleOp(scales, offsets, null); // op.filter(image, image); } else if (bufferType == BufferedImage.TYPE_INT_ARGB) { int index = 0; for (int y = 0; y < source.height; y++) { if (source.format == RGB) { int alpha = tintColor & 0xFF000000; for (int x = 0; x < source.width; x++) { int argb1 = source.pixels[index++]; int r1 = (argb1 >> 16) & 0xff; int g1 = (argb1 >> 8) & 0xff; int b1 = (argb1) & 0xff; tintedPixels[x] = alpha | (((r2 * r1) & 0xff00) << 8) | ((g2 * g1) & 0xff00) | (((b2 * b1) & 0xff00) >> 8); } } else if (source.format == ARGB) { for (int x = 0; x < source.width; x++) { int argb1 = source.pixels[index++]; int a1 = (argb1 >> 24) & 0xff; int r1 = (argb1 >> 16) & 0xff; int g1 = (argb1 >> 8) & 0xff; int b1 = (argb1) & 0xff; tintedPixels[x] = (((a2 * a1) & 0xff00) << 16) | (((r2 * r1) & 0xff00) << 8) | ((g2 * g1) & 0xff00) | (((b2 * b1) & 0xff00) >> 8); } } else if (source.format == ALPHA) { int lower = tintColor & 0xFFFFFF; for (int x = 0; x < source.width; x++) { int a1 = source.pixels[index++]; tintedPixels[x] = (((a2 * a1) & 0xff00) << 16) | lower; } } wr.setDataElements(0, y, source.width, 1, tintedPixels); } // Not sure why ARGB images take the scales in this order... // float[] scales = { tintR, tintG, tintB, tintA }; // float[] offsets = new float[4]; // RescaleOp op = new RescaleOp(scales, offsets, null); // op.filter(image, image); } } else { wr.setDataElements(0, 0, source.width, source.height, source.pixels); } this.tinted = tint; this.tintedColor = tintColor; }
/** * Update the pixels[] buffer to the PGraphics image. * * <p>Unlike in PImage, where updatePixels() only requests that the update happens, in * PGraphicsJava2D, this will happen immediately. */ public void updatePixels() { // updatePixels(0, 0, width, height); WritableRaster raster = ((BufferedImage) (primarySurface ? offscreen : image)).getRaster(); raster.setDataElements(0, 0, width, height, pixels); }