| 1 |
#ifndef CVD_INTERNAL_IO_PNG_H |
| 2 |
#define CVD_INTERNAL_IO_PNG_H |
| 3 |
|
| 4 |
#include <iostream> |
| 5 |
#include <memory> |
| 6 |
#include <string> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
#include <cvd/image.h> |
| 10 |
#include <cvd/internal/convert_pixel_types.h> |
| 11 |
#include <cvd/internal/load_and_save.h> |
| 12 |
|
| 13 |
namespace CVD |
| 14 |
{ |
| 15 |
namespace PNG |
| 16 |
{ |
| 17 |
|
| 18 |
using CVD::Internal::Head; |
| 19 |
using CVD::Internal::TypeList; |
| 20 |
|
| 21 |
class PNGPimpl; |
| 22 |
class WriterPimpl; |
| 23 |
|
| 24 |
class Reader |
| 25 |
{ |
| 26 |
public: |
| 27 |
Reader(std::istream&); |
| 28 |
~Reader(); |
| 29 |
|
| 30 |
ImageRef size(); |
| 31 |
bool top_row_first(); |
| 32 |
|
| 33 |
void get_raw_pixel_line(bool*); |
| 34 |
void get_raw_pixel_line(unsigned char*); |
| 35 |
void get_raw_pixel_line(unsigned short*); |
| 36 |
void get_raw_pixel_line(Rgb<unsigned char>*); |
| 37 |
void get_raw_pixel_line(Rgb<unsigned short>*); |
| 38 |
void get_raw_pixel_line(Rgba<unsigned char>*); |
| 39 |
void get_raw_pixel_line(Rgba<unsigned short>*); |
| 40 |
|
| 41 |
std::string datatype(); |
| 42 |
std::string name(); |
| 43 |
|
| 44 |
typedef TypeList<bool, |
| 45 |
TypeList<byte, |
| 46 |
TypeList<unsigned short, |
| 47 |
TypeList<Rgb<byte>, |
| 48 |
TypeList<Rgb<unsigned short>, |
| 49 |
TypeList<Rgba<byte>, |
| 50 |
TypeList<Rgba<unsigned short>, |
| 51 |
Head>>>>>>> |
| 52 |
Types; |
| 53 |
static bool first_byte_matches(const int b) |
| 54 |
{ |
| 55 |
return b == 0x89; |
| 56 |
} |
| 57 |
|
| 58 |
private: |
| 59 |
std::unique_ptr<PNGPimpl> p; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
template <int g1, int g8> |
| 71 |
struct IntMapper |
| 72 |
{ |
| 73 |
typedef unsigned short type; |
| 74 |
}; |
| 75 |
template <> |
| 76 |
struct IntMapper<1, 0> |
| 77 |
{ |
| 78 |
typedef unsigned char type; |
| 79 |
}; |
| 80 |
template <> |
| 81 |
struct IntMapper<0, 0> |
| 82 |
{ |
| 83 |
typedef bool type; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
template <class ComponentIn, int is_integral> |
| 88 |
struct ComponentMapper_ |
| 89 |
{ |
| 90 |
typedef typename IntMapper< |
| 91 |
(Pixel::traits<ComponentIn>::bits_used > 1), |
| 92 |
(Pixel::traits<ComponentIn>::bits_used > 8)>::type type; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
template <class ComponentIn> |
| 97 |
struct ComponentMapper_<ComponentIn, 0> |
| 98 |
{ |
| 99 |
typedef unsigned short type; |
| 100 |
}; |
| 101 |
|
| 102 |
template <class ComponentIn> |
| 103 |
struct ComponentMapper |
| 104 |
{ |
| 105 |
typedef typename ComponentMapper_<ComponentIn, Pixel::traits<ComponentIn>::integral>::type type; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
template <class ComponentIn> |
| 110 |
struct ComponentMapper<Rgb<ComponentIn>> |
| 111 |
{ |
| 112 |
typedef Rgb<typename ComponentMapper_<ComponentIn, Pixel::traits<ComponentIn>::integral>::type> type; |
| 113 |
}; |
| 114 |
|
| 115 |
template <class ComponentIn> |
| 116 |
struct ComponentMapper<Rgba<ComponentIn>> |
| 117 |
{ |
| 118 |
typedef Rgba<typename ComponentMapper_<ComponentIn, Pixel::traits<ComponentIn>::integral>::type> type; |
| 119 |
}; |
| 120 |
|
| 121 |
template <> |
| 122 |
struct ComponentMapper<Rgb8> |
| 123 |
{ |
| 124 |
typedef Rgb8 type; |
| 125 |
}; |
| 126 |
|
| 127 |
class png_writer |
| 128 |
{ |
| 129 |
public: |
| 130 |
png_writer(std::ostream&, ImageRef size, const std::string& type, const std::map<std::string, Parameter<>>& p); |
| 131 |
~png_writer(); |
| 132 |
|
| 133 |
void write_raw_pixel_line(const bool*); |
| 134 |
void write_raw_pixel_line(const unsigned char*); |
| 135 |
void write_raw_pixel_line(const unsigned short*); |
| 136 |
void write_raw_pixel_line(const Rgb<unsigned char>*); |
| 137 |
void write_raw_pixel_line(const Rgb8*); |
| 138 |
void write_raw_pixel_line(const Rgb<unsigned short>*); |
| 139 |
void write_raw_pixel_line(const Rgba<unsigned char>*); |
| 140 |
void write_raw_pixel_line(const Rgba<unsigned short>*); |
| 141 |
|
| 142 |
template <class Incoming> |
| 143 |
struct Outgoing |
| 144 |
{ |
| 145 |
typedef typename ComponentMapper<Incoming>::type type; |
| 146 |
}; |
| 147 |
static const int top_row_first = 1; |
| 148 |
|
| 149 |
private: |
| 150 |
std::unique_ptr<WriterPimpl> p; |
| 151 |
}; |
| 152 |
|
| 153 |
} |
| 154 |
} |
| 155 |
#endif |