simplify image_readers creation implementation

* remove static callbacks registration
* link png_reader/jpeg_reader into mbgl-linux lib
This commit is contained in:
artemp
2014-11-19 13:06:47 +01:00
parent 08357cfcf8
commit 759a5e65cf
11 changed files with 156 additions and 347 deletions

View File

@@ -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>;
}}