Demo/src/Bitmap.cpp

64 lines
1.5 KiB
C++

#include "Bitmap.hpp"
#include <fstream>
#include <iostream>
Bitmap::Bitmap() {
offset = 0;
width = 0;
height = 0;
bitsPerPixel = 0;
}
Bitmap::Bitmap(const std::string &file) {
offset = 0;
width = 0;
height = 0;
bitsPerPixel = 0;
load(file);
}
void Bitmap::load(const std::string &file) {
std::ifstream stream;
stream.open(file);
if(stream.is_open()){
std::string str((std::istreambuf_iterator<char>(stream)),std::istreambuf_iterator<char>());
data.resize(str.size());
for(int i = 0; i < str.size();i++){
data[i] = str[i];
}
offset = static_cast<int>(*reinterpret_cast<unsigned int *>(&data[10]));
width = static_cast<int>(*reinterpret_cast<unsigned int *>(&data[18]));
height = static_cast<int>(*reinterpret_cast<unsigned int *>(&data[22]));
bitsPerPixel = static_cast<int>(*reinterpret_cast<uint16_t *>(&data[28]));
}
}
int Bitmap::getWidth() {
return width;
}
int Bitmap::getHeight() {
return height;
}
char *Bitmap::getData() {
return &data[offset];
}
int Bitmap::getBitsPerPixel() {
return bitsPerPixel;
}
unsigned int Bitmap::getPixel(int x, int y) {
unsigned int pixel = 0;
char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8);
for(int i = 0; i < getBitsPerPixel() / 8;i++){
*((char*)&pixel+i) = ptr[i];//NOLINT
}
if(pixel != 0){
return pixel;
}
return pixel;
}