| 1 |
#ifndef CVD_LOAD_AND_SAVE_H |
| 2 |
#define CVD_LOAD_AND_SAVE_H |
| 3 |
|
| 4 |
#include <cvd/colourspace_convert.h> |
| 5 |
#include <cvd/convert_image.h> |
| 6 |
#include <cvd/exceptions.h> |
| 7 |
#include <cvd/internal/convert_pixel_types.h> |
| 8 |
#include <cvd/internal/io/parameter.h> |
| 9 |
#include <cvd/internal/name_CVD_rgb_types.h> |
| 10 |
#include <iostream> |
| 11 |
#include <map> |
| 12 |
#include <memory> |
| 13 |
#include <string> |
| 14 |
#include <typeinfo> |
| 15 |
|
| 16 |
namespace CVD |
| 17 |
{ |
| 18 |
|
| 19 |
namespace Exceptions |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
namespace Image_IO |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
struct All : public CVD::Exceptions::All |
| 28 |
{ |
| 29 |
using CVD::Exceptions::All::All; |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
struct MissingImageType : public All |
| 35 |
{ |
| 36 |
MissingImageType(const std::string& type); |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
struct UnsupportedImageType : public All |
| 42 |
{ |
| 43 |
UnsupportedImageType(); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
struct EofBeforeImage : public All |
| 49 |
{ |
| 50 |
EofBeforeImage(); |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
struct IfstreamNotOpen : public All |
| 56 |
{ |
| 57 |
IfstreamNotOpen(); |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
struct MalformedImage : public All |
| 63 |
{ |
| 64 |
MalformedImage(const std::string&); |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
struct ImageSizeMismatch : public All |
| 70 |
{ |
| 71 |
ImageSizeMismatch(const ImageRef& src, const ImageRef& dest); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
struct WriteError : public All |
| 77 |
{ |
| 78 |
WriteError(const std::string& err); |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
struct UnseekableIstream : public All |
| 84 |
{ |
| 85 |
UnseekableIstream(const std::string& type); |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
struct ReadTypeMismatch : public All |
| 91 |
{ |
| 92 |
ReadTypeMismatch(bool read8); |
| 93 |
ReadTypeMismatch(const std::string& available, const std::string& requested); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
struct WriteTypeMismatch : public All |
| 99 |
{ |
| 100 |
WriteTypeMismatch(const std::string& available, const std::string& requested); |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
struct InternalLibraryError : public All |
| 106 |
{ |
| 107 |
InternalLibraryError(const std::string& lib, const std::string err); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
struct UnsupportedImageSubType : public All |
| 113 |
{ |
| 114 |
UnsupportedImageSubType(const std::string&, const std::string&); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
struct OpenError : public All |
| 120 |
{ |
| 121 |
OpenError(const std::string&, const std::string&, int); |
| 122 |
}; |
| 123 |
|
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
namespace Internal |
| 128 |
{ |
| 129 |
template <class C, int i> |
| 130 |
struct save_default_ |
| 131 |
{ |
| 132 |
static const bool use_16bit = 1; |
| 133 |
}; |
| 134 |
|
| 135 |
template <class C> |
| 136 |
struct save_default_<C, 1> |
| 137 |
{ |
| 138 |
static const bool use_16bit = (CVD::Pixel::traits<typename CVD::Pixel::Component<C>::type>::bits_used) > 8; |
| 139 |
}; |
| 140 |
|
| 141 |
template <class C> |
| 142 |
struct save_default |
| 143 |
{ |
| 144 |
static const bool use_16bit = save_default_<C, CVD::Pixel::traits<typename CVD::Pixel::Component<C>::type>::integral>::use_16bit; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
struct Head |
| 160 |
{ |
| 161 |
}; |
| 162 |
template <class A, class B> |
| 163 |
struct TypeList |
| 164 |
{ |
| 165 |
typedef A Type; |
| 166 |
typedef B Next; |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
template <class PixelType, class DiskPixelType, class ImageLoader> |
| 175 |
struct read_and_maybe_process |
| 176 |
{ |
| 177 |
static void exec(BasicImage<PixelType>& im, ImageLoader& r) |
| 178 |
{ |
| 179 |
Image<DiskPixelType> rowbuf(ImageRef(r.size().x, 1)); |
| 180 |
|
| 181 |
for(int row = 0; row < r.size().y; row++) |
| 182 |
{ |
| 183 |
r.get_raw_pixel_line(rowbuf.data()); |
| 184 |
PixelType* rowptr; |
| 185 |
|
| 186 |
if(r.top_row_first()) |
| 187 |
rowptr = im[row]; |
| 188 |
else |
| 189 |
rowptr = im[im.size().y - row - 1]; |
| 190 |
|
| 191 |
Pixel::ConvertPixels<DiskPixelType, PixelType>::convert(rowbuf.data(), rowptr, r.size().x); |
| 192 |
} |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
template <class PixelType, class ImageLoader> |
| 197 |
struct read_and_maybe_process<PixelType, PixelType, ImageLoader> |
| 198 |
{ |
| 199 |
static void exec(BasicImage<PixelType>& im, ImageLoader& r) |
| 200 |
{ |
| 201 |
for(int row = 0; row < r.size().y; row++) |
| 202 |
if(r.top_row_first()) |
| 203 |
r.get_raw_pixel_line(im[row]); |
| 204 |
else |
| 205 |
r.get_raw_pixel_line(im[im.size().y - row - 1]); |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
template <class PixelType, class DiskPixelType, class ImageLoader> |
| 210 |
struct read_and_then_process |
| 211 |
{ |
| 212 |
static void exec(BasicImage<PixelType>& im, ImageLoader& r) |
| 213 |
{ |
| 214 |
Image<DiskPixelType> imgbuf(r.size()); |
| 215 |
read_and_maybe_process<DiskPixelType, DiskPixelType, ImageLoader>::exec(imgbuf, r); |
| 216 |
|
| 217 |
convert_image(imgbuf, im); |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
template <class PixelType, class ImageLoader, class List> |
| 226 |
struct Reader |
| 227 |
{ |
| 228 |
static void read(BasicImage<PixelType>& im, ImageLoader& r) |
| 229 |
{ |
| 230 |
if(r.datatype() == PNM::type_name<typename List::Type>::name()) |
| 231 |
{ |
| 232 |
|
| 233 |
if(PixelByPixelConvertible<typename List::Type, PixelType>::is) |
| 234 |
read_and_maybe_process<PixelType, typename List::Type, ImageLoader>::exec(im, r); |
| 235 |
else if(IsConvertible<typename List::Type, PixelType>::is) |
| 236 |
read_and_then_process<PixelType, typename List::Type, ImageLoader>::exec(im, r); |
| 237 |
else |
| 238 |
throw CVD::Exceptions::Image_IO::ReadTypeMismatch(r.datatype(), PNM::type_name<PixelType>::name()); |
| 239 |
} |
| 240 |
else |
| 241 |
Reader<PixelType, ImageLoader, typename List::Next>::read(im, r); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
template <class PixelType, class ImageLoader> |
| 246 |
struct Reader<PixelType, ImageLoader, Head> |
| 247 |
{ |
| 248 |
static void read(BasicImage<PixelType>&, ImageLoader& r) |
| 249 |
{ |
| 250 |
throw Exceptions::Image_IO::UnsupportedImageSubType(r.name(), r.datatype() + " not yet supported"); |
| 251 |
} |
| 252 |
}; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
template <class T, class ImageLoader> |
| 260 |
void readImage(BasicImage<T>& im, ImageLoader& r) |
| 261 |
{ |
| 262 |
Reader<T, ImageLoader, typename ImageLoader::Types>::read(im, r); |
| 263 |
} |
| 264 |
|
| 265 |
template <class T, class ImageLoader> |
| 266 |
void readImage(BasicImage<T>& im, std::istream& in) |
| 267 |
{ |
| 268 |
ImageLoader loader(in); |
| 269 |
ImageRef size = loader.size(); |
| 270 |
if(im.size() != size) |
| 271 |
throw Exceptions::Image_IO::ImageSizeMismatch(size, im.size()); |
| 272 |
|
| 273 |
readImage(im, loader); |
| 274 |
} |
| 275 |
|
| 276 |
template <class T, class ImageLoader> |
| 277 |
void readImage(Image<T>& im, std::istream& in) |
| 278 |
{ |
| 279 |
ImageLoader loader(in); |
| 280 |
im.resize(loader.size()); |
| 281 |
readImage(im, loader); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
template <class Pixel, class ImageWriter, class OutgoingPixel> |
| 305 |
struct maybe_process_and_write |
| 306 |
{ |
| 307 |
static void write(std::ostream& os, const BasicImage<Pixel>& im, const std::map<std::string, Parameter<>>& p) |
| 308 |
{ |
| 309 |
ImageWriter w(os, im.size(), CVD::PNM::type_name<OutgoingPixel>::name(), p); |
| 310 |
Image<OutgoingPixel> row(ImageRef(im.size().x, 1)); |
| 311 |
|
| 312 |
if(w.top_row_first) |
| 313 |
for(int r = 0; r < im.size().y; r++) |
| 314 |
{ |
| 315 |
CVD::Pixel::ConvertPixels<Pixel, OutgoingPixel>::convert(im[r], row.data(), im.size().x); |
| 316 |
w.write_raw_pixel_line(row.data()); |
| 317 |
} |
| 318 |
else |
| 319 |
for(int r = im.size().y - 1; r >= 0; r--) |
| 320 |
{ |
| 321 |
CVD::Pixel::ConvertPixels<Pixel, OutgoingPixel>::convert(im[r], row.data(), im.size().x); |
| 322 |
w.write_raw_pixel_line(row.data()); |
| 323 |
} |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
template <class Pixel, class ImageWriter> |
| 328 |
struct maybe_process_and_write<Pixel, ImageWriter, Pixel> |
| 329 |
{ |
| 330 |
static void write(std::ostream& os, const BasicImage<Pixel>& im, const std::map<std::string, Parameter<>>& p) |
| 331 |
{ |
| 332 |
ImageWriter w(os, im.size(), CVD::PNM::type_name<Pixel>::name(), p); |
| 333 |
|
| 334 |
if(w.top_row_first) |
| 335 |
for(int r = 0; r < im.size().y; r++) |
| 336 |
w.write_raw_pixel_line(im[r]); |
| 337 |
else |
| 338 |
for(int r = im.size().y - 1; r >= 0; r--) |
| 339 |
w.write_raw_pixel_line(im[r]); |
| 340 |
} |
| 341 |
}; |
| 342 |
|
| 343 |
template <class Pixel, class Writer> |
| 344 |
void writeImage(const BasicImage<Pixel>& im, std::ostream& o, const std::map<std::string, Parameter<>>& p) |
| 345 |
{ |
| 346 |
maybe_process_and_write<Pixel, Writer, typename Writer::template Outgoing<Pixel>::type>::write(o, im, p); |
| 347 |
} |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
#endif |