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

This commit is contained in:
Dane Springmeyer 2014-06-11 18:50:58 -07:00
parent 96a93c4b14
commit 8c5049e0f8
5 changed files with 26 additions and 11 deletions

View file

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

View file

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

View file

@ -66,12 +66,14 @@ private:
unsigned height_;
int bit_depth_;
int color_type_;
bool has_alpha_;
public:
explicit png_reader(std::string const& file_name);
png_reader(char const* data, std::size_t size);
~png_reader();
unsigned width() 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
void read(unsigned x,unsigned y,image_data_32& image);
private:
@ -126,7 +128,8 @@ png_reader<T>::png_reader(std::string const& file_name)
width_(0),
height_(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 (!stream_) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'");
@ -140,7 +143,8 @@ png_reader<T>::png_reader(char const* data, std::size_t size)
width_(0),
height_(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");
@ -190,7 +194,7 @@ void png_reader<T>::init()
png_uint_32 width, height;
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;
height_=height;

View file

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

View file

@ -106,12 +106,14 @@ private:
size_t size_;
unsigned width_;
unsigned height_;
bool has_alpha_;
public:
explicit webp_reader(char const* data, std::size_t size);
explicit webp_reader(std::string const& filename);
~webp_reader();
unsigned width() const;
unsigned height() const;
inline bool has_alpha() const { return has_alpha_; }
bool premultiplied_alpha() const { return false; }
void read(unsigned x,unsigned y,image_data_32& image);
private:
@ -141,7 +143,8 @@ template <typename T>
webp_reader<T>::webp_reader(char const* data, std::size_t size)
: buffer_(new buffer_policy_type(reinterpret_cast<uint8_t const*>(data), size)),
width_(0),
height_(0)
height_(0),
has_alpha_(false)
{
init();
}
@ -151,7 +154,8 @@ webp_reader<T>::webp_reader(std::string const& filename)
: buffer_(),
size_(0),
width_(0),
height_(0)
height_(0),
has_alpha_(false)
{
std::ifstream file(filename.c_str(), std::ios::binary);
if (!file)
@ -183,13 +187,17 @@ webp_reader<T>::~webp_reader()
template <typename T>
void webp_reader<T>::init()
{
int width, height;
if (!WebPGetInfo(buffer_->data(), buffer_->size(), &width, &height))
{
throw image_reader_exception("WEBP reader: WebPGetInfo failed");
WebPDecoderConfig config;
config_guard guard(config);
if (WebPGetFeatures(buffer_->data(), buffer_->size(), &config.input) == VP8_STATUS_OK) {
width_ = config.input.width;
height_ = config.input.height;
has_alpha_ = config.input.has_alpha;
}
else
{
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
}
width_ = width;
height_ = height;
}
template <typename T>