| 176 | } |
| 177 | |
| 178 | int SavePNGImage(FILE* f, surface_t* surface, bool writeTransparency) { |
| 179 | png_structp png = nullptr; |
| 180 | png_infop info = nullptr; |
| 181 | |
| 182 | (void)writeTransparency; |
| 183 | |
| 184 | png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 185 | if(!png) return -10; |
| 186 | |
| 187 | info = png_create_info_struct(png); |
| 188 | if(!info) return -11; |
| 189 | |
| 190 | int e = setjmp(png_jmpbuf(png)); |
| 191 | if(e){ |
| 192 | printf("[LibLemon] LoadPNGImage: setjmp error\n"); |
| 193 | return e; |
| 194 | } |
| 195 | |
| 196 | png_init_io(png, f); |
| 197 | png_set_compression_level(png, Z_BEST_COMPRESSION); |
| 198 | |
| 199 | png_uint_32 width = surface->width; |
| 200 | png_uint_32 height = surface->height; |
| 201 | |
| 202 | png_set_IHDR(png, info, width, height, 8 /*32 bits per pixel*/, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 203 | |
| 204 | png_set_packing(png); |
| 205 | |
| 206 | surface_t _surface = {.width = static_cast<int>(width), .height = static_cast<int>(height), .depth = 32, .buffer = (uint8_t*)malloc(width * height * 4)}; |
| 207 | *surface = _surface; |
| 208 | |
| 209 | png_bytepp rowPointers = new png_bytep[height]; |
| 210 | |
| 211 | for(png_uint_32 i = 0; i < height; i++){ |
| 212 | rowPointers[i] = surface->buffer + i * surface->width * 4; |
| 213 | } |
| 214 | |
| 215 | png_set_rows(png, info, rowPointers); |
| 216 | |
| 217 | png_write_png(png, info, 0, nullptr); |
| 218 | |
| 219 | png_destroy_write_struct(&png, &info); |
| 220 | |
| 221 | delete[] rowPointers; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int LoadBitmapImage(FILE* f, surface_t* surface) { |
| 227 | fseek(f, 0, SEEK_SET); |