c - 将位图文件读入结构

标签 c bitmap structure bitmapimage

我想将一个位图文件读入一个结构并像操作它一样操作它。制作镜像效果,但我不明白我应该创建哪种结构才能读入它。

感谢您的帮助。

最佳答案

»这就是您手动加载 .BMP 文件的方式

位图文件格式:

  • 位图文件头
  • 位图信息头
  • 调色板数据
  • 位图数据

代码部分继续。这是我们需要创建的结构来保存位图文件头。

#pragma pack(push, 1)

typedef struct tagBITMAPFILEHEADER
{
    WORD bfType;  //specifies the file type
    DWORD bfSize;  //specifies the size in bytes of the bitmap file
    WORD bfReserved1;  //reserved; must be 0
    WORD bfReserved2;  //reserved; must be 0
    DWORD bfOffBits;  //specifies the offset in bytes from the bitmapfileheader to the bitmap bits
}BITMAPFILEHEADER;

#pragma pack(pop)

bftype 字段检查您实际上是否正在加载 .BMP 文件,如果是,该字段应为 0x4D42。

现在我们需要创建我们的 bitmapinfoheader 结构。这包含有关我们的位图的信息。

#pragma pack(push, 1)

typedef struct tagBITMAPINFOHEADER
{
    DWORD biSize;  //specifies the number of bytes required by the struct
    LONG biWidth;  //specifies width in pixels
    LONG biHeight;  //specifies height in pixels
    WORD biPlanes;  //specifies the number of color planes, must be 1
    WORD biBitCount;  //specifies the number of bits per pixel
    DWORD biCompression;  //specifies the type of compression
    DWORD biSizeImage;  //size of image in bytes
    LONG biXPelsPerMeter;  //number of pixels per meter in x axis
    LONG biYPelsPerMeter;  //number of pixels per meter in y axis
    DWORD biClrUsed;  //number of colors used by the bitmap
    DWORD biClrImportant;  //number of colors that are important
}BITMAPINFOHEADER;

#pragma pack(pop)

现在开始加载我们的位图。

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
    FILE *filePtr;  //our file pointer
    BITMAPFILEHEADER bitmapFileHeader;  //our bitmap file header
    unsigned char *bitmapImage;  //store image data
    int imageIdx=0;  //image index counter
    unsigned char tempRGB;  //our swap variable

    //open file in read binary mode
    filePtr = fopen(filename,"rb");
    if (filePtr == NULL)
        return NULL;

    //read the bitmap file header
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr);

    //verify that this is a .BMP file by checking bitmap id
    if (bitmapFileHeader.bfType !=0x4D42)
    {
        fclose(filePtr);
        return NULL;
    }

    //read the bitmap info header
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr); 

    //move file pointer to the beginning of bitmap data
    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

    //allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    //verify memory allocation
    if (!bitmapImage)
    {
        free(bitmapImage);
        fclose(filePtr);
        return NULL;
    }

    //read in the bitmap image data
    fread(bitmapImage,bitmapInfoHeader->biSizeImage,1,filePtr);

    //make sure bitmap image data was read
    if (bitmapImage == NULL)
    {
        fclose(filePtr);
        return NULL;
    }

    //swap the R and B values to get RGB (bitmap is BGR)
    for (imageIdx = 0;imageIdx < bitmapInfoHeader->biSizeImage;imageIdx+=3)
    {
        tempRGB = bitmapImage[imageIdx];
        bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
        bitmapImage[imageIdx + 2] = tempRGB;
    }

    //close file and return bitmap image data
    fclose(filePtr);
    return bitmapImage;
}

现在利用所有这些:

BITMAPINFOHEADER bitmapInfoHeader;
unsigned char *bitmapData;
// ...
bitmapData = LoadBitmapFile("mypic.bmp",&bitmapInfoHeader);
//now do what you want with it, later on I will show you how to display it in a normal window

稍后我会提出写入 .BMP、如何加载 targa 文件以及如何显示它们。«

引自:http://www.vbforums.com/showthread.php?261522-C-C-Loading-Bitmap-Files-%28Manually%29 (用户:BeholderOf)。 (完成了一些小的更正)

关于c - 将位图文件读入结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14279242/

相关文章:

C、使用 "usleep"函数编译报错

java - 使用 Glide 加载位图到 ImageView

c - 为什么在结构数组中使用字符元素会出现运行时错误

c - 变量声明完成后一次性初始化/定义结构变量

c - 在c语言的结构中定义一个不带标识符的struct var是什么意思?

c - 是否可以使用 NSLog C 结构(如 CGRect 或 CGPoint)?

c++ - 如何确定 POINT 是否在按钮区域内

c - c语言中的ASCII字符串转换器

c# - System.Drawing.Graphics 非常大的图像

android - 如何使图钉标记在 Imageview 上可点击