diff options
author | artemp <artem@mapnik.org> | 2014-11-19 13:06:47 +0100 |
---|---|---|
committer | artemp <artem@mapnik.org> | 2014-11-19 13:06:47 +0100 |
commit | 759a5e65cf853d679650339e26fef25141ab1901 (patch) | |
tree | 56c18daefe394fa83e23d44143677460dc715fb3 /platform | |
parent | 08357cfcf86ad0b6c8e8563767110ed50cbb2f56 (diff) | |
download | qtlocation-mapboxgl-759a5e65cf853d679650339e26fef25141ab1901.tar.gz |
simplify image_readers creation implementation
* remove static callbacks registration
* link png_reader/jpeg_reader into mbgl-linux lib
Diffstat (limited to 'platform')
-rw-r--r-- | platform/default/image_reader.cpp | 16 | ||||
-rw-r--r-- | platform/default/jpeg_reader.cpp | 87 | ||||
-rw-r--r-- | platform/default/png_reader.cpp | 75 |
3 files changed, 22 insertions, 156 deletions
diff --git a/platform/default/image_reader.cpp b/platform/default/image_reader.cpp index 53ab5758aa..599ccb13a4 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/util/png_reader.hpp" +#include "mbgl/util/jpeg_reader.hpp" + #include <boost/optional.hpp> +#include <boost/iostreams/device/array.hpp> namespace mbgl { namespace util { @@ -43,10 +47,16 @@ 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>::create_object(*type, data,size); + if (*type == "png") + { + return new png_reader<boost::iostreams::array_source>(data, size); + } + else if (*type == "jpeg") + { + return new jpeg_reader<boost::iostreams::array_source>(data, size); + } } - else - throw image_reader_exception("image_reader: can't determine type from input data"); + throw image_reader_exception("image_reader: can't determine type from input data"); } }} diff --git a/platform/default/jpeg_reader.cpp b/platform/default/jpeg_reader.cpp index f1e9d0fdfe..b722c08fa1 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/util/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,80 +11,7 @@ extern "C" 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 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(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); -} - -// 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(); -} - +// ctor template <typename T> jpeg_reader<T>::jpeg_reader(char const* data, size_t size) : source_(data, size), @@ -277,4 +198,6 @@ void jpeg_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char jpeg_finish_decompress(&cinfo); } +template class jpeg_reader<boost::iostreams::array_source>; + }} diff --git a/platform/default/png_reader.cpp b/platform/default/png_reader.cpp index 838344286e..4d0abce04c 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/util/png_reader.hpp" #include <iostream> extern "C" { @@ -16,61 +16,6 @@ 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(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); - -} - - 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 + "'"); @@ -95,21 +40,6 @@ 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) : source_(data,size), stream_(source_), @@ -263,4 +193,7 @@ void png_reader<T>::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char } png_read_end(png_ptr,0); } + +template class png_reader<boost::iostreams::array_source>; + }} |