Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [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> |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 18 | #include <string.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include <linux/fb.h> |
| 29 | #include <linux/kd.h> |
| 30 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | #include <png.h> |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 32 | |
| 33 | #include <pixelflinger/pixelflinger.h> |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 34 | #ifdef TW_INCLUDE_JPEG |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 35 | extern "C" { |
Dees_Troy | 930bf01 | 2013-08-10 22:19:03 +0000 | [diff] [blame] | 36 | #include "jpeglib.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | } |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 38 | #endif |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 39 | #include "minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 41 | #define SURFACE_DATA_ALIGNMENT 8 |
| 42 | |
| 43 | static GGLSurface* malloc_surface(size_t data_size) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 44 | size_t size = sizeof(GGLSurface) + data_size + SURFACE_DATA_ALIGNMENT; |
| 45 | unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size)); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 46 | if (temp == NULL) return NULL; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 47 | GGLSurface* surface = reinterpret_cast<GGLSurface*>(temp); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 48 | surface->data = temp + sizeof(GGLSurface) + |
| 49 | (SURFACE_DATA_ALIGNMENT - (sizeof(GGLSurface) % SURFACE_DATA_ALIGNMENT)); |
| 50 | return surface; |
| 51 | } |
| 52 | |
| 53 | static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 54 | png_uint_32* width, png_uint_32* height, png_byte* channels, FILE** fpp) { |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 55 | char resPath[256]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | unsigned char header[8]; |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 57 | int result = 0; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 58 | int color_type, bit_depth; |
| 59 | size_t bytesRead; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 60 | |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 61 | snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s.png", name); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 62 | resPath[sizeof(resPath)-1] = '\0'; |
| 63 | FILE* fp = fopen(resPath, "rb"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | if (fp == NULL) { |
Ethan Yonker | ac21cb5 | 2014-12-11 18:05:59 -0600 | [diff] [blame] | 65 | fp = fopen(name, "rb"); |
| 66 | if (fp == NULL) { |
| 67 | result = -1; |
| 68 | goto exit; |
| 69 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 72 | bytesRead = fread(header, 1, sizeof(header), fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | if (bytesRead != sizeof(header)) { |
| 74 | result = -2; |
| 75 | goto exit; |
| 76 | } |
| 77 | |
| 78 | if (png_sig_cmp(header, 0, sizeof(header))) { |
| 79 | result = -3; |
| 80 | goto exit; |
| 81 | } |
| 82 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 83 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 84 | if (!*png_ptr) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 85 | result = -4; |
| 86 | goto exit; |
| 87 | } |
| 88 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 89 | *info_ptr = png_create_info_struct(*png_ptr); |
| 90 | if (!*info_ptr) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | result = -5; |
| 92 | goto exit; |
| 93 | } |
| 94 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 95 | if (setjmp(png_jmpbuf(*png_ptr))) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 96 | result = -6; |
| 97 | goto exit; |
| 98 | } |
| 99 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 100 | png_init_io(*png_ptr, fp); |
| 101 | png_set_sig_bytes(*png_ptr, sizeof(header)); |
| 102 | png_read_info(*png_ptr, *info_ptr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 104 | png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 105 | &color_type, NULL, NULL, NULL); |
| 106 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 107 | *channels = png_get_channels(*png_ptr, *info_ptr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 109 | if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 110 | // 8-bit RGB images: great, nothing to do. |
| 111 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { |
| 112 | // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. |
| 113 | png_set_expand_gray_1_2_4_to_8(*png_ptr); |
| 114 | } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { |
| 115 | // paletted images: expand to 8-bit RGB. Note that we DON'T |
| 116 | // currently expand the tRNS chunk (if any) to an alpha |
| 117 | // channel, because minui doesn't support alpha channels in |
| 118 | // general. |
| 119 | png_set_palette_to_rgb(*png_ptr); |
| 120 | *channels = 3; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 121 | } else if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 122 | png_set_palette_to_rgb(*png_ptr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 123 | } |
| 124 | |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 125 | *fpp = fp; |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 126 | return result; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 127 | |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 128 | exit: |
| 129 | if (result < 0) { |
| 130 | png_destroy_read_struct(png_ptr, info_ptr, NULL); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 132 | if (fp != NULL) { |
| 133 | fclose(fp); |
| 134 | } |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 135 | |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | // "display" surfaces are transformed into the framebuffer's required |
| 140 | // pixel format (currently only RGBX is supported) at load time, so |
| 141 | // gr_blit() can be nothing more than a memcpy() for each row. The |
| 142 | // next two functions are the only ones that know anything about the |
| 143 | // framebuffer pixel format; they need to be modified if the |
| 144 | // framebuffer format changes (but nothing else should). |
| 145 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 146 | // Allocate and return a GRSurface* sufficient for storing an image of |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 147 | // the indicated size in the framebuffer pixel format. |
| 148 | static GGLSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 149 | GGLSurface* surface = malloc_surface(width * height * 4); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 150 | if (surface == NULL) return NULL; |
| 151 | |
| 152 | surface->version = sizeof(GGLSurface); |
| 153 | surface->width = width; |
| 154 | surface->height = height; |
| 155 | surface->stride = width; |
| 156 | |
| 157 | return surface; |
| 158 | } |
| 159 | |
| 160 | // Copy 'input_row' to 'output_row', transforming it to the |
| 161 | // framebuffer pixel format. The input format depends on the value of |
| 162 | // 'channels': |
| 163 | // |
| 164 | // 1 - input is 8-bit grayscale |
| 165 | // 3 - input is 24-bit RGB |
| 166 | // 4 - input is 32-bit RGBA/RGBX |
| 167 | // |
| 168 | // 'width' is the number of pixels in the row. |
| 169 | static void transform_rgb_to_draw(unsigned char* input_row, |
| 170 | unsigned char* output_row, |
| 171 | int channels, int width) { |
| 172 | int x; |
| 173 | unsigned char* ip = input_row; |
| 174 | unsigned char* op = output_row; |
| 175 | |
| 176 | switch (channels) { |
| 177 | case 1: |
| 178 | // expand gray level to RGBX |
| 179 | for (x = 0; x < width; ++x) { |
| 180 | *op++ = *ip; |
| 181 | *op++ = *ip; |
| 182 | *op++ = *ip; |
| 183 | *op++ = 0xff; |
| 184 | ip++; |
| 185 | } |
| 186 | break; |
| 187 | |
| 188 | case 3: |
| 189 | // expand RGBA to RGBX |
| 190 | for (x = 0; x < width; ++x) { |
| 191 | *op++ = *ip++; |
| 192 | *op++ = *ip++; |
| 193 | *op++ = *ip++; |
| 194 | *op++ = 0xff; |
| 195 | } |
| 196 | break; |
| 197 | |
| 198 | case 4: |
| 199 | // copy RGBA to RGBX |
| 200 | memcpy(output_row, input_row, width*4); |
| 201 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | } |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | int res_create_surface_png(const char* name, gr_surface* pSurface) { |
| 206 | GGLSurface* surface = NULL; |
| 207 | int result = 0; |
| 208 | png_structp png_ptr = NULL; |
| 209 | png_infop info_ptr = NULL; |
| 210 | png_uint_32 width, height; |
| 211 | png_byte channels; |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 212 | FILE* fp; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 213 | unsigned char* p_row; |
| 214 | unsigned int y; |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 215 | |
| 216 | *pSurface = NULL; |
| 217 | |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 218 | result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels, &fp); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 219 | if (result < 0) return result; |
| 220 | |
| 221 | surface = init_display_surface(width, height); |
| 222 | if (surface == NULL) { |
| 223 | result = -8; |
| 224 | goto exit; |
| 225 | } |
| 226 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 227 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 228 | png_set_bgr(png_ptr); |
| 229 | #endif |
| 230 | |
| 231 | p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); |
| 232 | if (p_row == NULL) { |
| 233 | result = -9; |
| 234 | goto exit; |
| 235 | } |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 236 | for (y = 0; y < height; ++y) { |
| 237 | png_read_row(png_ptr, p_row, NULL); |
| 238 | transform_rgb_to_draw(p_row, surface->data + y * width * 4, channels, width); |
| 239 | } |
| 240 | free(p_row); |
| 241 | |
| 242 | if (channels == 3) |
| 243 | surface->format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 244 | else |
| 245 | surface->format = GGL_PIXEL_FORMAT_RGBA_8888; |
| 246 | |
| 247 | *pSurface = (gr_surface) surface; |
| 248 | |
| 249 | exit: |
that | c26ba0d | 2015-02-13 01:09:55 +0100 | [diff] [blame] | 250 | fclose(fp); |
Ethan Yonker | 448c8dc | 2014-12-08 15:34:34 -0600 | [diff] [blame] | 251 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 252 | if (result < 0 && surface != NULL) free(surface); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 256 | #ifdef TW_INCLUDE_JPEG |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 257 | int res_create_surface_jpg(const char* name, gr_surface* pSurface) { |
| 258 | GGLSurface* surface = NULL; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 259 | int result = 0, y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 260 | struct jpeg_decompress_struct cinfo; |
| 261 | struct jpeg_error_mgr jerr; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 262 | unsigned char* pData; |
| 263 | size_t width, height, stride, pixelSize; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 264 | |
| 265 | FILE* fp = fopen(name, "rb"); |
| 266 | if (fp == NULL) { |
| 267 | char resPath[256]; |
| 268 | |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 269 | snprintf(resPath, sizeof(resPath)-1, TWRES "images/%s", name); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 270 | resPath[sizeof(resPath)-1] = '\0'; |
| 271 | fp = fopen(resPath, "rb"); |
| 272 | if (fp == NULL) { |
| 273 | result = -1; |
| 274 | goto exit; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | cinfo.err = jpeg_std_error(&jerr); |
| 279 | jpeg_create_decompress(&cinfo); |
| 280 | |
| 281 | /* Specify data source for decompression */ |
| 282 | jpeg_stdio_src(&cinfo, fp); |
| 283 | |
| 284 | /* Read file header, set default decompression parameters */ |
| 285 | if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) |
| 286 | goto exit; |
| 287 | |
| 288 | /* Start decompressor */ |
| 289 | (void) jpeg_start_decompress(&cinfo); |
| 290 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 291 | width = cinfo.image_width; |
| 292 | height = cinfo.image_height; |
| 293 | stride = 4 * width; |
| 294 | pixelSize = stride * height; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 295 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 296 | surface = reinterpret_cast<GGLSurface*>(malloc(sizeof(GGLSurface) + pixelSize)); |
| 297 | //p_row = reinterpret_cast<unsigned char*>(malloc(width * 4)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 298 | if (surface == NULL) { |
| 299 | result = -8; |
| 300 | goto exit; |
| 301 | } |
| 302 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 303 | pData = (unsigned char*) (surface + 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 304 | surface->version = sizeof(GGLSurface); |
| 305 | surface->width = width; |
| 306 | surface->height = height; |
| 307 | surface->stride = width; /* Yes, pixels, not bytes */ |
| 308 | surface->data = pData; |
| 309 | surface->format = GGL_PIXEL_FORMAT_RGBX_8888; |
| 310 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 311 | for (y = 0; y < (int) height; ++y) { |
| 312 | unsigned char* pRow = pData + y * stride; |
| 313 | jpeg_read_scanlines(&cinfo, &pRow, 1); |
| 314 | |
| 315 | int x; |
| 316 | for(x = width - 1; x >= 0; x--) { |
| 317 | int sx = x * 3; |
| 318 | int dx = x * 4; |
| 319 | unsigned char r = pRow[sx]; |
| 320 | unsigned char g = pRow[sx + 1]; |
| 321 | unsigned char b = pRow[sx + 2]; |
| 322 | unsigned char a = 0xff; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 323 | #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) |
| 324 | pRow[dx ] = b; // r |
| 325 | pRow[dx + 1] = g; // g |
| 326 | pRow[dx + 2] = r; // b |
| 327 | pRow[dx + 3] = a; |
| 328 | #else |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 329 | pRow[dx ] = r; // r |
| 330 | pRow[dx + 1] = g; // g |
| 331 | pRow[dx + 2] = b; // b |
| 332 | pRow[dx + 3] = a; |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 333 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | *pSurface = (gr_surface) surface; |
| 337 | |
| 338 | exit: |
| 339 | if (fp != NULL) |
| 340 | { |
| 341 | if (surface) |
| 342 | { |
| 343 | (void) jpeg_finish_decompress(&cinfo); |
| 344 | if (result < 0) |
| 345 | { |
| 346 | free(surface); |
| 347 | } |
| 348 | } |
| 349 | jpeg_destroy_decompress(&cinfo); |
| 350 | fclose(fp); |
| 351 | } |
| 352 | return result; |
| 353 | } |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 354 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 355 | |
| 356 | int res_create_surface(const char* name, gr_surface* pSurface) { |
| 357 | int ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | if (!name) return -1; |
| 359 | |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 360 | #ifdef TW_INCLUDE_JPEG |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 361 | if (strlen(name) > 4 && strcmp(name + strlen(name) - 4, ".jpg") == 0) |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 362 | return res_create_surface_jpg(name,pSurface); |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 363 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 364 | |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 365 | ret = res_create_surface_png(name, pSurface); |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 366 | #ifdef TW_INCLUDE_JPEG |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | if (ret < 0) |
| 368 | ret = res_create_surface_jpg(name,pSurface); |
Ethan Yonker | e95c486 | 2016-01-27 10:57:26 -0600 | [diff] [blame] | 369 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | void res_free_surface(gr_surface surface) { |
| 375 | GGLSurface* pSurface = (GGLSurface*) surface; |
| 376 | if (pSurface) { |
| 377 | free(pSurface); |
| 378 | } |
| 379 | } |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 380 | |
| 381 | // Scale image function |
| 382 | int res_scale_surface(gr_surface source, gr_surface* destination, float scale_w, float scale_h) { |
Ethan Yonker | 727aeb8 | 2015-02-10 18:07:44 -0600 | [diff] [blame] | 383 | GGLContext *gl = NULL; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 384 | GGLSurface* sc_mem_surface = NULL; |
| 385 | *destination = NULL; |
| 386 | GGLSurface *surface = (GGLSurface*)source; |
| 387 | int w = gr_get_width(source), h = gr_get_height(source); |
| 388 | int sx = 0, sy = 0, dx = 0, dy = 0; |
| 389 | float dw = (float)w * scale_w; |
| 390 | float dh = (float)h * scale_h; |
| 391 | |
| 392 | // Create a new surface that is the appropriate size |
| 393 | sc_mem_surface = init_display_surface((int)dw, (int)dh); |
| 394 | if (!sc_mem_surface) { |
| 395 | printf("gr_scale_surface failed to init_display_surface\n"); |
| 396 | return -1; |
| 397 | } |
| 398 | sc_mem_surface->format = surface->format; |
| 399 | |
Ethan Yonker | 727aeb8 | 2015-02-10 18:07:44 -0600 | [diff] [blame] | 400 | // Initialize the context |
| 401 | gglInit(&gl); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 402 | gl->colorBuffer(gl, sc_mem_surface); |
| 403 | gl->activeTexture(gl, 0); |
| 404 | |
| 405 | // Enable or disable blending based on source surface format |
| 406 | if (surface->format == GGL_PIXEL_FORMAT_RGBX_8888) { |
| 407 | gl->disable(gl, GGL_BLEND); |
| 408 | } else { |
| 409 | gl->enable(gl, GGL_BLEND); |
| 410 | gl->blendFunc(gl, GGL_ONE, GGL_ZERO); |
| 411 | } |
| 412 | |
| 413 | // Bind our source surface to the context |
| 414 | gl->bindTexture(gl, surface); |
| 415 | |
| 416 | // Deal with the scaling |
| 417 | gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_MIN_FILTER, GGL_LINEAR); |
| 418 | gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_MAG_FILTER, GGL_LINEAR); |
| 419 | gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_S, GGL_CLAMP); |
| 420 | gl->texParameteri(gl, GGL_TEXTURE_2D, GGL_TEXTURE_WRAP_T, GGL_CLAMP); |
| 421 | gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); |
| 422 | gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC); |
| 423 | gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_AUTOMATIC); |
| 424 | gl->enable(gl, GGL_TEXTURE_2D); |
| 425 | |
| 426 | int32_t grad[8]; |
| 427 | memset(grad, 0, sizeof(grad)); |
| 428 | // s, dsdx, dsdy, scale, t, dtdx, dtdy, tscale <- this is wrong! |
| 429 | // This api uses block floating-point for S and T texture coordinates. |
| 430 | // All values are given in 16.16, scaled by 'scale'. In other words, |
| 431 | // set scale to 0, for 16.16 values. |
| 432 | |
| 433 | // s, dsdx, dsdy, t, dtdx, dtdy, sscale, tscale |
| 434 | float dsdx = (float)w / dw; |
| 435 | float dtdy = (float)h / dh; |
| 436 | grad[0] = ((float)sx - (dsdx * dx)) * 65536; |
| 437 | grad[1] = dsdx * 65536; |
| 438 | grad[3] = ((float)sy - (dtdy * dy)) * 65536; |
| 439 | grad[5] = dtdy * 65536; |
| 440 | // printf("blit: w=%d h=%d dx=%d dy=%d dw=%f dh=%f dsdx=%f dtdy=%f s0=%x dsdx=%x t0=%x dtdy=%x\n", |
| 441 | // w, h, dx, dy, dw, dh, dsdx, dtdy, grad[0], grad[1], grad[3], grad[5]); |
| 442 | gl->texCoordGradScale8xv(gl, 0 /*tmu*/, grad); |
| 443 | |
| 444 | // draw / scale the source surface to our target context |
| 445 | gl->recti(gl, dx, dy, dx + dw, dy + dh); |
Ethan Yonker | 727aeb8 | 2015-02-10 18:07:44 -0600 | [diff] [blame] | 446 | gglUninit(gl); |
| 447 | gl = NULL; |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 448 | // put the scaled surface in our destination |
| 449 | *destination = (gr_surface*) sc_mem_surface; |
| 450 | // free memory used in the source |
| 451 | res_free_surface(source); |
| 452 | source = NULL; |
| 453 | return 0; |
| 454 | } |