Bitmap: Add ::writeToPng()

This commit is contained in:
Amaryllis Kulla 2021-11-05 18:18:43 +01:00
parent a92adee7f8
commit 1856e677a3
3 changed files with 38 additions and 12 deletions

View File

@ -433,6 +433,18 @@ RB_METHOD(bitmapInitializeCopy)
return self;
}
RB_METHOD(bitmapWriteToPng)
{
Bitmap *b = getPrivateData<Bitmap>(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");
}

View File

@ -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;

View File

@ -105,6 +105,9 @@ public:
* use at construction */
void setInitFont(Font *value);
/* extensions */
void writeToPng(const char *filename);
/* <internal> */
TEXFBO &getGLTypes();
SDL_Surface *megaSurface() const;