在 opengl 环境下将 texture 保存为 bmp 图片

首先说一下做这个功能的原因,是在应用里,有一个渲染线程,负责渲染出图,将最终的画面以texture形式传递给另一个线程,后者会再进行一些处理,然后渲染到屏幕上。那么在最后显示出来画面偶尔有花屏撕裂的现象,这样就需要确认问题出在哪一步。所以想要在中间把texture保存成图片检查一下。

实现这个功能主要依赖函数 glReadPixels(),它可以把gpu里的像素值读取到cpu内存中,这样才有可能保存下来。

另外一个关键点是,如何把RGBA格式的像素值写成BMP格式的文件。

接下来说实现步骤,首先需要绑定framebuffer,以及要读取的texture。

glBindFramebuffer(GL_FRAMEBUFFER, m_uSpareFbId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);

然后 glReadBuffer(), glReadPixels(), 这里以宽高为 1920 * 1920 为例。

        int width = 1920;
        int height = 1920;
        glReadBuffer ( GL_COLOR_ATTACHMENT0 );
        GLint readType, readFormat;
        GLubyte *pixels;
        glGetIntegerv ( GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType );
        glGetIntegerv ( GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat );

        unsigned int bytesPerPixel = 0;
        switch ( readType )
        {
            case GL_UNSIGNED_BYTE:
            case GL_BYTE:
                switch ( readFormat )
                {
                    case GL_RGBA:
                        bytesPerPixel = 4;
                        break;
                    case GL_RGB:
                    case GL_RGB_INTEGER:
                        bytesPerPixel = 3;
                        break;
                    case GL_RG:
                    case GL_RG_INTEGER:
                    case GL_LUMINANCE_ALPHA:
                        bytesPerPixel = 2;
                        break;
                    case GL_RED:
                    case GL_RED_INTEGER:
                    case GL_ALPHA:
                    case GL_LUMINANCE:
                        bytesPerPixel = 1;
                        break;
                    default:
                        break;
                }
                break;
            case GL_FLOAT:
            case GL_UNSIGNED_INT:
            case GL_INT:
                switch ( readFormat )
                {
                    case GL_RGBA:
                    case GL_RGBA_INTEGER:
                        bytesPerPixel = 16;
                        break;
                    case GL_RGB:
                    case GL_RGB_INTEGER:
                        bytesPerPixel = 12;
                        break;
                    case GL_RG:
                    case GL_RG_INTEGER:
                        bytesPerPixel = 8;
                        break;
                    case GL_RED:
                    case GL_RED_INTEGER:
                    case GL_DEPTH_COMPONENT:
                        bytesPerPixel = 4;
                        break;
                    default:
                        break;
                }
                break;
            case GL_HALF_FLOAT:
            case GL_UNSIGNED_SHORT:
            case GL_SHORT:
                switch ( readFormat )
                {
                    case GL_RGBA:
                    case GL_RGBA_INTEGER:
                        bytesPerPixel = 8;
                        break;
                    case GL_RGB:
                    case GL_RGB_INTEGER:
                        bytesPerPixel = 6;
                        break;
                    case GL_RG:
                    case GL_RG_INTEGER:
                        bytesPerPixel = 4;
                        break;
                    case GL_RED:
                    case GL_RED_INTEGER:
                        bytesPerPixel = 2;
                        break;
                    default:
                        break;
                }
                break;
            case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: // GL_DEPTH_STENCIL
                bytesPerPixel = 8;
                break;
                // GL_RGBA, GL_RGBA_INTEGER format
            case GL_UNSIGNED_INT_2_10_10_10_REV:
            case GL_UNSIGNED_INT_10F_11F_11F_REV: // GL_RGB format
            case GL_UNSIGNED_INT_5_9_9_9_REV: // GL_RGB format
            case GL_UNSIGNED_INT_24_8: // GL_DEPTH_STENCIL format
                bytesPerPixel = 4;
                break;
            case GL_UNSIGNED_SHORT_4_4_4_4: // GL_RGBA format
            case GL_UNSIGNED_SHORT_5_5_5_1: // GL_RGBA format
            case GL_UNSIGNED_SHORT_5_6_5: // GL_RGB format
                bytesPerPixel = 2;
                break;
            default:
                break;
        }
        pixels = ( GLubyte* ) malloc ( width * height * bytesPerPixel );
        glReadPixels ( 0, 0, width, height, readFormat, readType, pixels );

然后就可以把数据写到图片文件里,最后记得清内存。

SaveBmp(frameCount,pixels,width,height);
free(pixels);
frameCount ++;

转换bmp文件的实现方法如下,关键就是文件头部信息的填写,填写错误的话最后就会打不开图片文件,详细规则还未深入研究。

这里还存在一点问题,原格式是rgba,最后查看图片发现颜色变了,应该是显示的bgra,还没弄明白,有大佬路过还望不吝赐教!

#define FILENAME_MAX 64
#define CAMERA_DUMP_FRM_LOCATION "/sdcard/rgb/"

    static int frameCount = 0;

    typedef struct /**** BMP file header structure ****/
    {
        unsigned int bfSize; /* Size of file */
        unsigned short bfReserved1;      /* Reserved */
        unsigned short bfReserved2;      /* ... */
        unsigned int   bfOffBits;        /* Offset to bitmap data */
    } BITMAPFILEHEADER;

    typedef struct                       /**** BMP file info structure ****/
    {
        unsigned int   biSize;           /* Size of info header */
        int            biWidth;          /* Width of image */
        int            biHeight;         /* Height of image */
        unsigned short biPlanes;         /* Number of color planes */
        unsigned short biBitCount;       /* Number of bits per pixel */
        unsigned int   biCompression;    /* Type of compression to use */
        unsigned int   biSizeImage;      /* Size of image data */
        int            biXPelsPerMeter;  /* X pixels per meter */
        int            biYPelsPerMeter;  /* Y pixels per meter */
        unsigned int   biClrUsed;        /* Number of colors used */
        unsigned int   biClrImportant;   /* Number of important colors */
    } BITMAPINFOHEADER;

    void SaveBmp(uint32_t frameid, unsigned char *rgbbuf, int width, int height) {
        BITMAPFILEHEADER bfh;
        BITMAPINFOHEADER bih;
        /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
        unsigned short bfType = 0x4d42;
        bfh.bfReserved1 = 0;
        bfh.bfReserved2 = 0;
        bfh.bfSize = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + width * height * 4;
        bfh.bfOffBits = 0x36;

        bih.biSize = sizeof(BITMAPINFOHEADER);
        bih.biWidth = width;
        bih.biHeight = height;
        bih.biPlanes = 1;
        bih.biBitCount = 32;
        bih.biCompression = 0;
        bih.biSizeImage = bih.biWidth * bih.biHeight * bih.biBitCount;
        bih.biXPelsPerMeter = 5000;
        bih.biYPelsPerMeter = 5000;
        bih.biClrUsed = 0;
        bih.biClrImportant = 0;
        char buf[FILENAME_MAX];
        memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), CAMERA_DUMP_FRM_LOCATION "frame_%dX%d_%d.bmp",
                 width, height, frameid);
        LOG(" jjj: %s,%d buf : %s", __FUNCTION__, __LINE__, buf);
        int file_fd = open(buf, O_RDWR | O_CREAT, 0644);

        if (file_fd < 0) {
            printf("Could not write file\n");
            return;
        }

        /*Write headers*/
        write(file_fd, &bfType, sizeof(bfType));
        write(file_fd, &bfh, sizeof(bfh));
        write(file_fd, &bih, sizeof(bih));

        ssize_t result = write(file_fd, rgbbuf, width * height * 4);
        LOG(" jjj: %s,%d : written to file %s, result %ld", __FUNCTION__, __LINE__, buf,
            result);

        close(file_fd);
    }

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值