Importing BITMAP

Hi,

I wanted to know is there any way to import bitmap image in a c++ program just like importing data from text (.txt) file and analyse it pixel by pixel?

Thanks in advance :)
closed account (4Gb4jE8b)
as far as i know, that is impossible to do without a GUI or an API.
1: the easiest way maybe is downloading a open source library to help you, like openCV2.2
2: read through the format of the bitmap you want to read, and write a program to decode it
Last edited on
The .bmp file format is very well-documented and easy to read, so you can simply open the .bmp file in binary mode, and follow the byte structure which you can find at Wikipedia - http://en.wikipedia.org/wiki/BMP_file_format - and other documentation sites.
@Ramses12
Thanks,
I had read this wiki link but i couldn't understand it. Can you point me to something basic and easy to start with (the absolute basics).
Last edited on
Well, I could explain you the basics of byte structures:
Any file is made of several bits (1s and 0s). A group of eight bits usually form a byte. When you open a file in any text editor like Notepad, every byte will be shown as a singular character (a letter, a number or a different symbol). That's a plain ASCII encoding. However, those bytes do not neccessarely represent characters; they are more like numbers from 0 to 255, which are interpreted by the program (in this case Notepad, who greates their graphical representation). But the program can do anything with that data, not only draw it's ASCII graphical representation. Say you want to store a variable in a file and load it later. Let's say that the variable holds the value 97. You can open the file in binary mode, and write a byte holding the value of 97. When you will open the file in Notepad, you will notice that there is displayed an "a". That's because the ASCII representation of number 97 is the small letter "a". Google "ASCII Table" if you want to find more info about this, because in this case it's not strictly relevant.
Later, when you load your variable back from the file, its value will obvioulsy be 97.

Now, you might be wondering what can you do if you want to store a variable larger than 255. The answer is simple: you use more than one byte:
Your initial variable: 528491
The variable written in the binary system: 10000001000001101011
This binary data is automatically outputed by your compiler into the file, until it becomes something like: k�, which of course does not make any sense when watching it as that.
I guess you know what data types are, so there's no need to explain what a 32-bit or a 16-bit integer is. Basically, when you declare a variable like this:
int test=20;
a portion of the computer's available memory is taken by this variable. Since the variable is an int, it will normally take 4 bytes of memory. Note that the variable will be stored in the floating memory just like you would normally store it in a binary file:
- a byte holding the value of 20
- three other bytes holding the value of 0
The blank bytes are still needed even if they are zero, because an int uses four bytes and no less. If for example the number is 258, there would be:
- a byte holding the value of 2
- a byte holdgin the value of 1
- two bytes holding the value of 0
because every more important byte is multiplied with a power of 256: 1x256+2=258.
Say you have the sequence: 52 2 0 0 which holds a 4-byte integer. To calculate the integer itself, you simply use: 2x256+52=564. And so on. But again, C++ does this automatically for you.

When you want to create a file, you might want to store more that one variable. This is when a data structure is useful. I assume you know what a structure is (otherwise read this article: http://cplusplus.com/doc/tutorial/structures/ ). When encoding a binary file, the whole structure is converted into binary data, and written, to be loaded later from that file.
Let's say that you execute the following code:

1
2
3
4
5
6
7
8
9
10
11
12
struct myStruct{
    short n1;
    short n2;
    long n3;
};
int main(){
   myStruct test;
   test.n1=212;
   test.n2=2162;
   test.n3=6000;
   return 0;
};


Watching directly into the computer's memory allocated for the "test" structure would show these bytes:

---------
212
0
---------
114
8
---------
112
23
0
0
---------

The first group is used to hold n1, the second one to hold n2 and the third one to hold n3 (as you might know, a long generally uses 4 bytes, and a short generally uses 2 bytes).
Getting to the practical part of copying the contents of the memory into an external file would be easy. All you have to know are these two articles:
http://cplusplus.com/doc/tutorial/files/
http://cplusplus.com/doc/tutorial/typecasting/

This is an example of how to write and read a byte structure to/from a file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
using namespace std;

struct byteStruct{
    long n1;
    short n2;
    short n3;
    long n4;
};
int main(){
    byteStruct myStruct;
    myStruct.n1=53;
    myStruct.n2=6234;
    myStruct.n3=212;
    myStruct.n4=12414;
    fstream myFile("test.txt",ios::binary|ios::out);
    char* buffer=reinterpret_cast<char*>(&myStruct);
    myFile.write(buffer,sizeof(myStruct));
    myFile.close();
    return 0;
};
If you would compile that, running it should create a file with 12 bytes in it. This was of course the writing process. For reading, make another source code and include this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
using namespace std;

struct byteStruct{
    long n1;
    short n2;
    short n3;
    long n4;
};
int main(){
    fstream myFile("test.txt",ios::binary|ios::in);
    char* buffer=new char[sizeof(byteStruct)];
    myFile.read(buffer,sizeof(myStruct));
    byteStruct* myStructPtr=reinterpret_cast<byteStruct*>(buffer);
    myFile.close();
    cout<<(*myStructPtr).n1<<endl;
    cout<<(*myStructPtr).n2<<endl;
    cout<<(*myStructPtr).n3<<endl;
    cout<<(*myStructPtr).n4<<endl;
    system("PAUSE");
    return 0;
};

This should display:
53
6234
212
12414
Press any key to continue...


Note: All codes were written in the forum window, so they might not be perfectly accurate.

I hope this helps!
Last edited on
The absolute easiest way I've ever seen is to use easyBMP; it's a cross-platform library for handling Windows-style bitmaps.

Version 1.0 is a single *.h file, containing everything you need. All you need do is include it in your build and you've got access to all its classes, structures and functions. Versions after 1.0 split the code a little, but it's still pretty easy.

It doesn't handle all possible kinds of BMP, but with any luck the ones you need to handle are covered, and whilst having the entire thing in a single *.h file isn't great coding practice, it does make including its functionality very easy.

1
2
3
4
5
BMP theBMPImage;
theBMPImage.ReadFromFile("somefile.bmp");

//Get RGBA value of one pixel, at location x,y
RGBApixel thePixelValues = theBMPImage.GetPixel(x,y);

Try this code, it works for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// put a bitmap image on a Windows Console display
// BCX basic original by Joe Caverly and Kevin Diggins
// BCX generated C code modified for PellesC/Dev-C++

#include <stdio.h>
#include <string.h>
#include <windows.h>    // Win32Api Header File

static HWND  hConWnd;

HWND BCX_Bitmap(char*,HWND=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0);
HWND GetConsoleWndHandle(void);


int main()
{
  hConWnd = GetConsoleWndHandle();
  if (hConWnd)
  {
    // select a bitmap file you have or use one of the files in the Windows folder
    // filename, handle, ID, ulcX, ulcY, width, height   0,0 auto-adjusts
    for(int x = 0; x< 100; x=x+10) {
	  BCX_Bitmap("logo.bmp",hConWnd,123,x,100,0,0);
	  Sleep(1000);
    }
    getchar();  // wait
  }
  return 0;
}


// draw the bitmap
HWND BCX_Bitmap(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Res,int Style,int Exstyle)
{
  HWND A;
  HBITMAP hBitmap;

  // set default style
  if (!Style) Style = WS_CLIPSIBLINGS|WS_CHILD|WS_VISIBLE|SS_BITMAP|WS_TABSTOP;

  // form for the image
  A = CreateWindowEx(Exstyle,"static",NULL,Style,X,Y,0,0,hWnd,(HMENU)id,GetModuleHandle(0),NULL);

  // Text contains filename
  hBitmap=(HBITMAP)LoadImage(0,Text,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);

  // auto-adjust width and height
  if (W || H) hBitmap = (HBITMAP)CopyImage(hBitmap,IMAGE_BITMAP,W,H,LR_COPYRETURNORG);
  SendMessage(A,(UINT)STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
  if (W || H) SetWindowPos(A,HWND_TOP,X,Y,W,H,SWP_DRAWFRAME);
  return A;
}


// tricking Windows just a little ...
HWND GetConsoleWndHandle(void)
{
  HWND hConWnd;
  OSVERSIONINFO os;
  char szTempTitle[64], szClassName[128], szOriginalTitle[1024];

  os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  GetVersionEx( &os );
  // may not work on WIN9x
  if ( os.dwPlatformId == VER_PLATFORM_WIN32s ) return 0;

  GetConsoleTitle( szOriginalTitle, sizeof ( szOriginalTitle ) );
  sprintf( szTempTitle,"%u - %u", GetTickCount(), GetCurrentProcessId() );
  SetConsoleTitle( szTempTitle );
  Sleep( 40 );
  // handle for NT and XP
  hConWnd = FindWindow( NULL, szTempTitle );
  SetConsoleTitle( szOriginalTitle );

  // may not work on WIN9x
  if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
  {
    hConWnd = GetWindow( hConWnd, GW_CHILD );
    if ( hConWnd == NULL ) return 0;
    GetClassName( hConWnd, szClassName, sizeof ( szClassName ) );
    // while ( _stricmp( szClassName, "ttyGrab" ) != 0 )
    while ( strcmp( szClassName, "ttyGrab" ) != 0 )
    {
      hConWnd = GetNextWindow( hConWnd, GW_HWNDNEXT );
      if ( hConWnd == NULL ) return 0;
      GetClassName( hConWnd, szClassName, sizeof( szClassName ) );
    }
  }
  return hConWnd;
}



http://codewall.blogspot.com
@ Ramses12:
Woah!...
Thanks a lot pal! I understood many things in your post.
Its nice you took out time to write it in detail..
I owe you a lot...!!!
found further info here: http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
thanks a ton! :)
You're welcome mate ;)
Topic archived. No new replies allowed.