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