/* 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 #include using namespace CVD; using std::cerr; using std::clog; using std::endl; using std::equal; using std::ifstream; using std::ios; using std::ios_base; using std::map; using std::max; using std::min; using std::ofstream; using std::string; using std::stringstream; using CVD::Internal::Head; using CVD::Internal::TypeList; std::mt19937 rng; template struct randpix { static C r() { union { unsigned int i; C c; } u; u.i = rand(); return u.c; } }; template <> struct randpix { static bool r() { return std::uniform_int_distribution<>(0, 1)(rng); } }; template <> struct randpix { static byte r() { return static_cast(std::uniform_int_distribution<>(0, 255)(rng)); } }; template <> struct randpix { static byte r() { return static_cast(std::uniform_int_distribution<>(0, 255)(rng)); } }; template <> struct randpix { static byte r() { return static_cast(std::uniform_int_distribution<>(0, 255)(rng)); } }; template <> struct randpix { static byte r() { return static_cast(std::uniform_int_distribution<>(0, 255)(rng)); } }; template <> struct randpix { static double r() { return rand() * 10. / RAND_MAX - 5; } }; template <> struct randpix { static float r() { return std::uniform_real_distribution(-5, 5)(rng); } }; 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 = static_cast(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 clog << "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"); clog << "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"); clog << "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"); clog << "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"); clog << "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(const 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(256 + i, 256 + 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"; 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.size().area() / (maxval - minval) << "% per pixel\n"; cerr << "Min is: " << minval << endl; cerr << "Max is: " << maxval << endl; exit(1); } else cerr << "OK.\n"; } catch(const 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]); } //Test some of the variations { std::stringstream str; Image im(ImageRef(32, 32), 0); img_save(im, str, ImageType::PNM); str.seekg(0); im = img_load(str); str.seekg(0); im = img_load(str); str.seekg(0); im = img_load(str); str.seekg(0); im = img_load>(str); str.seekg(0); im = img_load>(str); try { str.seekg(0); im = img_load(str); throw std::logic_error("Image load of the wrong type did not succeed"); } catch(const CVD::Exceptions::Image_IO::UnsupportedImageType&) { } } 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); }