+Bitmap class

+Bitmap file loading
+string output using font
This commit is contained in:
Julian Hinxlage 2019-10-15 16:12:56 +02:00
parent 8b42c13bc6
commit d84e36d50b
4 changed files with 173 additions and 3 deletions

View File

@ -9,7 +9,7 @@ include_directories(src)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library/build") set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library/build")
include_directories(${LIB_PATH}/include) include_directories(${LIB_PATH}/include)
add_executable(TextRenderer ${SOURCES} ${HEADERS}) add_executable(TextRenderer ${SOURCES} ${HEADERS} src/Bitmap.cpp src/Bitmap.h)
target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a) target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a)

54
src/Bitmap.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "Bitmap.h"
#include <fstream>
#include <iostream>
Bitmap::Bitmap() {
offset = 0;
width = 0;
height = 0;
bpp = 0;
data = nullptr;
}
Bitmap::~Bitmap() {
if(data){
delete data;
data = nullptr;
}
}
void Bitmap::load(const std::string &file) {
std::ifstream stream(file);
if(stream.is_open()){
std::string str((std::istreambuf_iterator<char>(stream)),std::istreambuf_iterator<char>());
data = new char[str.size()];
for(int i = 0; i < str.size();i++){
data[i] = str[i];
}
offset = *(unsigned int*)(data + 10);
width = *(unsigned int*)(data + 18);
height = *(unsigned int*)(data + 22);
bpp = *(unsigned short*)(data + 28);
}
}
int Bitmap::getWidth() {
return width;
}
int Bitmap::getHeight() {
return height;
}
char *Bitmap::getData() {
return data + offset;
}
int Bitmap::getBitsPerPixel() {
return bpp;
}
char *Bitmap::getPixel(int x, int y) {
return getData() + ((getHeight() - y) * getWidth() + x) * getBitsPerPixel() / 8;
}

68
src/Bitmap.h Normal file
View File

@ -0,0 +1,68 @@
#ifndef TEXTRENDERER_BITMAP_H
#define TEXTRENDERER_BITMAP_H
#include <string>
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @brief: Used to load a Bitmap from a file.
*/
class Bitmap {
public:
Bitmap();
~Bitmap();
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @brief: Used to load a Bitmap from a file.
*/
void load(const std::string &file);
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @return: the width of the image.
*/
int getWidth();
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @return: the height of the image
*/
int getHeight();
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @return: the pixel data as an byte array
*/
char *getData();
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @return: the number of bits uses per pixel
*/
int getBitsPerPixel();
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @return: the pixel value by coordinates, (0,0) is on the top left
*/
char *getPixel(int x, int y);
private:
int width;
int height;
int offset;
int bpp;
char *data;
};
#endif //TEXTRENDERER_BITMAP_H

View File

@ -1,9 +1,57 @@
#include <iostream> #include <iostream>
#include "add.h" #include "add.h"
#include "Bitmap.h"
/*
* @author: Julian Hinxlage
* @since: v0.0.0
* @brief: An image is loaded and used as a font.
* A string is converted and displayed in the console.
* Currently only to test.
*/
int main() { int main() {
std::cout << "Hello, World!" << std::endl; Bitmap bitmap;
std::cout << add(6,7) << std::endl; bitmap.load("../res/font.bmp");
std::string str = "Hello, World!";
//values used to calculate the coordinates of the characters
int xOffset = 1;
int yOffset = 2;
int xSize = 6;
int ySize = 7;
int xCount = 18;
int xStart = 0;
int yStart = 2;
//print vertical
//i is the current row
for (int i = 0; i < ySize; i++) {
//loop through the characters in the string
for (char c : str) {
//index of character(x and y)
int index = (c - ' ');
int xIndex = index % xCount;
int yIndex = index / xCount;
//character index to pixel index conversion
int x = xIndex * (xSize + xOffset) + xStart;
int y = yIndex * (ySize + yOffset) + yStart;
//print current row of the current character
for (int j = x; j < x + xSize; j++) {
auto *pixel = (unsigned int *) bitmap.getPixel(j, i + y);
if (*pixel == 0) {
std::cout << " ";
} else {
std::cout << "1";
}
}
std::cout << " ";
}
std::cout << std::endl;
}
return 0; return 0;
} }