From 36daeee34ff04f64c933e94a9cdffe9080061fb0 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Tue, 1 Dec 2020 22:21:16 +0100 Subject: [PATCH] LibGfx: Fix BMP mask detection off-by-one Also, since the loops can be replaced by a little bit-twiddling, call ctz() directly. This might be a bit faster, or it might not. --- Libraries/LibGfx/BMPLoader.cpp | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/Libraries/LibGfx/BMPLoader.cpp b/Libraries/LibGfx/BMPLoader.cpp index 72bad19642..be05b1c670 100644 --- a/Libraries/LibGfx/BMPLoader.cpp +++ b/Libraries/LibGfx/BMPLoader.cpp @@ -343,10 +343,10 @@ static void populate_dib_mask_info(BMPLoadingContext& context) return; // Mask shift is the number of right shifts needed to align the MSb of the - // mask to the MSb of the LSB. + // mask to the MSb of the LSB. Note that this can be a negative number. // Mask size is the number of set bits in the mask. This is required for - // color scaling (for example, ensuring that a 4-bit color value spans the - // entire 256 value color spectrum. + // color scaling (for example, ensuring that a 4-bit color value spans the + // entire 256 value color spectrum. auto& masks = context.dib.info.masks; auto& mask_shifts = context.dib.info.mask_shifts; auto& mask_sizes = context.dib.info.mask_sizes; @@ -361,29 +361,15 @@ static void populate_dib_mask_info(BMPLoadingContext& context) for (size_t i = 0; i < masks.size(); ++i) { u32 mask = masks[i]; - u8 shift = 0; - u8 size = 0; - bool found_set_bit = false; - - while (shift <= 32) { - u8 bit = (mask >> shift) & 0x1; - if (found_set_bit) - size++; - if (!found_set_bit && bit) { - found_set_bit = true; - } else if (found_set_bit && !bit) { - break; - } - shift++; - } - - if (shift > 32) { + if (!mask) { mask_shifts.append(0); mask_sizes.append(0); - } else { - mask_shifts.append(shift - 8); - mask_sizes.append(size); + continue; } + int trailing_zeros = count_trailing_zeroes_32(mask); + int size = count_trailing_zeroes_32(~(mask >> trailing_zeros)); + mask_shifts.append(trailing_zeros - 8); + mask_sizes.append(size); } }