From 0c1e378bc9555f6cf826bb38b1a36fa742f8ce9b Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 24 Nov 2015 10:07:18 -0800 Subject: [core] Rewrite image handling * Consolidate Image and StillImage * Typecheck unassociated vs premultiplied images * Rewrite default platform image decoding implementation --- include/mbgl/map/map.hpp | 4 +- include/mbgl/map/still_image.hpp | 19 ------- include/mbgl/map/view.hpp | 6 +-- include/mbgl/platform/default/headless_view.hpp | 2 +- include/mbgl/platform/default/image_reader.hpp | 40 -------------- include/mbgl/platform/default/jpeg_reader.hpp | 72 ------------------------- include/mbgl/platform/default/png_reader.hpp | 65 ---------------------- include/mbgl/util/image.hpp | 35 +++++++----- 8 files changed, 27 insertions(+), 216 deletions(-) delete mode 100644 include/mbgl/map/still_image.hpp delete mode 100644 include/mbgl/platform/default/image_reader.hpp delete mode 100644 include/mbgl/platform/default/jpeg_reader.hpp delete mode 100644 include/mbgl/platform/default/png_reader.hpp (limited to 'include') 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 +#include #include #include #include @@ -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)>; + using StillImageCallback = std::function; 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 - -#include - -namespace mbgl { - -class StillImage : util::noncopyable { -public: - size_t width = 0; - size_t height = 0; - std::unique_ptr 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 -#include +#include +#include #include 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 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 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 -// stl -#include -#include -#include - -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 read()=0; - virtual ~ImageReader() {} -}; - -std::unique_ptr 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 - -// jpeg -extern "C" -{ -#include -} - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include -#pragma GCC diagnostic pop - -namespace mbgl { namespace util { - -template -class JpegReader : public ImageReader -{ -public: - using source_type = T; - using input_stream = boost::iostreams::stream; - 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 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 - -extern "C" -{ -#include -} - -#include -#include - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-pragmas" -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#pragma GCC diagnostic ignored "-Wshadow" -#include -#pragma GCC diagnostic pop - -namespace mbgl { namespace util { - -template -class PngReader : public ImageReader -{ - using source_type = T; - using input_stream = boost::iostreams::stream; - - 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 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 namespace mbgl { -namespace util { - -std::string compress_png(size_t width, size_t height, const uint8_t* rgba); +enum ImageAlphaMode { + Unassociated, + Premultiplied +}; +template 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(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 img; + size_t width = 0; + size_t height = 0; + std::unique_ptr data; }; +using UnassociatedImage = Image; +using PremultipliedImage = Image; + +// TODO: don't use std::string for binary data. +PremultipliedImage decodeImage(const std::string&); +std::string encodePNG(const UnassociatedImage&); -} } #endif -- cgit v1.2.1