step-frame added to debug
[picodrive.git] / platform / common / readpng.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <string.h>
3#include <png.h>
4#include "readpng.h"
5#include "lprintf.h"
6
7#ifdef PSP
8#define BG_WIDTH 480
9#define BG_HEIGHT 272
10#else
11#define BG_WIDTH 320
12#define BG_HEIGHT 240
13#endif
14
15int readpng(void *dest, const char *fname, readpng_what what)
16{
17 FILE *fp;
18 png_structp png_ptr = NULL;
19 png_infop info_ptr = NULL;
20 png_bytepp row_ptr = NULL;
21 int ret = -1;
22
23 if (dest == NULL || fname == NULL)
24 {
25 return -1;
26 }
27
28 fp = fopen(fname, "rb");
29 if (fp == NULL)
30 {
31 lprintf(__FILE__ ": failed to open: %s\n", fname);
32 return -1;
33 }
34
35 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
36 if (!png_ptr)
37 {
38 lprintf(__FILE__ ": png_create_read_struct() failed\n");
39 fclose(fp);
40 return -1;
41 }
42
43 info_ptr = png_create_info_struct(png_ptr);
44 if (!info_ptr)
45 {
46 lprintf(__FILE__ ": png_create_info_struct() failed\n");
47 goto done;
48 }
49
50 // Start reading
51 png_init_io(png_ptr, fp);
52 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
53 row_ptr = png_get_rows(png_ptr, info_ptr);
54 if (row_ptr == NULL)
55 {
56 lprintf(__FILE__ ": png_get_rows() failed\n");
57 goto done;
58 }
59
60 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
61
62 switch (what)
63 {
64 case READPNG_BG:
65 {
66 int height, width, h;
67 unsigned short *dst = dest;
68 if (info_ptr->pixel_depth != 24)
69 {
70 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
71 break;
72 }
73 height = info_ptr->height;
74 if (height > BG_HEIGHT) height = BG_HEIGHT;
75 width = info_ptr->width;
76 if (width > BG_WIDTH) width = BG_WIDTH;
77
78 for (h = 0; h < height; h++)
79 {
80 unsigned char *src = row_ptr[h];
81 int len = width;
82 while (len--)
83 {
84#ifdef PSP
85 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
86#else
87 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
88#endif
89 src += 3;
90 }
91 dst += BG_WIDTH - width;
92 }
93 break;
94 }
95
96 case READPNG_FONT:
97 {
98 int x, y, x1, y1;
99 unsigned char *dst = dest;
100 if (info_ptr->width != 128 || info_ptr->height != 160)
101 {
102 lprintf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n",
103 (int)info_ptr->width, (int)info_ptr->height);
104 break;
105 }
106 if (info_ptr->pixel_depth != 8)
107 {
108 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
109 break;
110 }
111 for (y = 0; y < 16; y++)
112 {
113 for (x = 0; x < 16; x++)
114 {
115 for (y1 = 0; y1 < 10; y1++)
116 {
117 unsigned char *src = row_ptr[y*10 + y1] + x*8;
118 for (x1 = 8/2; x1 > 0; x1--, src+=2)
119 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
120 }
121 }
122 }
123 break;
124 }
125
126 case READPNG_SELECTOR:
127 {
128 int x1, y1;
129 unsigned char *dst = dest;
130 if (info_ptr->width != 8 || info_ptr->height != 10)
131 {
132 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n",
133 (int)info_ptr->width, (int)info_ptr->height);
134 break;
135 }
136 if (info_ptr->pixel_depth != 8)
137 {
138 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
139 break;
140 }
141 for (y1 = 0; y1 < 10; y1++)
142 {
143 unsigned char *src = row_ptr[y1];
144 for (x1 = 8/2; x1 > 0; x1--, src+=2)
145 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
146 }
147 break;
148 }
149
150 case READPNG_320_24:
151 case READPNG_480_24:
152 {
153 int height, width, h;
154 int needw = (what == READPNG_480_24) ? 480 : 320;
155 unsigned char *dst = dest;
156 if (info_ptr->pixel_depth != 24)
157 {
158 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
159 break;
160 }
161 height = info_ptr->height;
162 if (height > 240) height = 240;
163 width = info_ptr->width;
164 if (width > needw) width = needw;
165
166 for (h = 0; h < height; h++)
167 {
168 int len = width;
169 unsigned char *src = row_ptr[h];
170 dst += (needw - width) * 3;
171 for (len = width; len > 0; len--, dst+=3, src+=3)
172 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
173 }
174 break;
175 }
176 }
177
178
179 ret = 0;
180done:
181 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
182 fclose(fp);
183 return ret;
184}
185
186