Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C

后端 未结 8 1506
南方客
南方客 2020-12-16 08:52

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).

I

相关标签:
8条回答
  • 2020-12-16 09:38

    I'm not familiar with the BMP file format, but wouldn't you need to read in the header information first? Something like:

    BmpHeader header;
    fread(&header,sizeof(BmpHeader),1,inFile);
    

    and read in detailed image information, which you'll need:

    BmpImageInfo info;
    fread(&info,sizeof(BmpImageInfo),1,inFile);
    

    and read in palette information as well.

    once you have that you will know the file size and data offset. You could pre-allocate sufficient space and read in all at once, which may be simplest. Alternatively you could read in in chunks and parse as you're going (reduces chance of not having enough memory, but parsing is more complicated).

    You'll also know from the info section if compression is enabled, image dimensions etc.

    If you're reading in all at once, jump to the offset of the data, and do something like:

    Rgb* rgb = offset;
    blueVal = rgb->blue;
    greenVal = rgb->green;
    redVal = rgb->red;
    rgb += sizeof( Rgb );
    

    and so on. Obviously that code isn't checking for errors, end of buffer, and so on, so you'll need to do that. You'll probably also have to read in palette information to make sense of the image data.

    Or, as someone else said, look at the format spec on Wikipedia

    0 讨论(0)
  • 2020-12-16 09:41

    If the BMP file has a palette then the below code should work:

      FILE *inFile, *outFile;
      inFile = fopen("C:/in.bmp", "rb");
      Rgb Palette[256];
      if ( inFile ) {
        // Bypass headers
        fseek(inFile, sizeof(BmpHeader) + sizeof(BmpImageInfo), SEEK_SET);
        // Load the whole palette
        fread(Palette, sizeof(Palette), 1, inFile);
        fclose(inFile);
      }
    
    0 讨论(0)
提交回复
热议问题