LibGfx: Skip useless iterations during PNG::FilterType::Sub unfiltering

This commit is contained in:
Ryan Liptak 2022-08-18 02:29:36 -07:00 committed by Linus Groh
parent b19f3b5106
commit b123309b0d
Notes: sideshowbarker 2024-07-17 08:09:19 +09:00

View file

@ -183,8 +183,13 @@ static void unfilter_scanline(PNG::FilterType filter, Bytes scanline_data, Reado
switch (filter) {
case PNG::FilterType::Sub:
for (size_t i = 0; i < scanline_data.size(); ++i) {
u8 left = (i < bytes_per_complete_pixel) ? 0 : scanline_data[i - bytes_per_complete_pixel];
// This loop starts at bytes_per_complete_pixel because all bytes before that are
// guaranteed to have no valid byte at index (i - bytes_per_complete pixel).
// All such invalid byte indexes should be treated as 0, and adding 0 to the current
// byte would do nothing, so the first bytes_per_complete_pixel bytes can instead
// just be skipped.
for (size_t i = bytes_per_complete_pixel; i < scanline_data.size(); ++i) {
u8 left = scanline_data[i - bytes_per_complete_pixel];
scanline_data[i] += left;
}
break;