From fcce9d93d454faf57bfcf7ae6f644ba8ae2b0e6a Mon Sep 17 00:00:00 2001 From: Evgenij Titarenko Date: Sat, 20 Apr 2024 00:03:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=9C=D0=9D=D0=AB=D0=95=20=D0=A3=D0=9A?= =?UTF-8?q?=D0=90=D0=97=D0=90=D0=A2=D0=95=D0=9B=D0=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- font-lib/psf.cpp | 8 +- font-lib/psf.h | 3 +- image-lib/bmpimage.cpp | 267 +++++++++++++++++++-------------------- image-lib/bmpimage.h | 36 +++--- image-lib/pixelarray.cpp | 8 +- image-lib/pixelarray.h | 1 + main.cpp | 171 ++++++++++++------------- 7 files changed, 240 insertions(+), 254 deletions(-) diff --git a/font-lib/psf.cpp b/font-lib/psf.cpp index 018fce8..e79e338 100644 --- a/font-lib/psf.cpp +++ b/font-lib/psf.cpp @@ -7,7 +7,7 @@ std::wstring_convert, char16_t> conv_utf8_utf16; Font readPSF(const std::string &filename) { { - PSF2Header psf2Header; + PSF2Header psf2Header{}; std::ifstream ifs(filename, std::ios_base::binary); if (!ifs.good()) { throw std::runtime_error("File read error"); @@ -80,8 +80,4 @@ Font::Font(uint32_t glyphsCount, uint32_t glyphWidth, uint32_t glyphHeight) { this->glyphsCount = glyphsCount; this->glyphWidth = glyphWidth; this->glyphHeight = glyphHeight; -} - -Font::Font() { - -} +} \ No newline at end of file diff --git a/font-lib/psf.h b/font-lib/psf.h index b709d00..932affb 100644 --- a/font-lib/psf.h +++ b/font-lib/psf.h @@ -6,6 +6,7 @@ #include #include +static_assert(true); //Обход clangd бага #pragma pack(push, 1) struct PSF1Header { char magicNumber[2]; @@ -44,8 +45,6 @@ public: uint32_t glyphHeight; uint32_t glyphsCount; - Font(); - explicit Font(uint32_t glyphs, uint32_t glyphWidth, uint32_t glyphHeight); }; diff --git a/image-lib/bmpimage.cpp b/image-lib/bmpimage.cpp index eb3c7d4..c8791b8 100644 --- a/image-lib/bmpimage.cpp +++ b/image-lib/bmpimage.cpp @@ -1,12 +1,14 @@ #include #include #include +#include #include "bmpimage.h" -BMPImage::BMPImage(const BitmapFileHeader &fileHeader, const BITMAPINFOHEADER &infoHeader, const PixelArray &pixelArray) - : pixelArray(pixelArray) { +BMPImage::BMPImage(const BitmapFileHeader &fileHeader, const BITMAPINFOHEADER &infoHeader, + std::shared_ptr pixelArray) { this->fileHeader = fileHeader; this->infoHeader = infoHeader; + this->pixelArray = std::move(pixelArray); } @@ -26,25 +28,26 @@ void BMPImage::save(const std::string &filename) { uint32_t byteByRow = this->infoHeader.BitmapWidth * 3; uint8_t padding = (4 - byteByRow % 4) % 4; for (auto i = (int) this->infoHeader.BitmapHeight - 1; i > -1; --i) { - ofs.write((char *) pixelArray(i), byteByRow); + ofs.write((char *) (*pixelArray)(i), byteByRow); if (padding != 4) ofs.write(PADDING_ZEROES, padding); // Write padding } } } -PixelArray BMPImage::pixels() { +std::shared_ptr BMPImage::pixels() const { return this->pixelArray; } -PixelArray BMPImage::pixels_copy() { +std::shared_ptr BMPImage::pixels_copy() { // Pixel **newPixelArray; - PixelArray newPixelArray(this->infoHeader.BitmapWidth, this->infoHeader.BitmapHeight); + std::shared_ptr newPixelArray = std::make_shared(this->infoHeader.BitmapWidth, + this->infoHeader.BitmapHeight); // newPixelArray = new Pixel *[this->infoHeader.BitmapHeight]; for (int i = 0; i < this->infoHeader.BitmapHeight; ++i) { // newPixelArray[i] = new Pixel[this->infoHeader.BitmapWidth]; // std::copy(this->pixelArray(i), this->pixelArray(i) + this->infoHeader.BitmapWidth, newPixelArray(i)); // TODO for (int j = 0; j < this->infoHeader.BitmapWidth; ++j) { - newPixelArray(i, j) = this->pixelArray(i, j); + (*newPixelArray)(i, j) = (*this->pixelArray)(i, j); // TODO: Сделать конструктор копирования в PixelArray } } return newPixelArray; @@ -58,17 +61,9 @@ BITMAPINFOHEADER BMPImage::infoHeader_copy() { return this->infoHeader; } -BMPImage::~BMPImage() { -// delete pixelArray; -// for (int i = 0; i < this->infoHeader.BitmapHeight; ++i) { -// delete[] this->pixelArray(i); -// } -// delete[] this->pixelArray; -} - -BMPImage::BMPImage(const PixelArray &pixelArray) : pixelArray(pixelArray) { - uint32_t width = pixelArray.width(); - uint32_t height = pixelArray.height(); +BMPImage::BMPImage(std::shared_ptr pixelArray) { + uint32_t width = pixelArray->width(); + uint32_t height = pixelArray->height(); BitmapFileHeader _fileHeader; BITMAPINFOHEADER _infoHeader; _infoHeader.BitmapWidth = width; @@ -77,65 +72,66 @@ BMPImage::BMPImage(const PixelArray &pixelArray) : pixelArray(pixelArray) { _fileHeader.fileSize = _infoHeader.ImageSize + _fileHeader.imageDataOffset; this->infoHeader = _infoHeader; this->fileHeader = _fileHeader; -// this->pixelArray = pixelArray; + this->pixelArray = pixelArray; } -BMPImage BMPImage::appendRight(BMPImage &img) { - uint32_t newHeight = std::max(this->infoHeader.BitmapHeight, img.height()); - uint32_t newWidth = this->infoHeader.BitmapWidth + img.width(); +std::shared_ptr BMPImage::appendRight(const std::shared_ptr& img) { + uint32_t newHeight = std::max(this->infoHeader.BitmapHeight, img->height()); + uint32_t newWidth = this->infoHeader.BitmapWidth + img->width(); // Pixel **newPixelArray; - PixelArray newPixelArray(newWidth, newHeight); + std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); // std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); // newPixelArray = new Pixel *[newHeight]; for (int i = 0; i < this->infoHeader.BitmapHeight; ++i) { // newPixelArray[i] = new Pixel[newWidth]; - std::copy(this->pixelArray(i), this->pixelArray(i) + this->infoHeader.BitmapWidth, newPixelArray(i)); + std::copy((*this->pixelArray)(i), (*this->pixelArray)(i) + this->infoHeader.BitmapWidth, (*newPixelArray)(i)); } - for (int i = 0; i < img.height(); ++i) { - std::copy(img.pixels()(i), img.pixelArray(i) + img.width(), newPixelArray(i) + this->infoHeader.BitmapWidth); + for (int i = 0; i < img->height(); ++i) { + std::copy((*img->pixels())(i), (*img->pixelArray)(i) + img->width(), + (*newPixelArray)(i) + this->infoHeader.BitmapWidth); } - return {newPixelArray}; + return std::make_shared(newPixelArray); } -BMPImage BMPImage::overlay(BMPImage &img, uint32_t pos_x, uint32_t pos_y) { - if (pos_x + img.width() > this->width() || pos_y + img.height() > this->height()) +std::shared_ptr BMPImage::overlay(const std::shared_ptr& img, uint32_t pos_x, uint32_t pos_y) { + if (pos_x + img->width() > this->width() || pos_y + img->height() > this->height()) throw std::runtime_error("The overlaid image is outside the image"); auto pixels = this->pixels_copy(); - for (int i = 0; i < img.height(); ++i) { - std::copy(img.pixels()(i), img.pixels()(i) + img.width(), pixels(pos_y + i) + pos_x); + for (int i = 0; i < img->height(); ++i) { + std::copy((*img->pixels())(i), (*img->pixels())(i) + img->width(), (*pixels)(pos_y + i) + pos_x); } - return {pixels}; + return std::make_shared(pixels); } -BMPImage BMPImage::applyFilter(const std::function &)> &filter) { +std::shared_ptr BMPImage::applyFilter(const std::function &)> &filter) { auto origPixels = this->pixels_copy(); - PixelArray pixels(this->width(), this->height()); + std::shared_ptr pixels = std::make_shared(this->width(), this->height()); for (int y = 1; y < this->height() - 1; ++y) { for (int x = 1; x < this->width() - 1; ++x) { - auto p1 = origPixels(y - 1, x - 1); - auto p2 = origPixels(y - 1, x); - auto p3 = origPixels(y - 1, x + 1); - auto p4 = origPixels(y, x - 1); - auto p5 = origPixels(y, x); - auto p6 = origPixels(y, x + 1); - auto p7 = origPixels(y + 1, x - 1); - auto p8 = origPixels(y + 1, x); - auto p9 = origPixels(y + 1, x + 1); + auto p1 = (*origPixels)(y - 1, x - 1); + auto p2 = (*origPixels)(y - 1, x); + auto p3 = (*origPixels)(y - 1, x + 1); + auto p4 = (*origPixels)(y, x - 1); + auto p5 = (*origPixels)(y, x); + auto p6 = (*origPixels)(y, x + 1); + auto p7 = (*origPixels)(y + 1, x - 1); + auto p8 = (*origPixels)(y + 1, x); + auto p9 = (*origPixels)(y + 1, x + 1); std::array red_channel = {p1.r, p2.r, p3.r, p4.r, p5.r, p6.r, p7.r, p8.r, p9.r}; std::array green_channel = {p1.g, p2.g, p3.g, p4.g, p5.g, p6.g, p7.g, p8.g, p9.g}; std::array blue_channel = {p1.b, p2.b, p3.b, p4.b, p5.b, p6.b, p7.b, p8.b, p9.b}; auto r = filter(red_channel); auto g = filter(green_channel); auto b = filter(blue_channel); - pixels(y, x) = {r, g, b}; + (*pixels)(y, x) = {r, g, b}; } } - return {pixels}; + return std::make_shared(pixels); } -BMPImage readBMPImage(const std::string &filename) { +std::shared_ptr readBMPImage(const std::string &filename) { BitmapFileHeader bitmapFileHeader; BITMAPINFOHEADER bitmapInfoHeader; uint32_t DIB_Header_Size; @@ -159,8 +155,8 @@ BMPImage readBMPImage(const std::string &filename) { ifs.seekg(14, std::ios::beg); ifs.read((char *) &bitmapInfoHeader, sizeof(bitmapInfoHeader)); } - PixelArray pixelArray = PixelArray(bitmapInfoHeader.BitmapWidth, bitmapInfoHeader.BitmapHeight); -// pixelArray = new Pixel *[bitmapInfoHeader.BitmapHeight]; + std::shared_ptr pixelArray = std::make_shared(bitmapInfoHeader.BitmapWidth, + bitmapInfoHeader.BitmapHeight); { std::ifstream ifs(filename, std::ios_base::binary); if (!ifs.good()) { @@ -171,216 +167,217 @@ BMPImage readBMPImage(const std::string &filename) { uint8_t padding = (4 - byteByRow % 4) % 4; for (auto i = (int) bitmapInfoHeader.BitmapHeight - 1; i > -1; --i) { // pixelArray(i) = new Pixel[bitmapInfoHeader.BitmapWidth]; - ifs.read((char *) pixelArray(i), byteByRow); + ifs.read((char *) (*pixelArray)(i), byteByRow); if (padding != 4) ifs.seekg(padding, std::ios_base::cur); // Skip padding } } - return {bitmapFileHeader, bitmapInfoHeader, pixelArray}; + return std::make_shared(bitmapFileHeader, bitmapInfoHeader, pixelArray); } -BMPImage grayscale(BMPImage &img) { - auto pixels = img.pixels_copy(); - for (int i = 0; i < img.height(); ++i) { - for (int j = 0; j < img.width(); ++j) { - uint8_t gray = pixels(i, j).r / 3 + pixels(i, j).g / 3 + pixels(i, j).b / 3; - pixels(i, j) = {gray, gray, gray}; +std::shared_ptr grayscale(const std::shared_ptr& img) { + auto pixels = img->pixels_copy(); + for (int i = 0; i < img->height(); ++i) { + for (int j = 0; j < img->width(); ++j) { + uint8_t gray = (*pixels)(i, j).r / 3 + (*pixels)(i, j).g / 3 + (*pixels)(i, j).b / 3; + (*pixels)(i, j) = {gray, gray, gray}; } } - return {img.fileHeader_copy(), img.infoHeader_copy(), pixels}; + return std::make_shared(img->fileHeader_copy(), img->infoHeader_copy(), pixels); } -BMPImage invertColors(BMPImage &img) { - auto pixels = img.pixels_copy(); - for (int i = 0; i < img.height(); ++i) { - for (int j = 0; j < img.width(); ++j) { - pixels(i, j) = 255 - pixels(i, j); +std::shared_ptr invertColors(const std::shared_ptr& img) { + auto pixels = img->pixels_copy(); + for (int i = 0; i < img->height(); ++i) { + for (int j = 0; j < img->width(); ++j) { + (*pixels)(i, j) = 255 - (*pixels)(i, j); } } - return {img.fileHeader_copy(), img.infoHeader_copy(), pixels}; + return std::make_shared(img->fileHeader_copy(), img->infoHeader_copy(), pixels); } -BMPImage upscale2x(BMPImage &img) { - auto oldPixels = img.pixels(); - const uint32_t newHeight = img.height() * 2; - const uint32_t newWidth = img.width() * 2; - PixelArray newPixelArray(newWidth, newHeight); +std::shared_ptr upscale2x(const std::shared_ptr& img) { + auto oldPixels = img->pixels(); + const uint32_t newHeight = img->height() * 2; + const uint32_t newWidth = img->width() * 2; + std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); for (int i = 0; i < newHeight; i += 2) { for (int j = 0; j < newWidth; ++j) { if (j % 2 == 0) - newPixelArray(i, j) = oldPixels(i / 2, j / 2); + (*newPixelArray)(i, j) = (*oldPixels)(i / 2, j / 2); else if (j == newWidth - 1) - newPixelArray(i, j) = oldPixels(i / 2, j / 2) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(i / 2, j / 2) / 2; else - newPixelArray(i, j) = oldPixels(i / 2, j / 2) / 2 + oldPixels(i / 2, j / 2 + 1) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(i / 2, j / 2) / 2 + (*oldPixels)(i / 2, j / 2 + 1) / 2; } } for (int i = 1; i < newHeight; i += 2) { if (i == newHeight - 1) for (int j = 0; j < newWidth; ++j) { - newPixelArray(i, j) = newPixelArray(i - 1, j) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i - 1, j) / 2; } else for (int j = 0; j < newWidth; ++j) { - newPixelArray(i, j) = newPixelArray(i - 1, j) / 2 + newPixelArray(i + 1, j) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i - 1, j) / 2 + (*newPixelArray)(i + 1, j) / 2; } } - return {newPixelArray}; + return std::make_shared(newPixelArray); } -BMPImage downscale2x(BMPImage &img) { - auto oldPixels = img.pixels_copy(); - const uint32_t newHeight = img.height() / 2; - const uint32_t newWidth = img.width() / 2; - PixelArray newPixelArray(newWidth, newHeight); +std::shared_ptr downscale2x(const std::shared_ptr& img) { + auto oldPixels = img->pixels(); + const uint32_t newHeight = img->height() / 2; + const uint32_t newWidth = img->width() / 2; + std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); for (int i = 0; i < newHeight; ++i) { for (int j = 0; j < newWidth; ++j) { - newPixelArray(i, j) = oldPixels(i * 2, j * 2); + (*newPixelArray)(i, j) = (*oldPixels)(i * 2, j * 2); } } - return {newPixelArray}; + return std::make_shared(newPixelArray); } -BMPImage upscale1_5x(BMPImage &img) { - auto oldPixels = img.pixels(); - const uint32_t newHeight = img.height() * 3 / 2; - const uint32_t newWidth = img.width() * 3 / 2; - PixelArray newPixelArray(newWidth, newHeight); +std::shared_ptr upscale1_5x(const std::shared_ptr& img) { + auto oldPixels = img->pixels(); + const uint32_t newHeight = img->height() * 3 / 2; + const uint32_t newWidth = img->width() * 3 / 2; + std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); for (int i = 0; i < newHeight; ++i) { if ((i + 1) % 3 == 0) continue; for (int j = 0; j < newWidth; ++j) { int oldi = i * 2 / 3; int oldj = j * 2 / 3; if ((j + 1) % 3 != 0) - newPixelArray(i, j) = oldPixels(oldi, oldj); + (*newPixelArray)(i, j) = (*oldPixels)(oldi, oldj); else if (j == newWidth - 1) - newPixelArray(i, j) = oldPixels(oldi, oldj) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(oldi, oldj) / 2; else - newPixelArray(i, j) = oldPixels(oldi, oldj) / 2 + oldPixels(oldi, oldj + 1) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(oldi, oldj) / 2 + (*oldPixels)(oldi, oldj + 1) / 2; } } for (int i = 2; i < newHeight; i += 3) { if (i == newHeight - 1) for (int j = 0; j < newWidth; ++j) { - newPixelArray(i, j) = newPixelArray(i - 1, j) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i - 1, j) / 2; } else for (int j = 0; j < newWidth; ++j) { - newPixelArray(i, j) = newPixelArray(i - 1, j) / 2 + newPixelArray(i + 1, j) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i - 1, j) / 2 + (*newPixelArray)(i + 1, j) / 2; } } - return {newPixelArray}; + return std::make_shared(newPixelArray); } -BMPImage textImg(const std::u16string &str, Font *font, uint8_t scale, Pixel background_color, Pixel font_color) { +std::shared_ptr +textImg(const std::u16string &str, Font *font, uint8_t scale, Pixel background_color, Pixel font_color) { auto strSize = str.size(); uint32_t glyphHeight = font->glyphHeight; uint32_t glyphWidth = font->glyphWidth; uint32_t imgWidth = strSize * glyphWidth * scale; uint32_t imgHeight = glyphHeight * scale; - PixelArray pixels(imgWidth, imgHeight); + std::shared_ptr pixels = std::make_shared(imgWidth, imgHeight); for (int i = 0; i < strSize; ++i) { auto glyph = font->_glyphs[str[i]]; for (int j = 0; j < glyphHeight * scale; ++j) { for (int l = 0; l < glyphWidth * scale; ++l) { if (glyph->glyph[j / scale][l / scale]) - pixels(j, glyphWidth * scale * i + l) = font_color; + (*pixels)(j, glyphWidth * scale * i + l) = font_color; else - pixels(j, glyphWidth * scale * i + l) = background_color; + (*pixels)(j, glyphWidth * scale * i + l) = background_color; } } } - return {pixels}; + return std::make_shared(pixels); } -BMPImage upscale1_5x_ver2(BMPImage &img) { - auto oldPixels = img.pixels(); - const uint32_t newHeight = img.height() * 3 / 2; - const uint32_t newWidth = img.width() * 3 / 2; - PixelArray newPixelArray(newWidth, newHeight); +std::shared_ptr upscale1_5x_ver2(const std::shared_ptr& img) { + auto oldPixels = img->pixels(); + const uint32_t newHeight = img->height() * 3 / 2; + const uint32_t newWidth = img->width() * 3 / 2; + std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); for (int i = 0; i < newHeight; ++i) { - int oldi = std::round(i * 2 / 3.); + auto oldi = (int)std::round(i * 2 / 3.); if ((i + 1) % 3 == 0) { if (i == newHeight - 1) - newPixelArray(i, 0) = oldPixels(oldi, 0) / 2; + (*newPixelArray)(i, 0) = (*oldPixels)(oldi, 0) / 2; else { - newPixelArray(i, 0) = oldPixels(oldi, 0) / 2 + oldPixels(oldi + 1, 0) / 2; + (*newPixelArray)(i, 0) = (*oldPixels)(oldi, 0) / 2 + (*oldPixels)(oldi + 1, 0) / 2; } continue; } for (int j = 0; j < newWidth; ++j) { - int oldj = std::round(j * 2 / 3.); + auto oldj = (int)std::round(j * 2 / 3.); if ((j + 1) % 3 != 0) - newPixelArray(i, j) = oldPixels(oldi, oldj); + (*newPixelArray)(i, j) = (*oldPixels)(oldi, oldj); else if (j == newWidth - 1) - newPixelArray(i, j) = oldPixels(oldi, oldj) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(oldi, oldj) / 2; else - newPixelArray(i, j) = oldPixels(oldi, oldj) / 2 + oldPixels(oldi, oldj + 1) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(oldi, oldj) / 2 + (*oldPixels)(oldi, oldj + 1) / 2; } } for (int i = 2; i < newHeight; i += 3) { if (i == newHeight - 1) for (int j = 0; j < newWidth; ++j) { - newPixelArray(i, j) = newPixelArray(i - 1, j) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i - 1, j) / 2; } else for (int j = 1; j < newWidth; ++j) { if (j == newWidth - 1) { - newPixelArray(i, j) = newPixelArray(i, j - 1) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i, j - 1) / 2; continue; } - auto f00 = newPixelArray(i - 1, j - 1); - auto f10 = newPixelArray(i - 1, j + 1); - auto f01 = newPixelArray(i + 1, j - 1); - auto f11 = newPixelArray(i + 1, j + 1); - newPixelArray(i, j) = f10 / 4 + f00 / 4 + f01 / 4 + f11 / 4; + auto f00 = (*newPixelArray)(i - 1, j - 1); + auto f10 = (*newPixelArray)(i - 1, j + 1); + auto f01 = (*newPixelArray)(i + 1, j - 1); + auto f11 = (*newPixelArray)(i + 1, j + 1); + (*newPixelArray)(i, j) = f10 / 4 + f00 / 4 + f01 / 4 + f11 / 4; } } - return {newPixelArray}; + return std::make_shared(newPixelArray); } -BMPImage upscale2x_ver2(BMPImage &img) { - auto oldPixels = img.pixels(); - const uint32_t newHeight = img.height() * 2; - const uint32_t newWidth = img.width() * 2; - PixelArray newPixelArray(newWidth, newHeight); +std::shared_ptr upscale2x_ver2(const std::shared_ptr& img) { + auto oldPixels = img->pixels(); + const uint32_t newHeight = img->height() * 2; + const uint32_t newWidth = img->width() * 2; + std::shared_ptr newPixelArray = std::make_shared(newWidth, newHeight); for (int i = 0; i < newHeight; ++i) { if ((i + 1) % 2 == 0) { if (i == newHeight - 1) - newPixelArray(i, 0) = oldPixels(i / 2, 0) / 2; + (*newPixelArray)(i, 0) = (*oldPixels)(i / 2, 0) / 2; else - newPixelArray(i, 0) = oldPixels(i / 2, 0) / 2 + oldPixels(i / 2 + 1, 0) / 2; + (*newPixelArray)(i, 0) = (*oldPixels)(i / 2, 0) / 2 + (*oldPixels)(i / 2 + 1, 0) / 2; continue; } for (int j = 0; j < newWidth; ++j) { if (j % 2 == 0) - newPixelArray(i, j) = oldPixels(i / 2, j / 2); + (*newPixelArray)(i, j) = (*oldPixels)(i / 2, j / 2); else if (j == newWidth - 1) - newPixelArray(i, j) = oldPixels(i / 2, j / 2) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(i / 2, j / 2) / 2; else - newPixelArray(i, j) = oldPixels(i / 2, j / 2) / 2 + oldPixels(i / 2, j / 2 + 1) / 2; + (*newPixelArray)(i, j) = (*oldPixels)(i / 2, j / 2) / 2 + (*oldPixels)(i / 2, j / 2 + 1) / 2; } } for (int i = 1; i < newHeight; i += 2) { if (i == newHeight - 1) for (int j = 1; j < newWidth; ++j) { - newPixelArray(i, j) = newPixelArray(i - 1, j) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i - 1, j) / 2; } else for (int j = 1; j < newWidth; ++j) { if (j == newWidth - 1) { - newPixelArray(i, j) = newPixelArray(i, j - 1) / 2; + (*newPixelArray)(i, j) = (*newPixelArray)(i, j - 1) / 2; continue; } - auto f00 = newPixelArray(i - 1, j - 1); - auto f10 = newPixelArray(i - 1, j + 1); - auto f01 = newPixelArray(i + 1, j - 1); - auto f11 = newPixelArray(i + 1, j + 1); - newPixelArray(i, j) = f10 / 4 + f00 / 4 + f01 / 4 + f11 / 4; + auto f00 = (*newPixelArray)(i - 1, j - 1); + auto f10 = (*newPixelArray)(i - 1, j + 1); + auto f01 = (*newPixelArray)(i + 1, j - 1); + auto f11 = (*newPixelArray)(i + 1, j + 1); + (*newPixelArray)(i, j) = f10 / 4 + f00 / 4 + f01 / 4 + f11 / 4; } } - return {newPixelArray}; + return std::make_shared(newPixelArray); } diff --git a/image-lib/bmpimage.h b/image-lib/bmpimage.h index a098bef..d5078f0 100644 --- a/image-lib/bmpimage.h +++ b/image-lib/bmpimage.h @@ -39,21 +39,19 @@ struct BITMAPINFOHEADER { class BMPImage { BitmapFileHeader fileHeader; BITMAPINFOHEADER infoHeader; - PixelArray pixelArray; + std::shared_ptr pixelArray; public: - BMPImage(const BitmapFileHeader &fileHeader, const BITMAPINFOHEADER &infoHeader, const PixelArray &pixelArray); + BMPImage(const BitmapFileHeader &fileHeader, const BITMAPINFOHEADER &infoHeader, std::shared_ptr pixelArray); - BMPImage(const PixelArray &pixelArray); - - ~BMPImage(); + explicit BMPImage(std::shared_ptr pixelArray); [[nodiscard]] const uint32_t &width() const; [[nodiscard]] const uint32_t &height() const; - [[nodiscard]] PixelArray pixels(); + [[nodiscard]] std::shared_ptr pixels() const; - [[nodiscard]] PixelArray pixels_copy(); + [[nodiscard]] std::shared_ptr pixels_copy(); [[nodiscard]] BitmapFileHeader fileHeader_copy(); @@ -61,28 +59,28 @@ public: void save(const std::string &); - BMPImage appendRight(BMPImage &); + std::shared_ptr appendRight(const std::shared_ptr&); - BMPImage overlay(BMPImage &, uint32_t, uint32_t); + std::shared_ptr overlay(const std::shared_ptr&, uint32_t, uint32_t); - BMPImage applyFilter(const std::function&)>& filter); + std::shared_ptr applyFilter(const std::function&)>& filter); }; -BMPImage readBMPImage(const std::string &filename); +std::shared_ptr readBMPImage(const std::string &filename); -BMPImage grayscale(BMPImage &); +std::shared_ptr grayscale(const std::shared_ptr&); // TODO: Сделать const BMPImage -BMPImage invertColors(BMPImage &); +std::shared_ptr invertColors(const std::shared_ptr&); // TODO: Сделать const BMPImage -BMPImage upscale2x(BMPImage &); +std::shared_ptr upscale2x(const std::shared_ptr&); -BMPImage downscale2x(BMPImage &); +std::shared_ptr downscale2x(const std::shared_ptr&); -BMPImage upscale1_5x(BMPImage &); +std::shared_ptr upscale1_5x(const std::shared_ptr&); -BMPImage upscale1_5x_ver2(BMPImage &); // TODO: BAD +std::shared_ptr upscale1_5x_ver2(const std::shared_ptr&); // TODO: BAD -BMPImage upscale2x_ver2(BMPImage &); // TODO: BAD +std::shared_ptr upscale2x_ver2(const std::shared_ptr&); // TODO: BAD -BMPImage textImg(const std::u16string &, Font *font, uint8_t scale = 1, Pixel background_color = Pixel{0, 0, 0}, +std::shared_ptr textImg(const std::u16string &, Font *font, uint8_t scale = 1, Pixel background_color = Pixel{0, 0, 0}, Pixel font_color = Pixel{255, 255, 255}); \ No newline at end of file diff --git a/image-lib/pixelarray.cpp b/image-lib/pixelarray.cpp index 54d16c9..2535d8a 100644 --- a/image-lib/pixelarray.cpp +++ b/image-lib/pixelarray.cpp @@ -56,10 +56,10 @@ Pixel &PixelArray::operator()(const uint32_t &i, const uint32_t &j) { } PixelArray::~PixelArray() { -// for (int i = 0; i < this->_height; ++i) { -// delete[] this->array[i]; -// } -// delete[] this->array; + for (int i = 0; i < this->_height; ++i) { + delete[] this->array[i]; + } + delete[] this->array; } PixelArray::PixelArray(const PixelArray &pA) { diff --git a/image-lib/pixelarray.h b/image-lib/pixelarray.h index 8f32260..d8759ca 100644 --- a/image-lib/pixelarray.h +++ b/image-lib/pixelarray.h @@ -2,6 +2,7 @@ #include #include +static_assert(true); #pragma pack(push, 1) struct Pixel { uint8_t r = 255; diff --git a/main.cpp b/main.cpp index bdc67bf..b8d15c8 100644 --- a/main.cpp +++ b/main.cpp @@ -6,12 +6,12 @@ auto font = readPSF("../fonts/ruscii_8x16_2.psf"); void lab01() { auto og_image = readBMPImage("../elef.bmp"); - auto pixels1 = og_image.pixels_copy(); - auto pixels2 = og_image.pixels_copy(); - for (int i = 0; i < og_image.height(); ++i) { - for (int j = 0; j < og_image.width(); ++j) { - pixels1(i, j) = pixels1(i, j) / 2; - pixels2(i, j) = pixels2(i, j) * 2; + auto pixels1 = og_image->pixels_copy(); + auto pixels2 = og_image->pixels_copy(); + for (int i = 0; i < og_image->height(); ++i) { + for (int j = 0; j < og_image->width(); ++j) { + (*pixels1)(i, j) = (*pixels1)(i, j) / 2; + (*pixels2)(i, j) = (*pixels2)(i, j) * 2; } } auto orig_text = textImg(u"Оригинал", &font, 3); @@ -21,11 +21,12 @@ void lab01() { auto grayscale_text = textImg(u"Оттенки серого", &font, 3); auto darkenImg = BMPImage(pixels1).overlay(darken_text, 0, 0); auto lighterImg = BMPImage(pixels2).overlay(lighter_text, 0, 0); - auto invert_img = invertColors(og_image).overlay(invert_text, 0, 0); // TODO - auto grayscale_img = grayscale(og_image).overlay(grayscale_text, 0, 0); + auto invert_img = invertColors(og_image)->overlay(invert_text, 0, 0); // TODO + auto grayscale_img = grayscale(og_image)->overlay(grayscale_text, 0, 0); - og_image.appendRight(lighterImg).appendRight(darkenImg).appendRight(invert_img).appendRight(grayscale_img).overlay( - orig_text, 0, 0).save("../lab01/elef1.bmp"); + og_image->appendRight(lighterImg)->appendRight(darkenImg)->appendRight(invert_img)->appendRight( + grayscale_img)->overlay( + orig_text, 0, 0)->save("../lab01/elef1.bmp"); } void lab02_01() { @@ -37,24 +38,24 @@ void lab02_01() { auto img_512 = downscale2x(img); auto img_256 = downscale2x(img_512); auto img_128 = downscale2x(img_256); - img_512.overlay(text512, 0, 0).save("../lab02/tea_512.bmp"); - img_256.overlay(text256, 0, 0).save("../lab02/tea_256.bmp"); - img_128.overlay(text128, 0, 0).save("../lab02/tea_128.bmp"); - img.overlay(text1024, 0, 0).appendRight(img_512).overlay(text512, 1024, 0).appendRight(img_256). - overlay(text256, 1536, 0).appendRight(img_128).overlay(text128, 1792, 0).save( - "../lab02/tea_downscale.bmp");; - auto img_from_512 = upscale2x(img_512).overlay(text512, 0, 0); + img_512->overlay(text512, 0, 0)->save("../lab02/tea_512.bmp"); + img_256->overlay(text256, 0, 0)->save("../lab02/tea_256.bmp"); + img_128->overlay(text128, 0, 0)->save("../lab02/tea_128.bmp"); + img->overlay(text1024, 0, 0)->appendRight(img_512)->overlay(text512, 1024, 0)->appendRight(img_256)-> + overlay(text256, 1536, 0)->appendRight(img_128)->overlay(text128, 1792, 0)->save( + "../lab02/tea_downscale.bmp"); + auto img_from_512 = upscale2x(img_512)->overlay(text512, 0, 0); //// auto img_from_256 = upscale2x(upscale2x(img_256)); //// auto img_from_128 = upscale2x(upscale2x(upscale2x(img_128))); auto tmp = upscale2x(img_256); - auto img_from_256 = upscale2x(tmp).overlay(text256, 0, 0); + auto img_from_256 = upscale2x(tmp)->overlay(text256, 0, 0); auto tmp2 = upscale2x(img_128); auto tmp3 = upscale2x(tmp2); - auto img_from_128 = upscale2x(tmp3).overlay(text128, 0, 0); - img_from_512.save("../lab02/tea_from_512.bmp"); - img_from_256.save("../lab02/tea_from_256.bmp"); - img_from_128.save("../lab02/tea_from_128.bmp"); - img.overlay(text1024, 0, 0).appendRight(img_from_512).appendRight(img_from_256).appendRight(img_from_128).save( + auto img_from_128 = upscale2x(tmp3)->overlay(text128, 0, 0); + img_from_512->save("../lab02/tea_from_512.bmp"); + img_from_256->save("../lab02/tea_from_256.bmp"); + img_from_128->save("../lab02/tea_from_128.bmp"); + img->overlay(text1024, 0, 0)->appendRight(img_from_512)->appendRight(img_from_256)->appendRight(img_from_128)->save( "../lab02/tea_upscale.bmp"); // newImg2->save("../lab02/tea_upscale.bmp"); } @@ -67,35 +68,35 @@ void lab02_02() { auto text64 = textImg(u"64 оттенка", &font, 3); auto text32 = textImg(u"32 оттенка", &font, 3); auto gsImg = grayscale(img); - gsImg.overlay(text256, 0, 0).save("../lab02/tea_grayscale_256.bmp"); - auto gsPixels = gsImg.pixels_copy(); - auto width = gsImg.width(); - auto height = gsImg.height(); + gsImg->overlay(text256, 0, 0)->save("../lab02/tea_grayscale_256.bmp"); + auto gsPixels = gsImg->pixels_copy(); + auto width = gsImg->width(); + auto height = gsImg->height(); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { - gsPixels(i, j) = (gsPixels(i, j) / 2) * 2; + (*gsPixels)(i, j) = ((*gsPixels)(i, j) / 2) * 2; } } auto gsImg128 = BMPImage(gsPixels).overlay(text128, 0, 0); - gsPixels = gsImg.pixels_copy(); + gsPixels = gsImg->pixels_copy(); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { - gsPixels(i, j) = (gsPixels(i, j) / 4) * 4; + (*gsPixels)(i, j) = ((*gsPixels)(i, j) / 4) * 4; } } auto gsImg64 = BMPImage(gsPixels).overlay(text64, 0, 0); - gsPixels = gsImg.pixels_copy(); + gsPixels = gsImg->pixels_copy(); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { - gsPixels(i, j) = (gsPixels(i, j) / 8) * 8; + (*gsPixels)(i, j) = ((*gsPixels)(i, j) / 8) * 8; } } auto gsImg32 = BMPImage(gsPixels).overlay(text32, 0, 0); - gsImg128.save("../lab02/tea_grayscale_128.bmp"); - gsImg64.save("../lab02/tea_grayscale_64.bmp"); - gsImg32.save("../lab02/tea_grayscale_32.bmp"); - img.overlay(ogText, 0, 0).appendRight(gsImg).overlay(text256, 1024, 0).appendRight(gsImg128).appendRight( - gsImg64).appendRight(gsImg32).save("../lab02/tea_grayscale.bmp"); + gsImg128->save("../lab02/tea_grayscale_128.bmp"); + gsImg64->save("../lab02/tea_grayscale_64.bmp"); + gsImg32->save("../lab02/tea_grayscale_32.bmp"); + img->overlay(ogText, 0, 0)->appendRight(gsImg)->overlay(text256, 1024, 0)->appendRight(gsImg128)->appendRight( + gsImg64)->appendRight(gsImg32)->save("../lab02/tea_grayscale.bmp"); } void lab02_03() { @@ -113,17 +114,17 @@ void lab02_03() { auto img_2_ver2 = upscale2x_ver2(img); auto img_3 = upscale2x(img_1_5); auto img_3_ver2 = upscale2x_ver2(img_1_5_ver2); - img_1_5.overlay(text_avrg, 0, 0).appendRight(img_1_5_ver2).overlay(text_lin, 1536, 0).save( + img_1_5->overlay(text_avrg, 0, 0)->appendRight(img_1_5_ver2)->overlay(text_lin, 1536, 0)->save( "../lab02/lab02_03/tea_x1_5.bmp"); - img_2.overlay(text_avrg2, 0, 0).appendRight(img_2_ver2).overlay(text_lin2, 2048, 0).save( + img_2->overlay(text_avrg2, 0, 0)->appendRight(img_2_ver2)->overlay(text_lin2, 2048, 0)->save( "../lab02/lab02_03/tea_x2.bmp"); - img_3.overlay(text_avrg3, 0, 0).appendRight(img_3_ver2).overlay(text_lin3, 3072, 0).save( + img_3->overlay(text_avrg3, 0, 0)->appendRight(img_3_ver2)->overlay(text_lin3, 3072, 0)->save( "../lab02/lab02_03/tea_x3.bmp"); - img.overlay(text, 0, 0).appendRight(img_1_5).overlay(text_avrg, 1024, 0).appendRight(img_2) - .overlay(text_avrg2, 2560, 0).appendRight(img_3).overlay(text_avrg3, 4608, 0).save( + img->overlay(text, 0, 0)->appendRight(img_1_5)->overlay(text_avrg, 1024, 0)->appendRight(img_2) + ->overlay(text_avrg2, 2560, 0)->appendRight(img_3)->overlay(text_avrg3, 4608, 0)->save( "../lab02/lab02_03/tea_upscale.bmp"); - img.overlay(text, 0, 0).appendRight(img_1_5_ver2).overlay(text_lin, 1024, 0).appendRight(img_2_ver2) - .overlay(text_lin, 2560, 0).appendRight(img_3_ver2).overlay(text_lin, 4608, 0).save( + img->overlay(text, 0, 0)->appendRight(img_1_5_ver2)->overlay(text_lin, 1024, 0)->appendRight(img_2_ver2) + ->overlay(text_lin, 2560, 0)->appendRight(img_3_ver2)->overlay(text_lin, 4608, 0)->save( "../lab02/lab02_03/tea_upscale_ver2.bmp"); } @@ -137,53 +138,48 @@ void lab03() { auto sobel_text = textImg(u"Фильтр Собеля", &font, 3); auto text = textImg(u"Оригинал", &font, 3); - auto avrg_image = imgGS.applyFilter(averageFilter).overlay(avrg_text, 0, 0); - auto median_image = imgGS.applyFilter(medianFilter).overlay(median_text, 0, 0); - auto prewitt_dx_image = imgGS.applyFilter(prewittDXFilter); - auto prewitt_dy_image = imgGS.applyFilter(prewittDYFilter); - prewitt_dx_image.save("../lab03/prewitt1.bmp"); - prewitt_dy_image.save("../lab03/prewitt2.bmp"); - PixelArray prewittPixels(prewitt_dx_image.width(), prewitt_dy_image.height()); - for (int y = 0; y < prewitt_dx_image.height(); ++y) { - for (int x = 0; x < prewitt_dx_image.width(); ++x) { - prewittPixels(y, x) = prewitt_dx_image.pixels()(y, x) + prewitt_dy_image.pixels()(y, x); + auto avrg_image = imgGS->applyFilter(averageFilter)->overlay(avrg_text, 0, 0); + auto median_image = imgGS->applyFilter(medianFilter)->overlay(median_text, 0, 0); + auto prewitt_dx_image = imgGS->applyFilter(prewittDXFilter); + auto prewitt_dy_image = imgGS->applyFilter(prewittDYFilter); + prewitt_dx_image->save("../lab03/prewitt1.bmp"); + prewitt_dy_image->save("../lab03/prewitt2.bmp"); + std::shared_ptr prewittPixels = std::make_shared(prewitt_dx_image->width(), prewitt_dy_image->height()); + for (int y = 0; y < prewitt_dx_image->height(); ++y) { + for (int x = 0; x < prewitt_dx_image->width(); ++x) { + (*prewittPixels)(y, x) = (*prewitt_dx_image->pixels())(y, x) + (*prewitt_dy_image->pixels())(y, x); } } auto prewitt_image = BMPImage(prewittPixels).overlay(prewitt_text, 0, 0); - auto sobel_dx_image = imgGS.applyFilter(sobelDXFilter).overlay(sobel_text, 0, 0); - auto sobel_dy_image2 = imgGS.applyFilter(sobelDYFilter).overlay(sobel_text, 0, 0); - PixelArray sobelPixels(sobel_dx_image.width(), sobel_dx_image.height()); - for (int y = 0; y < sobel_dx_image.height(); ++y) { - for (int x = 0; x < sobel_dx_image.width(); ++x) { - sobelPixels(y, x) = sobel_dx_image.pixels()(y, x) + sobel_dy_image2.pixels()(y, x); + auto sobel_dx_image = imgGS->applyFilter(sobelDXFilter)->overlay(sobel_text, 0, 0); + auto sobel_dy_image2 = imgGS->applyFilter(sobelDYFilter)->overlay(sobel_text, 0, 0); + std::shared_ptr sobelPixels = std::make_shared(sobel_dx_image->width(), sobel_dx_image->height()); + for (int y = 0; y < sobel_dx_image->height(); ++y) { + for (int x = 0; x < sobel_dx_image->width(); ++x) { + (*sobelPixels)(y, x) = (*sobel_dx_image->pixels())(y, x) + (*sobel_dy_image2->pixels())(y, x); } } auto sobel_image = BMPImage(sobelPixels).overlay(sobel_text, 0, 0); - imgGS.overlay(text, 0, 0).appendRight(avrg_image).appendRight(median_image).save("../lab03/avrg_median_filter.bmp"); - imgGS.overlay(text, 0, 0).appendRight(prewitt_image).save("../lab03/prewitt_filter.bmp"); - imgGS.overlay(text, 0, 0).appendRight(sobel_image).save("../lab03/sobel_filter.bmp"); + imgGS->overlay(text, 0, 0)->appendRight(avrg_image)->appendRight(median_image)->save("../lab03/avrg_median_filter.bmp"); + imgGS->overlay(text, 0, 0)->appendRight(prewitt_image)->save("../lab03/prewitt_filter.bmp"); + imgGS->overlay(text, 0, 0)->appendRight(sobel_image)->save("../lab03/sobel_filter.bmp"); } void test() { -// Pixel **test; - PixelArray test(3, 3); -// test = new Pixel *[3]; -// test[0] = new Pixel[3]; -// test[1] = new Pixel[3]; -// test[2] = new Pixel[3]; - test(0, 0) = Pixel{0, 0, 0}; - test(0, 1) = Pixel{127, 127, 127}; - test(0, 2) = Pixel{255, 255, 255}; - test(1, 0) = Pixel{127, 127, 127}; - test(1, 1) = Pixel{63, 63, 63}; - test(1, 2) = Pixel{127, 127, 127}; - test(2, 0) = Pixel{255, 255, 255}; - test(2, 1) = Pixel{127, 127, 127}; - test(2, 2) = Pixel{0, 0, 0}; - auto test_img = BMPImage(test); - test_img.save("../test.bmp"); + std::shared_ptr test = std::make_shared(3, 3); + (*test)(0, 0) = Pixel{0, 0, 0}; + (*test)(0, 1) = Pixel{127, 127, 127}; + (*test)(0, 2) = Pixel{255, 255, 255}; + (*test)(1, 0) = Pixel{127, 127, 127}; + (*test)(1, 1) = Pixel{63, 63, 63}; + (*test)(1, 2) = Pixel{127, 127, 127}; + (*test)(2, 0) = Pixel{255, 255, 255}; + (*test)(2, 1) = Pixel{127, 127, 127}; + (*test)(2, 2) = Pixel{0, 0, 0}; + auto test_img = std::make_shared(test); + test_img->save("../test.bmp"); auto test_scale = upscale1_5x_ver2(test_img); - test_scale.save("../test_scale.bmp"); + test_scale->save("../test_scale.bmp"); } void test2() { @@ -195,10 +191,10 @@ void test2() { uint32_t imgWidth = 16 * glyph->width; uint32_t imgHeight = 16 * glyph->height; - PixelArray test(imgWidth, imgHeight); + std::shared_ptr test = std::make_shared(imgWidth, imgHeight); std::u16string str = u"Hello, World! Привет, Мир!"; uint32_t k = 0; - for (auto it: font._glyphs) { + for (const auto &it: font._glyphs) { if (k == 256) continue; // glyph = font._glyphs[u'☺']; // glyph = font._glyphs[str[k % str.size()]]; @@ -206,9 +202,9 @@ void test2() { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (glyph->glyph[i][j]) - test((k / 16) * h + i, (k % 16) * w + j) = font_color; + (*test)((k / 16) * h + i, (k % 16) * w + j) = font_color; else - test((k / 16) * h + i, (k % 16) * w + j) = background_color; + (*test)((k / 16) * h + i, (k % 16) * w + j) = background_color; } } k++; @@ -217,11 +213,11 @@ void test2() { } void test3() { - textImg(u"Привет, Мир!", &font).save("../font_test.bmp"); + textImg(u"Привет, Мир!", &font)->save("../font_test.bmp"); } int main() { - //lab01(); +// lab01(); // lab02_01(); // lab02_02(); // lab02_03(); @@ -229,6 +225,5 @@ int main() { // test(); // test2(); // test3(); -// auto t = readBMPImage("../lab02/lab02_03/tea_x3.bmp"); return 0; }