pnmfile.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2006 Pedro Felzenszwalb
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 /* basic image I/O */
20 
21 #pragma once
22 
23 #include <cstdlib>
24 #include <climits>
25 #include <cstring>
26 #include <fstream>
27 
28 #include "image.h"
29 #include "misc.h"
30 
31 namespace distance_transform
32 {
33 
34  const short BUF_SIZE = 256;
35 
36  class pnm_error { };
37 
38  static void read_packed(unsigned char *data, int size, std::ifstream &f) {
39  unsigned char c = 0;
40 
41  int bitshift = -1;
42  for (int pos = 0; pos < size; pos++) {
43  if (bitshift == -1) {
44  c = f.get();
45  bitshift = 7;
46  }
47  data[pos] = (c >> bitshift) & 1;
48  bitshift--;
49  }
50  }
51 
52  static void write_packed(unsigned char *data, int size, std::ofstream &f) {
53  unsigned char c = 0;
54 
55  int bitshift = 7;
56  for (int pos = 0; pos < size; pos++) {
57  c = c | (data[pos] << bitshift);
58  bitshift--;
59  if ((bitshift == -1) || (pos == size-1)) {
60  f.put(c);
61  bitshift = 7;
62  c = 0;
63  }
64  }
65  }
66 
67  /* read PNM field, skipping comments */
68  static void pnm_read(std::ifstream &file, char *buf) {
69  char doc[BUF_SIZE];
70  char c;
71 
72  file >> c;
73  while (c == '#') {
74  file.getline(doc, BUF_SIZE);
75  file >> c;
76  }
77  file.putback(c);
78 
79  file.width(BUF_SIZE);
80  file >> buf;
81  file.ignore();
82  }
83 
84  static image<uchar> *loadPBM(const char *name) {
85  char buf[BUF_SIZE];
86 
87  /* read header */
88  std::ifstream file(name, std::ios::in | std::ios::binary);
89  pnm_read(file, buf);
90  if (strncmp(buf, "P4", 2))
91  throw pnm_error();
92 
93  pnm_read(file, buf);
94  int width = atoi(buf);
95  pnm_read(file, buf);
96  int height = atoi(buf);
97 
98  /* read data */
99  image<uchar> *im = new image<uchar>(width, height);
100  for (int i = 0; i < height; i++)
101  read_packed(imPtr(im, 0, i), width, file);
102 
103  return im;
104  }
105 
106  static void savePBM(image<uchar> *im, const char *name) {
107  int width = im->width();
108  int height = im->height();
109  std::ofstream file(name, std::ios::out | std::ios::binary);
110 
111  file << "P4\n" << width << " " << height << "\n";
112  for (int i = 0; i < height; i++)
113  write_packed(imPtr(im, 0, i), width, file);
114  }
115 
116  static image<uchar> *loadPGM(const char *name) {
117  char buf[BUF_SIZE];
118 
119  /* read header */
120  std::ifstream file(name, std::ios::in | std::ios::binary);
121  pnm_read(file, buf);
122  if (strncmp(buf, "P5", 2))
123  throw pnm_error();
124 
125  pnm_read(file, buf);
126  int width = atoi(buf);
127  pnm_read(file, buf);
128  int height = atoi(buf);
129 
130  pnm_read(file, buf);
131  if (atoi(buf) > UCHAR_MAX)
132  throw pnm_error();
133 
134  /* read data */
135  image<uchar> *im = new image<uchar>(width, height);
136  file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
137 
138  return im;
139  }
140 
141  static void savePGM(image<uchar> *im, const char *name) {
142  int width = im->width();
143  int height = im->height();
144  std::ofstream file(name, std::ios::out | std::ios::binary);
145 
146  file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
147  file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
148  }
149 
150  static image<rgb> *loadPPM(const char *name) {
151  char buf[BUF_SIZE], doc[BUF_SIZE];
152 
153  /* read header */
154  std::ifstream file(name, std::ios::in | std::ios::binary);
155  pnm_read(file, buf);
156  if (strncmp(buf, "P6", 2))
157  throw pnm_error();
158 
159  pnm_read(file, buf);
160  int width = atoi(buf);
161  pnm_read(file, buf);
162  int height = atoi(buf);
163 
164  pnm_read(file, buf);
165  if (atoi(buf) > UCHAR_MAX)
166  throw pnm_error();
167 
168  /* read data */
169  image<rgb> *im = new image<rgb>(width, height);
170  file.read((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
171 
172  return im;
173  }
174 
175  static void savePPM(image<rgb> *im, const char *name) {
176  int width = im->width();
177  int height = im->height();
178  std::ofstream file(name, std::ios::out | std::ios::binary);
179 
180  file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
181  file.write((char *)imPtr(im, 0, 0), width * height * sizeof(rgb));
182  }
183 
184  template <class T>
185  void load_image(image<T> **im, const char *name) {
186  char buf[BUF_SIZE];
187 
188  /* read header */
189  std::ifstream file(name, std::ios::in | std::ios::binary);
190  pnm_read(file, buf);
191  if (strncmp(buf, "VLIB", 9))
192  throw pnm_error();
193 
194  pnm_read(file, buf);
195  int width = atoi(buf);
196  pnm_read(file, buf);
197  int height = atoi(buf);
198 
199  /* read data */
200  *im = new image<T>(width, height);
201  file.read((char *)imPtr((*im), 0, 0), width * height * sizeof(T));
202  }
203 
204  template <class T>
205  void save_image(image<T> *im, const char *name) {
206  int width = im->width();
207  int height = im->height();
208  std::ofstream file(name, std::ios::out | std::ios::binary);
209 
210  file << "VLIB\n" << width << " " << height << "\n";
211  file.write((char *)imPtr(im, 0, 0), width * height * sizeof(T));
212  }
213 
214 } // namespace
static void savePBM(image< uchar > *im, const char *name)
Definition: pnmfile.h:106
static image< uchar > * loadPGM(const char *name)
Definition: pnmfile.h:116
static void read_packed(unsigned char *data, int size, std::ifstream &f)
Definition: pnmfile.h:38
int width() const
Definition: image.h:44
static void savePPM(image< rgb > *im, const char *name)
Definition: pnmfile.h:175
const short BUF_SIZE
Definition: pnmfile.h:34
int height() const
Definition: image.h:47
void save_image(image< T > *im, const char *name)
Definition: pnmfile.h:205
void load_image(image< T > **im, const char *name)
Definition: pnmfile.h:185
unsigned char uchar
Definition: misc.h:27
static void write_packed(unsigned char *data, int size, std::ofstream &f)
Definition: pnmfile.h:52
static void pnm_read(std::ifstream &file, char *buf)
Definition: pnmfile.h:68
static void savePGM(image< uchar > *im, const char *name)
Definition: pnmfile.h:141
static image< uchar > * loadPBM(const char *name)
Definition: pnmfile.h:84
#define imPtr(im, x, y)
Definition: image.h:63
static image< rgb > * loadPPM(const char *name)
Definition: pnmfile.h:150


grid_map_sdf
Author(s): Takahiro Miki , Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:49