diff --git a/include/renderd7/BitmapPrinter.hpp b/include/renderd7/BitmapPrinter.hpp index 61f7b74..69cba0b 100644 --- a/include/renderd7/BitmapPrinter.hpp +++ b/include/renderd7/BitmapPrinter.hpp @@ -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; } diff --git a/include/renderd7/Tasks.hpp b/include/renderd7/Tasks.hpp new file mode 100644 index 0000000..f3d918a --- /dev/null +++ b/include/renderd7/Tasks.hpp @@ -0,0 +1,8 @@ +#pragma once +#include <3ds.h> +#include + +namespace Tasks { +void create(ThreadFunc entrypoint); +void destroy(void); +} // namespace Tasks \ No newline at end of file diff --git a/include/renderd7/bmp.hpp b/include/renderd7/bmp.hpp index 8014724..783f00f 100644 --- a/include/renderd7/bmp.hpp +++ b/include/renderd7/bmp.hpp @@ -5,6 +5,7 @@ #include #include #include + 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(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(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) { diff --git a/source/BitmapPrinter.cpp b/source/BitmapPrinter.cpp index b060694..ef22e14 100644 --- a/source/BitmapPrinter.cpp +++ b/source/BitmapPrinter.cpp @@ -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))); + } + } +} \ No newline at end of file diff --git a/source/Tasks.cpp b/source/Tasks.cpp new file mode 100644 index 0000000..bf7ac7a --- /dev/null +++ b/source/Tasks.cpp @@ -0,0 +1,22 @@ +#include + +#include <3ds.h> +#include +#include + +static std::vector 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)); + } +} \ No newline at end of file