Advertisement
Ta7a99

Untitled

Jun 4th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #ifndef RW_PGM_HPP
  2. #define RW_PGM_HPP
  3.  
  4. #include <cstdint>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. uint8_t *read_pgm_image(int &width, int &height, std::string filename)
  13. {
  14.     string line;
  15.     ifstream in(filename);
  16.  
  17.     getline(in, line);
  18.  
  19.     getline(in, line);
  20.     stringstream width_height_ss(line);
  21.     width_height_ss >> width >> height;
  22.  
  23.     getline(in, line);
  24.  
  25.     uint8_t *img = new uint8_t[width * height];
  26.  
  27.     in.read((char *)img, width * height);
  28.  
  29.     in.close();
  30.     return img;
  31. }
  32.  
  33. void write_pgm_image(const uint8_t *img, int width, int height, std::string filename)
  34. {
  35.     std::ofstream out(filename);
  36.     out << "P5" << std::endl
  37.         << width << " " << height << std::endl
  38.         << 255 << std::endl;
  39.     out.write((const char *)img, width * height);
  40.     out.close();
  41. }
  42.  
  43. #endif //RW_PGM_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement