diff options
author | artemp <artem@mapnik.org> | 2014-11-27 11:43:13 +0100 |
---|---|---|
committer | artemp <artem@mapnik.org> | 2014-11-27 11:43:13 +0100 |
commit | 8f3d2d4a45d5790072a3c220451a5c630760ae3e (patch) | |
tree | 82858ff98ed1de690c13fefc829be6ea4d00ff68 /platform | |
parent | 9092cb42093aaff9031267a00f6ce36ba29f69d1 (diff) | |
parent | 79017060947c1a11f4124ab837bd171b748806de (diff) | |
download | qtlocation-mapboxgl-8f3d2d4a45d5790072a3c220451a5c630760ae3e.tar.gz |
Merge branch 'master' into nunicode
Diffstat (limited to 'platform')
-rw-r--r-- | platform/default/image.cpp | 16 | ||||
-rw-r--r-- | platform/default/image_reader.cpp | 20 | ||||
-rw-r--r-- | platform/default/jpeg_reader.cpp | 133 | ||||
-rw-r--r-- | platform/default/png_reader.cpp | 116 |
4 files changed, 74 insertions, 211 deletions
diff --git a/platform/default/image.cpp b/platform/default/image.cpp index 96d01ab718..242367e889 100644 --- a/platform/default/image.cpp +++ b/platform/default/image.cpp @@ -7,7 +7,8 @@ #include <cstdlib> #include <stdexcept> #include <cstring> -#include <mbgl/util/image_reader.hpp> + +#include <mbgl/platform/default/image_reader.hpp> // Check png library version. const static bool png_version_check = []() { @@ -76,20 +77,27 @@ Image::Image(std::string const& data) { try { - std::unique_ptr<image_reader> reader(get_image_reader(data.c_str(), data.size())); + auto reader = getImageReader(data.c_str(), data.size()); width = reader->width(); height = reader->height(); img = ::std::unique_ptr<char[]>(new char[width * height * 4]()); reader->read(0, 0, width, height, img.get()); } - catch (image_reader_exception const& ex) + catch (ImageReaderException const& ex) { - fprintf(stderr, "ImageReader: %s\n", ex.what()); + fprintf(stderr, "Image: %s\n", ex.what()); img.reset(); width = 0; height = 0; } + catch (...) // catch the rest + { + fprintf(stderr, "Image: exception in constructor"); + img.reset(); + width = 0; + height = 0; + } } } diff --git a/platform/default/image_reader.cpp b/platform/default/image_reader.cpp index 53ab5758aa..e80ccb6819 100644 --- a/platform/default/image_reader.cpp +++ b/platform/default/image_reader.cpp @@ -1,5 +1,9 @@ -#include "mbgl/util/image_reader.hpp" +#include <mbgl/platform/default/image_reader.hpp> +#include <mbgl/platform/default/png_reader.hpp> +#include <mbgl/platform/default/jpeg_reader.hpp> + #include <boost/optional.hpp> +#include <boost/iostreams/device/array.hpp> namespace mbgl { namespace util { @@ -38,15 +42,21 @@ inline boost::optional<std::string> type_from_bytes(char const* data, size_t siz return result_type(); } -image_reader* get_image_reader(char const* data, size_t size) +std::unique_ptr<ImageReader> getImageReader(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>::create_object(*type, data,size); + if (*type == "png") + { + return std::make_unique<PngReader<boost::iostreams::array_source>>(data, size); + } + else if (*type == "jpeg") + { + return std::make_unique<JpegReader<boost::iostreams::array_source>>(data, size); + } } - else - throw image_reader_exception("image_reader: can't determine type from input data"); + throw ImageReaderException("ImageReader: can't determine type from input data"); } }} diff --git a/platform/default/jpeg_reader.cpp b/platform/default/jpeg_reader.cpp index d56c10af2a..f89e766874 100644 --- a/platform/default/jpeg_reader.cpp +++ b/platform/default/jpeg_reader.cpp @@ -1,15 +1,9 @@ -#include "mbgl/util/image_reader.hpp" - -// jpeg -extern "C" -{ -#include <jpeglib.h> -} +#include <mbgl/platform/default/jpeg_reader.hpp> // boost #include <boost/iostreams/device/file.hpp> #include <boost/iostreams/device/array.hpp> -#include <boost/iostreams/stream.hpp> + // std #include <cstdio> @@ -17,111 +11,32 @@ extern "C" namespace mbgl { namespace util { +// ctor 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 w, unsigned h, 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 static bool registered = register_image_reader("jpeg",create_jpeg_reader); -const static 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) +JpegReader<T>::JpegReader(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"); + if (!stream_) throw ImageReaderException("cannot open image stream"); init(); } // dtor template <typename T> -jpeg_reader<T>::~jpeg_reader() {} +JpegReader<T>::~JpegReader() {} // jpeg stream wrapper template <typename T> -void jpeg_reader<T>::init_source (j_decompress_ptr cinfo) +void JpegReader<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) +boolean JpegReader<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); @@ -132,7 +47,7 @@ boolean jpeg_reader<T>::fill_input_buffer (j_decompress_ptr cinfo) } template <typename T> -void jpeg_reader<T>::skip(j_decompress_ptr cinfo, long count) +void JpegReader<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); @@ -152,20 +67,20 @@ void jpeg_reader<T>::skip(j_decompress_ptr cinfo, long count) } template <typename T> -void jpeg_reader<T>::term (j_decompress_ptr /*cinfo*/) +void JpegReader<T>::term (j_decompress_ptr /*cinfo*/) { // no-op } template <typename T> -void jpeg_reader<T>::attach_stream (j_decompress_ptr cinfo, input_stream* in) +void JpegReader<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); + JpegReader::jpeg_stream_wrapper * src = reinterpret_cast<JpegReader::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; @@ -177,20 +92,20 @@ void jpeg_reader<T>::attach_stream (j_decompress_ptr cinfo, input_stream* in) } template <typename T> -void jpeg_reader<T>::on_error(j_common_ptr /*cinfo*/) +void JpegReader<T>::on_error(j_common_ptr /*cinfo*/) { } template <typename T> -void jpeg_reader<T>::on_error_message(j_common_ptr cinfo) +void JpegReader<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); + throw ImageReaderException(std::string("JPEG Reader: libjpeg could not read image: ") + buffer); } template <typename T> -void jpeg_reader<T>::init() +void JpegReader<T>::init() { jpeg_decompress_struct cinfo; jpeg_info_guard iguard(&cinfo); @@ -202,35 +117,35 @@ void jpeg_reader<T>::init() 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"); + throw ImageReaderException("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"); + throw ImageReaderException("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"); + throw ImageReaderException("JPEG Reader: failed to read image size of"); } } template <typename T> -unsigned jpeg_reader<T>::width() const +unsigned JpegReader<T>::width() const { return width_; } template <typename T> -unsigned jpeg_reader<T>::height() const +unsigned JpegReader<T>::height() const { return height_; } template <typename T> -void jpeg_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char* image) +void JpegReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char* image) { stream_.clear(); stream_.seekg(0, std::ios_base::beg); @@ -244,7 +159,7 @@ void jpeg_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char 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"); + if (ret != JPEG_HEADER_OK) throw ImageReaderException("JPEG Reader read(): failed to read header"); jpeg_start_decompress(&cinfo); JSAMPARRAY buffer; int row_stride; @@ -283,4 +198,6 @@ void jpeg_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char jpeg_finish_decompress(&cinfo); } +template class JpegReader<boost::iostreams::array_source>; + }} diff --git a/platform/default/png_reader.cpp b/platform/default/png_reader.cpp index 33d17a1b82..cf96ca2363 100644 --- a/platform/default/png_reader.cpp +++ b/platform/default/png_reader.cpp @@ -1,4 +1,4 @@ -#include "mbgl/util/image_reader.hpp" +#include <mbgl/platform/default/png_reader.hpp> #include <iostream> extern "C" { @@ -16,70 +16,9 @@ extern "C" 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 static bool registered = register_image_reader("png",create_png_reader); -const static 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 + "'"); + throw ImageReaderException(std::string("failed to read invalid png: '") + error_msg + "'"); } void user_warning_fn(png_structp /*png_ptr*/, png_const_charp warning_msg) @@ -89,7 +28,7 @@ void user_warning_fn(png_structp /*png_ptr*/, png_const_charp warning_msg) } template <typename T> -void png_reader<T>::png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) +void PngReader<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); @@ -101,22 +40,7 @@ void png_reader<T>::png_read_data(png_structp png_ptr, png_bytep data, png_size_ } 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) +PngReader<T>::PngReader(char const* data, std::size_t size) : source_(data,size), stream_(source_), width_(0), @@ -126,36 +50,36 @@ png_reader<T>::png_reader(char const* data, std::size_t size) has_alpha_(false) { - if (!stream_) throw image_reader_exception("PNG reader: cannot open image stream"); + if (!stream_) throw ImageReaderException("PNG reader: cannot open image stream"); init(); } template <typename T> -png_reader<T>::~png_reader() {} +PngReader<T>::~PngReader() {} template <typename T> -void png_reader<T>::init() +void PngReader<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"); + throw ImageReaderException("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"); + throw ImageReaderException("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"); + throw ImageReaderException("failed to allocate png_ptr"); } // catch errors in a custom way to avoid the need for setjmp @@ -164,7 +88,7 @@ void png_reader<T>::init() 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"); + if (!info_ptr) throw ImageReaderException("failed to create info_ptr"); png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data); @@ -179,19 +103,19 @@ void png_reader<T>::init() } template <typename T> -unsigned png_reader<T>::width() const +unsigned PngReader<T>::width() const { return width_; } template <typename T> -unsigned png_reader<T>::height() const +unsigned PngReader<T>::height() const { return height_; } template <typename T> -void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char * image) +void PngReader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char * image) { stream_.clear(); stream_.seekg(0, std::ios_base::beg); @@ -201,7 +125,7 @@ void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char if (!png_ptr) { - throw image_reader_exception("failed to allocate png_ptr"); + throw ImageReaderException("failed to allocate png_ptr"); } // catch errors in a custom way to avoid the need for setjmp @@ -210,7 +134,7 @@ void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char 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"); + if (!info_ptr) throw ImageReaderException("failed to create info_ptr"); png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data); png_read_info(png_ptr, info_ptr); @@ -233,6 +157,7 @@ void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char double gamma; if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, 2.2, gamma); + png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, 2.2); if (x0 == 0 && y0 == 0 && w >= width_ && h >= height_) { @@ -248,7 +173,7 @@ void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char // 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_ ; + rows[row] = (png_bytep)image + row * width_ * 4 ; png_read_image(png_ptr, rows.get()); } else @@ -263,10 +188,13 @@ void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char png_read_row(png_ptr,row.get(),0); if (i >= y0 && i < (y0 + h)) { - std::copy(&row[x0 * 4], &row[x0 * 4] + w, image + i * width_* 4); + std::copy(&row[x0 * 4], &row[x0 * 4] + w * 4, image + i * width_* 4); } } } png_read_end(png_ptr,0); } + +template class PngReader<boost::iostreams::array_source>; + }} |