获取RGB值,从和24bpp位图C中的像素转换为GBA格式 [英] Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C

查看:112
本文介绍了获取RGB值,从和24bpp位图C中的像素转换为GBA格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的RGB值从 .BMP 文件中的每个像素,这样我就可以转换 BMP 成适合GBA(GameBoy Advance的)。

I want to read the RGB values for each pixel from a .bmp file, so I can convert the bmp into a format suitable for GBA (GameBoy Advance).

我需要得到公正的RGB每个像素,然后将此信息写入到文件中。

I need to get just the RGB for each pixel and then write this information to a file.

我试图使用<&WINDOWS.H GT; 结构:

typedef struct
{
    char signature[2];
    unsigned int fileSize;
    unsigned int reserved;
    unsigned int offset;
}BmpHeader;

typedef struct
{
    unsigned int headerSize;
    unsigned int width;
    unsigned int height;
    unsigned short planeCount;
    unsigned short bitDepth;
    unsigned int compression;
    unsigned int compressedImageSize;
    unsigned int horizontalResolution;
    unsigned int verticalResolution;
    unsigned int numColors;
    unsigned int importantColors;

}BmpImageInfo;

typedef struct
{
    unsigned char blue;
    unsigned char green;
    unsigned char red;
    unsigned char reserved;
}Rgb;

typedef struct
{
    BmpHeader header;
    BmpImageInfo info;
    Rgb colors[256];
    unsigned short image[1];
}BmpFile;

但我只需要RGB结构。所以我们可以说我读in.bmp

but I only need RGB struct. So let's say I read "in.bmp":

FILE *inFile, *outFile;
inFile = fopen("C://in.bmp", "rb");

Rgb Palette[256];
for(i=0;i<256;i++)
{
   fread(&Palette[i],sizeof(Rgb),1,inFile);
}

fclose(inFile);

这是正确的?
我怎么只写RGB信息到一个文件?

Is this correct? How do I write only the RGB information to a file?

推荐答案

您需要先获得在嵌入式调色板中的颜色数量。这是在DIB头可用

You need first to get the number of colors available in the embedded palette. This is available in the DIB Header.

然后就可以读取包含调色板所有颜色分量。

Then you can read all color components that contain the palette.

您可以看到所有的头信息等进行偏移知道寻求向其中:的http:// EN .wikipedia.org /维基/ BMP_file_format

You can see all header information like offset to know whereto seek : http://en.wikipedia.org/wiki/BMP_file_format.

这应该工作:(编辑:添加code在文件中写入)

This should work: ( Add code to write in file)

FILE *inFile, *outFile;
BMPHeader header;
BMPImageInfo info;
RGB *palette, *p;
int i = 0;

inFile = fopen("C://in.bmp", "rb");
if( !inFile )
   return;

if( fread(&header, sizeof(BMPHeader), 1, inFile) != 1 )
   return; // Manage error and close file

if( fread&info, sizeof(BMPImageInfo), 1, inFile) != 1 )
   return; // Manage error and close file

if( info.numColors > 0 )
{
   palette = (RGB*)malloc(sizeof(RGB) * info.numColors);
   if( fread(palette, sizeof(RGB), info.numColors, inFile) != info.numColors )
      return; // manage error and close file
}

fclose(inFile)

// Binary method => if read later by another computer
outFile = fopen("path", "wb");
if( !outFile )
   return;

if( fwrite(&info.numColors, sizeof(unsigned int), 1, outFile) != 1 )
   return; // Manage Error and close file

if( fwrite(&palette, sizeof(RGB), info.numColors, outFile) != info.numColors )
   return; // Manage error and close file

fclose(outFile);

// Text method => if read later by human
outFile = fopen("path", "w");
if( !outFile )
   return;

for( i=0; i<info.numColors; ++i )
{
   p = &palette[i];
   if( fprintf(outFile, "R:%d, G:%d, B:%d\n", p->red, p->green, p->blue) < 0 )
      return; // Manage error and close file
}

fclose(outFile);

这篇关于获取RGB值,从和24bpp位图C中的像素转换为GBA格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆