Returns the byte length of a scan line padded as necessary to be divisible * by four. For example, 3 pixels wide at 24 bpp would yield 12 (3 pixels * 3 * bytes each = 9 bytes, padded by 3 to the next multiple of 4). bpp is *bits* * per pixel, not bytes. Returns 0 in case of overflow. */
| 513 | * per pixel, not bytes. Returns 0 in case of overflow. |
| 514 | */ |
| 515 | static size_t GetLineLength(size_t width, size_t bpp) |
| 516 | { |
| 517 | size_t bits = width * bpp; |
| 518 | size_t pad_bits = (32 - (bits & 0x1f)) & 0x1f; /* x & 0x1f == x % 32 */ |
| 519 | |
| 520 | /* Check for overflow, in both the above multiplication and the below |
| 521 | * addition. It's well defined to do this in any order relative to the |
| 522 | * operations themselves (since size_t is unsigned), so we combine the |
| 523 | * checks into one if. bpp has been checked for being nonzero elsewhere. |
| 524 | */ |
| 525 | if(!CanMultiply(width, bpp) || !CanAdd(bits, pad_bits)) return 0; |
| 526 | |
| 527 | /* Convert to bytes. */ |
| 528 | return (bits + pad_bits) / 8; |
| 529 | } |
| 530 | |
| 531 | /* Reads and validates the bitmap header metadata from the context's file |
| 532 | * object. Assumes the file pointer is at the start of the file. Returns 1 if |
no test coverage detected