Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

使用C语言实现将内存中数据输出为.bmp格式

  1. User

    使用C语言实现将内存中数据输出为.bmp格式

  2. Assistant

    要将内存中的数据输出为.bmp格式,需要按照以下步骤进行:

    1. 定义BMP文件头和图片信息头结构体,以便生成.bmp文件时填写头信息。

    2. 分配内存,将图片像素点的RGB信息放入分配的内存中。

    3. 将BMP文件头和图像信息头写入输出.bmp文件中。

    4. 将RGB数据写入输出.bmp文件中。

    下面是一个简单的示例代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // BMP文件头
    typedef struct {
        unsigned short MagicNumber;   // 文件标识
        unsigned int FileSize;        // 文件大小
        unsigned int Reserve;         // 保留值
        unsigned int DataOffset;      // 数据偏移量
    } BMPHeader;
    
    // BMP图片信息头
    typedef struct {
        unsigned int HeaderSize;      // 信息头大小
        int Width;                    // 图像宽度
        int Height;                   // 图像高度
        unsigned short Planes;        // 色彩平面数
        unsigned short BitsPerPixel;  // 每像素比特数
        unsigned int CompressionType; // 压缩类型
        unsigned int ImageSize;       // 图像大小
        int HorizontalResolution;     // 横向分辨率
        int VerticalResolution;       // 纵向分辨率
        unsigned int Colors;          // 颜色数
        unsigned int ImportantColors; // 重要颜色数
    } BMPInfoHeader;
    
    // 内存中存储的图片信息
    typedef struct {
        int Width;
        int Height;
        unsigned char* pRed;
        unsigned char* pGreen;
        unsigned char* pBlue;
    } ImageData;
    
    // 将内存中的数据写入.bmp文件中
    int WriteBMPFile(const char* filename, const ImageData* pImg)
    {
        int i, j;
        BMPHeader header;
        BMPInfoHeader info;
        unsigned char* pData = NULL;
        FILE* pFile = NULL;
        int rowLen, paddingLen;
    
        // 模式为二进制写入
        pFile = fopen(filename, "wb");
        if(pFile == NULL)
            return -1;
    
        // BMP文件头
        header.MagicNumber = 0x4D42; // BM
        header.FileSize = sizeof(BMPHeader) + sizeof(BMPInfoHeader) + pImg->Height * pImg->Width * 3;
        header.Reserve = 0;
        header.DataOffset = sizeof(BMPHeader) + sizeof(BMPInfoHeader);
        fwrite(&header, sizeof(BMPHeader), 1, pFile);
    
        // BMP信息头
        memset(&info, 0, sizeof(BMPInfoHeader));
        info.HeaderSize = sizeof(BMPInfoHeader);
        info.Width = pImg->Width;
        info.Height = pImg->Height;
        info.Planes = 1;
        info.BitsPerPixel = 24;
        info.CompressionType = 0;
        info.ImageSize = 0;
        info.HorizontalResolution = 0;
        info.VerticalResolution = 0;
        info.Colors = 0;
        info.ImportantColors = 0;
        fwrite(&info, sizeof(BMPInfoHeader), 1, pFile);
    
        // 数据区
        rowLen = (pImg->Width * 3 + 3) / 4 * 4;
        paddingLen = rowLen - pImg->Width * 3;
        pData = (unsigned char*)malloc(rowLen * pImg->Height);
        memset(pData, 0, rowLen * pImg->Height);
        for (i = 0; i < pImg->Height; i++)
        {
            for (j = 0; j < pImg->Width; j++)
            {
                pData[i*rowLen + j*3] = pImg->pBlue[i*pImg->Width + j];
                pData[i*rowLen + j*3+1] = pImg->pGreen[i*pImg->Width + j];
                pData[i*rowLen + j*3+2] = pImg->pRed[i*pImg->Width + j];
            }
        }
        fwrite(pData, rowLen * pImg->Height, 1, pFile);
        free(pData);
        fclose(pFile);
        return 0;
    }
    
    int main()
    {
        ImageData img;
        int i, j;
    
        img.Width = 256;
        img.Height = 256;
        img.pRed = (unsigned char*)malloc(img.Width * img.Height);
        img.pGreen = (unsigned char*)malloc(img.Width * img.Height);
        img.pBlue = (unsigned char*)malloc(img.Width * img.Height);
        memset(img.pRed, 0, img.Width * img.Height);
        memset(img.pGreen, 0, img.Width * img.Height);
        memset(img.pBlue, 0, img.Width * img.Height);
        
        for (i = 0; i < img.Height; i++)
        {
            for (j = 0; j < img.Width; j++)
            {
                img.pRed[i*img.Width + j] = j % 256;
                img.pGreen[i*img.Width + j] = i % 256;
                img.pBlue[i*img.Width + j] = (i+j) % 256;
            }
        }
    
        WriteBMPFile("test.bmp", &img);
    
        free(img.pRed);
        free(img.pGreen);
        free(img.pBlue);
        return 0;
    }
    
  3. User

    使用C语言实现将内存中数据输出为.png格式

  4. Assistant

    要将内存中的数据输出为.png格式,可以使用第三方库libpng。libpng提供了一套方便的API,可以处理PNG文件格式的读写。

    下面是一个简单的示例代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <png.h>
    
    // 内存中存储的图片信息
    typedef struct {
        int Width;
        int Height;
        unsigned char* pRed;
        unsigned char* pGreen;
        unsigned char* pBlue;
    } ImageData;
    
    // 将内存中的数据写入.png文件中
    int WritePNGFile(const char* filename, const ImageData* pImg)
    {
        int i, j;
        png_bytep* row_pointers = NULL;
        png_structp png_ptr = NULL;
        png_infop info_ptr = NULL;
        FILE* fp = NULL;
        int ret = -1;
    
        fp = fopen(filename, "wb");
        if(fp == NULL)
            return -1;
    
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
        if(png_ptr == NULL)
        {
            fclose(fp);
            return -1;
        }
    
        info_ptr = png_create_info_struct(png_ptr);
        if(info_ptr == NULL)
        {
            png_destroy_write_struct(&png_ptr, NULL);
            fclose(fp);
            return -1;
        }
    
        // 设定输出png文件的信息
        png_set_IHDR(png_ptr, info_ptr, pImg->Width, pImg->Height,
                     8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
                     PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    
        // 分配一段内存来保存行数据,因为libpng要求为每行分配一个单独的缓冲区
        row_pointers = png_malloc(png_ptr, pImg->Height * sizeof(png_bytep));
        for(i = 0; i < pImg->Height; i++)
            row_pointers[i] = png_malloc(png_ptr, 3 * pImg->Width * sizeof(png_byte));
    
        // 拷贝图片的像素数据
        for(i = 0; i < pImg->Height; i++)
        {
            for(j = 0; j < pImg->Width; j++)
            {
                row_pointers[i][j*3] = pImg->pRed[i*pImg->Width + j];
                row_pointers[i][j*3+1] = pImg->pGreen[i*pImg->Width + j];
                row_pointers[i][j*3+2] = pImg->pBlue[i*pImg->Width + j];
            }
        }
    
        // 写入png文件
        png_init_io(png_ptr, fp);
        png_set_rows(png_ptr, info_ptr, row_pointers);
        png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
        ret = 0;
    
        // 释放内存和资源
        for(i = 0; i < pImg->Height; i++)
            png_free(png_ptr, row_pointers[i]);
        png_free(png_ptr, row_pointers);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
    
        return ret;
    }
    
    int main()
    {
        ImageData img;
        int i, j;
    
        img.Width = 256;
        img.Height = 256;
        img.pRed = (unsigned char*)malloc(img.Width * img.Height);
        img.pGreen = (unsigned char*)malloc(img.Width * img.Height);
        img.pBlue = (unsigned char*)malloc(img.Width * img.Height);
        memset(img.pRed, 0, img.Width * img.Height);
        memset(img.pGreen, 0, img.Width * img.Height);
        memset(img.pBlue, 0, img.Width * img.Height);
        
        for (i = 0; i < img.Height; i++)
        {
            for (j = 0; j < img.Width; j++)
            {
                img.pRed[i*img.Width + j] = j % 256;
                img.pGreen[i*img.Width + j] = i % 256;
                img.pBlue[i*img.Width + j] = (i+j) % 256;
            }
        }
    
        WritePNGFile("test.png", &img);
    
        free(img.pRed);
        free(img.pGreen);
        free(img.pBlue);
        return 0;
    }
    

    需要注意的是,上面的示例中没有进行错误信息处理,实际中需要加入错误处理代码。同时,也需要在编译时加入libpng的库文件链接,比如使用gcc编译:gcc -o test test.c -lpng。

  5. User

    使用C语言实现将内存中数据输出为.jpg

  6. Assistant

    要将内存中的数据输出为.jpg格式,可以使用第三方库libjpeg。libjpeg提供了一套方便的API,可以处理JPG文件格式的读写。

    下面是一个简单的示例代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <jpeglib.h>
    
    // 内存中存储的图片信息
    typedef struct {
        int Width;
        int Height;
        unsigned char* pRed;
        unsigned char* pGreen;
        unsigned char* pBlue;
    } ImageData;
    
    // 将内存中的数据写入.jpg文件中
    int WriteJPEGFile(const char* filename, const ImageData* pImg, int quality)
    {
        int i, j;
        struct jpeg_compress_struct cinfo;
        struct jpeg_error_mgr jerr;
        FILE* fp = NULL;
        JSAMPROW row_pointer[1];
        int row_stride;
    
        cinfo.err = jpeg_std_error(&jerr);
        jpeg_create_compress(&cinfo);
        fp = fopen(filename, "wb");
        if(fp == NULL)
        {
            jpeg_destroy_compress(&cinfo);
            return -1;
        }
    
        jpeg_stdio_dest(&cinfo, fp);
        cinfo.image_width = pImg->Width;
        cinfo.image_height = pImg->Height;
        cinfo.input_components = 3; // RGB三色
        cinfo.in_color_space = JCS_RGB; // RGB颜色空间
        jpeg_set_defaults(&cinfo);
        jpeg_set_quality(&cinfo, quality, TRUE); // 压缩质量
        jpeg_start_compress(&cinfo, TRUE);
    
        row_stride = pImg->Width * 3;
        while(cinfo.next_scanline < cinfo.image_height)
        {
            row_pointer[0] = &pImg->pRed[cinfo.next_scanline * row_stride];
            jpeg_write_scanlines(&cinfo, row_pointer, 1);
        }
    
        jpeg_finish_compress(&cinfo);
        fclose(fp);
        jpeg_destroy_compress(&cinfo);
    
        return 0;
    }
    
    int main()
    {
        ImageData img;
        int i, j;
    
        img.Width = 256;
        img.Height = 256;
        img.pRed = (unsigned char*)malloc(img.Width * img.Height);
        img.pGreen = (unsigned char*)malloc(img.Width * img.Height);
        img.pBlue = (unsigned char*)malloc(img.Width * img.Height);
        memset(img.pRed, 0, img.Width * img.Height);
        memset(img.pGreen, 0, img.Width * img.Height);
        memset(img.pBlue, 0, img.Width * img.Height);
        
        for (i = 0; i < img.Height; i++)
        {
            for (j = 0; j < img.Width; j++)
            {
                img.pRed[i*img.Width + j] = j % 256;
                img.pGreen[i*img.Width + j] = i % 256;
                img.pBlue[i*img.Width + j] = (i+j) % 256;
            }
        }
    
        WriteJPEGFile("test.jpg", &img, 80);
    
        free(img.pRed);
        free(img.pGreen);
        free(img.pBlue);
        return 0;
    }
    

    需要注意的是,上面的示例中没有进行错误信息处理,实际中需要加入错误处理代码。同时,也需要在编译时加入libjpeg的库文件链接,比如使用gcc编译:gcc -o test test.c -ljpeg。

  7. User

    使用C语言实现将内存中rgba数据输出为图片

  8. Assistant

    要将内存中的RGBA数据输出为图片,可以使用第三方库STB Image。STB Image是一个非常轻量的、只有一个头文件的图片处理库,可以处理多种图片格式的读写。

    下面是一个简单的示例代码:

    #define STB_IMAGE_WRITE_IMPLEMENTATION
    #include "stb/stb_image_write.h"
    
    // 内存中存储的图片信息
    typedef struct {
        int Width;
        int Height;
        unsigned char* pRGBA;
    } ImageData;
    
    // 将内存中的数据写入指定格式的图片文件中
    int WriteImageFile(const char* filename, const ImageData* pImg, int format)
    {
        return stbi_write_png(filename, pImg->Width, pImg->Height, 4, pImg->pRGBA, 0); // 写入png文件
        //return stbi_write_jpg(filename, pImg->Width, pImg->Height, 4, pImg->pRGBA, 80); // 写入jpg文件
        //return stbi_write_bmp(filename, pImg->Width, pImg->Height, 4, pImg->pRGBA); // 写入bmp文件
        //return stbi_write_tga(filename, pImg->Width, pImg->Height, 4, pImg->pRGBA); // 写入tga文件
    }
    
    int main()
    {
        ImageData img;
        int i, j;
    
        img.Width = 256;
        img.Height = 256;
        img.pRGBA = (unsigned char*)malloc(img.Width * img.Height * 4);
        memset(img.pRGBA, 0, img.Width * img.Height * 4);
    
        for (i = 0; i < img.Width; i++)
        {
            for (j = 0; j < img.Height; j++)
            {
                img.pRGBA[(i*img.Width+j)*4] = 255; // R
                img.pRGBA[(i*img.Width+j)*4+1] = i; // G
                img.pRGBA[(i*img.Width+j)*4+2] = j; // B
                img.pRGBA[(i*img.Width+j)*4+3] = 255; // A
            }
        }
    
        WriteImageFile("test.png", &img, STBI_rgb_alpha);
    
        free(img.pRGBA);
        return 0;
    }
    

    需要注意的是,上面的示例中没有进行错误信息处理,实际中需要加入错误处理代码。同时,需要根据输出的图片格式指定不同的输出参数,比如上面的示例中输出的是png格式的图片,第四个参数为STBI_rgb_alpha,表示是RGBA四通道数据。如果输出的是jpg格式,则第四个参数应该设为3,表示RGB三通道数据。