Tedd OKANO
/
bmp_8bit_per_pixel_format
main.cpp@0:45f5cdf3329b, 2011-01-18 (annotated)
- Committer:
- okano
- Date:
- Tue Jan 18 10:19:07 2011 +0000
- Revision:
- 0:45f5cdf3329b
for Andrew-san
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:45f5cdf3329b | 1 | #include "mbed.h" // for mbed |
okano | 0:45f5cdf3329b | 2 | LocalFileSystem local("local"); // for mbed to access local file system |
okano | 0:45f5cdf3329b | 3 | |
okano | 0:45f5cdf3329b | 4 | /* |
okano | 0:45f5cdf3329b | 5 | * |
okano | 0:45f5cdf3329b | 6 | * BMP file data handler sample |
okano | 0:45f5cdf3329b | 7 | * |
okano | 0:45f5cdf3329b | 8 | * reference : http://www.umekkii.jp/data/computer/file_format/bitmap.cgi |
okano | 0:45f5cdf3329b | 9 | * |
okano | 0:45f5cdf3329b | 10 | */ |
okano | 0:45f5cdf3329b | 11 | |
okano | 0:45f5cdf3329b | 12 | #include <stdio.h> |
okano | 0:45f5cdf3329b | 13 | |
okano | 0:45f5cdf3329b | 14 | //#define SOURCE_FILE "/local/small.bmp" |
okano | 0:45f5cdf3329b | 15 | #define SOURCE_FILE "/local/pre4.bmp" |
okano | 0:45f5cdf3329b | 16 | //#define SOURCE_FILE "pre_dsp_4.bmp" |
okano | 0:45f5cdf3329b | 17 | |
okano | 0:45f5cdf3329b | 18 | #define BUFFER_SIZE 1024 |
okano | 0:45f5cdf3329b | 19 | |
okano | 0:45f5cdf3329b | 20 | typedef struct bmp_header_st { |
okano | 0:45f5cdf3329b | 21 | unsigned short bfType __attribute__((packed)); |
okano | 0:45f5cdf3329b | 22 | unsigned long bfSize __attribute__((packed)); |
okano | 0:45f5cdf3329b | 23 | unsigned short bfReserved1 __attribute__((packed)); |
okano | 0:45f5cdf3329b | 24 | unsigned short bfReserved2 __attribute__((packed)); |
okano | 0:45f5cdf3329b | 25 | unsigned long bfOffBits __attribute__((packed)); |
okano | 0:45f5cdf3329b | 26 | |
okano | 0:45f5cdf3329b | 27 | unsigned long biSize __attribute__((packed)); |
okano | 0:45f5cdf3329b | 28 | long biWidth __attribute__((packed)); |
okano | 0:45f5cdf3329b | 29 | long biHeight __attribute__((packed)); |
okano | 0:45f5cdf3329b | 30 | unsigned short biPlanes __attribute__((packed)); |
okano | 0:45f5cdf3329b | 31 | unsigned short biBitCount __attribute__((packed)); |
okano | 0:45f5cdf3329b | 32 | unsigned long biCompression __attribute__((packed)); |
okano | 0:45f5cdf3329b | 33 | unsigned long biSizeImage __attribute__((packed)); |
okano | 0:45f5cdf3329b | 34 | long biXPixPerMeter __attribute__((packed)); |
okano | 0:45f5cdf3329b | 35 | long biYPixPerMeter __attribute__((packed)); |
okano | 0:45f5cdf3329b | 36 | unsigned long biClrUsed __attribute__((packed)); |
okano | 0:45f5cdf3329b | 37 | unsigned long biCirImportant __attribute__((packed)); |
okano | 0:45f5cdf3329b | 38 | } |
okano | 0:45f5cdf3329b | 39 | bmp_header; |
okano | 0:45f5cdf3329b | 40 | |
okano | 0:45f5cdf3329b | 41 | typedef struct color_palette_st { |
okano | 0:45f5cdf3329b | 42 | unsigned char red; |
okano | 0:45f5cdf3329b | 43 | unsigned char green; |
okano | 0:45f5cdf3329b | 44 | unsigned char blue; |
okano | 0:45f5cdf3329b | 45 | unsigned char dummy; |
okano | 0:45f5cdf3329b | 46 | } |
okano | 0:45f5cdf3329b | 47 | color_palette; |
okano | 0:45f5cdf3329b | 48 | |
okano | 0:45f5cdf3329b | 49 | |
okano | 0:45f5cdf3329b | 50 | int main() { |
okano | 0:45f5cdf3329b | 51 | unsigned char buffer[ BUFFER_SIZE ]; |
okano | 0:45f5cdf3329b | 52 | FILE *fs; |
okano | 0:45f5cdf3329b | 53 | bmp_header bh; |
okano | 0:45f5cdf3329b | 54 | color_palette cp[256]; |
okano | 0:45f5cdf3329b | 55 | unsigned long readsize; |
okano | 0:45f5cdf3329b | 56 | unsigned long i; |
okano | 0:45f5cdf3329b | 57 | unsigned long ofset = 0; |
okano | 0:45f5cdf3329b | 58 | |
okano | 0:45f5cdf3329b | 59 | printf( "BMP file access sample\r\n"); |
okano | 0:45f5cdf3329b | 60 | |
okano | 0:45f5cdf3329b | 61 | if ( NULL == (fs = fopen( SOURCE_FILE, "rb" )) ) { |
okano | 0:45f5cdf3329b | 62 | printf( "file open error when oening file \"%s\"\r\n", SOURCE_FILE ); |
okano | 0:45f5cdf3329b | 63 | return ( 1 ); |
okano | 0:45f5cdf3329b | 64 | } |
okano | 0:45f5cdf3329b | 65 | |
okano | 0:45f5cdf3329b | 66 | /* |
okano | 0:45f5cdf3329b | 67 | * reading header |
okano | 0:45f5cdf3329b | 68 | */ |
okano | 0:45f5cdf3329b | 69 | |
okano | 0:45f5cdf3329b | 70 | fread( &bh, sizeof( bh ), 1, fs ); |
okano | 0:45f5cdf3329b | 71 | |
okano | 0:45f5cdf3329b | 72 | printf( " bfType : 0x%04X\r\n", bh.bfType ); |
okano | 0:45f5cdf3329b | 73 | printf( " bfSize : %ld\r\n", bh.bfSize ); |
okano | 0:45f5cdf3329b | 74 | printf( " bfOffBits : %ld\r\n", bh.bfOffBits ); |
okano | 0:45f5cdf3329b | 75 | printf( " biSize : %lu\r\n", bh.biSize ); |
okano | 0:45f5cdf3329b | 76 | printf( " biWidth : %ld\r\n", bh.biWidth ); |
okano | 0:45f5cdf3329b | 77 | printf( " biHeight : %ld\r\n", bh.biHeight ); |
okano | 0:45f5cdf3329b | 78 | printf( " biPlanes : %d\r\n", bh.biPlanes ); |
okano | 0:45f5cdf3329b | 79 | printf( " biBitCount : %d\r\n", bh.biBitCount ); |
okano | 0:45f5cdf3329b | 80 | printf( " biCompression : %lu\r\n", bh.biCompression ); |
okano | 0:45f5cdf3329b | 81 | printf( " biSizeImage : %lu\r\n", bh.biSizeImage ); |
okano | 0:45f5cdf3329b | 82 | printf( " biXPixPerMeter : %ld\r\n", bh.biXPixPerMeter ); |
okano | 0:45f5cdf3329b | 83 | printf( " biYPixPerMeter : %ld\r\n", bh.biYPixPerMeter ); |
okano | 0:45f5cdf3329b | 84 | printf( " biClrUsed : %lu\r\n", bh.biClrUsed ); |
okano | 0:45f5cdf3329b | 85 | printf( " biCirImportant : %lu\r\n", bh.biCirImportant ); |
okano | 0:45f5cdf3329b | 86 | |
okano | 0:45f5cdf3329b | 87 | /* |
okano | 0:45f5cdf3329b | 88 | * checking header |
okano | 0:45f5cdf3329b | 89 | */ |
okano | 0:45f5cdf3329b | 90 | |
okano | 0:45f5cdf3329b | 91 | if ( (bh.bfType != 0x4D42) |
okano | 0:45f5cdf3329b | 92 | || (bh.bfOffBits != 54 + 256 * sizeof( color_palette ) ) |
okano | 0:45f5cdf3329b | 93 | || (bh.biBitCount != 8) |
okano | 0:45f5cdf3329b | 94 | || (bh.biCompression != 0) |
okano | 0:45f5cdf3329b | 95 | ) { |
okano | 0:45f5cdf3329b | 96 | printf( "unsupported file format\r\n" ); |
okano | 0:45f5cdf3329b | 97 | return ( 1 ); |
okano | 0:45f5cdf3329b | 98 | } |
okano | 0:45f5cdf3329b | 99 | |
okano | 0:45f5cdf3329b | 100 | /* |
okano | 0:45f5cdf3329b | 101 | * header information |
okano | 0:45f5cdf3329b | 102 | */ |
okano | 0:45f5cdf3329b | 103 | |
okano | 0:45f5cdf3329b | 104 | printf( "header read, the image data size is %lu bytes\r\n", bh.bfSize ); |
okano | 0:45f5cdf3329b | 105 | printf( " the image data size is %lu bytes\r\n", bh.biSizeImage ); |
okano | 0:45f5cdf3329b | 106 | printf( " horizontal size %lu pixels\r\n", bh.biWidth ); |
okano | 0:45f5cdf3329b | 107 | printf( " vertical size %lu pixels\r\n", bh.biHeight ); |
okano | 0:45f5cdf3329b | 108 | |
okano | 0:45f5cdf3329b | 109 | /* |
okano | 0:45f5cdf3329b | 110 | * read color palette |
okano | 0:45f5cdf3329b | 111 | */ |
okano | 0:45f5cdf3329b | 112 | |
okano | 0:45f5cdf3329b | 113 | for ( i = 0; i < 256; i++ ) { |
okano | 0:45f5cdf3329b | 114 | fread( &(cp[i]), sizeof( color_palette ), 1, fs ); |
okano | 0:45f5cdf3329b | 115 | // printf( "cp 0x%02X : 0x%02X - 0x%02X - 0x%02X\r\n", i, (cp[i]).red, (cp[i]).green, (cp[i]).blue ); |
okano | 0:45f5cdf3329b | 116 | } |
okano | 0:45f5cdf3329b | 117 | |
okano | 0:45f5cdf3329b | 118 | |
okano | 0:45f5cdf3329b | 119 | while ( 0 != (readsize = fread( buffer, sizeof( unsigned char ) , BUFFER_SIZE, fs )) ) { |
okano | 0:45f5cdf3329b | 120 | for ( i = 0; i < readsize; i++ ) { |
okano | 0:45f5cdf3329b | 121 | // printf( " %6lX : 0x%02X, 0x%02X, 0x%02X\r\n", ofset, cp[ buffer[i] ].red, cp[ buffer[i] ].green, cp[ buffer[i] ].blue ); |
okano | 0:45f5cdf3329b | 122 | ofset++; |
okano | 0:45f5cdf3329b | 123 | } |
okano | 0:45f5cdf3329b | 124 | } |
okano | 0:45f5cdf3329b | 125 | |
okano | 0:45f5cdf3329b | 126 | printf( "done.\r\n\r\n" ); |
okano | 0:45f5cdf3329b | 127 | } |