diff --git a/include/llmr/geometry/sprite_atlas.hpp b/include/llmr/geometry/sprite_atlas.hpp index e6557846a..220edc701 100644 --- a/include/llmr/geometry/sprite_atlas.hpp +++ b/include/llmr/geometry/sprite_atlas.hpp @@ -12,6 +12,7 @@ namespace llmr { class Sprite; +class SpritePosition; class SpriteAtlas { public: @@ -28,23 +29,28 @@ public: // Update uninitialized sprites in this atlas from the given sprite. void update(const Sprite &sprite); - // Returns the coordinates of the icon. The getter also *creates* new icons in the atlas - // if they don't exist, but they'll be default-initialized with a a black circle. + // Returns the coordinates of a square icon. The getter also *creates* new square icons in the + // atlas if they don't exist, but they'll be default-initialized with a a black circle. Rect getIcon(int size, const std::string &name); - // Updates or creates an icon with a given size and name. The data must be a - // a (pixelRatio * size)^2 * 4 byte long RGBA image buffer. - Rect setIcon(int size, const std::string &name, const std::string &icon); + // Returns the coordinates of an image that is sourced from the sprite image. + // This getter does not create images, as the dimension of the texture us unknown if the + // sprite is not yet loaded. Instead, it returns a 0/0/0/0 rect. + Rect getImage(const std::string &name, const Sprite &sprite); // Binds the image buffer of this sprite atlas to the GPU, and uploads data if it is out // of date. void bind(bool linear = false); + inline float getWidth() const { return width; } + inline float getHeight() const { return height; } inline float getTextureWidth() const { return width * pixelRatio; } inline float getTextureHeight() const { return height * pixelRatio; } private: void allocate(); + Rect allocateImage(size_t width, size_t height); + void copy(const Rect &dst, const SpritePosition &src, const Sprite &sprite); public: const dimension width = 0; @@ -54,8 +60,8 @@ private: std::mutex mtx; float pixelRatio = 1.0f; BinPack bin; - std::map>> index; - std::set> uninitialized; + std::map> images; + std::set uninitialized; char *data = nullptr; std::atomic dirty; uint32_t texture = 0; diff --git a/include/llmr/style/sprite.hpp b/include/llmr/style/sprite.hpp index 58fd2b1ef..60a61c033 100644 --- a/include/llmr/style/sprite.hpp +++ b/include/llmr/style/sprite.hpp @@ -24,31 +24,19 @@ public: uint8_t pixelRatio = 1; }; -class ImagePosition { -public: - explicit ImagePosition() {} - explicit ImagePosition(const vec2& size, vec2 tl, vec2 br); - - vec2 size = { 0, 0 }; - vec2 tl = { 0, 0 }; - vec2 br = { 0, 0 }; -}; - - class Sprite : public std::enable_shared_from_this { public: Sprite(Map &map, float pixelRatio = 1); void load(const std::string& base_url); - ImagePosition getPosition(const std::string& name, bool repeating = false) const; const SpritePosition &getSpritePosition(const std::string& name) const; bool isLoaded() const; public: const float pixelRatio; - Raster raster; + std::unique_ptr raster; private: void asyncParseJSON(); diff --git a/include/llmr/util/image.hpp b/include/llmr/util/image.hpp index abc021e03..d38567041 100644 --- a/include/llmr/util/image.hpp +++ b/include/llmr/util/image.hpp @@ -8,6 +8,26 @@ namespace util { std::string compress_png(int width, int height, void *rgba, bool flip = false); + +class Image { +public: + Image(const std::string &img); + ~Image(); + + inline const char *getData() const { return img; } + inline uint32_t getWidth() const { return width; } + inline uint32_t getHeight() const { return height; } + +private: + // loaded image dimensions + uint32_t width = 0, height = 0; + + // the raw image data + char *img = nullptr; + +}; + + } } diff --git a/include/llmr/util/raster.hpp b/include/llmr/util/raster.hpp index 0ad29dc7a..b6d32eac8 100644 --- a/include/llmr/util/raster.hpp +++ b/include/llmr/util/raster.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -32,8 +33,6 @@ public: bool needsTransition() const; void updateTransitions(time now); - inline const char *getData() const { return img; } - public: // loaded image dimensions uint32_t width = 0, height = 0; @@ -47,10 +46,6 @@ public: // texture opacity double opacity = 0; -private: - // load raw pixels - void loadImage(const std::string& data); - private: mutable std::mutex mtx; @@ -64,7 +59,7 @@ private: uint32_t filter = 0; // the raw pixels - char *img = nullptr; + std::unique_ptr img; // fade in transition std::shared_ptr fade_transition = nullptr; diff --git a/src/geometry/sprite_atlas.cpp b/src/geometry/sprite_atlas.cpp index 4c6f51878..4f05df620 100644 --- a/src/geometry/sprite_atlas.cpp +++ b/src/geometry/sprite_atlas.cpp @@ -52,49 +52,26 @@ bool SpriteAtlas::resize(const float newRatio) { return dirty; } -Rect SpriteAtlas::getIcon(const int size, const std::string &name) { - std::lock_guard lock(mtx); - - std::map> &size_index = index[size]; - - auto rect_it = size_index.find(name); - if (rect_it != size_index.end()) { - return rect_it->second; +void copy_bitmap(const uint32_t *src, const int src_stride, const int src_x, const int src_y, + uint32_t *dst, const int dst_stride, const int dst_x, const int dst_y, + const int width, const int height) { + const int stride = width * sizeof(uint32_t); + src += src_y * src_stride + src_x; + dst += dst_y * dst_stride + dst_x; + for (int y = 0; y < height; y++, src += src_stride, dst += dst_stride) { + memcpy(dst, src, stride); } +} - // We have to allocate a new area in the bin, and store an empty image in it. - // Add a 1px border around every image. - const dimension pack_size = size + 2 * buffer; - - Rect rect = bin.allocate(pack_size, pack_size); - if (rect.w == 0) { - fprintf(stderr, "sprite atlas bitmap overflow"); - return rect; - } - - size_index.emplace(name, rect); - - allocate(); - - - // Draw an antialiased circle. - const int img_size = size * pixelRatio; - const int img_offset_x = (rect.x + buffer) * pixelRatio; - const int img_offset_y = (rect.y + buffer) * pixelRatio; - - uint32_t *sprite_img = reinterpret_cast(data); - const float blur = 1.0f / size / pixelRatio; - - const uint8_t r = 0x7F; - const uint8_t g = 0x7F; - const uint8_t b = 0x7F; - - const int sprite_stride = width * pixelRatio; - for (int y = 0; y < img_size; y++) { - const int img_y = (img_offset_y + y) * sprite_stride + img_offset_x; - for (int x = 0; x < img_size; x++) { - - const float dist = util::length(float(x) / img_size - 0.5f, float(y) / img_size - 0.5f); +void draw_circle(uint32_t *dst, const int dst_stride, const int dst_x, const int dst_y, + const int width, const int height, const float blur, + const uint8_t r = 0xFF, const uint8_t g = 0xFF, const uint8_t b = 0xFF) { + const int sprite_stride = dst_stride; + const int radius = util::min(width, height); + for (int y = 0; y < height; y++) { + const int img_y = (dst_y + y) * sprite_stride + dst_x; + for (int x = 0; x < height; x++) { + const float dist = util::length(float(x) / radius - 0.5f, float(y) / radius - 0.5f); const float t = util::smoothstep(0.5f, 0.5f - blur, dist); const uint8_t alpha = t * 255; @@ -102,11 +79,87 @@ Rect SpriteAtlas::getIcon(const int size, const std::str (uint32_t(g * t) << 8) | (uint32_t(b * t) << 16) | (uint32_t(alpha) << 24); - sprite_img[img_y + x] = color; + dst[img_y + x] = color; } } +} - uninitialized.emplace(size, name); +Rect SpriteAtlas::allocateImage(size_t width, size_t height) { + // We have to allocate a new area in the bin, and store an empty image in it. + // Add a 1px border around every image. + Rect rect = bin.allocate(width + 2 * buffer, height + 2 * buffer); + if (rect.w == 0) { + return rect; + } + + rect.x += buffer; + rect.y += buffer; + rect.w -= 2 * buffer; + rect.h -= 2 * buffer; + + return rect; +} + +Rect SpriteAtlas::getIcon(const int size, const std::string &name) { + std::lock_guard lock(mtx); + + const std::string id = name + "-" + std::to_string(size); + + auto rect_it = images.find(id); + if (rect_it != images.end()) { + return rect_it->second; + } + + Rect rect = allocateImage(size, size); + if (rect.w == 0) { + fprintf(stderr, "sprite atlas bitmap overflow\n"); + return rect; + } + + images.emplace(id, rect); + + allocate(); + + // Draw an antialiased circle. + draw_circle( + reinterpret_cast(data), + width * pixelRatio, + rect.x * pixelRatio, + rect.y * pixelRatio, + size * pixelRatio, + size * pixelRatio, + 1.0f / size / pixelRatio + ); + + uninitialized.emplace(id); + + dirty = true; + + return rect; +} + +Rect SpriteAtlas::getImage(const std::string &name, const Sprite &sprite) { + std::lock_guard lock(mtx); + + auto rect_it = images.find(name); + if (rect_it != images.end()) { + return rect_it->second; + } + + const SpritePosition &pos = sprite.getSpritePosition(name); + if (!pos.width || !pos.height) { + return Rect { 0, 0, 0, 0 }; + } + + Rect rect = allocateImage(pos.width / pos.pixelRatio, pos.height / pos.pixelRatio); + if (rect.w == 0) { + fprintf(stderr, "sprite atlas bitmap overflow\n"); + return rect; + } + + images.emplace(name, rect); + + copy(rect, pos, sprite); dirty = true; @@ -121,68 +174,39 @@ void SpriteAtlas::allocate() { } } -Rect SpriteAtlas::setIcon(const int size, const std::string &name, const std::string &icon) { - Rect rect = getIcon(size, name); +void SpriteAtlas::copy(const Rect &dst, const SpritePosition &src, const Sprite &sprite) { + if (!sprite.raster) return; + const uint32_t *src_img = reinterpret_cast(sprite.raster->getData()); + allocate(); + uint32_t *dst_img = reinterpret_cast(data); - // Copy the bitmap - const int img_size = size * pixelRatio; - const int img_offset_x = (rect.x + buffer) * pixelRatio; - const int img_offset_y = (rect.y + buffer) * pixelRatio; - - if (std::pow(size * pixelRatio, 2) * 4 /* rgba */ != icon.size()) { fprintf(stderr, "mismatched icon buffer size!"); } - - const uint32_t *icon_img = reinterpret_cast(icon.data()); - uint32_t *sprite_img = reinterpret_cast(data); - - const int sprite_stride = width * pixelRatio; - const int icon_stride = size * pixelRatio; - for (size_t y = 0; y < img_size; y++) { - const int img_y = (img_offset_y + y) * sprite_stride + img_offset_x; - const int icon_y = y * icon_stride; - for (size_t x = 0; x < img_size; x++) { - sprite_img[img_y + x] = icon_img[icon_y + x]; - } - } - - dirty = true; - - return rect; + copy_bitmap( + /* source buffer */ src_img, + /* source stride */ sprite.raster->getWidth(), + /* source x */ src.x, + /* source y */ src.y, + /* dest buffer */ dst_img, + /* dest stride */ width * pixelRatio, + /* dest x */ dst.x * pixelRatio, + /* dest y */ dst.y * pixelRatio, + /* icon dimension */ src.width, + /* icon dimension */ src.height + ); } + void SpriteAtlas::update(const Sprite &sprite) { if (!sprite.isLoaded()) return; SpriteAtlas &atlas = *this; - std::erase_if(uninitialized, [&sprite, &atlas](const std::pair &pair) { - const int &size = pair.first; - const std::string &name = pair.second; - const SpritePosition &src = sprite.getSpritePosition(name + "-" + std::to_string(size)); - if (src.width == size && src.height == size && src.pixelRatio == atlas.pixelRatio) { - const uint32_t *src_img = reinterpret_cast(sprite.raster.getData()); - - uint32_t *dst_img = reinterpret_cast(atlas.data); - Rect dst = atlas.getIcon(size, name); - dst.x = (dst.x + buffer) * atlas.pixelRatio; - dst.y = (dst.y + buffer) * atlas.pixelRatio; - dst.w = (dst.w - 2 * buffer) * atlas.pixelRatio; - dst.h = (dst.h - 2 * buffer) * atlas.pixelRatio; - - - const int src_image_stride = sprite.raster.width; - const int dst_image_stride = atlas.width * atlas.pixelRatio; - const int src_stride = src.width * src.pixelRatio; - - for (int y = 0; y < dst.h; y++) { - const int src_pos = (src.y * src.pixelRatio + y) * src_image_stride + src.x * src.pixelRatio; - const int dst_pos = (dst.y + y) * dst_image_stride + dst.x; - - // TODO: this is crashing - // memcpy(dst_img + dst_pos, src_img + src_pos, src_stride * sizeof(uint32_t)); - } - - + std::erase_if(uninitialized, [&sprite, &atlas](const std::string &name) { + Rect dst = atlas.getImage(name, sprite); + const SpritePosition &src = sprite.getSpritePosition(name); + if (src.width == dst.w * atlas.pixelRatio && src.height == dst.h * atlas.pixelRatio && src.pixelRatio == atlas.pixelRatio) { + // atlas.copy(dst, src, sprite); return true; } else { + fprintf(stderr, "sprite icon dimension mismatch\n"); return false; } }); diff --git a/src/renderer/painter_fill.cpp b/src/renderer/painter_fill.cpp index 6193e3068..289e85dfb 100644 --- a/src/renderer/painter_fill.cpp +++ b/src/renderer/painter_fill.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace llmr; @@ -73,16 +74,17 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons if ((fill_color[3] >= 1.0f) == (pass == Opaque)) { auto &sprite = map.getStyle().sprite; - if (properties.image.size() && sprite && sprite->isLoaded()) { - // Draw texture fill - ImagePosition imagePos = sprite->getPosition(properties.image, true); + if (properties.image.size() && sprite) { + auto &spriteAtlas = map.getSpriteAtlas(); + Rect imagePos = spriteAtlas.getImage(properties.image, *sprite); + float factor = 8.0 / std::pow(2, map.getState().getIntegerZoom() - id.z); float mix = std::fmod(map.getState().getZoom(), 1.0); std::array imageSize = {{ - imagePos.size.x * factor, - imagePos.size.y *factor + imagePos.w * factor, + imagePos.h * factor } }; @@ -96,11 +98,17 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons patternShader->setMatrix(vtxMatrix); patternShader->setOffset(offset); patternShader->setPatternSize(imageSize); - patternShader->setPatternTopLeft({{ imagePos.tl.x, imagePos.tl.y }}); - patternShader->setPatternBottomRight({{ imagePos.br.x, imagePos.br.y }}); + patternShader->setPatternTopLeft({{ + float(imagePos.x) / spriteAtlas.getWidth(), + float(imagePos.y) / spriteAtlas.getHeight(), + }}); + patternShader->setPatternBottomRight({{ + float(imagePos.x + imagePos.w) / spriteAtlas.getWidth(), + float(imagePos.y + imagePos.h) / spriteAtlas.getHeight(), + }}); patternShader->setColor(fill_color); patternShader->setMix(mix); - sprite->raster.bind(true); + spriteAtlas.bind(true); // Draw the actual triangles into the color & stencil buffer. glDepthRange(strata + strata_epsilon, 1.0f); diff --git a/src/style/sprite.cpp b/src/style/sprite.cpp index fb3a959f9..bb24c5fec 100644 --- a/src/style/sprite.cpp +++ b/src/style/sprite.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -18,14 +19,9 @@ SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t pixelRatio(pixelRatio) { } -ImagePosition::ImagePosition(const vec2& size, vec2 tl, vec2 br) - : size(size), - tl(tl), - br(br) {} - Sprite::Sprite(Map &map, float pixelRatio) : pixelRatio(pixelRatio), - raster(map.getTexturepool()), + raster(), map(map), loaded(false) { } @@ -55,7 +51,7 @@ void Sprite::load(const std::string& base_url) { } void Sprite::complete(std::shared_ptr &sprite) { - if (sprite->raster.isLoaded() && sprite->pos.size()) { + if (sprite->raster && sprite->pos.size()) { sprite->loaded = true; sprite->map.update(); fprintf(stderr, "sprite loaded\n"); @@ -75,7 +71,7 @@ void Sprite::asyncParseJSON() { } void Sprite::parseImage(std::shared_ptr &sprite) { - sprite->raster.load(sprite->image); + sprite->raster = std::make_unique(sprite->image); sprite->image.clear(); } @@ -107,34 +103,6 @@ void Sprite::parseJSON(std::shared_ptr &sprite) { } } -ImagePosition Sprite::getPosition(const std::string& name, bool repeating) const { - if (!isLoaded()) return ImagePosition {}; - - // `repeating` indicates that the image will be used in a repeating pattern - // repeating pattern images are assumed to have a 1px padding that mirrors the opposite edge - // positions for repeating images are adjusted to exclude the edge - int8_t offset = repeating ? 1 : 0; - - auto it = pos.find(name); - if (it == pos.end()) return ImagePosition {}; - - const SpritePosition& pos = it->second; - return ImagePosition { - { - pos.width, - pos.height - }, - { - (float)(pos.x + offset) / raster.width, - (float)(pos.y + offset) / raster.height - }, - { - (float)(pos.x + pos.width - 2 * offset) / raster.width, - (float)(pos.y + pos.height - 2 * offset) / raster.height - } - }; -} - const SpritePosition &Sprite::getSpritePosition(const std::string& name) const { if (!isLoaded()) return empty; auto it = pos.find(name); diff --git a/src/util/image.cpp b/src/util/image.cpp index 3a29a95a2..156828183 100644 --- a/src/util/image.cpp +++ b/src/util/image.cpp @@ -1,6 +1,8 @@ #include #include +#include + std::string llmr::util::compress_png(int width, int height, void *rgba, bool flip) { png_voidp error_ptr = 0; @@ -48,3 +50,127 @@ std::string llmr::util::compress_png(int width, int height, void *rgba, bool fli return result; } + + +using namespace llmr::util; + + +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(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) { + 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 = (char *)malloc(width * height * 4); + char *surface = img; + 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) { + free(img); + img = nullptr; + } + width = 0; + height = 0; + } +} + +Image::~Image() { + if (img) { + free(img); + img = nullptr; + } +} diff --git a/src/util/raster.cpp b/src/util/raster.cpp index 90268c09b..4f26f8b55 100644 --- a/src/util/raster.cpp +++ b/src/util/raster.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -18,9 +19,7 @@ Raster::Raster(Texturepool &texturepool) {} Raster::~Raster() { - if (img && !textured) { - free(img); - } else if (textured) { + if (textured) { texturepool.removeTextureID(texture); } } @@ -31,130 +30,17 @@ bool Raster::isLoaded() const { } bool Raster::load(const std::string &data) { - loadImage(data); + img = std::make_unique(data); + width = img->getWidth(); + height = img->getHeight(); std::lock_guard lock(mtx); - if (img) { + if (img->getData()) { loaded = true; } return loaded; } -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(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); -} - -void Raster::loadImage(const std::string& data) { - std::lock_guard lock(mtx); - - 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 = (char *)malloc(width * height * 4); - char *surface = img; - 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) { - free(img); - img = nullptr; - } - width = 0; - height = 0; - } -} - void Raster::bind(bool linear) { if (!width || !height) { @@ -167,9 +53,8 @@ void Raster::bind(bool linear) { glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); - free(img); - img = nullptr; + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData()); + img.reset(); textured = true; } else if (textured) { glBindTexture(GL_TEXTURE_2D, texture);