Demo/src/Bitmap.cpp

64 lines
1.5 KiB
C++
Raw Normal View History

2019-12-30 15:13:06 +01:00
#include "Bitmap.hpp"
2019-11-28 15:20:15 +01:00
#include <fstream>
#include <iostream>
2019-12-30 15:13:06 +01:00
Bitmap::Bitmap() {
2019-11-28 15:20:15 +01:00
offset = 0;
width = 0;
height = 0;
2019-12-30 15:13:06 +01:00
bitsPerPixel = 0;
2019-11-28 15:20:15 +01:00
}
2019-12-30 15:13:06 +01:00
Bitmap::Bitmap(const std::string &file) {
2019-11-28 15:20:15 +01:00
offset = 0;
width = 0;
height = 0;
2019-12-30 15:13:06 +01:00
bitsPerPixel = 0;
2019-11-28 15:20:15 +01:00
load(file);
}
2019-12-30 15:13:06 +01:00
void Bitmap::load(const std::string &file) {
2019-11-28 15:20:15 +01:00
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];
}
2019-12-30 15:13:06 +01:00
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]));
2019-11-28 15:20:15 +01:00
}
}
2019-12-30 15:13:06 +01:00
int Bitmap::getWidth() {
2019-11-28 15:20:15 +01:00
return width;
}
2019-12-30 15:13:06 +01:00
int Bitmap::getHeight() {
2019-11-28 15:20:15 +01:00
return height;
}
2019-12-30 15:13:06 +01:00
char *Bitmap::getData() {
2019-11-28 15:20:15 +01:00
return &data[offset];
}
2019-12-30 15:13:06 +01:00
int Bitmap::getBitsPerPixel() {
return bitsPerPixel;
2019-11-28 15:20:15 +01:00
}
2019-12-30 15:13:06 +01:00
unsigned int Bitmap::getPixel(int x, int y) {
2019-11-28 15:20:15 +01:00
unsigned int pixel = 0;
char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8);
for(int i = 0; i < getBitsPerPixel() / 8;i++){
2019-12-30 15:13:06 +01:00
*((char*)&pixel+i) = ptr[i];//NOLINT
2019-11-28 15:20:15 +01:00
}
if(pixel != 0){
return pixel;
}
2019-11-28 17:14:27 +01:00
2019-11-28 15:20:15 +01:00
return pixel;
}