Created
February 7, 2012 20:43
-
-
Save slembcke/1761818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Some old PNG screenshot code that I used in a Ruby binding. | |
static VALUE | |
rbas_screen_shot(VALUE self, VALUE filename) | |
{ | |
int bytes = AS_SCREEN_W * AS_SCREEN_H * 3; | |
GLubyte pixels[bytes]; | |
glReadPixels(0, 0, AS_SCREEN_W, AS_SCREEN_H, GL_RGB, GL_UNSIGNED_BYTE, pixels); | |
char *name = StringValuePtr(filename); | |
FILE *png_file = fopen(name, "wb"); | |
if(!png_file) | |
rb_raise(rb_eRuntimeError, "\"%s\" could not be opened for writing.", name); | |
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
if(!png_ptr) | |
rb_raise(rb_eRuntimeError, "LibPNG error when saving \"%s\".", name); | |
png_infop info_ptr = png_create_info_struct(png_ptr); | |
if(!info_ptr) | |
rb_raise(rb_eRuntimeError, "LibPNG error when saving \"%s\".", name); | |
if(setjmp(png_jmpbuf(png_ptr))) | |
rb_raise(rb_eRuntimeError, "LibPNG error when saving \"%s\".", name); | |
png_init_io(png_ptr, png_file); | |
png_set_IHDR(png_ptr, info_ptr, AS_SCREEN_W, AS_SCREEN_H, | |
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, | |
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | |
png_byte* row_ptrs[AS_SCREEN_H]; | |
int i; | |
for (i=0; i<AS_SCREEN_H; i++) | |
row_ptrs[i] = pixels + (AS_SCREEN_H - 1 - i)*(AS_SCREEN_W*3); | |
png_set_rows(png_ptr, info_ptr, row_ptrs); | |
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); | |
png_destroy_write_struct(&png_ptr, &info_ptr); | |
fclose(png_file); | |
return Qnil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment