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 | #ifndef PNM_FILE_H |
---|
22 | #define PNM_FILE_H |
---|
23 | |
---|
24 | #include <cstdlib> |
---|
25 | #include <climits> |
---|
26 | #include <cstring> |
---|
27 | #include <fstream> |
---|
28 | #include "image.h" |
---|
29 | #include "misc.h" |
---|
30 | |
---|
31 | #define BUF_SIZE 256 |
---|
32 | |
---|
33 | class pnm_error { }; |
---|
34 | /* |
---|
35 | static void read_packed(unsigned char *data, int size, std::ifstream &f) { |
---|
36 | unsigned char c = 0; |
---|
37 | |
---|
38 | int bitshift = -1; |
---|
39 | for (int pos = 0; pos < size; pos++) { |
---|
40 | if (bitshift == -1) { |
---|
41 | c = f.get(); |
---|
42 | bitshift = 7; |
---|
43 | } |
---|
44 | data[pos] = (c >> bitshift) & 1; |
---|
45 | bitshift--; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | static void write_packed(unsigned char *data, int size, std::ofstream &f) { |
---|
50 | unsigned char c = 0; |
---|
51 | |
---|
52 | int bitshift = 7; |
---|
53 | for (int pos = 0; pos < size; pos++) { |
---|
54 | c = c | (data[pos] << bitshift); |
---|
55 | bitshift--; |
---|
56 | if ((bitshift == -1) || (pos == size-1)) { |
---|
57 | f.put(c); |
---|
58 | bitshift = 7; |
---|
59 | c = 0; |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | */ |
---|
64 | |
---|
65 | /* read PNM field, skipping comments */ |
---|
66 | |
---|
67 | static void pnm_read(std::ifstream &file, char *buf) { |
---|
68 | char doc[BUF_SIZE]; |
---|
69 | char c; |
---|
70 | |
---|
71 | file >> c; |
---|
72 | while (c == '#') { |
---|
73 | file.getline(doc, BUF_SIZE); |
---|
74 | file >> c; |
---|
75 | } |
---|
76 | file.putback(c); |
---|
77 | |
---|
78 | file.width(BUF_SIZE); |
---|
79 | file >> buf; |
---|
80 | file.ignore(); |
---|
81 | } |
---|
82 | /* |
---|
83 | static image<uchar> *loadPBM(const char *name) { |
---|
84 | char buf[BUF_SIZE]; |
---|
85 | |
---|
86 | |
---|
87 | std::ifstream file(name, std::ios::in | std::ios::binary); |
---|
88 | pnm_read(file, buf); |
---|
89 | if (strncmp(buf, "P4", 2)) |
---|
90 | throw pnm_error(); |
---|
91 | |
---|
92 | pnm_read(file, buf); |
---|
93 | int width = atoi(buf); |
---|
94 | pnm_read(file, buf); |
---|
95 | int height = atoi(buf); |
---|
96 | |
---|
97 | |
---|
98 | image<uchar> *im = new image<uchar>(width, height); |
---|
99 | for (int i = 0; i < height; i++) |
---|
100 | read_packed(imPtr(im, 0, i), width, file); |
---|
101 | |
---|
102 | return im; |
---|
103 | } |
---|
104 | |
---|
105 | static void savePBM(image<uchar> *im, const char *name) { |
---|
106 | int width = im->width(); |
---|
107 | int height = im->height(); |
---|
108 | std::ofstream file(name, std::ios::out | std::ios::binary); |
---|
109 | |
---|
110 | file << "P4\n" << width << " " << height << "\n"; |
---|
111 | for (int i = 0; i < height; i++) |
---|
112 | write_packed(imPtr(im, 0, i), width, file); |
---|
113 | } |
---|
114 | |
---|
115 | static image<uchar> *loadPGM(const char *name) { |
---|
116 | char buf[BUF_SIZE]; |
---|
117 | |
---|
118 | |
---|
119 | std::ifstream file(name, std::ios::in | std::ios::binary); |
---|
120 | pnm_read(file, buf); |
---|
121 | if (strncmp(buf, "P5", 2)) |
---|
122 | throw pnm_error(); |
---|
123 | |
---|
124 | pnm_read(file, buf); |
---|
125 | int width = atoi(buf); |
---|
126 | pnm_read(file, buf); |
---|
127 | int height = atoi(buf); |
---|
128 | |
---|
129 | pnm_read(file, buf); |
---|
130 | if (atoi(buf) > UCHAR_MAX) |
---|
131 | throw pnm_error(); |
---|
132 | |
---|
133 | |
---|
134 | image<uchar> *im = new image<uchar>(width, height); |
---|
135 | file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar)); |
---|
136 | |
---|
137 | return im; |
---|
138 | } |
---|
139 | |
---|
140 | static void savePGM(image<uchar> *im, const char *name) { |
---|
141 | int width = im->width(); |
---|
142 | int height = im->height(); |
---|
143 | std::ofstream file(name, std::ios::out | std::ios::binary); |
---|
144 | |
---|
145 | file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; |
---|
146 | file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar)); |
---|
147 | } |
---|
148 | */ |
---|
149 | |
---|
150 | static image *loadPPM(const char *name) { |
---|
151 | char buf[BUF_SIZE];//, doc[BUF_SIZE]; |
---|
152 | |
---|
153 | |
---|
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 | |
---|
169 | image *im =image_construct(width,height,false);// new image<rgb>(width, height); |
---|
170 | file.read((char *)imPtr(im, 0, 0), width * height * sizeof(rgb)); |
---|
171 | |
---|
172 | return im; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | static void savePPM(image *im, const char *name) { |
---|
177 | int width = im->w; |
---|
178 | int height = im->h; |
---|
179 | std::ofstream file(name, std::ios::out | std::ios::binary); |
---|
180 | |
---|
181 | file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; |
---|
182 | file.write((char *)imPtr(im, 0, 0), width * height * sizeof(rgb)); |
---|
183 | } |
---|
184 | /* |
---|
185 | template <class T> |
---|
186 | void load_image(image **im, const char *name) { |
---|
187 | char buf[BUF_SIZE]; |
---|
188 | |
---|
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 | |
---|
200 | *im = image_construct(width, height,false); |
---|
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 | #endif |
---|