PICCANTE  0.4
The hottest HDR imaging library!
pgm.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_IO_PGM_HPP
19 #define PIC_IO_PGM_HPP
20 
21 #include <iostream>
22 #include <fstream>
23 
24 #include "../base.hpp"
25 
26 namespace pic {
27 
37 PIC_INLINE unsigned char *ReadPGM(std::string nameFile, unsigned char *data,
38  int &width, int &height, int &channels)
39 {
40 
41  std::ifstream ppm_in(nameFile.c_str(), std::ios::binary);
42 
43  std::string magic_number(" ");
44 
45  ppm_in.get(magic_number[0]);
46  ppm_in.get(magic_number[1]);
47 
48  bool bBinary = true;
49 
50  if(magic_number != std::string("P5")) {
51  ppm_in.close();
52 
53  if(magic_number == std::string("P2")) {
54  bBinary = false;
55  ppm_in.open(nameFile.c_str(), std::ios::in);
56  ppm_in.get(magic_number[0]);
57  ppm_in.get(magic_number[1]);
58  } else {
59  return data;
60  }
61  }
62 
63  unsigned int tmpWidth, tmpHeight, bpp;
64 
65  ppm_in >> tmpWidth >> tmpHeight >> bpp;
66 
67  if(bpp > 255) {
68  printf("ERROR ReadPGM: BPP\n");
69  return data;
70  }
71 
72  channels = 1;
73  width = int(tmpWidth);
74  height = int(tmpHeight);
75 
76  //Allocate memory
77  if(data == NULL) {
78  data = new unsigned char[width * height * channels];
79  }
80 
81  for(int y = 0; y < height; y++) {
82  int ind = y * width;
83 
84  for(int x = 0; x < width; x++) {
85  int c = (ind + x);
86 
87  if(bBinary) {
88  char value;
89  ppm_in.get(value);
90  data[c] = (static_cast<unsigned char>(value) * 255) / bpp;
91  } else {
92  int value;
93  ppm_in >> value;
94  data[c] = (static_cast<unsigned char>(value) * 255) / bpp;
95  }
96  }
97  }
98 
99  ppm_in.close();
100 
101  return data;
102 }
103 
114 PIC_INLINE bool WritePGM(std::string nameFile, const unsigned char *data,
115  int width, int height, int channels, bool bAscii = false)
116 {
117  std::ofstream pgm_out;
118 
119  if(bAscii) {
120  pgm_out.open(nameFile.c_str(), std::ios::out);
121  } else {
122  pgm_out.open(nameFile.c_str(), std::ios::binary);
123  }
124 
125  if(!pgm_out.is_open()) {
126  return false;
127  }
128 
129  if(bAscii){
130  pgm_out << "P2";
131  } else {
132  pgm_out << "P5";
133  }
134 
135  pgm_out << ' ';
136  pgm_out << '\n';
137  pgm_out << width;
138  pgm_out << ' ';
139  pgm_out << height;
140  pgm_out << ' ';
141  pgm_out << '\n';
142  pgm_out << "255";
143  pgm_out << '\n';
144 
145  for(int y = 0; y < height; y++) {
146  int ind = y * width;
147 
148  for(int x = 0; x < width; x++) {
149  int c = (ind + x) * channels;
150 
151  if(bAscii) {
152  int value = data[c];
153  pgm_out << value << ' ';
154  } else {
155  pgm_out << data[c];
156  }
157  }
158  if(bAscii) {
159  pgm_out << '\n';
160  }
161  }
162 
163  pgm_out.flush();
164  pgm_out.close();
165  return true;
166 }
167 
168 } // end namespace pic
169 
170 #endif /* PIC_IO_PPM_HPP */
171 
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