ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

N5110_Modded/Bitmap.cpp

Committer:
rottenegg
Date:
2019-04-18
Revision:
2:964e19f2f3f1
Parent:
1:0efd25071a6d
Child:
3:63cdd5cab431

File content as of revision 2:964e19f2f3f1:

#include "Bitmap.h"

//Total New Process time = 0.04 Seconds (still a bit long)
//Image data is Stored in DWORDS (32-bits) not Bytes (8-bits)

void Bitmap::renderBMP(const char *path, N5110 &lcd, 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 RIFF data block
    unsigned char *buffer = (unsigned char*)std::malloc(4 * sizeof(unsigned char));
    unsigned long offbits;
    unsigned long height;
    unsigned long width;
    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;
    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);
        //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];
        //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++;
            }
        }
    }
    std::fclose(bmp);
}