cpp-bmp-reader/main.cpp

229 lines
10 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <image-lib/bmpimage.h>
#include <image-lib/filters.h>
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 orig_text = textImg(u"Оригинал", &font, 3);
auto darken_text = textImg(u"Темнее", &font, 3);
auto lighter_text = textImg(u"Светлее", &font, 3);
auto invert_text = textImg(u"Инвертирован", &font, 3);
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);
og_image->appendRight(lighterImg)->appendRight(darkenImg)->appendRight(invert_img)->appendRight(
grayscale_img)->overlay(
orig_text, 0, 0)->save("../lab01/elef1.bmp");
}
void lab02_01() {
auto img = readBMPImage("../tea.bmp");
auto text1024 = textImg(u"1024", &font, 3);
auto text512 = textImg(u"512", &font, 3);
auto text256 = textImg(u"256", &font, 3);
auto text128 = textImg(u"128", &font, 3);
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);
//// 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 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(
"../lab02/tea_upscale.bmp");
// newImg2->save("../lab02/tea_upscale.bmp");
}
void lab02_02() {
auto img = readBMPImage("../tea.bmp");
auto ogText = textImg(u"Оригинал", &font, 3);
auto text256 = textImg(u"256 оттенков", &font, 3);
auto text128 = textImg(u"128 оттенков", &font, 3);
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();
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
(*gsPixels)(i, j) = ((*gsPixels)(i, j) / 2) * 2;
}
}
auto gsImg128 = BMPImage(gsPixels).overlay(text128, 0, 0);
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;
}
}
auto gsImg64 = BMPImage(gsPixels).overlay(text64, 0, 0);
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;
}
}
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");
}
void lab02_03() {
auto img = readBMPImage("../tea.bmp");
auto text = textImg(u"Оригинал", &font, 3);
auto text_avrg = textImg(u"Среднее 1.5x", &font, 5);
auto text_avrg2 = textImg(u"Среднее 2x", &font, 6);
auto text_avrg3 = textImg(u"Среднее 3x", &font, 9);
auto text_lin = textImg(u"Линейное 1.5x", &font, 5);
auto text_lin2 = textImg(u"Линейное 2x", &font, 6);
auto text_lin3 = textImg(u"Линейное 3x", &font, 9);
auto img_1_5 = upscale1_5x(img);
auto img_1_5_ver2 = upscale1_5x_ver2(img);
auto img_2 = upscale2x(img);
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(
"../lab02/lab02_03/tea_x1_5.bmp");
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(
"../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(
"../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(
"../lab02/lab02_03/tea_upscale_ver2.bmp");
}
void lab03() {
auto img = readBMPImage("../bike.bmp");
auto imgGS = grayscale(img);
auto avrg_text = textImg(u"Осреднение", &font, 3);
auto median_text = textImg(u"Медиана", &font, 3);
auto prewitt_text = textImg(u"Фильтр Превитта", &font, 3);
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");
std::shared_ptr<PixelArray> prewittPixels = std::make_shared<PixelArray>(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);
std::shared_ptr<PixelArray> sobelPixels = std::make_shared<PixelArray>(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");
}
void test() {
std::shared_ptr<PixelArray> test = std::make_shared<PixelArray>(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<BMPImage>(test);
test_img->save("../test.bmp");
auto test_scale = upscale1_5x_ver2(test_img);
test_scale->save("../test_scale.bmp");
}
void test2() {
auto background_color = Pixel{0, 0, 0};
auto font_color = Pixel{255, 255, 255};
auto glyph = font._glyphs[u'б'];
auto w = glyph->width;
auto h = glyph->height;
uint32_t imgWidth = 16 * glyph->width;
uint32_t imgHeight = 16 * glyph->height;
std::shared_ptr<PixelArray> test = std::make_shared<PixelArray>(imgWidth, imgHeight);
std::u16string str = u"Hello, World! Привет, Мир!";
uint32_t k = 0;
for (const auto &it: font._glyphs) {
if (k == 256) continue;
// glyph = font._glyphs[u'☺'];
// glyph = font._glyphs[str[k % str.size()]];
glyph = it.second;
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;
else
(*test)((k / 16) * h + i, (k % 16) * w + j) = background_color;
}
}
k++;
}
BMPImage(test).save("test_font.bmp");
}
void test3() {
textImg(u"Привет, Мир!", &font)->save("../font_test.bmp");
}
int main() {
// lab01();
// lab02_01();
// lab02_02();
// lab02_03();
lab03();
// test();
// test2();
// test3();
return 0;
}