MASH logo
Login | Register

Class: Image

From MASH Wiki

Jump to: navigation, search

The Image class is declared in mash/image.h.


Contents


Representations of an image

The Image class can store several different representations of a single image. Currently, the following are supported:

  • RGB: Color image, each pixel is stored in RGB (24 bits) format
  • Grayscale: Grayscale image, each pixel is stored on 8 bits


The Image class

Here are the relevant part of the declaration of the class. See below for usage examples

//--------------------------------------------------------------------------
// Represents a byte
//--------------------------------------------------------------------------
typedef unsigned char byte_t;
 
 
//--------------------------------------------------------------------------
// Represents a RGB pixel
//--------------------------------------------------------------------------
struct RGBPixel_t
{
byte_t r;
byte_t g;
byte_t b;
};
 
 
class Image
{
...
 
public:
//----------------------------------------------------------------------
// Returns the width of the image, in pixels
//----------------------------------------------------------------------
unsigned int width() const;
 
//----------------------------------------------------------------------
// Returns the height of the image, in pixels
//----------------------------------------------------------------------
unsigned int height() const;
 
//----------------------------------------------------------------------
// Returns a pointer to the pixels in RGB (24 bits) format
//----------------------------------------------------------------------
RGBPixel_t* rgbBuffer() const;
 
//----------------------------------------------------------------------
// Returns an array of pointers to the lines of the RGB (24 bits) pixel
// buffer
//----------------------------------------------------------------------
RGBPixel_t** rgbLines() const;
 
//----------------------------------------------------------------------
// Returns a pointer to the pixels in grayscale (8 bits) format
//----------------------------------------------------------------------
byte_t* grayBuffer() const;
 
//----------------------------------------------------------------------
// Returns an array of pointers to the lines of the grayscale pixel
// buffer
//----------------------------------------------------------------------
byte_t** grayLines() const;
 
....
};


How to retrieve the value of a grayscale pixel

There's two ways to access the pixels:

  • Use a one-dimensional array of <image width>x<image height> pixels. This forces you to compute the offset of the desired pixel like that:
// Access the grayscale pixel at (x, y)
byte_t* pBuffer = image->grayBuffer();
byte_t mypixel = pBuffer[y * image->width() + x];
  • Use an array of pointers to the lines of the image. This allows you to write a shorter code like:
// Access the grayscale pixel at (x, y)
byte_t** pLines = image->grayLines();
byte_t mypixel = pLines[y][x];


How to retrieve the value of a RGB pixel

Like for the grayscale case above, there's two ways to access the pixels:

  • Use a one-dimensional array of <image width>x<image height> pixels:
// Access the RGB pixel at (x, y)
RGBPixel_t* pBuffer = image->rgbBuffer();
RGBPixel_t mypixel = pBuffer[y * image->width() + x];
  • Use an array of pointers to the lines of the image:
// Access the RGB pixel at (x, y)
RGBPixel_t** pLines = image->rgbLines();
RGBPixel_t mypixel = pLines[y][x];
Personal