Skip to content
Myr

stb_image_write.h: Fix jpg flipping for non-multiple-of-8 sizes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

JPG always encodes 8x8 pixel blocks. If the input image does not have a width or height that's a multiple of 8, the last column or row is just used multiple times for the remaining pixels of the block. The original code first calculated p (the index into the pixel data) with the "imaginary" row/colum (that might be up to 7 pixels too far into each direction) and then subtracted the necessary amount of bytes it if row >= height or col >= width. That was a bit cryptic (IMHO), and didn't get more readable/obvious when vertical flipping was added - which introduced a bug, by not taking stbi__flip_vertically_on_write into account when adjusting p for row >= height... The code should be more obvious (and less buggy) now. This fixes bug #592

authored by Daniel Gibson · committed by Daniel Gibson · parent e6afb9c · Browse files · 1 file+8 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+8 added−6 removed

  • other:+8 added−6 removed

1 file+8 added−6 removed

modified:stb_image_write.h+8 added-6 removed
1422 unmodified lines
1423 for(x = 0; x < width; x += 8) {1424 float YDU[64], UDU[64], VDU[64];1425 for(row = y, pos = 0; row < y+8; ++row) {1426 for(col = x; col < x+8; ++col, ++pos) {1427 int p = (stbi__flip_vertically_on_write ? height-1-row : row)*width*comp + col*comp;1427 float r, g, b;1429 if(row >= height) {1430 p -= width*comp*(row+1 - height);1428 int p;1429 if(row < height) {1430 p = (stbi__flip_vertically_on_write ? (height-1-row) : row)*width*comp;1431 } else {1432 // row >= height => use last input row (=> first if flipping)1433 p = stbi__flip_vertically_on_write ? 0 : ((height-1)*width*comp);1434 }1432 if(col >= width) {1433 p -= comp*(col+1 - width);1434 }1435 // if col >= width => use pixel from last input column1436 p += ((col < width) ? col : (width-1))*comp;14371438 r = imageData[p+0];1439 g = imageData[p+ofsG];1440 b = imageData[p+ofsB];
130 unmodified lines