这是我关于将屏幕截图保存到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。

c&#43;&#43; - 通过SOIL保存位图时,BMP损坏。屏幕截图区域-LMLPHP

c&#43;&#43; - 通过SOIL保存位图时,BMP损坏。屏幕截图区域-LMLPHP

我检查了两次转换和代码两次,但是在我看来,原因是错误的blit函数。 RGB转换功能不影响问题。可能是什么原因?这是在BitBlt中分配区域的正确方法吗?

更新偶数或奇数没有区别。当此数字相等时,将产生正确的图像。我不知道哪里有问题。((

更新2

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
}

外档功能:
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;
}

最佳答案

损坏的位图图像是Windows位图与SOIL库期望的结果之间数据布局不一致的结果。从CreateDIBSection返回的像素缓冲区遵循Windows规则(请参阅Bitmap Header Types):



换句话说:每条扫描线的宽度(以字节为单位)为(biWidth * (biBitCount / 8) + 3) & ~3。另一方面,SOIL库不希望像素缓冲区与DWORD对齐。

为了解决这个问题,像素数据需要先进行转换,然后再剥除(潜在的)填充并交换R和B颜色通道,以将其传输到SOIL。以下代码在place2中执行此操作:

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明显的迹象是扫描线向左或向右移动一个或两个像素(或各个颜色通道的速度不同)。通常这是一个安全的指示,即扫描线对齐方式存在分歧。
2此操作具有破坏性,即即使转换了原始数据(即使涉及更多),也无法将像素缓冲区一旦转换后再传递给Windows GDI函数。

关于c++ - 通过SOIL保存位图时,BMP损坏。屏幕截图区域,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44219764/

10-11 16:57