PICCANTE  0.4
The hottest HDR imaging library!
ppm.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_PPM_HPP
19 #define PIC_IO_PPM_HPP
20 
21 #include <iostream>
22 #include <fstream>
23 
24 #include "../base.hpp"
25 
26 namespace pic {
27 
37 PIC_INLINE unsigned char *ReadPPM(std::string nameFile, unsigned char *data,
38  int &width, int &height, int &channels)
39 {
40  std::ifstream ppm_in(nameFile.c_str(), std::ios::binary);
41 
42  std::string magic_number(" ");
43 
44  ppm_in.get(magic_number[0]);
45  ppm_in.get(magic_number[1]);
46 
47  bool bBinary = true;
48 
49  if(magic_number != std::string("P6")) {
50  ppm_in.close();
51 
52  if(magic_number == std::string("P3")) {
53  bBinary = false;
54  ppm_in.open(nameFile.c_str(), std::ios::in);
55  ppm_in.get(magic_number[0]);
56  ppm_in.get(magic_number[1]);
57  } else {
58  return data;
59  }
60  }
61 
62  unsigned tmpWidth, tmpHeight, bpp;
63 
64  ppm_in >> tmpWidth >> tmpHeight >> bpp;
65 
66  if(bpp > 255) {
67  printf("ERROR ReadPPM: BPP\n");
68  return data;
69  }
70 
71  channels = 3;
72 
73  //Allocate memory
74  if(data == NULL) {
75  data = new unsigned char[tmpWidth * tmpHeight * channels];
76  }
77 
78  width = tmpWidth;
79  height = tmpHeight;
80 
81  char ch;
82  ppm_in.get(ch); // Trailing white space.
83 
84  for(int y = 0; y < height; y++) {
85  int ind = y * width;
86 
87  for(int x = 0; x < width; x++) {
88  int c = (ind + x) * 3;
89 
90  if(bBinary) {
91  char r, g, b;
92  ppm_in.get(r);
93  ppm_in.get(g);
94  ppm_in.get(b);
95 
96  data[c ] = (static_cast<unsigned char>(r) * 255) / bpp;
97  data[c + 1] = (static_cast<unsigned char>(g) * 255) / bpp;
98  data[c + 2] = (static_cast<unsigned char>(b) * 255) / bpp;
99  } else {
100  int r, g, b;
101  ppm_in >> r;
102  ppm_in >> g;
103  ppm_in >> b;
104 
105  data[c ] = (r * 255) / bpp;
106  data[c + 1] = (g * 255) / bpp;
107  data[c + 2] = (b * 255) / bpp;
108  }
109  }
110  }
111 
112  ppm_in.close();
113 
114  return data;
115 }
116 
126 PIC_INLINE bool WritePPM(std::string nameFile, const unsigned char *data,
127  int width, int height, int channels)
128 {
129  std::ofstream ppm_out(nameFile.c_str(), std::ios::binary);
130 
131  if(!ppm_out.is_open()) {
132  return false;
133  }
134 
135  ppm_out << "P6";
136  ppm_out << ' ';
137  ppm_out << '\n';
138  ppm_out << width;
139  ppm_out << ' ';
140  ppm_out << height;
141  ppm_out << ' ';
142  ppm_out << '\n';
143  ppm_out << "255";
144  ppm_out << '\n';
145 
146  int shiftG = 1;
147  int shiftB = 2;
148 
149  if(channels == 1) {
150  shiftG = 0;
151  shiftB = 0;
152  }
153 
154  for(int y = 0; y < height; y++) {
155  int ind = y * width;
156 
157  for(int x = 0; x < width; x++) {
158  int c = (ind + x) * channels;
159  ppm_out << data[c ];
160  ppm_out << data[c + shiftG];
161  ppm_out << data[c + shiftB];
162  }
163  }
164 
165  ppm_out.flush();
166  ppm_out.close();
167  return true;
168 }
169 
170 } // end namespace pic
171 
172 #endif /* PIC_IO_PPM_HPP */
173 
#define PIC_INLINE
Definition: base.hpp:33
PIC_INLINE bool WritePPM(std::string nameFile, const unsigned char *data, int width, int height, int channels)
WritePPM writes an .ppm file.
Definition: ppm.hpp:126
Definition: bilateral_separation.hpp:25
PIC_INLINE unsigned char * ReadPPM(std::string nameFile, unsigned char *data, int &width, int &height, int &channels)
ReadPPM reads an .ppm file.
Definition: ppm.hpp:37