Debian Codegraph @

tests/load_and_save.cc

raw symbols SCIP
1 /*
2 This file is part of the CVD Library.
3
4 Copyright (C) 2005 The Authors
5
6 This library is free software, see LICENSE file for details
7 */
8 #include <algorithm>
9 #include <cstdlib>
10 #include <fstream>
11 #include <iostream>
12 #include <limits>
13 #include <random>
14 #include <sstream>
15 #include <string>
16
17 #include <cvd/image_io.h>
18
19 using namespace CVD;
20 using std::cerr;
21 using std::clog;
22 using std::endl;
23 using std::equal;
24 using std::ifstream;
25 using std::ios;
26 using std::ios_base;
27 using std::map;
28 using std::max;
29 using std::min;
30 using std::ofstream;
31 using std::string;
32 using std::stringstream;
33
34 using CVD::Internal::Head;
35 using CVD::Internal::TypeList;
36
37 std::mt19937 rng;
38
39 template <class C>
40 struct randpix
41 {
42 static C r()
43 {
44 union
45 {
46 unsigned int i;
47 C c;
48 } u;
49
50 u.i = rand();
51 return u.c;
52 }
53 };
54
55 template <>
56 struct randpix<bool>
57 {
58 static bool r()
59 {
60 return std::uniform_int_distribution<>(0, 1)(rng);
61 }
62 };
63
64 template <>
65 struct randpix<bayer_bggr>
66 {
67 static byte r()
68 {
69 return static_cast<byte>(std::uniform_int_distribution<>(0, 255)(rng));
70 }
71 };
72 template <>
73 struct randpix<bayer_rggb>
74 {
75 static byte r()
76 {
77 return static_cast<byte>(std::uniform_int_distribution<>(0, 255)(rng));
78 }
79 };
80
81 template <>
82 struct randpix<bayer_grbg>
83 {
84 static byte r()
85 {
86 return static_cast<byte>(std::uniform_int_distribution<>(0, 255)(rng));
87 }
88 };
89
90 template <>
91 struct randpix<bayer_gbrg>
92 {
93 static byte r()
94 {
95 return static_cast<byte>(std::uniform_int_distribution<>(0, 255)(rng));
96 }
97 };
98
99 template <>
100 struct randpix<double>
101 {
102 static double r()
103 {
104 return rand() * 10. / RAND_MAX - 5;
105 }
106 };
107
108 template <>
109 struct randpix<float>
110 {
111 static float r()
112 {
113 return std::uniform_real_distribution<float>(-5, 5)(rng);
114 }
115 };
116
117 template <class C>
118 struct randpix<Rgb<C>>
119 {
120 static Rgb<C> r()
121 {
122 return Rgb<C>(randpix<C>::r(), randpix<C>::r(), randpix<C>::r());
123 }
124 };
125
126 template <class C>
127 struct randpix<Rgba<C>>
128 {
129 static Rgba<C> r()
130 {
131 return Rgba<C>(randpix<C>::r(), randpix<C>::r(), randpix<C>::r(), randpix<C>::r());
132 }
133 };
134
135 template <class T>
136 string make_output_file_name(string fin, string type)
137 {
138 std::string fn;
139
140 //T tmp;
141
142 //Remove the path from the filename
143 for(int i = static_cast<int>(fin.size() - 1); i >= 0; i--)
144 {
145 if(fin[i] != '/')
146 fn = fin[i] + fn;
147 else
148 break;
149 }
150
151 fn = " from " + fn;
152 fn = PNM::type_name<T>::name() + fn;
153
154 Image<T> im;
155
156 string fout = "testout/" + fn + "." + type;
157
158 return fout;
159 }
160
161 template <class T>
162 void loadsave(string fin)
163 {
164 Image<T> im;
165
166 //Load the image
167 clog << "Reading: " << fin << endl;
168 ifstream i(fin.c_str(), ios::in | ios::binary);
169 img_load(im, i);
170 i.close();
171
172 string fout = make_output_file_name<T>(fin, "pnm");
173 clog << "Writing: " << fout << endl
174 << endl;
175
176 //Save it
177 ofstream o(fout.c_str(), ios::out | ios::binary);
178 img_save(im, o, ImageType::PNM);
179 o.close();
180
181 //Save it
182 #ifdef CVD_HAVE_JPEG
183 fout = make_output_file_name<T>(fin, "jpg");
184 clog << "Writing: " << fout << endl
185 << endl;
186 o.open(fout.c_str());
187 img_save(im, o, ImageType::JPEG);
188 o.close();
189 #endif
190 #ifdef CVD_HAVE_PNG
191 fout = make_output_file_name<T>(fin, "png");
192 clog << "Writing: " << fout << endl
193 << endl;
194 o.open(fout.c_str());
195 img_save(im, o, ImageType::PNG);
196 o.close();
197 #endif
198 #ifdef CVD_HAVE_TIFF
199 fout = make_output_file_name<T>(fin, "tiff");
200 clog << "Writing: " << fout << endl
201 << endl;
202 o.open(fout.c_str());
203 img_save(im, o, ImageType::TIFF);
204 o.close();
205 #endif
206 }
207
208 template <class T>
209 void loadsave_safe(const char* n)
210 {
211 try
212 {
213 loadsave<T>(n);
214 }
215 catch(const CVD::Exceptions::All& b0rk3d)
216 {
217 cerr << "Image " << n << " error: " << b0rk3d.what() << endl;
218 }
219 }
220
221 map<string, Parameter<>> empty()
222 {
223 map<string, Parameter<>> nothing;
224 return nothing;
225 }
226
227 template <class T>
228 struct randtest
229 {
230 typedef typename T::Type Type;
231
232 static void exec(ImageType::ImageType fmt, const map<string, Parameter<>>& p = empty())
233 {
234 for(int i = 0; i < 10; i++)
235 {
236 try
237 {
238 //Make a random image
239 Image<Type> in(ImageRef(256 + i, 256 + i)), out;
240
241 cerr << "Testing " << in.size() << " " << fmt << " " << CVD::PNM::type_name<Type>::name() << " ";
242
243 for(int y = 0; y < in.size().y; y++)
244 for(int x = 0; x < in.size().x; x++)
245 in[y][x] = randpix<Type>::r();
246
247 stringstream s;
248
249 //Save the image
250 img_save(in, s, fmt, p);
251
252 s.seekg(0, ios_base::beg);
253 s.seekp(0, ios_base::beg);
254
255 //Load the image
256 out = img_load(s);
257
258 //Compare the results
259 if(out.size() != in.size())
260 cerr << " size mismatch.\n";
261 else if(!equal(in.begin(), in.end(), out.begin()))
262 {
263 cerr << " data mismatch.\n";
264
265 double t = 0, minval = HUGE_VAL, maxval = -HUGE_VAL;
266 for(int y = 0; y < in.size().y; y++)
267 for(int x = 0; x < in.size().x; x++)
268 for(unsigned int c = 0; c < Pixel::Component<Type>::count; c++)
269 {
270 double p = Pixel::Component<Type>::get(in[y][x], c);
271 double p2 = Pixel::Component<Type>::get(out[y][x], c);
272 t += abs(p - p2);
273
274 maxval = max(maxval, p);
275 minval = min(minval, p);
276
277 //cerr << p << " " << p2 << endl;
278 }
279
280 cerr << "Mismatch is " << 100 * t * 1.0 / in.size().area() / (maxval - minval) << "% per pixel\n";
281
282 cerr << "Min is: " << minval << endl;
283 cerr << "Max is: " << maxval << endl;
284
285 exit(1);
286 }
287 else
288 cerr << "OK.\n";
289 }
290 catch(const Exceptions::All& w)
291 {
292 cerr << w.what() << endl;
293 }
294 }
295
296 randtest<typename T::Next>::exec(fmt, p);
297 }
298 };
299
300 template <>
301 struct randtest<Head>
302 {
303 static void exec(ImageType::ImageType, const map<string, Parameter<>>& = empty())
304 {
305 }
306 };
307
308 int main(int ac, char** av)
309 {
310
311 for(int i = 1; i < ac; i++)
312 {
313 loadsave_safe<bool>(av[i]);
314 loadsave_safe<unsigned char>(av[i]);
315 loadsave_safe<signed char>(av[i]);
316 loadsave_safe<short>(av[i]);
317 loadsave_safe<unsigned short>(av[i]);
318 loadsave_safe<int>(av[i]);
319 loadsave_safe<unsigned int>(av[i]);
320 loadsave_safe<long>(av[i]);
321 loadsave_safe<unsigned long>(av[i]);
322 loadsave_safe<long long>(av[i]);
323 loadsave_safe<unsigned long long>(av[i]);
324 loadsave_safe<float>(av[i]);
325 loadsave_safe<double>(av[i]);
326 loadsave_safe<long double>(av[i]);
327
328 loadsave_safe<CVD::Rgb<unsigned char>>(av[i]);
329 loadsave_safe<CVD::Rgb<signed char>>(av[i]);
330 loadsave_safe<CVD::Rgb<short>>(av[i]);
331 loadsave_safe<CVD::Rgb<unsigned short>>(av[i]);
332 loadsave_safe<CVD::Rgb<int>>(av[i]);
333 loadsave_safe<CVD::Rgb<unsigned int>>(av[i]);
334 loadsave_safe<CVD::Rgb<long>>(av[i]);
335 loadsave_safe<CVD::Rgb<unsigned long>>(av[i]);
336 loadsave_safe<CVD::Rgb<long long>>(av[i]);
337 loadsave_safe<CVD::Rgb<unsigned long long>>(av[i]);
338 loadsave_safe<CVD::Rgb<float>>(av[i]);
339 loadsave_safe<CVD::Rgb<double>>(av[i]);
340 loadsave_safe<CVD::Rgb<long double>>(av[i]);
341
342 loadsave_safe<CVD::Rgb8>(av[i]);
343 loadsave_safe<CVD::Rgba<unsigned char>>(av[i]);
344 loadsave_safe<CVD::Rgba<unsigned int>>(av[i]);
345 }
346
347 //Test some of the variations
348 {
349 std::stringstream str;
350 Image<unsigned char> im(ImageRef(32, 32), 0);
351 img_save(im, str, ImageType::PNM);
352
353 str.seekg(0);
354 im = img_load(str);
355
356 str.seekg(0);
357 im = img_load<PNM::Reader>(str);
358
359 str.seekg(0);
360 im = img_load<PNM::Reader, FITS::Reader>(str);
361
362 str.seekg(0);
363 im = img_load<std::tuple<PNM::Reader, FITS::Reader>>(str);
364
365 str.seekg(0);
366 im = img_load<std::tuple<PNM::Reader>>(str);
367
368 try
369 {
370 str.seekg(0);
371 im = img_load<FITS::Reader>(str);
372 throw std::logic_error("Image load of the wrong type did not succeed");
373 }
374 catch(const CVD::Exceptions::Image_IO::UnsupportedImageType&)
375 {
376 }
377 }
378
379 cerr << "Testing TEXT (type " << ImageType::TEXT << ")\n";
380 randtest<
381 TypeList<double,
382 TypeList<float,
383 Head>>>::exec(ImageType::TEXT);
384
385 cerr << "Testing BMP (type " << ImageType::BMP << ")\n";
386 randtest<
387 TypeList<byte,
388 TypeList<Rgb<byte>,
389 Head>>>::exec(ImageType::BMP);
390
391 cerr << "Testing PNM (type " << ImageType::PNM << ")\n";
392 randtest<
393 TypeList<byte,
394 TypeList<unsigned short,
395 TypeList<Rgb<byte>,
396 TypeList<Rgb<unsigned short>,
397 Head>>>>>::exec(ImageType::PNM);
398
399 cerr << "Testing PNM (type " << ImageType::PNM << "), text I/O\n";
400 map<string, Parameter<>> p;
401 p["pnm.raw"] = Parameter<bool>(0);
402 randtest<
403 TypeList<byte,
404 TypeList<unsigned short,
405 TypeList<Rgb<byte>,
406 TypeList<Rgb<unsigned short>,
407 Head>>>>>::exec(ImageType::PNM, p);
408
409 cerr << "Testing FITS (type " << ImageType::FITS << ")\n";
410 randtest<
411 TypeList<byte,
412 TypeList<short,
413 TypeList<unsigned short,
414 TypeList<int,
415 TypeList<float,
416 TypeList<double,
417 TypeList<Rgb<byte>,
418 TypeList<Rgb<short>,
419 TypeList<Rgb<unsigned short>,
420 TypeList<Rgb<int>,
421 TypeList<Rgb<float>,
422 TypeList<Rgb<double>,
423 TypeList<Rgba<byte>,
424 TypeList<Rgba<short>,
425 TypeList<Rgba<unsigned short>,
426 TypeList<Rgba<int>,
427 TypeList<Rgba<float>,
428 TypeList<Rgba<double>,
429 Head>>>>>>>>>>>>>>>>>>>::exec(ImageType::FITS);
430
431 #ifdef CVD_HAVE_PNG
432 cerr << "Testing PNG (type " << ImageType::PNG << ")\n";
433 randtest<
434 TypeList<bool,
435 TypeList<byte,
436 TypeList<unsigned short,
437 TypeList<Rgb<byte>,
438 TypeList<Rgb<unsigned short>,
439 TypeList<Rgba<byte>,
440 TypeList<Rgba<unsigned short>,
441 Head>>>>>>>>::exec(ImageType::PNG);
442 #endif
443 #ifdef CVD_HAVE_TIFF
444 cerr << "Testing TIFF (type " << ImageType::TIFF << ")\n";
445 randtest<
446 TypeList<bool,
447 TypeList<byte,
448 TypeList<unsigned short,
449 TypeList<float,
450 TypeList<double,
451
452 TypeList<Rgb<byte>,
453 TypeList<Rgb<unsigned short>,
454 TypeList<Rgb<float>,
455 TypeList<Rgb<double>,
456
457 TypeList<Rgba<byte>,
458 TypeList<Rgba<unsigned short>,
459 TypeList<Rgba<float>,
460 TypeList<Rgba<double>,
461
462 Head>>>>>>>>>>>>>>::exec(ImageType::TIFF);
463 #endif
464 cerr << "Testing CVD (type " << ImageType::CVD << ")\n";
465 randtest<
466 TypeList<byte,
467 TypeList<bayer_bggr,
468 TypeList<bayer_rggb,
469 TypeList<bayer_grbg,
470 TypeList<bayer_gbrg,
471 TypeList<Rgb<byte>,
472 TypeList<Rgba<byte>,
473 Head>>>>>>>>::exec(ImageType::CVD);
474
475 exit(0);
476 }