This commit is contained in:
tobid7 2022-11-25 01:05:50 +01:00
parent c1e471f23a
commit 222a13387d
5 changed files with 113 additions and 114 deletions

View File

@ -35,6 +35,7 @@ public:
void DrawPixel(int x, int y, u8 b, u8 g, u8 r, u8 a);
void DrawRect(int x, int y, int w, int h, u8 line_w, u8 b, u8 g, u8 r, u8 a);
void DrawRectFilled(int x, int y, int w, int h, u8 b, u8 g, u8 r, u8 a);
void DrawBitmap(int x, int y, BMP map);
void UsePreMap(BMP map);
void UsePrePrintMap(BitmapPrinter printmap);
BMP GetBitmap() { return bitmap; }

View File

@ -0,0 +1,8 @@
#pragma once
#include <3ds.h>
#include <vector>
namespace Tasks {
void create(ThreadFunc entrypoint);
void destroy(void);
} // namespace Tasks

View File

@ -5,6 +5,7 @@
#include <sstream>
#include <stdexcept>
#include <vector>
using namespace std;
#pragma pack(push, 1)
@ -48,7 +49,8 @@ struct BMPColorHeader {
};
#pragma pack(pop)
struct BMP {
class BMP {
public:
BMPFileHeader file_header;
BMPInfoHeader bmp_info_header;
BMPColorHeader bmp_color_header;
@ -62,8 +64,8 @@ struct BMP {
inp.read((char *)&file_header, sizeof(file_header));
if (file_header.file_type != 0x4D42) {
return 50; // throw std::runtime_error("Error! Unrecognized file
// format.");
return 50;
// throw std::runtime_error("Error! Unrecognized file format.");
}
inp.read((char *)&bmp_info_header, sizeof(bmp_info_header));
@ -78,9 +80,8 @@ struct BMP {
check_color_header(bmp_color_header);
} else {
// std::cerr << "Error! The file \"" << fname << "\" does not seem to
// contain bit mask information\n";
return 51; // throw std::runtime_error("Error! Unrecognized file
// format.");
// contain bit mask information\n"; return 51;//throw
// std::runtime_error("Error! Unrecognized file format.");
}
}
@ -137,12 +138,14 @@ struct BMP {
std::stringstream inp;
std::copy(buffer.begin(), buffer.end(),
std::ostream_iterator<unsigned char>(inp, "\n"));
std::cout << buffer.size() << std::endl;
if (inp) {
inp.read((char *)&file_header, sizeof(file_header));
if (file_header.file_type != 0x4D42) {
return 50; // throw std::runtime_error("Error! Unrecognized file
// format.");
return 50;
// throw std::runtime_error("Error! Unrecognized file format. Header " +
// std::to_string(file_header.file_type));
}
inp.read((char *)&bmp_info_header, sizeof(bmp_info_header));
@ -158,8 +161,8 @@ struct BMP {
} else {
// std::cerr << "Error! The file \"" << fname << "\" does not seem to
// contain bit mask information\n";
return 51; // throw std::runtime_error("Error! Unrecognized file
// format.");
return 51;
// throw std::runtime_error("Error! Unrecognized file format. Size");
}
}
@ -181,8 +184,10 @@ struct BMP {
file_header.file_size = file_header.offset_data;
if (bmp_info_header.height < 0) {
return 52; // throw std::runtime_error("The program can treat only BMP
// images with the origin in the bottom left corner!");
return 52;
// 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 *
@ -206,8 +211,8 @@ struct BMP {
bmp_info_header.height * static_cast<uint32_t>(padding_row.size());
}
} else {
return 53; // throw std::runtime_error("Unable to open the input image
// file "+std::string(fname));
return 53;
// throw std::runtime_error("Unable to open the input image buffer");
}
return 0;
}
@ -525,108 +530,14 @@ struct BMP {
}
}
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;
}
unsigned 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 59; // throw std::runtime_error("The point is outside the image
// boundaries!");
return 59;
// std::string errr = "The point is outside the image boundaries! -> " +
// std::to_string(x0) + "," + std::to_string(y0); throw
// std::runtime_error(errr);
}
uint32_t channels = bmp_info_header.bit_count / 8;
data[channels * (y0 * bmp_info_header.width + x0) + 0] = B;
@ -643,8 +554,9 @@ struct BMP {
uint32_t y1 = this->bmp_info_header.height - y0;
if (x0 >= (uint32_t)bmp_info_header.width ||
y1 <= (uint32_t)bmp_info_header.width || x0 < 0 || y0 > 0) {
return; // throw std::runtime_error("The point is outside the image
// boundaries!");
return;
// throw std::runtime_error("The point is outside the image boundaries! ->
// " + std::to_string(x0) + "," + std::to_string(y1));
}
uint32_t channels = bmp_info_header.bit_count / 8;
@ -656,6 +568,52 @@ struct BMP {
}
}
uint32_t get_pixel(uint32_t x0, uint32_t y0) {
if (x0 >= (uint32_t)bmp_info_header.width ||
y0 >= (uint32_t)bmp_info_header.height || x0 < 0 || y0 < 0) {
return 59; // throw std::runtime_error("The point is outside the image
// boundaries!");
}
uint32_t channels = bmp_info_header.bit_count / 8;
uint32_t outcol = 0;
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
uint8_t alpha = 255;
blue = data[channels * (y0 * bmp_info_header.width + x0) + 0];
green = data[channels * (y0 * bmp_info_header.width + x0) + 1];
red = data[channels * (y0 * bmp_info_header.width + x0) + 2];
if (channels == 4) {
alpha = data[channels * (y0 * bmp_info_header.width + x0) + 3];
}
outcol = ((red << 24) | (green << 16) | (blue << 8) | alpha);
return outcol;
}
uint32_t get_pixel_df(uint32_t x0, uint32_t y0) {
uint32_t y1 = this->bmp_info_header.height - y0;
if (x0 >= (uint32_t)bmp_info_header.width ||
y1 <= (uint32_t)bmp_info_header.width || x0 < 0 || y0 > 0) {
return 0; // throw std::runtime_error("The point is outside the image
// boundaries!");
}
uint32_t channels = bmp_info_header.bit_count / 8;
uint32_t outcol = 0;
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
uint8_t alpha = 255;
blue = data[channels * (y0 * bmp_info_header.width + x0) + 0];
green = data[channels * (y0 * bmp_info_header.width + x0) + 1];
red = data[channels * (y0 * bmp_info_header.width + x0) + 2];
if (channels == 4) {
alpha = data[channels * (y0 * bmp_info_header.width + x0) + 3];
}
outcol = ((red << 24) | (green << 16) | (blue << 8) | alpha);
return outcol;
}
unsigned 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) {

View File

@ -463,3 +463,13 @@ bool RenderD7::BitmapPrinter::Decode(Decoder deccc) {
}
return res;
}
void RenderD7::BitmapPrinter::DrawBitmap(int x, int y, BMP map) {
for (int i = 0; i < map.bmp_info_header.width; i++) {
for (int j = 0; j < map.bmp_info_header.height; j++) {
bitmap.set_pixel(
x + i, (bitmap.bmp_info_header.height - 1) - (y + j),
UNPACK_BGRA(map.get_pixel(i, (map.bmp_info_header.height - 1) - j)));
}
}
}

22
source/Tasks.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <renderd7/Tasks.hpp>
#include <3ds.h>
#include <stdio.h>
#include <string.h>
static std::vector<Thread> threads;
void Tasks::create(ThreadFunc entrypoint) {
s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
Thread thread = threadCreate((ThreadFunc)entrypoint, NULL, 64 * 1024,
prio - 1, -2, false);
threads.push_back(thread);
}
void Tasks::destroy(void) {
for (u32 i = 0; i < threads.size(); i++) {
threadJoin(threads.at(i), U64_MAX);
threadFree(threads.at(i));
}
}