summaryrefslogtreecommitdiff
path: root/platform/default/image.cpp
diff options
context:
space:
mode:
authorartemp <artem@mapnik.org>2014-11-07 11:49:31 +0000
committerLeith Bade <leith@mapbox.com>2014-11-28 00:14:11 +1100
commitae2d4e740ee1f7f7dd674b4c6b1ccc697500a166 (patch)
treef1488f8459acd1ed20cf63f3136a2e14b32f6219 /platform/default/image.cpp
parent90480ee58cbc5a9056f857c151b2b4f0cf350608 (diff)
downloadqtlocation-mapboxgl-ae2d4e740ee1f7f7dd674b4c6b1ccc697500a166.tar.gz
add image_reader abstraction layer to hanfle multiple image formats
(work-in-progress)
Diffstat (limited to 'platform/default/image.cpp')
-rw-r--r--platform/default/image.cpp128
1 files changed, 15 insertions, 113 deletions
diff --git a/platform/default/image.cpp b/platform/default/image.cpp
index eb4b3e8f55..bc9e720069 100644
--- a/platform/default/image.cpp
+++ b/platform/default/image.cpp
@@ -8,7 +8,7 @@
#include <cstdlib>
#include <stdexcept>
#include <cstring>
-
+#include <mbgl/util/image_reader.hpp>
// Check png library version.
const static bool png_version_check = []() {
@@ -73,121 +73,23 @@ std::string compress_png(int width, int height, void *rgba) {
return result;
}
-
-struct Buffer {
- Buffer(const std::string& data_)
- : data(data_.data()), length(data_.size()) {}
- const char *const data = 0;
- const size_t length = 0;
- size_t pos = 0;
-};
-
-void readCallback(png_structp png, png_bytep data, png_size_t length) {
- Buffer *reader = static_cast<Buffer *>(png_get_io_ptr(png));
-
- // Read `length` bytes into `data`.
- if (reader->pos + length > reader->length) {
- png_error(png, "Read Error");
- } else {
- std::memcpy(data, reader->data + reader->pos, length);
- reader->pos += length;
- }
-}
-
-void errorHandler(png_structp, png_const_charp error_msg) {
- Log::Error(Event::Image, "PNG: %s", error_msg);
- throw std::runtime_error(error_msg);
-}
-
-void warningHandler(png_structp, png_const_charp error_msg) {
- Log::Warning(Event::General, "PNG: %s", error_msg);
-}
-
-Image::Image(const std::string &data) {
- Buffer buffer(data);
-
- if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
- Log::Error(Event::Image, "image is not a valid PNG image");
- return;
- }
-
- png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, errorHandler, warningHandler);
- assert(png);
-
- png_infop info = png_create_info_struct(png);
- assert(info);
-
- int depth, color, interlace;
-
- try {
- png_set_read_fn(png, (png_voidp)&buffer, readCallback);
- png_read_info(png, info);
- png_get_IHDR(png, info, (png_uint_32*)&width, (png_uint_32*)&height, &depth, &color, &interlace, nullptr, nullptr);
- bool alpha = (color & PNG_COLOR_MASK_ALPHA) || png_get_valid(png, info, PNG_INFO_tRNS);
-
- // From http://trac.mapnik.org/browser/trunk/src/png_reader.cpp
- if (color == PNG_COLOR_TYPE_PALETTE)
- png_set_expand(png);
- if (color == PNG_COLOR_TYPE_GRAY)
- png_set_expand(png);
- if (png_get_valid(png, info, PNG_INFO_tRNS))
- png_set_expand(png);
- if (depth == 16)
- png_set_strip_16(png);
- if (depth < 8)
- png_set_packing(png);
- if (color == PNG_COLOR_TYPE_GRAY ||
- color == PNG_COLOR_TYPE_GRAY_ALPHA)
- png_set_gray_to_rgb(png);
-
- if (interlace == PNG_INTERLACE_ADAM7)
- png_set_interlace_handling(png);
-
- // Always add an alpha channel.
- if (!alpha) {
- png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER);
- }
-
- // FIXME work around gamma crash
- double gamma;
- if (png_get_gAMA(png, info, &gamma))
- png_set_gamma(png, 2.2, gamma);
-
- png_set_alpha_mode(png, PNG_ALPHA_PREMULTIPLIED, 2.2);
-
- png_read_update_info(png, info);
-
- png_size_t rowbytes = png_get_rowbytes(png, info);
- assert(width * 4 == rowbytes);
-
+Image::Image(std::string const& data)
+{
+ try
+ {
+ std::unique_ptr<image_reader> reader(get_image_reader(data.c_str(), data.size()));
+ width = reader->width();
+ height = reader->height();
img = ::std::unique_ptr<char[]>(new char[width * height * 4]());
-
- char *surface = img.get();
- assert(surface);
-
- struct ptrs {
- ptrs(size_t count) : rows(new png_bytep[count]) {}
- ~ptrs() { delete[] rows; }
- png_bytep *rows = nullptr;
- } pointers(height);
- for (unsigned i = 0; i < height; ++i) {
- pointers.rows[i] = (png_bytep)(surface + (i * rowbytes));
- }
-
- // Read image data
- png_read_image(png, pointers.rows);
-
- png_read_end(png, nullptr);
-
- png_destroy_read_struct(&png, &info, nullptr);
- } catch (std::exception& e) {
- Log::Error(Event::Image, "loading PNG failed: %s", e.what());
- png_destroy_read_struct(&png, &info, nullptr);
- if (img) {
- img.reset();
- }
+ reader->read(0, 0, width, height, img.get());
+ }
+ catch (image_reader_exception const& ex)
+ {
+ fprintf(stderr, "ImageReader: %s\n", ex.what());
+ img.reset();
width = 0;
height = 0;
+
}
}