Merge branch 'master' of github.com:mapbox/mapbox-gl-native

This commit is contained in:
Mike Morris
2014-11-26 10:00:54 -08:00
8 changed files with 67 additions and 61 deletions

View File

@@ -9,15 +9,15 @@
namespace mbgl { namespace util {
class image_reader_exception : public std::exception
class ImageReaderException : public std::exception
{
private:
std::string message_;
public:
image_reader_exception(std::string const& message)
ImageReaderException(std::string const& message)
: message_(message) {}
~image_reader_exception() throw() {}
~ImageReaderException() throw() {}
virtual const char* what() const throw()
{
@@ -25,17 +25,17 @@ public:
}
};
struct image_reader : private noncopyable
struct ImageReader : 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 bool hasAlpha() const=0;
virtual bool premultipliedAlpha() const=0;
virtual void read(unsigned x,unsigned y, unsigned width, unsigned height, char* image)=0;
virtual ~image_reader() {}
virtual ~ImageReader() {}
};
std::unique_ptr<image_reader> get_image_reader(char const* data, size_t size);
std::unique_ptr<ImageReader> getImageReader(char const* data, size_t size);
}}

View File

@@ -14,7 +14,7 @@ extern "C"
namespace mbgl { namespace util {
template <typename T>
class jpeg_reader : public image_reader
class JpegReader : public ImageReader
{
public:
using source_type = T;
@@ -46,12 +46,12 @@ private:
unsigned width_;
unsigned height_;
public:
jpeg_reader(char const* data, size_t size);
~jpeg_reader();
JpegReader(char const* data, size_t size);
~JpegReader();
unsigned width() const;
unsigned height() const;
inline bool has_alpha() const { return false; }
inline bool premultiplied_alpha() const { return true; }
inline bool hasAlpha() const { return false; }
inline bool premultipliedAlpha() const { return true; }
void read(unsigned x,unsigned y, unsigned w, unsigned h, char *image);
private:
void init();

View File

@@ -16,7 +16,7 @@ extern "C"
namespace mbgl { namespace util {
template <typename T>
class png_reader : public image_reader
class PngReader : public ImageReader
{
using source_type = T;
using input_stream = boost::iostreams::stream<source_type>;
@@ -44,12 +44,12 @@ private:
int color_type_;
bool has_alpha_;
public:
png_reader(char const* data, std::size_t size);
~png_reader();
PngReader(char const* data, std::size_t size);
~PngReader();
unsigned width() const;
unsigned height() const;
inline bool has_alpha() const { return has_alpha_; }
bool premultiplied_alpha() const { return true; } // png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2)
inline bool hasAlpha() const { return has_alpha_; }
bool premultipliedAlpha() const { return true; } // png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2)
void read(unsigned x,unsigned y, unsigned width, unsigned height, char * image);
private:
void init();

View File

@@ -77,13 +77,13 @@ Image::Image(std::string const& data)
{
try
{
auto 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, "Image: %s\n", ex.what());
img.reset();

View File

@@ -42,21 +42,21 @@ inline boost::optional<std::string> type_from_bytes(char const* data, size_t siz
return result_type();
}
std::unique_ptr<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)
{
if (*type == "png")
{
return std::make_unique<png_reader<boost::iostreams::array_source>>(data, size);
return std::make_unique<PngReader<boost::iostreams::array_source>>(data, size);
}
else if (*type == "jpeg")
{
return std::make_unique<jpeg_reader<boost::iostreams::array_source>>(data, size);
return std::make_unique<JpegReader<boost::iostreams::array_source>>(data, size);
}
}
throw image_reader_exception("image_reader: can't determine type from input data");
throw ImageReaderException("ImageReader: can't determine type from input data");
}
}}

View File

@@ -13,30 +13,30 @@ namespace mbgl { namespace util {
// ctor
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);
@@ -47,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);
@@ -67,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;
@@ -92,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);
@@ -117,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);
@@ -159,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;
@@ -198,6 +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>;
template class JpegReader<boost::iostreams::array_source>;
}}

View File

@@ -18,7 +18,7 @@ namespace mbgl { namespace util {
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)
@@ -28,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);
@@ -40,7 +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(char const* data, std::size_t size)
PngReader<T>::PngReader(char const* data, std::size_t size)
: source_(data,size),
stream_(source_),
width_(0),
@@ -50,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
@@ -88,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);
@@ -103,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);
@@ -125,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
@@ -134,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);
@@ -195,6 +195,6 @@ 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>;
template class PngReader<boost::iostreams::array_source>;
}}

View File

@@ -37,6 +37,12 @@ void TileData::request(FileSource& fileSource) {
if (token == "z") return std::to_string(id.z);
if (token == "x") return std::to_string(id.x);
if (token == "y") return std::to_string(id.y);
if (token == "prefix") {
std::string prefix { 2 };
prefix[0] = "0123456789abcdef"[id.x % 16];
prefix[1] = "0123456789abcdef"[id.y % 16];
return prefix;
}
if (token == "ratio") return (map.getState().getPixelRatio() > 1.0 ? "@2x" : "");
return "";
});