( biWidth*biBitCount+31)/32*4 或 ((width * depth + 31) >> 5) << 2
一个扫描行所占的字节数
biWidth 是图像的宽度以像素为单位
biBitCount 是每个像素的位数
1.数字图像处理,BMP图像操作( biWidth*biBitCount+31)/32*4
http://blog.csdn.net/lscan/article/details/14647979
准备知识:
位:"位(bit)"是电子计算机中最小的数据单位。每一位的状态只能是0或1,是计算机处理、存储、传输数据时使用的二进制格式。
字节:8个二进制位构成1个"字节(Byte)",它是存储空间的基本计量单位。1个字节可以储存1个英文字母或者半个汉字,换句话说,1个汉字占据2个字节的存储空间。
像素:
位图的一个像素值所占的字节数:
当biBitCount=1时,8个像素占1个字节;
当biBitCount=4时,2个像素占1个字节;
当biBitCount=8时,1个像素占1个字节;
当biBitCount=24时,1个像素占3个字节,此时图像为真彩色图像。
即:1个像素所占的字节数是biBitCount/8
解决:
Windows规定图像文件中一个扫描行所占的字节数必须是4的倍数(即以字为单位),不足的以0填充,图像文件中一个扫描行所占的字节数计算方法:
( biWidth*biBitCount+31)/32*4 一个扫描行所占的字节数
biWidth 是图像的宽度以像素为单位,
biBitCount 是每个像素的位数,
biWidth*biBitCount 是一行所占的位数
( biWidth*biBitCount+31) 把不满4字节的补满,使最终结果得出来的位数只会比原来的多(结尾有余数,不满4字节),或者不变(刚好以4个字节(32位)结束)
( biWidth*biBitCount+31)/32 分成一块块4字节(32位)
( biWidth*biBitCount+31)/32*4 前面对齐4字节结束。得出字节总和
2.Capturing an Image
https://msdn.microsoft.com/en-us/library/windows/desktop/dd183402%28v=vs.85%29.aspx
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
3.QT qimage.cpp
const int bytes_per_line = ((width * depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4)
QImageData * QImageData::create(const QSize &size, QImage::Format format)
{
if (!size.isValid() || format == QImage::Format_Invalid)
return 0; // invalid parameter(s)
uint width = size.width();
uint height = size.height();
uint depth = qt_depthForFormat(format);
const int bytes_per_line = ((width * depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4)
// sanity check for potential overflows
if (INT_MAX/depth < width
|| bytes_per_line <= 0
|| height <= 0
|| INT_MAX/uint(bytes_per_line) < height
|| INT_MAX/sizeof(uchar *) < uint(height))
return 0;
QScopedPointer<QImageData> d(new QImageData);
switch (format) {
case QImage::Format_Mono:
case QImage::Format_MonoLSB:
d->colortable.resize(2);
d->colortable[0] = QColor(Qt::black).rgba();
d->colortable[1] = QColor(Qt::white).rgba();
break;
default:
break;
}
d->width = width;
d->height = height;
d->depth = depth;
d->format = format;
d->has_alpha_clut = false;
d->is_cached = false;
d->bytes_per_line = bytes_per_line;
d->nbytes = d->bytes_per_line*height;
d->data = (uchar *)malloc(d->nbytes);
if (!d->data) {
return 0;
}
d->ref.ref();
return d.take();
}