-
Notifications
You must be signed in to change notification settings - Fork 0
/
psp_png.c
248 lines (231 loc) · 7.3 KB
/
psp_png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* main.c - Screenshot and load image sample for PSP.
*
* Copyright (c) 2005 Frank Buss <[email protected]> (aka Shine)
*/
#include <stdio.h>
#include <stdlib.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <png.h>
/* Define the module info section */
PSP_MODULE_INFO("SCREENSHOT", 0, 1, 1);
/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
void fillLine(u32* vram32, int bufferwidth, int y0, u8 r, u8 g, u8 b)
{
// assume PSP_DISPLAY_PIXEL_FORMAT_8888
int x, y;
for (x = 0; x < 255; x++) {
u32 color = r * x / 256;
color |= (g * x / 256) << 8;
color |= (b * x / 256) << 16;
for (y = 0; y < 8; y++) vram32[x + 100 + (y + y0) * bufferwidth] = color;
}
}
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272
/* Save current visible screen as PNG */
void screenshot(const char* filename)
{
u32* vram32;
u16* vram16;
int bufferwidth;
int pixelformat;
int unknown;
int i, x, y;
png_structp png_ptr;
png_infop info_ptr;
FILE* fp;
u8* line;
fp = fopen(filename, "wb");
if (!fp) return;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) return;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
return;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, SCREEN_WIDTH, SCREEN_HEIGHT,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
line = (u8*) malloc(SCREEN_WIDTH * 3);
sceDisplayWaitVblankStart(); // if framebuf was set with PSP_DISPLAY_SETBUF_NEXTFRAME, wait until it is changed
sceDisplayGetFrameBuf((void**)&vram32, &bufferwidth, &pixelformat, &unknown);
vram16 = (u16*) vram32;
for (y = 0; y < SCREEN_HEIGHT; y++) {
for (i = 0, x = 0; x < SCREEN_WIDTH; x++) {
u32 color = 0;
u8 r = 0, g = 0, b = 0;
switch (pixelformat) {
case PSP_DISPLAY_PIXEL_FORMAT_565:
color = vram16[x + y * bufferwidth];
r = (color & 0x1f) << 3;
g = ((color >> 5) & 0x3f) << 2 ;
b = ((color >> 11) & 0x1f) << 3 ;
break;
case PSP_DISPLAY_PIXEL_FORMAT_5551:
color = vram16[x + y * bufferwidth];
r = (color & 0x1f) << 3;
g = ((color >> 5) & 0x1f) << 3 ;
b = ((color >> 10) & 0x1f) << 3 ;
break;
case PSP_DISPLAY_PIXEL_FORMAT_4444:
color = vram16[x + y * bufferwidth];
r = (color & 0xf) << 4;
g = ((color >> 4) & 0xf) << 4 ;
b = ((color >> 8) & 0xf) << 4 ;
break;
case PSP_DISPLAY_PIXEL_FORMAT_8888:
color = vram32[x + y * bufferwidth];
r = color & 0xff;
g = (color >> 8) & 0xff;
b = (color >> 16) & 0xff;
break;
}
line[i++] = r;
line[i++] = g;
line[i++] = b;
}
png_write_row(png_ptr, line);
}
free(line);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(fp);
}
void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
{
// ignore PNG warnings
}
/* Load an image and show it to screen */
void showImage(const char* filename)
{
u32* vram32;
u16* vram16;
int bufferwidth;
int pixelformat;
int unknown;
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, x, y;
u32* line;
FILE *fp;
if ((fp = fopen(filename, "rb")) == NULL) return;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return;
}
png_set_error_fn(png_ptr, (png_voidp) NULL, (png_error_ptr) NULL, user_warning_fn);
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
png_set_strip_16(png_ptr);
png_set_packing(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
line = (u32*) malloc(width * 4);
if (!line) {
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return;
}
sceDisplayWaitVblankStart(); // if framebuf was set with PSP_DISPLAY_SETBUF_NEXTFRAME, wait until it is changed
sceDisplayGetFrameBuf((void**)&vram32, &bufferwidth, &pixelformat, &unknown);
vram16 = (u16*) vram32;
for (y = 0; y < height; y++) {
png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
for (x = 0; x < width; x++) {
u32 color32 = line[x];
u16 color16;
int r = color32 & 0xff;
int g = (color32 >> 8) & 0xff;
int b = (color32 >> 16) & 0xff;
switch (pixelformat) {
case PSP_DISPLAY_PIXEL_FORMAT_565:
color16 = (r >> 3) | ((g >> 2) << 5) | ((b >> 3) << 11);
vram16[x + y * bufferwidth] = color16;
break;
case PSP_DISPLAY_PIXEL_FORMAT_5551:
color16 = (r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10);
vram16[x + y * bufferwidth] = color16;
break;
case PSP_DISPLAY_PIXEL_FORMAT_4444:
color16 = (r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8);
vram16[x + y * bufferwidth] = color16;
break;
case PSP_DISPLAY_PIXEL_FORMAT_8888:
color32 = r | (g << 8) | (b << 16);
vram32[x + y * bufferwidth] = color32;
break;
}
}
}
free(line);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
}
void waitOneSecond()
{
int x;
for (x = 0; x < 60; x++) sceDisplayWaitVblankStart();
}
int main(void)
{
u16* vram16;
u32* vram32;
int bufferwidth;
int unusedPixelformat;
int unknown;
pspDebugScreenInit();
/* show some test output */
pspDebugScreenPrintf("red\n\ngreen\n\nblue\n\nwhite");
sceDisplayWaitVblankStart(); // if framebuf was set with PSP_DISPLAY_SETBUF_NEXTFRAME, wait until it is changed
sceDisplayGetFrameBuf((void**)&vram32, &bufferwidth, &unusedPixelformat, &unknown);
vram16 = (u16*) vram32;
fillLine(vram32, bufferwidth, 0, 255, 0, 0);
fillLine(vram32, bufferwidth, 16, 0, 255, 0);
fillLine(vram32, bufferwidth, 32, 0, 0, 255);
fillLine(vram32, bufferwidth, 48, 255, 255, 255);
/* save current screen */
screenshot("screenshot8888.png");
waitOneSecond();
/* load for testing it */
showImage("screenshot8888.png");
waitOneSecond();
/* switch mode, load original and save in new mode */
#define TEST_FORMAT(pixelformat, screenshotName) \
sceDisplayWaitVblankStart(); \
sceDisplaySetFrameBuf(vram32, bufferwidth, pixelformat, 1); \
sceDisplayWaitVblankStart(); \
showImage("screenshot8888.png"); \
screenshot(screenshotName); \
waitOneSecond();
TEST_FORMAT(PSP_DISPLAY_PIXEL_FORMAT_565, "screenshot565.png");
TEST_FORMAT(PSP_DISPLAY_PIXEL_FORMAT_5551, "screenshot5551.png");
TEST_FORMAT(PSP_DISPLAY_PIXEL_FORMAT_4444, "screenshot4444.png");
sceKernelExitGame();
return 0;
}