From 1856e677a3bf45cebcebec201ed127804db3e569 Mon Sep 17 00:00:00 2001 From: Amaryllis Kulla Date: Fri, 5 Nov 2021 18:18:43 +0100 Subject: [PATCH] Bitmap: Add ::writeToPng() --- binding-mri/bitmap-binding.cpp | 14 ++++++++++++++ src/bitmap.cpp | 33 +++++++++++++++++++++------------ src/bitmap.h | 3 +++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/binding-mri/bitmap-binding.cpp b/binding-mri/bitmap-binding.cpp index 49e0093..11a0697 100644 --- a/binding-mri/bitmap-binding.cpp +++ b/binding-mri/bitmap-binding.cpp @@ -433,6 +433,18 @@ RB_METHOD(bitmapInitializeCopy) return self; } +RB_METHOD(bitmapWriteToPng) +{ + Bitmap *b = getPrivateData(self); + const char *filename; + + rb_get_args(argc, argv, "z", &filename RB_ARG_END); + + b->writeToPng(filename); + + return self; +} + void bitmapBindingInit() @@ -466,5 +478,7 @@ bitmapBindingInit() _rb_define_method(klass, "radial_blur", bitmapRadialBlur); } + _rb_define_method(klass, "write_to_png", bitmapWriteToPng); + INIT_PROP_BIND(Bitmap, Font, "font"); } diff --git a/src/bitmap.cpp b/src/bitmap.cpp index a2f27be..cec61bb 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -245,6 +245,20 @@ struct BitmapPrivate self->modified(); } + + void downloadToSurface() + { + if (!surface) + allocSurface(); + + FBO::bind(gl.fbo); + + glState.viewport.pushSet(IntRect(0, 0, gl.width, gl.height)); + + ::gl.ReadPixels(0, 0, gl.width, gl.height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + + glState.viewport.pop(); + } }; struct BitmapOpenHandler : FileSystem::OpenHandler @@ -900,18 +914,7 @@ Color Bitmap::getPixel(int x, int y) const if (x < 0 || y < 0 || x >= width() || y >= height()) return Vec4(); - if (!p->surface) - { - p->allocSurface(); - - FBO::bind(p->gl.fbo); - - glState.viewport.pushSet(IntRect(0, 0, width(), height())); - - gl.ReadPixels(0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, p->surface->pixels); - - glState.viewport.pop(); - } + p->downloadToSurface(); uint32_t pixel = getPixelAt(p->surface, p->format, x, y); @@ -1400,6 +1403,12 @@ void Bitmap::setInitFont(Font *value) p->font = value; } +void Bitmap::writeToPng(const char *filename) +{ + p->downloadToSurface(); + IMG_SavePNG(p->surface, filename); +} + TEXFBO &Bitmap::getGLTypes() { return p->gl; diff --git a/src/bitmap.h b/src/bitmap.h index b744911..68bb7f9 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -105,6 +105,9 @@ public: * use at construction */ void setInitFont(Font *value); + /* extensions */ + void writeToPng(const char *filename); + /* */ TEXFBO &getGLTypes(); SDL_Surface *megaSurface() const;