The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <linux/fb.h> |
| 28 | #include <linux/kd.h> |
| 29 | |
| 30 | #include <pixelflinger/pixelflinger.h> |
| 31 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 32 | #include <png.h> |
| 33 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include "minui.h" |
| 35 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 36 | #ifdef FASTMMI_FEATURE |
| 37 | char *locale = NULL; |
| 38 | #else |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 39 | extern char* locale; |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 40 | #endif |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 41 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 42 | // libpng gives "undefined reference to 'pow'" errors, and I have no |
| 43 | // idea how to convince the build system to link with -lm. We don't |
| 44 | // need this functionality (it's used for gamma adjustment) so provide |
| 45 | // a dummy implementation to satisfy the linker. |
| 46 | double pow(double x, double y) { |
Edwin Vane | edc5d17 | 2012-07-27 11:32:23 -0400 | [diff] [blame] | 47 | return x * y; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 48 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
| 50 | int res_create_surface(const char* name, gr_surface* pSurface) { |
| 51 | char resPath[256]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 52 | GGLSurface* surface = NULL; |
| 53 | int result = 0; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 54 | unsigned char header[8]; |
| 55 | png_structp png_ptr = NULL; |
| 56 | png_infop info_ptr = NULL; |
| 57 | |
Doug Zongker | 6809c51 | 2011-03-01 14:04:34 -0800 | [diff] [blame] | 58 | *pSurface = NULL; |
| 59 | |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 60 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | resPath[sizeof(resPath)-1] = '\0'; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 62 | FILE* fp = fopen(resPath, "rb"); |
| 63 | if (fp == NULL) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | result = -1; |
| 65 | goto exit; |
| 66 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 67 | |
| 68 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | if (bytesRead != sizeof(header)) { |
| 70 | result = -2; |
| 71 | goto exit; |
| 72 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 73 | |
| 74 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 75 | result = -3; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | goto exit; |
| 77 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 78 | |
| 79 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 80 | if (!png_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 81 | result = -4; |
| 82 | goto exit; |
| 83 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 84 | |
| 85 | info_ptr = png_create_info_struct(png_ptr); |
| 86 | if (!info_ptr) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 87 | result = -5; |
| 88 | goto exit; |
| 89 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 90 | |
| 91 | if (setjmp(png_jmpbuf(png_ptr))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 92 | result = -6; |
| 93 | goto exit; |
| 94 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 95 | |
| 96 | png_init_io(png_ptr, fp); |
| 97 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 98 | png_read_info(png_ptr, info_ptr); |
| 99 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 100 | int color_type = info_ptr->color_type; |
| 101 | int bit_depth = info_ptr->bit_depth; |
| 102 | int channels = info_ptr->channels; |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 103 | if (!(bit_depth == 8 && |
| 104 | ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) || |
| 105 | (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) || |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 106 | (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE || |
| 107 | color_type == PNG_COLOR_TYPE_GRAY))))) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 108 | return -7; |
| 109 | goto exit; |
| 110 | } |
| 111 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 112 | size_t width = info_ptr->width; |
| 113 | size_t height = info_ptr->height; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 114 | size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width; |
| 115 | size_t pixelSize = stride * height; |
| 116 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 117 | surface = malloc(sizeof(GGLSurface) + pixelSize); |
| 118 | if (surface == NULL) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 119 | result = -8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 120 | goto exit; |
| 121 | } |
| 122 | unsigned char* pData = (unsigned char*) (surface + 1); |
| 123 | surface->version = sizeof(GGLSurface); |
| 124 | surface->width = width; |
| 125 | surface->height = height; |
| 126 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 127 | surface->data = pData; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 128 | surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 : |
| 129 | ((color_type == PNG_COLOR_TYPE_PALETTE ? GGL_PIXEL_FORMAT_RGBA_8888 : GGL_PIXEL_FORMAT_L_8)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | |
Doug Zongker | 68189f2 | 2011-03-04 16:28:48 -0800 | [diff] [blame] | 131 | int alpha = 0; |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 132 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
Doug Zongker | 68189f2 | 2011-03-04 16:28:48 -0800 | [diff] [blame] | 133 | png_set_palette_to_rgb(png_ptr); |
| 134 | } |
| 135 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 136 | png_set_tRNS_to_alpha(png_ptr); |
| 137 | alpha = 1; |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 138 | } |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 139 | if (color_type == PNG_COLOR_TYPE_GRAY) { |
| 140 | alpha = 1; |
| 141 | } |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 142 | |
Edwin Vane | edc5d17 | 2012-07-27 11:32:23 -0400 | [diff] [blame] | 143 | unsigned int y; |
Doug Zongker | 68189f2 | 2011-03-04 16:28:48 -0800 | [diff] [blame] | 144 | if (channels == 3 || (channels == 1 && !alpha)) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 145 | for (y = 0; y < height; ++y) { |
| 146 | unsigned char* pRow = pData + y * stride; |
| 147 | png_read_row(png_ptr, pRow, NULL); |
| 148 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 149 | int x; |
| 150 | for(x = width - 1; x >= 0; x--) { |
| 151 | int sx = x * 3; |
| 152 | int dx = x * 4; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 153 | unsigned char r = pRow[sx]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 154 | unsigned char g = pRow[sx + 1]; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 155 | unsigned char b = pRow[sx + 2]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 156 | unsigned char a = 0xff; |
| 157 | pRow[dx ] = r; // r |
| 158 | pRow[dx + 1] = g; // g |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 159 | pRow[dx + 2] = b; // b |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 160 | pRow[dx + 3] = a; |
| 161 | } |
| 162 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 163 | } else { |
| 164 | for (y = 0; y < height; ++y) { |
| 165 | unsigned char* pRow = pData + y * stride; |
| 166 | png_read_row(png_ptr, pRow, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 169 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | *pSurface = (gr_surface) surface; |
| 171 | |
| 172 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 173 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 174 | |
| 175 | if (fp != NULL) { |
| 176 | fclose(fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 177 | } |
| 178 | if (result < 0) { |
| 179 | if (surface) { |
| 180 | free(surface); |
| 181 | } |
| 182 | } |
| 183 | return result; |
| 184 | } |
| 185 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 186 | static int matches_locale(const char* loc) { |
| 187 | if (locale == NULL) return 0; |
| 188 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 189 | if (strcmp(loc, locale) == 0) return 1; |
| 190 | |
| 191 | // if loc does *not* have an underscore, and it matches the start |
| 192 | // of locale, and the next character in locale *is* an underscore, |
| 193 | // that's a match. For instance, loc == "en" matches locale == |
| 194 | // "en_US". |
| 195 | |
| 196 | int i; |
| 197 | for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); |
| 198 | if (loc[i] == '_') return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 199 | |
| 200 | return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); |
| 201 | } |
| 202 | |
| 203 | int res_create_localized_surface(const char* name, gr_surface* pSurface) { |
| 204 | char resPath[256]; |
| 205 | GGLSurface* surface = NULL; |
| 206 | int result = 0; |
| 207 | unsigned char header[8]; |
| 208 | png_structp png_ptr = NULL; |
| 209 | png_infop info_ptr = NULL; |
| 210 | |
| 211 | *pSurface = NULL; |
| 212 | |
| 213 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
| 214 | resPath[sizeof(resPath)-1] = '\0'; |
| 215 | FILE* fp = fopen(resPath, "rb"); |
| 216 | if (fp == NULL) { |
| 217 | result = -1; |
| 218 | goto exit; |
| 219 | } |
| 220 | |
| 221 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
| 222 | if (bytesRead != sizeof(header)) { |
| 223 | result = -2; |
| 224 | goto exit; |
| 225 | } |
| 226 | |
| 227 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 228 | result = -3; |
| 229 | goto exit; |
| 230 | } |
| 231 | |
| 232 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 233 | if (!png_ptr) { |
| 234 | result = -4; |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | info_ptr = png_create_info_struct(png_ptr); |
| 239 | if (!info_ptr) { |
| 240 | result = -5; |
| 241 | goto exit; |
| 242 | } |
| 243 | |
| 244 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 245 | result = -6; |
| 246 | goto exit; |
| 247 | } |
| 248 | |
| 249 | png_init_io(png_ptr, fp); |
| 250 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 251 | png_read_info(png_ptr, info_ptr); |
| 252 | |
Dees Troy | 62b75ab | 2014-05-02 13:20:33 +0000 | [diff] [blame] | 253 | size_t width = info_ptr->width; |
| 254 | size_t height = info_ptr->height; |
| 255 | size_t stride = 4 * width; |
| 256 | |
| 257 | int color_type = info_ptr->color_type; |
| 258 | int bit_depth = info_ptr->bit_depth; |
| 259 | int channels = info_ptr->channels; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 260 | |
| 261 | if (!(bit_depth == 8 && |
| 262 | (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) { |
| 263 | return -7; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 264 | goto exit; |
| 265 | } |
| 266 | |
| 267 | unsigned char* row = malloc(width); |
| 268 | int y; |
| 269 | for (y = 0; y < height; ++y) { |
| 270 | png_read_row(png_ptr, row, NULL); |
| 271 | int w = (row[1] << 8) | row[0]; |
| 272 | int h = (row[3] << 8) | row[2]; |
| 273 | int len = row[4]; |
| 274 | char* loc = row+5; |
| 275 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 276 | if (y+1+h >= height || matches_locale(loc)) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 277 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 278 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 279 | surface = malloc(sizeof(GGLSurface)); |
| 280 | if (surface == NULL) { |
| 281 | result = -8; |
| 282 | goto exit; |
| 283 | } |
| 284 | unsigned char* pData = malloc(w*h); |
| 285 | |
| 286 | surface->version = sizeof(GGLSurface); |
| 287 | surface->width = w; |
| 288 | surface->height = h; |
| 289 | surface->stride = w; /* Yes, pixels, not bytes */ |
| 290 | surface->data = pData; |
| 291 | surface->format = GGL_PIXEL_FORMAT_A_8; |
| 292 | |
| 293 | int i; |
| 294 | for (i = 0; i < h; ++i, ++y) { |
| 295 | png_read_row(png_ptr, row, NULL); |
| 296 | memcpy(pData + i*w, row, w); |
| 297 | } |
| 298 | |
| 299 | *pSurface = (gr_surface) surface; |
| 300 | break; |
| 301 | } else { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 302 | int i; |
| 303 | for (i = 0; i < h; ++i, ++y) { |
| 304 | png_read_row(png_ptr, row, NULL); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | exit: |
| 310 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 311 | |
| 312 | if (fp != NULL) { |
| 313 | fclose(fp); |
| 314 | } |
| 315 | if (result < 0) { |
| 316 | if (surface) { |
| 317 | free(surface); |
| 318 | } |
| 319 | } |
| 320 | return result; |
| 321 | } |
| 322 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 323 | void res_free_surface(gr_surface surface) { |
| 324 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 325 | if (pSurface) { |
| 326 | free(pSurface); |
| 327 | } |
| 328 | } |