Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:05:43+00:00 2026-05-12T17:05:43+00:00

I want to read the RGB values for each pixel from a .bmp file,

  • 0

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 need to get just the RGB for each pixel and then write this information to a file.

I am trying to use the <windows.h> structures:

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;

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

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

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:05:43+00:00Added an answer on May 12, 2026 at 5:05 pm

    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.

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

    This should work: (Edit: 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read line n1->n2 from file foo.c into the current buffer. I
I want read a text file into an array. How can I do that?
I want to create a program that loads the RGB values of each pixel
I want to read each of the 4 columns stored in a .txt file
i want read data from the binary file named xyz.bin and want to parse
I want to read some code from a file and give this to a
Hi I want read from a hung text file data directly to a string
I want to load a bmp file into memory in C. I've found many
I have a text file. I want read that file. But In that if
I want to read and write from serial using events/interrupts. Currently, I have it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.