- /* biSizeImage is the size of the raw bitmap data.
- * A dummy 0 can be given for BI_RGB bitmaps.
- * https://en.wikipedia.org/wiki/BMP_file_format */
- if ((bmih.biCompression == BI_RGB) && (bmih.biSizeImage == 0))
- {
- bmih.biSizeImage = ((bmih.biWidth * bmih.biBitCount + 31) / 32) * 4 *
- (bmih.biHeight / 2);
- }
+ /* biSizeImage is the size of the raw bitmap data.
+ * A dummy 0 can be given for BI_RGB bitmaps.
+ * https://en.wikipedia.org/wiki/BMP_file_format */
+ if (bi.biSizeImage == 0 || bi.biSize == sizeof(BITMAPCOREHEADER))
+ {
+ /* Calculate image size */
+#define WIDTHBYTES(width, bits) (((width) * (bits) + 31) / 32 * 4)
+ bi.biSizeImage = WIDTHBYTES(bi.biWidth, bi.biBitCount) * (bi.biHeight / 2);
+ bi.biSizeImage += WIDTHBYTES(bi.biWidth, 1) * (bi.biHeight / 2);
+#undef WIDTHBYTES
+ }
+ /* Calculate total size */
+ cbTotal = bi.biSize + cbColorTable + bi.biSizeImage;