I'm having some weird issues using libjpeg-turbo, in particular I have a 1920x1080 rgba buffer that I'm reading via mmap (so it's read only) that I'm trying to encode as a jpeg, but I'd only like to encode the top left corner of say, 320x568 pixels (and some other sizes). There's some overhead associated with memcpying those pixels that I'm trying to avoid, so I'm trying to set the width and height in cinfo to 320x568 and then manually pass to jpeg_write_scanlines only the appropriate lines.
Here's an image showing what I mean: https://i.stack.imgur.com/OI5Z3.png
My hope is that it will take the first 320 pixels of each of the only 568 lines I am passing it from the 1920x1080 buffer.
JSAMPROW row_pointer[1];
int row_stride = 1920 * bpp;
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &in[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
So I'm setting up the jpeg compress as 320x568 and passing it a 1920x1080 buffer, and then telling it specifically to skip the pointer 1920x4bpp every line.
It only half works though, I get an image that looks correct for the first ~94 rows or so and then the rest is garbage. Does anyone have any idea why this doesn't work?