/*
 *  pixel_access.h
 *
 */

inline void get_rgb_pixel (IplImage* I, int x, int y, int& r, int& g, int& b)
{
    //CvScalar pt = cvGet2D(I, y, x); // slow but safe - also cvSet2D
    //r = (int)pt.val[2];  g = (int)pt.val[1];  b = (int)pt.val[0];
    //faster but less safe...
    //http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00053000000000000000
    unsigned char* pixel_ptr = (uchar *)(I->imageData + y*I->widthStep) + (x*I->nChannels + 0);
    r = (int)(pixel_ptr[2]); g = (int)(pixel_ptr[1]); b = (int)(pixel_ptr[0]);
}

inline void get_gs_pixel (IplImage* I, int x, int y, int& gs)
{
    //CvScalar pt = cvGet2D(I, y, x); // slow but safe // gs = (int)pt.val[0]; // see get_rgb_pixel
    unsigned char* pixel_ptr = (uchar *)(I->imageData + y*I->widthStep) + (x*1 + 0); // only 1 channel
    gs = (int)(pixel_ptr[0]);
}

inline void set_rgb_pixel (IplImage* I, int x, int y, int r, int g, int b)
{
    unsigned char* pixel_ptr = (uchar *)(I->imageData + y*I->widthStep) + (x*I->nChannels + 0);
    pixel_ptr[2] = (unsigned char)r;  pixel_ptr[1] = (unsigned char)g;  pixel_ptr[0] = (unsigned char)b;
}

inline void set_gs_pixel (IplImage* I, int x, int y, int gs)
{
    unsigned char* pixel_ptr = (uchar *)(I->imageData + y*I->widthStep) + (x*1 + 0); // only 1 channel
    pixel_ptr[0] = (unsigned char)gs;
}
