processing.core
Class PImage

java.lang.Object
  extended by processing.core.PImage
All Implemented Interfaces:
java.lang.Cloneable, PConstants
Direct Known Subclasses:
Capture, Movie, PGraphics

public class PImage
extends java.lang.Object
implements PConstants, java.lang.Cloneable

Storage class for pixel data. This is the base class for most image and pixel information, such as PGraphics and the video library classes.

Code for copying, resizing, scaling, and blending contributed by toxi.


Field Summary
 int format
          Format for this image, one of RGB, ARGB or ALPHA.
 int height
           
 PApplet parent
          Path to parent object that will be used with save().
 int[] pixels
           
 int width
           
 
Fields inherited from interface processing.core.PConstants
A, AB, ADD, AG, ALPHA, ALPHA_MASK, ALT, AMBIENT, AR, ARC, ARGB, ARROW, B, BACKSPACE, BASELINE, BEEN_LIT, BEVEL, BLEND, BLUE_MASK, BLUR, BOTTOM, BOX, BURN, CENTER, CENTER_DIAMETER, CENTER_RADIUS, CHATTER, CLOSE, CMYK, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CUSTOM, DA, DARKEST, DB, DEG_TO_RAD, DELETE, DG, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ACCURATE_TEXTURES, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_OPENGL_2X_SMOOTH, DISABLE_OPENGL_ERROR_REPORT, DODGE, DOWN, DR, DXF, EB, EDGE, EG, ELLIPSE, ENABLE_ACCURATE_TEXTURES, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_2X_SMOOTH, ENABLE_OPENGL_4X_SMOOTH, ENABLE_OPENGL_ERROR_REPORT, ENTER, EPSILON, ER, ERODE, ERROR_BACKGROUND_IMAGE_FORMAT, ERROR_BACKGROUND_IMAGE_SIZE, ERROR_PUSHMATRIX_OVERFLOW, ERROR_PUSHMATRIX_UNDERFLOW, ERROR_TEXTFONT_NULL_PFONT, ESC, EXCLUSION, G, GIF, GRAY, GREEN_MASK, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LEFT, LIGHTEST, LINE, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MOVE, MULTIPLY, NORMAL, NORMALIZED, NX, NY, NZ, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, platformNames, POINT, POINTS, POLYGON, POSTERIZE, PROBLEM, PROJECT, QUAD, QUAD_STRIP, QUADS, QUARTER_PI, R, RAD_TO_DEG, RADIUS, RECT, RED_MASK, REPLACE, RETURN, RGB, RIGHT, ROUND, SA, SB, SCREEN, SG, SHAPE, SHIFT, SHINE, SOFT_LIGHT, SPB, SPG, SPHERE, SPOT, SPR, SQUARE, SR, SUBTRACT, SW, TAB, TARGA, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, TX, TY, TZ, U, UP, V, VERTEX_FIELD_COUNT, VW, VX, VY, VZ, WAIT, WHITESPACE, WINDOWS, X, Y, Z
 
Constructor Summary
PImage()
          Create an empty image object, set its format to RGB.
PImage(java.awt.Image img)
          Construct a new PImage from a java.awt.Image.
PImage(int width, int height)
          Create a new RGB (alpha ignored) image of a specific size.
PImage(int width, int height, int format)
           
 
Method Summary
 void blend(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, int mode)
          Blends one area of this image to another area.
 void blend(PImage src, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, int mode)
          Copies area of one image into another PImage object.
static int blendColor(int c1, int c2, int mode)
          Blend two colors based on a particular mode.
 java.lang.Object clone()
          Duplicate an image, returns new PImage object.
 void copy(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh)
          Copy things from one area of this image to another area in the same image.
 void copy(PImage src, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh)
          Copies area of one image into another PImage object.
 void filter(int kind)
          Method to apply a variety of basic filters to this image.
 void filter(int kind, float param)
          Method to apply a variety of basic filters to this image.
 PImage get()
          Returns a copy of this PImage.
 int get(int x, int y)
          Returns an ARGB "color" type (a packed 32 bit int with the color.
 PImage get(int x, int y, int w, int h)
          Grab a subsection of a PImage, and copy it into a fresh PImage.
 java.lang.Object getCache(java.lang.Object parent)
          Get cache storage data for the specified renderer.
 java.awt.Image getImage()
          Returns a BufferedImage from this PImage.
 void init(int width, int height, int format)
          Function to be used by subclasses of PImage to init later than at the constructor, or re-init later when things changes.
 boolean isModified()
           
 void loadPixels()
          Call this when you want to mess with the pixels[] array.
 void mask(int[] alpha)
          Set alpha channel for an image.
 void mask(PImage alpha)
          Set alpha channel for an image using another image as the source.
 void removeCache(java.lang.Object parent)
          Remove information associated with this renderer from the cache, if any.
 void resize(int wide, int high)
          Resize this image to a new width and height.
 void save(java.lang.String path)
          Save this image to disk.
 void set(int x, int y, int c)
          Set a single pixel to the specified color.
 void set(int x, int y, PImage src)
          Efficient method of drawing an image's pixels directly to this surface.
 void setCache(java.lang.Object parent, java.lang.Object storage)
          Store data of some kind for a renderer that requires extra metadata of some kind.
 void setModified()
           
 void setModified(boolean m)
           
 void updatePixels()
          Call this when finished messing with the pixels[] array.
 void updatePixels(int x, int y, int w, int h)
          Mark the pixels in this region as needing an update.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

format

public int format
Format for this image, one of RGB, ARGB or ALPHA. note that RGB images still require 0xff in the high byte because of how they'll be manipulated by other functions


pixels

public int[] pixels

width

public int width

height

public int height

parent

public PApplet parent
Path to parent object that will be used with save(). This prevents users from needing savePath() to use PImage.save().

Constructor Detail

PImage

public PImage()
Create an empty image object, set its format to RGB. The pixel array is not allocated.


PImage

public PImage(int width,
              int height)
Create a new RGB (alpha ignored) image of a specific size. All pixels are set to zero, meaning black, but since the alpha is zero, it will be transparent.


PImage

public PImage(int width,
              int height,
              int format)

PImage

public PImage(java.awt.Image img)
Construct a new PImage from a java.awt.Image. This constructor assumes that you've done the work of making sure a MediaTracker has been used to fully download the data and that the img is valid.

Method Detail

init

public void init(int width,
                 int height,
                 int format)
Function to be used by subclasses of PImage to init later than at the constructor, or re-init later when things changes. Used by Capture and Movie classes (and perhaps others), because the width/height will not be known when super() is called. (Leave this public so that other libraries can do the same.)


getImage

public java.awt.Image getImage()
Returns a BufferedImage from this PImage.


setCache

public void setCache(java.lang.Object parent,
                     java.lang.Object storage)
Store data of some kind for a renderer that requires extra metadata of some kind. Usually this is a renderer-specific representation of the image data, for instance a BufferedImage with tint() settings applied for PGraphicsJava2D, or resized image data and OpenGL texture indices for PGraphicsOpenGL.


getCache

public java.lang.Object getCache(java.lang.Object parent)
Get cache storage data for the specified renderer. Because each renderer will cache data in different formats, it's necessary to store cache data keyed by the renderer object. Otherwise, attempting to draw the same image to both a PGraphicsJava2D and a PGraphicsOpenGL will cause errors.

Parameters:
parent - The PGraphics object (or any object, really) associated
Returns:
data stored for the specified parent

removeCache

public void removeCache(java.lang.Object parent)
Remove information associated with this renderer from the cache, if any.

Parameters:
parent - The PGraphics object whose cache data should be removed

isModified

public boolean isModified()

setModified

public void setModified()

setModified

public void setModified(boolean m)

loadPixels

public void loadPixels()
Call this when you want to mess with the pixels[] array.

For subclasses where the pixels[] buffer isn't set by default, this should copy all data into the pixels[] array


updatePixels

public void updatePixels()
Call this when finished messing with the pixels[] array.

Mark all pixels as needing update.


updatePixels

public void updatePixels(int x,
                         int y,
                         int w,
                         int h)
Mark the pixels in this region as needing an update.

This is not currently used by any of the renderers, however the api is structured this way in the hope of being able to use this to speed things up in the future.


clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Duplicate an image, returns new PImage object. The pixels[] array for the new object will be unique and recopied from the source image. This is implemented as an override of Object.clone(). We recommend using get() instead, because it prevents you from needing to catch the CloneNotSupportedException, and from doing a cast from the result.

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

resize

public void resize(int wide,
                   int high)
Resize this image to a new width and height. Use 0 for wide or high to make that dimension scale proportionally.


get

public int get(int x,
               int y)
Returns an ARGB "color" type (a packed 32 bit int with the color. If the coordinate is outside the image, zero is returned (black, but completely transparent).

If the image is in RGB format (i.e. on a PVideo object), the value will get its high bits set, just to avoid cases where they haven't been set already.

If the image is in ALPHA format, this returns a white with its alpha value set.

This function is included primarily for beginners. It is quite slow because it has to check to see if the x, y that was provided is inside the bounds, and then has to check to see what image type it is. If you want things to be more efficient, access the pixels[] array directly.


get

public PImage get(int x,
                  int y,
                  int w,
                  int h)
Grab a subsection of a PImage, and copy it into a fresh PImage. As of release 0149, no longer honors imageMode() for the coordinates.


get

public PImage get()
Returns a copy of this PImage. Equivalent to get(0, 0, width, height).


set

public void set(int x,
                int y,
                int c)
Set a single pixel to the specified color.


set

public void set(int x,
                int y,
                PImage src)
Efficient method of drawing an image's pixels directly to this surface. No variations are employed, meaning that any scale, tint, or imageMode settings will be ignored.


mask

public void mask(int[] alpha)
Set alpha channel for an image. Black colors in the source image will make the destination image completely transparent, and white will make things fully opaque. Gray values will be in-between steps.

Strictly speaking the "blue" value from the source image is used as the alpha color. For a fully grayscale image, this is correct, but for a color image it's not 100% accurate. For a more accurate conversion, first use filter(GRAY) which will make the image into a "correct" grayscake by performing a proper luminance-based conversion.


mask

public void mask(PImage alpha)
Set alpha channel for an image using another image as the source.


filter

public void filter(int kind)
Method to apply a variety of basic filters to this image.

Luminance conversion code contributed by toxi

Gaussian blur code contributed by Mario Klingemann


filter

public void filter(int kind,
                   float param)
Method to apply a variety of basic filters to this image. These filters all take a parameter.

Gaussian blur code contributed by Mario Klingemann and later updated by toxi for better speed.


copy

public void copy(int sx,
                 int sy,
                 int sw,
                 int sh,
                 int dx,
                 int dy,
                 int dw,
                 int dh)
Copy things from one area of this image to another area in the same image.


copy

public void copy(PImage src,
                 int sx,
                 int sy,
                 int sw,
                 int sh,
                 int dx,
                 int dy,
                 int dw,
                 int dh)
Copies area of one image into another PImage object.


blendColor

public static int blendColor(int c1,
                             int c2,
                             int mode)
Blend two colors based on a particular mode.

A useful reference for blending modes and their algorithms can be found in the SVG specification.

It is important to note that Processing uses "fast" code, not necessarily "correct" code. No biggie, most software does. A nitpicker can find numerous "off by 1 division" problems in the blend code where >>8 or >>7 is used when strictly speaking /255.0 or /127.0 should have been used.

For instance, exclusion (not intended for real-time use) reads r1 + r2 - ((2 * r1 * r2) / 255) because 255 == 1.0 not 256 == 1.0. In other words, (255*255)>>8 is not the same as (255*255)/255. But for real-time use the shifts are preferrable, and the difference is insignificant for applications built with Processing.


blend

public void blend(int sx,
                  int sy,
                  int sw,
                  int sh,
                  int dx,
                  int dy,
                  int dw,
                  int dh,
                  int mode)
Blends one area of this image to another area.

See Also:
blendColor(int,int,int)

blend

public void blend(PImage src,
                  int sx,
                  int sy,
                  int sw,
                  int sh,
                  int dx,
                  int dy,
                  int dw,
                  int dh,
                  int mode)
Copies area of one image into another PImage object.

See Also:
blendColor(int,int,int)

save

public void save(java.lang.String path)
Save this image to disk.

As of revision 0100, this function requires an absolute path, in order to avoid confusion. To save inside the sketch folder, use the function savePath() from PApplet, or use saveFrame() instead. As of revision 0116, savePath() is not needed if this object has been created (as recommended) via createImage() or createGraphics() or one of its neighbors.

As of revision 0115, when using Java 1.4 and later, you can write to several formats besides tga and tiff. If Java 1.4 is installed and the extension used is supported (usually png, jpg, jpeg, bmp, and tiff), then those methods will be used to write the image. To get a list of the supported formats for writing, use:
println(javax.imageio.ImageIO.getReaderFormatNames())

To use the original built-in image writers, use .tga or .tif as the extension, or don't include an extension. When no extension is used, the extension .tif will be added to the file name.

The ImageIO API claims to support wbmp files, however they probably require a black and white image. Basic testing produced a zero-length file with no error.