/* This file is part of the CVD Library. Copyright (C) 2005 The Authors This library is free software, see LICENSE file for details */ #include #include #include #include #include #include #include #include using namespace std; using namespace CVD; using CVD::Internal::Head; using CVD::Internal::TypeList; template struct randpix { static C r() { union { unsigned int i; C c; } u; u.i = rand(); return u.c; } }; template <> struct randpix { static double r() { return rand() % 2; } }; template <> struct randpix { static double r() { return rand() % 256; } }; template <> struct randpix { static double r() { return rand() % 256; } }; template <> struct randpix { static double r() { return rand() % 256; } }; template <> struct randpix { static double r() { return rand() % 256; } }; template <> struct randpix { static double r() { return rand() * 10. / RAND_MAX - 5; } }; template <> struct randpix { static float r() { return rand() * 10. / RAND_MAX - 5; } }; template struct randpix> { static Rgb r() { return Rgb(randpix::r(), randpix::r(), randpix::r()); } }; template struct randpix> { static Rgba r() { return Rgba(randpix::r(), randpix::r(), randpix::r(), randpix::r()); } }; template string make_output_file_name(string fin, string type) { std::string fn; //T tmp; //Remove the path from the filename for(int i = fin.size() - 1; i >= 0; i--) { if(fin[i] != '/') fn = fin[i] + fn; else break; } fn = " from " + fn; fn = PNM::type_name::name() + fn; Image im; string fout = "testout/" + fn + "." + type; return fout; } template void loadsave(string fin) { Image im; //Load the image cout << "Reading: " << fin << endl; ifstream i(fin.c_str(), ios::in | ios::binary); img_load(im, i); i.close(); string fout = make_output_file_name(fin, "pnm"); cout << "Writing: " << fout << endl << endl; //Save it ofstream o(fout.c_str(), ios::out | ios::binary); img_save(im, o, ImageType::PNM); o.close(); //Save it #ifdef CVD_HAVE_JPEG fout = make_output_file_name(fin, "jpg"); cout << "Writing: " << fout << endl << endl; o.open(fout.c_str()); img_save(im, o, ImageType::JPEG); o.close(); #endif #ifdef CVD_HAVE_PNG fout = make_output_file_name(fin, "png"); cout << "Writing: " << fout << endl << endl; o.open(fout.c_str()); img_save(im, o, ImageType::PNG); o.close(); #endif #ifdef CVD_HAVE_TIFF fout = make_output_file_name(fin, "tiff"); cout << "Writing: " << fout << endl << endl; o.open(fout.c_str()); img_save(im, o, ImageType::TIFF); o.close(); #endif } template void loadsave_safe(const char* n) { try { loadsave(n); } catch(CVD::Exceptions::All b0rk3d) { cerr << "Image " << n << " error: " << b0rk3d.what << endl; } } map> empty() { map> nothing; return nothing; } template struct randtest { typedef typename T::Type Type; static void exec(ImageType::ImageType fmt, const map>& p = empty()) { for(int i = 0; i < 10; i++) { try { //Make a random image Image in(ImageRef(1000 + i, 1000 + i)), out; cerr << "Testing " << in.size() << " " << fmt << " " << CVD::PNM::type_name::name() << " "; for(int y = 0; y < in.size().y; y++) for(int x = 0; x < in.size().x; x++) in[y][x] = randpix::r(); stringstream s; //Save the image img_save(in, s, fmt, p); s.seekg(0, ios_base::beg); s.seekp(0, ios_base::beg); //Load the image out = img_load(s); //Compare the results if(out.size() != in.size()) cerr << " size mismatch.\n"; else if(!equal(in.begin(), in.end(), out.begin())) { cerr << " data mismatch.\n"; typedef typename Pixel::Component::type Ct; double t = 0, minval = HUGE_VAL, maxval = -HUGE_VAL; for(int y = 0; y < in.size().y; y++) for(int x = 0; x < in.size().x; x++) for(unsigned int c = 0; c < Pixel::Component::count; c++) { double p = Pixel::Component::get(in[y][x], c); double p2 = Pixel::Component::get(out[y][x], c); t += abs(p - p2); maxval = max(maxval, p); minval = min(minval, p); cerr << p << " " << p2 << endl; } cerr << "Mismatch is " << 100 * t * 1.0 / in.totalsize() / (maxval - minval) << "% per pixel\n"; cerr << "Min is: " << minval << endl; cerr << "Max is: " << maxval << endl; } else cerr << "OK.\n"; } catch(Exceptions::All w) { cerr << w.what << endl; } } randtest::exec(fmt, p); } }; template <> struct randtest { static void exec(ImageType::ImageType, const map>& = empty()) { } }; int main(int ac, char** av) { for(int i = 1; i < ac; i++) { loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); loadsave_safe(av[i]); loadsave_safe>(av[i]); loadsave_safe>(av[i]); } cerr << "Testing TEXT (type " << ImageType::TEXT << ")\n"; randtest< TypeList>>::exec(ImageType::TEXT); cerr << "Testing BMP (type " << ImageType::BMP << ")\n"; randtest< TypeList, Head>>>::exec(ImageType::BMP); cerr << "Testing PNM (type " << ImageType::PNM << ")\n"; randtest< TypeList, TypeList, Head>>>>>::exec(ImageType::PNM); cerr << "Testing PNM (type " << ImageType::PNM << "), text I/O\n"; map> p; p["pnm.raw"] = Parameter(0); randtest< TypeList, TypeList, Head>>>>>::exec(ImageType::PNM, p); cerr << "Testing FITS (type " << ImageType::FITS << ")\n"; randtest< TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, Head>>>>>>>>>>>>>>>>>>>::exec(ImageType::FITS); #ifdef CVD_HAVE_PNG cerr << "Testing PNG (type " << ImageType::PNG << ")\n"; randtest< TypeList, TypeList, TypeList, TypeList, Head>>>>>>>>::exec(ImageType::PNG); #endif #ifdef CVD_HAVE_TIFF cerr << "Testing TIFF (type " << ImageType::TIFF << ")\n"; randtest< TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, TypeList, Head>>>>>>>>>>>>>>::exec(ImageType::TIFF); #endif cerr << "Testing CVD (type " << ImageType::CVD << ")\n"; randtest< TypeList, TypeList, Head>>>>>>>>::exec(ImageType::CVD); exit(0); }