Debian Codegraph @

ad_hoc_tests/test_images.cxx

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