summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorartemp <artem@mapnik.org>2014-11-03 10:43:55 +0000
committerartemp <artem@mapnik.org>2014-11-03 10:43:55 +0000
commit4a5d4ea0aa823871b504a9189d08667daf91b63c (patch)
treea1d48291592dff3bdd0e73f4e0c163961d8c5336 /platform
parent318d08706fdf1ee8e32ed00c96d2e8506a9a9935 (diff)
parent551ff954a5e30c89c27f59a9848ba0a9be871adc (diff)
downloadqtlocation-mapboxgl-4a5d4ea0aa823871b504a9189d08667daf91b63c.tar.gz
Merge branch 'master' into nunicode
Conflicts: configure styles
Diffstat (limited to 'platform')
-rw-r--r--platform/darwin/image.mm125
-rw-r--r--platform/default/headless_view.cpp4
-rw-r--r--platform/default/image.cpp178
3 files changed, 305 insertions, 2 deletions
diff --git a/platform/darwin/image.mm b/platform/darwin/image.mm
new file mode 100644
index 0000000000..50c871533d
--- /dev/null
+++ b/platform/darwin/image.mm
@@ -0,0 +1,125 @@
+#include <mbgl/util/image.hpp>
+
+#import <ImageIO/ImageIO.h>
+
+#if TARGET_OS_IPHONE
+#import <MobileCoreServices/MobileCoreServices.h>
+#else
+#import <CoreServices/CoreServices.h>
+#endif
+
+namespace mbgl {
+namespace util {
+
+std::string compress_png(int width, int height, void *rgba) {
+ CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, width * height * 4, NULL);
+ if (!provider) {
+ return "";
+ }
+
+ CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
+ if (!color_space) {
+ CGDataProviderRelease(provider);
+ return "";
+ }
+
+ CGImageRef image = CGImageCreate(width, height, 8, 32, 4 * width, color_space,
+ kCGBitmapByteOrderDefault | kCGImageAlphaLast, provider, NULL, false,
+ kCGRenderingIntentDefault);
+ if (!image) {
+ CGColorSpaceRelease(color_space);
+ CGDataProviderRelease(provider);
+ return "";
+ }
+
+ CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
+ if (!data) {
+ CGImageRelease(image);
+ CGColorSpaceRelease(color_space);
+ CGDataProviderRelease(provider);
+ return "";
+ }
+
+ CGImageDestinationRef image_destination = CGImageDestinationCreateWithData(data, kUTTypePNG, 1, NULL);
+ if (!image_destination) {
+ CFRelease(data);
+ CGImageRelease(image);
+ CGColorSpaceRelease(color_space);
+ CGDataProviderRelease(provider);
+ return "";
+ }
+
+ CGImageDestinationAddImage(image_destination, image, NULL);
+ CGImageDestinationFinalize(image_destination);
+
+ const std::string result {
+ reinterpret_cast<const char *>(CFDataGetBytePtr(data)),
+ static_cast<size_t>(CFDataGetLength(data))
+ };
+
+ CFRelease(image_destination);
+ CFRelease(data);
+ CGImageRelease(image);
+ CGColorSpaceRelease(color_space);
+ CGDataProviderRelease(provider);
+
+ return result;
+}
+
+Image::Image(const std::string &source_data) {
+ CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const unsigned char *>(source_data.data()), source_data.size(), kCFAllocatorNull);
+ if (!data) {
+ return;
+ }
+
+ CGImageSourceRef image_source = CGImageSourceCreateWithData(data, NULL);
+ if (!image_source) {
+ CFRelease(data);
+ return;
+ }
+
+ CGImageRef image = CGImageSourceCreateImageAtIndex(image_source, 0, NULL);
+ if (!image) {
+ CFRelease(image_source);
+ CFRelease(data);
+ return;
+ }
+
+ CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
+ if (!color_space) {
+ CGImageRelease(image);
+ CFRelease(image_source);
+ CFRelease(data);
+ return;
+ }
+
+ width = CGImageGetWidth(image);
+ height = CGImageGetHeight(image);
+ CGRect rect = {{ 0, 0 }, { static_cast<CGFloat>(width), static_cast<CGFloat>(height) }};
+
+ img = ::std::unique_ptr<char[]>(new char[width * height * 4]());
+ CGContextRef context = CGBitmapContextCreate(img.get(), width, height, 8, width * 4,
+ color_space, kCGImageAlphaPremultipliedLast);
+ if (!context) {
+ CGColorSpaceRelease(color_space);
+ CGImageRelease(image);
+ CFRelease(image_source);
+ CFRelease(data);
+ width = 0;
+ height = 0;
+ img.release();
+ return;
+ }
+
+ CGContextSetBlendMode(context, kCGBlendModeCopy);
+ CGContextDrawImage(context, rect, image);
+
+ CGContextRelease(context);
+ CGColorSpaceRelease(color_space);
+ CGImageRelease(image);
+ CFRelease(image_source);
+ CFRelease(data);
+}
+
+}
+} \ No newline at end of file
diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp
index ec5c4aabec..71096beba0 100644
--- a/platform/default/headless_view.cpp
+++ b/platform/default/headless_view.cpp
@@ -181,11 +181,11 @@ void HeadlessView::resize(uint16_t width, uint16_t height, float pixelRatio) {
make_inactive();
}
-const std::unique_ptr<uint32_t[]> HeadlessView::readPixels() {
+std::unique_ptr<uint32_t[]> HeadlessView::readPixels() {
const unsigned int w = width_ * pixelRatio_;
const unsigned int h = height_ * pixelRatio_;
- std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
+ auto pixels = std::unique_ptr<uint32_t[]>(new uint32_t[w * h]);
make_active();
glReadPixels(0, 0, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get());
diff --git a/platform/default/image.cpp b/platform/default/image.cpp
new file mode 100644
index 0000000000..68d1786913
--- /dev/null
+++ b/platform/default/image.cpp
@@ -0,0 +1,178 @@
+#include <mbgl/util/image.hpp>
+
+#include <png.h>
+
+#include <cassert>
+#include <cstdlib>
+#include <stdexcept>
+#include <cstring>
+
+
+namespace mbgl {
+namespace util {
+
+std::string compress_png(int width, int height, void *rgba) {
+ png_voidp error_ptr = 0;
+ png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, error_ptr, NULL, NULL);
+ if (!png_ptr) {
+ fprintf(stderr, "Couldn't create png_ptr\n");
+ return "";
+ }
+
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+ if (!png_ptr) {
+ png_destroy_write_struct(&png_ptr, (png_infopp)0);
+ fprintf(stderr, "Couldn't create info_ptr\n");
+ return "";
+ }
+
+ png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+ jmp_buf *jmp_context = (jmp_buf *)png_get_error_ptr(png_ptr);
+ if (jmp_context) {
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ return "";
+ }
+
+ std::string result;
+ png_set_write_fn(png_ptr, &result, [](png_structp png_ptr_, png_bytep data, png_size_t length) {
+ std::string *out = static_cast<std::string *>(png_get_io_ptr(png_ptr_));
+ out->append(reinterpret_cast<char *>(data), length);
+ }, NULL);
+
+ struct ptrs {
+ ptrs(size_t count) : rows(new png_bytep[count]) {}
+ ~ptrs() { delete[] rows; }
+ png_bytep *rows = nullptr;
+ } pointers(height);
+
+ for (int i = 0; i < height; i++) {
+ pointers.rows[i] = (png_bytep)((png_bytep)rgba + width * 4 * i);
+ }
+
+ png_set_rows(png_ptr, info_ptr, pointers.rows);
+ png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+
+ 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) {
+ throw std::runtime_error(error_msg);
+}
+
+void warningHandler(png_structp, png_const_charp error_msg) {
+ fprintf(stderr, "PNG: %s\n", error_msg);
+}
+
+Image::Image(const std::string &data) {
+ Buffer buffer(data);
+
+ if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
+ fprintf(stderr, "image is not a valid PNG image\n");
+ 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);
+ }
+
+ 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);
+
+ 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) {
+ fprintf(stderr, "loading PNG failed: %s\n", e.what());
+ png_destroy_read_struct(&png, &info, nullptr);
+ if (img) {
+ img.reset();
+ }
+ width = 0;
+ height = 0;
+ }
+}
+
+}
+}