shushus

Спринт 15. Системы сборки и зачем они нужны.

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