shiftlightws2818bcoilxrpm
Dependencies: mbed NeoStrip PinDetect
BitmapFile.cpp@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 | #include "BitmapFile.h" |
lz307 | 0:a26aca3e4760 | 2 | |
lz307 | 0:a26aca3e4760 | 3 | |
lz307 | 0:a26aca3e4760 | 4 | BitmapFile::BitmapFile(char* fname) : m_pFile(NULL) |
lz307 | 0:a26aca3e4760 | 5 | { |
lz307 | 0:a26aca3e4760 | 6 | m_fileName = fname; |
lz307 | 0:a26aca3e4760 | 7 | Initialize(); |
lz307 | 0:a26aca3e4760 | 8 | } |
lz307 | 0:a26aca3e4760 | 9 | |
lz307 | 0:a26aca3e4760 | 10 | BitmapFile::~BitmapFile() |
lz307 | 0:a26aca3e4760 | 11 | { |
lz307 | 0:a26aca3e4760 | 12 | delete[] m_fileName; |
lz307 | 0:a26aca3e4760 | 13 | } |
lz307 | 0:a26aca3e4760 | 14 | |
lz307 | 0:a26aca3e4760 | 15 | bool BitmapFile::Initialize() |
lz307 | 0:a26aca3e4760 | 16 | { |
lz307 | 0:a26aca3e4760 | 17 | bool success = true; |
lz307 | 0:a26aca3e4760 | 18 | open(); |
lz307 | 0:a26aca3e4760 | 19 | fread(&BMPHeader,sizeof(BMPHeader),1,m_pFile); |
lz307 | 0:a26aca3e4760 | 20 | success = (BMPHeader.b == 'B' && BMPHeader.m == 'M'); |
lz307 | 0:a26aca3e4760 | 21 | |
lz307 | 0:a26aca3e4760 | 22 | fread(&m_headerlength,sizeof(m_headerlength),1,m_pFile); |
lz307 | 0:a26aca3e4760 | 23 | fread(&DIBHeader,m_headerlength,1,m_pFile); |
lz307 | 0:a26aca3e4760 | 24 | |
lz307 | 0:a26aca3e4760 | 25 | /*Debugging code*/ |
lz307 | 0:a26aca3e4760 | 26 | |
lz307 | 0:a26aca3e4760 | 27 | Serial pc2(USBTX,USBRX); |
lz307 | 0:a26aca3e4760 | 28 | |
lz307 | 0:a26aca3e4760 | 29 | pc2.printf("\n\rFile = %s", m_fileName); |
lz307 | 0:a26aca3e4760 | 30 | |
lz307 | 0:a26aca3e4760 | 31 | pc2.printf("\n\rBMPHeader - Size = %d:\n\r",sizeof(BMPHeader)); |
lz307 | 0:a26aca3e4760 | 32 | pc2.printf("\tbm:\t\t%c%c\n\r",BMPHeader.b,BMPHeader.m); |
lz307 | 0:a26aca3e4760 | 33 | pc2.printf("\tfilesize:\t%d\n\r",BMPHeader.filesize); |
lz307 | 0:a26aca3e4760 | 34 | pc2.printf("\treserved:\t%d,%d\n\r",BMPHeader.reserved1,BMPHeader.reserved2); |
lz307 | 0:a26aca3e4760 | 35 | pc2.printf("\toffset:\t\t%d\n\r",BMPHeader.offset); |
lz307 | 0:a26aca3e4760 | 36 | |
lz307 | 0:a26aca3e4760 | 37 | pc2.printf("\n\rDIBHeader - Size = %d:\n\r",sizeof(DIBHeader)); |
lz307 | 0:a26aca3e4760 | 38 | //pc2.printf("\theaderLength:\t%d\n\r",DIBHeader.headerLength); |
lz307 | 0:a26aca3e4760 | 39 | pc2.printf("\theight:\t\t\t%d\n\r",DIBHeader.height); |
lz307 | 0:a26aca3e4760 | 40 | pc2.printf("\twidth:\t\t%d\n\r",DIBHeader.width); |
lz307 | 0:a26aca3e4760 | 41 | pc2.printf("\tcplanes:\t\t%d\n\r",DIBHeader.cplanes); |
lz307 | 0:a26aca3e4760 | 42 | pc2.printf("\tcolordepth:\t\t%d\n\r",DIBHeader.colordepth); |
lz307 | 0:a26aca3e4760 | 43 | pc2.printf("\tcompression:\t%d\n\r",DIBHeader.compression); |
lz307 | 0:a26aca3e4760 | 44 | pc2.printf("\tdatasize:\t\t%d\n\r",DIBHeader.datasize); |
lz307 | 0:a26aca3e4760 | 45 | pc2.printf("\tvres:\t\t%d\n\r",DIBHeader.vres); |
lz307 | 0:a26aca3e4760 | 46 | pc2.printf("\thres:\t\t%d\n\r",DIBHeader.hres); |
lz307 | 0:a26aca3e4760 | 47 | |
lz307 | 0:a26aca3e4760 | 48 | |
lz307 | 0:a26aca3e4760 | 49 | |
lz307 | 0:a26aca3e4760 | 50 | m_rowsize = 4*((getColorDepth()*getWidth()+31)/32); |
lz307 | 0:a26aca3e4760 | 51 | |
lz307 | 0:a26aca3e4760 | 52 | close(); |
lz307 | 0:a26aca3e4760 | 53 | return success; |
lz307 | 0:a26aca3e4760 | 54 | } |
lz307 | 0:a26aca3e4760 | 55 | |
lz307 | 0:a26aca3e4760 | 56 | void BitmapFile::open() |
lz307 | 0:a26aca3e4760 | 57 | { |
lz307 | 0:a26aca3e4760 | 58 | if(m_pFile==NULL) |
lz307 | 0:a26aca3e4760 | 59 | { |
lz307 | 0:a26aca3e4760 | 60 | m_pFile = fopen(m_fileName, "r"); |
lz307 | 0:a26aca3e4760 | 61 | } |
lz307 | 0:a26aca3e4760 | 62 | } |
lz307 | 0:a26aca3e4760 | 63 | |
lz307 | 0:a26aca3e4760 | 64 | void BitmapFile::close() |
lz307 | 0:a26aca3e4760 | 65 | { |
lz307 | 0:a26aca3e4760 | 66 | if(m_pFile!=NULL) |
lz307 | 0:a26aca3e4760 | 67 | { |
lz307 | 0:a26aca3e4760 | 68 | fclose(m_pFile); |
lz307 | 0:a26aca3e4760 | 69 | m_pFile = NULL; |
lz307 | 0:a26aca3e4760 | 70 | } |
lz307 | 0:a26aca3e4760 | 71 | } |
lz307 | 0:a26aca3e4760 | 72 | |
lz307 | 0:a26aca3e4760 | 73 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 74 | /*BMP Header Gets */ |
lz307 | 0:a26aca3e4760 | 75 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 76 | |
lz307 | 0:a26aca3e4760 | 77 | int BitmapFile::getFileSize() |
lz307 | 0:a26aca3e4760 | 78 | { |
lz307 | 0:a26aca3e4760 | 79 | return BMPHeader.filesize; |
lz307 | 0:a26aca3e4760 | 80 | } |
lz307 | 0:a26aca3e4760 | 81 | |
lz307 | 0:a26aca3e4760 | 82 | int BitmapFile::getReserved1() |
lz307 | 0:a26aca3e4760 | 83 | { |
lz307 | 0:a26aca3e4760 | 84 | return BMPHeader.reserved1; |
lz307 | 0:a26aca3e4760 | 85 | } |
lz307 | 0:a26aca3e4760 | 86 | |
lz307 | 0:a26aca3e4760 | 87 | int BitmapFile::getReserved2() |
lz307 | 0:a26aca3e4760 | 88 | { |
lz307 | 0:a26aca3e4760 | 89 | return BMPHeader.reserved2; |
lz307 | 0:a26aca3e4760 | 90 | } |
lz307 | 0:a26aca3e4760 | 91 | |
lz307 | 0:a26aca3e4760 | 92 | int BitmapFile::getOffset() |
lz307 | 0:a26aca3e4760 | 93 | { |
lz307 | 0:a26aca3e4760 | 94 | return BMPHeader.offset; |
lz307 | 0:a26aca3e4760 | 95 | } |
lz307 | 0:a26aca3e4760 | 96 | |
lz307 | 0:a26aca3e4760 | 97 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 98 | /*DIB Header Gets */ |
lz307 | 0:a26aca3e4760 | 99 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 100 | |
lz307 | 0:a26aca3e4760 | 101 | int BitmapFile::getHeaderType() |
lz307 | 0:a26aca3e4760 | 102 | { |
lz307 | 0:a26aca3e4760 | 103 | return m_headerlength; |
lz307 | 0:a26aca3e4760 | 104 | } |
lz307 | 0:a26aca3e4760 | 105 | |
lz307 | 0:a26aca3e4760 | 106 | int BitmapFile::getHeight() |
lz307 | 0:a26aca3e4760 | 107 | { |
lz307 | 0:a26aca3e4760 | 108 | return DIBHeader.height; |
lz307 | 0:a26aca3e4760 | 109 | } |
lz307 | 0:a26aca3e4760 | 110 | |
lz307 | 0:a26aca3e4760 | 111 | int BitmapFile::getWidth() |
lz307 | 0:a26aca3e4760 | 112 | { |
lz307 | 0:a26aca3e4760 | 113 | return DIBHeader.width; |
lz307 | 0:a26aca3e4760 | 114 | } |
lz307 | 0:a26aca3e4760 | 115 | |
lz307 | 0:a26aca3e4760 | 116 | int BitmapFile::getCPlanes() |
lz307 | 0:a26aca3e4760 | 117 | { |
lz307 | 0:a26aca3e4760 | 118 | return DIBHeader.cplanes; |
lz307 | 0:a26aca3e4760 | 119 | } |
lz307 | 0:a26aca3e4760 | 120 | |
lz307 | 0:a26aca3e4760 | 121 | int BitmapFile::getColorDepth() |
lz307 | 0:a26aca3e4760 | 122 | { |
lz307 | 0:a26aca3e4760 | 123 | return DIBHeader.colordepth; |
lz307 | 0:a26aca3e4760 | 124 | } |
lz307 | 0:a26aca3e4760 | 125 | |
lz307 | 0:a26aca3e4760 | 126 | int BitmapFile::getCompression() |
lz307 | 0:a26aca3e4760 | 127 | { |
lz307 | 0:a26aca3e4760 | 128 | return DIBHeader.compression; |
lz307 | 0:a26aca3e4760 | 129 | } |
lz307 | 0:a26aca3e4760 | 130 | |
lz307 | 0:a26aca3e4760 | 131 | int BitmapFile::getDataSize() |
lz307 | 0:a26aca3e4760 | 132 | { |
lz307 | 0:a26aca3e4760 | 133 | return DIBHeader.datasize; |
lz307 | 0:a26aca3e4760 | 134 | } |
lz307 | 0:a26aca3e4760 | 135 | |
lz307 | 0:a26aca3e4760 | 136 | int BitmapFile::getHRes() |
lz307 | 0:a26aca3e4760 | 137 | { |
lz307 | 0:a26aca3e4760 | 138 | return DIBHeader.hres; |
lz307 | 0:a26aca3e4760 | 139 | } |
lz307 | 0:a26aca3e4760 | 140 | |
lz307 | 0:a26aca3e4760 | 141 | int BitmapFile::getVRes() |
lz307 | 0:a26aca3e4760 | 142 | { |
lz307 | 0:a26aca3e4760 | 143 | return DIBHeader.vres; |
lz307 | 0:a26aca3e4760 | 144 | } |
lz307 | 0:a26aca3e4760 | 145 | |
lz307 | 0:a26aca3e4760 | 146 | int BitmapFile::getNumPaletteColors() |
lz307 | 0:a26aca3e4760 | 147 | { |
lz307 | 0:a26aca3e4760 | 148 | return DIBHeader.numpalettecolors; |
lz307 | 0:a26aca3e4760 | 149 | } |
lz307 | 0:a26aca3e4760 | 150 | |
lz307 | 0:a26aca3e4760 | 151 | int BitmapFile::getImportantColors() |
lz307 | 0:a26aca3e4760 | 152 | { |
lz307 | 0:a26aca3e4760 | 153 | return DIBHeader.importantcolors; |
lz307 | 0:a26aca3e4760 | 154 | } |
lz307 | 0:a26aca3e4760 | 155 | |
lz307 | 0:a26aca3e4760 | 156 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 157 | /*Data Gets */ |
lz307 | 0:a26aca3e4760 | 158 | /**********************************************************/ |
lz307 | 0:a26aca3e4760 | 159 | |
lz307 | 0:a26aca3e4760 | 160 | int BitmapFile::getRowSize() |
lz307 | 0:a26aca3e4760 | 161 | { |
lz307 | 0:a26aca3e4760 | 162 | return m_rowsize; |
lz307 | 0:a26aca3e4760 | 163 | } |
lz307 | 0:a26aca3e4760 | 164 | |
lz307 | 0:a26aca3e4760 | 165 | int BitmapFile::getPixel(int row, int col, bool closefile) |
lz307 | 0:a26aca3e4760 | 166 | { |
lz307 | 0:a26aca3e4760 | 167 | int color = -1; |
lz307 | 0:a26aca3e4760 | 168 | if(row>=0 && row < getHeight() && col>=0 && col< getWidth()) |
lz307 | 0:a26aca3e4760 | 169 | { |
lz307 | 0:a26aca3e4760 | 170 | if(getColorDepth() == 24) |
lz307 | 0:a26aca3e4760 | 171 | { |
lz307 | 0:a26aca3e4760 | 172 | open(); |
lz307 | 0:a26aca3e4760 | 173 | color = 0; //make sure the last byte is 00 |
lz307 | 0:a26aca3e4760 | 174 | |
lz307 | 0:a26aca3e4760 | 175 | int index = getOffset(); |
lz307 | 0:a26aca3e4760 | 176 | index += col*3; |
lz307 | 0:a26aca3e4760 | 177 | index += row*4*ceil(getWidth()*3/4.0); |
lz307 | 0:a26aca3e4760 | 178 | fseek(m_pFile, index, SEEK_SET); |
lz307 | 0:a26aca3e4760 | 179 | |
lz307 | 0:a26aca3e4760 | 180 | fread (&color, 3,1,m_pFile); |
lz307 | 0:a26aca3e4760 | 181 | |
lz307 | 0:a26aca3e4760 | 182 | if(closefile) |
lz307 | 0:a26aca3e4760 | 183 | { |
lz307 | 0:a26aca3e4760 | 184 | close(); |
lz307 | 0:a26aca3e4760 | 185 | } |
lz307 | 0:a26aca3e4760 | 186 | } |
lz307 | 0:a26aca3e4760 | 187 | } |
lz307 | 0:a26aca3e4760 | 188 | return color; |
lz307 | 0:a26aca3e4760 | 189 | } |
lz307 | 0:a26aca3e4760 | 190 | |
lz307 | 0:a26aca3e4760 | 191 | int *BitmapFile::getRow(int row, bool closefile) |
lz307 | 0:a26aca3e4760 | 192 | { |
lz307 | 0:a26aca3e4760 | 193 | open(); |
lz307 | 0:a26aca3e4760 | 194 | int *colors = new int[getWidth()]; |
lz307 | 0:a26aca3e4760 | 195 | int index = getOffset() + m_rowsize*row; |
lz307 | 0:a26aca3e4760 | 196 | fseek(m_pFile, index, SEEK_SET); |
lz307 | 0:a26aca3e4760 | 197 | if(getColorDepth() == 24) |
lz307 | 0:a26aca3e4760 | 198 | { |
lz307 | 0:a26aca3e4760 | 199 | for(int i=0; i<getWidth(); i++) |
lz307 | 0:a26aca3e4760 | 200 | { |
lz307 | 0:a26aca3e4760 | 201 | fread(&colors[i],3,1,m_pFile); |
lz307 | 0:a26aca3e4760 | 202 | } |
lz307 | 0:a26aca3e4760 | 203 | } |
lz307 | 0:a26aca3e4760 | 204 | else if(getColorDepth() == 1) |
lz307 | 0:a26aca3e4760 | 205 | { |
lz307 | 0:a26aca3e4760 | 206 | char *temp = new char[m_rowsize]; |
lz307 | 0:a26aca3e4760 | 207 | for(int i=0; i<m_rowsize; i++) |
lz307 | 0:a26aca3e4760 | 208 | { |
lz307 | 0:a26aca3e4760 | 209 | fread(&temp[i],sizeof(char),1,m_pFile); |
lz307 | 0:a26aca3e4760 | 210 | } |
lz307 | 0:a26aca3e4760 | 211 | for(int i=0; i<getWidth(); i++) |
lz307 | 0:a26aca3e4760 | 212 | { |
lz307 | 0:a26aca3e4760 | 213 | int byte = i / 8; |
lz307 | 0:a26aca3e4760 | 214 | int bit = i % 8; |
lz307 | 0:a26aca3e4760 | 215 | colors[i] = ((temp[byte] << bit) & 0x80) ? 0xFFFFFF : 0x000000; |
lz307 | 0:a26aca3e4760 | 216 | } |
lz307 | 0:a26aca3e4760 | 217 | delete [] temp; |
lz307 | 0:a26aca3e4760 | 218 | } |
lz307 | 0:a26aca3e4760 | 219 | if(closefile) |
lz307 | 0:a26aca3e4760 | 220 | { |
lz307 | 0:a26aca3e4760 | 221 | close(); |
lz307 | 0:a26aca3e4760 | 222 | } |
lz307 | 0:a26aca3e4760 | 223 | return colors; |
lz307 | 0:a26aca3e4760 | 224 | } |
lz307 | 0:a26aca3e4760 | 225 | |
lz307 | 0:a26aca3e4760 | 226 | int *BitmapFile::getRowBW(int row, bool closefile) |
lz307 | 0:a26aca3e4760 | 227 | { |
lz307 | 0:a26aca3e4760 | 228 | open(); |
lz307 | 0:a26aca3e4760 | 229 | int *colors = new int[getWidth()]; |
lz307 | 0:a26aca3e4760 | 230 | int index = getOffset() + m_rowsize*row; |
lz307 | 0:a26aca3e4760 | 231 | fseek(m_pFile, index, SEEK_SET); |
lz307 | 0:a26aca3e4760 | 232 | if(getColorDepth() == 24) |
lz307 | 0:a26aca3e4760 | 233 | { |
lz307 | 0:a26aca3e4760 | 234 | for(int i=0; i<getWidth(); i++) |
lz307 | 0:a26aca3e4760 | 235 | { |
lz307 | 0:a26aca3e4760 | 236 | char temp[3]; |
lz307 | 0:a26aca3e4760 | 237 | fread(temp,sizeof(char),3,m_pFile); |
lz307 | 0:a26aca3e4760 | 238 | int average = (temp[0]+temp[1]+temp[2])/3; |
lz307 | 0:a26aca3e4760 | 239 | colors[i] = average>128 ? 0xFFFFFF : 0x000000; |
lz307 | 0:a26aca3e4760 | 240 | } |
lz307 | 0:a26aca3e4760 | 241 | } |
lz307 | 0:a26aca3e4760 | 242 | else if(getColorDepth() == 1) |
lz307 | 0:a26aca3e4760 | 243 | { |
lz307 | 0:a26aca3e4760 | 244 | delete [] colors; |
lz307 | 0:a26aca3e4760 | 245 | colors = getRow(row, closefile); |
lz307 | 0:a26aca3e4760 | 246 | } |
lz307 | 0:a26aca3e4760 | 247 | if(closefile) |
lz307 | 0:a26aca3e4760 | 248 | { |
lz307 | 0:a26aca3e4760 | 249 | close(); |
lz307 | 0:a26aca3e4760 | 250 | } |
lz307 | 0:a26aca3e4760 | 251 | return colors; |
lz307 | 0:a26aca3e4760 | 252 | } |
lz307 | 0:a26aca3e4760 | 253 | |
lz307 | 0:a26aca3e4760 | 254 | char *BitmapFile::getRowBitstream(int row, bool closefile) |
lz307 | 0:a26aca3e4760 | 255 | { |
lz307 | 0:a26aca3e4760 | 256 | open(); |
lz307 | 0:a26aca3e4760 | 257 | int bitsperrow = (getWidth()+7)/8; |
lz307 | 0:a26aca3e4760 | 258 | char *data = new char[bitsperrow]; |
lz307 | 0:a26aca3e4760 | 259 | for(int i = 0; i<bitsperrow; i++) |
lz307 | 0:a26aca3e4760 | 260 | { |
lz307 | 0:a26aca3e4760 | 261 | data[i] = 0; |
lz307 | 0:a26aca3e4760 | 262 | } |
lz307 | 0:a26aca3e4760 | 263 | int index = getOffset() + m_rowsize*row; |
lz307 | 0:a26aca3e4760 | 264 | fseek(m_pFile, index, SEEK_SET); |
lz307 | 0:a26aca3e4760 | 265 | |
lz307 | 0:a26aca3e4760 | 266 | if(getColorDepth() == 24) |
lz307 | 0:a26aca3e4760 | 267 | { |
lz307 | 0:a26aca3e4760 | 268 | for(int i=0; i<getWidth(); i++) |
lz307 | 0:a26aca3e4760 | 269 | { |
lz307 | 0:a26aca3e4760 | 270 | char temp[3]; |
lz307 | 0:a26aca3e4760 | 271 | fread(temp,sizeof(char),3,m_pFile); |
lz307 | 0:a26aca3e4760 | 272 | int average = (temp[0]+temp[1]+temp[2])/3; |
lz307 | 0:a26aca3e4760 | 273 | int val = average<128?0:1; |
lz307 | 0:a26aca3e4760 | 274 | data[i/8] |= (val*0x80) >> (i%8); |
lz307 | 0:a26aca3e4760 | 275 | } |
lz307 | 0:a26aca3e4760 | 276 | } |
lz307 | 0:a26aca3e4760 | 277 | else if(getColorDepth() == 1) |
lz307 | 0:a26aca3e4760 | 278 | { |
lz307 | 0:a26aca3e4760 | 279 | fread(data,sizeof(char),bitsperrow,m_pFile); |
lz307 | 0:a26aca3e4760 | 280 | } |
lz307 | 0:a26aca3e4760 | 281 | |
lz307 | 0:a26aca3e4760 | 282 | if(closefile) |
lz307 | 0:a26aca3e4760 | 283 | { |
lz307 | 0:a26aca3e4760 | 284 | close(); |
lz307 | 0:a26aca3e4760 | 285 | } |
lz307 | 0:a26aca3e4760 | 286 | return data; |
lz307 | 0:a26aca3e4760 | 287 | } |