shushus

Спринт 15. Основы синтаксиса CMake.

May 31st, 2023
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.70 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. CMakeLists.txt
  3. -----------------------------------------------------------------------
  4. cmake_minimum_required(VERSION 3.11)
  5.  
  6. project(Negate CXX)
  7. set(CMAKE_CXX_STANDARD 17)
  8.  
  9. set(IMGLIB_FILES img_lib.h img_lib.cpp
  10.                  ppm_image.h ppm_image.cpp)
  11.  
  12. if(CMAKE_SYSTEM_NAME MATCHES "^MINGW")
  13.     set(SYSTEM_LIBS -lstdc++)
  14. else()
  15.     set(SYSTEM_LIBS)
  16. endif()
  17.  
  18. add_executable("hmirror" "hmirr.cpp" ${IMGLIB_FILES})
  19. add_executable("vmirror" "vmirr.cpp" ${IMGLIB_FILES})
  20. add_executable("sobel" "sobel.cpp" ${IMGLIB_FILES})
  21. -----------------------------------------------------------------------
  22. hmirr.cpp
  23. -----------------------------------------------------------------------
  24. #include "ppm_image.h"
  25.  
  26. #include <iostream>
  27. #include <string_view>
  28.  
  29. using namespace std;
  30.  
  31. void HMirrInplace(img_lib::Image& image) {
  32.    
  33.     int height = image.GetHeight();
  34.     int middle = image.GetWidth()/2;
  35.    
  36.     for (int i = 0; i < height; ++i) {
  37.        
  38.         for (int j = 0; j < middle; ++j) {
  39.            
  40.             std::swap(image.GetPixel(j, i).r, image.GetPixel(image.GetWidth() - j - 1, i).r);
  41.             std::swap(image.GetPixel(j, i).g, image.GetPixel(image.GetWidth() - j - 1, i).g);
  42.             std::swap(image.GetPixel(j, i).b, image.GetPixel(image.GetWidth() - j - 1, i).b);
  43.         }
  44.     }
  45. }
  46.  
  47. int main(int argc, const char** argv) {
  48.    
  49.     if (argc != 3) {
  50.         cerr << "Usage: "sv << argv[0] << " <input image> <output image>"sv << endl;
  51.         return 1;
  52.     }
  53.  
  54.     auto image = img_lib::LoadPPM(argv[1]);
  55.     if (!image) {
  56.         cerr << "Error loading image"sv << endl;
  57.         return 2;
  58.     }
  59.  
  60.     HMirrInplace(image);
  61.  
  62.     if (!img_lib::SavePPM(argv[2], image)) {
  63.         cerr << "Error saving image"sv << endl;
  64.         return 3;
  65.     }
  66.  
  67.     cout << "Image saved successfully!"sv << endl;
  68. }
  69. -----------------------------------------------------------------------
  70. vmirr.cpp
  71. -----------------------------------------------------------------------
  72. #include "ppm_image.h"
  73.  
  74. #include <algorithm>
  75. #include <iostream>
  76. #include <string_view>
  77.  
  78. using namespace std;
  79.  
  80. void VMirrInplace(img_lib::Image& image) {
  81.    
  82.     int width = image.GetWidth();
  83.     int middle = image.GetHeight()/2;
  84.    
  85.     for (int i = 0; i < middle; ++i) {
  86.        
  87.         for (int j = 0; j < width; ++j) {
  88.            
  89.             std::swap(image.GetPixel(j, i).r, image.GetPixel(j, image.GetHeight() - i - 1).r);
  90.             std::swap(image.GetPixel(j, i).g, image.GetPixel(j, image.GetHeight() - i - 1).g);
  91.             std::swap(image.GetPixel(j, i).b, image.GetPixel(j, image.GetHeight() - i - 1).b);
  92.         }
  93.     }
  94. }
  95.  
  96. int main(int argc, const char** argv) {
  97.    
  98.     if (argc != 3) {
  99.         cerr << "Usage: "sv << argv[0] << " <input image> <output image>"sv << endl;
  100.         return 1;
  101.     }
  102.  
  103.     auto image = img_lib::LoadPPM(argv[1]);
  104.     if (!image) {
  105.         cerr << "Error loading image"sv << endl;
  106.         return 2;
  107.     }
  108.  
  109.     VMirrInplace(image);
  110.  
  111.     if (!img_lib::SavePPM(argv[2], image)) {
  112.         cerr << "Error saving image"sv << endl;
  113.         return 3;
  114.     }
  115.  
  116.     cout << "Image saved successfully!"sv << endl;
  117. }
  118. -----------------------------------------------------------------------
  119. sobel.cpp
  120. -----------------------------------------------------------------------
  121. #include "ppm_image.h"
  122.  
  123. #include <algorithm>
  124. #include <array>
  125. #include <cmath>
  126. #include <iostream>
  127. #include <string_view>
  128.  
  129. using namespace std;
  130.  
  131. int Sum(img_lib::Color c) {return to_integer<int>(c.r) + to_integer<int>(c.g) + to_integer<int>(c.b);}
  132.  
  133. img_lib::Image Sobel(const img_lib::Image& image) {
  134.    
  135.     img_lib::Color color = img_lib::Color::Black();
  136.     int width = image.GetWidth() - 1;
  137.     int height = image.GetHeight() - 1;
  138.    
  139.     img_lib::Image result(width + 1, height + 1, color);
  140.    
  141.     for (int i = 1; i < height; ++i) {
  142.        
  143.         for (int j = 1; j < width; ++j) {
  144.            
  145.             int tl = Sum(image.GetPixel(j - 1, i - 1));
  146.             int tc = Sum(image.GetPixel(j, i - 1));
  147.             int tr = Sum(image.GetPixel(j + 1, i - 1));
  148.            
  149.             int cl = Sum(image.GetPixel(j - 1, i));
  150.             int cr = Sum(image.GetPixel(j + 1, i));
  151.  
  152.             int bl = Sum(image.GetPixel(j - 1, i + 1));
  153.             int bc = Sum(image.GetPixel(j, i + 1));
  154.             int br = Sum(image.GetPixel(j + 1, i + 1));
  155.  
  156.             int gx = -tl - 2 * tc - tr + bl + 2 * bc + br;
  157.             int gy = -tl - 2 * cl - bl + tr + 2 * cr + br;
  158.  
  159.             byte helper = byte(std::clamp<double>(std::sqrt(gx * gx + gy * gy), 0, 255));
  160.  
  161.             result.GetPixel(j, i).r = helper;
  162.             result.GetPixel(j, i).g = helper;
  163.             result.GetPixel(j, i).b = helper;
  164.         }
  165.     }
  166.  
  167.     return result;
  168. }
  169.  
  170. int main(int argc, const char** argv) {
  171.    
  172.     if (argc != 3) {
  173.         cerr << "Usage: "sv << argv[0] << " <input image> <output image>"sv << endl;
  174.         return 1;
  175.     }
  176.  
  177.     auto image = img_lib::LoadPPM(argv[1]);
  178.     if (!image) {
  179.         cerr << "Error loading image"sv << endl;
  180.         return 2;
  181.     }
  182.  
  183.     image = Sobel(image);
  184.  
  185.     if (!img_lib::SavePPM(argv[2], image)) {
  186.         cerr << "Error saving image"sv << endl;
  187.         return 3;
  188.     }
  189.  
  190.     cout << "Image saved successfully!"sv << endl;
  191. }
  192. -----------------------------------------------------------------------
  193. img_lib.h
  194. -----------------------------------------------------------------------
  195. #pragma once
  196.  
  197. #include <array>
  198. #include <cassert>
  199. #include <cstddef>
  200. #include <vector>
  201.  
  202. namespace img_lib {
  203.  
  204. struct Size {
  205.     int width;
  206.     int height;
  207. };
  208.  
  209. struct Color {
  210.    
  211.     static Color Black() {
  212.        
  213.         return {std::byte{0},
  214.                 std::byte{0},
  215.                 std::byte{0},
  216.                 std::byte{255}};
  217.     }
  218.  
  219.     std::byte r, g, b, a;
  220. };
  221.  
  222. class Image {
  223. public:
  224.  
  225.     Image() = default;
  226.  
  227.     Image(int w, int h, Color fill);
  228.  
  229.     Color GetPixel(int x, int y) const {return const_cast<Image*>(this)->GetPixel(x, y);}
  230.    
  231.     Color& GetPixel(int x, int y) {
  232.         assert(x < GetWidth() && y < GetHeight() && x >= 0 && y >= 0);
  233.         return GetLine(y)[x];
  234.     }
  235.  
  236.     Color* GetLine(int y);
  237.     const Color* GetLine(int y) const;
  238.  
  239.     int GetWidth() const;
  240.     int GetHeight() const;
  241.  
  242.     int GetStep() const;
  243.  
  244.     explicit operator bool() const {return GetWidth() > 0 && GetHeight() > 0;}
  245.     bool operator!() const {return !operator bool();}
  246.  
  247. private:
  248.     int width_ = 0;
  249.     int height_ = 0;
  250.     int step_;
  251.  
  252.     std::vector<Color> pixels_;
  253. };
  254.  
  255. }//end namespace img_lib
  256. -----------------------------------------------------------------------
  257. img_lib.cpp
  258. -----------------------------------------------------------------------
  259. #include "img_lib.h"
  260.  
  261. namespace img_lib {
  262.  
  263. Image::Image(int w, int h, Color fill) : width_(w)
  264.                                        , height_(h)
  265.                                        , step_(w)
  266.                                        , pixels_(step_ * height_, fill) {}
  267.  
  268. Color* Image::GetLine(int y) {
  269.     assert(y >= 0 && y < height_);
  270.     return pixels_.data() + step_ * y;
  271. }
  272.  
  273. const Color* Image::GetLine(int y) const {return const_cast<Image*>(this)->GetLine(y);}
  274. int Image::GetWidth() const {return width_;}
  275. int Image::GetHeight() const {return height_;}
  276. int Image::GetStep() const {return step_;}
  277.  
  278. }//end namespace img_lib
  279. -----------------------------------------------------------------------
  280. ppm_image.h
  281. -----------------------------------------------------------------------
  282. #pragma once
  283. #include "img_lib.h"
  284.  
  285. #include <filesystem>
  286.  
  287. namespace img_lib {
  288.    
  289. using Path = std::filesystem::path;
  290.  
  291. bool SavePPM(const Path& file, const Image& image);
  292. Image LoadPPM(const Path& file);
  293.  
  294. }//end namespace img_lib
  295. -----------------------------------------------------------------------
  296. ppm_image.cpp
  297. -----------------------------------------------------------------------
  298. #include "ppm_image.h"
  299.  
  300. #include <array>
  301. #include <fstream>
  302. #include <string_view>
  303.  
  304. using namespace std;
  305.  
  306. namespace img_lib {
  307.  
  308. static const string_view PPM_SIG = "P6"sv;
  309. static const int PPM_MAX = 255;
  310.  
  311. bool SavePPM(const Path& file, const Image& image) {
  312.     ofstream out(file, ios::binary);
  313.  
  314.     out << PPM_SIG << '\n' << image.GetWidth() << ' ' << image.GetHeight() << '\n' << PPM_MAX << '\n';
  315.  
  316.     const int w = image.GetWidth();
  317.     const int h = image.GetHeight();
  318.     std::vector<char> buff(w * 3);
  319.  
  320.     for (int y = 0; y < h; ++y) {
  321.        
  322.         const Color* line = image.GetLine(y);
  323.        
  324.         for (int x = 0; x < w; ++x) {
  325.             buff[x * 3 + 0] = static_cast<char>(line[x].r);
  326.             buff[x * 3 + 1] = static_cast<char>(line[x].g);
  327.             buff[x * 3 + 2] = static_cast<char>(line[x].b);
  328.         }
  329.        
  330.         out.write(buff.data(), w * 3);
  331.     }
  332.  
  333.     return out.good();
  334. }
  335.  
  336. Image LoadPPM(const Path& file) {
  337.     ifstream ifs(file, ios::binary);
  338.     std::string sign;
  339.     int w, h, color_max;
  340.  
  341.     ifs >> sign >> w >> h >> color_max;
  342.  
  343.     if (sign != PPM_SIG || color_max != PPM_MAX) {return {};}
  344.  
  345.     const char next = ifs.get();
  346.     if (next != '\n') {return {};}
  347.  
  348.     Image result(w, h, Color::Black());
  349.     std::vector<char> buff(w * 3);
  350.  
  351.     for (int y = 0; y < h; ++y) {
  352.        
  353.         Color* line = result.GetLine(y);
  354.         ifs.read(buff.data(), w * 3);
  355.  
  356.         for (int x = 0; x < w; ++x) {
  357.             line[x].r = static_cast<byte>(buff[x * 3 + 0]);
  358.             line[x].g = static_cast<byte>(buff[x * 3 + 1]);
  359.             line[x].b = static_cast<byte>(buff[x * 3 + 2]);
  360.         }
  361.     }
  362.  
  363.     return result;
  364. }
  365.  
  366. }//end namespace img_lib
Advertisement
Add Comment
Please, Sign In to add comment