bitmap to 2D binary array??

Mm

Go to GitHub. Search for Adafruit TFT LCD. Pretty much any one of those will have an example of reading a BMP.

Why are you reading your file into a 2D array? You do know that a BMP is not just raw data; It actually has a structure to it with a bunch of extra information and formatting.

Read this BMP File Format

I wrote something that could read and draw BMPs years ago when I was still learning, but here is a list of the header. There are some updates, so when you see "The following entries are possible", there are more options, just read the Wiki page

/*================================================================================================================================
offset	field				size		contents
0000h	identifier			2 bytes		the characters identifying the bitmap. the following entries are possible: BM
0002h	file size			4 bytes		complete file size in bytes.
0006h	reserved			4 bytes		reserved for later use.
000ah	bitmap data offset	4 bytes		offset from beginning of file to the beginning of the bitmap data.
000eh	bitmap header size	4 bytes		length of the bitmap info header used to describe the bitmap colors, compression, ? the following sizes are possible:
0012h	width				4 bytes		horizontal width of bitmap in pixels.
0016h	height				4 bytes		vertical height of bitmap in pixels.
001ah	planes				2 bytes		number of planes in this bitmap.
001ch	bits per pixel		2 bytes		bits per pixel used to store palette entry information. this also identifies in an indirect way the number of possible colors. possible values are: (bits per bitmap data entry)
											1 - monochrome bitmap
											4 - 16 color bitmap
											8 - 256 color bitmap
											16 - 16bit (high color) bitmap
											24 - 24bit (true color) bitmap
											32 - 32bit (true color) bitmap
001eh	compression			4 bytes		compression specifications. the following values are possible:
							0 - none (also identified by bi_rgb)
											1 - rle 8-bit / pixel (also identified by bi_rle4)
											2 - rle 4-bit / pixel (also identified by bi_rle8)
											3 - bitfields (also identified by bi_bitfields)
0022h	bitmap data size	4 bytes		size of the bitmap data in bytes. this number must be rounded to the next 4 byte boundary.
0026h	hresolution			4 bytes		horizontal resolution expressed in pixel per meter.
002ah	vresolution			4 bytes		vertical resolution expressed in pixels per meter.
002eh	colors				4 bytes		number of colors used by this bitmap. for a 8-bit / pixel bitmap this will be 100h or 256.
0032h	important colors	4 bytes		number of important colors. this number will be equal to the number of colors when every color is important.
0036h	palette				n * 4 byte	the palette specification. for every entry in the palette four bytes are used to describe the rgb values of the color in the following way:
											1 byte for blue component
											1 byte for green component
											1 byte for red component
											1 byte filler which is set to 0 (zero)
							NOTE : IF A TRUE COLOR BITMAP(16bit/24bit/32bit) THEN THESE VALUES BECOME THE BITMAP DATA WHICH IS OTHERWISE LOCATED AT 436h AND THERE IS NO 1 BYTE FILLER OR PALETTE AND EVERY LINE MUST END IN A DIVISION OF 32BITS
0436h	bitmap data			x bytes		depending on the compression specifications, this field contains all the bitmap data bytes which represent indices in the color palette.	
==================================================================================================================================*/

The amount of added whitespace to each line can be calculated as

4 - (width * bytes per pixel) % 4
//the amount of added whitespace in bytes per line, bytes per pixel is usually 3 for 24 bit BGR

MorganS:
Go to GitHub. Search for Adafruit TFT LCD. Pretty much any one of those will have an example of reading a BMP.

Dear MorganS my supervisor don't allow me to use it, i just need to get 2D binary array in form of zeros & ones. Just like we do in matlab

a=imread('2b.BMP');
b1= im2bw(a); %to binary

Ps991:
Why are you reading your file into a 2D array? You do know that a BMP is not just raw data; It actually has a structure to it with a bunch of extra information and formatting.

Read this BMP File Format

I wrote something that could read and draw BMPs years ago when I was still learning, but here is a list of the header. There are some updates, so when you see "The following entries are possible", there are more options, just read the Wiki page

/*================================================================================================================================

offset field size contents
0000h identifier 2 bytes the characters identifying the bitmap. the following entries are possible: BM
0002h file size 4 bytes complete file size in bytes.
0006h reserved 4 bytes reserved for later use.
000ah bitmap data offset 4 bytes offset from beginning of file to the beginning of the bitmap data.
000eh bitmap header size 4 bytes length of the bitmap info header used to describe the bitmap colors, compression, ? the following sizes are possible:
0012h width 4 bytes horizontal width of bitmap in pixels.
0016h height 4 bytes vertical height of bitmap in pixels.
001ah planes 2 bytes number of planes in this bitmap.
001ch bits per pixel 2 bytes bits per pixel used to store palette entry information. this also identifies in an indirect way the number of possible colors. possible values are: (bits per bitmap data entry)
1 - monochrome bitmap
4 - 16 color bitmap
8 - 256 color bitmap
16 - 16bit (high color) bitmap
24 - 24bit (true color) bitmap
32 - 32bit (true color) bitmap
001eh compression 4 bytes compression specifications. the following values are possible:
0 - none (also identified by bi_rgb)
1 - rle 8-bit / pixel (also identified by bi_rle4)
2 - rle 4-bit / pixel (also identified by bi_rle8)
3 - bitfields (also identified by bi_bitfields)
0022h bitmap data size 4 bytes size of the bitmap data in bytes. this number must be rounded to the next 4 byte boundary.
0026h hresolution 4 bytes horizontal resolution expressed in pixel per meter.
002ah vresolution 4 bytes vertical resolution expressed in pixels per meter.
002eh colors 4 bytes number of colors used by this bitmap. for a 8-bit / pixel bitmap this will be 100h or 256.
0032h important colors 4 bytes number of important colors. this number will be equal to the number of colors when every color is important.
0036h palette n * 4 byte the palette specification. for every entry in the palette four bytes are used to describe the rgb values of the color in the following way:
1 byte for blue component
1 byte for green component
1 byte for red component
1 byte filler which is set to 0 (zero)
NOTE : IF A TRUE COLOR BITMAP(16bit/24bit/32bit) THEN THESE VALUES BECOME THE BITMAP DATA WHICH IS OTHERWISE LOCATED AT 436h AND THERE IS NO 1 BYTE FILLER OR PALETTE AND EVERY LINE MUST END IN A DIVISION OF 32BITS
0436h bitmap data x bytes depending on the compression specifications, this field contains all the bitmap data bytes which represent indices in the color palette.
==================================================================================================================================*/




The amount of added whitespace to each line can be calculated as 

4 - (width * bytes per pixel) % 4
//the amount of added whitespace in bytes per line, bytes per pixel is usually 3 for 24 bit BGR

Thanks Alot dear @Ps991 for the guideline. I will hopefully study bitmap's depth later.
can you please help me out in converting any monochromatic Binary image in to 2D binary array in arduino just like we do in matlab

a=imread('2b.jpg');
b1= im2bw(a); %to binary

although i have searched alot but couldn't find even a single function which could do it, by the way i have posted my code. If you can help me, that will be a honor.

Here is a simple plan of attack if you already know it is a 1-bit monochrome (black and white) BMP

  1. read 0x0Ah (10) to get the starting address, of the byte where the bitmap image data (pixel array) can be found
  2. read 0x12h (18) to get the width in pixels
  3. read 0x16h (22) to get the height in pixels
  4. read the bitmap data starting at the location you found in step 1. You will need to read (width * height) / 8 bytes of data.

The pixel values are stored in each bit, with the first (left-most) pixel in the most-significant bit of the first byte. Each bit is an index into a table of 2 colors. An unset bit will refer to the first color table entry, and a set bit will refer to the last (second) color table entry. <-- You need not worry about this if you just want black and white, but I just wanted to show you that 1 bit refers to 2 colors located in the color table, not necessarily black and white.

I think your supervisor want you to WRITE it not to FIND it ...