aboutsummaryrefslogtreecommitdiff
path: root/frontend/common
diff options
context:
space:
mode:
authornotaz2011-03-12 22:09:04 +0200
committernotaz2011-03-14 00:59:31 +0200
commit29a8c4f3e7fee7678e4b18de1748b9c4255dcef2 (patch)
tree933c42d696ae5f7ff3c6f0cf5c32aae71017d4c9 /frontend/common
parent9411ba241f8f6ea0926a76c13a3329a95856b420 (diff)
downloadpcsx_rearmed-29a8c4f3e7fee7678e4b18de1748b9c4255dcef2.tar.gz
pcsx_rearmed-29a8c4f3e7fee7678e4b18de1748b9c4255dcef2.tar.bz2
pcsx_rearmed-29a8c4f3e7fee7678e4b18de1748b9c4255dcef2.zip
frontend: add screenshot functionality
Diffstat (limited to 'frontend/common')
-rw-r--r--frontend/common/readpng.c75
-rw-r--r--frontend/common/readpng.h1
2 files changed, 75 insertions, 1 deletions
diff --git a/frontend/common/readpng.c b/frontend/common/readpng.c
index b7bad57..cad1217 100644
--- a/frontend/common/readpng.c
+++ b/frontend/common/readpng.c
@@ -1,5 +1,5 @@
/*
- * (C) Gražvydas "notaz" Ignotas, 2008-2010
+ * (C) Gražvydas "notaz" Ignotas, 2008-2011
*
* This work is licensed under the terms of any of these licenses
* (at your option):
@@ -9,6 +9,7 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <png.h>
#include "readpng.h"
@@ -190,4 +191,76 @@ done:
return ret;
}
+int writepng(const char *fname, unsigned short *src, int w, int h)
+{
+ png_structp png_ptr = NULL;
+ png_infop info_ptr = NULL;
+ png_bytepp row_pointers;
+ int i, j, ret = -1;
+ FILE *f;
+
+ f = fopen(fname, "wb");
+ if (f == NULL) {
+ lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
+ return -1;
+ }
+
+ row_pointers = calloc(h, sizeof(row_pointers[0]));
+ if (row_pointers == NULL)
+ goto end1;
+
+ for (i = 0; i < h; i++) {
+ unsigned char *dst = malloc(w * 3);
+ if (dst == NULL)
+ goto end2;
+ row_pointers[i] = dst;
+ for (j = 0; j < w; j++, src++, dst += 3) {
+ dst[0] = (*src & 0xf800) >> 8;
+ dst[1] = (*src & 0x07e0) >> 3;
+ dst[2] = (*src & 0x001f) << 3;
+ }
+ }
+
+ /* initialize stuff */
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (png_ptr == NULL) {
+ fprintf(stderr, "png_create_write_struct() failed");
+ goto end2;
+ }
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if (info_ptr == NULL) {
+ fprintf(stderr, "png_create_info_struct() failed");
+ goto end3;
+ }
+
+ if (setjmp(png_jmpbuf(png_ptr)) != 0) {
+ fprintf(stderr, "error in png code\n");
+ goto end4;
+ }
+
+ png_init_io(png_ptr, f);
+
+ png_set_IHDR(png_ptr, info_ptr, w, h,
+ 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+ png_write_info(png_ptr, info_ptr);
+ png_write_image(png_ptr, row_pointers);
+ png_write_end(png_ptr, NULL);
+
+ ret = 0;
+
+end4:
+// png_destroy_info_struct(png_ptr, &info_ptr); // freed below
+end3:
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+end2:
+ for (i = 0; i < h; i++)
+ free(row_pointers[i]);
+ free(row_pointers);
+end1:
+ fclose(f);
+ return ret;
+}
diff --git a/frontend/common/readpng.h b/frontend/common/readpng.h
index ce5d635..924b341 100644
--- a/frontend/common/readpng.h
+++ b/frontend/common/readpng.h
@@ -12,6 +12,7 @@ extern "C" {
#endif
int readpng(void *dest, const char *fname, readpng_what what, int w, int h);
+int writepng(const char *fname, unsigned short *src, int w, int h);
#ifdef __cplusplus
}