Simplified Doodle Jump game for mbed

Dependencies:   4DGL-uLCD-SE LSM9DS1_Library_cal SDFileSystem mbed-rtos mbed wave_player

Committer:
bhill42
Date:
Mon Mar 14 03:03:26 2016 +0000
Revision:
1:bdeb188cb474
Child:
3:141c57be5a2d
before we fuck shit up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhill42 1:bdeb188cb474 1 #include "myBMP.h"
bhill42 1:bdeb188cb474 2
bhill42 1:bdeb188cb474 3 int BitDepth = 1;
bhill42 1:bdeb188cb474 4 int Width = 1;
bhill42 1:bdeb188cb474 5 int Height = 1;
bhill42 1:bdeb188cb474 6 RGBApixel *Colors;
bhill42 1:bdeb188cb474 7
bhill42 1:bdeb188cb474 8 bool SafeFread( char* buffer, int size, int number, FILE* fp )
bhill42 1:bdeb188cb474 9 {
bhill42 1:bdeb188cb474 10 using namespace std;
bhill42 1:bdeb188cb474 11 int ItemsRead;
bhill42 1:bdeb188cb474 12 if( feof(fp) )
bhill42 1:bdeb188cb474 13 { return false; }
bhill42 1:bdeb188cb474 14 ItemsRead = (int) fread( buffer , size , number , fp );
bhill42 1:bdeb188cb474 15 if( ItemsRead < number )
bhill42 1:bdeb188cb474 16 { return false; }
bhill42 1:bdeb188cb474 17 return true;
bhill42 1:bdeb188cb474 18 }
bhill42 1:bdeb188cb474 19
bhill42 1:bdeb188cb474 20
bhill42 1:bdeb188cb474 21 RGBApixel GetColor( int ColorNumber)
bhill42 1:bdeb188cb474 22 {
bhill42 1:bdeb188cb474 23 RGBApixel Output;
bhill42 1:bdeb188cb474 24 Output.Red = 255;
bhill42 1:bdeb188cb474 25 Output.Green = 255;
bhill42 1:bdeb188cb474 26 Output.Blue = 255;
bhill42 1:bdeb188cb474 27 Output.Alpha = 0;
bhill42 1:bdeb188cb474 28
bhill42 1:bdeb188cb474 29 if( BitDepth != 1 && BitDepth != 4 && BitDepth != 8 )
bhill42 1:bdeb188cb474 30 {
bhill42 1:bdeb188cb474 31 return Output;
bhill42 1:bdeb188cb474 32 }
bhill42 1:bdeb188cb474 33 if( !Colors )
bhill42 1:bdeb188cb474 34 {
bhill42 1:bdeb188cb474 35 return Output;
bhill42 1:bdeb188cb474 36 }
bhill42 1:bdeb188cb474 37 Output = Colors[ColorNumber];
bhill42 1:bdeb188cb474 38 return Output;
bhill42 1:bdeb188cb474 39 }
bhill42 1:bdeb188cb474 40
bhill42 1:bdeb188cb474 41 bool Read8bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd)
bhill42 1:bdeb188cb474 42 {
bhill42 1:bdeb188cb474 43 int i;
bhill42 1:bdeb188cb474 44 if( Width > BufferSize )
bhill42 1:bdeb188cb474 45 { return false; }
bhill42 1:bdeb188cb474 46 for( i=0 ; i < Width ; i++ )
bhill42 1:bdeb188cb474 47 {
bhill42 1:bdeb188cb474 48 int Index = Buffer[i];
bhill42 1:bdeb188cb474 49 //Blue, Green, Red, Alpha
bhill42 1:bdeb188cb474 50 RGBApixel colors = GetColor(Index);
bhill42 1:bdeb188cb474 51 int color = 0x00000000 | (colors.Red<< 16) | (colors.Blue << 8) | (colors.Green);
bhill42 1:bdeb188cb474 52 (*lcd).pixel((130-Width)/2+i,(130-Height)/2+Row,color);
bhill42 1:bdeb188cb474 53 }
bhill42 1:bdeb188cb474 54 return true;
bhill42 1:bdeb188cb474 55 }
bhill42 1:bdeb188cb474 56
bhill42 1:bdeb188cb474 57 bool Read4bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd)
bhill42 1:bdeb188cb474 58 {
bhill42 1:bdeb188cb474 59 int Shifts[2] = {4 ,0 };
bhill42 1:bdeb188cb474 60 int Masks[2] = {240,15};
bhill42 1:bdeb188cb474 61
bhill42 1:bdeb188cb474 62 int i=0;
bhill42 1:bdeb188cb474 63 int j;
bhill42 1:bdeb188cb474 64 int k=0;
bhill42 1:bdeb188cb474 65 if( Width > 2*BufferSize )
bhill42 1:bdeb188cb474 66 { return false; }
bhill42 1:bdeb188cb474 67 while( i < Width )
bhill42 1:bdeb188cb474 68 {
bhill42 1:bdeb188cb474 69 j=0;
bhill42 1:bdeb188cb474 70 while( j < 2 && i < Width )
bhill42 1:bdeb188cb474 71 {
bhill42 1:bdeb188cb474 72 int Index = (int) ( (Buffer[k]&Masks[j]) >> Shifts[j]);
bhill42 1:bdeb188cb474 73 RGBApixel colors = GetColor(Index);
bhill42 1:bdeb188cb474 74 int color = 0x00000000 | (colors.Red<< 16) | (colors.Blue << 8) | (colors.Green);
bhill42 1:bdeb188cb474 75 (*lcd).pixel((130-Width)/2+i,(130-Height)/2+Row,color);
bhill42 1:bdeb188cb474 76 i++; j++;
bhill42 1:bdeb188cb474 77 }
bhill42 1:bdeb188cb474 78 k++;
bhill42 1:bdeb188cb474 79 }
bhill42 1:bdeb188cb474 80 return true;
bhill42 1:bdeb188cb474 81 }
bhill42 1:bdeb188cb474 82
bhill42 1:bdeb188cb474 83
bhill42 1:bdeb188cb474 84
bhill42 1:bdeb188cb474 85
bhill42 1:bdeb188cb474 86
bhill42 1:bdeb188cb474 87
bhill42 1:bdeb188cb474 88
bhill42 1:bdeb188cb474 89
bhill42 1:bdeb188cb474 90
bhill42 1:bdeb188cb474 91 bool Read32bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd, int x, int y)
bhill42 1:bdeb188cb474 92 {
bhill42 1:bdeb188cb474 93 int i;
bhill42 1:bdeb188cb474 94 char Colors[4];
bhill42 1:bdeb188cb474 95 if( Width*4 > BufferSize )
bhill42 1:bdeb188cb474 96 { return false; }
bhill42 1:bdeb188cb474 97 for( i=0 ; i < Width ; i++ )
bhill42 1:bdeb188cb474 98 {
bhill42 1:bdeb188cb474 99 memcpy( (char*) &(Colors), (char*) Buffer+4*i, 4 );
bhill42 1:bdeb188cb474 100 //Blue, Green, Red, Alpha
bhill42 1:bdeb188cb474 101 int color = 0x00000000 | (Colors[2] << 16) | (Colors[1] << 8) | (Colors[0]);
bhill42 1:bdeb188cb474 102 (*lcd).pixel(x+i,y+Row,color);
bhill42 1:bdeb188cb474 103 }
bhill42 1:bdeb188cb474 104 return true;
bhill42 1:bdeb188cb474 105 }
bhill42 1:bdeb188cb474 106
bhill42 1:bdeb188cb474 107
bhill42 1:bdeb188cb474 108 bool Read24bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd, int x, int y)
bhill42 1:bdeb188cb474 109 {
bhill42 1:bdeb188cb474 110 int i;
bhill42 1:bdeb188cb474 111 //char Colors[4];
bhill42 1:bdeb188cb474 112 //if( Width*3 > BufferSize )
bhill42 1:bdeb188cb474 113 //{ return false; }
bhill42 1:bdeb188cb474 114 for( i=0 ; i < Width ; i++ )
bhill42 1:bdeb188cb474 115 {
bhill42 1:bdeb188cb474 116 //memcpy( (char*) &(Colors), (char*) Buffer+3*i, 3 );
bhill42 1:bdeb188cb474 117 //Blue, Green, Red, Alpha
bhill42 1:bdeb188cb474 118 int color = 0x00000000 | (Buffer[3*i+2] << 16) | (Buffer[3*i+1] << 8) | (Buffer[3*i+0]);
bhill42 1:bdeb188cb474 119 (*lcd).pixel(x+i,y+Row,color);
bhill42 1:bdeb188cb474 120 }
bhill42 1:bdeb188cb474 121 return true;
bhill42 1:bdeb188cb474 122 }
bhill42 1:bdeb188cb474 123
bhill42 1:bdeb188cb474 124
bhill42 1:bdeb188cb474 125 bool Read8bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd, int x, int y)
bhill42 1:bdeb188cb474 126 {
bhill42 1:bdeb188cb474 127 int i;
bhill42 1:bdeb188cb474 128 if( Width > BufferSize )
bhill42 1:bdeb188cb474 129 { return false; }
bhill42 1:bdeb188cb474 130 for( i=0 ; i < Width ; i++ )
bhill42 1:bdeb188cb474 131 {
bhill42 1:bdeb188cb474 132 int Index = Buffer[i];
bhill42 1:bdeb188cb474 133 //Blue, Green, Red, Alpha
bhill42 1:bdeb188cb474 134 RGBApixel colors = GetColor(Index);
bhill42 1:bdeb188cb474 135 int color = 0x00000000 | (colors.Red<< 16) | (colors.Blue << 8) | (colors.Green);
bhill42 1:bdeb188cb474 136 (*lcd).pixel(x+i,y+Row,color);
bhill42 1:bdeb188cb474 137 }
bhill42 1:bdeb188cb474 138 return true;
bhill42 1:bdeb188cb474 139 }
bhill42 1:bdeb188cb474 140
bhill42 1:bdeb188cb474 141
bhill42 1:bdeb188cb474 142 bool Read4bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd, int x, int y)
bhill42 1:bdeb188cb474 143 {
bhill42 1:bdeb188cb474 144 int Shifts[2] = {4 ,0 };
bhill42 1:bdeb188cb474 145 int Masks[2] = {240,15};
bhill42 1:bdeb188cb474 146
bhill42 1:bdeb188cb474 147 int i=0;
bhill42 1:bdeb188cb474 148 int j;
bhill42 1:bdeb188cb474 149 int k=0;
bhill42 1:bdeb188cb474 150 if( Width > 2*BufferSize )
bhill42 1:bdeb188cb474 151 { return false; }
bhill42 1:bdeb188cb474 152 while( i < Width )
bhill42 1:bdeb188cb474 153 {
bhill42 1:bdeb188cb474 154 j=0;
bhill42 1:bdeb188cb474 155 while( j < 2 && i < Width )
bhill42 1:bdeb188cb474 156 {
bhill42 1:bdeb188cb474 157 int Index = (int) ( (Buffer[k]&Masks[j]) >> Shifts[j]);
bhill42 1:bdeb188cb474 158 RGBApixel colors = GetColor(Index);
bhill42 1:bdeb188cb474 159 int color = 0x00000000 | (colors.Red<< 16) | (colors.Blue << 8) | (colors.Green);
bhill42 1:bdeb188cb474 160 (*lcd).pixel(x+i,y+Row,color);
bhill42 1:bdeb188cb474 161 i++; j++;
bhill42 1:bdeb188cb474 162 }
bhill42 1:bdeb188cb474 163 k++;
bhill42 1:bdeb188cb474 164 }
bhill42 1:bdeb188cb474 165 return true;
bhill42 1:bdeb188cb474 166 }
bhill42 1:bdeb188cb474 167
bhill42 1:bdeb188cb474 168
bhill42 1:bdeb188cb474 169 bool Read1bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd, int x, int y)
bhill42 1:bdeb188cb474 170 {
bhill42 1:bdeb188cb474 171 int Shifts[8] = {7 ,6 ,5 ,4 ,3,2,1,0};
bhill42 1:bdeb188cb474 172 int Masks[8] = {128,64,32,16,8,4,2,1};
bhill42 1:bdeb188cb474 173
bhill42 1:bdeb188cb474 174 int i=0;
bhill42 1:bdeb188cb474 175 int j;
bhill42 1:bdeb188cb474 176 int k=0;
bhill42 1:bdeb188cb474 177
bhill42 1:bdeb188cb474 178 if( Width > 8*BufferSize )
bhill42 1:bdeb188cb474 179 { return false; }
bhill42 1:bdeb188cb474 180 while( i < Width )
bhill42 1:bdeb188cb474 181 {
bhill42 1:bdeb188cb474 182 j=0;
bhill42 1:bdeb188cb474 183 while( j < 8 && i < Width )
bhill42 1:bdeb188cb474 184 {
bhill42 1:bdeb188cb474 185 int Index = (int) ( (Buffer[k]&Masks[j]) >> Shifts[j]);
bhill42 1:bdeb188cb474 186 RGBApixel colors = GetColor(Index);
bhill42 1:bdeb188cb474 187 int color = 0x00000000 | (colors.Red<< 16) | (colors.Blue << 8) | (colors.Green);
bhill42 1:bdeb188cb474 188 (*lcd).pixel(x+i,y+Row,color);
bhill42 1:bdeb188cb474 189 i++; j++;
bhill42 1:bdeb188cb474 190 }
bhill42 1:bdeb188cb474 191 k++;
bhill42 1:bdeb188cb474 192 }
bhill42 1:bdeb188cb474 193 return true;
bhill42 1:bdeb188cb474 194 }
bhill42 1:bdeb188cb474 195
bhill42 1:bdeb188cb474 196
bhill42 1:bdeb188cb474 197
bhill42 1:bdeb188cb474 198
bhill42 1:bdeb188cb474 199
bhill42 1:bdeb188cb474 200
bhill42 1:bdeb188cb474 201 bool Read32bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd)
bhill42 1:bdeb188cb474 202 {
bhill42 1:bdeb188cb474 203 int i;
bhill42 1:bdeb188cb474 204 char Colors[4];
bhill42 1:bdeb188cb474 205 if( Width*4 > BufferSize )
bhill42 1:bdeb188cb474 206 { return false; }
bhill42 1:bdeb188cb474 207 for( i=0 ; i < Width ; i++ )
bhill42 1:bdeb188cb474 208 {
bhill42 1:bdeb188cb474 209 memcpy( (char*) &(Colors), (char*) Buffer+4*i, 4 );
bhill42 1:bdeb188cb474 210 //Blue, Green, Red, Alpha
bhill42 1:bdeb188cb474 211 int color = 0x00000000 | (Colors[2] << 16) | (Colors[1] << 8) | (Colors[0]);
bhill42 1:bdeb188cb474 212 (*lcd).pixel((130-Width)/2+i,(130-Height)/2+Row,color);
bhill42 1:bdeb188cb474 213 }
bhill42 1:bdeb188cb474 214 return true;
bhill42 1:bdeb188cb474 215 }
bhill42 1:bdeb188cb474 216
bhill42 1:bdeb188cb474 217 bool Read24bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd)
bhill42 1:bdeb188cb474 218 {
bhill42 1:bdeb188cb474 219 int i;
bhill42 1:bdeb188cb474 220 char Colors[4];
bhill42 1:bdeb188cb474 221 if( Width*3 > BufferSize )
bhill42 1:bdeb188cb474 222 { return false; }
bhill42 1:bdeb188cb474 223 for( i=0 ; i < Width ; i++ )
bhill42 1:bdeb188cb474 224 {
bhill42 1:bdeb188cb474 225 memcpy( (char*) &(Colors), (char*) Buffer+3*i, 3 );
bhill42 1:bdeb188cb474 226 //Blue, Green, Red, Alpha
bhill42 1:bdeb188cb474 227 int color = 0x00000000 | (Colors[2] << 16) | (Colors[1] << 8) | (Colors[0]);
bhill42 1:bdeb188cb474 228 (*lcd).pixel((130-Width)/2+i,(130-Height)/2+Row,color);
bhill42 1:bdeb188cb474 229 }
bhill42 1:bdeb188cb474 230 return true;
bhill42 1:bdeb188cb474 231 }
bhill42 1:bdeb188cb474 232
bhill42 1:bdeb188cb474 233
bhill42 1:bdeb188cb474 234 bool Read1bitRow( ebmpBYTE* Buffer, int BufferSize, int Row, uLCD_4DGL *lcd)
bhill42 1:bdeb188cb474 235 {
bhill42 1:bdeb188cb474 236 int Shifts[8] = {7 ,6 ,5 ,4 ,3,2,1,0};
bhill42 1:bdeb188cb474 237 int Masks[8] = {128,64,32,16,8,4,2,1};
bhill42 1:bdeb188cb474 238
bhill42 1:bdeb188cb474 239 int i=0;
bhill42 1:bdeb188cb474 240 int j;
bhill42 1:bdeb188cb474 241 int k=0;
bhill42 1:bdeb188cb474 242
bhill42 1:bdeb188cb474 243 if( Width > 8*BufferSize )
bhill42 1:bdeb188cb474 244 { return false; }
bhill42 1:bdeb188cb474 245 while( i < Width )
bhill42 1:bdeb188cb474 246 {
bhill42 1:bdeb188cb474 247 j=0;
bhill42 1:bdeb188cb474 248 while( j < 8 && i < Width )
bhill42 1:bdeb188cb474 249 {
bhill42 1:bdeb188cb474 250 int Index = (int) ( (Buffer[k]&Masks[j]) >> Shifts[j]);
bhill42 1:bdeb188cb474 251 RGBApixel colors = GetColor(Index);
bhill42 1:bdeb188cb474 252 int color = 0x00000000 | (colors.Red<< 16) | (colors.Blue << 8) | (colors.Green);
bhill42 1:bdeb188cb474 253 (*lcd).pixel((130-Width)/2+i,(130-Height)/2+Row,color);
bhill42 1:bdeb188cb474 254 i++; j++;
bhill42 1:bdeb188cb474 255 }
bhill42 1:bdeb188cb474 256 k++;
bhill42 1:bdeb188cb474 257 }
bhill42 1:bdeb188cb474 258 return true;
bhill42 1:bdeb188cb474 259 }
bhill42 1:bdeb188cb474 260
bhill42 1:bdeb188cb474 261 int TellNumberOfColors( int BitDepth )
bhill42 1:bdeb188cb474 262 {
bhill42 1:bdeb188cb474 263 int output = 1 << BitDepth;
bhill42 1:bdeb188cb474 264 if( BitDepth == 32 )
bhill42 1:bdeb188cb474 265 { output = 1 << 24; }
bhill42 1:bdeb188cb474 266 return output;
bhill42 1:bdeb188cb474 267 }
bhill42 1:bdeb188cb474 268
bhill42 1:bdeb188cb474 269 bool ReadBMPFromFile( const char* FileName, RGBApixel *Colors, uLCD_4DGL *lcd)
bhill42 1:bdeb188cb474 270 {
bhill42 1:bdeb188cb474 271 FILE* fp = fopen( FileName, "rb" );
bhill42 1:bdeb188cb474 272 if( fp == NULL )
bhill42 1:bdeb188cb474 273 {
bhill42 1:bdeb188cb474 274 return false;
bhill42 1:bdeb188cb474 275 }
bhill42 1:bdeb188cb474 276
bhill42 1:bdeb188cb474 277 // read the file header
bhill42 1:bdeb188cb474 278
bhill42 1:bdeb188cb474 279 BMFH bmfh;
bhill42 1:bdeb188cb474 280 bool NotCorrupted = true;
bhill42 1:bdeb188cb474 281
bhill42 1:bdeb188cb474 282 NotCorrupted &= SafeFread( (char*) &(bmfh.bfType) , sizeof(ebmpWORD), 1, fp);
bhill42 1:bdeb188cb474 283
bhill42 1:bdeb188cb474 284 bool IsBitmap = false;
bhill42 1:bdeb188cb474 285
bhill42 1:bdeb188cb474 286 if( bmfh.bfType == 19778 )
bhill42 1:bdeb188cb474 287 { IsBitmap = true; }
bhill42 1:bdeb188cb474 288
bhill42 1:bdeb188cb474 289 if( !IsBitmap )
bhill42 1:bdeb188cb474 290 {
bhill42 1:bdeb188cb474 291 fclose( fp );
bhill42 1:bdeb188cb474 292 return false;
bhill42 1:bdeb188cb474 293 }
bhill42 1:bdeb188cb474 294
bhill42 1:bdeb188cb474 295 NotCorrupted &= SafeFread( (char*) &(bmfh.bfSize) , sizeof(ebmpDWORD) , 1, fp);
bhill42 1:bdeb188cb474 296 NotCorrupted &= SafeFread( (char*) &(bmfh.bfReserved1) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 297 NotCorrupted &= SafeFread( (char*) &(bmfh.bfReserved2) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 298 NotCorrupted &= SafeFread( (char*) &(bmfh.bfOffBits) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 299
bhill42 1:bdeb188cb474 300 // read the info header
bhill42 1:bdeb188cb474 301
bhill42 1:bdeb188cb474 302 BMIH bmih;
bhill42 1:bdeb188cb474 303
bhill42 1:bdeb188cb474 304 NotCorrupted &= SafeFread( (char*) &(bmih.biSize) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 305 NotCorrupted &= SafeFread( (char*) &(bmih.biWidth) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 306 NotCorrupted &= SafeFread( (char*) &(bmih.biHeight) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 307 NotCorrupted &= SafeFread( (char*) &(bmih.biPlanes) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 308 NotCorrupted &= SafeFread( (char*) &(bmih.biBitCount) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 309
bhill42 1:bdeb188cb474 310 NotCorrupted &= SafeFread( (char*) &(bmih.biCompression) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 311 NotCorrupted &= SafeFread( (char*) &(bmih.biSizeImage) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 312 NotCorrupted &= SafeFread( (char*) &(bmih.biXPelsPerMeter) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 313 NotCorrupted &= SafeFread( (char*) &(bmih.biYPelsPerMeter) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 314 NotCorrupted &= SafeFread( (char*) &(bmih.biClrUsed) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 315 NotCorrupted &= SafeFread( (char*) &(bmih.biClrImportant) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 316
bhill42 1:bdeb188cb474 317 // a safety catch: if any of the header information didn't read properly, abort
bhill42 1:bdeb188cb474 318 // future idea: check to see if at least most is self-consistent
bhill42 1:bdeb188cb474 319
bhill42 1:bdeb188cb474 320 if( !NotCorrupted )
bhill42 1:bdeb188cb474 321 {
bhill42 1:bdeb188cb474 322 fclose(fp);
bhill42 1:bdeb188cb474 323 return false;
bhill42 1:bdeb188cb474 324 }
bhill42 1:bdeb188cb474 325
bhill42 1:bdeb188cb474 326 // if bmih.biCompression 1 or 2, then the file is RLE compressed
bhill42 1:bdeb188cb474 327
bhill42 1:bdeb188cb474 328 if( bmih.biCompression == 1 || bmih.biCompression == 2 )
bhill42 1:bdeb188cb474 329 {
bhill42 1:bdeb188cb474 330 fclose(fp);
bhill42 1:bdeb188cb474 331 return false;
bhill42 1:bdeb188cb474 332 }
bhill42 1:bdeb188cb474 333
bhill42 1:bdeb188cb474 334 // if bmih.biCompression > 3, then something strange is going on
bhill42 1:bdeb188cb474 335 // it's probably an OS2 bitmap file.
bhill42 1:bdeb188cb474 336
bhill42 1:bdeb188cb474 337 if( bmih.biCompression > 3 )
bhill42 1:bdeb188cb474 338 {
bhill42 1:bdeb188cb474 339 fclose(fp);
bhill42 1:bdeb188cb474 340 return false;
bhill42 1:bdeb188cb474 341 }
bhill42 1:bdeb188cb474 342
bhill42 1:bdeb188cb474 343 if( bmih.biCompression == 3 && bmih.biBitCount != 16 )
bhill42 1:bdeb188cb474 344 {
bhill42 1:bdeb188cb474 345 fclose(fp);
bhill42 1:bdeb188cb474 346 return false;
bhill42 1:bdeb188cb474 347 }
bhill42 1:bdeb188cb474 348
bhill42 1:bdeb188cb474 349 // set the bit depth
bhill42 1:bdeb188cb474 350
bhill42 1:bdeb188cb474 351 int TempBitDepth = (int) bmih.biBitCount;
bhill42 1:bdeb188cb474 352 if( TempBitDepth != 1 && TempBitDepth != 4
bhill42 1:bdeb188cb474 353 && TempBitDepth != 8 && TempBitDepth != 16
bhill42 1:bdeb188cb474 354 && TempBitDepth != 24 && TempBitDepth != 32 )
bhill42 1:bdeb188cb474 355 {
bhill42 1:bdeb188cb474 356 fclose(fp);
bhill42 1:bdeb188cb474 357 return false;
bhill42 1:bdeb188cb474 358 }
bhill42 1:bdeb188cb474 359 BitDepth = (int)bmih.biBitCount;
bhill42 1:bdeb188cb474 360 // set the size
bhill42 1:bdeb188cb474 361
bhill42 1:bdeb188cb474 362 if( (int) bmih.biWidth <= 0 || (int) bmih.biHeight <= 0 )
bhill42 1:bdeb188cb474 363 {
bhill42 1:bdeb188cb474 364 fclose(fp);
bhill42 1:bdeb188cb474 365 return false;
bhill42 1:bdeb188cb474 366 }
bhill42 1:bdeb188cb474 367 Width = (int) bmih.biWidth;
bhill42 1:bdeb188cb474 368 Height = (int) bmih.biHeight;
bhill42 1:bdeb188cb474 369 // some preliminaries
bhill42 1:bdeb188cb474 370
bhill42 1:bdeb188cb474 371 double dBytesPerPixel = ( (double) BitDepth ) / 8.0;
bhill42 1:bdeb188cb474 372 double dBytesPerRow = dBytesPerPixel * (Width+0.0);
bhill42 1:bdeb188cb474 373 dBytesPerRow = ceil(dBytesPerRow);
bhill42 1:bdeb188cb474 374
bhill42 1:bdeb188cb474 375 int BytePaddingPerRow = 4 - ( (int) (dBytesPerRow) )% 4;
bhill42 1:bdeb188cb474 376 if( BytePaddingPerRow == 4 )
bhill42 1:bdeb188cb474 377 { BytePaddingPerRow = 0; }
bhill42 1:bdeb188cb474 378
bhill42 1:bdeb188cb474 379 // if < 16 bits, read the palette
bhill42 1:bdeb188cb474 380
bhill42 1:bdeb188cb474 381 if( BitDepth < 16 )
bhill42 1:bdeb188cb474 382 {
bhill42 1:bdeb188cb474 383 // determine the number of colors specified in the
bhill42 1:bdeb188cb474 384 // color table
bhill42 1:bdeb188cb474 385
bhill42 1:bdeb188cb474 386 int NumberOfColorsToRead = ((int) bmfh.bfOffBits - 54 )/4;
bhill42 1:bdeb188cb474 387 if( NumberOfColorsToRead > (1 << BitDepth) )
bhill42 1:bdeb188cb474 388 { NumberOfColorsToRead = (1 << BitDepth); }
bhill42 1:bdeb188cb474 389
bhill42 1:bdeb188cb474 390 int n;
bhill42 1:bdeb188cb474 391 for( n=0; n < NumberOfColorsToRead ; n++ )
bhill42 1:bdeb188cb474 392 {
bhill42 1:bdeb188cb474 393 SafeFread( (char*) &(Colors[n]) , 4 , 1 , fp);
bhill42 1:bdeb188cb474 394 }
bhill42 1:bdeb188cb474 395 for( n=NumberOfColorsToRead ; n < TellNumberOfColors(BitDepth) ; n++ )
bhill42 1:bdeb188cb474 396 {
bhill42 1:bdeb188cb474 397 RGBApixel HWITE;
bhill42 1:bdeb188cb474 398 HWITE.Red = 255;
bhill42 1:bdeb188cb474 399 HWITE.Green = 255;
bhill42 1:bdeb188cb474 400 HWITE.Blue = 255;
bhill42 1:bdeb188cb474 401 HWITE.Alpha = 0;
bhill42 1:bdeb188cb474 402 Colors[n] = HWITE;
bhill42 1:bdeb188cb474 403 }
bhill42 1:bdeb188cb474 404 }
bhill42 1:bdeb188cb474 405
bhill42 1:bdeb188cb474 406 // skip blank data if bfOffBits so indicates
bhill42 1:bdeb188cb474 407
bhill42 1:bdeb188cb474 408 int BytesToSkip = bmfh.bfOffBits - 54;;
bhill42 1:bdeb188cb474 409 if( BitDepth < 16 )
bhill42 1:bdeb188cb474 410 { BytesToSkip -= 4*(1 << BitDepth); }
bhill42 1:bdeb188cb474 411 if( BitDepth == 16 && bmih.biCompression == 3 )
bhill42 1:bdeb188cb474 412 { BytesToSkip -= 3*4; }
bhill42 1:bdeb188cb474 413 if( BytesToSkip < 0 )
bhill42 1:bdeb188cb474 414 { BytesToSkip = 0; }
bhill42 1:bdeb188cb474 415 if( BytesToSkip > 0 && BitDepth != 16 )
bhill42 1:bdeb188cb474 416 {
bhill42 1:bdeb188cb474 417 ebmpBYTE* TempSkipBYTE;
bhill42 1:bdeb188cb474 418 TempSkipBYTE = new ebmpBYTE [BytesToSkip];
bhill42 1:bdeb188cb474 419 SafeFread( (char*) TempSkipBYTE , BytesToSkip , 1 , fp);
bhill42 1:bdeb188cb474 420 delete [] TempSkipBYTE;
bhill42 1:bdeb188cb474 421 }
bhill42 1:bdeb188cb474 422
bhill42 1:bdeb188cb474 423 // This code reads 1, 4, 8, 24, and 32-bpp files
bhill42 1:bdeb188cb474 424 // with a more-efficient buffered technique.
bhill42 1:bdeb188cb474 425
bhill42 1:bdeb188cb474 426 int i,j;
bhill42 1:bdeb188cb474 427 if( BitDepth != 16 )
bhill42 1:bdeb188cb474 428 {
bhill42 1:bdeb188cb474 429 int BufferSize = (int) ( (Width*BitDepth) / 8.0 );
bhill42 1:bdeb188cb474 430 while( 8*BufferSize < Width*BitDepth )
bhill42 1:bdeb188cb474 431 { BufferSize++; }
bhill42 1:bdeb188cb474 432 while( BufferSize % 4 )
bhill42 1:bdeb188cb474 433 { BufferSize++; }
bhill42 1:bdeb188cb474 434 ebmpBYTE* Buffer;
bhill42 1:bdeb188cb474 435 Buffer = new ebmpBYTE [BufferSize];
bhill42 1:bdeb188cb474 436 j= Height-1;
bhill42 1:bdeb188cb474 437 while( j > -1 )
bhill42 1:bdeb188cb474 438 {
bhill42 1:bdeb188cb474 439 int BytesRead = (int) fread( (char*) Buffer, 1, BufferSize, fp );
bhill42 1:bdeb188cb474 440 if( BytesRead < BufferSize )
bhill42 1:bdeb188cb474 441 {
bhill42 1:bdeb188cb474 442 j = -1;
bhill42 1:bdeb188cb474 443 }
bhill42 1:bdeb188cb474 444 else
bhill42 1:bdeb188cb474 445 {
bhill42 1:bdeb188cb474 446 bool Success = false;
bhill42 1:bdeb188cb474 447 if( BitDepth == 1 )
bhill42 1:bdeb188cb474 448 { Success = Read1bitRow( Buffer, BufferSize, j , lcd); }
bhill42 1:bdeb188cb474 449 if( BitDepth == 4 )
bhill42 1:bdeb188cb474 450 { Success = Read4bitRow( Buffer, BufferSize, j , lcd); }
bhill42 1:bdeb188cb474 451 if( BitDepth == 8 )
bhill42 1:bdeb188cb474 452 { Success = Read8bitRow( Buffer, BufferSize, j , lcd); }
bhill42 1:bdeb188cb474 453 if( BitDepth == 24 )
bhill42 1:bdeb188cb474 454 { Success = Read24bitRow( Buffer, BufferSize, j , lcd); }
bhill42 1:bdeb188cb474 455 if( BitDepth == 32 )
bhill42 1:bdeb188cb474 456 { Success = Read32bitRow( Buffer, BufferSize, j , lcd); }
bhill42 1:bdeb188cb474 457 if( !Success )
bhill42 1:bdeb188cb474 458 {
bhill42 1:bdeb188cb474 459 j = -1;
bhill42 1:bdeb188cb474 460 }
bhill42 1:bdeb188cb474 461 }
bhill42 1:bdeb188cb474 462 j--;
bhill42 1:bdeb188cb474 463 }
bhill42 1:bdeb188cb474 464 delete [] Buffer;
bhill42 1:bdeb188cb474 465 }
bhill42 1:bdeb188cb474 466
bhill42 1:bdeb188cb474 467 if( BitDepth == 16 )
bhill42 1:bdeb188cb474 468 {
bhill42 1:bdeb188cb474 469 int DataBytes = Width*2;
bhill42 1:bdeb188cb474 470 int PaddingBytes = ( 4 - DataBytes % 4 ) % 4;
bhill42 1:bdeb188cb474 471
bhill42 1:bdeb188cb474 472 // set the default mask
bhill42 1:bdeb188cb474 473
bhill42 1:bdeb188cb474 474 ebmpWORD BlueMask = 31; // bits 12-16
bhill42 1:bdeb188cb474 475 ebmpWORD GreenMask = 992; // bits 7-11
bhill42 1:bdeb188cb474 476 ebmpWORD RedMask = 31744; // bits 2-6
bhill42 1:bdeb188cb474 477
bhill42 1:bdeb188cb474 478 // read the bit fields, if necessary, to
bhill42 1:bdeb188cb474 479 // override the default 5-5-5 mask
bhill42 1:bdeb188cb474 480
bhill42 1:bdeb188cb474 481 if( bmih.biCompression != 0 )
bhill42 1:bdeb188cb474 482 {
bhill42 1:bdeb188cb474 483 // read the three bit masks
bhill42 1:bdeb188cb474 484
bhill42 1:bdeb188cb474 485 ebmpWORD TempMaskWORD;
bhill42 1:bdeb188cb474 486
bhill42 1:bdeb188cb474 487 SafeFread( (char*) &RedMask , 2 , 1 , fp );
bhill42 1:bdeb188cb474 488 SafeFread( (char*) &TempMaskWORD , 2, 1, fp );
bhill42 1:bdeb188cb474 489
bhill42 1:bdeb188cb474 490 SafeFread( (char*) &GreenMask , 2 , 1 , fp );
bhill42 1:bdeb188cb474 491 SafeFread( (char*) &TempMaskWORD , 2, 1, fp );
bhill42 1:bdeb188cb474 492
bhill42 1:bdeb188cb474 493 SafeFread( (char*) &BlueMask , 2 , 1 , fp );
bhill42 1:bdeb188cb474 494 SafeFread( (char*) &TempMaskWORD , 2, 1, fp );
bhill42 1:bdeb188cb474 495 }
bhill42 1:bdeb188cb474 496
bhill42 1:bdeb188cb474 497 // read and skip any meta data
bhill42 1:bdeb188cb474 498
bhill42 1:bdeb188cb474 499 if( BytesToSkip > 0 )
bhill42 1:bdeb188cb474 500 {
bhill42 1:bdeb188cb474 501 ebmpBYTE* TempSkipBYTE;
bhill42 1:bdeb188cb474 502 TempSkipBYTE = new ebmpBYTE [BytesToSkip];
bhill42 1:bdeb188cb474 503 SafeFread( (char*) TempSkipBYTE , BytesToSkip , 1 , fp);
bhill42 1:bdeb188cb474 504 delete [] TempSkipBYTE;
bhill42 1:bdeb188cb474 505 }
bhill42 1:bdeb188cb474 506
bhill42 1:bdeb188cb474 507 // determine the red, green and blue shifts
bhill42 1:bdeb188cb474 508
bhill42 1:bdeb188cb474 509 int GreenShift = 0;
bhill42 1:bdeb188cb474 510 ebmpWORD TempShiftWORD = GreenMask;
bhill42 1:bdeb188cb474 511 while( TempShiftWORD > 31 )
bhill42 1:bdeb188cb474 512 { TempShiftWORD = TempShiftWORD>>1; GreenShift++; }
bhill42 1:bdeb188cb474 513 int BlueShift = 0;
bhill42 1:bdeb188cb474 514 TempShiftWORD = BlueMask;
bhill42 1:bdeb188cb474 515 while( TempShiftWORD > 31 )
bhill42 1:bdeb188cb474 516 { TempShiftWORD = TempShiftWORD>>1; BlueShift++; }
bhill42 1:bdeb188cb474 517 int RedShift = 0;
bhill42 1:bdeb188cb474 518 TempShiftWORD = RedMask;
bhill42 1:bdeb188cb474 519 while( TempShiftWORD > 31 )
bhill42 1:bdeb188cb474 520 { TempShiftWORD = TempShiftWORD>>1; RedShift++; }
bhill42 1:bdeb188cb474 521
bhill42 1:bdeb188cb474 522 // read the actual pixels
bhill42 1:bdeb188cb474 523
bhill42 1:bdeb188cb474 524 for( j=Height-1 ; j >= 0 ; j-- )
bhill42 1:bdeb188cb474 525 {
bhill42 1:bdeb188cb474 526 i=0;
bhill42 1:bdeb188cb474 527 int ReadNumber = 0;
bhill42 1:bdeb188cb474 528 while( ReadNumber < DataBytes )
bhill42 1:bdeb188cb474 529 {
bhill42 1:bdeb188cb474 530 ebmpWORD TempWORD;
bhill42 1:bdeb188cb474 531 SafeFread( (char*) &TempWORD , 2 , 1 , fp );
bhill42 1:bdeb188cb474 532 ReadNumber += 2;
bhill42 1:bdeb188cb474 533
bhill42 1:bdeb188cb474 534 ebmpWORD Red = RedMask & TempWORD;
bhill42 1:bdeb188cb474 535 ebmpWORD Green = GreenMask & TempWORD;
bhill42 1:bdeb188cb474 536 ebmpWORD Blue = BlueMask & TempWORD;
bhill42 1:bdeb188cb474 537
bhill42 1:bdeb188cb474 538 ebmpBYTE BlueBYTE = (ebmpBYTE) 8*(Blue>>BlueShift);
bhill42 1:bdeb188cb474 539 ebmpBYTE GreenBYTE = (ebmpBYTE) 8*(Green>>GreenShift);
bhill42 1:bdeb188cb474 540 ebmpBYTE RedBYTE = (ebmpBYTE) 8*(Red>>RedShift);
bhill42 1:bdeb188cb474 541
bhill42 1:bdeb188cb474 542 int color = 0x00000000 | (RedBYTE << 16) | (GreenBYTE << 8) | (BlueBYTE);
bhill42 1:bdeb188cb474 543 (*lcd).pixel((130-Width)/2+i,(130-Height)/2+j,color);
bhill42 1:bdeb188cb474 544 i++;
bhill42 1:bdeb188cb474 545 }
bhill42 1:bdeb188cb474 546 ReadNumber = 0;
bhill42 1:bdeb188cb474 547 while( ReadNumber < PaddingBytes )
bhill42 1:bdeb188cb474 548 {
bhill42 1:bdeb188cb474 549 ebmpBYTE TempBYTE;
bhill42 1:bdeb188cb474 550 SafeFread( (char*) &TempBYTE , 1, 1, fp);
bhill42 1:bdeb188cb474 551 ReadNumber++;
bhill42 1:bdeb188cb474 552 }
bhill42 1:bdeb188cb474 553 }
bhill42 1:bdeb188cb474 554
bhill42 1:bdeb188cb474 555 }
bhill42 1:bdeb188cb474 556
bhill42 1:bdeb188cb474 557 fclose(fp);
bhill42 1:bdeb188cb474 558 return true;
bhill42 1:bdeb188cb474 559 }
bhill42 1:bdeb188cb474 560
bhill42 1:bdeb188cb474 561 bool ReadBMPFromFile(const char * FileName, RGBApixel *Colors, uLCD_4DGL *lcd, int x, int y)
bhill42 1:bdeb188cb474 562 {
bhill42 1:bdeb188cb474 563 FILE* fp = fopen( FileName, "rb" );
bhill42 1:bdeb188cb474 564 if( fp == NULL )
bhill42 1:bdeb188cb474 565 {
bhill42 1:bdeb188cb474 566 return false;
bhill42 1:bdeb188cb474 567 }
bhill42 1:bdeb188cb474 568
bhill42 1:bdeb188cb474 569 // read the file header
bhill42 1:bdeb188cb474 570
bhill42 1:bdeb188cb474 571 BMFH bmfh;
bhill42 1:bdeb188cb474 572 bool NotCorrupted = true;
bhill42 1:bdeb188cb474 573
bhill42 1:bdeb188cb474 574 NotCorrupted &= SafeFread( (char*) &(bmfh.bfType) , sizeof(ebmpWORD), 1, fp);
bhill42 1:bdeb188cb474 575
bhill42 1:bdeb188cb474 576 bool IsBitmap = false;
bhill42 1:bdeb188cb474 577
bhill42 1:bdeb188cb474 578 if( bmfh.bfType == 19778 )
bhill42 1:bdeb188cb474 579 { IsBitmap = true; }
bhill42 1:bdeb188cb474 580
bhill42 1:bdeb188cb474 581 if( !IsBitmap )
bhill42 1:bdeb188cb474 582 {
bhill42 1:bdeb188cb474 583 fclose( fp );
bhill42 1:bdeb188cb474 584 return false;
bhill42 1:bdeb188cb474 585 }
bhill42 1:bdeb188cb474 586
bhill42 1:bdeb188cb474 587 NotCorrupted &= SafeFread( (char*) &(bmfh.bfSize) , sizeof(ebmpDWORD) , 1, fp);
bhill42 1:bdeb188cb474 588 NotCorrupted &= SafeFread( (char*) &(bmfh.bfReserved1) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 589 NotCorrupted &= SafeFread( (char*) &(bmfh.bfReserved2) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 590 NotCorrupted &= SafeFread( (char*) &(bmfh.bfOffBits) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 591
bhill42 1:bdeb188cb474 592 // read the info header
bhill42 1:bdeb188cb474 593
bhill42 1:bdeb188cb474 594 BMIH bmih;
bhill42 1:bdeb188cb474 595
bhill42 1:bdeb188cb474 596 NotCorrupted &= SafeFread( (char*) &(bmih.biSize) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 597 NotCorrupted &= SafeFread( (char*) &(bmih.biWidth) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 598 NotCorrupted &= SafeFread( (char*) &(bmih.biHeight) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 599 NotCorrupted &= SafeFread( (char*) &(bmih.biPlanes) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 600 NotCorrupted &= SafeFread( (char*) &(bmih.biBitCount) , sizeof(ebmpWORD) , 1, fp);
bhill42 1:bdeb188cb474 601
bhill42 1:bdeb188cb474 602 NotCorrupted &= SafeFread( (char*) &(bmih.biCompression) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 603 NotCorrupted &= SafeFread( (char*) &(bmih.biSizeImage) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 604 NotCorrupted &= SafeFread( (char*) &(bmih.biXPelsPerMeter) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 605 NotCorrupted &= SafeFread( (char*) &(bmih.biYPelsPerMeter) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 606 NotCorrupted &= SafeFread( (char*) &(bmih.biClrUsed) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 607 NotCorrupted &= SafeFread( (char*) &(bmih.biClrImportant) , sizeof(ebmpDWORD) , 1 , fp);
bhill42 1:bdeb188cb474 608
bhill42 1:bdeb188cb474 609 // a safety catch: if any of the header information didn't read properly, abort
bhill42 1:bdeb188cb474 610 // future idea: check to see if at least most is self-consistent
bhill42 1:bdeb188cb474 611
bhill42 1:bdeb188cb474 612 if( !NotCorrupted )
bhill42 1:bdeb188cb474 613 {
bhill42 1:bdeb188cb474 614 fclose(fp);
bhill42 1:bdeb188cb474 615 return false;
bhill42 1:bdeb188cb474 616 }
bhill42 1:bdeb188cb474 617
bhill42 1:bdeb188cb474 618 // if bmih.biCompression 1 or 2, then the file is RLE compressed
bhill42 1:bdeb188cb474 619
bhill42 1:bdeb188cb474 620 if( bmih.biCompression == 1 || bmih.biCompression == 2 )
bhill42 1:bdeb188cb474 621 {
bhill42 1:bdeb188cb474 622 fclose(fp);
bhill42 1:bdeb188cb474 623 return false;
bhill42 1:bdeb188cb474 624 }
bhill42 1:bdeb188cb474 625
bhill42 1:bdeb188cb474 626 // if bmih.biCompression > 3, then something strange is going on
bhill42 1:bdeb188cb474 627 // it's probably an OS2 bitmap file.
bhill42 1:bdeb188cb474 628
bhill42 1:bdeb188cb474 629 if( bmih.biCompression > 3 )
bhill42 1:bdeb188cb474 630 {
bhill42 1:bdeb188cb474 631 fclose(fp);
bhill42 1:bdeb188cb474 632 return false;
bhill42 1:bdeb188cb474 633 }
bhill42 1:bdeb188cb474 634
bhill42 1:bdeb188cb474 635 if( bmih.biCompression == 3 && bmih.biBitCount != 16 )
bhill42 1:bdeb188cb474 636 {
bhill42 1:bdeb188cb474 637 fclose(fp);
bhill42 1:bdeb188cb474 638 return false;
bhill42 1:bdeb188cb474 639 }
bhill42 1:bdeb188cb474 640
bhill42 1:bdeb188cb474 641 // set the bit depth
bhill42 1:bdeb188cb474 642
bhill42 1:bdeb188cb474 643 int TempBitDepth = (int) bmih.biBitCount;
bhill42 1:bdeb188cb474 644 if( TempBitDepth != 1 && TempBitDepth != 4
bhill42 1:bdeb188cb474 645 && TempBitDepth != 8 && TempBitDepth != 16
bhill42 1:bdeb188cb474 646 && TempBitDepth != 24 && TempBitDepth != 32 )
bhill42 1:bdeb188cb474 647 {
bhill42 1:bdeb188cb474 648 fclose(fp);
bhill42 1:bdeb188cb474 649 return false;
bhill42 1:bdeb188cb474 650 }
bhill42 1:bdeb188cb474 651 BitDepth = (int)bmih.biBitCount;
bhill42 1:bdeb188cb474 652 // set the size
bhill42 1:bdeb188cb474 653
bhill42 1:bdeb188cb474 654 if( (int) bmih.biWidth <= 0 || (int) bmih.biHeight <= 0 )
bhill42 1:bdeb188cb474 655 {
bhill42 1:bdeb188cb474 656 fclose(fp);
bhill42 1:bdeb188cb474 657 return false;
bhill42 1:bdeb188cb474 658 }
bhill42 1:bdeb188cb474 659 Width = (int) bmih.biWidth;
bhill42 1:bdeb188cb474 660 Height = (int) bmih.biHeight;
bhill42 1:bdeb188cb474 661 // some preliminaries
bhill42 1:bdeb188cb474 662
bhill42 1:bdeb188cb474 663 double dBytesPerPixel = ( (double) BitDepth ) / 8.0;
bhill42 1:bdeb188cb474 664 double dBytesPerRow = dBytesPerPixel * (Width+0.0);
bhill42 1:bdeb188cb474 665 dBytesPerRow = ceil(dBytesPerRow);
bhill42 1:bdeb188cb474 666
bhill42 1:bdeb188cb474 667 int BytePaddingPerRow = 4 - ( (int) (dBytesPerRow) )% 4;
bhill42 1:bdeb188cb474 668 if( BytePaddingPerRow == 4 )
bhill42 1:bdeb188cb474 669 { BytePaddingPerRow = 0; }
bhill42 1:bdeb188cb474 670
bhill42 1:bdeb188cb474 671 // if < 16 bits, read the palette
bhill42 1:bdeb188cb474 672
bhill42 1:bdeb188cb474 673 if( BitDepth < 16 )
bhill42 1:bdeb188cb474 674 {
bhill42 1:bdeb188cb474 675 // determine the number of colors specified in the
bhill42 1:bdeb188cb474 676 // color table
bhill42 1:bdeb188cb474 677
bhill42 1:bdeb188cb474 678 int NumberOfColorsToRead = ((int) bmfh.bfOffBits - 54 )/4;
bhill42 1:bdeb188cb474 679 if( NumberOfColorsToRead > (1 << BitDepth) )
bhill42 1:bdeb188cb474 680 { NumberOfColorsToRead = (1 << BitDepth); }
bhill42 1:bdeb188cb474 681
bhill42 1:bdeb188cb474 682 int n;
bhill42 1:bdeb188cb474 683 for( n=0; n < NumberOfColorsToRead ; n++ )
bhill42 1:bdeb188cb474 684 {
bhill42 1:bdeb188cb474 685 SafeFread( (char*) &(Colors[n]) , 4 , 1 , fp);
bhill42 1:bdeb188cb474 686 }
bhill42 1:bdeb188cb474 687 for( n=NumberOfColorsToRead ; n < TellNumberOfColors(BitDepth) ; n++ )
bhill42 1:bdeb188cb474 688 {
bhill42 1:bdeb188cb474 689 RGBApixel HWITE;
bhill42 1:bdeb188cb474 690 HWITE.Red = 255;
bhill42 1:bdeb188cb474 691 HWITE.Green = 255;
bhill42 1:bdeb188cb474 692 HWITE.Blue = 255;
bhill42 1:bdeb188cb474 693 HWITE.Alpha = 0;
bhill42 1:bdeb188cb474 694 Colors[n] = HWITE;
bhill42 1:bdeb188cb474 695 }
bhill42 1:bdeb188cb474 696 }
bhill42 1:bdeb188cb474 697
bhill42 1:bdeb188cb474 698 // skip blank data if bfOffBits so indicates
bhill42 1:bdeb188cb474 699
bhill42 1:bdeb188cb474 700 int BytesToSkip = bmfh.bfOffBits - 54;;
bhill42 1:bdeb188cb474 701 if( BitDepth < 16 )
bhill42 1:bdeb188cb474 702 { BytesToSkip -= 4*(1 << BitDepth); }
bhill42 1:bdeb188cb474 703 if( BitDepth == 16 && bmih.biCompression == 3 )
bhill42 1:bdeb188cb474 704 { BytesToSkip -= 3*4; }
bhill42 1:bdeb188cb474 705 if( BytesToSkip < 0 )
bhill42 1:bdeb188cb474 706 { BytesToSkip = 0; }
bhill42 1:bdeb188cb474 707 if( BytesToSkip > 0 && BitDepth != 16 )
bhill42 1:bdeb188cb474 708 {
bhill42 1:bdeb188cb474 709 ebmpBYTE* TempSkipBYTE;
bhill42 1:bdeb188cb474 710 TempSkipBYTE = new ebmpBYTE [BytesToSkip];
bhill42 1:bdeb188cb474 711 SafeFread( (char*) TempSkipBYTE , BytesToSkip , 1 , fp);
bhill42 1:bdeb188cb474 712 delete [] TempSkipBYTE;
bhill42 1:bdeb188cb474 713 }
bhill42 1:bdeb188cb474 714
bhill42 1:bdeb188cb474 715 // This code reads 1, 4, 8, 24, and 32-bpp files
bhill42 1:bdeb188cb474 716 // with a more-efficient buffered technique.
bhill42 1:bdeb188cb474 717
bhill42 1:bdeb188cb474 718 int i,j;
bhill42 1:bdeb188cb474 719 if( BitDepth != 16 )
bhill42 1:bdeb188cb474 720 {
bhill42 1:bdeb188cb474 721 int BufferSize = (int) ( (Width*BitDepth) / 8.0 );
bhill42 1:bdeb188cb474 722 while( 8*BufferSize < Width*BitDepth )
bhill42 1:bdeb188cb474 723 { BufferSize++; }
bhill42 1:bdeb188cb474 724 while( BufferSize % 4 )
bhill42 1:bdeb188cb474 725 { BufferSize++; }
bhill42 1:bdeb188cb474 726 ebmpBYTE* Buffer;
bhill42 1:bdeb188cb474 727 Buffer = new ebmpBYTE [BufferSize];
bhill42 1:bdeb188cb474 728 j= Height-1;
bhill42 1:bdeb188cb474 729 while( j > -1 )
bhill42 1:bdeb188cb474 730 {
bhill42 1:bdeb188cb474 731 int BytesRead = (int) fread( (char*) Buffer, 1, BufferSize, fp );
bhill42 1:bdeb188cb474 732 if( BytesRead < BufferSize )
bhill42 1:bdeb188cb474 733 {
bhill42 1:bdeb188cb474 734 j = -1;
bhill42 1:bdeb188cb474 735 }
bhill42 1:bdeb188cb474 736 else
bhill42 1:bdeb188cb474 737 {
bhill42 1:bdeb188cb474 738 bool Success = false;
bhill42 1:bdeb188cb474 739 if( BitDepth == 1 )
bhill42 1:bdeb188cb474 740 { Success = Read1bitRow( Buffer, BufferSize, j , lcd, x, y); }
bhill42 1:bdeb188cb474 741 if( BitDepth == 4 )
bhill42 1:bdeb188cb474 742 { Success = Read4bitRow( Buffer, BufferSize, j , lcd, x, y); }
bhill42 1:bdeb188cb474 743 if( BitDepth == 8 )
bhill42 1:bdeb188cb474 744 { Success = Read8bitRow( Buffer, BufferSize, j , lcd, x, y); }
bhill42 1:bdeb188cb474 745 if( BitDepth == 24 )
bhill42 1:bdeb188cb474 746 { Success = Read24bitRow( Buffer, BufferSize, j , lcd, x, y); }
bhill42 1:bdeb188cb474 747 if( BitDepth == 32 )
bhill42 1:bdeb188cb474 748 { Success = Read32bitRow( Buffer, BufferSize, j , lcd, x, y); }
bhill42 1:bdeb188cb474 749 if( !Success )
bhill42 1:bdeb188cb474 750 {
bhill42 1:bdeb188cb474 751 j = -1;
bhill42 1:bdeb188cb474 752 }
bhill42 1:bdeb188cb474 753 }
bhill42 1:bdeb188cb474 754 j--;
bhill42 1:bdeb188cb474 755 }
bhill42 1:bdeb188cb474 756 delete [] Buffer;
bhill42 1:bdeb188cb474 757 }
bhill42 1:bdeb188cb474 758
bhill42 1:bdeb188cb474 759 if( BitDepth == 16 )
bhill42 1:bdeb188cb474 760 {
bhill42 1:bdeb188cb474 761 int DataBytes = Width*2;
bhill42 1:bdeb188cb474 762 int PaddingBytes = ( 4 - DataBytes % 4 ) % 4;
bhill42 1:bdeb188cb474 763
bhill42 1:bdeb188cb474 764 // set the default mask
bhill42 1:bdeb188cb474 765
bhill42 1:bdeb188cb474 766 ebmpWORD BlueMask = 31; // bits 12-16
bhill42 1:bdeb188cb474 767 ebmpWORD GreenMask = 992; // bits 7-11
bhill42 1:bdeb188cb474 768 ebmpWORD RedMask = 31744; // bits 2-6
bhill42 1:bdeb188cb474 769
bhill42 1:bdeb188cb474 770 // read the bit fields, if necessary, to
bhill42 1:bdeb188cb474 771 // override the default 5-5-5 mask
bhill42 1:bdeb188cb474 772
bhill42 1:bdeb188cb474 773 if( bmih.biCompression != 0 )
bhill42 1:bdeb188cb474 774 {
bhill42 1:bdeb188cb474 775 // read the three bit masks
bhill42 1:bdeb188cb474 776
bhill42 1:bdeb188cb474 777 ebmpWORD TempMaskWORD;
bhill42 1:bdeb188cb474 778
bhill42 1:bdeb188cb474 779 SafeFread( (char*) &RedMask , 2 , 1 , fp );
bhill42 1:bdeb188cb474 780 SafeFread( (char*) &TempMaskWORD , 2, 1, fp );
bhill42 1:bdeb188cb474 781
bhill42 1:bdeb188cb474 782 SafeFread( (char*) &GreenMask , 2 , 1 , fp );
bhill42 1:bdeb188cb474 783 SafeFread( (char*) &TempMaskWORD , 2, 1, fp );
bhill42 1:bdeb188cb474 784
bhill42 1:bdeb188cb474 785 SafeFread( (char*) &BlueMask , 2 , 1 , fp );
bhill42 1:bdeb188cb474 786 SafeFread( (char*) &TempMaskWORD , 2, 1, fp );
bhill42 1:bdeb188cb474 787 }
bhill42 1:bdeb188cb474 788
bhill42 1:bdeb188cb474 789 // read and skip any meta data
bhill42 1:bdeb188cb474 790
bhill42 1:bdeb188cb474 791 if( BytesToSkip > 0 )
bhill42 1:bdeb188cb474 792 {
bhill42 1:bdeb188cb474 793 ebmpBYTE* TempSkipBYTE;
bhill42 1:bdeb188cb474 794 TempSkipBYTE = new ebmpBYTE [BytesToSkip];
bhill42 1:bdeb188cb474 795 SafeFread( (char*) TempSkipBYTE , BytesToSkip , 1 , fp);
bhill42 1:bdeb188cb474 796 delete [] TempSkipBYTE;
bhill42 1:bdeb188cb474 797 }
bhill42 1:bdeb188cb474 798
bhill42 1:bdeb188cb474 799 // determine the red, green and blue shifts
bhill42 1:bdeb188cb474 800
bhill42 1:bdeb188cb474 801 int GreenShift = 0;
bhill42 1:bdeb188cb474 802 ebmpWORD TempShiftWORD = GreenMask;
bhill42 1:bdeb188cb474 803 while( TempShiftWORD > 31 )
bhill42 1:bdeb188cb474 804 { TempShiftWORD = TempShiftWORD>>1; GreenShift++; }
bhill42 1:bdeb188cb474 805 int BlueShift = 0;
bhill42 1:bdeb188cb474 806 TempShiftWORD = BlueMask;
bhill42 1:bdeb188cb474 807 while( TempShiftWORD > 31 )
bhill42 1:bdeb188cb474 808 { TempShiftWORD = TempShiftWORD>>1; BlueShift++; }
bhill42 1:bdeb188cb474 809 int RedShift = 0;
bhill42 1:bdeb188cb474 810 TempShiftWORD = RedMask;
bhill42 1:bdeb188cb474 811 while( TempShiftWORD > 31 )
bhill42 1:bdeb188cb474 812 { TempShiftWORD = TempShiftWORD>>1; RedShift++; }
bhill42 1:bdeb188cb474 813
bhill42 1:bdeb188cb474 814 // read the actual pixels
bhill42 1:bdeb188cb474 815
bhill42 1:bdeb188cb474 816 for( j=Height-1 ; j >= 0 ; j-- )
bhill42 1:bdeb188cb474 817 {
bhill42 1:bdeb188cb474 818 i=0;
bhill42 1:bdeb188cb474 819 int ReadNumber = 0;
bhill42 1:bdeb188cb474 820 while( ReadNumber < DataBytes )
bhill42 1:bdeb188cb474 821 {
bhill42 1:bdeb188cb474 822 ebmpWORD TempWORD;
bhill42 1:bdeb188cb474 823 SafeFread( (char*) &TempWORD , 2 , 1 , fp );
bhill42 1:bdeb188cb474 824 ReadNumber += 2;
bhill42 1:bdeb188cb474 825
bhill42 1:bdeb188cb474 826 ebmpWORD Red = RedMask & TempWORD;
bhill42 1:bdeb188cb474 827 ebmpWORD Green = GreenMask & TempWORD;
bhill42 1:bdeb188cb474 828 ebmpWORD Blue = BlueMask & TempWORD;
bhill42 1:bdeb188cb474 829
bhill42 1:bdeb188cb474 830 ebmpBYTE BlueBYTE = (ebmpBYTE) 8*(Blue>>BlueShift);
bhill42 1:bdeb188cb474 831 ebmpBYTE GreenBYTE = (ebmpBYTE) 8*(Green>>GreenShift);
bhill42 1:bdeb188cb474 832 ebmpBYTE RedBYTE = (ebmpBYTE) 8*(Red>>RedShift);
bhill42 1:bdeb188cb474 833
bhill42 1:bdeb188cb474 834 int color = 0x00000000 | (RedBYTE << 16) | (GreenBYTE << 8) | (BlueBYTE);
bhill42 1:bdeb188cb474 835 (*lcd).pixel(x+i,y+j,color);
bhill42 1:bdeb188cb474 836 i++;
bhill42 1:bdeb188cb474 837 }
bhill42 1:bdeb188cb474 838 ReadNumber = 0;
bhill42 1:bdeb188cb474 839 while( ReadNumber < PaddingBytes )
bhill42 1:bdeb188cb474 840 {
bhill42 1:bdeb188cb474 841 ebmpBYTE TempBYTE;
bhill42 1:bdeb188cb474 842 SafeFread( (char*) &TempBYTE , 1, 1, fp);
bhill42 1:bdeb188cb474 843 ReadNumber++;
bhill42 1:bdeb188cb474 844 }
bhill42 1:bdeb188cb474 845 }
bhill42 1:bdeb188cb474 846
bhill42 1:bdeb188cb474 847 }
bhill42 1:bdeb188cb474 848
bhill42 1:bdeb188cb474 849 fclose(fp);
bhill42 1:bdeb188cb474 850 return true;
bhill42 1:bdeb188cb474 851 }
bhill42 1:bdeb188cb474 852
bhill42 1:bdeb188cb474 853
bhill42 1:bdeb188cb474 854 BMIH::BMIH()
bhill42 1:bdeb188cb474 855 {
bhill42 1:bdeb188cb474 856 biPlanes = 1;
bhill42 1:bdeb188cb474 857 biCompression = 0;
bhill42 1:bdeb188cb474 858 biXPelsPerMeter = DefaultXPelsPerMeter;
bhill42 1:bdeb188cb474 859 biYPelsPerMeter = DefaultYPelsPerMeter;
bhill42 1:bdeb188cb474 860 biClrUsed = 0;
bhill42 1:bdeb188cb474 861 biClrImportant = 0;
bhill42 1:bdeb188cb474 862 }
bhill42 1:bdeb188cb474 863
bhill42 1:bdeb188cb474 864 BMFH::BMFH()
bhill42 1:bdeb188cb474 865 {
bhill42 1:bdeb188cb474 866 bfType = 19778;
bhill42 1:bdeb188cb474 867 bfReserved1 = 0;
bhill42 1:bdeb188cb474 868 bfReserved2 = 0;
bhill42 1:bdeb188cb474 869 }