I want to read a bmp file to my program, how can I do that? Can I read it directly?
Please be more specific about what you're actually trying to do. Do you need to do this at compile-time, or at run-time?
I want to load a external BMP file and show it in the external device. So I want to compile the BMP file.
This has been discussed before - try a search here, and at http://www.8052.com/find8052.phtml Basically, you will need to extract the binary data from the BMP file, possibly convert it to something suitable for your application, and then either make it into a HEX file to combine with your code, or make a 'C' source representation of it.
If I am understanding your requirements correctly, 010 Editor (an inexpensive Windows application) can do this interactively or programmatically. See http://www.sweetscape.com/010editor/ For example, the tool exported a .bmp file exported to a C source file:
//------------------------------------------------------------ //----------- Created with 010 Editor ----------- //------ http://www.sweetscape.com/010editor/ ------ // // File : C:\WINNT\Blue Lace 16.bmp // Address : 0 (0x0) // Size : 1272 (0x4F8) //------------------------------------------------------------ unsigned char hexData[1272] = { 0x42, 0x4D, 0xF8, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC3, 0x0E, 0x00, 0x00, 0xC3, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x44, 0x44, 0x44, 0x4C, 0x44, 0x44, 0x44, 0x44, 0xC4, 0xC4, 0x44, 0x44, 0x44, 0x44, 0xC4, 0xC4, 0x44, 0x44, /* ... */ 0x44, 0x44, 0x44, 0x44, 0xC4, 0x4C, 0x44, 0x44, 0x44, 0x44, 0xCC, 0xC4, 0x44, 0x44, 0x00, 0x00 };
http://www.keil.com/forum/docs/thread133.asp http://www.keil.com/forum/docs/thread336.asp
http://www.8052.com/forum/read.phtml?id=1577 http://www.8052.com/forum/read.phtml?id=32893 http://www.8052.com/forum/read.phtml?id=38772 http://www.8052.com/forum/read.phtml?id=53641
Dan, Presumably this is just a byte-by-byte representation of the .BMP file's contents? Before this could be used by the embedded device, the appropriate information would need to be extracted from the .BMP file and converted to a suitable form for the embedded device? eg, the first few bytes are just header information, including:
unsigned char hexData[1272] = { 0x42, 0x4D, // File Identifier - "BM" 0xF8, 0x04, 0x00, 0x00, 0x00, // File Size
"Presumably this is just a byte-by-byte representation of the .BMP file's contents?" Correct "Before this could be used by the embedded device, the appropriate information would need to be extracted from the .BMP file and converted to a suitable form for the embedded device?" I suppose that depends on the embedded application. For example, one application's BMP rendering implementation might be flexible and would use the parameters contained in the BITMAPINFO structure, while another application's implementation might be designed to only support a fixed format with parameters established by a priori agreement. The latter case would require preprocessing the BMP, but seems more reasonable for applications where an 8051 would be used. The nifty thing about 010 Editor is that all you need to do is define a template (e.g., from the likes of file format descriptions on wotsit.org) describing the file's structure in a very C-like way and then you can write very C-like scripts to programmatically manipulate those files. The contents of SweetScape's BMP template below, which only defines how 010 Editor should display a file, might give you some idea how easy it would be to use built-in I/O functions (having similar C-like syntax) to perform custom file manipulation or data extraction. Of course one could just write a little C program to do it too ;-)
//----------------------------------- //--- 010 Editor v1.1 Binary Template // // File: BMPTemplate.bt // Author: SweetScape Software // Purpose: Defines a template for // parsing BMP files. //----------------------------------- // Define structures used in BMP files typedef struct { // bmfh CHAR bfType[2]; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER; typedef struct { // bmih DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER; typedef struct { // rgbq UBYTE rgbBlue; UBYTE rgbGreen; UBYTE rgbRed; UBYTE rgbReserved; } RGBQUAD; typedef struct { // rgbt UBYTE rgbBlue; UBYTE rgbGreen; UBYTE rgbRed; } RGBTRIPLE; //--------------------------------------------- // Define the headers LittleEndian(); SetBackColor( cLtGray ); BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; // Check for header if( bmfh.bfType != "BM" ) { Warning( "File is not a bitmap. Template stopped." ); return -1; } // Define the color table if( bmih.biBitCount != 24 ) { SetBackColor( cLtAqua ); if( bmih.biClrUsed > 0 ) RGBQUAD aColors[ bmih.biClrUsed ]; else RGBQUAD aColors[ 1 << bmih.biBitCount ]; } // Define the bytes of the data SetBackColor( cNone ); if( bmih.biCompression > 0 ) { // Bytes are compressed if( bmih.biSizeImage > 0 ) UBYTE rleData[ bmih.biSizeImage ]; else UBYTE rleData[ bmfh.bfSize - FTell() ]; } else { // Calculate bytes per line and padding required local int bytesPerLine = (int)Ceil( bmih.biWidth * bmih.biBitCount / 8.0 ); local int padding = 4 - (bytesPerLine % 4); if( padding == 4 ) padding = 0; // Define each line of the image struct BITMAPLINE { // Define color data if( bmih.biBitCount < 8 ) UBYTE imageData[ bytesPerLine ]; else if( bmih.biBitCount == 8 ) UBYTE colorIndex[ bmih.biWidth ]; else if( bmih.biBitCount == 24 ) RGBTRIPLE colors[ bmih.biWidth ]; // Pad if necessary if( padding != 0 ) UBYTE padBytes[ padding ]; } lines[ bmih.biHeight ]; }