Implement BitmapEngine to RenderD7
This commit is contained in:
parent
86166f87eb
commit
25ced38cba
615
bmp.hpp
Normal file
615
bmp.hpp
Normal file
@ -0,0 +1,615 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
|
||||||
|
|
||||||
|
struct BMPFileHeader {
|
||||||
|
uint16_t file_type{ 0x4D42 }; // File type always BM which is 0x4D42 (stored as hex uint16_t in little endian)
|
||||||
|
uint32_t file_size{ 0 }; // Size of the file (in bytes)
|
||||||
|
uint16_t reserved1{ 0 }; // Reserved, always 0
|
||||||
|
uint16_t reserved2{ 0 }; // Reserved, always 0
|
||||||
|
uint32_t offset_data{ 0 }; // Start position of pixel data (bytes from the beginning of the file)
|
||||||
|
};
|
||||||
|
|
||||||
|
struct BMPInfoHeader {
|
||||||
|
uint32_t size{ 0 }; // Size of this header (in bytes)
|
||||||
|
int32_t width{ 0 }; // width of bitmap in pixels
|
||||||
|
int32_t height{ 0 }; // height of bitmap in pixels
|
||||||
|
// (if positive, bottom-up, with origin in lower left corner)
|
||||||
|
// (if negative, top-down, with origin in upper left corner)
|
||||||
|
uint16_t planes{ 1 }; // No. of planes for the target device, this is always 1
|
||||||
|
uint16_t bit_count{ 0 }; // No. of bits per pixel
|
||||||
|
uint32_t compression{ 0 }; // 0 or 3 - uncompressed. THIS PROGRAM CONSIDERS ONLY UNCOMPRESSED BMP images
|
||||||
|
uint32_t size_image{ 0 }; // 0 - for uncompressed images
|
||||||
|
int32_t x_pixels_per_meter{ 0 };
|
||||||
|
int32_t y_pixels_per_meter{ 0 };
|
||||||
|
uint32_t colors_used{ 0 }; // No. color indexes in the color table. Use 0 for the max number of colors allowed by bit_count
|
||||||
|
uint32_t colors_important{ 0 }; // No. of colors used for displaying the bitmap. If 0 all colors are required
|
||||||
|
};
|
||||||
|
|
||||||
|
struct BMPColorHeader {
|
||||||
|
uint32_t red_mask{ 0x00ff0000 }; // Bit mask for the red channel
|
||||||
|
uint32_t green_mask{ 0x0000ff00 }; // Bit mask for the green channel
|
||||||
|
uint32_t blue_mask{ 0x000000ff }; // Bit mask for the blue channel
|
||||||
|
uint32_t alpha_mask{ 0xff000000 }; // Bit mask for the alpha channel
|
||||||
|
uint32_t color_space_type{ 0x73524742 }; // Default "sRGB" (0x73524742)
|
||||||
|
uint32_t unused[16]{ 0 }; // Unused data for sRGB color space
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
struct BMP {
|
||||||
|
BMPFileHeader file_header;
|
||||||
|
BMPInfoHeader bmp_info_header;
|
||||||
|
BMPColorHeader bmp_color_header;
|
||||||
|
std::vector<uint8_t> data;
|
||||||
|
|
||||||
|
|
||||||
|
BMP(const char *fname) {
|
||||||
|
read(fname);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(const char *fname) {
|
||||||
|
std::ifstream inp{ fname, std::ios_base::binary };
|
||||||
|
if (inp) {
|
||||||
|
inp.read((char*)&file_header, sizeof(file_header));
|
||||||
|
if(file_header.file_type != 0x4D42) {
|
||||||
|
|
||||||
|
return;//throw std::runtime_error("Error! Unrecognized file format.");
|
||||||
|
}
|
||||||
|
inp.read((char*)&bmp_info_header, sizeof(bmp_info_header));
|
||||||
|
|
||||||
|
// The BMPColorHeader is used only for transparent images
|
||||||
|
if(bmp_info_header.bit_count == 32) {
|
||||||
|
// Check if the file has bit mask color information
|
||||||
|
if(bmp_info_header.size >= (sizeof(BMPInfoHeader) + sizeof(BMPColorHeader))) {
|
||||||
|
inp.read((char*)&bmp_color_header, sizeof(bmp_color_header));
|
||||||
|
// Check if the pixel data is stored as BGRA and if the color space type is sRGB
|
||||||
|
check_color_header(bmp_color_header);
|
||||||
|
} else {
|
||||||
|
//std::cerr << "Error! The file \"" << fname << "\" does not seem to contain bit mask information\n";
|
||||||
|
return;//throw std::runtime_error("Error! Unrecognized file format.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jump to the pixel data location
|
||||||
|
inp.seekg(file_header.offset_data, inp.beg);
|
||||||
|
|
||||||
|
// Adjust the header fields for output.
|
||||||
|
// Some editors will put extra info in the image file, we only save the headers and the data.
|
||||||
|
if(bmp_info_header.bit_count == 32) {
|
||||||
|
bmp_info_header.size = sizeof(BMPInfoHeader) + sizeof(BMPColorHeader);
|
||||||
|
file_header.offset_data = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + sizeof(BMPColorHeader);
|
||||||
|
} else {
|
||||||
|
bmp_info_header.size = sizeof(BMPInfoHeader);
|
||||||
|
file_header.offset_data = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
|
||||||
|
}
|
||||||
|
file_header.file_size = file_header.offset_data;
|
||||||
|
|
||||||
|
if (bmp_info_header.height < 0) {
|
||||||
|
return;//throw std::runtime_error("The program can treat only BMP images with the origin in the bottom left corner!");
|
||||||
|
}
|
||||||
|
|
||||||
|
data.resize(bmp_info_header.width * bmp_info_header.height * bmp_info_header.bit_count / 8);
|
||||||
|
|
||||||
|
// Here we check if we need to take into account row padding
|
||||||
|
if (bmp_info_header.width % 4 == 0) {
|
||||||
|
inp.read((char*)data.data(), data.size());
|
||||||
|
file_header.file_size += static_cast<uint32_t>(data.size());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
row_stride = bmp_info_header.width * bmp_info_header.bit_count / 8;
|
||||||
|
uint32_t new_stride = make_stride_aligned(4);
|
||||||
|
std::vector<uint8_t> padding_row(new_stride - row_stride);
|
||||||
|
|
||||||
|
for (int y = 0; y < bmp_info_header.height; ++y) {
|
||||||
|
inp.read((char*)(data.data() + row_stride * y), row_stride);
|
||||||
|
inp.read((char*)padding_row.data(), padding_row.size());
|
||||||
|
}
|
||||||
|
file_header.file_size += static_cast<uint32_t>(data.size()) + bmp_info_header.height * static_cast<uint32_t>(padding_row.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return;//throw std::runtime_error("Unable to open the input image file "+std::string(fname));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BMP(int32_t width, int32_t height, bool has_alpha = true) {
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
return;//throw std::runtime_error("The image width and height must be positive numbers.");
|
||||||
|
}
|
||||||
|
|
||||||
|
bmp_info_header.width = width;
|
||||||
|
bmp_info_header.height = height;
|
||||||
|
if (has_alpha) {
|
||||||
|
bmp_info_header.size = sizeof(BMPInfoHeader) + sizeof(BMPColorHeader);
|
||||||
|
file_header.offset_data = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + sizeof(BMPColorHeader);
|
||||||
|
|
||||||
|
bmp_info_header.bit_count = 32;
|
||||||
|
bmp_info_header.compression = 3;
|
||||||
|
row_stride = width * 4;
|
||||||
|
data.resize(row_stride * height);
|
||||||
|
file_header.file_size = file_header.offset_data + data.size();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bmp_info_header.size = sizeof(BMPInfoHeader);
|
||||||
|
file_header.offset_data = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
|
||||||
|
|
||||||
|
bmp_info_header.bit_count = 24;
|
||||||
|
bmp_info_header.compression = 0;
|
||||||
|
row_stride = width * 3;
|
||||||
|
data.resize(row_stride * height);
|
||||||
|
|
||||||
|
uint32_t new_stride = make_stride_aligned(4);
|
||||||
|
file_header.file_size = file_header.offset_data + static_cast<uint32_t>(data.size()) + bmp_info_header.height * (new_stride - row_stride);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(const char *fname) {
|
||||||
|
std::ofstream of{ fname, std::ios_base::binary };
|
||||||
|
if (of) {
|
||||||
|
if (bmp_info_header.bit_count == 32) {
|
||||||
|
write_headers_and_data(of);
|
||||||
|
}
|
||||||
|
else if (bmp_info_header.bit_count == 24) {
|
||||||
|
if (bmp_info_header.width % 4 == 0) {
|
||||||
|
write_headers_and_data(of);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
uint32_t new_stride = make_stride_aligned(4);
|
||||||
|
std::vector<uint8_t> padding_row(new_stride - row_stride);
|
||||||
|
|
||||||
|
write_headers(of);
|
||||||
|
|
||||||
|
for (int y = 0; y < bmp_info_header.height; ++y) {
|
||||||
|
of.write((const char*)(data.data() + row_stride * y), row_stride);
|
||||||
|
of.write((const char*)padding_row.data(), padding_row.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return;//throw std::runtime_error("The program can treat only 24 or 32 bits per pixel BMP files");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return;//throw std::runtime_error("Unable to open the output image file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned char> DATA() {
|
||||||
|
std::stringstream ss;
|
||||||
|
if (ss) {
|
||||||
|
if (bmp_info_header.bit_count == 32) {
|
||||||
|
write_headers_and_datass(ss);
|
||||||
|
}
|
||||||
|
else if (bmp_info_header.bit_count == 24) {
|
||||||
|
if (bmp_info_header.width % 4 == 0) {
|
||||||
|
write_headers_and_datass(ss);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
uint32_t new_stride = make_stride_aligned(4);
|
||||||
|
std::vector<uint8_t> padding_row(new_stride - row_stride);
|
||||||
|
|
||||||
|
write_headersss(ss);
|
||||||
|
|
||||||
|
for (int y = 0; y < bmp_info_header.height; ++y) {
|
||||||
|
ss.write((const char*)(data.data() + row_stride * y), row_stride);
|
||||||
|
ss.write((const char*)padding_row.data(), padding_row.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
}
|
||||||
|
std::string test11 = ss.str();
|
||||||
|
std::vector<unsigned char> test12(test11.begin(), test11.end());
|
||||||
|
return test12;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void fill_region(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint8_t B, uint8_t G, uint8_t R, uint8_t A) {
|
||||||
|
if (x0 + w > (uint32_t)bmp_info_header.width || y0 + h > (uint32_t)bmp_info_header.height) {
|
||||||
|
return;//throw std::runtime_error("The region does not fit in the image!");
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = B;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = G;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = R;
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fill_region_df(uint32_t x1, uint32_t y1, uint32_t w, uint32_t h, uint8_t B, uint8_t G, uint8_t R, uint8_t A) {
|
||||||
|
|
||||||
|
int x0 = x1;
|
||||||
|
int y0 = this->bmp_info_header.height - y1 - h;
|
||||||
|
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
if (!(x + w > (uint32_t)bmp_info_header.width) || !(y + h > (uint32_t)-1)) {
|
||||||
|
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = B;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = G;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = R;
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void manipulate_region(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint8_t A) {
|
||||||
|
int choice, choice2, intensity;
|
||||||
|
cout << "What color do you want to change? " << endl;
|
||||||
|
cout << "Enter 1 for Blue, 2 for Green, 3 for Red " << endl;
|
||||||
|
cin >> choice;
|
||||||
|
cout << "To what color do you want to change it too?" << endl;
|
||||||
|
cout << "Enter 1 for Blue, 2 for Green, 3 for Red " << endl;
|
||||||
|
cin >> choice2;
|
||||||
|
cout << "Enter the intensity of the color. (From 0 to 255) " << endl;
|
||||||
|
cin >> intensity;
|
||||||
|
if (x0 + w > (uint32_t)bmp_info_header.width || y0 + h > (uint32_t)bmp_info_header.height) {
|
||||||
|
return;//throw std::runtime_error("The region does not fit in the image!");
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
if (choice==1 && choice2==1)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make blue thing blue
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 0]>80 && data[channels * (y * bmp_info_header.width + x) + 0]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = intensity;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = 0;
|
||||||
|
}
|
||||||
|
//data[channels * (y * bmp_info_header.width + x) + 0] = B;
|
||||||
|
//data[channels * (y * bmp_info_header.width + x) + 1] = G;
|
||||||
|
//data[channels * (y * bmp_info_header.width + x) + 2] = R;
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 1 && choice2==2)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make blue thing green
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 0]>80 && data[channels * (y * bmp_info_header.width + x) + 0]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = intensity;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = 0;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 1 && choice2==3)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make blue thing red
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 0]>80 && data[channels * (y * bmp_info_header.width + x) + 0]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = intensity;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 2 && choice2==1)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make green thing blue
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 1]>80 && data[channels * (y * bmp_info_header.width + x) + 1]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = intensity;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = 0;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 2 && choice2==2)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make green thing green
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 1]>80 && data[channels * (y * bmp_info_header.width + x) + 1]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = intensity;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = 0;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 2 && choice2==3)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make green thing red
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 1]>80 && data[channels * (y * bmp_info_header.width + x) + 1]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = intensity;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 3 && choice2==1)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make red thing blue
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 2]>80 && data[channels * (y * bmp_info_header.width + x) + 2]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = intensity;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = 0;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 3 && choice2==2)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make red thing green
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 2]>80 && data[channels * (y * bmp_info_header.width + x) + 2]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = intensity;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = 0;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (choice == 3 && choice2==3)
|
||||||
|
{
|
||||||
|
for (uint32_t y = y0; y < y0 + h; ++y) {
|
||||||
|
for (uint32_t x = x0; x < x0 + w; ++x) {
|
||||||
|
cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Make red thing blue
|
||||||
|
if(data[channels * (y * bmp_info_header.width + x) + 2]>80 && data[channels * (y * bmp_info_header.width + x) + 2]<255)
|
||||||
|
{
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 0] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 1] = 0;
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 2] = intensity;
|
||||||
|
}
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y * bmp_info_header.width + x) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int OrganizeAverageRed()
|
||||||
|
{
|
||||||
|
int ColorRed[bmp_info_header.height][bmp_info_header.width];
|
||||||
|
int ColorGreen[bmp_info_header.height][bmp_info_header.width];;
|
||||||
|
int ColorBlue[bmp_info_header.height][bmp_info_header.width];
|
||||||
|
float pixels=bmp_info_header.height*bmp_info_header.width;
|
||||||
|
float intensity=0;
|
||||||
|
float sum=0;
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
cout << "The Width of the image is " << bmp_info_header.width << endl;
|
||||||
|
cout << "The height of the image is " << bmp_info_header.height << endl;
|
||||||
|
for (int y = 0; y < bmp_info_header.height; ++y) {
|
||||||
|
for (int x = 0; x < bmp_info_header.width; ++x) {
|
||||||
|
//cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Read red
|
||||||
|
ColorBlue[y][x]=data[channels * (y * bmp_info_header.width + x) + 0];
|
||||||
|
ColorGreen[y][x]=data[channels * (y * bmp_info_header.width + x) + 1];
|
||||||
|
ColorRed[y][x]=data[channels * (y * bmp_info_header.width + x) + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int y=0; y<bmp_info_header.height; y++)
|
||||||
|
{
|
||||||
|
for(int x=0; x<bmp_info_header.width; x++)
|
||||||
|
{
|
||||||
|
sum=ColorRed[y][x]+sum-((ColorBlue[y][x])/2+(ColorGreen[y][x])/2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
intensity=sum/pixels;
|
||||||
|
cout << intensity << endl;
|
||||||
|
return intensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OrganizeAverageGreen()
|
||||||
|
{
|
||||||
|
int ColorRed[bmp_info_header.height][bmp_info_header.width];
|
||||||
|
int ColorGreen[bmp_info_header.height][bmp_info_header.width];;
|
||||||
|
int ColorBlue[bmp_info_header.height][bmp_info_header.width];
|
||||||
|
float pixels=bmp_info_header.height*bmp_info_header.width;
|
||||||
|
float intensity=0;
|
||||||
|
float sum=0;
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
cout << "The Width of the image is " << bmp_info_header.width << endl;
|
||||||
|
cout << "The height of the image is " << bmp_info_header.height << endl;
|
||||||
|
for (int y = 0; y < bmp_info_header.height; ++y) {
|
||||||
|
for (int x = 0; x < bmp_info_header.width; ++x) {
|
||||||
|
//cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Read Green
|
||||||
|
ColorBlue[y][x]=data[channels * (y * bmp_info_header.width + x) + 0];
|
||||||
|
ColorGreen[y][x]=data[channels * (y * bmp_info_header.width + x) + 1];
|
||||||
|
ColorRed[y][x]=data[channels * (y * bmp_info_header.width + x) + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int y=0; y<bmp_info_header.height; y++)
|
||||||
|
{
|
||||||
|
for(int x=0; x<bmp_info_header.width; x++)
|
||||||
|
{
|
||||||
|
sum=ColorGreen[y][x]+sum-((ColorBlue[y][x])/2+(ColorRed[y][x])/2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
intensity=sum/pixels;
|
||||||
|
cout << intensity << endl;
|
||||||
|
return intensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OrganizeAverageBlue()
|
||||||
|
{
|
||||||
|
int ColorRed[bmp_info_header.height][bmp_info_header.width];
|
||||||
|
int ColorGreen[bmp_info_header.height][bmp_info_header.width];;
|
||||||
|
int ColorBlue[bmp_info_header.height][bmp_info_header.width];
|
||||||
|
float pixels=bmp_info_header.height*bmp_info_header.width;
|
||||||
|
float intensity=0;
|
||||||
|
float sum=0;
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
cout << "The Width of the image is " << bmp_info_header.width << endl;
|
||||||
|
cout << "The height of the image is " << bmp_info_header.height << endl;
|
||||||
|
for (int y = 0; y < bmp_info_header.height; ++y) {
|
||||||
|
for (int x = 0; x < bmp_info_header.width; ++x) {
|
||||||
|
//cout << channels*(y*bmp_info_header.width+x) << endl;
|
||||||
|
//Read Blue
|
||||||
|
ColorBlue[y][x]=data[channels * (y * bmp_info_header.width + x) + 0];
|
||||||
|
ColorGreen[y][x]=data[channels * (y * bmp_info_header.width + x) + 1];
|
||||||
|
ColorRed[y][x]=data[channels * (y * bmp_info_header.width + x) + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int y=0; y<bmp_info_header.height; y++)
|
||||||
|
{
|
||||||
|
for(int x=0; x<bmp_info_header.width; x++)
|
||||||
|
{
|
||||||
|
sum=ColorBlue[y][x]+sum-((ColorGreen[y][x])/2+(ColorRed[y][x])/2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
intensity=sum/pixels;
|
||||||
|
cout << intensity << endl;
|
||||||
|
return intensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_pixel(uint32_t x0, uint32_t y0, uint8_t B, uint8_t G, uint8_t R, uint8_t A) {
|
||||||
|
if (x0 >= (uint32_t)bmp_info_header.width || y0 >= (uint32_t)bmp_info_header.height || x0 < 0 || y0 < 0) {
|
||||||
|
return;//throw std::runtime_error("The point is outside the image boundaries!");
|
||||||
|
}
|
||||||
|
uint32_t channels = bmp_info_header.bit_count / 8;
|
||||||
|
data[channels * (y0 * bmp_info_header.width + x0) + 0] = B;
|
||||||
|
data[channels * (y0 * bmp_info_header.width + x0) + 1] = G;
|
||||||
|
data[channels * (y0 * bmp_info_header.width + x0) + 2] = R;
|
||||||
|
if (channels == 4) {
|
||||||
|
data[channels * (y0 * bmp_info_header.width + x0) + 3] = A;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_rectangle(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h,
|
||||||
|
uint8_t B, uint8_t G, uint8_t R, uint8_t A, uint8_t line_w) {
|
||||||
|
if (x0 + w > (uint32_t)bmp_info_header.width || y0 + h > (uint32_t)bmp_info_header.height) {
|
||||||
|
return;//throw std::runtime_error("The rectangle does not fit in the image!");
|
||||||
|
}
|
||||||
|
|
||||||
|
fill_region(x0, y0, w, line_w, B, G, R, A); // top line
|
||||||
|
fill_region(x0, (y0 + h - line_w), w, line_w, B, G, R, A); // bottom line
|
||||||
|
fill_region((x0 + w - line_w), (y0 + line_w), line_w, (h - (2 * line_w)), B, G, R, A); // right line
|
||||||
|
fill_region(x0, (y0 + line_w), line_w, (h - (2 * line_w)), B, G, R, A); // left line
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint32_t row_stride{ 0 };
|
||||||
|
|
||||||
|
void write_headers(std::ofstream &of) {
|
||||||
|
of.write((const char*)&file_header, sizeof(file_header));
|
||||||
|
of.write((const char*)&bmp_info_header, sizeof(bmp_info_header));
|
||||||
|
if(bmp_info_header.bit_count == 32) {
|
||||||
|
of.write((const char*)&bmp_color_header, sizeof(bmp_color_header));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_headers_and_data(std::ofstream &of) {
|
||||||
|
write_headers(of);
|
||||||
|
of.write((const char*)data.data(), data.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_headersss(std::stringstream &of) {
|
||||||
|
of.write((const char*)&file_header, sizeof(file_header));
|
||||||
|
of.write((const char*)&bmp_info_header, sizeof(bmp_info_header));
|
||||||
|
if(bmp_info_header.bit_count == 32) {
|
||||||
|
of.write((const char*)&bmp_color_header, sizeof(bmp_color_header));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_headers_and_datass(std::stringstream &of) {
|
||||||
|
write_headersss(of);
|
||||||
|
of.write((const char*)data.data(), data.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add 1 to the row_stride until it is divisible with align_stride
|
||||||
|
uint32_t make_stride_aligned(uint32_t align_stride) {
|
||||||
|
uint32_t new_stride = row_stride;
|
||||||
|
while (new_stride % align_stride != 0) {
|
||||||
|
new_stride++;
|
||||||
|
}
|
||||||
|
return new_stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the pixel data is stored as BGRA and if the color space type is sRGB
|
||||||
|
void check_color_header(BMPColorHeader &bmp_color_header) {
|
||||||
|
BMPColorHeader expected_color_header;
|
||||||
|
if(expected_color_header.red_mask != bmp_color_header.red_mask ||
|
||||||
|
expected_color_header.blue_mask != bmp_color_header.blue_mask ||
|
||||||
|
expected_color_header.green_mask != bmp_color_header.green_mask ||
|
||||||
|
expected_color_header.alpha_mask != bmp_color_header.alpha_mask) {
|
||||||
|
return;//throw std::runtime_error("Unexpected color mask format! The program expects the pixel data to be in the BGRA format");
|
||||||
|
}
|
||||||
|
if(expected_color_header.color_space_type != bmp_color_header.color_space_type) {
|
||||||
|
return;//throw std::runtime_error("Unexpected color space type! The program expects sRGB values");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
106
bmpconverter.hpp
Normal file
106
bmpconverter.hpp
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "lodepng.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
//returns 0 if all went ok, non-0 if error
|
||||||
|
//output image is always given in RGBA (with alpha channel), even if it's a BMP without alpha channel
|
||||||
|
unsigned decodeBMP(std::vector<unsigned char>& image, unsigned& w, unsigned& h, const std::vector<unsigned char>& bmp) {
|
||||||
|
static const unsigned MINHEADER = 54; //minimum BMP header size
|
||||||
|
|
||||||
|
if(bmp.size() < MINHEADER) return -1;
|
||||||
|
if(bmp[0] != 'B' || bmp[1] != 'M') return 1; //It's not a BMP file if it doesn't start with marker 'BM'
|
||||||
|
unsigned pixeloffset = bmp[10] + 256 * bmp[11]; //where the pixel data starts
|
||||||
|
//read width and height from BMP header
|
||||||
|
w = bmp[18] + bmp[19] * 256;
|
||||||
|
h = bmp[22] + bmp[23] * 256;
|
||||||
|
//read number of channels from BMP header
|
||||||
|
if(bmp[28] != 24 && bmp[28] != 32) return 2; //only 24-bit and 32-bit BMPs are supported.
|
||||||
|
unsigned numChannels = bmp[28] / 8;
|
||||||
|
|
||||||
|
//The amount of scanline bytes is width of image times channels, with extra bytes added if needed
|
||||||
|
//to make it a multiple of 4 bytes.
|
||||||
|
unsigned scanlineBytes = w * numChannels;
|
||||||
|
if(scanlineBytes % 4 != 0) scanlineBytes = (scanlineBytes / 4) * 4 + 4;
|
||||||
|
|
||||||
|
unsigned dataSize = scanlineBytes * h;
|
||||||
|
if(bmp.size() < dataSize + pixeloffset) return 3; //BMP file too small to contain all pixels
|
||||||
|
|
||||||
|
image.resize(w * h * 4);
|
||||||
|
|
||||||
|
/*
|
||||||
|
There are 3 differences between BMP and the raw image buffer for LodePNG:
|
||||||
|
-it's upside down
|
||||||
|
-it's in BGR instead of RGB format (or BRGA instead of RGBA)
|
||||||
|
-each scanline has padding bytes to make it a multiple of 4 if needed
|
||||||
|
The 2D for loop below does all these 3 conversions at once.
|
||||||
|
*/
|
||||||
|
for(unsigned y = 0; y < h; y++)
|
||||||
|
for(unsigned x = 0; x < w; x++) {
|
||||||
|
//pixel start byte position in the BMP
|
||||||
|
unsigned bmpos = pixeloffset + (h - y - 1) * scanlineBytes + numChannels * x;
|
||||||
|
//pixel start byte position in the new raw image
|
||||||
|
unsigned newpos = 4 * y * w + 4 * x;
|
||||||
|
if(numChannels == 3) {
|
||||||
|
image[newpos + 0] = bmp[bmpos + 2]; //R
|
||||||
|
image[newpos + 1] = bmp[bmpos + 1]; //G
|
||||||
|
image[newpos + 2] = bmp[bmpos + 0]; //B
|
||||||
|
image[newpos + 3] = 255; //A
|
||||||
|
} else {
|
||||||
|
image[newpos + 0] = bmp[bmpos + 2]; //R
|
||||||
|
image[newpos + 1] = bmp[bmpos + 1]; //G
|
||||||
|
image[newpos + 2] = bmp[bmpos + 0]; //B
|
||||||
|
image[newpos + 3] = bmp[bmpos + 3]; //A
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned char> ConvertFile(std::string filename) {
|
||||||
|
|
||||||
|
std::vector<unsigned char> bmp;
|
||||||
|
lodepng::load_file(bmp, filename);
|
||||||
|
|
||||||
|
std::vector<unsigned char> image;
|
||||||
|
unsigned w, h;
|
||||||
|
unsigned error = decodeBMP(image, w, h, bmp);
|
||||||
|
|
||||||
|
if(error) {
|
||||||
|
std::cout << "BMP decoding error " << error << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned char> png;
|
||||||
|
error = lodepng::encode(png, image, w, h);
|
||||||
|
|
||||||
|
if(error) {
|
||||||
|
std::cout << "PNG encoding error " << error << ": " << lodepng_error_text(error) << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return png;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned char> ConvertData(std::vector<unsigned char> data) {
|
||||||
|
|
||||||
|
std::vector<unsigned char> image;
|
||||||
|
unsigned w, h;
|
||||||
|
unsigned error = decodeBMP(image, w, h, data);
|
||||||
|
|
||||||
|
if(error) {
|
||||||
|
std::cout << "BMP decoding error " << error << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned char> png;
|
||||||
|
error = lodepng::encode(png, image, w, h);
|
||||||
|
|
||||||
|
if(error) {
|
||||||
|
std::cout << "PNG encoding error " << error << ": " << lodepng_error_text(error) << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return png;
|
||||||
|
|
||||||
|
}
|
64
renderd7.cpp
64
renderd7.cpp
@ -1422,3 +1422,67 @@ std::string RenderD7::GetTimeStr(void)
|
|||||||
return RenderD7::FormatString("%02i:%02i:%02i", timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec);
|
return RenderD7::FormatString("%02i:%02i:%02i", timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned Bitmap_to_C3D(C2D_Image img, const std::vector<unsigned char>& bmp) {
|
||||||
|
static const unsigned MINHEADER = 54; //minimum BMP header size
|
||||||
|
|
||||||
|
if(bmp.size() < MINHEADER) return -1;
|
||||||
|
if(bmp[0] != 'B' || bmp[1] != 'M') return 1; //It's not a BMP file if it doesn't start with marker 'BM'
|
||||||
|
unsigned pixeloffset = bmp[10] + 256 * bmp[11]; //where the pixel data starts
|
||||||
|
//read width and height from BMP header
|
||||||
|
unsigned w = bmp[18] + bmp[19] * 256;
|
||||||
|
unsigned h = bmp[22] + bmp[23] * 256;
|
||||||
|
//read number of channels from BMP header
|
||||||
|
if(bmp[28] != 24 && bmp[28] != 32) return 2; //only 24-bit and 32-bit BMPs are supported.
|
||||||
|
unsigned numChannels = bmp[28] / 8;
|
||||||
|
|
||||||
|
//The amount of scanline bytes is width of image times channels, with extra bytes added if needed
|
||||||
|
//to make it a multiple of 4 bytes.
|
||||||
|
unsigned scanlineBytes = w * numChannels;
|
||||||
|
if(scanlineBytes % 4 != 0) scanlineBytes = (scanlineBytes / 4) * 4 + 4;
|
||||||
|
|
||||||
|
unsigned dataSize = scanlineBytes * h;
|
||||||
|
if(bmp.size() < dataSize + pixeloffset) return 3; //BMP file too small to contain all pixels
|
||||||
|
|
||||||
|
img.tex = new C3D_Tex;
|
||||||
|
img.subtex = new Tex3DS_SubTexture({(u16)w, (u16)h, 0.0f, 1.0f, w / 1024.0f, 1.0f - (h / 1024.0f)});
|
||||||
|
C3D_TexInit(img.tex, 1024, 1024, GPU_RGBA8);
|
||||||
|
C3D_TexSetFilter(img.tex, GPU_LINEAR, GPU_LINEAR);
|
||||||
|
img.tex->border = 0xFFFFFFFF;
|
||||||
|
C3D_TexSetWrap(img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
|
||||||
|
/*
|
||||||
|
There are 3 differences between BMP and the raw image buffer for LodePNG:
|
||||||
|
-it's upside down
|
||||||
|
-it's in BGR instead of RGB format (or BRGA instead of RGBA)
|
||||||
|
-each scanline has padding bytes to make it a multiple of 4 if needed
|
||||||
|
The 2D for loop below does all these 3 conversions at once.
|
||||||
|
*/
|
||||||
|
for(unsigned y = 0; y < h; y++)
|
||||||
|
for(unsigned x = 0; x < w; x++) {
|
||||||
|
//pixel start byte position in the BMP
|
||||||
|
unsigned bmpos = pixeloffset + (h - y - 1) * scanlineBytes + numChannels * x;
|
||||||
|
//pixel start byte position in the new raw image
|
||||||
|
unsigned newpos = 4 * y * w + 4 * x;
|
||||||
|
if(numChannels == 3) {
|
||||||
|
((u8*)img.tex->data)[newpos + 3] = bmp[bmpos + 2]; //R
|
||||||
|
((u8*)img.tex->data)[newpos + 2] = bmp[bmpos + 1]; //G
|
||||||
|
((u8*)img.tex->data)[newpos + 1] = bmp[bmpos + 0]; //B
|
||||||
|
((u8*)img.tex->data)[newpos + 0] = 255; //A
|
||||||
|
} else {
|
||||||
|
((u8*)img.tex->data)[newpos + 3] = bmp[bmpos + 2]; //R
|
||||||
|
((u8*)img.tex->data)[newpos + 2] = bmp[bmpos + 1]; //G
|
||||||
|
((u8*)img.tex->data)[newpos + 1] = bmp[bmpos + 0]; //B
|
||||||
|
((u8*)img.tex->data)[newpos + 0] = bmp[bmpos + 3]; //A
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderD7::Image::LoadFromBitmap(BMP bitmap)
|
||||||
|
{
|
||||||
|
unsigned error = Bitmap_to_C3D(this->img, bitmap.DATA());
|
||||||
|
|
||||||
|
if(error) {
|
||||||
|
std::cout << "BMP decoding error " << error << std::endl;
|
||||||
|
RenderD7::DrawText(0, 0, 2.0f, RenderD7::Color::Hex("#000000"), "Error : " + std::to_string(error));
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,7 @@
|
|||||||
#include "ini.hpp"
|
#include "ini.hpp"
|
||||||
#include "stringtool.hpp"
|
#include "stringtool.hpp"
|
||||||
#include "Clock.hpp"
|
#include "Clock.hpp"
|
||||||
|
#include "bmp.hpp"
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
@ -105,6 +106,7 @@ namespace RenderD7
|
|||||||
/// Load the Image from buffer
|
/// Load the Image from buffer
|
||||||
/// \param buffer the frame buffer
|
/// \param buffer the frame buffer
|
||||||
void LoadPFromBuffer(const std::vector<u8> &buffer);
|
void LoadPFromBuffer(const std::vector<u8> &buffer);
|
||||||
|
void LoadFromBitmap(BMP bitmap);
|
||||||
/// Draw the Image directly
|
/// Draw the Image directly
|
||||||
/// \param x The x position
|
/// \param x The x position
|
||||||
/// \param y the y position
|
/// \param y the y position
|
||||||
|
Loading…
Reference in New Issue
Block a user