So I've been trying to convert some cpp to rust (still learning ) I tried a couple times but it doesn't seem to work and I'm just scratching my head
void flipPixels(GLubyte *pixels, size_t bytesPerRow, size_t rows) {
if (!pixels) {
return;
}
GLuint middle = (GLuint)rows / 2;
GLuint intsPerRow = (GLuint)bytesPerRow / sizeof(GLuint);
GLuint remainingBytes = (GLuint)bytesPerRow - intsPerRow * sizeof(GLuint);
for (GLuint rowTop = 0, rowBottom = (GLuint)rows - 1; rowTop < middle; ++rowTop, --rowBottom) {
// Swap in packs of sizeof(GLuint) bytes
GLuint *iTop = (GLuint *)(pixels + rowTop * bytesPerRow);
GLuint *iBottom = (GLuint *)(pixels + rowBottom * bytesPerRow);
GLuint iTmp;
GLuint n = intsPerRow;
do {
iTmp = *iTop;
*iTop++ = *iBottom;
*iBottom++ = iTmp;
} while (--n > 0);
// Swap remainder bytes
GLubyte *bTop = (GLubyte *)iTop;
GLubyte *bBottom = (GLubyte *)iBottom;
GLubyte bTmp;
switch (remainingBytes) {
case 3:
bTmp = *bTop;
*bTop++ = *bBottom;
*bBottom++ = bTmp;
case 2:
bTmp = *bTop;
*bTop++ = *bBottom;
*bBottom++ = bTmp;
case 1:
bTmp = *bTop;
*bTop = *bBottom;
*bBottom = bTmp;
}
}
}
My first attempt
fn flip_in_place(pixels: *mut u8, bytes_per_row: i32, height: i32) {
let middle: u32 = (height / 2) as u32;
let ints_per_row = bytes_per_row / std::mem::size_of::<u32>() as i32;
let remaining_bytes = bytes_per_row - ints_per_row * std::mem::size_of::<u32>() as i32;
let mut row_top = 0;
let mut row_bottom = height - 1;
unsafe {
for _ in 0..middle {
let mut i_top = pixels.offset((row_top * bytes_per_row) as isize);
let mut i_bottom = pixels.offset((row_bottom * bytes_per_row) as isize);
let mut i_tmp;
let mut n = ints_per_row;
loop {
i_tmp = i_top;
i_top = i_top.offset(1);
*i_top = *i_bottom;
i_bottom = i_bottom.offset(1);
*i_bottom = *i_tmp;
n -= 1;
if n == 0 {
break;
}
}
let mut b_top = i_top;
let mut b_bottom = i_bottom;
let mut b_tmp;
match remaining_bytes {
3 => {
b_tmp = b_top;
b_top = b_top.offset(1);
*b_top = *b_bottom;
b_bottom = b_bottom.offset(1);
*b_bottom = *b_tmp;
}
2 => {
b_tmp = b_top;
b_top = b_top.offset(1);
*b_top = *b_bottom;
b_bottom = b_bottom.offset(1);
*b_bottom = *b_tmp;
}
1 => {
b_tmp = b_top;
*b_top = *b_bottom;
*b_bottom = *b_tmp;
}
_ => {}
}
row_top += 1;
row_bottom -= 1;
}
}
}
There were others but feeling lost at this point
Thanks in advance ... oh the method is for
UNPACK_FLIP_Y_WEBGL