我现在正在使用OpenGL和PCL_lib,我想在背景图片上画一个配件。
这是我显示的output_image:
图像源是PCD格式(PCL lib),由OpenGL绘制
以下是我保存GL_pic的代码:
char filename[100];
sprintf(filename, "path", _mframe_Count);
// Set Parameter
GLint x, y, width, height;
int i;
unsigned long imageSize;
GLenum lastBuffer
glGetIntegerv( GL_VIEWPORT, mViewPort );
x = mViewPort[0];
y = mViewPort[1];
width = mViewPort[2];
height = mViewPort[3];
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
bmfh.bfType = 0x4D42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih);
imageSize = ((width+((4-(width%4))%4))*height*3)+2;
bmfh.bfSize = imageSize + sizeof(bmfh) + sizeof(bmih);
GLubyte *pImageData = (GLubyte *)malloc(imageSize);
// Allocate apicture buffer
if(pImageData == NULL)
{
printf("[Error] : grab_GL(), Cannot create Memory_Sapce for pImageData.\n");
return false;
}
// Byte alignment (that is, no alignment)
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ROW_LENGTH,0);
glPixelStorei(GL_PACK_SKIP_ROWS,0);
glPixelStorei(GL_PACK_SKIP_PIXELS,0);
glPixelStorei(GL_PACK_SWAP_BYTES,1);
glGetIntegerv(GL_READ_BUFFER, ( GLint *)&lastBuffer);
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pImageData);
pImageData[imageSize-1] = 0;
pImageData[imageSize-2] = 0;
glReadBuffer(lastBuffer);
FILE *pWritingFile = fopen(filename, "wb");
if (pWritingFile == NULL)
{
free(pImageData);
printf("[Error] : grab_GL(), Cannot open %s for writting.\n", filename);
return false;
}
// BMP Structures
bmih.biSize = sizeof(bmih);
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = 0;
bmih.biSizeImage = imageSize;
bmih.biXPelsPerMeter = 2835;
bmih.biYPelsPerMeter = 2835;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
// BMP Compress
fwrite(&bmfh, 8, 1, pWritingFile);
fwrite(&bmfh.bfReserved2, sizeof(bmfh.bfReserved2), 1, pWritingFile);
fwrite(&bmfh.bfOffBits, sizeof(bmfh.bfOffBits), 1, pWritingFile);
fwrite(&bmih, sizeof(bmih), 1, pWritingFile);
fwrite(pImageData, imageSize, 1, pWritingFile);
// End The Process
fclose(pWritingFile);
free(pImageData);
return true;
但是当将图像保存到显示屏上显示的BMP文件时,它变为:
答案 0 :(得分:1)
看起来写入磁盘有点难以理解:
// BMP Compress
fwrite(&bmfh, 8, 1, pWritingFile);
fwrite(&bmfh.bfReserved2, sizeof(bmfh.bfReserved2), 1, pWritingFile);
fwrite(&bmfh.bfOffBits, sizeof(bmfh.bfOffBits), 1, pWritingFile);
fwrite(&bmih, sizeof(bmih), 1, pWritingFile);
fwrite(pImageData, imageSize, 1, pWritingFile);
我不明白为什么你要分别编写bmfh
的元素,所以只需要确保一切都清楚:
// BMP Compress
fwrite(&bmfh, sizeof(bmfh), 1, pWritingFile);
fwrite(&bmih, sizeof(bmih), 1, pWritingFile);
fwrite(pImageData, imageSize, 1, pWritingFile);
如果没有区别,那么图像的外观是颜色成分反转(RGB而不是BGR) - 虽然glPixelStorei(GL_PACK_SWAP_BYTES,1)
不应该影响像素数据本身,但值得尝试要么:
glPixelStorei(GL_PACK_SWAP_BYTES,1);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pImageData);
或者这个:
glPixelStorei(GL_PACK_SWAP_BYTES,0);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pImageData);