diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-10-29 19:58:34 -0400 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-10-30 12:02:28 -0400 |
commit | e3bc69b7e77aa6771c8db3695f12548447e1de51 (patch) | |
tree | 0c1a0bde928fea3e09b1d72222b400176c0936c7 /src | |
parent | 7a13db9a863c9be58709e941f44eb2c370276e2a (diff) | |
download | qtlocation-mapboxgl-e3bc69b7e77aa6771c8db3695f12548447e1de51.tar.gz |
use CoreImage for decoding/encoding images on osx/ios
Diffstat (limited to 'src')
-rw-r--r-- | src/geometry/sprite_atlas.cpp | 13 | ||||
-rw-r--r-- | src/util/image.cpp | 176 |
2 files changed, 1 insertions, 188 deletions
diff --git a/src/geometry/sprite_atlas.cpp b/src/geometry/sprite_atlas.cpp index e5bad1eada..d7ef1e789b 100644 --- a/src/geometry/sprite_atlas.cpp +++ b/src/geometry/sprite_atlas.cpp @@ -72,18 +72,7 @@ void copy_bitmap(const uint32_t *src, const int src_stride, const int src_x, con dst += dst_y * dst_stride + dst_x; for (int y = 0; y < height; y++, src += src_stride, dst += dst_stride) { for (int x = 0; x < width; x++) { - const uint8_t *s = reinterpret_cast<const uint8_t *>(src + x); - uint8_t *d = reinterpret_cast<uint8_t *>(dst + x); - - // Premultiply the bitmap. - // Note: We don't need to clamp the component values to 0..255, since - // the source value is already 0..255 and the operation means they will - // stay within the range of 0..255 and won't overflow. - const uint8_t a = s[3]; - d[0] = s[0] * a / 255; - d[1] = s[1] * a / 255; - d[2] = s[2] * a / 255; - d[3] = a; + dst[x] = src[x]; } } } diff --git a/src/util/image.cpp b/src/util/image.cpp deleted file mode 100644 index bd5d7b8913..0000000000 --- a/src/util/image.cpp +++ /dev/null @@ -1,176 +0,0 @@ -#include <mbgl/util/image.hpp> -#include <mbgl/util/std.hpp> - -#include <png.h> - -#include <cassert> -#include <cstdlib> -#include <stdexcept> - - -namespace mbgl { -namespace util { - -std::string compress_png(int width, int height, void *rgba, bool flip) { - 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[flip ? height - 1 - i : 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 { - 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, bool flip) { - 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_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[flip ? height - 1 - i : 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; - } -} - -} -} |