这是我关于将屏幕截图保存到SOIL的最后一个问题的延续。here现在我想知道,如何制作屏幕部分的屏幕截图并消除奇怪行为的原因。我的代码:
bool saveTexture(string path, glm::vec2 startPos, glm::vec2 endPos)
{
const char *charPath = path.c_str();
GLuint widthPart = abs(endPos.x - startPos.x);
GLuint heightPart = abs(endPos.y - startPos.y);
BITMAPINFO bmi;
auto& hdr = bmi.bmiHeader;
hdr.biSize = sizeof(bmi.bmiHeader);
hdr.biWidth = widthPart;
hdr.biHeight = -1.0 * heightPart;
hdr.biPlanes = 1;
hdr.biBitCount = 24;
hdr.biCompression = BI_RGB;
hdr.biSizeImage = 0;
hdr.biXPelsPerMeter = 0;
hdr.biYPelsPerMeter = 0;
hdr.biClrUsed = 0;
hdr.biClrImportant = 0;
unsigned char* bitmapBits = (unsigned char*)malloc(3 * widthPart * heightPart);
HDC hdc = GetDC(NULL);
HDC hBmpDc = CreateCompatibleDC(hdc);
HBITMAP hBmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&bitmapBits, nullptr, 0);
SelectObject(hBmpDc, hBmp);
BitBlt(hBmpDc, 0, 0, widthPart, heightPart, hdc, startPos.x, startPos.y, SRCCOPY);
//UPDATE:
- int bytes = widthPart * heightPart * 3;
- // invert R and B chanels
- for (unsigned i = 0; i< bytes - 2; i += 3)
- {
- int tmp = bitmapBits[i + 2];
- bitmapBits[i + 2] = bitmapBits[i];
- bitmapBits[i] = tmp;
- }
+ unsigned stride = (widthPart * (hdr.biBitCount / 8) + 3) & ~3;
+ // invert R and B chanels
+ for (unsigned row = 0; row < heightPart; ++row) {
+ for (unsigned col = 0; col < widthPart; ++col) {
+ // Calculate the pixel index into the buffer, taking the
alignment into account
+ const size_t index{ row * stride + col * hdr.biBitCount / 8 };
+ std::swap(bitmapBits[index], bitmapBits[index + 2]);
+ }
+ }
int texture = SOIL_save_image(charPath, SOIL_SAVE_TYPE_BMP, widthPart, heightPart, 3, bitmapBits);
return texture;
}
如果我运行此操作,如果widthPart和heightPart是偶数,那就完美了。但是,如果这个数字是奇数,我会得到这个BMP。:
我检查了任何转换和代码两次,但在我看来,原因在于我错误的blit函数。转换RGB的功能不会影响问题。可能是什么原因?这是BitBlt中区域的正确方式吗?
更新偶数或奇数无差异。当这个数字相等时,产生正确的图像。我不知道问题出在哪里。((
UPDATE2
SOIL_save_image 函数检查错误参数并发送到 stbi_write_bmp :
int stbi_write_bmp(char *filename, int x, int y, int comp, void *data)
{
int pad = (-x*3) & 3;
return outfile(filename,-1,-1,x,y,comp,data,0,pad,
"11 4 22 4" "4 44 22 444444",
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
}
outfile 功能:
static int outfile(char const *filename, int rgb_dir, int vdir, int x, int
y, int comp, void *data, int alpha, int pad, char *fmt, ...)
{
FILE *f = fopen(filename, "wb");
if (f) {
va_list v;
va_start(v, fmt);
writefv(f, fmt, v);
va_end(v);
write_pixels(f,rgb_dir,vdir,x,y,comp,data,alpha,pad);
fclose(f);
}
return f != NULL;
}
答案 0 :(得分:4)
破坏的位图图像是Windows位图与SOIL库期望 1 之间数据布局不一致的结果。从CreateDIBSection
返回的像素缓冲区遵循Windows规则(请参阅Bitmap Header Types):
扫描线是DWORD对齐的[...]。它们必须填充扫描线宽,以字节为单位,不能被四[...]整除。
换句话说:每条扫描线的宽度(以字节为单位)为(biWidth * (biBitCount / 8) + 3) & ~3
。另一方面,SOIL库不希望像素缓冲区与DWORD对齐。
为了解决这个问题,需要在传递到SOIL之前转换像素数据,方法是剥离(潜在)填充并交换R和B颜色通道。以下代码就地 2 :
unsigned stride = (widthPart * (hdr.biBitCount / 8) + 3) & ~3;
for (unsigned row = 0; row < heightPart; ++row) {
for (unsigned col = 0; col < widthPart; ++col) {
// Calculate the source pixel index, taking the alignment into account
const size_t index_src{ row * stride + col * hdr.biBitCount / 8 };
// Calculate the destination pixel index (no alignment)
const size_t index_dst{ (row * width + col) * (hdr.biBitCount / 8) };
// Read color channels
const unsigned char b{ bitmapBits[index_src] };
const unsigned char g{ bitmapBits[index_src + 1] };
const unsigned char r{ bitmapBits[index_src + 2] };
// Write color channels switching R and B, and remove padding
bitmapBits[index_dst] = r;
bitmapBits[index_dst + 1] = g;
bitmapBits[index_dst + 2] = b;
}
}
使用此代码,index_src
是像素缓冲区的索引,其中包括填充以强制执行正确的DWORD对齐。 index_dst
是未应用任何填充的索引。将像素从index_src
移动到index_dst
会移除(潜在)填充。
<小时/> 1 告示标志是扫描线向左或向右移动一个或两个像素(或不同速度的单个颜色通道)。这通常是一个安全的指示,即扫描线对齐存在分歧。