diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-24 10:07:18 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-25 15:57:36 -0800 |
commit | 0c1e378bc9555f6cf826bb38b1a36fa742f8ce9b (patch) | |
tree | 9aec8e4f475ff715645072503b3e0ec78f0573af /include/mbgl | |
parent | 2de0a351a0635192bd05116cebdf0103c2638d05 (diff) | |
download | qtlocation-mapboxgl-0c1e378bc9555f6cf826bb38b1a36fa742f8ce9b.tar.gz |
[core] Rewrite image handling
* Consolidate Image and StillImage
* Typecheck unassociated vs premultiplied images
* Rewrite default platform image decoding implementation
Diffstat (limited to 'include/mbgl')
-rw-r--r-- | include/mbgl/map/map.hpp | 4 | ||||
-rw-r--r-- | include/mbgl/map/still_image.hpp | 19 | ||||
-rw-r--r-- | include/mbgl/map/view.hpp | 6 | ||||
-rw-r--r-- | include/mbgl/platform/default/headless_view.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/platform/default/image_reader.hpp | 40 | ||||
-rw-r--r-- | include/mbgl/platform/default/jpeg_reader.hpp | 72 | ||||
-rw-r--r-- | include/mbgl/platform/default/png_reader.hpp | 65 | ||||
-rw-r--r-- | include/mbgl/util/image.hpp | 35 |
8 files changed, 27 insertions, 216 deletions
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index c190088088..80b50c2fa4 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -2,6 +2,7 @@ #define MBGL_MAP_MAP #include <mbgl/util/chrono.hpp> +#include <mbgl/util/image.hpp> #include <mbgl/map/update.hpp> #include <mbgl/map/mode.hpp> #include <mbgl/util/geo.hpp> @@ -21,7 +22,6 @@ class FileSource; class View; class MapData; class MapContext; -class StillImage; class SpriteImage; class Transform; class PointAnnotation; @@ -58,7 +58,7 @@ public: // Register a callback that will get called (on the render thread) when all resources have // been loaded and a complete render occurs. - using StillImageCallback = std::function<void(std::exception_ptr, std::unique_ptr<const StillImage>)>; + using StillImageCallback = std::function<void (std::exception_ptr, UnassociatedImage&&)>; void renderStill(StillImageCallback callback); // Triggers a synchronous render. diff --git a/include/mbgl/map/still_image.hpp b/include/mbgl/map/still_image.hpp deleted file mode 100644 index 36922fe95a..0000000000 --- a/include/mbgl/map/still_image.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef MBGL_MAP_STILL_IMAGE -#define MBGL_MAP_STILL_IMAGE - -#include <mbgl/util/noncopyable.hpp> - -#include <cstdint> - -namespace mbgl { - -class StillImage : util::noncopyable { -public: - size_t width = 0; - size_t height = 0; - std::unique_ptr<uint8_t[]> pixels; -}; - -} - -#endif diff --git a/include/mbgl/map/view.hpp b/include/mbgl/map/view.hpp index fadd035e49..68bde5fe30 100644 --- a/include/mbgl/map/view.hpp +++ b/include/mbgl/map/view.hpp @@ -2,14 +2,14 @@ #define MBGL_MAP_VIEW #include <mbgl/util/chrono.hpp> -#include <functional> +#include <mbgl/util/image.hpp> +#include <functional> #include <memory> namespace mbgl { class Map; -class StillImage; enum MapChange : uint8_t { MapChangeRegionWillChange = 0, @@ -70,7 +70,7 @@ public: // Reads the pixel data from the current framebuffer. If your View implementation // doesn't support reading from the framebuffer, return a null pointer. - virtual std::unique_ptr<StillImage> readStillImage(); + virtual UnassociatedImage readStillImage(); // Notifies a watcher of map x/y/scale/rotation changes. // Must only be called from the same thread that caused the change. diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp index 50edc48428..8b25620524 100644 --- a/include/mbgl/platform/default/headless_view.hpp +++ b/include/mbgl/platform/default/headless_view.hpp @@ -39,7 +39,7 @@ public: void invalidate() override; void beforeRender() override; void afterRender() override; - std::unique_ptr<StillImage> readStillImage() override; + UnassociatedImage readStillImage() override; void resizeFramebuffer(); void resize(uint16_t width, uint16_t height); diff --git a/include/mbgl/platform/default/image_reader.hpp b/include/mbgl/platform/default/image_reader.hpp deleted file mode 100644 index e46bb08ea2..0000000000 --- a/include/mbgl/platform/default/image_reader.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef MBGL_UTIL_IMAGE_READER_HPP -#define MBGL_UTIL_IMAGE_READER_HPP - -#include <mbgl/util/noncopyable.hpp> -// stl -#include <stdexcept> -#include <string> -#include <memory> - -namespace mbgl { namespace util { - -class ImageReaderException : public std::exception -{ -private: - std::string message_; -public: - ImageReaderException(std::string const& message) - : message_(message) {} - - ~ImageReaderException() throw() {} - - virtual const char* what() const throw() - { - return message_.c_str(); - } -}; - -struct ImageReader : private noncopyable -{ - virtual unsigned width() const=0; - virtual unsigned height() const=0; - virtual std::unique_ptr<uint8_t[]> read()=0; - virtual ~ImageReader() {} -}; - -std::unique_ptr<ImageReader> getImageReader(const uint8_t* data, size_t size); - -}} - -#endif diff --git a/include/mbgl/platform/default/jpeg_reader.hpp b/include/mbgl/platform/default/jpeg_reader.hpp deleted file mode 100644 index 2f988374c3..0000000000 --- a/include/mbgl/platform/default/jpeg_reader.hpp +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef MBGL_UTIL_JPEG_READER_HPP -#define MBGL_UTIL_JPEG_READER_HPP - -#include <mbgl/platform/default/image_reader.hpp> - -// jpeg -extern "C" -{ -#include <jpeglib.h> -} - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include <boost/iostreams/stream.hpp> -#pragma GCC diagnostic pop - -namespace mbgl { namespace util { - -template <typename T> -class JpegReader : public ImageReader -{ -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: - JpegReader(const uint8_t* data, size_t size); - ~JpegReader(); - unsigned width() const; - unsigned height() const; - std::unique_ptr<uint8_t[]> read(); -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); -}; - -}} - -#endif // MBGL_UTIL_JPEG_READER_HPP diff --git a/include/mbgl/platform/default/png_reader.hpp b/include/mbgl/platform/default/png_reader.hpp deleted file mode 100644 index c869e78ea4..0000000000 --- a/include/mbgl/platform/default/png_reader.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef MBGL_UTIL_PNG_READER_HPP -#define MBGL_UTIL_PNG_READER_HPP - -#include <mbgl/platform/default/image_reader.hpp> - -extern "C" -{ -#include <png.h> -} - -#include <cstring> -#include <memory> - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include <boost/iostreams/stream.hpp> -#pragma GCC diagnostic pop - -namespace mbgl { namespace util { - -template <typename T> -class PngReader : public ImageReader -{ - 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: - PngReader(const uint8_t* data, std::size_t size); - ~PngReader(); - unsigned width() const; - unsigned height() const; - std::unique_ptr<uint8_t[]> read(); -private: - void init(); - static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length); -}; - - -}} - -#endif // MBGL_UTIL_PNG_READER_HPP diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp index fc4078501e..0604691dd1 100644 --- a/include/mbgl/util/image.hpp +++ b/include/mbgl/util/image.hpp @@ -5,30 +5,37 @@ #include <memory> namespace mbgl { -namespace util { - -std::string compress_png(size_t width, size_t height, const uint8_t* rgba); +enum ImageAlphaMode { + Unassociated, + Premultiplied +}; +template <ImageAlphaMode Mode> class Image { public: - explicit Image(const std::string& img); + Image() {} - inline const uint8_t* getData() const { return img.get(); } - inline uint32_t getWidth() const { return width; } - inline uint32_t getHeight() const { return height; } - inline operator bool() const { return img && width && height; } + Image(size_t w, size_t h) + : width(w), + height(h), + data(std::make_unique<uint8_t[]>(size())) {} -private: - // loaded image dimensions - uint32_t width = 0, height = 0; + size_t stride() const { return width * 4; } + size_t size() const { return width * height * 4; } - // the raw image data - std::unique_ptr<uint8_t[]> img; + size_t width = 0; + size_t height = 0; + std::unique_ptr<uint8_t[]> data; }; +using UnassociatedImage = Image<ImageAlphaMode::Unassociated>; +using PremultipliedImage = Image<ImageAlphaMode::Premultiplied>; + +// TODO: don't use std::string for binary data. +PremultipliedImage decodeImage(const std::string&); +std::string encodePNG(const UnassociatedImage&); -} } #endif |