Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将帧缓冲区数据写入JPEG或BMP图像_C++_Opengl_Visual C++ - Fatal编程技术网

C++ 如何将帧缓冲区数据写入JPEG或BMP图像

C++ 如何将帧缓冲区数据写入JPEG或BMP图像,c++,opengl,visual-c++,C++,Opengl,Visual C++,我是openGl新手,我的任务是将帧缓冲区(openGl屏幕外渲染数据)写入JPG或Bmp图像 我用谷歌搜索它,但找不到正确的解决方案 我尝试了以下代码,但结果是一个不受支持的文件。我无法打开该文件 FILE *Out; unsigned char *Buff; // Capture a screen shot. Save as a RAW-format file. // First, allocate memory. Buff = new unsigned c

我是openGl新手,我的任务是将帧缓冲区(openGl屏幕外渲染数据)写入JPG或Bmp图像

我用谷歌搜索它,但找不到正确的解决方案

我尝试了以下代码,但结果是一个不受支持的文件。我无法打开该文件

FILE *Out;
    unsigned char *Buff;

    // Capture a screen shot. Save as a RAW-format file.
    // First, allocate memory.
    Buff = new unsigned char[512*512*3];

   // Now, get pixels.
    glReadBuffer(GL_BACK);
    glReadPixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, Buff);

  // Now, open to file and write pixels.
   Out = fopen(Filename, "wb");
   if(!Out) return;
   fwrite(Buff, 3, 512*512, Out);


   fclose(Out);
   delete[] Buff;

这里我使用的是原始文件,但我想要的是jpeg或bmp文件。您使用的是原始文件或正确的路径

首先,不要声明未初始化的变量(尤其是指针)。这可能导致程序故障(更多信息,请阅读:)

因此,不要这样做:

unsigned char *Buff;
Buff = new unsigned char[512*512*3];
做:

你的buff需要是byte*。如果要写入BMP文件,需要在标题中包含以下内容:

#include <Windows.h>

(代码来源:)

要转换为BMP,您需要以24bpp格式创建一个大小合适的空位图,然后将GL\u RGB数据的每一行复制到其像素阵列中。(请记住,BMP从下到上存储行,并将每行填充为4字节的倍数。)转换为JPEG的最简单方法可能是在内存中创建BMP并压缩它。有几个图形库会有帮助。

我很确定,只有行大小恰好是4的倍数时,它才起作用。@Mihai daniel virna.我在尝试此代码时得到了一个空白图像作为输出。.你知道原因吗。.我是否应该将其转换为其他内容以查看图像?@GreeshmaPanicker。。目前我不能测试代码,因为我不在家。我可以在晚上亲自测试后得到回复。使用图像加载/写入库,jpeg是创建文件编写器的一种复杂格式。@GreeshmaPanicker我今天做不到。您知道,StackExchange不是编码服务。:)
#include <Windows.h>
    byte* Buff = new byte[512*512*3];
if (!Buff)
    return;

glReadBuffer(GL_BACK);
glReadPixels(0, 0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, Buff);


FILE *Out = fopen(filename, "wb");
if (!Out)
    return;
BITMAPFILEHEADER bitmapFileHeader;
BITMAPINFOHEADER bitmapInfoHeader;

bitmapFileHeader.bfType = 0x4D42;
bitmapFileHeader.bfSize = windowWidth*windowHeight * 3;
bitmapFileHeader.bfReserved1 = 0;
bitmapFileHeader.bfReserved2 = 0;
bitmapFileHeader.bfOffBits =
    sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfoHeader.biWidth = windowWidth - 1;
bitmapInfoHeader.biHeight = windowHeight - 1;
bitmapInfoHeader.biPlanes = 1;
bitmapInfoHeader.biBitCount = 24;
bitmapInfoHeader.biCompression = BI_RGB;
bitmapInfoHeader.biSizeImage = 0;
bitmapInfoHeader.biXPelsPerMeter = 0; // ?
bitmapInfoHeader.biYPelsPerMeter = 0; // ?
bitmapInfoHeader.biClrUsed = 0;
bitmapInfoHeader.biClrImportant = 0;

fwrite(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, Out);
fwrite(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, Out);
fwrite(Buff, windowWidth*windowHeight * 3, 1, Out);
fclose(Out);

delete[] Buff;