add image_reader abstraction layer to hanfle multiple image formats

(work-in-progress)
This commit is contained in:
artemp
2014-11-07 11:49:31 +00:00
committed by Leith Bade
parent 90480ee58c
commit ae2d4e740e
9 changed files with 884 additions and 113 deletions

View File

@@ -19,6 +19,7 @@
],
'ldflags': [
'<@(png_ldflags)',
'<@(jpeg_ldflags)',
'<@(uv_ldflags)',
'<@(curl_ldflags)',
'<@(nu_ldflags)',

View File

@@ -0,0 +1,50 @@
#ifndef MBGL_UTIL_FACTORY_HPP
#define MBGL_UTIL_FACTORY_HPP
#include "singleton.hpp"
// stl
#include <stdexcept>
#include <map>
namespace mbgl { namespace util {
template
<
typename product_type,
typename key_type,
typename ...Args >
class factory : public singleton<factory <product_type,
key_type,
Args...> >
{
private:
using product_creator = product_type* (*) (Args...);
using product_map = std::map<key_type,product_creator>;
product_map map_;
public:
bool register_product(key_type const& key, product_creator creator)
{
return map_.insert(typename product_map::value_type(key,creator)).second;
}
bool unregister_product(const key_type& key)
{
return map_.erase(key)==1;
}
product_type* create_object(key_type const& key, Args...args)
{
typename product_map::const_iterator pos=map_.find(key);
if (pos!=map_.end())
{
return (pos->second)(args...);
}
return 0;
}
};
}}
#endif

View File

@@ -0,0 +1,50 @@
#ifndef MBGL_UTIL_IMAGE_READER_HPP
#define MBGL_UTIL_IMAGE_READER_HPP
#include "factory.hpp"
#include "noncopyable.hpp"
// stl
#include <stdexcept>
#include <string>
namespace mbgl { namespace util {
class image_reader_exception : public std::exception
{
private:
std::string message_;
public:
image_reader_exception(std::string const& message)
: message_(message) {}
~image_reader_exception() throw() {}
virtual const char* what() const throw()
{
return message_.c_str();
}
};
struct image_reader : private 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, unsigned width, unsigned height, char* image)=0;
virtual ~image_reader() {}
};
template <typename...Args>
bool register_image_reader(std::string const& type, image_reader* (* fun)(Args...))
{
return factory<image_reader,std::string, Args...>::instance().register_product(type, fun);
}
//image_reader* get_image_reader(std::string const& file,std::string const& type);
//image_reader* get_image_reader(std::string const& file);
image_reader* get_image_reader(char const* data, size_t size);
}}
#endif

View File

@@ -0,0 +1,126 @@
#ifndef MBGL_UTIL_SINGLETON_HPP
#define MBGL_UTIL_SINGLETON_HPP
// stl
#include <stdexcept> // std::runtime_error
#include <cstdlib> // std::atexit
#include <new> // operator new
#define SINGLETON_THREADSAFE
#ifdef SINGLETON_THREADSAFE
#include <mutex>
#endif
namespace mbgl { namespace util {
template <typename T>
class CreateUsingNew
{
public:
static T* create()
{
return new T;
}
static void destroy(T* obj)
{
delete obj;
}
};
template <typename T>
class CreateStatic
{
private:
union MaxAlign
{
char t_[sizeof(T)];
short int shortInt_;
int int_;
long int longInt_;
float float_;
double double_;
long double longDouble_;
struct Test;
int Test::* pMember_;
int (Test::*pMemberFn_)(int);
};
public:
static T* create()
{
static MaxAlign staticMemory;
return new(&staticMemory) T;
}
static void destroy(volatile T* obj)
{
obj->~T();
}
};
template <typename T,
template <typename U> class CreatePolicy=CreateStatic> class singleton
{
friend class CreatePolicy<T>;
static T* pInstance_;
static bool destroyed_;
singleton(const singleton &rhs);
singleton& operator=(const singleton&);
static void onDeadReference()
{
throw std::runtime_error("dead reference!");
}
static void DestroySingleton()
{
CreatePolicy<T>::destroy(pInstance_);
pInstance_ = 0;
destroyed_ = true;
}
protected:
#ifdef SINGLETON_THREADSAFE
static std::mutex mutex_;
#endif
singleton() {}
public:
static T& instance()
{
if (! pInstance_)
{
#ifdef SINGLETON_THREADSAFE
std::unique_lock<std::mutex> lock(mutex_);
#endif
if (! pInstance_)
{
if (destroyed_)
{
destroyed_ = false;
onDeadReference();
}
else
{
pInstance_ = CreatePolicy<T>::create();
// register destruction
std::atexit(&DestroySingleton);
}
}
}
return *pInstance_;
}
};
#ifdef SINGLETON_THREADSAFE
template <typename T,
template <typename U> class CreatePolicy> std::mutex singleton<T,CreatePolicy>::mutex_;
#endif
template <typename T,
template <typename U> class CreatePolicy> T* singleton<T,CreatePolicy>::pInstance_=0;
template <typename T,
template <typename U> class CreatePolicy> bool singleton<T,CreatePolicy>::destroyed_=false;
}}
#endif //

View File

@@ -12,10 +12,15 @@
'../platform/default/settings_json.cpp',
'../platform/default/glfw_view.cpp',
'../platform/default/log_stderr.cpp',
'../platform/default/image_reader.cpp',
'../platform/default/png_reader.cpp',
'../platform/default/jpeg_reader.cpp',
],
'xcode_settings': {
'OTHER_CPLUSPLUSFLAGS':[
'<@(glfw3_cflags)',
'<@(png_cflags)',
'<@(jpeg_cflags)',
],
},
'cflags_cc': [

View File

@@ -8,7 +8,7 @@
#include <cstdlib>
#include <stdexcept>
#include <cstring>
#include <mbgl/util/image_reader.hpp>
// Check png library version.
const static bool png_version_check = []() {
@@ -73,121 +73,23 @@ std::string compress_png(int width, int height, void *rgba) {
return result;
}
struct Buffer {
Buffer(const std::string& data_)
: data(data_.data()), length(data_.size()) {}
const char *const data = 0;
const size_t length = 0;
size_t pos = 0;
};
void readCallback(png_structp png, png_bytep data, png_size_t length) {
Buffer *reader = static_cast<Buffer *>(png_get_io_ptr(png));
// Read `length` bytes into `data`.
if (reader->pos + length > reader->length) {
png_error(png, "Read Error");
} else {
std::memcpy(data, reader->data + reader->pos, length);
reader->pos += length;
}
}
void errorHandler(png_structp, png_const_charp error_msg) {
Log::Error(Event::Image, "PNG: %s", error_msg);
throw std::runtime_error(error_msg);
}
void warningHandler(png_structp, png_const_charp error_msg) {
Log::Warning(Event::General, "PNG: %s", error_msg);
}
Image::Image(const std::string &data) {
Buffer buffer(data);
if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
Log::Error(Event::Image, "image is not a valid PNG image");
return;
}
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, errorHandler, warningHandler);
assert(png);
png_infop info = png_create_info_struct(png);
assert(info);
int depth, color, interlace;
try {
png_set_read_fn(png, (png_voidp)&buffer, readCallback);
png_read_info(png, info);
png_get_IHDR(png, info, (png_uint_32*)&width, (png_uint_32*)&height, &depth, &color, &interlace, nullptr, nullptr);
bool alpha = (color & PNG_COLOR_MASK_ALPHA) || png_get_valid(png, info, PNG_INFO_tRNS);
// From http://trac.mapnik.org/browser/trunk/src/png_reader.cpp
if (color == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png);
if (color == PNG_COLOR_TYPE_GRAY)
png_set_expand(png);
if (png_get_valid(png, info, PNG_INFO_tRNS))
png_set_expand(png);
if (depth == 16)
png_set_strip_16(png);
if (depth < 8)
png_set_packing(png);
if (color == PNG_COLOR_TYPE_GRAY ||
color == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
if (interlace == PNG_INTERLACE_ADAM7)
png_set_interlace_handling(png);
// Always add an alpha channel.
if (!alpha) {
png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER);
}
// FIXME work around gamma crash
double gamma;
if (png_get_gAMA(png, info, &gamma))
png_set_gamma(png, 2.2, gamma);
png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2);
png_read_update_info(png, info);
png_size_t rowbytes = png_get_rowbytes(png, info);
assert(width * 4 == rowbytes);
Image::Image(std::string const& data)
{
try
{
std::unique_ptr<image_reader> reader(get_image_reader(data.c_str(), data.size()));
width = reader->width();
height = reader->height();
img = ::std::unique_ptr<char[]>(new char[width * height * 4]());
char *surface = img.get();
assert(surface);
struct ptrs {
ptrs(size_t count) : rows(new png_bytep[count]) {}
~ptrs() { delete[] rows; }
png_bytep *rows = nullptr;
} pointers(height);
for (unsigned i = 0; i < height; ++i) {
pointers.rows[i] = (png_bytep)(surface + (i * rowbytes));
}
// Read image data
png_read_image(png, pointers.rows);
png_read_end(png, nullptr);
png_destroy_read_struct(&png, &info, nullptr);
} catch (std::exception& e) {
Log::Error(Event::Image, "loading PNG failed: %s", e.what());
png_destroy_read_struct(&png, &info, nullptr);
if (img) {
img.reset();
}
reader->read(0, 0, width, height, img.get());
}
catch (image_reader_exception const& ex)
{
fprintf(stderr, "ImageReader: %s\n", ex.what());
img.reset();
width = 0;
height = 0;
}
}

View File

@@ -0,0 +1,67 @@
#include "mbgl/util/image_reader.hpp"
#include <boost/optional.hpp>
namespace mbgl { namespace util {
inline boost::optional<std::string> type_from_bytes(char const* data, size_t size)
{
using result_type = boost::optional<std::string>;
if (size >= 4)
{
unsigned int magic = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
if (magic == 0x89504E47U)
{
return result_type("png");
}
else if (magic == 0x49492A00 || magic == 0x4D4D002A)
{
return result_type("tiff");
}
}
if (size>=2)
{
unsigned int magic = ((data[0] << 8) | data[1]) & 0xffff;
if (magic == 0xffd8)
{
return result_type("jpeg");
}
}
if (size>=12)
{
if (data[0] == 'R' && data[1] == 'I' && data[2] == 'F' && data[3] == 'F' &&
data[8] == 'W' && data[9] == 'E' && data[10] == 'B' && data[11] == 'P')
{
return result_type("webp");
}
}
return result_type();
}
image_reader* get_image_reader(char const* data, size_t size)
{
boost::optional<std::string> type = type_from_bytes(data,size);
if (type)
{
return factory<image_reader,std::string,char const*,size_t>::instance().create_object(*type, data,size);
}
else
throw image_reader_exception("image_reader: can't determine type from input data");
}
//image_reader* get_image_reader(std::string const& filename,std::string const& type)
//{
// return factory<image_reader,std::string,std::string const&>::instance().create_object(type,filename);
//}
//image_reader* get_image_reader(std::string const& filename)
//{
//boost::optional<std::string> type = type_from_filename(filename);
//if (type)
//{
// return factory<image_reader,std::string,std::string const&>::instance().create_object(*type,filename);
//}
// return 0;
//}
}}

View File

@@ -0,0 +1,291 @@
#include "mbgl/util/image_reader.hpp"
//#include <mapnik/color.hpp>
// jpeg
extern "C"
{
#include <jpeglib.h>
}
// boost
#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored "-Wunused-local-typedef"
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#pragma GCC diagnostic pop
// std
#include <cstdio>
#include <memory>
namespace mbgl { namespace util {
template <typename T>
class jpeg_reader : public image_reader
{
public:
using source_type = T;
using input_stream = boost::iostreams::stream<source_type>;
const static unsigned BUF_SIZE = 4096;
private:
struct jpeg_stream_wrapper
{
jpeg_source_mgr manager;
input_stream * stream;
JOCTET buffer[BUF_SIZE];
};
struct jpeg_info_guard
{
jpeg_info_guard(jpeg_decompress_struct * cinfo)
: i_(cinfo) {}
~jpeg_info_guard()
{
jpeg_destroy_decompress(i_);
}
jpeg_decompress_struct * i_;
};
private:
source_type source_;
input_stream stream_;
unsigned width_;
unsigned height_;
public:
explicit jpeg_reader(std::string const& file_name);
explicit jpeg_reader(char const* data, size_t size);
~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, unsigned width, unsigned height, char *image);
private:
void init();
static void on_error(j_common_ptr cinfo);
static void on_error_message(j_common_ptr cinfo);
static void init_source(j_decompress_ptr cinfo);
static boolean fill_input_buffer(j_decompress_ptr cinfo);
static void skip(j_decompress_ptr cinfo, long count);
static void term(j_decompress_ptr cinfo);
static void attach_stream(j_decompress_ptr cinfo, input_stream* in);
};
namespace
{
image_reader* create_jpeg_reader(std::string const& file)
{
return new jpeg_reader<boost::iostreams::file_source>(file);
}
image_reader* create_jpeg_reader2(char const* data, size_t size)
{
return new jpeg_reader<boost::iostreams::array_source>(data, size);
}
const bool registered = register_image_reader("jpeg",create_jpeg_reader);
const bool registered2 = register_image_reader("jpeg",create_jpeg_reader2);
}
// ctors
template <typename T>
jpeg_reader<T>::jpeg_reader(std::string const& file_name)
: source_(file_name,std::ios_base::in | std::ios_base::binary),
stream_(source_),
width_(0),
height_(0)
{
if (!stream_) throw image_reader_exception("cannot open image file "+ file_name);
init();
}
template <typename T>
jpeg_reader<T>::jpeg_reader(char const* data, size_t size)
: source_(data, size),
stream_(source_),
width_(0),
height_(0)
{
if (!stream_) throw image_reader_exception("cannot open image stream");
init();
}
// dtor
template <typename T>
jpeg_reader<T>::~jpeg_reader() {}
// jpeg stream wrapper
template <typename T>
void jpeg_reader<T>::init_source (j_decompress_ptr cinfo)
{
jpeg_stream_wrapper* wrap = reinterpret_cast<jpeg_stream_wrapper*>(cinfo->src);
wrap->stream->seekg(0,std::ios_base::beg);
}
template <typename T>
boolean jpeg_reader<T>::fill_input_buffer (j_decompress_ptr cinfo)
{
jpeg_stream_wrapper* wrap = reinterpret_cast<jpeg_stream_wrapper*>(cinfo->src);
wrap->stream->read(reinterpret_cast<char*>(&wrap->buffer[0]),BUF_SIZE);
std::streamsize size = wrap->stream->gcount();
wrap->manager.next_input_byte = wrap->buffer;
wrap->manager.bytes_in_buffer = BUF_SIZE;
return (size > 0) ? TRUE : FALSE;
}
template <typename T>
void jpeg_reader<T>::skip(j_decompress_ptr cinfo, long count)
{
if (count <= 0) return; //A zero or negative skip count should be treated as a no-op.
jpeg_stream_wrapper* wrap = reinterpret_cast<jpeg_stream_wrapper*>(cinfo->src);
if (wrap->manager.bytes_in_buffer > 0 && count < static_cast<long>(wrap->manager.bytes_in_buffer))
{
wrap->manager.bytes_in_buffer -= count;
wrap->manager.next_input_byte = &wrap->buffer[BUF_SIZE - wrap->manager.bytes_in_buffer];
}
else
{
wrap->stream->seekg(count - wrap->manager.bytes_in_buffer, std::ios_base::cur);
// trigger buffer fill
wrap->manager.next_input_byte = 0;
wrap->manager.bytes_in_buffer = 0; //bytes_in_buffer may be zero on return.
}
}
template <typename T>
void jpeg_reader<T>::term (j_decompress_ptr /*cinfo*/)
{
// no-op
}
template <typename T>
void jpeg_reader<T>::attach_stream (j_decompress_ptr cinfo, input_stream* in)
{
if (cinfo->src == 0)
{
cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(jpeg_stream_wrapper));
}
jpeg_reader::jpeg_stream_wrapper * src = reinterpret_cast<jpeg_reader::jpeg_stream_wrapper*> (cinfo->src);
src->manager.init_source = init_source;
src->manager.fill_input_buffer = fill_input_buffer;
src->manager.skip_input_data = skip;
src->manager.resync_to_restart = jpeg_resync_to_restart;
src->manager.term_source = term;
src->manager.bytes_in_buffer = 0;
src->manager.next_input_byte = 0;
src->stream = in;
}
template <typename T>
void jpeg_reader<T>::on_error(j_common_ptr /*cinfo*/)
{
}
template <typename T>
void jpeg_reader<T>::on_error_message(j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, buffer);
throw image_reader_exception(std::string("JPEG Reader: libjpeg could not read image: ") + buffer);
}
template <typename T>
void jpeg_reader<T>::init()
{
jpeg_decompress_struct cinfo;
jpeg_info_guard iguard(&cinfo);
jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = on_error;
jerr.output_message = on_error_message;
jpeg_create_decompress(&cinfo);
attach_stream(&cinfo, &stream_);
int ret = jpeg_read_header(&cinfo, TRUE);
if (ret != JPEG_HEADER_OK)
throw image_reader_exception("JPEG Reader: failed to read header");
jpeg_start_decompress(&cinfo);
width_ = cinfo.output_width;
height_ = cinfo.output_height;
if (cinfo.out_color_space == JCS_UNKNOWN)
{
throw image_reader_exception("JPEG Reader: failed to read unknown color space");
}
if (cinfo.output_width == 0 || cinfo.output_height == 0)
{
throw image_reader_exception("JPEG Reader: failed to read image size of");
}
}
template <typename T>
unsigned jpeg_reader<T>::width() const
{
return width_;
}
template <typename T>
unsigned jpeg_reader<T>::height() const
{
return height_;
}
template <typename T>
void jpeg_reader<T>::read(unsigned x0, unsigned y0, unsigned width, unsigned height, char* image)
{
stream_.clear();
stream_.seekg(0, std::ios_base::beg);
jpeg_decompress_struct cinfo;
jpeg_info_guard iguard(&cinfo);
jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = on_error;
jerr.output_message = on_error_message;
jpeg_create_decompress(&cinfo);
attach_stream(&cinfo, &stream_);
int ret = jpeg_read_header(&cinfo, TRUE);
if (ret != JPEG_HEADER_OK) throw image_reader_exception("JPEG Reader read(): failed to read header");
jpeg_start_decompress(&cinfo);
JSAMPARRAY buffer;
int row_stride;
unsigned char r,g,b;
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
unsigned w = std::min(width,width_ - x0);
unsigned h = std::min(height,height_ - y0);
const std::unique_ptr<unsigned int[]> out_row(new unsigned int[w]);
unsigned row = 0;
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
if (row >= y0 && row < y0 + h)
{
for (unsigned int x = 0; x < w; ++x)
{
unsigned col = x + x0;
r = buffer[0][cinfo.output_components * col];
if (cinfo.output_components > 2)
{
g = buffer[0][cinfo.output_components * col + 1];
b = buffer[0][cinfo.output_components * col + 2];
} else {
g = r;
b = r;
}
out_row[x] = (0xff << 24) | (b << 16) | (g << 8) | r;//color(r, g, b, a).rgba();
}
//image.setRow(row - y0, out_row.get(), w);
std::copy((char*)out_row.get(), (char*)out_row.get() + w*4, image + (row - y0)*width_*4);
}
++row;
}
jpeg_finish_decompress(&cinfo);
}
}}

View File

@@ -0,0 +1,279 @@
#include "mbgl/util/image_reader.hpp"
#include <iostream>
extern "C"
{
#include <png.h>
}
// boost
#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored "-Wunused-local-typedef"
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#pragma GCC diagnostic pop
// stl
#include <cstring>
#include <memory>
namespace mbgl { namespace util {
template <typename T>
class png_reader : public image_reader
{
using source_type = T;
using input_stream = boost::iostreams::stream<source_type>;
struct png_struct_guard
{
png_struct_guard(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
: p_(png_ptr_ptr),
i_(info_ptr_ptr) {}
~png_struct_guard()
{
png_destroy_read_struct(p_,i_,0);
}
png_structpp p_;
png_infopp i_;
};
private:
source_type source_;
input_stream stream_;
unsigned width_;
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, unsigned width, unsigned height, char * image);
private:
void init();
static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length);
};
namespace
{
image_reader* create_png_reader(std::string const& file)
{
return new png_reader<boost::iostreams::file_source>(file);
}
image_reader* create_png_reader2(char const * data, std::size_t size)
{
return new png_reader<boost::iostreams::array_source>(data, size);
}
const bool registered = register_image_reader("png",create_png_reader);
const bool registered2 = register_image_reader("png", create_png_reader2);
}
void user_error_fn(png_structp /*png_ptr*/, png_const_charp error_msg)
{
throw image_reader_exception(std::string("failed to read invalid png: '") + error_msg + "'");
}
void user_warning_fn(png_structp /*png_ptr*/, png_const_charp warning_msg)
{
fprintf(stderr, "ImageReader (PNG): %s\n", warning_msg);
}
template <typename T>
void png_reader<T>::png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
input_stream * fin = reinterpret_cast<input_stream*>(png_get_io_ptr(png_ptr));
fin->read(reinterpret_cast<char*>(data), length);
std::streamsize read_count = fin->gcount();
if (read_count < 0 || static_cast<png_size_t>(read_count) != length)
{
png_error(png_ptr, "Read Error");
}
}
template <typename T>
png_reader<T>::png_reader(std::string const& file_name)
: source_(file_name,std::ios_base::in | std::ios_base::binary),
stream_(source_),
width_(0),
height_(0),
bit_depth_(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 + "'");
init();
}
template <typename T>
png_reader<T>::png_reader(char const* data, std::size_t size)
: source_(data,size),
stream_(source_),
width_(0),
height_(0),
bit_depth_(0),
color_type_(0),
has_alpha_(false)
{
if (!stream_) throw image_reader_exception("PNG reader: cannot open image stream");
init();
}
template <typename T>
png_reader<T>::~png_reader() {}
template <typename T>
void png_reader<T>::init()
{
png_byte header[8];
std::memset(header,0,8);
stream_.read(reinterpret_cast<char*>(header),8);
if ( stream_.gcount() != 8)
{
throw image_reader_exception("PNG reader: Could not read image");
}
int is_png=!png_sig_cmp(header,0,8);
if (!is_png)
{
throw image_reader_exception("File or stream is not a png");
}
png_structp png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING,0,0,0);
if (!png_ptr)
{
throw image_reader_exception("failed to allocate png_ptr");
}
// catch errors in a custom way to avoid the need for setjmp
png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn);
png_infop info_ptr;
png_struct_guard sguard(&png_ptr,&info_ptr);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) throw image_reader_exception("failed to create info_ptr");
png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr, info_ptr);
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;
//MAPNIK_LOG_DEBUG(png_reader) << "png_reader: bit_depth=" << bit_depth_ << ",color_type=" << color_type_;
}
template <typename T>
unsigned png_reader<T>::width() const
{
return width_;
}
template <typename T>
unsigned png_reader<T>::height() const
{
return height_;
}
template <typename T>
void png_reader<T>::read(unsigned x0, unsigned y0, unsigned width, unsigned height, char * image)
{
stream_.clear();
stream_.seekg(0, std::ios_base::beg);
png_structp png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING,0,0,0);
if (!png_ptr)
{
throw image_reader_exception("failed to allocate png_ptr");
}
// catch errors in a custom way to avoid the need for setjmp
png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn);
png_infop info_ptr;
png_struct_guard sguard(&png_ptr,&info_ptr);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) throw image_reader_exception("failed to create info_ptr");
png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data);
png_read_info(png_ptr, info_ptr);
if (color_type_ == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
if (bit_depth_ == 16)
png_set_strip_16(png_ptr);
if (color_type_ == PNG_COLOR_TYPE_GRAY ||
color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
// quick hack -- only work in >=libpng 1.2.7
png_set_add_alpha(png_ptr,0xff,PNG_FILLER_AFTER); //rgba
double gamma;
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, 2.2, gamma);
if (x0 == 0 && y0 == 0 && width >= width_ && height >= height_)
{
if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7)
{
png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
// according to docs png_read_image
// "..automatically handles interlacing,
// so you don't need to call png_set_interlace_handling()"
}
png_read_update_info(png_ptr, info_ptr);
// we can read whole image at once
// alloc row pointers
const std::unique_ptr<png_bytep[]> rows(new png_bytep[height_]);
for (unsigned row = 0; row < height_; ++row)
rows[row] = (png_bytep)image + row * width_ ;
png_read_image(png_ptr, rows.get());
}
else
{
png_read_update_info(png_ptr, info_ptr);
//unsigned w=std::min(width, width_ - x0);
unsigned h=std::min(height, height_ - y0);
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
const std::unique_ptr<png_byte[]> row(new png_byte[rowbytes]);
//START read image rows
for (unsigned i = 0; i < height_; ++i)
{
png_read_row(png_ptr,row.get(),0);
if (i >= y0 && i < (y0 + h))
{
////image.setRow(i-y0,reinterpret_cast<unsigned*>(&row[x0 * 4]),w);
//std::copy(image, buf + size, pData_ + i * width);
}
}
//END
}
png_read_end(png_ptr,0);
}
}}