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 | |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 96 | int color_type, bit_depth; |
| 97 | size_t width, height; |
| 98 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, |
| 99 | &color_type, NULL, NULL, NULL); |
| 100 | |
| 101 | int channels = png_get_channels(png_ptr, info_ptr); |
| 102 | |
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 | |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 112 | size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width; |
| 113 | size_t pixelSize = stride * height; |
| 114 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 115 | surface = malloc(sizeof(GGLSurface) + pixelSize); |
| 116 | if (surface == NULL) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 117 | result = -8; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 118 | goto exit; |
| 119 | } |
| 120 | unsigned char* pData = (unsigned char*) (surface + 1); |
| 121 | surface->version = sizeof(GGLSurface); |
| 122 | surface->width = width; |
| 123 | surface->height = height; |
| 124 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 125 | surface->data = pData; |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 126 | surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 : |
| 127 | ((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] | 128 | |
Doug Zongker | 68189f2 | 2011-03-04 16:28:48 -0800 | [diff] [blame] | 129 | int alpha = 0; |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 130 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
Doug Zongker | 68189f2 | 2011-03-04 16:28:48 -0800 | [diff] [blame] | 131 | png_set_palette_to_rgb(png_ptr); |
| 132 | } |
| 133 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 134 | png_set_tRNS_to_alpha(png_ptr); |
| 135 | alpha = 1; |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 136 | } |
Doug Zongker | 55a36ac | 2013-03-04 15:49:02 -0800 | [diff] [blame] | 137 | if (color_type == PNG_COLOR_TYPE_GRAY) { |
| 138 | alpha = 1; |
| 139 | } |
Doug Zongker | d93a254 | 2009-10-08 16:32:58 -0700 | [diff] [blame] | 140 | |
Edwin Vane | edc5d17 | 2012-07-27 11:32:23 -0400 | [diff] [blame] | 141 | unsigned int y; |
Doug Zongker | 68189f2 | 2011-03-04 16:28:48 -0800 | [diff] [blame] | 142 | if (channels == 3 || (channels == 1 && !alpha)) { |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 143 | for (y = 0; y < height; ++y) { |
| 144 | unsigned char* pRow = pData + y * stride; |
| 145 | png_read_row(png_ptr, pRow, NULL); |
| 146 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 147 | int x; |
| 148 | for(x = width - 1; x >= 0; x--) { |
| 149 | int sx = x * 3; |
| 150 | int dx = x * 4; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 151 | unsigned char r = pRow[sx]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | unsigned char g = pRow[sx + 1]; |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 153 | unsigned char b = pRow[sx + 2]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 154 | unsigned char a = 0xff; |
| 155 | pRow[dx ] = r; // r |
| 156 | pRow[dx + 1] = g; // g |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 157 | pRow[dx + 2] = b; // b |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 158 | pRow[dx + 3] = a; |
| 159 | } |
| 160 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 161 | } else { |
| 162 | for (y = 0; y < height; ++y) { |
| 163 | unsigned char* pRow = pData + y * stride; |
| 164 | png_read_row(png_ptr, pRow, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 167 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 168 | *pSurface = (gr_surface) surface; |
| 169 | |
| 170 | exit: |
Doug Zongker | 19faefa | 2009-03-27 17:06:24 -0700 | [diff] [blame] | 171 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 172 | |
| 173 | if (fp != NULL) { |
| 174 | fclose(fp); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | } |
| 176 | if (result < 0) { |
| 177 | if (surface) { |
| 178 | free(surface); |
| 179 | } |
| 180 | } |
| 181 | return result; |
| 182 | } |
| 183 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 184 | static int matches_locale(const char* loc) { |
| 185 | if (locale == NULL) return 0; |
| 186 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 187 | if (strcmp(loc, locale) == 0) return 1; |
| 188 | |
| 189 | // if loc does *not* have an underscore, and it matches the start |
| 190 | // of locale, and the next character in locale *is* an underscore, |
| 191 | // that's a match. For instance, loc == "en" matches locale == |
| 192 | // "en_US". |
| 193 | |
| 194 | int i; |
| 195 | for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); |
| 196 | if (loc[i] == '_') return 0; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 197 | |
| 198 | return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); |
| 199 | } |
| 200 | |
| 201 | int res_create_localized_surface(const char* name, gr_surface* pSurface) { |
| 202 | char resPath[256]; |
| 203 | GGLSurface* surface = NULL; |
| 204 | int result = 0; |
| 205 | unsigned char header[8]; |
| 206 | png_structp png_ptr = NULL; |
| 207 | png_infop info_ptr = NULL; |
| 208 | |
| 209 | *pSurface = NULL; |
| 210 | |
| 211 | snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); |
| 212 | resPath[sizeof(resPath)-1] = '\0'; |
| 213 | FILE* fp = fopen(resPath, "rb"); |
| 214 | if (fp == NULL) { |
| 215 | result = -1; |
| 216 | goto exit; |
| 217 | } |
| 218 | |
| 219 | size_t bytesRead = fread(header, 1, sizeof(header), fp); |
| 220 | if (bytesRead != sizeof(header)) { |
| 221 | result = -2; |
| 222 | goto exit; |
| 223 | } |
| 224 | |
| 225 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 226 | result = -3; |
| 227 | goto exit; |
| 228 | } |
| 229 | |
| 230 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 231 | if (!png_ptr) { |
| 232 | result = -4; |
| 233 | goto exit; |
| 234 | } |
| 235 | |
| 236 | info_ptr = png_create_info_struct(png_ptr); |
| 237 | if (!info_ptr) { |
| 238 | result = -5; |
| 239 | goto exit; |
| 240 | } |
| 241 | |
| 242 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 243 | result = -6; |
| 244 | goto exit; |
| 245 | } |
| 246 | |
| 247 | png_init_io(png_ptr, fp); |
| 248 | png_set_sig_bytes(png_ptr, sizeof(header)); |
| 249 | png_read_info(png_ptr, info_ptr); |
| 250 | |
John Reck | 94fd07b | 2013-08-26 16:45:33 -0700 | [diff] [blame] | 251 | int color_type, bit_depth; |
| 252 | size_t width, height; |
| 253 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, |
| 254 | &color_type, NULL, NULL, NULL); |
| 255 | int channels = png_get_channels(png_ptr, info_ptr); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 256 | |
| 257 | if (!(bit_depth == 8 && |
| 258 | (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) { |
| 259 | return -7; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 260 | goto exit; |
| 261 | } |
| 262 | |
| 263 | unsigned char* row = malloc(width); |
| 264 | int y; |
| 265 | for (y = 0; y < height; ++y) { |
| 266 | png_read_row(png_ptr, row, NULL); |
| 267 | int w = (row[1] << 8) | row[0]; |
| 268 | int h = (row[3] << 8) | row[2]; |
| 269 | int len = row[4]; |
| 270 | char* loc = row+5; |
| 271 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 272 | if (y+1+h >= height || matches_locale(loc)) { |
Doug Zongker | 52eeea4f | 2012-09-04 14:28:25 -0700 | [diff] [blame] | 273 | printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); |
| 274 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 275 | surface = malloc(sizeof(GGLSurface)); |
| 276 | if (surface == NULL) { |
| 277 | result = -8; |
| 278 | goto exit; |
| 279 | } |
| 280 | unsigned char* pData = malloc(w*h); |
| 281 | |
| 282 | surface->version = sizeof(GGLSurface); |
| 283 | surface->width = w; |
| 284 | surface->height = h; |
| 285 | surface->stride = w; /* Yes, pixels, not bytes */ |
| 286 | surface->data = pData; |
| 287 | surface->format = GGL_PIXEL_FORMAT_A_8; |
| 288 | |
| 289 | int i; |
| 290 | for (i = 0; i < h; ++i, ++y) { |
| 291 | png_read_row(png_ptr, row, NULL); |
| 292 | memcpy(pData + i*w, row, w); |
| 293 | } |
| 294 | |
| 295 | *pSurface = (gr_surface) surface; |
| 296 | break; |
| 297 | } else { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 298 | int i; |
| 299 | for (i = 0; i < h; ++i, ++y) { |
| 300 | png_read_row(png_ptr, row, NULL); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | exit: |
| 306 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 307 | |
| 308 | if (fp != NULL) { |
| 309 | fclose(fp); |
| 310 | } |
| 311 | if (result < 0) { |
| 312 | if (surface) { |
| 313 | free(surface); |
| 314 | } |
| 315 | } |
| 316 | return result; |
| 317 | } |
| 318 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 319 | void res_free_surface(gr_surface surface) { |
| 320 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 321 | if (pSurface) { |
| 322 | free(pSurface); |
| 323 | } |
| 324 | } |