Core commit. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-core / src / osd / screenshot.cpp
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - screenshot.c                                            *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2008 Richard42                                          *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <ctype.h>
26
27 #ifdef PANDORA
28 #include <SDL_opengles2.h>
29 #else
30 #include <SDL_opengl.h>
31 #endif
32 #include <SDL.h>
33 #include <png.h>
34
35 #include "osd.h"
36
37 extern "C" {
38 #define M64P_CORE_PROTOTYPES 1
39 #include "api/m64p_types.h"
40 #include "api/callbacks.h"
41 #include "api/m64p_config.h"
42 #include "api/config.h"
43 #include "main/main.h"
44 #include "main/util.h"
45 #include "main/rom.h"
46 #include "osal/files.h"
47 #include "osal/preproc.h"
48 #include "plugin/plugin.h"
49 }
50
51 /*********************************************************************************************************
52 * PNG support functions for writing screenshot files
53 */
54
55 static void mupen_png_error(png_structp png_write, const char *message)
56 {
57     DebugMessage(M64MSG_ERROR, "PNG Error: %s", message);
58 }
59
60 static void mupen_png_warn(png_structp png_write, const char *message)
61 {
62     DebugMessage(M64MSG_WARNING, "PNG Warning: %s", message);
63 }
64
65 static void user_write_data(png_structp png_write, png_bytep data, png_size_t length)
66 {
67     FILE *fPtr = (FILE *) png_get_io_ptr(png_write);
68     if (fwrite(data, 1, length, fPtr) != length)
69         DebugMessage(M64MSG_ERROR, "Failed to write %zi bytes to screenshot file.", length);
70 }
71
72 static void user_flush_data(png_structp png_write)
73 {
74     FILE *fPtr = (FILE *) png_get_io_ptr(png_write);
75     fflush(fPtr);
76 }
77
78 /*********************************************************************************************************
79 * Other Local (static) functions
80 */
81
82 static int SaveRGBBufferToFile(const char *filename, const unsigned char *buf, int width, int height, int pitch)
83 {
84     int i;
85
86     // allocate PNG structures
87     png_structp png_write = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, mupen_png_error, mupen_png_warn);
88     if (!png_write)
89     {
90         DebugMessage(M64MSG_ERROR, "Error creating PNG write struct.");
91         return 1;
92     }
93     png_infop png_info = png_create_info_struct(png_write);
94     if (!png_info)
95     {
96         png_destroy_write_struct(&png_write, (png_infopp)NULL);
97         DebugMessage(M64MSG_ERROR, "Error creating PNG info struct.");
98         return 2;
99     }
100     // Set the jumpback
101     if (setjmp(png_jmpbuf(png_write)))
102     {
103         png_destroy_write_struct(&png_write, &png_info);
104         DebugMessage(M64MSG_ERROR, "Error calling setjmp()");
105         return 3;
106     }
107     // open the file to write
108     FILE *savefile = fopen(filename, "wb");
109     if (savefile == NULL)
110     {
111         DebugMessage(M64MSG_ERROR, "Error opening '%s' to save screenshot.", filename);
112         return 4;
113     }
114     // set function pointers in the PNG library, for write callbacks
115     png_set_write_fn(png_write, (png_voidp) savefile, user_write_data, user_flush_data);
116     // set the info
117     png_set_IHDR(png_write, png_info, width, height, 8, PNG_COLOR_TYPE_RGB,
118                  PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
119     // allocate row pointers and scale each row to 24-bit color
120     png_byte **row_pointers;
121     row_pointers = (png_byte **) malloc(height * sizeof(png_bytep));
122     for (i = 0; i < height; i++)
123     {
124         row_pointers[i] = (png_byte *) (buf + (height - 1 - i) * pitch);
125     }
126     // set the row pointers
127     png_set_rows(png_write, png_info, row_pointers);
128     // write the picture to disk
129     png_write_png(png_write, png_info, 0, NULL);
130     // free memory
131     free(row_pointers);
132     png_destroy_write_struct(&png_write, &png_info);
133     // close file
134     fclose(savefile);
135     // all done
136     return 0;
137 }
138
139 static int CurrentShotIndex;
140
141 static char *GetNextScreenshotPath(void)
142 {
143     char *ScreenshotPath;
144     char ScreenshotFileName[20 + 8 + 1];
145
146     // generate the base name of the screenshot
147     // add the ROM name, convert to lowercase, convert spaces to underscores
148     strcpy(ScreenshotFileName, ROM_PARAMS.headername);
149     for (char *pch = ScreenshotFileName; *pch != '\0'; pch++)
150         *pch = (*pch == ' ') ? '_' : tolower(*pch);
151     strcat(ScreenshotFileName, "-###.png");
152     
153     // add the base path to the screenshot file name
154     const char *SshotDir = ConfigGetParamString(g_CoreConfig, "ScreenshotPath");
155     if (SshotDir == NULL || *SshotDir == '\0')
156     {
157         // note the trick to avoid an allocation. we add a NUL character
158         // instead of the separator, call mkdir, then add the separator
159         ScreenshotPath = formatstr("%sscreenshot%c%s", ConfigGetUserDataPath(), '\0', ScreenshotFileName);
160         if (ScreenshotPath == NULL)
161             return NULL;
162         osal_mkdirp(ScreenshotPath, 0700);
163         ScreenshotPath[strlen(ScreenshotPath)] = OSAL_DIR_SEPARATORS[0];
164     }
165     else
166     {
167         ScreenshotPath = combinepath(SshotDir, ScreenshotFileName);
168         if (ScreenshotPath == NULL)
169             return NULL;
170     }
171
172     // patch the number part of the name (the '###' part) until we find a free spot
173     char *NumberPtr = ScreenshotPath + strlen(ScreenshotPath) - 7;
174     for (; CurrentShotIndex < 1000; CurrentShotIndex++)
175     {
176         sprintf(NumberPtr, "%03i.png", CurrentShotIndex);
177         FILE *pFile = fopen(ScreenshotPath, "r");
178         if (pFile == NULL)
179             break;
180         fclose(pFile);
181     }
182
183     if (CurrentShotIndex >= 1000)
184     {
185         DebugMessage(M64MSG_ERROR, "Can't save screenshot; folder already contains 1000 screenshots for this ROM");
186         return NULL;
187     }
188     CurrentShotIndex++;
189
190     return ScreenshotPath;
191 }
192
193 /*********************************************************************************************************
194 * Global screenshot functions
195 */
196
197 extern "C" void ScreenshotRomOpen(void)
198 {
199     CurrentShotIndex = 0;
200 }
201
202 extern "C" void TakeScreenshot(int iFrameNumber)
203 {
204     char *filename;
205
206     // look for an unused screenshot filename
207     filename = GetNextScreenshotPath();
208     if (filename == NULL)
209         return;
210
211     // get the width and height
212     int width = 640;
213     int height = 480;
214     gfx.readScreen(NULL, &width, &height, 0);
215
216     // allocate memory for the image
217     unsigned char *pucFrame = (unsigned char *) malloc(width * height * 3);
218     if (pucFrame == NULL)
219     {
220         free(filename);
221         return;
222     }
223
224     // grab the back image from OpenGL by calling the video plugin
225     gfx.readScreen(pucFrame, &width, &height, 0);
226
227     // write the image to a PNG
228     SaveRGBBufferToFile(filename, pucFrame, width, height, width * 3);
229     // free the memory
230     free(pucFrame);
231     free(filename);
232     // print message -- this allows developers to capture frames and use them in the regression test
233     main_message(M64MSG_INFO, OSD_BOTTOM_LEFT, "Captured screenshot for frame %i.", iFrameNumber);
234 }
235