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