| 1 | #include "cvd/image_io.h" |
| 2 | #include "cvd/internal/load_and_save.h" |
| 3 | |
| 4 | #include <cstring> |
| 5 | #include <fstream> |
| 6 | #include <sstream> |
| 7 | |
| 8 | using namespace std; |
| 9 | namespace CVD |
| 10 | { |
| 11 | |
| 12 | Exceptions::Image_IO::ImageSizeMismatch::ImageSizeMismatch(const ImageRef& src, const ImageRef& dest) |
| 13 | : All([=]() { |
| 14 | ostringstream o; |
| 15 | o << "Image load: Size mismatch when loading an image (size " << src << ") in to a non\ |
| 16 | resizable image (size " |
| 17 | << dest << ")."; |
| 18 | |
| 19 | return o.str(); |
| 20 | }()) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | Exceptions::Image_IO::MissingImageType::MissingImageType(const std::string& type) |
| 25 | : All("Image input: " + type + " support is not compiled in.") |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | Exceptions::Image_IO::OpenError::OpenError(const string& name, const string& why, int error) |
| 30 | : All("Opening file: " + name + " (" + why + "): " + strerror(error)) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | Exceptions::Image_IO::MalformedImage::MalformedImage(const string& why) |
| 35 | : All("Image input: " + why) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | Exceptions::Image_IO::UnsupportedImageType::UnsupportedImageType() |
| 40 | : All("Image input: Unsuppported image type.") |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Exceptions::Image_IO::IfstreamNotOpen::IfstreamNotOpen() |
| 45 | : All("Image input: File stream has not been opened succesfully.") |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | Exceptions::Image_IO::EofBeforeImage::EofBeforeImage() |
| 50 | : All("Image input: End of file occured before image.") |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | Exceptions::Image_IO::WriteError::WriteError(const string& s) |
| 55 | : All("Error writing " + s) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | Exceptions::Image_IO::WriteTypeMismatch::WriteTypeMismatch(const string& avail, const string& req) |
| 60 | : All("Image output (CVD internal error): Attempting to write " + req + " data to a file containing " + avail) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | Exceptions::Image_IO::ReadTypeMismatch::ReadTypeMismatch(const string& avail, const string& req) |
| 65 | : All("Image input (CVD internal error): Attempting to read " + req + " data from a file containing " + avail) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | Exceptions::Image_IO::ReadTypeMismatch::ReadTypeMismatch(const bool read8) |
| 70 | : All(string("Image input (CVD internal error): Attempting to read ") + (read8 ? "8" : "16") + "bit data from " + (read8 ? "16" : "8") + "bit file (probably an internal error).") |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | Exceptions::Image_IO::UnseekableIstream::UnseekableIstream(const string& s) |
| 75 | : All("Image input: Loading " + s + " images requires seekable istream.") |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | Exceptions::Image_IO::UnsupportedImageSubType::UnsupportedImageSubType(const string& i, const string& why) |
| 80 | : All("Image input: Unsupported subtype of " + i + " image: " + why) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | Exceptions::Image_IO::InternalLibraryError::InternalLibraryError(const std::string& l, const std::string e) |
| 85 | : All("Internal error in " + l + " library: " + e) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | ImageType::ImageType string_to_image_type(const std::string& name) |
| 90 | { |
| 91 | size_t dot = name.rfind('.'); |
| 92 | if(dot == std::string::npos) |
| 93 | return ImageType::PNM; |
| 94 | |
| 95 | std::string suffix = name.substr(dot + 1, name.length() - dot - 1); |
| 96 | for(size_t i = 0; i < suffix.length(); i++) |
| 97 | suffix[i] = static_cast<char>(tolower(suffix[i])); |
| 98 | |
| 99 | if(suffix == "ps") |
| 100 | return ImageType::PS; |
| 101 | #ifdef CVD_HAVE_JPEG |
| 102 | else if(suffix == "jpg" || suffix == "jpeg") |
| 103 | return ImageType::JPEG; |
| 104 | #endif |
| 105 | #ifdef CVD_HAVE_PNG |
| 106 | else if(suffix == "png") |
| 107 | return ImageType::PNG; |
| 108 | #endif |
| 109 | #ifdef CVD_HAVE_TIFF |
| 110 | else if(suffix == "tif" || suffix == "tiff") |
| 111 | return ImageType::TIFF; |
| 112 | #endif |
| 113 | else if(suffix == "eps") |
| 114 | return ImageType::EPS; |
| 115 | else if(suffix == "bmp") |
| 116 | return ImageType::BMP; |
| 117 | else if(suffix == "pnm" || suffix == "ppm" || suffix == "pgm" || suffix == "pbm") |
| 118 | return ImageType::PNM; |
| 119 | else if(suffix == "txt") |
| 120 | return ImageType::TXT; |
| 121 | else if(suffix == "fits" || suffix == "fts") |
| 122 | return ImageType::FITS; |
| 123 | else if(suffix == "cvd") |
| 124 | return ImageType::CVD; |
| 125 | else |
| 126 | return ImageType::Unknown; |
| 127 | } |
| 128 | |
| 129 | } |