Results 1 to 32 of 32

Thread: C/C++ - Loading Bitmap Files (Manually)

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2003
    Posts
    34

    C/C++ - Loading Bitmap Files (Manually)

    This is how you maually load a bmp file

    The bitmap file format:
    Bitmap file header
    Bitmap info header
    palette data
    Bitmap Dada

    so on with the code part, this is our struct we need to create to hold the bitmap file header
    Code:
    typedef struct tagBITMAPFILEHEADER
    {
    WORD bfType;  //specifies the file type
    DWORD bfSize;  //specifies the size in bytes of the bitmap file
    WORD bfReserved1;  //reserved; must be 0
    WORD bfReserved2;  //reserved; must be 0
    DWORD bOffBits;  //species the offset in bytes from the bitmapfileheader to the bitmap bits
    }BITMAPFILEHEADER;
    the bftype field checks to see if you are infact loading a bmp file, and if you are the field should be 0x4D42.

    Now we need to create our bitmapinfoheader struct. This holds info about our bitmap(self explainitory).
    Code:
    typedef struct tagBITMAPINFOHEADER
    {
    DWORD biSize;  //specifies the number of bytes required by the struct
    LONG biWidth;  //specifies width in pixels
    LONG biHeight;  //species height in pixels
    WORD biPlanes; //specifies the number of color planes, must be 1
    WORD biBitCount; //specifies the number of bit per pixel
    DWORD biCompression;//spcifies the type of compression
    DWORD biSizeImage;  //size of image in bytes
    LONG biXPelsPerMeter;  //number of pixels per meter in x axis
    LONG biYPelsPerMeter;  //number of pixels per meter in y axis
    DWORD biClrUsed;  //number of colors used by th ebitmap
    DWORD biClrImportant;  //number of colors that are important
    }BITMAPINFOHEADER;
    Now on to loading our Bitmap
    Code:
    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
    
    FILE *filePtr; //our file pointer
    BITMAPFILEHEADER bitmapFileHeader; //our bitmap file header
    unsigned char *bitmapImage;  //store image data
    int imageIdx=0;  //image index counter
    unsigned char tempRGB;  //our swap variable
    
    //open filename in read binary mode
    filePtr = fopen(filename,"rb");
    if (filePtr == NULL)
    return NULL;
    
    //read the bitmap file header
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER,1,filePtr);
    
    //verify that this is a bmp file by check bitmap id
    if (bitmapFileHeader.bfType !=0x4D42)
    {
    fclose(filePtr);
    return NULL;
    }
    
    //read the bitmap info header
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr);
    
    //move file point to the begging of bitmap data
    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
    
    //allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
    
    //verify memory allocation
    if (!bitmapImage)
    {
    free(bitmapImage);
    fclose(filePtr);
    return NULL;
    }
    
    //read in the bitmap image data
    fread(bitmapImage,bitmapInfoHeader->biSizeImage,filePtr);
    
    //make sure bitmap image data was read
    if (bitmapImage == NULL)
    {
    fclose(filePtr);
    return NULL;
    }
    
    //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;
    }
    
    //close file and return bitmap iamge data
    fclose(filePtr);
    return bitmapImage;
    }
    Now to make use of all of this:
    Code:
    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char *bitmapData;
    ...
    bitmapData = LoadBitmapFile("mypic.bmp",&bitmapInfoHeader);
    //now do what you want with it, later on i will show you how to display it in a normal window
    Later on ill put up Writing a to bmp and how to load a targa file and how to display them.
    Last edited by BeholderOf; Sep 18th, 2003 at 02:22 PM.
    a 17 year old kid who has nothing better to do !
    and i never said i was right !!!

  2. #2
    New Member
    Join Date
    Jun 2005
    Posts
    1

    Re: C/C++ - Loading Bitmap Files (Manually)

    Can any one tell me how to display a Bitmap on a GLUT window or a Win32 window

  3. #3
    New Member
    Join Date
    Sep 2007
    Posts
    1

    Talking Re: C/C++ - Loading Bitmap Files (Manually)

    Your code doesn't work under Dev C++. It gives a hundred error messages. Missa thinks its the Dev C++ fault. What compile do you use?

    Afterthoughts: maybe Missa should try compiling it as C and not C++? Hummm

  4. #4
    Banned
    Join Date
    Dec 2007
    Posts
    26

    Re: C/C++ - Loading Bitmap Files (Manually)

    The reason why this is giving you so many errors is because the code contains them. Starting with a missing curly at the second line, continuing with a missing bracket...

  5. #5
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hey,
    sry for my bad english, Im a german baby boy (;
    So, I wrote a Bitmaploader, too. But in OOP (ObjectOrientatedProgramming)
    The text's in the Messagebox's are in German, if you guys want it in
    english i can also write it in english. so, have fun.
    Code:
    #include "fstream"
    class Bitmap
    {
     public:
    	 void *LoadBitmap(const char *Filename);
    
    	 //BITMAPFILEHEADER
    	 typedef struct 
    	 {
    	 WORD bfType;
    	 DWORD bfSize;
    	 DWORD bfReserved;
    	 DWORD bfOffBits;
    	 }BITMAPFILEHEADER;
    
    	 //BITMAPINFOHEADER
    	 typedef struct 
    	 {
    	 DWORD biSize;
    	 LONG biWidth;
    	 LONG biHeight;
    	 WORD biPlanes;
    	 WORD biBitCount;
    	 DWORD biCompression;
    	 DWORD biSizeImage;
    	 LONG biXPelsPerMeter;
    	 LONG biYPelsPerMeter;
    	 DWORD biClrUsed;
    	 DWORD biClrImportant;
    	 }BITMAPINFOHEADER;
    
    	 BITMAPFILEHEADER FileHeader;
    	 BITMAPINFOHEADER InfoHeader;
    };
    
    void *Bitmap::LoadBitmap(const char *Filename)
    {
     Bitmap o_Bitmap;
     unsigned char *bitmap_Image;
     FILE *File=NULL;
     unsigned int ImageIdx=0;
     if(!Filename)
     {
     MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
     }
     else
     {
     File=fopen(Filename,"rb");
     }
     fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
     if(FileHeader.bfType != 0x4D42)
     {
     MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
     }
     fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
     fseek(File,o_Bitmap.FileHeader.bfOffBits,SEEK_SET);
     if(o_Bitmap.InfoHeader.biSizeImage != 0)
     {
     bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
     }
     if(!bitmap_Image)
     {
     free(bitmap_Image);
     fclose(File);
     }
     if(bitmap_Image==NULL)
     {
     fclose(File);
     }
     for(ImageIdx=0;ImageIdx < o_Bitmap.InfoHeader.biSizeImage; ImageIdx+=3)
     {
     bitmap_Image[ImageIdx] = bitmap_Image[+2];
     bitmap_Image[ImageIdx+2] = bitmap_Image[ImageIdx];
     }
     fclose(File);
     return bitmap_Image;
    };
    Thanks
    Last edited by si_the_geek; Oct 13th, 2008 at 05:53 PM. Reason: removed advertising link

  6. #6
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hey guys,
    i just wanted to say that I can now handle the RunLengthEncoding (RLE). To be precise the RLE4 (8 Bit) and RLE8 (24 Bit). If some guys are interested in the SourceCode, I can post it. But if there comes no feedback, I don't post it...So let me know if you want to see the full length of code!
    Thanks!

  7. #7
    New Member
    Join Date
    Oct 2008
    Posts
    9

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hi!
    Yes I would like to see the entire code.
    I was following this to, but didn't login yet.

    Thx for the help i got here so far

  8. #8
    New Member
    Join Date
    May 2017
    Posts
    3

    Cool Re: C/C++ - Loading Bitmap Files (Manually)

    i were working with your code, plus win32 system calls plus winbgim win api, this code also works, so:

    #include <winbgim.h>
    #include <string.h>
    #include<iostream>
    #include<windows.h>
    #include<stdio.h>
    #include <stdlib.h>
    #include <fstream.h>
    const char *Filename;

    WORD bibitcount;


    BITMAPFILEHEADER FileHeader;
    BITMAPINFOHEADER InfoHeader;
    int k;
    typedef struct{
    BYTE colorr;
    BYTE colorg;
    BYTE colorb;
    }cine;

    cine color;

    HANDLE hArch, hProy;
    LPSTR base,puntero;
    DWORD tam;
    int main()
    {
    char *p;
    char *base;
    DWORD buf;
    Filename="C:\\path to file\\mybmp.BMP";
    int gd = DETECT, gm;
    int x = 320, y = 240, radius;
    k=0;
    initgraph(&gd, &gm, "C:\\path to BGI folder\\BGI");//download TC 3.0 for dos
    int i;
    int j;
    char g[20];



    FILE *File=NULL;
    if(!Filename)
    {
    MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
    }
    else
    {
    File=fopen("C:\\path to my file\\mybmp.BMP","rb");
    }
    if(FileHeader.bfType != 0x4D42)
    {
    MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
    exit(1);
    }
    fread(&FileHeader,sizeof(BITMAPFILEHEADER),1,File);

    printf("tamaño total del archivo %d\n",FileHeader.bfSize);
    printf("comienzo del mapa de bits (imagen en pixels) en bits %d\n",FileHeader.bfOffBits);

    buf=FileHeader.bfOffBits/8; //offset from the begining of BMP file (pixel array)
    printf("comienzo del mapa de bits en bytes desde el origen del archivo %d\n",buf);
    fread(&InfoHeader,sizeof(BITMAPINFOHEADER),1,File);

    printf("horizontal resolution in pixels por metro %li\n",InfoHeader.biWidth);
    printf("vertical resolution in pixels por metro %li\n",InfoHeader.biHeight);
    printf("numero de bits por pixel %d", InfoHeader.biBitCount);

    fclose(File);

    hArch = CreateFile("bar.bmp", /* file name */
    GENERIC_ALL , /* read/write access */
    0, /* no sharing of the file */
    NULL, /* default security */
    OPEN_ALWAYS, /* open new or existing file */
    FILE_ATTRIBUTE_NORMAL, /* routine file attributes */
    NULL); /* no file template */
    if (hArch==INVALID_HANDLE_VALUE){

    fprintf(stderr,"no puede abrirse el archivo");

    }

    hProy = CreateFileMapping(hArch, /* file handle */
    NULL, /* default security */
    PAGE_READWRITE, /* read/write access to mapped pages */
    0, /* map entire file */
    0,
    TEXT("SharedObject")); /* named shared memory object */

    /* write to shared memory */
    base=(LPSTR)MapViewOfFile(hProy,FILE_MAP_ALL_ACCESS,0,0,0);
    tam=GetFileSize(hArch,NULL);
    int cont=0;
    puntero=base;
    p=base+buf;
    k=0;

    for (i=0;i<InfoHeader.biHeight;i++)
    for(j=0;j<InfoHeader.biWidth;j++)

    {

    putpixel(j+6,InfoHeader.biHeight-i,COLOR(int(*(p+k)),int(*(p+k+2)),int(*(p+k+1))));
    k=k+3;
    }

    getch();
    }

  9. #9
    New Member
    Join Date
    May 2017
    Posts
    3

    Post Re: C/C++ - Loading Bitmap Files (Manually)

    maybe in my program is necesary to write:initwindow(400,400) in the top of the main function to open the graphic window, it depends on the winbgim installation

  10. #10
    New Member
    Join Date
    Oct 2008
    Posts
    9

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hello again

    Afther my long weekend i'm back on the work again.
    I was wondering, could it be possible to make this in c# to ?

  11. #11
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hey,
    what do you mean, my Bitmapdecoder or the Tutorials on the site i gave you!?

  12. #12
    New Member
    Join Date
    Oct 2008
    Posts
    9

    Re: C/C++ - Loading Bitmap Files (Manually)

    no, the file i downloaded.
    but got it to work now

    At the moment i'm writing a rapport on all this.
    and i'm looking for a way to generate a bmp file in c#

    so that when i launch the program i would be able to say what the height is , the width and so . Even what colour.

    i must say, i find that alot more difficult then reading one

  13. #13
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hey, first of all, I'm not sure about youre saying me^^
    So, I just looked on "wikipedia" for the article. You just have to look, what size the variables must have to load in there the Bitmapinformations. If you don't know what to do, you can cheat with my loader.

    I think the c-compiler problems are a few commandos. You have to search them!

    For example, the fread()-command:
    http://www.google.de/search?hl=de&q=...le-Suche&meta=
    You know? You just have to look for the commands and to replace them.
    I hope i helped you a lil bit...(:

    The next weeks me and my mates want to erase the official site. We want to have a new, she's gonna be very good. So check it up, the next time ;-)

    Good luck with the Bitmapdecoder.

  14. #14
    New Member
    Join Date
    Oct 2008
    Posts
    9

    Re: C/C++ - Loading Bitmap Files (Manually)

    the decoder works fine by now

    and indeed, on wikipedia there is allot of information about bmp's.
    Very usefull to. Be sure that when your new site is on to post a link here.
    so i can have watch at it . btw will it be in German to ? or in English.

  15. #15
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hey (:
    Cool, so it works.
    The new site will have a phpbb-Forum, so you can switch between the English or Germanlanguage ;-)
    I'll post the link here.

  16. #16
    Banned
    Join Date
    Mar 2009
    Posts
    17

    Re: C/C++ - Loading Bitmap Files (Manually)

    this is interesting but I want to know that is it work in turbo compiler also.
    And which compiler spot that coding.

  17. #17
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hey,

    sry, I can't decrypt your question. When you want to know, if you can write this decoder in Turbo-Pascal, i think yes, it has to be. You just have to know, how to open binary-files and how to read in the data-blocks.

    pivke

  18. #18
    Banned
    Join Date
    Mar 2009
    Posts
    17

    Re: C/C++ - Loading Bitmap Files (Manually)

    Quote Originally Posted by pivke
    Hey,

    sry, I can't decrypt your question. When you want to know, if you can write this decoder in Turbo-Pascal, i think yes, it has to be. You just have to know, how to open binary-files and how to read in the data-blocks.

    pivke
    I have tried it in turbo compiler it is not working in the turbo compiler..

  19. #19
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: C/C++ - Loading Bitmap Files (Manually)

    Are you're sure, that you have created the right project? You have to include some Headers. Please show me your Error-Log.

  20. #20
    New Member
    Join Date
    Aug 2011
    Posts
    13

    Re: C/C++ - Loading Bitmap Files (Manually)

    Hi and Help! I can't open texture files when writing a OpenGL screen saver in windows envirnoment. Could anyone tell me how to load bitmap textures in screen saver programming??

  21. #21
    New Member
    Join Date
    Sep 2021
    Posts
    1

    Re: C/C++ - Loading Bitmap Files (Manually)

    What do the WORD, DWORD and LONG word represent?

  22. #22
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: C/C++ - Loading Bitmap Files (Manually)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width