aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/browser_png.c (3813B)


      1 #include "browser_png.h"
      2 #include "browser_profile.h"
      3 #include "guest_heap.h"
      4 #include "recomp_types.h"
      5 #include <png.h>
      6 
      7 struct png_load {
      8     uint32_t stream, scratch, pixels, palette;
      9     png_bytep *rows;
     10 };
     11 
     12 static void read_stream(png_structp png, png_bytep output, png_size_t size)
     13 {
     14     struct png_load *load = png_get_io_ptr(png);
     15     while (size) {
     16         unsigned count = size > 8192 ? 8192 : (unsigned)size;
     17         g_ecx = load->stream;
     18         PUSH32(g_esp, count);
     19         PUSH32(g_esp, load->scratch);
     20         PUSH32(g_esp, RECOMP_RETADDR);
     21         recomp_lookup(MEM32(MEM32(load->stream)))();
     22         memcpy(output, (void *)ADDR(load->scratch), count);
     23         output += count;
     24         size -= count;
     25     }
     26 }
     27 
     28 int browser_png_load(uint32_t image, uint32_t stream)
     29 {
     30     double start = ao_profile_begin("PNG decode");
     31     png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
     32     png_infop info = png ? png_create_info_struct(png) : NULL;
     33     struct png_load *load = calloc(1, sizeof(*load));
     34     int result = -1;
     35     if (!png || !info || !load) goto done;
     36     load->stream = stream;
     37     load->scratch = guest_malloc(8192);
     38     if (!load->scratch) goto done;
     39     if (setjmp(png_jmpbuf(png))) goto done;
     40 
     41     /* Match bbr.dll!sub_25001830: unpack, truncate 16-bit samples and use BGR.
     42      * Palette indices and transparency chunks retain the guest's semantics. */
     43     png_set_read_fn(png, load, read_stream);
     44     png_read_info(png, info);
     45     int depth = png_get_bit_depth(png, info);
     46     int color = png_get_color_type(png, info);
     47     if (depth < 8) png_set_packing(png);
     48     if (color == PNG_COLOR_TYPE_GRAY && depth < 8) png_set_expand(png);
     49     if (depth == 16) png_set_strip_16(png);
     50     png_set_bgr(png);
     51     png_set_interlace_handling(png);
     52     png_read_update_info(png, info);
     53 
     54     /* Guest image owners free these buffers through the guest heap. */
     55     unsigned width = png_get_image_width(png, info);
     56     unsigned height = png_get_image_height(png, info);
     57     size_t stride = png_get_rowbytes(png, info);
     58     if (!height || stride > UINT32_MAX / height) png_error(png, "PNG image too large");
     59     load->pixels = guest_malloc(stride * height);
     60     load->rows = malloc(height * sizeof(*load->rows));
     61     if (!load->pixels || !load->rows) png_error(png, "PNG allocation failed");
     62     for (unsigned y = 0; y < height; ++y)
     63         load->rows[y] = (void *)ADDR(load->pixels + y * stride);
     64 
     65     /* The guest stores palettes as BGRX and supplies a ramp for grayscale. */
     66     png_colorp palette;
     67     int colors;
     68     if (png_get_PLTE(png, info, &palette, &colors)) {
     69         load->palette = guest_malloc(colors * 4);
     70         if (!load->palette) png_error(png, "PNG palette allocation failed");
     71         for (int i = 0; i < colors; ++i)
     72             MEM32(load->palette + i * 4) = palette[i].blue |
     73                 (uint32_t)palette[i].green << 8 | (uint32_t)palette[i].red << 16;
     74     } else if (color == PNG_COLOR_TYPE_GRAY) {
     75         load->palette = guest_malloc(1024);
     76         if (!load->palette) png_error(png, "PNG palette allocation failed");
     77         for (unsigned i = 0; i < 256; ++i) MEM32(load->palette + i * 4) = i * 0x010101;
     78     }
     79     png_read_image(png, load->rows);
     80     png_read_end(png, NULL);
     81     MEM32(image + 0x110) = width;
     82     MEM32(image + 0x114) = height;
     83     MEM32(image + 0x10c) = png_get_channels(png, info) * 8;
     84     MEM32(image + 0x118) = load->pixels;
     85     MEM32(image + 0x11c) = load->palette;
     86     load->pixels = load->palette = 0;
     87     result = 0;
     88 
     89 done:
     90     png_destroy_read_struct(&png, &info, NULL);
     91     if (load) {
     92         guest_free(load->scratch);
     93         guest_free(load->pixels);
     94         guest_free(load->palette);
     95         free(load->rows);
     96         free(load);
     97     }
     98     ao_profile_end("PNG decode", start, NULL);
     99     return result;
    100 }