Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Fri May 10 21:25:27 2019 +0000
Revision:
26:716bcd47f3ca
Parent:
21:f3b0ce18b44f
FINAL_SUBMISSION; ; Changes:; WDplayer: Major Memory Leek fixed related to fclose(); Game_Manager: Tuned Scene Order and Improved Random number generator.; SceneFuctions: Added a Randomly changing Object to Scene 4.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rottenegg 18:14e5391beccf 1 #include "Bitmap.h"
rottenegg 18:14e5391beccf 2
rottenegg 18:14e5391beccf 3 //Total New Process time = 0.04 Seconds (still a bit long)
rottenegg 18:14e5391beccf 4 //Image data is Stored in DWORDS (32-bits) not Bytes (8-bits)
rottenegg 18:14e5391beccf 5 Bitmap::Bitmap(PinName const scePin,
rottenegg 18:14e5391beccf 6 PinName const rstPin,
rottenegg 18:14e5391beccf 7 PinName const dcPin,
rottenegg 18:14e5391beccf 8 PinName const mosiPin,
rottenegg 18:14e5391beccf 9 PinName const sclkPin,
rottenegg 18:14e5391beccf 10 PinName const ledPin)
rottenegg 18:14e5391beccf 11 :
rottenegg 18:14e5391beccf 12 N5110(scePin, rstPin, dcPin, mosiPin, sclkPin, ledPin)
rottenegg 18:14e5391beccf 13 {};
rottenegg 18:14e5391beccf 14
rottenegg 18:14e5391beccf 15 void Bitmap::renderBMP(const char *path, unsigned int const x, unsigned int const y) {
rottenegg 18:14e5391beccf 16 FILE *bmp = fopen(path,"r");
rottenegg 21:f3b0ce18b44f 17 if (bmp == NULL) {
rottenegg 26:716bcd47f3ca 18 std::cerr << "failure " << path << std::endl;
rottenegg 21:f3b0ce18b44f 19 }
rottenegg 18:14e5391beccf 20 unsigned char *buffer = (unsigned char*)std::malloc(4 * sizeof(unsigned char));
rottenegg 21:f3b0ce18b44f 21 this->readDIB(bmp); //Reading DIB header
rottenegg 18:14e5391beccf 22 unsigned short int dcount = (width / 32) + 1; //Counting DWORDS required per Row of Image
rottenegg 18:14e5391beccf 23 fseek(bmp,offbits,SEEK_SET);
rottenegg 18:14e5391beccf 24 std::bitset<32> bits;
rottenegg 18:14e5391beccf 25 offbits = 0;
rottenegg 18:14e5391beccf 26 row = 0;
rottenegg 18:14e5391beccf 27 colomn = 0;
rottenegg 18:14e5391beccf 28 for (unsigned int dwcount = 1; dwcount < ((dcount*height) + 1); dwcount++) {
rottenegg 18:14e5391beccf 29 this->swapEndian(bmp, buffer, bits); //Correcing Machine Endiness
rottenegg 18:14e5391beccf 30 if (dwcount % dcount == 0) {
rottenegg 18:14e5391beccf 31 this->halfLoop(x,y,bits);
rottenegg 18:14e5391beccf 32 } else {
rottenegg 18:14e5391beccf 33 this->fullLoop(x,y,bits);
rottenegg 18:14e5391beccf 34 }
rottenegg 18:14e5391beccf 35 }
rottenegg 18:14e5391beccf 36 std::fclose(bmp);
rottenegg 21:f3b0ce18b44f 37 free(buffer);
rottenegg 18:14e5391beccf 38 }
rottenegg 18:14e5391beccf 39
rottenegg 18:14e5391beccf 40 void Bitmap::readDIB(FILE *bmp) {
rottenegg 18:14e5391beccf 41 std::fseek(bmp,10,SEEK_SET);
rottenegg 21:f3b0ce18b44f 42 std::fread(&offbits,4,1,bmp); //Bytes to skip to get to data block
rottenegg 18:14e5391beccf 43 std::fseek(bmp,18,SEEK_SET);
rottenegg 21:f3b0ce18b44f 44 std::fread(&width,4,1,bmp); //Width of Image
rottenegg 18:14e5391beccf 45 std::fseek(bmp,22,SEEK_SET);
rottenegg 21:f3b0ce18b44f 46 std::fread(&height,4,1,bmp); //Height of image
rottenegg 18:14e5391beccf 47 }
rottenegg 18:14e5391beccf 48
rottenegg 18:14e5391beccf 49 void Bitmap::swapEndian(FILE *bmp, unsigned char *buffer, std::bitset<32> &bits) {
rottenegg 26:716bcd47f3ca 50 std::fread(buffer,1,4,bmp);
rottenegg 18:14e5391beccf 51 //Endian Swap
rottenegg 18:14e5391beccf 52 bits = buffer[0];
rottenegg 18:14e5391beccf 53 bits = (bits << 8) | (std::bitset<32>)buffer[1];
rottenegg 18:14e5391beccf 54 bits = (bits << 8) | (std::bitset<32>)buffer[2];
rottenegg 18:14e5391beccf 55 bits = (bits << 8) | (std::bitset<32>)buffer[3];
rottenegg 18:14e5391beccf 56 }
rottenegg 18:14e5391beccf 57
rottenegg 18:14e5391beccf 58 void Bitmap::halfLoop(unsigned const int x, unsigned const int y, std::bitset<32> bits) {
rottenegg 18:14e5391beccf 59 //First Loop only reads remaining bits in a DWORD and skips the Packing bits
rottenegg 18:14e5391beccf 60 for (offbits = 0; offbits < (width % 32); offbits++){
rottenegg 18:14e5391beccf 61 //Bit Loop to print out pixel data but skips the packing bits
rottenegg 18:14e5391beccf 62 this->renderDWORD(bits,x,y);
rottenegg 18:14e5391beccf 63 }
rottenegg 18:14e5391beccf 64 row++;
rottenegg 18:14e5391beccf 65 colomn = 0;
rottenegg 18:14e5391beccf 66 }
rottenegg 18:14e5391beccf 67
rottenegg 18:14e5391beccf 68 void Bitmap::fullLoop(unsigned const int x, unsigned const int y, std::bitset<32> bits) {
rottenegg 18:14e5391beccf 69 //Second Loop reads the full DWORD
rottenegg 18:14e5391beccf 70 for (offbits = 0; offbits < 32; offbits++) {
rottenegg 18:14e5391beccf 71 this->renderDWORD(bits,x,y);
rottenegg 18:14e5391beccf 72 }
rottenegg 21:f3b0ce18b44f 73 }
rottenegg 21:f3b0ce18b44f 74
rottenegg 21:f3b0ce18b44f 75 //Aiding Funtion for fullloop and halfloop
rottenegg 21:f3b0ce18b44f 76 void Bitmap::renderDWORD(std::bitset<32> bits, unsigned int const x, unsigned int const y) {
rottenegg 21:f3b0ce18b44f 77 bool state;
rottenegg 21:f3b0ce18b44f 78 if (bits[31 - offbits] == 1) {
rottenegg 21:f3b0ce18b44f 79 state = false;
rottenegg 21:f3b0ce18b44f 80 } else {
rottenegg 21:f3b0ce18b44f 81 state = true;
rottenegg 21:f3b0ce18b44f 82 }
rottenegg 21:f3b0ce18b44f 83 this->setPixel((colomn + x) ,(row + y),state);
rottenegg 21:f3b0ce18b44f 84 colomn++;
rottenegg 18:14e5391beccf 85 }