Dependencies:   mbed FATFileSystem

Revision:
16:279b870447be
Parent:
3:63cdd5cab431
--- a/N5110_Modded/Bitmap.cpp	Tue May 07 03:20:24 2019 +0000
+++ b/N5110_Modded/Bitmap.cpp	Tue May 07 21:26:26 2019 +0000
@@ -2,68 +2,83 @@
 
 //Total New Process time = 0.04 Seconds (still a bit long)
 //Image data is Stored in DWORDS (32-bits) not Bytes (8-bits)
+Bitmap::Bitmap(PinName const scePin,
+               PinName const rstPin,
+               PinName const dcPin,
+               PinName const mosiPin,
+               PinName const sclkPin,
+               PinName const ledPin)
+        :
+        N5110(scePin, rstPin, dcPin, mosiPin, sclkPin, ledPin) {
+    row = 0; 
+    colomn = 0;
+};
 
-void Bitmap::renderBMP(const char *path, N5110 &lcd, unsigned int const x, unsigned int const y) {
+void Bitmap::renderBMP(const char *path, unsigned int const x, unsigned int const y) {
     //Opening File and Checking
     FILE *bmp = fopen(path,"r");
-    if (bmp == NULL) {
-        std::cerr << "File Not Found" << std::endl;
-    }
     //Creating Buffer and Geting data from Memory locations for DIB Header data block
     unsigned char *buffer = (unsigned char*)std::malloc(4 * sizeof(unsigned char));
-    unsigned long offbits;
-    unsigned long height;
-    unsigned long width;
+    this->readDIB(bmp);
+    unsigned short int dcount = (width / 32) + 1;   //Counting DWORDS required per Row of Image
+    fseek(bmp,offbits,SEEK_SET);
+    std::bitset<32> bits;
+    offbits = 0;
+    //Looping relative to DWORDS to remove packing bits
+    for (unsigned int dwcount = 1; dwcount < ((dcount*height) + 1); dwcount++) {
+        this->swapEndian(bmp, buffer, bits);
+        //Uses two selective For loops 
+        if (dwcount % dcount == 0) {
+            this->halfLoop(x,y,bits);
+        } else {
+            this->fullLoop(x,y,bits);
+        }
+    }
+    std::fclose(bmp);
+}
+
+void Bitmap::readDIB(FILE *bmp) {
     std::fseek(bmp,10,SEEK_SET);
     std::fread(&offbits,4,1,bmp);
     std::fseek(bmp,18,SEEK_SET);
     std::fread(&width,4,1,bmp);
     std::fseek(bmp,22,SEEK_SET);
     std::fread(&height,4,1,bmp);
-    //Counting DWORDS required per Row of Image
-    unsigned short int dcount = (width / 32) + 1;
-    fseek(bmp,offbits,SEEK_SET);
-    std::bitset<32> bits;
-    int row = 0;
-    offbits = 0;
-    int colomn = 0;
+}
+
+void Bitmap::renderDWORD(std::bitset<32> bits, unsigned int const x, unsigned int const y) {
     bool state;
-    //NEW ENGINE CORE
-    //Looping relative to DWORDS to remove packing bits
-    for (unsigned int dwcount = 1; dwcount < ((dcount*height) + 1); dwcount++) {
-        std::fread(buffer,1,4,bmp);
+    if (bits[31 - offbits] == 1) {
+        state = false;
+    } else {
+        state = true;
+    }
+    this->setPixel((colomn + x) ,(row + y),state);
+    colomn++;
+}
+
+void Bitmap::swapEndian(FILE *bmp, unsigned char *buffer, std::bitset<32> &bits) {
+    std::fread(buffer,1,4,bmp);
         //Endian Swap
         bits = buffer[0];
         bits = (bits << 8) | (std::bitset<32>)buffer[1];
         bits = (bits << 8) | (std::bitset<32>)buffer[2];
         bits = (bits << 8) | (std::bitset<32>)buffer[3];
+}
+
+void Bitmap::halfLoop(unsigned const int x, unsigned const int y, std::bitset<32> bits) {
+    //First Loop only reads remaining bits in a DWORD and skips the Packing bits
+    for (offbits = 0; offbits < (width % 32); offbits++){
         //Bit Loop to print out pixel data but skips the packing bits
-        //Uses two selective For loops 
-        //First Loop only reads remaining bits in a DWORD and skips the Packing bits
-        //Second Loop reads the full DWORD
-        if (dwcount % dcount == 0) {
-            for (offbits = 0; offbits < (width % 32); offbits++){
-                if (bits[31 - offbits] == 1) {
-                    state = false;
-                } else {
-                    state = true;
-                }
-                lcd.setPixel((colomn + x) ,(row + y),state);
-                colomn++;
-            }
-            row++;
-            colomn = 0;
-        } else {
-            for (offbits = 0; offbits < 32; offbits++) {
-                if (bits[31 - offbits] == 1) {
-                    state = false;
-                } else {
-                    state = true;
-                }
-                lcd.setPixel((colomn + x) ,(row + y),state);
-                colomn++;
-            }
-        }
+        this->renderDWORD(bits,x,y);
     }
-    std::fclose(bmp);
+    row++;
+    colomn = 0;
 }
+
+void Bitmap::fullLoop(unsigned const int x, unsigned const int y, std::bitset<32> bits) {
+    //Second Loop reads the full DWORD
+    for (offbits = 0; offbits < 32; offbits++) {
+        this->renderDWORD(bits,x,y);
+    }
+}
\ No newline at end of file