FCam::Image Class Reference

A reference-counted Image object. More...

#include <Image.h>

Collaboration diagram for FCam::Image:

Collaboration graph
[legend]

List of all members.

Public Member Functions

const Imageoperator= (const Image &other)
 Become a new reference to an existing image.
void setData (unsigned char *)
 Make the image refer to data stored elsewhere.
void debug () const
 Print out some debugging info about the image.
bool valid ()
 Test if it's safe to dereference the image data.
bool lock (int timeout=-1)
 Lock the image to prevent other threads writing to it with a configurable timeout in microseconds.
void unlock ()
 Unlock the image.
Allocating Constructors
These image constructors allocate new memory for image data.

 Image (Size, ImageFormat)
 Construct a new image of the given size in the given format.
 Image (int, int, ImageFormat)
 Construct a new image of the given size in the given format.
Non-allocating Constructors
These image constructors make a new reference to already existing image data.

 Image (Size, ImageFormat, unsigned char *)
 Construct a new image of the given size in the given format using already existing data.
 Image (int, int, ImageFormat, unsigned char *)
 Construct a new image of the given size in the given format using already existing data.
 Image ()
 Construct a new image with no memory allocated.
 Image (const Image &other)
 Make a copy of the image reference.

Public Attributes

Size size
 The size of the image in pixels.
ImageFormat type
 The format of the image data.
unsigned char *const data
 A pointer to the image data.

Static Public Attributes

static unsigned char * Discard = (unsigned char *)(0)
 This value for data represents an image with no allocated memory that should never have data copied into it.
static unsigned char * AutoAllocate = (unsigned char *)(-1)
 This value for data represents image data whose first user should allocate memory for.


Detailed Description

A reference-counted Image object.

If you instantiate an image using one of the constructors that allocates data, the result will be a reference counted image object, that automatically deletes the data when the last reference to it is destroyed.

If you use a constructor that sets the data field, you're telling the image class that someone else is managing that memory, and the destructor will never be called.

Definition at line 24 of file Image.h.


Constructor & Destructor Documentation

FCam::Image::Image ( Size  s,
ImageFormat  f,
unsigned char *  d 
)

Construct a new image of the given size in the given format using already existing data.

The image will never deallocate this data. This is a good way to cast some other image type to FCam's image type without allocating new data or copying image data.

Definition at line 32 of file Image.cpp.

FCam::Image::Image ( int  w,
int  h,
ImageFormat  f,
unsigned char *  d 
)

Construct a new image of the given size in the given format using already existing data.

The image will never deallocate this data. This is a good way to cast some other image type to FCam's image type without allocating new data or copying image data.

Definition at line 42 of file Image.cpp.

FCam::Image::Image ( const Image other  ) 

Make a copy of the image reference.

Doesn't copy image data, just produces a new reference to the same data.

Definition at line 56 of file Image.cpp.


Member Function Documentation

const Image & FCam::Image::operator= ( const Image other  ) 

Become a new reference to an existing image.

This never copies image data. It just produces a new reference to the same data.

Definition at line 64 of file Image.cpp.

void FCam::Image::setData ( unsigned char *  d  ) 

Make the image refer to data stored elsewhere.

This is useful for wrapping image data allocated by another library inside an FCam image (e.g. the framebuffer of an overlay, or the image object of another image processing library). This data will not be reference counted, and hence will never be deleted by this object.

You can also use this method to set the data to NULL, Image::Discard, or Image::AutoAllocate, or even to make a weak reference to another image's data.

Definition at line 84 of file Image.cpp.

bool FCam::Image::lock ( int  timeout = -1  ) 

Lock the image to prevent other threads writing to it with a configurable timeout in microseconds.

Returns whether the lock was acquired.

The default timeout (-1) indicates you should wait indefinitely, and should always return true. A timeout of zero will not block at all.

Images have locks to coordinate access between multiple threads. When new frames come in, the FCam API will try to lock the target image for them with a short timeout and copy the data in. If the image is still locked at the end of the timeout, it will instead drop the data on the floor (avoiding the expensive memcpy). You can use this for rate control of a viewfinder like so:

	 Shot viewfinder;
	 ...
	 viewfinder.image = Image(640, 480, UYUV);
	 ...
	 sensor.stream(viewfinder);
	 while (1) {
	   // Get the most recent viewfinder frame with image data
	   Frame::Ptr f;
	   do {
             f = sensor.getFrame();
	     // feed the frame to the autoexposure and autofocus routines
	     ...
           } while (sensor.framesPending() || !f || !f->image.valid());
	   
	   f->image.lock();
	   // Do something expensive with the image. 
	   // No memcpys into the image will take place during this time.
	   f->image.unlock();
         }	
	 

If you absolutely want to save every piece of image data, you should set the target image to an AutoAllocated one, Even the example above doesn't guarantee image data hasn't been clobbered just before you call lock(). It only guarantees you get one consistent image and not a half-written one. If you spend all your CPU time with the frame locked, you may starve the FCam runtime of chances to give you new image data.

Your image may still get clobbered if other threads don't respect the lock, or if the image is a weak reference, and other independently created weak references to the same data exist are being used to write to it. Best to only have one image that refers to external data (images created by other libraries, framebuffers, etc), and construct other references to that data using copies of the first image.

Definition at line 118 of file Image.cpp.

void FCam::Image::unlock (  ) 

Unlock the image.

See lock for details.

Definition at line 141 of file Image.cpp.


Member Data Documentation

The size of the image in pixels.

Be careful assigning to this - if there is existing data it will result in a reinterpretation of it.

Definition at line 31 of file Image.h.

The format of the image data.

Be careful assigning this - it will result in a reinterpretation of the existing data.

Definition at line 38 of file Image.h.

unsigned char* const FCam::Image::data

A pointer to the image data.

Don't assign to this (use setData instead).

Definition at line 44 of file Image.h.

unsigned char * FCam::Image::Discard = (unsigned char *)(0) [static]

This value for data represents an image with no allocated memory that should never have data copied into it.

It is equal to NULL.

Definition at line 51 of file Image.h.

unsigned char * FCam::Image::AutoAllocate = (unsigned char *)(-1) [static]

This value for data represents image data whose first user should allocate memory for.

If used as the image field of a Shot, it indicates that a new buffer should be allocated for every new frame.

Definition at line 58 of file Image.h.


The documentation for this class was generated from the following files:

Generated on Mon Jan 18 20:48:13 2010 for FCam by  doxygen 1.5.6