ofDocsdocumentation graphics ofPixels

ofPixels

ofPixels is an object for working with blocks of pixels, those pixels can be copied from an image that you've loaded, something that you've drawn using ofGraphics, or a ofVideoGrabber instance. You can create an image from pixels, using on ofPixels object like so:

ofPixels p;
ofLoadImage(p, "pathToImage.jpg");

ofPixels represents pixels data on the CPU as opposed to an ofTexture which represents pixel data on the GPU. They can easily be made inter-operational though:

ofTexture tex;
// do some stuff with t
ofPixels pix;
tex.readToPixels(pix); // now all the pixels from tex are in pix

You can access the pixels in an ofPixels object with the [] operator.

ofPixels pix;
// put some stuff in the pixels
int i = 0;
while( i < pix.size()) {
	char c = pix[i];
	i++;
}

You can think of the ofPixels as the CPU side representation of pixel data that can be sent to the GPU as an ofTexture object. To draw pixels, you need to put them into an ofTexture and to manipulate an ofTextures pixel data you need an ofPixels object.


allocate( ... )

void allocate(size_t w, size_t h, size_t channels)

Allocates space for pixel data

Parameters:

w Width of pixel array

h Height of pixel array

channels Number of channels per pixel

Allocates space for pixel data of the given width (w), height (h) and number of channels (channels). If an ofImageType or ofPixelFormat is passed in, it will allocate based on the required number of channels.


allocate( ... )

void allocate(size_t w, size_t h, ofImageType imageType)

Allocates space for pixel data

The imageType can be one of the following:

OF_IMAGE_GRAYSCALE
OF_IMAGE_COLOR
OF_IMAGE_COLOR_ALPHA

Parameters:

w Width of pixel array

h Height of pixel array

imageType ofImageType defining number of channels per pixel

ofImageType:

OF_IMAGE_GRAYSCALE

OF_IMAGE_COLOR

OF_IMAGE_COLOR_ALPHA

allocate( ... )

void allocate(size_t w, size_t h, ofPixelFormat pixelFormat)

Allocates space for pixel data

The pixelFormat can be one of the following:

OF_PIXELS_RGB
OF_PIXELS_RGBA
OF_PIXELS_BGRA
OF_PIXELS_MONO

Parameters:

w Width of pixel array

h Height of pixel array

pixelFormat ofPixelFormat defining number of channels per pixel

ofPixelFormat:

OF_PIXELS_RGB

OF_PIXELS_RGBA

OF_PIXELS_BGRA

OF_PIXELS_MONO

begin( )

ofPixels_::iterator begin()

begin( )

ofPixels_::const_iterator begin()

bicubicInterpolate( ... )

float bicubicInterpolate(const float *patch, float x, float y, float x2, float y2, float x3, float y3)

\endcond


bytesFromPixelFormat( ... )

size_t bytesFromPixelFormat(size_t w, size_t h, ofPixelFormat format)

clear( )

void clear()

Clear all the data from the ofPixels objects. After calling this you'll need to allocate() the ofPixels object again to use it.

This clears all the data from the ofPixels objects. After calling this you'll need to allocate the ofPixels object again to use it.


crop( ... )

void crop(size_t x, size_t y, size_t width, size_t height)

Crop the pixels to a new width and height.

As a word of caution this reallocates memory and can be a bit expensive if done a lot.

This crops the pixels to a new width and height. As a word of caution this reallocates memory and can be a bit expensive if done a lot.


end( )

ofPixels_::iterator end()

end( )

ofPixels_::const_iterator end()

getBitsPerChannel( )

size_t getBitsPerChannel()

This is how large each channel of a pixels is, ofPixels objects that store pixel data as unsigned char are smaller than ofPixels objects that store pixel data as floats. This returns bit, not bytes, so you'll probably see ofPixels<float


getBitsPerPixel( )

size_t getBitsPerPixel()

Get number of bits per pixel

If you have RGB pixel data, this will return 24, if you have RGBA, you'll have 32, if you have grayscale, this will return 8.


getBytesPerChannel( )

size_t getBytesPerChannel()

This is how large each channel of a pixels is, ofPixels objects that store pixel data as unsigned char are smaller than ofPixels objects that store pixel data as floats. This returns bytes, not bits, so you'll probably see ofPixels<float


getBytesPerPixel( )

size_t getBytesPerPixel()

Get the number of bytes per pixel

Returns the number of the pixels.


getBytesStride( )

size_t getBytesStride()

getConstLine( ... )

ofPixels_::ConstLine getConstLine(size_t line)

getConstLines( )

ofPixels_::ConstLines getConstLines()

getConstLines( ... )

ofPixels_::ConstLines getConstLines(size_t first, size_t numLines)

getConstPixelsIter( )

ofPixels_::ConstPixels getConstPixelsIter()

getData( )

PixelType * getData()

Retrieves pixel data from the ofPixel object.

Returns: A raw pointer to the pixel data.


getData( )

const PixelType * getData()

getHeight( )

size_t getHeight()

Get the height of the pixel array.

Returns the height of the pixels.


getImageType( )

ofImageType getImageType()

Get the type of the image

Returns: One of the following types: OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA

Returns what image type the ofPixels object is.


getLine( ... )

ofPixels_::Line getLine(size_t line)

getLines( )

ofPixels_::Lines getLines()

getLines( ... )

ofPixels_::Lines getLines(size_t first, size_t numLines)

getNumChannels( )

size_t getNumChannels()

Get the number of channels that the ofPixels object contains. RGB is 3 channels, RGBA is 4, and grayscale is 1.

This returns the number of channels that the ofPixels object contains. RGB is 3 channels, RGBA is 4, and grayscale is 1.


getNumPlanes( )

size_t getNumPlanes()

getPixelFormat( )

ofPixelFormat getPixelFormat()

getPixelIndex( ... )

size_t getPixelIndex(size_t x, size_t y)

Get the pixel index at a x,y position

ofColor yellow = ofColor::yellow;
size_t ind = pix.getPixelIndex(mouseX, mouseY);
pix.setPixel(ind, yellow);

This method gives you the index of the pixel at x,y. For instance:

ofColor yellow = ofColor::yellow;
int ind = pix.getPixelIndex(mouseX, mouseY);
pix.setColor(ind, yellow);

getPixelsIter( )

ofPixels_::Pixels getPixelsIter()

getTotalBytes( )

size_t getTotalBytes()

getWidth( )

size_t getWidth()

Get the width of the pixel array.

Returns the width of the pixels.


isAllocated( )

bool isAllocated()

Get whether memory has been allocated for an ofPixels object or not

Many operations like copying pixels, etc, automatically allocate the memory needed, but it's sometimes good to check.

Returns whether memory has been allocated for an ofPixels object or not. Many operations like copying pixels, etc, automatically allocate the memory needed, but it's sometimes good to check.


mirror( ... )

void mirror(bool vertically, bool horizontal)

Mirror the pixels across the vertical and/or horizontal axis.

Parameters:

vertically Set to true to mirror vertically

horizontal Set to true to mirror horizontal

This reflects the pixels across the vertical and/or horizontal axis.


ofPixels_( )

ofPixels_()

\name Construction And Allocation {


operator[]( ... )

const PixelType & operator[](size_t pos)

Provides access to each channel of each pixel. If you have RGB pixel data, then you'll have 3 values for each pixel, if you have RGBA, you'll have 4


operator[]( ... )

PixelType & operator[](size_t pos)

Provides access to each channel of each pixel. If you have RGB pixel data, then you'll have 3 values for each pixel, if you have RGBA, you'll have 4.


pixelBitsFromPixelFormat( ... )

size_t pixelBitsFromPixelFormat(ofPixelFormat format)

rbegin( )

ofPixels_::reverse_iterator rbegin()

rbegin( )

ofPixels_::const_reverse_iterator rbegin()

rend( )

ofPixels_::reverse_iterator rend()

rend( )

ofPixels_::const_reverse_iterator rend()

resize( ... )

bool resize(size_t dstWidth, size_t dstHeight, ofInterpolationMethod interpMethod=OF_INTERPOLATE_NEAREST_NEIGHBOR)

Resize the ofPixels instance to the dstHeight and dstWidth.

The options for the interpolation methods are as follows:

OF_INTERPOLATE_NEAREST_NEIGHBOR
OF_INTERPOLATE_BILINEAR
OF_INTERPOLATE_BICUBIC

This resizes the ofPixels instance to the dstHeight and dstWidth. The options for the interpolation methods are as follows: OF_INTERPOLATE_NEAREST_NEIGHBOR =1 OF_INTERPOLATE_BILINEAR =2 OF_INTERPOLATE_BICUBIC =3


rotate90( ... )

void rotate90(int nClockwiseRotations)

crop to a new width and height, this reallocates memory.


set( ... )

void set(size_t channel, PixelType val)

set( ... )

void set(PixelType val)

} \name Set Pixel Data {


setFromAlignedPixels( ... )

void setFromAlignedPixels(const PixelType *newPixels, size_t width, size_t height, size_t channels, size_t stride)

setFromAlignedPixels( ... )

void setFromAlignedPixels(const PixelType *newPixels, size_t width, size_t height, ofPixelFormat pixelFormat, size_t stride)

setFromAlignedPixels( ... )

void setFromAlignedPixels(const PixelType *newPixels, size_t width, size_t height, ofPixelFormat pixelFormat, int strides)

used to copy i420 pixels from gstreamer when (width % 4) != 0


setFromExternalPixels( ... )

void setFromExternalPixels(PixelType *newPixels, size_t w, size_t h, size_t channels)

setFromExternalPixels( ... )

void setFromExternalPixels(PixelType *newPixels, size_t w, size_t h, ofPixelFormat pixelFormat)

setFromPixels( ... )

void setFromPixels(const PixelType *newPixels, size_t w, size_t h, size_t channels)

setFromPixels( ... )

void setFromPixels(const PixelType *newPixels, size_t w, size_t h, ofPixelFormat pixelFormat)

setFromPixels( ... )

void setFromPixels(const PixelType *newPixels, size_t w, size_t h, ofImageType type)

setImageType( ... )

void setImageType(ofImageType imageType)

Changes the image type for the ofPixels object

Parameters:

imageType Can be one of the following: OF_IMAGE_GRAYSCALE, OF_IMAGE_COLOR, OF_IMAGE_COLOR_ALPHA


setNumChannels( ... )

void setNumChannels(size_t numChannels)

size( )

size_t size()

Get the number of values that the ofPixels object contains, so an RGB data 400x400 would be 480,000, whereas RGBA data of the same dimensions would be 640,000.

This gives you the number of values that the ofPixels object contains, so an RGB data 400x400 would be 480,000, whereas RGBA data of the same dimensions would be 640,000.


swapRgb( )

void swapRgb()

Swaps the R and B channels of an image, leaving the G and A channels as is.

As implemented right now, this method swaps the R and B channels of an image, leaving the G and A channels as is.


~ofPixels_( )

~ofPixels_()