Создание изображения из текста и накладывание одного изображения на другое.

This commit is contained in:
Евгений Титаренко 2024-04-11 09:47:52 +03:00
parent 9af7778b77
commit 38655e9c1b
5 changed files with 69 additions and 27 deletions

View file

@ -94,11 +94,20 @@ BMPImage BMPImage::appendRight(BMPImage &img) {
return {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())
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);
}
return {pixels};
}
BMPImage readBMPImage(const std::string &filename) {
BitmapFileHeader bitmapFileHeader;
BITMAPINFOHEADER bitmapInfoHeader;
// Pixel **pixelArray;
uint32_t DIB_Header_Size;
{
std::ifstream ifs(filename, std::ios_base::binary);
@ -207,13 +216,10 @@ Pixel operator-(const Pixel &p, const uint8_t &n) {
BMPImage upscale2x(BMPImage &img) {
auto oldPixels = img.pixels();
// Pixel **newPixelArray;
const uint32_t newHeight = img.height() * 2;
const uint32_t newWidth = img.width() * 2;
// newPixelArray = new Pixel *[newHeight];
PixelArray newPixelArray(newWidth, newHeight);
for (int i = 0; i < newHeight; i += 2) {
// newPixelArray(i) = new Pixel[newWidth];
for (int j = 0; j < newWidth; ++j) {
if (j % 2 == 0)
newPixelArray(i, j) = oldPixels(i / 2, j / 2);
@ -224,7 +230,6 @@ BMPImage upscale2x(BMPImage &img) {
}
}
for (int i = 1; i < newHeight; i += 2) {
// newPixelArray(i) = new Pixel[newWidth];
if (i == newHeight - 1)
for (int j = 0; j < newWidth; ++j) {
newPixelArray(i, j) = newPixelArray(i - 1, j) / 2;
@ -235,35 +240,27 @@ BMPImage upscale2x(BMPImage &img) {
}
}
return {newPixelArray};
// return BMPImage(newPixelArray, newWidth, newHeight);
}
BMPImage downscale2x(BMPImage &img) {
// Pixel **newPixelArray;
auto oldPixels = img.pixels_copy();
const uint32_t newHeight = img.height() / 2;
const uint32_t newWidth = img.width() / 2;
PixelArray newPixelArray(newWidth, newHeight);
// newPixelArray = new Pixel *[newHeight];
for (int i = 0; i < newHeight; ++i) {
// newPixelArray(i) = new Pixel[newWidth];
for (int j = 0; j < newWidth; ++j) {
newPixelArray(i, j) = oldPixels(i * 2, j * 2);
}
}
// return BMPImage(newPixelArray, newWidth, newHeight);
return {newPixelArray};
}
BMPImage upscale1_5x(BMPImage &img) {
auto oldPixels = img.pixels();
// Pixel **newPixelArray;
const uint32_t newHeight = img.height() * 3 / 2;
const uint32_t newWidth = img.width() * 3 / 2;
PixelArray newPixelArray(newWidth, newHeight);
// newPixelArray = new Pixel *[newHeight];
for (int i = 0; i < newHeight; ++i) {
// newPixelArray(i) = new Pixel[newWidth];
if ((i + 1) % 3 == 0) continue;
for (int j = 0; j < newWidth; ++j) {
int oldi = i * 2 / 3;
@ -290,6 +287,28 @@ BMPImage upscale1_5x(BMPImage &img) {
}
BMPImage 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);
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;
else
pixels(j, glyphWidth * scale * i + l) = background_color;
}
}
}
return {pixels};
}
PixelArray::PixelArray(uint32_t width, uint32_t height) {
this->_width = width;
this->_height = height;