Trying to read a c program

A

Andreas

Hi!

I'm not very familiar to C, but I do know a little.
No I'm trying to read someone elses program, and this line (and
simular, less complex lines) realy confuses me. Can someone explain?
I'm not new to the concept of pointers, so I think that I have a fair
chance. :)

What is this line doing?!?:
srcptr=(MY_PIXEL*)(source->imageData+(top-j)*source->widthStep)+(left-i);

Some header:
#define MY_PIXEL unsigned char
//Only the intresting variabels.
typedef struct _IplImage {
char *imageData; ///< Pointer to aligned image
int widthStep; ///< The size of aligned line in
bytes
...
} IplImage;
int i,j
int left,top;
MY_PIXEL *srcptr;
IplImage *source; // standard Ipl.

best regards,
Andreas Lundgren
 
M

Matt Gregory

Andreas said:
Hi!

I'm not very familiar to C, but I do know a little.
No I'm trying to read someone elses program, and this line (and
simular, less complex lines) realy confuses me. Can someone explain?
I'm not new to the concept of pointers, so I think that I have a fair
chance. :)

What is this line doing?!?:
srcptr=(MY_PIXEL*)(source->imageData+(top-j)*source->widthStep)+(left-i);

It's using source->imageData as the start of a 2D array and casting the
entry's address from a pointer to a char to a pointer to an unsigned char,
basically. The arithmetic part is equivalent to:

int x = left - i;
int y = (top - j) * source->widthStep;
source->imageData[x][y];

Or maybe it's [y][x], I always get those confused. In any case, it's
converting a coordinate that's relative to some sub-area of the image
to a coordinate that's relative to the upper-left corner of the entire
image.

Matt Gregory
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top