fbdev: make sure color format is set
[libpicofe.git] / common / readpng.c
CommitLineData
b7c7cd5d 1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
13059a60 11#include <stdio.h>
12#include <string.h>
13#include <png.h>
14#include "readpng.h"
c7a4ff64 15#include "lprintf.h"
13059a60 16
f6eaae4f 17int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req_h)
13059a60 18{
19 FILE *fp;
20 png_structp png_ptr = NULL;
21 png_infop info_ptr = NULL;
22 png_bytepp row_ptr = NULL;
ff63afa1 23 int ret = -1;
13059a60 24
25 if (dest == NULL || fname == NULL)
26 {
ff63afa1 27 return -1;
13059a60 28 }
29
30 fp = fopen(fname, "rb");
31 if (fp == NULL)
32 {
c7a4ff64 33 lprintf(__FILE__ ": failed to open: %s\n", fname);
ff63afa1 34 return -1;
13059a60 35 }
36
37 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
38 if (!png_ptr)
39 {
c7a4ff64 40 lprintf(__FILE__ ": png_create_read_struct() failed\n");
13059a60 41 fclose(fp);
ff63afa1 42 return -1;
13059a60 43 }
44
45 info_ptr = png_create_info_struct(png_ptr);
46 if (!info_ptr)
47 {
c7a4ff64 48 lprintf(__FILE__ ": png_create_info_struct() failed\n");
13059a60 49 goto done;
50 }
51
52 // Start reading
53 png_init_io(png_ptr, fp);
54 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_PACKING, NULL);
55 row_ptr = png_get_rows(png_ptr, info_ptr);
56 if (row_ptr == NULL)
57 {
c7a4ff64 58 lprintf(__FILE__ ": png_get_rows() failed\n");
13059a60 59 goto done;
60 }
61
c7a4ff64 62 // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
13059a60 63
64 switch (what)
65 {
66 case READPNG_BG:
67 {
68 int height, width, h;
69 unsigned short *dst = dest;
70 if (info_ptr->pixel_depth != 24)
71 {
c7a4ff64 72 lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
13059a60 73 break;
74 }
75 height = info_ptr->height;
f6eaae4f 76 if (height > req_h)
77 height = req_h;
13059a60 78 width = info_ptr->width;
f6eaae4f 79 if (width > req_w)
80 width = req_w;
13059a60 81
82 for (h = 0; h < height; h++)
83 {
84 unsigned char *src = row_ptr[h];
85 int len = width;
86 while (len--)
87 {
703e4c7b 88#ifdef PSP
89 *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR
90#else
91 *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB
92#endif
13059a60 93 src += 3;
94 }
f6eaae4f 95 dst += req_w - width;
13059a60 96 }
97 break;
98 }
99
100 case READPNG_FONT:
101 {
102 int x, y, x1, y1;
103 unsigned char *dst = dest;
f6eaae4f 104 if (info_ptr->width != req_w || info_ptr->height != req_h)
13059a60 105 {
f6eaae4f 106 lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
107 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
13059a60 108 break;
109 }
110 if (info_ptr->pixel_depth != 8)
111 {
c7a4ff64 112 lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
13059a60 113 break;
114 }
115 for (y = 0; y < 16; y++)
116 {
117 for (x = 0; x < 16; x++)
118 {
f6eaae4f 119 /* 16x16 grid of syms */
120 int sym_w = req_w / 16;
121 int sym_h = req_h / 16;
122 for (y1 = 0; y1 < sym_h; y1++)
13059a60 123 {
f6eaae4f 124 unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w;
125 for (x1 = sym_w/2; x1 > 0; x1--, src+=2)
13059a60 126 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
127 }
128 }
129 }
130 break;
131 }
132
133 case READPNG_SELECTOR:
134 {
135 int x1, y1;
136 unsigned char *dst = dest;
f6eaae4f 137 if (info_ptr->width != req_w || info_ptr->height != req_h)
13059a60 138 {
f6eaae4f 139 lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
140 (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
13059a60 141 break;
142 }
143 if (info_ptr->pixel_depth != 8)
144 {
c7a4ff64 145 lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
13059a60 146 break;
147 }
f6eaae4f 148 for (y1 = 0; y1 < req_h; y1++)
13059a60 149 {
150 unsigned char *src = row_ptr[y1];
f6eaae4f 151 for (x1 = req_w/2; x1 > 0; x1--, src+=2)
13059a60 152 *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4);
153 }
154 break;
155 }
ff63afa1 156
f6eaae4f 157 case READPNG_24:
ff63afa1 158 {
159 int height, width, h;
ff63afa1 160 unsigned char *dst = dest;
161 if (info_ptr->pixel_depth != 24)
162 {
163 lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
164 break;
165 }
166 height = info_ptr->height;
f6eaae4f 167 if (height > req_h)
168 height = req_h;
ff63afa1 169 width = info_ptr->width;
f6eaae4f 170 if (width > req_w)
171 width = req_w;
ff63afa1 172
173 for (h = 0; h < height; h++)
174 {
175 int len = width;
176 unsigned char *src = row_ptr[h];
f6eaae4f 177 dst += (req_w - width) * 3;
ff63afa1 178 for (len = width; len > 0; len--, dst+=3, src+=3)
179 dst[0] = src[2], dst[1] = src[1], dst[2] = src[0];
180 }
181 break;
182 }
13059a60 183 }
184
185
ff63afa1 186 ret = 0;
13059a60 187done:
188 png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL);
189 fclose(fp);
ff63afa1 190 return ret;
13059a60 191}
192
193