Text-Renderer/src/Bitmap.cpp

69 lines
1.5 KiB
C++
Raw Permalink Normal View History

2020-01-07 13:23:25 +01:00
#include "Bitmap.hpp"
#include <fstream>
#include <iostream>
Bitmap::Bitmap() {
offset = 0;
width = 0;
height = 0;
bpp = 0;
}
2019-10-22 15:34:51 +02:00
Bitmap::Bitmap(const std::string &file) {
offset = 0;
width = 0;
height = 0;
bpp = 0;
load(file);
}
void Bitmap::load(const std::string &file) {
2019-10-22 15:34:51 +02:00
std::ifstream stream;
stream.open(file);
if(stream.is_open()){
std::string str((std::istreambuf_iterator<char>(stream)),std::istreambuf_iterator<char>());
2019-10-22 15:34:51 +02:00
data.resize(str.size());
for(int i = 0; i < str.size();i++){
data[i] = str[i];
}
2019-12-17 12:06:44 +01:00
offset = *reinterpret_cast<int*>(&data[10]);
width = *reinterpret_cast<int*>(&data[18]);
height = *reinterpret_cast<int*>(&data[22]);
bpp = *reinterpret_cast<uint16_t *>(&data[28]);
}
}
int Bitmap::getWidth() {
return width;
}
int Bitmap::getHeight() {
return height;
}
char *Bitmap::getData() {
2019-10-22 15:34:51 +02:00
return &data[offset];
}
int Bitmap::getBitsPerPixel() {
return bpp;
}
2019-10-22 15:34:51 +02:00
unsigned int Bitmap::getPixel(int x, int y) {
unsigned int pixel = 0;
2020-01-07 15:05:32 +01:00
if(bpp == 1){
int index = (getHeight() - 1 - y) * getWidth() + x;
char *ptr = getData() + (index / 8);
bool value = (ptr[0] >> (7 - (index % 8))) & 1u;
return (int)value;
}
2019-10-29 13:24:09 +01:00
char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8);
2019-10-22 15:34:51 +02:00
for(int i = 0; i < getBitsPerPixel() / 8;i++){
2020-01-07 13:23:25 +01:00
auto value = reinterpret_cast<char*>(&pixel)+i;
2019-12-17 12:06:44 +01:00
*value = ptr[i];
2019-10-22 15:34:51 +02:00
}
return pixel;
}