18 #ifndef PIC_IO_PGM_HPP 19 #define PIC_IO_PGM_HPP 24 #include "../base.hpp" 38 int &width,
int &height,
int &channels)
41 std::ifstream ppm_in(nameFile.c_str(), std::ios::binary);
43 std::string magic_number(
" ");
45 ppm_in.get(magic_number[0]);
46 ppm_in.get(magic_number[1]);
50 if(magic_number != std::string(
"P5")) {
53 if(magic_number == std::string(
"P2")) {
55 ppm_in.open(nameFile.c_str(), std::ios::in);
56 ppm_in.get(magic_number[0]);
57 ppm_in.get(magic_number[1]);
63 unsigned int tmpWidth, tmpHeight, bpp;
65 ppm_in >> tmpWidth >> tmpHeight >> bpp;
68 printf(
"ERROR ReadPGM: BPP\n");
73 width = int(tmpWidth);
74 height = int(tmpHeight);
78 data =
new unsigned char[width * height * channels];
81 for(
int y = 0; y < height; y++) {
84 for(
int x = 0; x < width; x++) {
90 data[c] = (
static_cast<unsigned char>(value) * 255) / bpp;
94 data[c] = (
static_cast<unsigned char>(value) * 255) / bpp;
115 int width,
int height,
int channels,
bool bAscii =
false)
117 std::ofstream pgm_out;
120 pgm_out.open(nameFile.c_str(), std::ios::out);
122 pgm_out.open(nameFile.c_str(), std::ios::binary);
125 if(!pgm_out.is_open()) {
145 for(
int y = 0; y < height; y++) {
148 for(
int x = 0; x < width; x++) {
149 int c = (ind + x) * channels;
153 pgm_out << value <<
' ';
PIC_INLINE unsigned char * ReadPGM(std::string nameFile, unsigned char *data, int &width, int &height, int &channels)
ReadPGM reads an .ppm file.
Definition: pgm.hpp:37
PIC_INLINE bool WritePGM(std::string nameFile, const unsigned char *data, int width, int height, int channels, bool bAscii=false)
WritePGM writes an .ppm file.
Definition: pgm.hpp:114
#define PIC_INLINE
Definition: base.hpp:33
Definition: bilateral_separation.hpp:25