157533a7ee20aeafe1a9787e12640c1fca701a64
[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
91 #ifdef HAVE_CONFIG_H
92         #include <config.h>
93 #endif
94 #include <stdio.h>
95
96 #ifndef __WIN32__
97 #include <unistd.h>
98 #else
99 #include <direct.h>
100 #endif
101 #include <string.h>
102 #include <fcntl.h>
103
104 #ifdef HAVE_LIBPNG
105 #include <png.h>
106 #endif
107
108 #include "snes9x.h"
109 #include "memmap.h"
110 #include "gfx.h"
111 #include "ppu.h"
112 #include "screenshot.h"
113
114 bool8 S9xDoScreenshot(int width, int height){
115 #ifdef HAVE_LIBPNG
116     FILE *fp;
117     png_structp png_ptr;
118     png_infop info_ptr;
119     png_color_8 sig_bit;
120     png_color pngpal[256];
121     int imgwidth;
122     int imgheight;
123     const char *fname=S9xGetFilenameInc(".png");
124     
125     Settings.TakeScreenshot=FALSE;
126
127     if((fp=fopen(fname, "wb"))==NULL){
128         perror("Screenshot failed");
129         return FALSE;
130     }
131
132     png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
133     if(!png_ptr){
134         fclose(fp);
135         unlink(fname);
136         return FALSE;
137     }
138     info_ptr=png_create_info_struct(png_ptr);
139     if(!info_ptr){
140         png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
141         fclose(fp);
142         unlink(fname);
143         return FALSE;
144     }
145
146     if(setjmp(png_jmpbuf(png_ptr))){
147         perror("Screenshot: setjmp");
148         png_destroy_write_struct(&png_ptr, &info_ptr);
149         fclose(fp);
150         unlink(fname);
151         return FALSE;
152     }
153
154     imgwidth=width;
155     imgheight=height;
156     if(Settings.StretchScreenshots==1){
157         if(width<=256 && height>SNES_HEIGHT_EXTENDED) imgwidth=width<<1;
158         if(width>256 && height<=SNES_HEIGHT_EXTENDED) imgheight=height<<1;
159     } else if(Settings.StretchScreenshots==2){
160         if(width<=256) imgwidth=width<<1;
161         if(height<=SNES_HEIGHT_EXTENDED) imgheight=height<<1;
162     }
163     
164     png_init_io(png_ptr, fp);
165     png_set_IHDR(png_ptr, info_ptr, imgwidth, imgheight, 8, 
166                  PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
167                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
168
169     /* 5 bits per color */
170     sig_bit.red=5;
171     sig_bit.green=5;
172     sig_bit.blue=5;
173     png_set_sBIT(png_ptr, info_ptr, &sig_bit);
174     png_set_shift(png_ptr, &sig_bit);
175
176     png_write_info(png_ptr, info_ptr);
177     
178     png_set_packing(png_ptr);
179
180     png_byte *row_pointer=new png_byte [png_get_rowbytes(png_ptr, info_ptr)];
181     uint8 *screen=GFX.Screen;
182     for(int y=0; y<height; y++, screen+=GFX.Pitch){
183         png_byte *rowpix = row_pointer;
184         for(int x=0; x<width; x++){
185             uint32 r, g, b;
186             DECOMPOSE_PIXEL((*(uint16 *)(screen+2*x)), r, g, b);
187             *(rowpix++) = r;
188             *(rowpix++) = g;
189             *(rowpix++) = b;
190             if (imgwidth!=width) {
191                 *(rowpix++) = r;
192                 *(rowpix++) = g;
193                 *(rowpix++) = b;
194             }
195         }
196         png_write_row(png_ptr, row_pointer);
197         if(imgheight!=height)
198             png_write_row(png_ptr, row_pointer);
199     }
200
201     delete [] row_pointer;
202         
203     png_write_end(png_ptr, info_ptr);
204     png_destroy_write_struct(&png_ptr, &info_ptr);
205
206     fclose(fp);
207     fprintf(stderr, "%s saved.\n", fname);
208     return TRUE;
209 #else
210     perror("Screenshot support not available (libpng was not found at build time)");
211         return FALSE;
212 #endif
213 }
214