loading a bitmap

This is a discussion on loading a bitmap within the C Programming forums, part of the General Programming Boards category; i a loading a 24-bit bitmap with this code but i don't know whats wrong withit? can any one help ...

  1. #1
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8

    loading a bitmap

    i a loading a 24-bit bitmap with this code but i don't know whats wrong withit?
    can any one help me?
    Code:
    typedef unsigned long DWORD;
    typedef unsigned short WORD;
    
    
    typedef struct {
        int Signature;
    	DWORD Size;
    	DWORD Reserved;
    	DWORD BitsOffset;
    } BITMAP_FILEHEADER;
    
    
    typedef struct {
    	DWORD HeaderSize;
    	DWORD Width;
    	DWORD Height;
    	WORD Planes;
    	WORD BitCount;
    	DWORD Compression;
    	DWORD SizeImage;
    	DWORD PelsPerMeterX;
    	DWORD PelsPerMeterY;
    	DWORD ClrUsed;
    	DWORD ClrImportant;
    
    } BITMAP_HEADER;
    
    #include <stdio.h>
    main()
    {
        FILE * fp;
        BITMAP_FILEHEADER bmpheader;
        BITMAP_HEADER bmpinfheader;
        int raw_data_size;
        char *pixel;
    
        //openning file
        fp=fopen("c:\\pic.bmp","rb");
    
        /*if(fp==NULL){
            printf("error in opennig file");
            exit(1);
        }*/
    
    
    
        fread(&bmpheader,sizeof(bmpheader),1,fp);//reads the header
        fread(&bmpinfheader,sizeof(bmpinfheader),1,fp);//reads the info header
    
        raw_data_size=bmpheader.SizeImage;//stores the size of raw bitmaps
    
        pixel=new char [raw_data_size];//create an array for holding the pixels
    
        fread(pixel,sizeof(char),raw_data_size,fp);//reads them from memory to aray
    
        printf("%x",bmpheader.Signature);//prints 424D
    
        fclose(fp);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I think that struct is going to be padded, I tried pasting it in here and did a sizeof of both structs. This returned 112 bytes, but a bmp have a 56 byte header.

    Edit: no, that's not it. A long is 64 bits here that's why.

    Anyway, this is not C:

    Code:
    pixel=new char [raw_data_size];//create an array for holding the pixels
    You need to use malloc or calloc instead:

    Code:
    pixel = malloc(raw_data_size);
    Last edited by Subsonics; 06-04-2010 at 03:57 PM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Another thing worth mentioning is that a pixel is three bytes in a 24bit bmp, but watch out for the padding on each row.

  4. #4
    Registered User hadi0x7c7's Avatar
    Join Date
    Jun 2010
    Location
    IRI
    Posts
    8
    can you explain it more clearly?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What part? You can not use "new" in C, you need to use malloc or similar, like in the example above.

    The last post was just a heads up, if you intend to manipulate the img data. You need to move ahead 3bytes to reach the next pixel. Then each row might be padded to an even multiple of 4 i believe. This mean that you could end up with let's say two empty bytes in the end of each row, which you will need to skip.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 05:04 PM
  3. Loading a Bitmap resource for OpenGL Texture[x]
    By the dead tree in forum Game Programming
    Replies: 4
    Last Post: 08-26-2004, 02:12 PM
  4. Loading bitmap in dll
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2003, 01:53 PM
  5. No one can seem to help me, loading bitmap
    By Shadow12345 in forum C++ Programming
    Replies: 7
    Last Post: 12-28-2002, 01:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21