Necto

BMP 24,32

Jul 30th, 2022 (edited)
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #pragma once;
  2. #include <vector>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. struct Color {
  7.     float r, g, b, a;
  8.     Color();
  9.     Color(float r, float g, float b, float a);
  10.     ~Color();
  11. };
  12. class Image {
  13. public:
  14.     Image(int width, int heigth);
  15.     Image();
  16.     ~Image();
  17.     Color GetColor(int x, int y) const;
  18.     void SetColor(const Color& color, int x, int y);
  19.     void Export(const char* path) const;
  20.     void Read(const char* path);
  21.  
  22.  
  23. private:
  24.     int temp;
  25.     int m_width;
  26.     int m_height;
  27.     std::vector <Color> m_colors;
  28.     int channels;
  29. };
  30. void Image::Read(const char* path) {
  31.     std::ifstream f;
  32.     f.open(path, std::ios::in | std::ios::binary);
  33.     if (!f.is_open()) {
  34.         std::cout << "eror file has not opened";
  35.         return;
  36.     }
  37.     const int FileHeaderSize = 14;
  38.     const int InfoHeaderSize = 40;
  39.     unsigned char FileHeader[FileHeaderSize];
  40.     f.read(reinterpret_cast<char*>(FileHeader), FileHeaderSize);
  41.     unsigned char InfoHeader[InfoHeaderSize];
  42.  
  43.     if (FileHeader[0] != 'B' || FileHeader[1] != 'M') {
  44.         std::cout << "that`s not a bit map image\n";
  45.         return;
  46.     }
  47.  
  48.     f.read(reinterpret_cast<char*>(InfoHeader), InfoHeaderSize);
  49.     int FileSize = FileHeader[2] + (FileHeader[3] << 8) + (FileHeader[4] << 16) + (FileHeader[5] << 24);
  50.     m_width = InfoHeader[4] + (InfoHeader[5] << 8) + (InfoHeader[6] << 16) + (InfoHeader[7] << 24);
  51.     m_height = InfoHeader[8] + (InfoHeader[9] << 8) + (InfoHeader[10] << 16) + (InfoHeader[11] << 24);
  52.     m_colors.resize(m_width * m_height);
  53.     const int padding = (4 - ((m_width * 3) % 4)) % 4;
  54.     channels = (int)InfoHeader[14];
  55.     temp = (int)FileHeader[10];
  56.     f.seekg(temp, f.beg);
  57.     for (int y = 0; y < m_height; y++)
  58.     {
  59.         for (int x = 0; x < m_width; x++) {
  60.             if (channels == 32) {
  61.                 unsigned char color[4];
  62.                 f.read(reinterpret_cast<char*> (color), 4);
  63.                 m_colors[y * m_width + x].a = static_cast<float>(color[3]) / 255.0f;
  64.                 m_colors[y * m_width + x].r = static_cast<float>(color[2]) / 255.0f;
  65.                 m_colors[y * m_width + x].g = static_cast<float>(color[1]) / 255.0f;
  66.                 m_colors[y * m_width + x].b = static_cast<float>(color[0]) / 255.0f;
  67.             }
  68.             else {
  69.                 unsigned char color[3];
  70.                 f.read(reinterpret_cast<char*> (color), 3);
  71.                 m_colors[y * m_width + x].r = static_cast<float>(color[2]) / 255.0f;
  72.                 m_colors[y * m_width + x].g = static_cast<float>(color[1]) / 255.0f;
  73.                 m_colors[y * m_width + x].b = static_cast<float>(color[0]) / 255.0f;
  74.             }
  75.         }
  76.         f.ignore(padding);
  77.  
  78.     }
  79.     f.close();
  80.     std::cout << "file read\n";
  81.  
  82. }
  83. Color::Color()
  84.     : r(0), g(0), b(0), a(0)
  85. {
  86.  
  87. }
  88.  
  89. Color::Color(float r, float g, float b, float a)
  90.     : r(r), g(g), b(b), a(a)
  91. {
  92. }
  93.  
  94. Color::~Color()
  95. {
  96. }
  97.  
  98. Image::Image(int width, int height)
  99.     : m_width(width), m_height(height), m_colors(std::vector<Color>(width* height))
  100. {
  101. }
  102.  
  103. Image::~Image()
  104. {
  105. }
  106.  
  107. Image::Image()
  108.     : m_width(0), m_height(0), m_colors(std::vector<Color>(0))
  109. {
  110. }
  111.  
  112. Color Image::GetColor(int x, int y) const
  113. {
  114.     return m_colors[y * m_width + x];
  115. }
  116.  
  117. void Image::SetColor(const Color& color, int x, int y)
  118. {
  119.     m_colors[y * m_width + x].r = color.r;
  120.     m_colors[y * m_width + x].g = color.g;
  121.     m_colors[y * m_width + x].b = color.b;
  122.     m_colors[y * m_width + x].a = color.a;
  123. }
  124.  
  125. void Image::Export(const char* path) const
  126. {
  127.     std::ofstream f;
  128.     f.open(path, std::ios::out | std::ios::binary);
  129.     if (!f.is_open()) {
  130.         std::cout << "error file has not openned";
  131.         return;
  132.     }
  133.     unsigned char bmpPad[3] = { 0,0,0 };
  134.     const int padding = (4 - ((m_width * 3) % 4)) % 4;
  135.     const int FileHeaderSize = 14;
  136.     const int InfoHeaderSize = 40;
  137.     const int FileSize = FileHeaderSize + InfoHeaderSize + m_width * m_height * channels / 8 + padding * m_height;
  138.     unsigned char FileHeader[FileHeaderSize] = { 'B','M', 0,0,0,0, 0,0, 0,0, 0,0,0,0 };
  139.     unsigned char InfoHeader[InfoHeaderSize] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 0,0 };
  140.  
  141.     FileHeader[2] = FileSize;
  142.     FileHeader[3] = FileSize >> 8;
  143.     FileHeader[4] = FileSize >> 16;
  144.     FileHeader[5] = FileSize >> 24;
  145.     FileHeader[10] = FileHeaderSize + InfoHeaderSize;
  146.     InfoHeader[4] = m_width;
  147.     InfoHeader[5] = m_width >> 8;
  148.     InfoHeader[6] = m_width >> 16;
  149.     InfoHeader[7] = m_width >> 24;
  150.     InfoHeader[8] = m_height;
  151.     InfoHeader[9] = m_height >> 8;
  152.     InfoHeader[10] = m_height >> 16;
  153.     InfoHeader[11] = m_height >> 24;
  154.     InfoHeader[14] = (unsigned char)channels;
  155.  
  156.     f.write(reinterpret_cast<char*>(FileHeader), FileHeaderSize);
  157.     f.write(reinterpret_cast<char*>(InfoHeader), InfoHeaderSize);
  158.  
  159.     for (int y = 0; y < m_height; y++) {
  160.         for (int x = 0; x < m_width; x++)
  161.         {
  162.             unsigned char r = static_cast<unsigned char>(GetColor(x, y).r * 255.0f);
  163.             unsigned char g = static_cast<unsigned char>(GetColor(x, y).g * 255.0f);
  164.             unsigned char b = static_cast<unsigned char>(GetColor(x, y).b * 255.0f);
  165.             unsigned char a = static_cast<unsigned char>(GetColor(x, y).a * 255.0f);
  166.             if (channels == 32) {
  167.                 unsigned char color[] = { b, g, r, a };
  168.                 f.write(reinterpret_cast<char*>(color), 4);
  169.             }
  170.             else {
  171.                 unsigned char color[] = { b, g, r};
  172.                 f.write(reinterpret_cast<char*>(color), 3);
  173.             }
  174.         }
  175.         f.write(reinterpret_cast<char*>(bmpPad), padding);
  176.  
  177.     }
  178.     f.close();
  179.     std::cout << "File created\n";
  180. }
Advertisement
Add Comment
Please, Sign In to add comment