ofImage
The ofImage is a useful object for loading, saving and drawing images in openFrameworks. ofImage is a convenient class that lets you both draw images to the screen and manipulate their pixel data. The ofImage allows you to load an image from disk, manipulate the pixels, and create an OpenGL texture that you can display and manipulate on the graphics card. Loading a file into the ofImage allocates an ofPixels object and creates the ofTexture to display the pixels.
ofImage uses a library called "freeImage" under the hood.
allocate( ... )
void allocate(int w, int h, ofImageType type)This allocates space in the ofImage, both the ofPixels and the ofTexture that the ofImage contains.
You don't need to call this before loading an image, but for when you want to allocate. space ahead of when you are going to use the image.
The types of images can be OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA
or OF_IMAGE_GRAYSCALE.
You need to call update() to update the texture after updating the pixels manually.
Parameters:
w Width of image to allocate.
h Height of image to allocate.
type The ofImageType.
This allocates space in the ofImage, both the ofPixels and the ofTexture that the ofImage contains.
img.allocate(640, 480, OF_IMAGE_COLOR);
int i = 0;
while ( i < img.getPixels().size() ) {
img.getPixels()[i] = abs(sin( float(i) / 18.f)) * 255.f; // make some op-art
i++;
}
img.update();
It allocates an image of width (w) and height (h). The type can be of three types: OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA. You don't need to call this before loading an image, but for when you want to allocate space ahead of when you are going to use the image.
bind( ... )
void bind(int textureLocation=0)Binds the oftexture instance that the ofImage contains so that it can be used for advanced drawing.
This binds the ofTexture instance that the ofImage contains so that it can be used for advanced drawing
void ofApp::setup() {
img.allocate(256, 256, OF_IMAGE_COLOR);
int i = 0;
while ( i < img.getPixels().size() ) {
img.getPixels()[i] = abs(sin( float(i) / 18.f )) * 255.f;
i++;
}
img.update();
mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
mesh.addVertex(ofVec2f(10, 10));
mesh.addVertex(ofVec2f(410, 10));
mesh.addVertex(ofVec2f(410, 410));
mesh.addVertex(ofVec2f(10, 410));
mesh.addVertex(ofVec2f(10, 10));
mesh.addTexCoord(ofVec2f(0, 0));
mesh.addTexCoord(ofVec2f(256, 0));
mesh.addTexCoord(ofVec2f(256, 256));
mesh.addTexCoord(ofVec2f(0, 256));
mesh.addTexCoord(ofVec2f(0, 0));
}
void ofApp::draw(){
ofBackground(255, 255, 255); // background white
img.bind();
mesh.draw();
img.unbind();
}
clear( )
void clear()This clears the texture and pixels contained within the ofImage.
This clears the texture and pixels contained within the ofImage.
crop( ... )
void crop(int x, int y, int w, int h)This crops the image to the w,h passed in from the x,y position.
This does an in place crop and allocates memory.
Parameters:
x x position of upper-left corner of region to crop.
y y position of upper-left corner of region to crop.
w Width of region to crop.
h Height of region to crop.
This crops the image to the w,h passed in from the x,y position.
draw( ... )
void draw(const glm::vec3 &pos)draw( ... )
void draw(const glm::vec3 &pos, float w, float h)draw( ... )
void draw(float x, float y)Draw the image at it's normal size.
Parameters:
x Draw position on the x axis.
y Draw position on the y axis.
Draws the ofImage into the x,y location using the default height and width of the image.
draw( ... )
void draw(float x, float y, float w, float h)Draw the image at a given size.
Parameters:
x Draw position on the x axis.
y Draw position on the y axis.
w Draw width.
h Draw height.
Draws the ofImage into the x,y location and with the width and height, with any attendant scaling that may occur from fitting the ofImage into the width and height.
draw( ... )
void draw(float x, float y, float z)Draw the texture at it's normal size with depth.
Parameters:
x Draw position on the x axis.
y Draw position on the y axis.
z Draw position on the z axis.
Draws the ofImage into the x,y,z location with the default height and width. You should ensure that you turn on depth sorting using glEnable(GL_DEPTH) before trying to draw multiple objects into z-space.
draw( ... )
void draw(float x, float y, float z, float w, float h)Draw the image at a given size with depth.
Parameters:
x Draw position on the x axis.
y Draw position on the y axis.
z Draw position on the z axis.
w Draw width.
h Draw height.
Draws the ofImage into the x,y,z location and with the width and height, with any attendant scaling that may occur from fitting the ofImage into the width and height. You should ensure that you turn on depth sorting using glEnable(GL_DEPTH) before trying to draw multiple objects into z-space.
drawSubsection( ... )
void drawSubsection(float x, float y, float w, float h, float sx, float sy)Draws a subsection of the image.
This functions like a clipping mask. Does not altering any pixel data.
Parameters:
x X position to draw cropped image at.
y Y position to draw cropped image at.
w Width of subsection to draw.
h Height of subsection to draw.
sx X position in image to begin cropping from.
sy Y position in image to begin cropping from.
Draws a subsection of the image (functions like a clipping mask) without altering any pixel data. (x,y) are the position to draw the cropped image at, (w,h) is the size of the subsection to draw and the size to crop (these can be different using the function below with sw,sh) and (sx,sy) are the source pixel positions in the image to begin cropping from.
// crop the image from the mouse position to 100x100 pixels and draw it at 0,0
img.drawSubsection(0, 0, 100, 100, mouseX, mouseY);
An example showing how to use drawSubsection can be found in of_release
drawSubsection( ... )
void drawSubsection(float x, float y, float w, float h, float sx, float sy, float sw, float sh)Draws a subsection of the image.
This functions like a clipping mask. Does not altering any pixel data.
Parameters:
x X position to draw cropped image at.
y Y position to draw cropped image at.
w Width of subsection to draw.
h Height of subsection to draw.
sx X position in image to begin cropping from.
sy Y position in image to begin cropping from.
sw Source width of cropped area.
sh Source height of cropped area.
(sw,sh) indicate the source width and height of the cropped area and the (w,h) indicate the size to draw the cropped area at.
drawSubsection( ... )
void drawSubsection(float x, float y, float z, float w, float h, float sx, float sy)Draws a subsection of the image.
This functions like a clipping mask. Does not altering any pixel data.
Parameters:
x X position to draw cropped image at.
y Y position to draw cropped image at.
z Z position to draw cropped image at.
w Width of subsection to draw.
h Height of subsection to draw.
sx X position in image to begin cropping from.
sy Y position in image to begin cropping from.
drawSubsection( ... )
void drawSubsection(float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh)Draws a subsection of the image.
This functions like a clipping mask. Does not altering any pixel data.
Parameters:
x X position to draw cropped image at.
y Y position to draw cropped image at.
z Z position to draw cropped image at.
w Width of subsection to draw.
h Height of subsection to draw.
sx X position in image to begin cropping from.
sy Y position in image to begin cropping from.
sw Source width of cropped area.
sh Source height of cropped area.
getHeight( )
float getHeight()Get height of image as a float.
Returns: Height of image as float.
Returns the height of the image in pixels.
getImageType( )
ofImageType getImageType()getTexture( )
ofTexture & getTexture()Returns a reference to the texture that the ofImage contains.
You can use this to directly manipulate the texture itself, but keep in mind that if you manipulate the texture directly, there is no simple way to copy the data from the texture back to the pixels and keep the ofImage in sync.
Returns: A reference to the texture that the ofImage contains.
getTexture( )
const ofTexture & getTexture()Returns a const reference to the texture that the ofImage contains.
Returns: A const reference to the texture that the ofImage contains.
getWidth( )
float getWidth()Get width of image as a float.
Returns: Width of image as float.
Returns the width of the image in pixels.
grabScreen( ... )
void grabScreen(int x, int y, int w, int h)Grabs pixels from the opengl window specified by the region (x, y, w, h) and turns them into an image.
It resizes or allocates the ofImage if it's necessary.
Warning: Uses glReadPixels() which can be slow.
Parameters:
x x position of upper-left corner of region.
y y position of upper-left corner of region.
w Width of region.
h Height of region.
Grabs pixels from the opengl window specified by the region (x, y, w, h) and turns them into an image. It resizes or allocates the ofImage if it's necessary.
isAllocated( )
bool isAllocated()Whether the image has been allocated either by a call to allocate or by loading pixel data into the image.
Returns: true if the image has been allocated.
Returns whether the image has been allocated either by a call to allocate or by loading pixel data into the image.
isUsingTexture( )
bool isUsingTexture()Returns whether the ofImage has a texture or not.
If the ofImage doesn't have a texture, nothing will be drawn to the screen.
Returns: true if the ofImage is using a texture.
Returns whether the ofImage has a texture or not. If not, nothing will be drawn to the screen if the draw() method is called.
load( ... )
bool load(const ofBuffer &buffer, const ofImageLoadSettings &settings)Loads an image from an ofBuffer instance created by, for instance, ofFile::readToBuffer().
This actually loads the image data into an ofPixels object and then into the texture.
load( ... )
bool load(const filesystem::path &fileName, const ofImageLoadSettings &settings)Loads an image given by fileName.
Parameters:
fileName Program looks for image given by fileName, relative to the data folder.
settings Load options
Returns: true if image loaded correctly.
mirror( ... )
void mirror(bool vertical, bool horizontal)This reflects the pixels of the image across the vertical and/or horizontal axis.
Parameters:
vertical Set to true to reflect image across vertical axis.
horizontal Set to true to reflect image across horizontal axis.
This reflects the pixels of the image across the vertical and/or horizontal axis.
ofImage_( ... )
ofImage_(const filesystem::path &fileName, const ofImageLoadSettings &settings)This creates an ofImage from a file which can be a local string or a URL, allocating space for the pixels, and copying the pixels into the texture that the ofImage instance contains.
ofImage img("http://www.openframeworks.cc/wp-content/themes/ofw/images/ofw-logo.gif");
or
ofImage img("anImage.png"); // assumes this is in bin/data
ofImage_( )
ofImage_()\name Image Construction {
This creates an ofImage but doesn't allocate any memory for it, so you can't use the image immediately after creating it.
resetAnchor( )
void resetAnchor()Removes anchor positioning.
Resets the anchor to (0, 0) so the image will be drawn from its upper left hand corner.
This removes any anchor positioning, meaning that the ofImage will be draw with the upper left hand corner at the point passed into draw().
resize( ... )
void resize(int newWidth, int newHeight)Resizes the image to a new size (w, h); Can be used to scale up or down an image.
Parameters:
newWidth New width of image.
newHeight New height of image.
Resizes the image to a new size (w, h); Can be used to scale up or down an image.
rotate90( ... )
void rotate90(int rotation)Rotates the image by a multiple of 90 degrees.
Parameters:
rotation Amount to rotate in multiples of 90. For instance, if you pass in 2, then the image will be rotated 180 degrees.
Rotates the image by a multiple of 90 degrees, for instance, if you pass in 2, then the image will be rotated 180 degrees.
save( ... )
bool save(ofBuffer &buffer, ofImageFormat imageFormat=OF_IMAGE_FORMAT_PNG, ofImageQualityType compressionLevel=OF_IMAGE_QUALITY_BEST)This saves the image to the ofBuffer passed with the image quality specified by compressionLevel.
Parameters:
buffer ofBuffer to save image to.
compressionLevel The ofImageQualityType.
save( ... )
bool save(const filesystem::path &fileName, ofImageQualityType compressionLevel=OF_IMAGE_QUALITY_BEST)Saves the image to the file path in fileName with the image quality specified by compressionLevel.
Parameters:
fileName Saves image to this path, relative to the data folder.
compressionLevel The ofImageQualityType.
setAnchorPercent( ... )
void setAnchorPercent(float xPct, float yPct)Change the drawing anchor from top-left corner to a position specified by xPct and yPct.
Changes the drawing position specified by draw() from the normal top- left corner of the image to a position specified by xPct and yPct in relation to the dimensions of the image. This can be useful for aligning and centering images as well as rotating an image around its center. Note: range of xPct and yPct is 0.0 to 1.0. For xPct, 1.0 represents the width of the image. For yPct, 1.0 represents the height of the image. These values are not capped.
Parameters:
xPct X position of the new anchor, specified as a percent of the width of the image.
yPct Y position of the new anchor, specified as a percent of the height of the image.
Changes the drawing position specified by draw() from the normal top-left corner of the image to a position specified by xPct and yPct in relation to the dimensions of the image. This can be useful for aligning and centering images as well as rotating an image around its center.
Note: range of xPct and yPct is 0.0 to 1.0. For xPct, 1.0 represents the width of the image. For yPct, 1.0 represents the height of the image. These values are not capped.
For example to draw an image so that its center is at 100, 100:
myImage.setAnchorPercent(0.5, 0.5);
myImage.draw(100, 100);
To rotate an image around its center at 100, 100:
ofPushMatrix();
ofTranslate(100, 100, 0);
ofRotate(45);
myImage.setAnchorPercent(0.5, 0.5);
myImage.draw(0, 0);
ofPopMatrix();
To align the right side of an image with the right edge of the window:
myImage.setAnchorPercent(1.0, 0.0);
myImage.draw(ofGetWidth(), 0);
setAnchorPoint( ... )
void setAnchorPoint(float x, float y)Changes drawing position from top-left corner to position specified by x,y.
Changes the drawing position specified by draw() from the normal top- left corner of the image to a position specified by x and y, measured in pixels. This can be useful for aligning and centering images as well as rotating an image around its center.
Parameters:
x X position of the new anchor.
y Y position of the new anchor.
Changes the drawing position specified by draw() from the normal top-left corner of the image to a position specified by x and y, measured in pixels. This can be useful for aligning and centering images as well as rotating an image around its center.
Note: see also setAnchorPercent() if you want to specify the anchor as a percentage of the image size.
For example to draw an image so that its center is at 100, 100:
myImage.setAnchorPoint(myImage.getWidth()/2, myImage.getHeight()/2);
myImage.draw(100, 100);
To rotate an image around its center at 100, 100:
ofPushMatrix();
ofTranslate(100, 100, 0);
ofRotate(45);
myImage.setAnchorPoint(myImage.getWidth()/2, myImage.getHeight()/2);
myImage.draw(0, 0);
ofPopMatrix();
To align the right side of an image with the right edge of the window:
myImage.setAnchorPoint(myImage.getWidth(), 0.0);
myImage.draw(ofGetWidth(), 0);
setCompression( ... )
void setCompression(ofTexCompression compression)This sets the compression level used when creating mipmaps for the ofTexture contained by the ofImage.
Parameters:
compression The ofTexCompression to set.
This sets the compression level used when creating mipmaps for the ofTexture contained by the ofImage. This is quite different than the ofImageQualityType image quality parameter used in saveImage(). The different texture compression levels are: OF_COMPRESS_NONE, OF_COMPRESS_SRGB, OF_COMPRESS_ARB
setFromPixels( ... )
void setFromPixels(const PixelType *pixels, int w, int h, ofImageType type, bool bOrderIsRGB=true)Sets the pixels of the image from an array of values.
Set the pixels of the image from an array of values, for an ofFloatImage these need to be floats, for an ofImage these need to be unsigned chars. The w and h values are important so that the correct dimensions are set in the image. This assumes that you're setting the pixels from 0,0 or the upper left hand corner of the image. The bOrderIsRGB flag allows you pass in pixel data that is BGR by setting bOrderIsRGB=false.
Parameters:
pixels Array of pixel values.
w Width of image being passed in.
h Height of image being passed in.
type The image type can be OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, or OF_IMAGE_COLOR_ALPHA.
bOrderIsRGB Pass in pixel data that is BGR by setting bOrderIsRGB=false.
Set the pixels of the image from an array of values, for an ofFloatImage these need to be floats, for an ofImage these need to be unsigned chars. The w and h values are important so that the correct dimensions are set in the image. This assumes that you're setting the pixels from 0,0 or the upper left hand corner of the image. The bOrderIsRGB flag allows you pass in pixel data that is BGR by setting bOrderIsRGB=false.
Copies in the pixel data from the 'pixels' array. Specify the corresponding width and height of the image you are passing in with 'w' and 'h'. The image type can be OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, or OF_IMAGE_COLOR_ALPHA.
Note: that your array has to be at least as big as [ width * height * bytes per pixel ].
If you have a grayscale image, you will have (widthheight) number of pixels. Color images will have (widthheight3) number of pixels (interlaced R,G,B), and coloralpha images will have (widthheight*4) number of pixels (interlaced R,G,B,A).
Note: You do not need to call allocate() before calling setFromPixels() as setFromPixels() re-allocates itself if needed.
setImageType( ... )
void setImageType(ofImageType type)Set type of image to one of the following: OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA
This does cause the image to be reallocated and the texture to be updated, so it can be an expensive operation if done frequently. Converting down, for example from color to grayscale, loses information and is a destructive change.
Parameters:
type The type of image, one of the following:
OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA
Set the type of image to one of the following: OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA. This does cause the image to be reallocated and the texture to be updated, so it can be an expensive operation if done frequently. Converting down, for example from color to grayscale, loses information and is a destructive change.
For example, you can load in a color image, and convert it to grayscale:
myImage.loadImage("somethingColor.jpg");
myImage.setImageType(OF_IMAGE_GRAYSCALE); // now I am grayscale;
setUseTexture( ... )
void setUseTexture(bool bUse)Turns on or off the allocation and use of a texture.
Parameters:
bUse Allocate and use a texture or not.
If you set the ofImage to not use a texture it will contain the pixels of the image but cannot be drawn to the screen without copying its data into an ofTexture instance.
This turns on or off the allocation and use of a texture. any time you change the image (loading, resizing, converting the type), ofImage will upload data to an opengl texture. It may not be necessary, though, and it could be that you need to save memory on the graphics card, or that you don't need to draw this image on the screen. You can call this even before you load an image in to OF:
myImage.setUseTexture(false);
myImage.loadImage("blah.gif");
Since in the majority of cases, ofImages will be loaded in and drawn onscreen, the default is set to use a texture.
unbind( ... )
void unbind(int textureLocation=0)Unbinds the ofTexture instance that the ofImage contains.
Call this after you call bind().
This unbinds the ofTexture instance that the ofImage contains. Call this after you call bind().
unloadTexture( )
void unloadTexture()update( )
void update()Call to ensure that changes to pixels are reflected in the ofTexture of the image.
Many of the ofImage methods call this after they change the pixels, but if you directly manipulate the pixels of the ofImage, then you should make sure to call update() before trying to draw the texture of the image to the screen.
This method should be called after you update the pixels of the image and want to ensure that the changes to the pixels are reflected in the ofTexture of the image. Many of the ofImage methods call this after they change the pixels, but if you directly manipulate the pixels of the ofImage, then you should make sure to call update() before trying to draw the texture of the image to the screen.
~ofImage_( )
~ofImage_()