| 126 | } |
| 127 | |
| 128 | int LoadPNGImage(FILE* f, surface_t* surface) { |
| 129 | png_structp png = nullptr; |
| 130 | png_infop info = nullptr; |
| 131 | |
| 132 | png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 133 | if(!png) return -10; |
| 134 | |
| 135 | info = png_create_info_struct(png); |
| 136 | if(!info) return -11; |
| 137 | |
| 138 | int e = setjmp(png_jmpbuf(png)); |
| 139 | if(e){ |
| 140 | printf("[LibLemon] LoadPNGImage: setjmp error %d\n", 0); |
| 141 | return e; |
| 142 | } |
| 143 | |
| 144 | png_init_io(png, f); |
| 145 | png_set_sig_bytes(png, 8);//ftell(f)); |
| 146 | |
| 147 | png_read_info(png, info); |
| 148 | |
| 149 | png_uint_32 width = png_get_image_width(png, info); |
| 150 | png_uint_32 height = png_get_image_height(png, info); |
| 151 | |
| 152 | if (png_get_color_type(png, info) == PNG_COLOR_TYPE_PALETTE) |
| 153 | png_set_palette_to_rgb(png); |
| 154 | |
| 155 | if (png_get_color_type(png, info) == PNG_COLOR_TYPE_RGB) |
| 156 | png_set_filler(png, 0xff, PNG_FILLER_BEFORE); |
| 157 | |
| 158 | png_set_bgr(png); |
| 159 | |
| 160 | assert(width < INT_MAX); |
| 161 | assert(height < INT_MAX); |
| 162 | |
| 163 | surface_t _surface = {.width = static_cast<int>(width), .height = static_cast<int>(height), .depth = 32, .buffer = (uint8_t*)malloc(width * height * 4)}; |
| 164 | *surface = _surface; |
| 165 | |
| 166 | png_bytep rowPointers[height]; |
| 167 | |
| 168 | for(png_uint_32 i = 0; i < height; i++){ |
| 169 | rowPointers[i] = surface->buffer + i * surface->width * 4; |
| 170 | png_read_row(png, rowPointers[i], NULL); |
| 171 | } |
| 172 | |
| 173 | png_destroy_read_struct(&png, &info, nullptr); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | int SavePNGImage(FILE* f, surface_t* surface, bool writeTransparency) { |
| 179 | png_structp png = nullptr; |