client/tests/browser_png.c (4933B)
1 #include "browser_png.h" 2 #include "guest_heap.h" 3 #include "recomp_types.h" 4 #include <assert.h> 5 #include <png.h> 6 7 #ifdef __EMSCRIPTEN__ 8 double ao_profile_begin(const char *name) { return 0; } 9 void ao_profile_end(const char *name, double start, const char *detail) {} 10 #endif 11 12 static unsigned char encoded[65536]; 13 static size_t encoded_size, read_offset; 14 static uint32_t image, stream; 15 16 static void write_png(png_structp png, png_bytep bytes, png_size_t count) 17 { 18 assert(encoded_size + count <= sizeof(encoded)); 19 memcpy(encoded + encoded_size, bytes, count); 20 encoded_size += count; 21 } 22 23 static void read_guest_stream(void) 24 { 25 assert(g_ecx == stream); 26 unsigned count = MEM32(g_esp + 8); 27 assert(read_offset + count <= encoded_size); 28 memcpy((void *)ADDR(MEM32(g_esp + 4)), encoded + read_offset, count); 29 read_offset += count; 30 g_esp += 12; 31 } 32 33 recomp_func_t recomp_lookup(uint32_t address) 34 { 35 assert(address == 0x12345678); 36 return read_guest_stream; 37 } 38 39 static void encode(int depth, int color, int interlace, const unsigned char *pixels, unsigned stride) 40 { 41 encoded_size = read_offset = 0; 42 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 43 png_infop info = png_create_info_struct(png); 44 assert(png && info); 45 if (setjmp(png_jmpbuf(png))) abort(); 46 png_set_write_fn(png, NULL, write_png, NULL); 47 png_set_IHDR(png, info, 2, 2, depth, color, interlace, 0, 0); 48 if (color == PNG_COLOR_TYPE_PALETTE) { 49 png_color palette[] = {{10, 20, 30}, {40, 50, 60}}; 50 unsigned char alpha[] = {0, 128}; 51 png_set_PLTE(png, info, palette, 2); 52 png_set_tRNS(png, info, alpha, 2, NULL); 53 } 54 png_set_gAMA(png, info, 0.7); 55 png_write_info(png, info); 56 png_bytep rows[] = {(png_bytep)pixels, (png_bytep)pixels + stride}; 57 png_write_image(png, rows); 58 png_write_end(png, info); 59 png_destroy_write_struct(&png, &info); 60 } 61 62 static void check_pixels(unsigned bits, const unsigned char *expected, unsigned size) 63 { 64 assert(MEM32(image + 0x110) == 2 && MEM32(image + 0x114) == 2); 65 assert(MEM32(image + 0x10c) == bits); 66 assert(!memcmp((void *)ADDR(MEM32(image + 0x118)), expected, size)); 67 assert(g_esp == 0x50010000); 68 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 69 assert(read_offset == encoded_size); 70 guest_free(MEM32(image + 0x118)); 71 guest_free(MEM32(image + 0x11c)); 72 } 73 74 static void rgba(void) 75 { 76 const unsigned char pixels[] = {10,20,30,0, 40,50,60,1, 70,80,90,128, 100,110,120,255}; 77 encode(8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_ADAM7, pixels, 8); 78 79 int result = browser_png_load(image, stream); 80 81 const unsigned char expected[] = {30,20,10,0, 60,50,40,1, 90,80,70,128, 120,110,100,255}; 82 assert(result == 0); 83 check_pixels(32, expected, sizeof(expected)); 84 } 85 86 static void rgb16(void) 87 { 88 const unsigned char pixels[] = {10,1,20,2,30,3, 40,4,50,5,60,6, 70,7,80,8,90,9, 100,10,110,11,120,12}; 89 encode(16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, pixels, 12); 90 91 int result = browser_png_load(image, stream); 92 93 const unsigned char expected[] = {30,20,10, 60,50,40, 90,80,70, 120,110,100}; 94 assert(result == 0); 95 check_pixels(24, expected, sizeof(expected)); 96 } 97 98 static void palette(void) 99 { 100 const unsigned char pixels[] = {0x40, 0x80}; 101 encode(1, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, pixels, 1); 102 103 int result = browser_png_load(image, stream); 104 105 const unsigned char expected[] = {0,1,1,0}; 106 assert(result == 0); 107 uint32_t colors = MEM32(image + 0x11c); 108 assert(MEM32(colors) == 0x000a141e && MEM32(colors + 4) == 0x0028323c); 109 check_pixels(8, expected, sizeof(expected)); 110 } 111 112 static void grayscale(void) 113 { 114 const unsigned char pixels[] = {0x05, 0xaf}; 115 encode(4, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, pixels, 1); 116 117 int result = browser_png_load(image, stream); 118 119 const unsigned char expected[] = {0,85,170,255}; 120 assert(result == 0); 121 uint32_t colors = MEM32(image + 0x11c); 122 assert(MEM32(colors + 85 * 4) == 0x00555555); 123 assert(MEM32(colors + 255 * 4) == 0x00ffffff); 124 check_pixels(8, expected, sizeof(expected)); 125 } 126 127 static void invalid_png(void) 128 { 129 memset(encoded, 0, 8); 130 encoded_size = 8; 131 read_offset = 0; 132 MEM32(image + 0x118) = 0xabcdef; 133 134 int result = browser_png_load(image, stream); 135 136 assert(result == -1); 137 assert(MEM32(image + 0x118) == 0xabcdef); 138 assert(g_esp == 0x50010000); 139 } 140 141 int main(void) 142 { 143 assert(guest_map("PNG test stack", 0x50000000, 0x20000)); 144 g_esp = 0x50010000; 145 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 146 image = guest_malloc(0x120); 147 stream = guest_malloc(8); 148 MEM32(stream) = stream + 4; 149 MEM32(stream + 4) = 0x12345678; 150 rgba(); 151 rgb16(); 152 palette(); 153 grayscale(); 154 invalid_png(); 155 guest_free(image); 156 guest_free(stream); 157 puts("PNG decoding passed"); 158 return 0; 159 }