workaround a problem with the harmattan gcc
[drnoksnes] / screenshot.cpp
1 /*******************************************************************************
2   Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  
4   (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and
5                             Jerremy Koot (jkoot@snes9x.com)
6
7   (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net)
8
9   (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net),
10                             funkyass (funkyass@spam.shaw.ca),
11                             Joel Yliluoma (http://iki.fi/bisqwit/)
12                             Kris Bleakley (codeviolation@hotmail.com),
13                             Matthew Kendora,
14                             Nach (n-a-c-h@users.sourceforge.net),
15                             Peter Bortas (peter@bortas.org) and
16                             zones (kasumitokoduck@yahoo.com)
17
18   C4 x86 assembler and some C emulation code
19   (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),
20                             _Demo_ (_demo_@zsnes.com), and Nach
21
22   C4 C++ code
23   (c) Copyright 2003 Brad Jorsch
24
25   DSP-1 emulator code
26   (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,
27                             John Weidman, neviksti (neviksti@hotmail.com),
28                             Kris Bleakley, Andreas Naive
29
30   DSP-2 emulator code
31   (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
32                      Lord Nightmare (lord_nightmare@users.sourceforge.net
33
34   OBC1 emulator code
35   (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and
36                             Kris Bleakley
37   Ported from x86 assembler to C by sanmaiwashi
38
39   SPC7110 and RTC C++ emulator code
40   (c) Copyright 2002 Matthew Kendora with research by
41                      zsKnight, John Weidman, and Dark Force
42
43   S-DD1 C emulator code
44   (c) Copyright 2003 Brad Jorsch with research by
45                      Andreas Naive and John Weidman
46  
47   S-RTC C emulator code
48   (c) Copyright 2001 John Weidman
49   
50   ST010 C++ emulator code
51   (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora
52
53   Super FX x86 assembler emulator code 
54   (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault 
55
56   Super FX C emulator code 
57   (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman
58
59
60   SH assembler code partly based on x86 assembler code
61   (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) 
62
63  
64   Specific ports contains the works of other authors. See headers in
65   individual files.
66  
67   Snes9x homepage: http://www.snes9x.com
68  
69   Permission to use, copy, modify and distribute Snes9x in both binary and
70   source form, for non-commercial purposes, is hereby granted without fee,
71   providing that this license information and copyright notice appear with
72   all copies and any derived work.
73  
74   This software is provided 'as-is', without any express or implied
75   warranty. In no event shall the authors be held liable for any damages
76   arising from the use of this software.
77  
78   Snes9x is freeware for PERSONAL USE only. Commercial users should
79   seek permission of the copyright holders first. Commercial use includes
80   charging money for Snes9x or software derived from Snes9x.
81  
82   The copyright holders request that bug fixes and improvements to the code
83   should be forwarded to them so everyone can benefit from the modifications
84   in future versions.
85  
86   Super NES and Super Nintendo Entertainment System are trademarks of
87   Nintendo Co., Limited and its subsidiary companies.
88 *******************************************************************************/
89
90 #include <stdio.h>
91 #include <string.h>
92 #include <assert.h>
93 #include <png.h>
94
95 #include "snes9x.h"
96 #include "memmap.h"
97 #include "gfx.h"
98 #include "ppu.h"
99 #include "screenshot.h"
100
101 typedef struct {
102         png_bytep buffer;
103         png_size_t size;
104         png_size_t buf_size;
105 } ScreenshotPriv;
106
107 static void write_data(png_structp png_ptr, png_bytep data, png_size_t length)
108 {
109         ScreenshotPriv *p = (ScreenshotPriv*) png_get_io_ptr(png_ptr);
110         png_size_t new_size = p->size + length;
111
112         if (!p->buffer) {
113                 p->buf_size = new_size + 256;
114                 p->size = 0;
115                 p->buffer = (png_bytep) malloc(p->buf_size);
116                 if (!p->buffer) {
117                         png_error(png_ptr, "Out of memory");
118                         return;
119                 }
120         }
121
122         if (new_size > p->buf_size) {
123                 png_size_t new_buf_size = p->buf_size;
124                 do {
125                         new_buf_size *= 2;
126                 } while (new_size > new_buf_size);
127                 png_bytep new_buf = (png_bytep) realloc(p->buffer, new_buf_size);
128                 if (!new_buf) {
129                         png_error(png_ptr, "Out of memory");
130                         return;
131                 }
132                 p->buffer = new_buf;
133                 p->buf_size = new_buf_size;
134         }
135
136         memcpy(p->buffer + p->size, data, length);
137         p->size += length;
138
139 }
140
141 static void flush_data(png_structp png_ptr)
142 {
143         ScreenshotPriv *p = (ScreenshotPriv*) png_get_io_ptr(png_ptr);
144         if (p->size < p->buf_size) {
145                 png_bytep newbuf = (png_bytep) realloc(p->buffer, p->size);
146                 if (!newbuf) {
147                         png_error(png_ptr, "Out of memory");
148                         return;
149                 }
150                 p->buffer = newbuf;
151                 p->buf_size = p->size;
152         }
153 }
154
155 static void write_png(png_structp png_ptr, png_infop info_ptr)
156 {
157         const int width = IPPU.RenderedScreenWidth, height = IPPU.RenderedScreenHeight;
158
159         png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
160                 PNG_INTERLACE_NONE,     PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
161
162         /* 5 bits per color is around what SNES is capable */
163         png_color_8 sig_bit;
164         sig_bit.red = 5;
165         sig_bit.green = 5;
166         sig_bit.blue = 5;
167         png_set_sBIT(png_ptr, info_ptr, &sig_bit);
168         png_set_shift(png_ptr, &sig_bit);
169
170         png_write_info(png_ptr, info_ptr);
171
172         png_byte *row_data = new png_byte[png_get_rowbytes(png_ptr, info_ptr)];
173         uint8 *screen = GFX.Screen;
174         for (int y = 0; y < height; y++, screen += GFX.Pitch) {
175                 png_byte *pix_data = row_data;
176                 for (int x = 0; x < width; x++) {
177                         uint32 r, g, b;
178                         DECOMPOSE_PIXEL((*(uint16*)(screen+2*x)), r, g, b);
179                         *(pix_data++) = r;
180                         *(pix_data++) = g;
181                         *(pix_data++) = b;
182                 }
183                 png_write_row(png_ptr, row_data);
184         }
185         delete [] row_data;
186
187         png_write_end(png_ptr, info_ptr);
188 }
189
190 void * S9xScreenshot(size_t *size)
191 {
192         ScreenshotPriv priv = { 0 };
193
194         png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
195         if (!png_ptr) {
196                 return 0;
197         }
198
199         png_infop info_ptr = png_create_info_struct(png_ptr);
200         if (!info_ptr) {
201                 goto clean_png;
202         }
203
204         if(setjmp(png_jmpbuf(png_ptr))){
205                 if (priv.buffer) {
206                         free(priv.buffer);
207                         priv.buffer = 0;
208                 }
209                 priv.size = 0;
210                 goto clean_png;
211         }
212
213         png_set_write_fn(png_ptr, &priv, write_data, flush_data);
214         png_set_compression_level(png_ptr, Z_BEST_SPEED);
215
216         write_png(png_ptr, info_ptr);
217
218 clean_png:
219         png_destroy_write_struct(&png_ptr, &info_ptr);
220
221         if (priv.size && size) *size = priv.size;
222         return priv.buffer;
223 }
224
225 bool S9xSaveScreenshot(const char * file)
226 {
227         FILE *f = fopen(file, "wb");
228         if (!f) return false;
229
230         png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
231         if (!png_ptr) {
232                 return 0;
233         }
234
235         png_infop info_ptr = png_create_info_struct(png_ptr);
236         if (!info_ptr) {
237                 fclose(f);
238                 png_destroy_write_struct(&png_ptr, 0);
239                 return false;
240         }
241
242         if(setjmp(png_jmpbuf(png_ptr))){
243                 fclose(f);
244                 png_destroy_write_struct(&png_ptr, &info_ptr);
245                 return false;
246         }
247
248         png_init_io(png_ptr, f);
249         png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
250
251         write_png(png_ptr, info_ptr);
252
253         png_destroy_write_struct(&png_ptr, &info_ptr);
254
255         fclose(f);
256
257         return true;
258 }
259