2019-10-15 16:12:56 +02:00
|
|
|
#include "Bitmap.h"
|
|
|
|
#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);
|
2019-10-15 16:12:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::load(const std::string &file) {
|
2019-10-22 15:34:51 +02:00
|
|
|
std::ifstream stream;
|
|
|
|
stream.open(file);
|
2019-10-15 16:12:56 +02:00
|
|
|
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());
|
2019-10-15 16:12:56 +02:00
|
|
|
for(int i = 0; i < str.size();i++){
|
|
|
|
data[i] = str[i];
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:34:51 +02:00
|
|
|
offset = (int)*(unsigned int*)(&data[10]);
|
|
|
|
width = (int)*(unsigned int*)(&data[18]);
|
|
|
|
height = (int)*(unsigned int*)(&data[22]);
|
|
|
|
bpp = (int)*(unsigned short*)(&data[28]);
|
2019-10-15 16:12:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Bitmap::getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Bitmap::getHeight() {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *Bitmap::getData() {
|
2019-10-22 15:34:51 +02:00
|
|
|
return &data[offset];
|
2019-10-15 16:12:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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++){
|
|
|
|
*((char*)&pixel+i) = ptr[i];
|
|
|
|
}
|
2019-10-23 13:39:51 +02:00
|
|
|
if(pixel != 0){
|
|
|
|
return pixel;
|
|
|
|
}
|
2019-10-22 15:34:51 +02:00
|
|
|
return pixel;
|
2019-10-15 16:12:56 +02:00
|
|
|
}
|