void Mem_Free(void **ptr) { if(!*ptr) { WGen_Complain("Mem_Free: Tried to free NULL"); } free(*ptr); *ptr = NULL; }
void* Mem_Alloc(int size) { void *ret = calloc(1, size); if(!ret) { WGen_Complain("Mem_Alloc: Out of memory"); } return ret; }
void Deflate_WriteOutput(byte outByte) { if(!((decoder.writePos - decoder.write) < OVERFLOWCHECK)) { //I_Error("Overflowed output buffer"); WGen_Complain("Overflowed output buffer"); return; } *decoder.writePos++ = outByte; }
cache Png_Create(int width, int height, int numpal, dPalette_t* pal, int bits, cache data, int lump, int* size) { int i = 0; int x = 0; int j = 0; cache image; cache out; cache* row_pointers; png_structp png_ptr; png_infop info_ptr; png_colorp palette; // setup png pointer png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); if(png_ptr == NULL) { WGen_Complain("Png_Create: Failed getting png_ptr"); return NULL; } // setup info pointer info_ptr = png_create_info_struct(png_ptr); if(info_ptr == NULL) { png_destroy_write_struct(&png_ptr, NULL); WGen_Complain("Png_Create: Failed getting info_ptr"); return NULL; } // what does this do again? if(setjmp(png_jmpbuf(png_ptr))) { png_destroy_write_struct(&png_ptr, &info_ptr); WGen_Complain("Png_Create: Failed on setjmp"); return NULL; } // setup custom data writing procedure png_set_write_fn(png_ptr, NULL, Png_WriteData, NULL); // setup image png_set_IHDR( png_ptr, info_ptr, width, height, bits, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT); // setup palette palette = (png_colorp)Mem_Alloc((16 * numpal) * png_sizeof(png_color)); // copy dPalette_t data over to png_colorp for(x = 0, j = 0; x < numpal; x++) { for(i = 0; i < 16; i++) { palette[j].red = pal[j].r; palette[j].green = pal[j].g; palette[j].blue = pal[j].b; j++; } } i = 0; // add palette to png png_set_PLTE(png_ptr, info_ptr, palette, (16 * numpal)); // set transparent index if(palette[0].red == 0 && palette[0].green == 0 && palette[0].blue == 0) { char tmp[9]; strncpy(tmp, romWadFile.lump[lump].name, 8); tmp[0] -= (char)0x80; tmp[8] = 0; // Exempt these lumps if(strcmp(tmp, "FIRE") && /*strcmp(tmp, "USLEGAL") && strcmp(tmp, "TITLE") &&*/ strcmp(tmp, "EVIL") && /*strcmp(tmp, "IDCRED1") && strcmp(tmp, "WMSCRED1") &&*/ strcmp(tmp, "SPACE") && strcmp(tmp, "CLOUD") && strcmp(tmp, "FINAL")) png_set_tRNS(png_ptr, info_ptr, (png_bytep)&i, 1, NULL); } // add png info to data png_write_info(png_ptr, info_ptr); // add offset chunk if png is a sprite if(INSPRITELIST(lump)) { for(i = 0; i < spriteExCount; i++) { if(exSpriteLump[i].lumpRef == lump) { int offs[2]; offs[0] = WGen_Swap32(exSpriteLump[i].sprite.offsetx); offs[1] = WGen_Swap32(exSpriteLump[i].sprite.offsety); png_write_chunk(png_ptr, "grAb", (byte*)offs, 8); break; } } } // setup packing if needed png_set_packing(png_ptr); png_set_packswap(png_ptr); // copy data over image = data; row_pointers = (cache*)Mem_Alloc(sizeof(byte*) * height); for(i = 0; i < height; i++) { row_pointers[i] = (cache)Mem_Alloc(width); if(bits == 4) { for(j = 0; j < width; j += 2) { row_pointers[i][j] = *image & 0xf; row_pointers[i][j+1] = *image >> 4; image++; } } else { for(j = 0; j < width; j++) { row_pointers[i][j] = *image; image++; } } png_write_rows(png_ptr, &row_pointers[i], 1); }