Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"                   //  for mbed
00002 LocalFileSystem     local("local"); //  for mbed to access local file system
00003 
00004 /*
00005  *
00006  *   BMP file data handler sample
00007  *
00008  *      reference : http://www.umekkii.jp/data/computer/file_format/bitmap.cgi
00009  *
00010  */
00011 
00012 #include    <stdio.h>
00013 
00014 //#define        SOURCE_FILE            "/local/small.bmp"
00015 #define        SOURCE_FILE            "/local/pre4.bmp"
00016 //#define        SOURCE_FILE            "pre_dsp_4.bmp"
00017 
00018 #define        BUFFER_SIZE            1024
00019 
00020 typedef struct  bmp_header_st    {
00021     unsigned short  bfType            __attribute__((packed));
00022     unsigned long   bfSize            __attribute__((packed));
00023     unsigned short  bfReserved1       __attribute__((packed));
00024     unsigned short  bfReserved2       __attribute__((packed));
00025     unsigned long   bfOffBits         __attribute__((packed));
00026 
00027     unsigned long   biSize            __attribute__((packed));
00028     long            biWidth           __attribute__((packed));
00029     long            biHeight          __attribute__((packed));
00030     unsigned short  biPlanes          __attribute__((packed));
00031     unsigned short  biBitCount        __attribute__((packed));
00032     unsigned long   biCompression     __attribute__((packed));
00033     unsigned long   biSizeImage       __attribute__((packed));
00034     long            biXPixPerMeter    __attribute__((packed));
00035     long            biYPixPerMeter    __attribute__((packed));
00036     unsigned long   biClrUsed         __attribute__((packed));
00037     unsigned long   biCirImportant    __attribute__((packed));
00038 }
00039 bmp_header;
00040 
00041 typedef struct  color_palette_st    {
00042     unsigned char    red;
00043     unsigned char    green;
00044     unsigned char    blue;
00045     unsigned char    dummy;
00046 }
00047 color_palette;
00048 
00049 
00050 int main() {
00051     unsigned char    buffer[ BUFFER_SIZE ];
00052     FILE             *fs;
00053     bmp_header       bh;
00054     color_palette    cp[256];
00055     unsigned long    readsize;
00056     unsigned long    i;
00057     unsigned long    ofset    = 0;
00058 
00059     printf( "BMP file access sample\r\n");
00060 
00061     if ( NULL == (fs    = fopen( SOURCE_FILE, "rb" )) ) {
00062         printf( "file open error when oening file \"%s\"\r\n", SOURCE_FILE );
00063         return ( 1 );
00064     }
00065 
00066     /*
00067      *   reading header
00068      */
00069 
00070     fread( &bh, sizeof( bh ), 1, fs );
00071 
00072     printf( "  bfType         : 0x%04X\r\n", bh.bfType );
00073     printf( "  bfSize         : %ld\r\n",    bh.bfSize );
00074     printf( "  bfOffBits      : %ld\r\n",    bh.bfOffBits );
00075     printf( "  biSize         : %lu\r\n",    bh.biSize );
00076     printf( "  biWidth        : %ld\r\n",    bh.biWidth );
00077     printf( "  biHeight       : %ld\r\n",    bh.biHeight );
00078     printf( "  biPlanes       : %d\r\n",     bh.biPlanes );
00079     printf( "  biBitCount     : %d\r\n",     bh.biBitCount );
00080     printf( "  biCompression  : %lu\r\n",    bh.biCompression );
00081     printf( "  biSizeImage    : %lu\r\n",    bh.biSizeImage );
00082     printf( "  biXPixPerMeter : %ld\r\n",    bh.biXPixPerMeter );
00083     printf( "  biYPixPerMeter : %ld\r\n",    bh.biYPixPerMeter );
00084     printf( "  biClrUsed      : %lu\r\n",    bh.biClrUsed );
00085     printf( "  biCirImportant : %lu\r\n",    bh.biCirImportant );
00086 
00087     /*
00088      *   checking header
00089      */
00090 
00091     if ( (bh.bfType != 0x4D42)
00092             || (bh.bfOffBits != 54 + 256 * sizeof( color_palette ) )
00093             || (bh.biBitCount != 8)
00094             || (bh.biCompression != 0)
00095        ) {
00096         printf( "unsupported file format\r\n" );
00097         return ( 1 );
00098     }
00099 
00100     /*
00101      *   header information
00102      */
00103 
00104     printf( "header read, the image data size is %lu bytes\r\n", bh.bfSize );
00105     printf( "   the image data size is %lu bytes\r\n", bh.biSizeImage );
00106     printf( "   horizontal size %lu pixels\r\n", bh.biWidth );
00107     printf( "   vertical   size %lu pixels\r\n", bh.biHeight );
00108 
00109     /*
00110      *   read color palette
00111      */
00112 
00113     for ( i = 0; i < 256; i++ ) {
00114         fread( &(cp[i]), sizeof( color_palette ), 1, fs );
00115 //        printf( "cp 0x%02X : 0x%02X - 0x%02X - 0x%02X\r\n", i, (cp[i]).red, (cp[i]).green, (cp[i]).blue );
00116     }
00117 
00118 
00119     while ( 0 != (readsize    = fread( buffer, sizeof( unsigned char ) , BUFFER_SIZE, fs )) ) {
00120         for ( i = 0; i < readsize; i++ ) {
00121 //            printf( "  %6lX : 0x%02X, 0x%02X, 0x%02X\r\n", ofset, cp[ buffer[i] ].red, cp[ buffer[i] ].green, cp[ buffer[i] ].blue );
00122             ofset++;
00123         }
00124     }
00125 
00126     printf( "done.\r\n\r\n" );
00127 }