uint8 getValue(IplImage *input,int x, int y) { uint8 *iData = (uint8 *)input->imageData; uint8 retval; if((x < input->width) && (x >= 0) && (y < input->height) && (y >= 0)) { // inside bounds retval = iData[(y * input->widthStep) + x]; } else if(x < 0) { if(y < 0) { // diagonal top left retval = iData[0]; } else if(y >= input->height) { // diagonal bottom left retval = iData[(input->height - 1) * input->widthStep]; } else { // off left retval = iData[(y * input->widthStep)]; } } else if(x >= input->width) { if(y < 0) { // diagonal top right retval = iData[input->width - 1]; } else if(y >= input->height) { // diagonal bottom right retval = iData[((input->height - 1) * input->widthStep) + (input->width - 1)]; } else { // off right retval = iData[(y * input->widthStep) + (input->width - 1)]; } } else if(y < 0) { // off top retval = iData[x]; } else if(y >= input->height) { // off bottom retval = iData[((input->height - 1) * input->widthStep) + x]; } return retval; }