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 3351696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:56:24+00:00 2026-05-18T01:56:24+00:00

I want to load a bmp file into memory in C. I’ve found many

  • 0

I want to load a bmp file into memory in C. I’ve found many different types of BmpFileHeader and BmpInfoHeader structures. The last one that I’ve taken it from msdn.microsoft.com but it still does not read properly from binary file. With test file lena
Image Size: -1076791624
Memory could not be allocated

What’s the point I am missing? Thanks.

#include <stdio.h>
#include <stdlib.h>

typedef struct tagBITMAPFILEHEADER {
  unsigned short bfType;
  unsigned int bfSize;
  short bfReserved1;
  short bfReserved2;
  unsigned int bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER {
  unsigned int biSize;
  int biWidth;
  int biHeight;
  short biPlanes;
  short biBitCount;
  unsigned int biCompression;
  unsigned int biSizeImage;
  int biXPelsPerMeter;
  int biYPelsPerMeter;
  unsigned int biClrUsed;
  unsigned int biClrImportant;
} BITMAPINFOHEADER;   

int main(int argc, char *argv[])
{
   if(argc != 2)
   {
    printf("Usage: %s input.bmp\n", argv[0]);
    exit(-1);
   }

   FILE *filePtr;
   BITMAPFILEHEADER bitmapFileHeader;
   BITMAPINFOHEADER bitmapInfoHeader;
   unsigned char *bitmapImage;
   int imageIdx=0;
   unsigned char tempRGB;

   filePtr = fopen(argv[1],"rb");
   if (filePtr == NULL)
   {
    printf("File could not opened\n");
    exit(-1);
    }

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

    if (bitmapFileHeader.bfType !=0x4D42)
    {
    fclose(filePtr);
    printf("Not a bmp file\n");
    exit(-1);
    }

    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
    bitmapImage = (unsigned char*)malloc(sizeof(unsigned char)*bitmapInfoHeader.biSizeImage);
    printf("Image Size: %d\n", bitmapInfoHeader.biSizeImage);

    if (!bitmapImage)
    {
    free(bitmapImage);
    fclose(filePtr);
    printf("Memory could not be allocated\n");
    exit(-1);
    }

    //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;
    }

    int i;

    for(i = 0; i < bitmapInfoHeader.biSizeImage; i+=3){
    printf("R: %c G: %c B: %c\n", bitmapImage[i], bitmapImage[i + 1], bitmapImage[i + 2]);
    }

    fclose(filePtr); 

    return 0;

}
  • 1 1 Answer
  • 0 Views
  • 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-18T01:56:24+00:00Added an answer on May 18, 2026 at 1:56 am

    The first thing that I would do is to find an authoritative statement on the byte layout of these headers and then, assuming that you want to continue to use the structs that you have defined, add some asserts on the field offsets, for example:

    assert(offsetof(BITMAPFILEHEADER, bfType) == 0);     // or whatever
    assert(offsetof(BITMAPFILEHEADER, bfSize) == 2);     // or whatever
    assert(offsetof(BITMAPFILEHEADER, bfOffBits) == 10); // or whatever
    etc.
    

    Chances are that your use of unsigned int, short, etc plus your compiler options (especially structure packing) might mean that your struct fields do not match the actual BMP file data (in offset, and maybe in length).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load multiple variables into one .mat file at the end of
I want to load a bmp file (in Java) and manipulate the image data
I want to load PNG-file from resources. There is a roughly MFC-way (with CResourceStream
I want to load a palette from a bitmap file I have created. The
I want to load defined folders into my website from same system, now I
I want a image loader library to help me load images (bmp, jpg, gif,
I have a WinForm application, and I want to load a Bitmap(.bmp) in a
I want to be able to open either a .jpg, .gif or .bmp into
I want load a css or scss file of my rails 3.1 project with
at first sorry my bad English I want load model in my plugin controller

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.