shiftlightws2818bcoilxrpm
Dependencies: mbed NeoStrip PinDetect
BitmapFile.h@0:a26aca3e4760, 2015-11-10 (annotated)
- Committer:
- lz307
- Date:
- Tue Nov 10 21:28:51 2015 +0000
- Revision:
- 0:a26aca3e4760
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lz307 | 0:a26aca3e4760 | 1 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 2 | /*BitmapFile.h */ |
lz307 | 0:a26aca3e4760 | 3 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 4 | |
lz307 | 0:a26aca3e4760 | 5 | #include "mbed.h" |
lz307 | 0:a26aca3e4760 | 6 | |
lz307 | 0:a26aca3e4760 | 7 | /* Class: BitmapFile |
lz307 | 0:a26aca3e4760 | 8 | * A parser for bitmap files. |
lz307 | 0:a26aca3e4760 | 9 | */ |
lz307 | 0:a26aca3e4760 | 10 | class BitmapFile |
lz307 | 0:a26aca3e4760 | 11 | { |
lz307 | 0:a26aca3e4760 | 12 | private: |
lz307 | 0:a26aca3e4760 | 13 | FILE *m_pFile; |
lz307 | 0:a26aca3e4760 | 14 | char *m_fileName; |
lz307 | 0:a26aca3e4760 | 15 | int m_rowsize; |
lz307 | 0:a26aca3e4760 | 16 | |
lz307 | 0:a26aca3e4760 | 17 | enum headerType { BITMAPCOREHEADER = 12, |
lz307 | 0:a26aca3e4760 | 18 | BITMAPCOREHEADER2 = 64, |
lz307 | 0:a26aca3e4760 | 19 | BITMAPINFOHEADER = 40, |
lz307 | 0:a26aca3e4760 | 20 | BITMAPV4HEADER = 108, |
lz307 | 0:a26aca3e4760 | 21 | BITMAPV5HEADER = 124}; |
lz307 | 0:a26aca3e4760 | 22 | |
lz307 | 0:a26aca3e4760 | 23 | enum compressType { BI_RGB, |
lz307 | 0:a26aca3e4760 | 24 | BI_RLE8, |
lz307 | 0:a26aca3e4760 | 25 | BI_RLE4, |
lz307 | 0:a26aca3e4760 | 26 | BI_BITFIELDS, |
lz307 | 0:a26aca3e4760 | 27 | BI_JPEG, |
lz307 | 0:a26aca3e4760 | 28 | BI_PNG}; |
lz307 | 0:a26aca3e4760 | 29 | |
lz307 | 0:a26aca3e4760 | 30 | /* Struct: BMPHeader |
lz307 | 0:a26aca3e4760 | 31 | * |
lz307 | 0:a26aca3e4760 | 32 | * The BMP header of the bitmap is read into this. |
lz307 | 0:a26aca3e4760 | 33 | * |
lz307 | 0:a26aca3e4760 | 34 | * b - the first byte of the header. Should equal 'B'. |
lz307 | 0:a26aca3e4760 | 35 | * m - the second byte of the header. Should equal 'M'. |
lz307 | 0:a26aca3e4760 | 36 | * filesize - the size of the whole file, in bytes. |
lz307 | 0:a26aca3e4760 | 37 | * reserved 1 and 2 - data specific to the applicaton which created the bitmap. |
lz307 | 0:a26aca3e4760 | 38 | * offset - the offset at which the actual bitmap begins |
lz307 | 0:a26aca3e4760 | 39 | */ |
lz307 | 0:a26aca3e4760 | 40 | |
lz307 | 0:a26aca3e4760 | 41 | __packed struct |
lz307 | 0:a26aca3e4760 | 42 | { |
lz307 | 0:a26aca3e4760 | 43 | char b:8; |
lz307 | 0:a26aca3e4760 | 44 | char m:8; |
lz307 | 0:a26aca3e4760 | 45 | int filesize:32; |
lz307 | 0:a26aca3e4760 | 46 | int reserved1:16; |
lz307 | 0:a26aca3e4760 | 47 | int reserved2:16; |
lz307 | 0:a26aca3e4760 | 48 | int offset:32; |
lz307 | 0:a26aca3e4760 | 49 | |
lz307 | 0:a26aca3e4760 | 50 | } BMPHeader; |
lz307 | 0:a26aca3e4760 | 51 | |
lz307 | 0:a26aca3e4760 | 52 | |
lz307 | 0:a26aca3e4760 | 53 | /* Struct: DIBHeader |
lz307 | 0:a26aca3e4760 | 54 | * |
lz307 | 0:a26aca3e4760 | 55 | * The DIB header of the bitmap is read into this. |
lz307 | 0:a26aca3e4760 | 56 | * |
lz307 | 0:a26aca3e4760 | 57 | * headerlength - the length of the header. Should equal 40. |
lz307 | 0:a26aca3e4760 | 58 | * height - the height of the bitmap. |
lz307 | 0:a26aca3e4760 | 59 | * width - the width of the bitmap. |
lz307 | 0:a26aca3e4760 | 60 | * cplanes - the number of color planes. Should equal 1. |
lz307 | 0:a26aca3e4760 | 61 | * colordepth - the number of bits per pixel. |
lz307 | 0:a26aca3e4760 | 62 | * compression - the compression method used. |
lz307 | 0:a26aca3e4760 | 63 | * datasize - the size of the bitmap data, in bytes. |
lz307 | 0:a26aca3e4760 | 64 | */ |
lz307 | 0:a26aca3e4760 | 65 | |
lz307 | 0:a26aca3e4760 | 66 | int m_headerlength; |
lz307 | 0:a26aca3e4760 | 67 | __packed struct |
lz307 | 0:a26aca3e4760 | 68 | { |
lz307 | 0:a26aca3e4760 | 69 | int width:32; |
lz307 | 0:a26aca3e4760 | 70 | int height:32; |
lz307 | 0:a26aca3e4760 | 71 | int cplanes:16; |
lz307 | 0:a26aca3e4760 | 72 | int colordepth:16; |
lz307 | 0:a26aca3e4760 | 73 | int compression:32; |
lz307 | 0:a26aca3e4760 | 74 | int datasize:32; |
lz307 | 0:a26aca3e4760 | 75 | int hres:32; |
lz307 | 0:a26aca3e4760 | 76 | int vres:32; |
lz307 | 0:a26aca3e4760 | 77 | int numpalettecolors:32; |
lz307 | 0:a26aca3e4760 | 78 | int importantcolors:32; |
lz307 | 0:a26aca3e4760 | 79 | } DIBHeader; |
lz307 | 0:a26aca3e4760 | 80 | |
lz307 | 0:a26aca3e4760 | 81 | |
lz307 | 0:a26aca3e4760 | 82 | public: |
lz307 | 0:a26aca3e4760 | 83 | /* Constructor: BitmapFile |
lz307 | 0:a26aca3e4760 | 84 | * Create the BitmapFile class, and call <Initialize> |
lz307 | 0:a26aca3e4760 | 85 | * |
lz307 | 0:a26aca3e4760 | 86 | * Parameters: |
lz307 | 0:a26aca3e4760 | 87 | * fname - The path of the file to open. |
lz307 | 0:a26aca3e4760 | 88 | */ |
lz307 | 0:a26aca3e4760 | 89 | BitmapFile(char* fname); |
lz307 | 0:a26aca3e4760 | 90 | ~BitmapFile(); |
lz307 | 0:a26aca3e4760 | 91 | |
lz307 | 0:a26aca3e4760 | 92 | /* Function: Initialize |
lz307 | 0:a26aca3e4760 | 93 | * Parses the headers of the bitmap. |
lz307 | 0:a26aca3e4760 | 94 | * |
lz307 | 0:a26aca3e4760 | 95 | * Returns: |
lz307 | 0:a26aca3e4760 | 96 | * Whether the bitmap is valid. |
lz307 | 0:a26aca3e4760 | 97 | */ |
lz307 | 0:a26aca3e4760 | 98 | bool Initialize(); //parses the header |
lz307 | 0:a26aca3e4760 | 99 | |
lz307 | 0:a26aca3e4760 | 100 | /* Function: open |
lz307 | 0:a26aca3e4760 | 101 | * Opens the bitmap for reading, if not already open. |
lz307 | 0:a26aca3e4760 | 102 | */ |
lz307 | 0:a26aca3e4760 | 103 | void open(); |
lz307 | 0:a26aca3e4760 | 104 | |
lz307 | 0:a26aca3e4760 | 105 | /* Function: close |
lz307 | 0:a26aca3e4760 | 106 | * Closes the bitmap. |
lz307 | 0:a26aca3e4760 | 107 | */ |
lz307 | 0:a26aca3e4760 | 108 | void close(); |
lz307 | 0:a26aca3e4760 | 109 | |
lz307 | 0:a26aca3e4760 | 110 | /***BMP Header gets begin***/ |
lz307 | 0:a26aca3e4760 | 111 | int getFileSize(); |
lz307 | 0:a26aca3e4760 | 112 | int getReserved1(); |
lz307 | 0:a26aca3e4760 | 113 | int getReserved2(); |
lz307 | 0:a26aca3e4760 | 114 | int getOffset(); |
lz307 | 0:a26aca3e4760 | 115 | |
lz307 | 0:a26aca3e4760 | 116 | /***DIB Header gets begin***/ |
lz307 | 0:a26aca3e4760 | 117 | int getHeaderType(); |
lz307 | 0:a26aca3e4760 | 118 | int getHeight(); |
lz307 | 0:a26aca3e4760 | 119 | int getWidth(); |
lz307 | 0:a26aca3e4760 | 120 | int getCPlanes(); |
lz307 | 0:a26aca3e4760 | 121 | int getColorDepth(); |
lz307 | 0:a26aca3e4760 | 122 | int getCompression(); |
lz307 | 0:a26aca3e4760 | 123 | int getDataSize(); |
lz307 | 0:a26aca3e4760 | 124 | int getHRes(); |
lz307 | 0:a26aca3e4760 | 125 | int getVRes(); |
lz307 | 0:a26aca3e4760 | 126 | int getNumPaletteColors(); |
lz307 | 0:a26aca3e4760 | 127 | int getImportantColors(); |
lz307 | 0:a26aca3e4760 | 128 | /****DIB Header gets end****/ |
lz307 | 0:a26aca3e4760 | 129 | |
lz307 | 0:a26aca3e4760 | 130 | /******Data gets begin******/ |
lz307 | 0:a26aca3e4760 | 131 | /* Function: getPixel |
lz307 | 0:a26aca3e4760 | 132 | * Gets the color of a pixel |
lz307 | 0:a26aca3e4760 | 133 | * |
lz307 | 0:a26aca3e4760 | 134 | * Parameters: |
lz307 | 0:a26aca3e4760 | 135 | * x - The x coordinate of the pixel. |
lz307 | 0:a26aca3e4760 | 136 | * y - The y coordinate of the pixel. |
lz307 | 0:a26aca3e4760 | 137 | * closefile - if specified, close the file after reading |
lz307 | 0:a26aca3e4760 | 138 | * |
lz307 | 0:a26aca3e4760 | 139 | * Returns: |
lz307 | 0:a26aca3e4760 | 140 | * the color of the pixel, in hexadecimal. |
lz307 | 0:a26aca3e4760 | 141 | */ |
lz307 | 0:a26aca3e4760 | 142 | int getPixel(int x, int y, bool closefile = true); |
lz307 | 0:a26aca3e4760 | 143 | /* Function: getRow |
lz307 | 0:a26aca3e4760 | 144 | * Gets the colors of a row |
lz307 | 0:a26aca3e4760 | 145 | * |
lz307 | 0:a26aca3e4760 | 146 | * Parameters: |
lz307 | 0:a26aca3e4760 | 147 | * row - The number of the row.. |
lz307 | 0:a26aca3e4760 | 148 | * closefile - if specified, close the file after reading |
lz307 | 0:a26aca3e4760 | 149 | * |
lz307 | 0:a26aca3e4760 | 150 | * Returns: |
lz307 | 0:a26aca3e4760 | 151 | * An array of the colors of the pixels, in hexadecimal. |
lz307 | 0:a26aca3e4760 | 152 | */ |
lz307 | 0:a26aca3e4760 | 153 | int *getRow(int row, bool closefile = true); |
lz307 | 0:a26aca3e4760 | 154 | int *getRowBW(int row, bool closefile = true); |
lz307 | 0:a26aca3e4760 | 155 | char *getRowBitstream(int row, bool closefile = true); |
lz307 | 0:a26aca3e4760 | 156 | /*******Data gets end*******/ |
lz307 | 0:a26aca3e4760 | 157 | int getRowSize(); |
lz307 | 0:a26aca3e4760 | 158 | }; |