Results 1 to 32 of 32

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

  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
    Oct 2008
    Posts
    14

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

    Hey!
    Very cool. So, how i said one time before, the Bitmapdecoder can read
    all RLE's. I've added 2 more methods, in this the next operation i will
    get starting to overgive the textureinformations to the OpenGL-Paraneters
    to laod the texture. So, if you want i can also write from the
    OpenGL-Operations in the Win32-API.
    So, here is the Code:
    Code:
    #include "fstream"
    class Bitmap
    {
    private:
    	unsigned long BPP;
    	unsigned long width;
    	unsigned long height;
    	unsigned long size;
    	unsigned char *bitmap_Image;
    	unsigned int bps;
    	GLuint KompressionFormat;
    
     public:
    	 Bitmap();
    	 ~Bitmap();
    
    	 void *LoadBitmap(const char *Filename);
    	 void Load8BitKompression(const char *Filename);
    	 void Load24BitKompression(const char *Filename);
     
    	 #define BI_RGB   0L
    	 #define BI_RLE4  2L 
    	 #define BI_RLE8  4L
    
    	 #pragma pack(push,1);
    	 //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;
    
       	 #pragma pack(pop);
     
    	 void Kompression();
     
    	 BITMAPFILEHEADER FileHeader;
    	 BITMAPINFOHEADER InfoHeader;
    };
    
    Bitmap::Bitmap()
    {
    	BPP=0;
    	width=0;
    	height=0;
    	bitmap_Image=NULL;
    	size=0;
    	bps=0;
    	KompressionFormat=0;
    }
    
    Bitmap::~Bitmap()
    {
    	BPP=NULL;
    	width=NULL;
    	height=NULL;
    	delete[] bitmap_Image;
    	KompressionFormat=NULL;
    	size=NULL;
    	bps=NULL;
    }
     
    void *Bitmap::LoadBitmap(const char *Filename)
    {
     Bitmap o_Bitmap;
     FILE *File=NULL;
     unsigned int ImageIdx=0;
     unsigned char *bitmap_Image=NULL;
     
     if(!Filename)
     {
    	MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
    	return false;
     }
     else
     {
    	File=fopen(Filename,"rb");
    	if(!File)
    	{
    	 MessageBox(NULL,"'File' hat keinen Inhalt!","Error",MB_OK|MB_ICONERROR);
    	 return 0;
    	}
     }
    
      fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
     if(ferror(File))
     {
    	MessageBox(NULL,"'FileHeader' konnte nicht gelesen werden!","Error",MB_OK|MB_ICONERROR);
    	fclose(File);
    	return 0;
     }
     
    
      fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
     if(ferror(File))
     {
    	MessageBox(NULL,"'InfoHeader' konnte nicht gelesen werden!","Error",MB_OK|MB_ICONERROR);
    	fclose(File);
    	return 0;
     }
     
     fseek(File,o_Bitmap.FileHeader.bfOffBits,SEEK_SET);
     if(ferror(File))
     {
    	MessageBox(NULL,"Zu 'bfOffBits' konnte nicht gesprungen werden!","Error",MB_OK|MB_ICONERROR);
    	fclose(File);
    	return 0;
     }
    
     if(o_Bitmap.InfoHeader.biSizeImage != 0)
     {
    	 bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
    	fread(bitmap_Image,o_Bitmap.InfoHeader.biSizeImage,1,File);
     }
    
     if(o_Bitmap.FileHeader.bfType != 0x4D42)
     {
    	MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
    	fclose(File);
     }
    
      if(!bitmap_Image)
     {
    	free(bitmap_Image);
    	fclose(File);
     }
    
     if(bitmap_Image==NULL)
     {
    	fclose(File);
     }
     
      switch(o_Bitmap.InfoHeader.biCompression)
      {
      case BI_RGB:
    	   MessageBox(NULL,"Standardkompression ist aktiviert 'BI_RGB'!","Info",MB_OK);
    	   break;
     
      case BI_RLE4:
    	   Load8BitKompression(Filename);
    	   break;
     
      case BI_RLE8:
    	   Load24BitKompression(Filename);
    	   break;
     
      default:
    	  MessageBox(NULL,"Keine Kompression vorhanden","Error",MB_OK|MB_ICONERROR);
      }
     
     fclose(File);
    };
     
    void Bitmap::Load8BitKompression(const char *Filename)
    {
    	Bitmap o_Bitmap;
    	FILE *File=NULL;
    	MessageBox(NULL,"'BI_RLE4'-Kompression ist aktiviert!","Info",MB_OK);
    	File=fopen(Filename,"rb");
        fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
    	fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
    
    	if(FileHeader.bfType != 8)
    	{
    		MessageBox(NULL,"8-Bitzustand konnte nicht gefunden werden!","Error",MB_OK|MB_ICONERROR);
    		fclose(File);
    	}
    
    	BPP=o_Bitmap.InfoHeader.biBitCount; 
    	width=o_Bitmap.InfoHeader.biHeight;
    	height=o_Bitmap.InfoHeader.biHeight;
    	KompressionFormat=GL_RGB;
    
    	if(InfoHeader.biSizeImage != 0)
    	{
    		bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
    		fread(bitmap_Image,o_Bitmap.InfoHeader.biSizeImage,1,File);
    	}
    
    };
     
    void Bitmap::Load24BitKompression(const char *Filename)
    {
    	Bitmap o_Bitmap;
    	FILE *File=NULL;
        MessageBox(NULL,"'BI_RLE8'-Kompression ist aktiviert!","Info",MB_OK);
    	File=fopen(Filename,"rb");
        fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
    	fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
    
    	if(o_Bitmap.FileHeader.bfType != 24)
    	{
    		MessageBox(NULL,"24-Bitzustand konnte nicht gefunden werden!","Error",MB_OK|MB_ICONERROR);
    		fclose(File);
    	}
    
    	BPP=o_Bitmap.InfoHeader.biBitCount; 
    	width=o_Bitmap.InfoHeader.biHeight;
    	height=o_Bitmap.InfoHeader.biHeight;
    	KompressionFormat=GL_COLOR_INDEX;
    };
    So, i don't think that you can find an error or mistake. I hope you enjoy it and thanks for everything.

    (Sry for my bad english, im german!)

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

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

    It still gives many error's .
    I can generate a file whit it . But the file is not usefull.

    Any idea how this can be ?

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

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

    So, there can be a few reasons. So, you have to create a Win32-Application. It doesn't work if you just include it. I don't exactly what problem you have, but the Includes you have to include are:
    Code:
    #include <windows.h>		
    #include <gl\gl.h>			
    #include <gl\glu.h>			
    #include <glaux.h>		
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    #include "Bitmap.h"
    The path of the OpenGL's libs mustn't be right, i installed them on "gl\...". Which Compiler [IDE] do you use? I can write for you in Win32-API or in DirectX if you want...just say (:

    Maybe the problem is just you don't know how to do this all, i just uploaded the whole project for you, here: (You must have OpenGL for this (-
    http://www.megaupload.com/de/?d=OE2VLS8J

    Another way (I think it's better), youre registering on my site:
    (Please don't erase !!Mods!!!)
    http://www.Online-Programmierung.com

    So just register under "Forum" and write your problem in it, i or some guys will help you. In this subforum aren't accepted a big quantity of posts ;-).

    I hope i have helped you, so if there a few more questions or problems, just register ;-).

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

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

    Yes i have used win-32 projects before.
    But i didnt realise that i needed to make one for this program to.

    When i make a win-32 one, to add the gl/.. thing i need to do something the configs.

    At the linker thing, the input => additional dependencies => and here i must put some type for code.

    I forgot what it was. Do U think u know what it is.

    So far you helped me allot, and sorry for my english to. I'm Flemish.

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

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

    And it doesnt work to register on the forum.
    At the momment i"m not at home. So I will dwnload it later on.

    Thx for all the help allready

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

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

    @Forum
    I created a Account for you, I sended you the access dates per PM (Personal Message).

    @To your problem
    Sry, I have to write some candidacies. So, at this problem no time. Just rewrite with your acces dates and I will answer soon. Sry for all the problems ;-)

    http://www.Online-Programmierung.com

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

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

    @ forum

    it doesnt seem to reconaise my account...

    @ problem
    i made a win-32 version to paste it in.
    and i ajusted the properties so the gl\ should work
    It seems that the path to the project is wrong.
    I am using c++ express edition btw.


    The program looks pretty good i must say.A fieuw translation things that i need to make and it's 100%

    /thx

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

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

    @forum
    Maybe you wont to post there, but ok....
    @problem
    You should try to compile this projectfile:
    http://www.joachimrohde.com/cms/xoop...icle.php?id=10
    This must work, if it does'nt, we know that your problem is the OpenGL-Include...;-)

    If it works, just put in the Headers in the main and create a new Header and copy in there the bitmapdecoder....And yea, you have to call the function in the main.

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

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

    thanks!

    I understand some german , but to read it, thats something ellse.
    In belgium we have another 5 day holiday at the end of this i'll reply whit a report on if i get it to work.


    But i'm sure i'll be able to lett it work so

  17. #17
    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 ?

  18. #18
    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!?

  19. #19
    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

  20. #20
    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.

  21. #21
    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.

  22. #22
    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.

  23. #23
    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.

  24. #24
    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

  25. #25
    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..

  26. #26
    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.

  27. #27
    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??

  28. #28
    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();
    }

  29. #29
    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

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

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

    thats the working code for 32 bit windows machine, in my example above i made a mistake, making an if without reading the file, in 64 bit machines it`s necesary to include iostream and fstream, that's how I load a BMP file of 24 bits per pixel, not for indexed coloured bitmaps, download winbgim from here: http://www.mediafire.com/file/z9wnl0...evCpp.zip/file, is in dev-c++, you probably have to load a couple of parameters for the linker, the path is: project, project options, parameters, and write: -lbgi -lgdi32 -luser32 -lcomdlg32 -luuid -loleaut32 -lole32 -mwindows -lwinmm, one thing more, after installing winbgim, go to dev c and create new project as console graphics application, this item must be in the project list if winbgim is properly installed, I wrote "write to shared memory", because I wrote the file also in memory although I did not access the file this way, but from the same file so:

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

    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()
    {
    int gdriver=9;
    int gmode=2;
    // initgraph(&gdriver,&gmode, "");
    cleardevice();

    UnmapViewOfFile(base);
    CloseHandle(hProy);
    CloseHandle(hArch);
    char *p;
    char *base;
    DWORD buf;
    Filename="D:\\music\\IMSLP00795-BWV0971\\01.bmp";
    int gd = DETECT, gm;
    int x = 320, y = 240, radius;
    k=0;

    int i;
    int j;




    FILE *File=NULL;
    if(!Filename)
    {
    MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
    }
    else
    {
    File=fopen("D:\\music\\IMSLP00795-BWV0971\\01.bmp","rb");
    }

    fread(&FileHeader,sizeof(BITMAPFILEHEADER),1,File);
    if(FileHeader.bfType != 0x4D42)
    {
    MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
    exit(1);
    }
    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);
    initwindow(InfoHeader.biWidth,InfoHeader.biHeight);


    hArch = CreateFile("D:\\music\\IMSLP00795-BWV0971\\01.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+FileHeader.bfOffBits;
    k=0;int t=0,v,l;
    fseek(File,FileHeader.bfOffBits,SEEK_SET );
    int read=0,read2=0;
    k=0;
    for( i=0; i<InfoHeader.biWidth; i++ ) {
    fread(&color,sizeof(cine),1,File);
    read += sizeof(cine);
    printf( "Pixel %d: %3d %3d %3d\n", i+1, int(color.colorb), int(color.colorg), int(color.colorr) );
    }
    if( read % 4 != 0 ) {
    read2 = 4 - (read%4);
    printf( "Padding: %d bytes\n", read2 );
    //fread( &color, read2, 1, File );

    }
    fseek(File,FileHeader.bfOffBits,SEEK_SET );
    for (i=0;i<InfoHeader.biHeight;i++)
    for(j=0;j<InfoHeader.biWidth ;j++)

    {
    fread(&color,sizeof(cine),1,File);

    putpixel(j,InfoHeader.biHeight- i,COLOR(int(color.colorb),int(color.colorg),int(color.colorr)));
    if(j==InfoHeader.biWidth-1&&read2!=0)fseek(File,read2,SEEK_CUR);
    }
    fclose(File);

    UnmapViewOfFile(base);
    CloseHandle(hProy);
    CloseHandle(hArch);
    getch();
    }
    Last edited by Carl G; Sep 8th, 2021 at 02:17 PM.

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

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

    What do the WORD, DWORD and LONG word represent?

  32. #32
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,854

    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