shushus

Спринт 15. Собираем CMake'ом.

May 31st, 2023
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 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. add_executable("negate" "main.cpp" "img_lib.h" "img_lib.cpp" "ppm_image.h" "ppm_image.cpp")
  10. -----------------------------------------------------------------------
  11. main.cpp
  12. -----------------------------------------------------------------------
  13. #include "ppm_image.h"
  14.  
  15. #include <iostream>
  16. #include <string_view>
  17.  
  18. using namespace std;
  19.  
  20. void NegateInplace(img_lib::Image& image) {
  21.    
  22.     int w{image.GetWidth()};
  23.     int h{image.GetHeight()};
  24.  
  25.     std::vector<char> buff(w * 3);
  26.  
  27.     for (int i = 0; i < h; ++i) {
  28.         img_lib::Color* line = image.GetLine(i);
  29.  
  30.         for (int j = 0; j < w; ++j) {
  31.             line[j].r = byte(255 - std::to_integer<int>(line[j].r));
  32.             line[j].g = byte(255 - std::to_integer<int>(line[j].g));
  33.             line[j].b = byte(255 - std::to_integer<int>(line[j].b));
  34.         }
  35.     }
  36. }
  37.  
  38. int main(int argc, const char** argv) {
  39.    
  40.     if (argc != 3) {
  41.         cerr << "Usage: "sv << argv[0] << " <input image> <output image>"sv << endl;
  42.         return 1;
  43.     }
  44.  
  45.     auto image = img_lib::LoadPPM(argv[1]);
  46.     if (!image) {
  47.         cerr << "Error loading image"sv << endl;
  48.         return 2;
  49.     }
  50.    
  51.     NegateInplace(image);
  52.  
  53.     if (!img_lib::SavePPM(argv[2], image)) {
  54.         cerr << "Error saving image"sv << endl;
  55.         return 3;
  56.     }
  57.  
  58.     cout << "Image saved successfully!"sv << endl;
  59. }
  60. -----------------------------------------------------------------------
  61. img_lib.h
  62. -----------------------------------------------------------------------
  63. #pragma once
  64.  
  65. #include <array>
  66. #include <cassert>
  67. #include <cstddef>
  68. #include <vector>
  69.  
  70. namespace img_lib {
  71.  
  72. struct Size {
  73.     int width;
  74.     int height;
  75. };
  76.  
  77. struct Color {
  78.     static Color Black() {return {std::byte{0}, std::byte{0}, std::byte{0}, std::byte{255}};}
  79.  
  80.     std::byte r, g, b, a;
  81. };
  82.  
  83. class Image {
  84. public:
  85.  
  86.     Image() = default;
  87.  
  88.     Image(int w, int h, Color fill);
  89.  
  90.     Color GetPixel(int x, int y) const {
  91.         return const_cast<Image*>(this)->GetPixel(x, y);
  92.     }
  93.     Color& GetPixel(int x, int y) {
  94.         assert(x < GetWidth() && y < GetHeight() && x >= 0 && y >= 0);
  95.         return GetLine(y)[x];
  96.     }
  97.  
  98.     Color* GetLine(int y);
  99.     const Color* GetLine(int y) const;
  100.  
  101.     int GetWidth() const;
  102.     int GetHeight() const;
  103.  
  104.     int GetStep() const;
  105.  
  106.     explicit operator bool() const {return GetWidth() > 0 && GetHeight() > 0;}
  107.     bool operator!() const {return !operator bool();}
  108.  
  109. private:
  110.     int width_ = 0;
  111.     int height_ = 0;
  112.     int step_;
  113.  
  114.     std::vector<Color> pixels_;
  115. };
  116.  
  117. }//end namespace img_lib
  118. -----------------------------------------------------------------------
  119. img_lib.cpp
  120. -----------------------------------------------------------------------
  121. #include "img_lib.h"
  122.  
  123. namespace img_lib {
  124.  
  125. Image::Image(int w, int h, Color fill) : width_(w)
  126.                                        , height_(h)
  127.                                        , step_(w)
  128.                                        , pixels_(step_ * height_, fill) {}
  129.  
  130. Color* Image::GetLine(int y) {
  131.     assert(y >= 0 && y < height_);
  132.     return pixels_.data() + step_ * y;
  133. }
  134.  
  135. const Color* Image::GetLine(int y) const {return const_cast<Image*>(this)->GetLine(y);}
  136. int Image::GetWidth() const {return width_;}
  137. int Image::GetHeight() const {return height_;}
  138. int Image::GetStep() const {return step_;}
  139.  
  140. }//end namespace img_lib
  141. -----------------------------------------------------------------------
  142. ppm_image.h
  143. -----------------------------------------------------------------------
  144. #pragma once
  145. #include "img_lib.h"
  146.  
  147. #include <filesystem>
  148.  
  149. namespace img_lib {
  150. using Path = std::filesystem::path;
  151.  
  152. bool SavePPM(const Path& file, const Image& image);
  153. Image LoadPPM(const Path& file);
  154.  
  155. }//end namespace img_lib
  156. -----------------------------------------------------------------------
  157. ppm_image.cpp
  158. -----------------------------------------------------------------------
  159. #include "ppm_image.h"
  160.  
  161. #include <array>
  162. #include <fstream>
  163. #include <string_view>
  164.  
  165. using namespace std;
  166.  
  167. namespace img_lib {
  168.  
  169. static const string_view PPM_SIG = "P6"sv;
  170. static const int PPM_MAX = 255;
  171.  
  172. bool SavePPM(const Path& file, const Image& image) {
  173.     ofstream ofs(file, ios::out | ios::binary);
  174.     int w{image.GetWidth()};
  175.     int h{image.GetHeight()};
  176.     int color_max{PPM_MAX};
  177.  
  178.     ofs << PPM_SIG << '\n' << w << ' ' << h << '\n' << color_max << '\n';
  179.  
  180.     Image result{image};
  181.     std::vector<char> buff(w * 3);
  182.  
  183.     for (int y = 0; y < h; ++y) {
  184.        
  185.         Color* line = result.GetLine(y);
  186.         for (int x = 0; x < w; ++x) {
  187.             buff[x * 3 + 0] = static_cast<char>(line[x].r);
  188.             buff[x * 3 + 1] = static_cast<char>(line[x].g);
  189.             buff[x * 3 + 2] = static_cast<char>(line[x].b);
  190.         }
  191.        
  192.         ofs.write(buff.data(), w * 3);
  193.        
  194.         if (ofs.fail()) {
  195.             return false;
  196.         }
  197.     }
  198.  
  199.     return true;
  200. }
  201.  
  202. Image LoadPPM(const Path& file) {
  203.     ifstream ifs(file, ios::binary);
  204.     std::string sign;
  205.     int w, h, color_max;
  206.  
  207.     ifs >> sign >> w >> h >> color_max;
  208.  
  209.     if (sign != PPM_SIG || color_max != PPM_MAX) {return {};}
  210.  
  211.     const char next = ifs.get();
  212.     if (next != '\n') {return {};}
  213.  
  214.     Image result(w, h, Color::Black());
  215.     std::vector<char> buff(w * 3);
  216.  
  217.     for (int y = 0; y < h; ++y) {
  218.        
  219.         Color* line = result.GetLine(y);
  220.         ifs.read(buff.data(), w * 3);
  221.  
  222.         for (int x = 0; x < w; ++x) {
  223.             line[x].r = static_cast<byte>(buff[x * 3 + 0]);
  224.             line[x].g = static_cast<byte>(buff[x * 3 + 1]);
  225.             line[x].b = static_cast<byte>(buff[x * 3 + 2]);
  226.         }
  227.     }
  228.  
  229.     return result;
  230. }
  231.  
  232. }//end namespace img_lib
Advertisement
Add Comment
Please, Sign In to add comment