This is the algorithm I am using to flip an image in-place. Thought so far it works perfect, I would like if any one can spot any potential problems or give any points on improvement and optimization. Thanks
/**
* pixels_buffer - Pixels buffer to be operated
* width - Image width
* height - Image height
* bytes_per_pixel - Number of image components, ie: 3 for rgb, 4 rgba, etc...
**/
void flipVertically(unsigned char *pixels_buffer, const unsigned int width, const unsigned int height, const int bytes_per_pixel)
{
const unsigned int rows = height / 2; // Iterate only half the buffer to get a full flip
const unsigned int row_stride = width * bytes_per_pixel;
unsigned char* temp_row = (unsigned char*)malloc(row_stride);
int source_offset, target_offset;
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
source_offset = rowIndex * row_stride;
target_offset = (height - rowIndex - 1) * row_stride;
memcpy(temp_row, pixels_buffer + source_offset, row_stride);
memcpy(pixels_buffer + source_offset, pixels_buffer + target_offset, row_stride);
memcpy(pixels_buffer + target_offset, temp_row, row_stride);
}
free(temp_row);
temp_row = NULL;
}
malloc
in C. – Lstor Aug 11 '13 at 22:17