From f17ab86ac25aea02f93ffa1dbef64bd726f5bb50 Mon Sep 17 00:00:00 2001 From: Inori Date: Fri, 6 Sep 2019 16:18:09 -0400 Subject: [PATCH] Use glReadPixels for Bitmap::getRaw --- binding/bitmap-binding.cpp | 14 ++++++-------- src/bitmap.cpp | 7 +++++-- src/bitmap.h | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/binding/bitmap-binding.cpp b/binding/bitmap-binding.cpp index ece8f5a5..3e6cfef6 100644 --- a/binding/bitmap-binding.cpp +++ b/binding/bitmap-binding.cpp @@ -422,14 +422,12 @@ RB_METHOD(bitmapGetRawData) RB_UNUSED_PARAM; Bitmap *b = getPrivateData(self); + int size = b->width()*b->height()*4; + VALUE ret = rb_str_new(0, size); + + GUARD_EXC ( b->getRaw(RSTRING_PTR(ret), size); ); - char *pixels{}; - int size{}; - - GUARD_EXC( pixels = (char*)b->getRaw(); ); - GUARD_EXC( size = b->width()*b->height()*4; ); - - return rb_str_new(pixels, size); + return ret; } RB_METHOD(bitmapSetRawData) @@ -437,7 +435,7 @@ RB_METHOD(bitmapSetRawData) RB_UNUSED_PARAM; VALUE str; - rb_get_args(argc, argv, "1", &str); + rb_scan_args(argc, argv, "1", &str); SafeStringValue(str); Bitmap *b = getPrivateData(self); diff --git a/src/bitmap.cpp b/src/bitmap.cpp index b0518572..15ea4991 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -883,13 +883,16 @@ void Bitmap::setPixel(int x, int y, const Color &color) p->onModified(false); } -void *Bitmap::getRaw() +void Bitmap::getRaw(void *output, int output_size) { + if (output_size != width()*height()*4) return; + guardDisposed(); GUARD_MEGA; - return p->surface->pixels; + FBO::bind(p->gl.fbo); + glReadPixels(0,0,width(),height(),GL_BGRA,GL_UNSIGNED_BYTE,output); } void Bitmap::replaceRaw(void *pixel_data, int size) diff --git a/src/bitmap.h b/src/bitmap.h index 97e8a8dd..536d5bf1 100644 --- a/src/bitmap.h +++ b/src/bitmap.h @@ -82,7 +82,7 @@ public: Color getPixel(int x, int y) const; void setPixel(int x, int y, const Color &color); - void *getRaw(); + void getRaw(void *output, int output_size); void replaceRaw(void *pixel_data, int size); void hueChange(int hue);