bugfix
[picodrive.git] / platform / common / readpng.c
CommitLineData
a12e0116 1#include <stdio.h>
2#include <string.h>
3#include <png.h>
4#include "readpng.h"
e5f426aa 5#include "lprintf.h"
a12e0116 6
1820b5a7 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
7a87643e 15int readpng(void *dest, const char *fname, readpng_what what)
a12e0116 16{
17 FILE *fp;
18 png_structp png_ptr = NULL;
19 png_infop info_ptr = NULL;
20 png_bytepp row_ptr = NULL;
7a87643e 21 int ret = -1;
a12e0116 22
23 if (dest == NULL || fname == NULL)
24 {
7a87643e 25 return -1;
a12e0116 26 }
27
28 fp = fopen(fname, "rb");
29 if (fp == NULL)
30 {
e5f426aa 31 lprintf(__FILE__ ": failed to open: %s\n", fname);
7a87643e 32 return -1;
a12e0116 33 }
34
35 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
36 if (!png_ptr)
37 {
e5f426aa 38 lprintf(__FILE__ ": png_create_read_struct() failed\n");
a12e0116 39 fclose(fp);
7a87643e 40 return -1;
a12e0116 41 }
42
43 info_ptr = png_create_info_struct(png_ptr);
44 if (!info_ptr)
45 {
e5f426aa 46 lprintf(__FILE__ ": png_create_info_struct() failed\n");
a12e0116 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 {
e5f426aa 56 lprintf(__FILE__ ": png_get_rows() failed\n");
a12e0116 57 goto done;
58 }
59
e5f426aa 60 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
a12e0116 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 {
e5f426aa 70 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
a12e0116 71 break;
72 }
73 height = info_ptr->height;
1820b5a7 74 if (height > BG_HEIGHT) height = BG_HEIGHT;
a12e0116 75 width = info_ptr->width;
1820b5a7 76 if (width > BG_WIDTH) width = BG_WIDTH;
a12e0116 77
78 for (h = 0; h < height; h++)
79 {
80 unsigned char *src = row_ptr[h];
81 int len = width;
82 while (len--)
83 {
7d4906bf 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
a12e0116 89 src += 3;
90 }
1820b5a7 91 dst += BG_WIDTH - width;
a12e0116 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 {
e5f426aa 102 lprintf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n",
a12e0116 103 (int)info_ptr->width, (int)info_ptr->height);
104 break;
105 }
106 if (info_ptr->pixel_depth != 8)
107 {
e5f426aa 108 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
a12e0116 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 {
e5f426aa 132 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n",
a12e0116 133 (int)info_ptr->width, (int)info_ptr->height);
134 break;
135 }
136 if (info_ptr->pixel_depth != 8)
137 {
e5f426aa 138 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
a12e0116 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 }
7a87643e 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 }
a12e0116 176 }
177
178
7a87643e 179 ret = 0;
a12e0116 180done:
181 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
182 fclose(fp);
7a87643e 183 return ret;
a12e0116 184}
185
186