image readers: ability to check if image has alpha before full read

Conflicts:
	src/webp_reader.cpp
This commit is contained in:
Dane Springmeyer 2014-06-11 18:50:58 -07:00
parent 6e6c9f21ac
commit 8bb77bce92
5 changed files with 20 additions and 5 deletions

View file

@ -57,6 +57,7 @@ struct MAPNIK_DECL image_reader : private mapnik::noncopyable
{ {
virtual unsigned width() const=0; virtual unsigned width() const=0;
virtual unsigned height() const=0; virtual unsigned height() const=0;
virtual bool has_alpha() const=0;
virtual bool premultiplied_alpha() const=0; virtual bool premultiplied_alpha() const=0;
virtual void read(unsigned x,unsigned y,image_data_32& image)=0; virtual void read(unsigned x,unsigned y,image_data_32& image)=0;
virtual ~image_reader() {} virtual ~image_reader() {}

View file

@ -80,6 +80,7 @@ public:
~jpeg_reader(); ~jpeg_reader();
unsigned width() const; unsigned width() const;
unsigned height() const; unsigned height() const;
inline bool has_alpha() const { return false; }
inline bool premultiplied_alpha() const { return true; } inline bool premultiplied_alpha() const { return true; }
void read(unsigned x,unsigned y,image_data_32& image); void read(unsigned x,unsigned y,image_data_32& image);
private: private:

View file

@ -69,12 +69,14 @@ private:
unsigned height_; unsigned height_;
int bit_depth_; int bit_depth_;
int color_type_; int color_type_;
bool has_alpha_;
public: public:
explicit png_reader(std::string const& file_name); explicit png_reader(std::string const& file_name);
png_reader(char const* data, std::size_t size); png_reader(char const* data, std::size_t size);
~png_reader(); ~png_reader();
unsigned width() const; unsigned width() const;
unsigned height() const; unsigned height() const;
inline bool has_alpha() const { return has_alpha_; }
bool premultiplied_alpha() const { return false; } //http://www.libpng.org/pub/png/spec/1.1/PNG-Rationale.html bool premultiplied_alpha() const { return false; } //http://www.libpng.org/pub/png/spec/1.1/PNG-Rationale.html
void read(unsigned x,unsigned y,image_data_32& image); void read(unsigned x,unsigned y,image_data_32& image);
private: private:
@ -129,7 +131,8 @@ png_reader<T>::png_reader(std::string const& file_name)
width_(0), width_(0),
height_(0), height_(0),
bit_depth_(0), bit_depth_(0),
color_type_(0) color_type_(0),
has_alpha_(false)
{ {
if (!source_.is_open()) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'"); if (!source_.is_open()) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'");
if (!stream_) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'"); if (!stream_) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'");
@ -143,7 +146,8 @@ png_reader<T>::png_reader(char const* data, std::size_t size)
width_(0), width_(0),
height_(0), height_(0),
bit_depth_(0), bit_depth_(0),
color_type_(0) color_type_(0),
has_alpha_(false)
{ {
if (!stream_) throw image_reader_exception("PNG reader: cannot open image stream"); if (!stream_) throw image_reader_exception("PNG reader: cannot open image stream");
@ -193,7 +197,7 @@ void png_reader<T>::init()
png_uint_32 width, height; png_uint_32 width, height;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0);
has_alpha_ = (color_type_ & PNG_COLOR_MASK_ALPHA) || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS);
width_=width; width_=width;
height_=height; height_=height;

View file

@ -139,6 +139,7 @@ public:
virtual ~tiff_reader(); virtual ~tiff_reader();
unsigned width() const; unsigned width() const;
unsigned height() const; unsigned height() const;
inline bool has_alpha() const { return false; /*FIXME*/ }
bool premultiplied_alpha() const; bool premultiplied_alpha() const;
void read(unsigned x,unsigned y,image_data_32& image); void read(unsigned x,unsigned y,image_data_32& image);
private: private:

View file

@ -108,12 +108,14 @@ private:
size_t size_; size_t size_;
unsigned width_; unsigned width_;
unsigned height_; unsigned height_;
bool has_alpha_;
public: public:
explicit webp_reader(char const* data, std::size_t size); explicit webp_reader(char const* data, std::size_t size);
explicit webp_reader(std::string const& filename); explicit webp_reader(std::string const& filename);
~webp_reader(); ~webp_reader();
unsigned width() const; unsigned width() const;
unsigned height() const; unsigned height() const;
inline bool has_alpha() const { return has_alpha_; }
bool premultiplied_alpha() const { return false; } bool premultiplied_alpha() const { return false; }
void read(unsigned x,unsigned y,image_data_32& image); void read(unsigned x,unsigned y,image_data_32& image);
private: private:
@ -143,7 +145,8 @@ template <typename T>
webp_reader<T>::webp_reader(char const* data, std::size_t size) webp_reader<T>::webp_reader(char const* data, std::size_t size)
: buffer_(new buffer_policy_type(reinterpret_cast<uint8_t const*>(data), size)), : buffer_(new buffer_policy_type(reinterpret_cast<uint8_t const*>(data), size)),
width_(0), width_(0),
height_(0) height_(0),
has_alpha_(false)
{ {
init(); init();
} }
@ -153,7 +156,8 @@ webp_reader<T>::webp_reader(std::string const& filename)
: buffer_(nullptr), : buffer_(nullptr),
size_(0), size_(0),
width_(0), width_(0),
height_(0) height_(0),
has_alpha_(false)
{ {
std::ifstream file(filename.c_str(), std::ios::binary); std::ifstream file(filename.c_str(), std::ios::binary);
if (!file) if (!file)
@ -199,6 +203,10 @@ void webp_reader<T>::init()
height_ = config.input.height; height_ = config.input.height;
has_alpha_ = config.input.has_alpha; has_alpha_ = config.input.has_alpha;
} }
else
{
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
}
} }
template <typename T> template <typename T>