// _________ __ __ // / _____// |_____________ _/ |______ ____ __ __ ______ // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > // \/ \/ \//_____/ \/ // ______________________ ______________________ // T H E W A R B E G I N S // Utility for Stratagus - A free fantasy real time strategy game engine // // tile2png.c - Convert tileset files to png files. // // (c) Copyright 1998,2000,2001 by Lutz Sammer // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 dated June, 1991. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. // // $Id$ #include #include #include #ifndef _MSC_VER #include #include #include #endif #include extern int SavePNG(const char* name,unsigned char* image,int w,int h); #undef main // // Print debug information of level 0. // #define DebugLevel0(fmt,args...) printf(fmt,##args) // // Print debug information of level 1. // #define DebugLevel1(fmt,args...) /*printf(fmt,##args)*/ // // Print debug information of level 2. // #define DebugLevel2(fmt,args...) /*printf(fmt,##args)*/ // // Print debug information of level 0. // #define DebugLevel0Fn(fmt,args...) printf(__FUNCTION__": "fmt,##args) // // Print debug information of level 1. // #define DebugLevel1Fn(fmt,args...) /*printf(__FUNCTION__": "fmt,##args)*/ // // Print debug information of level 2. // #define DebugLevel2Fn(fmt,args...) /*printf(__FUNCTION__": "fmt,##args)*/ #define GetByte(p) (*((unsigned char*)(p))) #define GetWord(p) (*((unsigned short*)(p))) #define GetLong(p) (*((unsigned long*)(p))) #define FetchByte(p) (*((unsigned char*)(p))++) #define FetchWord(p) (*((unsigned short*)(p))++) #define FetchLong(p) (*((unsigned long*)(p))++) int Map2Tile[0x9E0]; int Img2Tile[0x9E0]; int NumTiles; /** ** Count used mega tiles for map. */ void CountUsedTiles(const unsigned char* map,const unsigned char* mega) { int i; int j; int used; const char* tp; DebugLevel1Fn("\n"); memset(Map2Tile,0,sizeof(Map2Tile)); // // Build conversion table. // for( i=0; i<0x9E; ++i ) { tp=map+i*42; DebugLevel2("%02X:",i); for( j=0; j<0x10; ++j ) { DebugLevel2("%04X ",GetWord(tp+j*2)); Map2Tile[(i<<4)|j]=GetWord(tp+j*2); } DebugLevel2("\n"); } // // Mark all used mega tiles. // used=0; for( i=0; i<0x9E0; ++i ) { if( !Map2Tile[i] ) { continue; } for( j=0; jjmpbuf) ) { // FIXME: must free buffers!! png_destroy_write_struct(&png_ptr,&info_ptr); fclose(fp); return 1; } png_init_io(png_ptr,fp); // zlib parameters png_set_compression_level(png_ptr,Z_BEST_COMPRESSION); // prepare the file information info_ptr->width=w; info_ptr->height=h; info_ptr->bit_depth=8; info_ptr->color_type=PNG_COLOR_TYPE_PALETTE; info_ptr->interlace_type=0; info_ptr->valid|=PNG_INFO_PLTE; info_ptr->palette=(void*)OwnPalette; info_ptr->num_palette=256; png_write_info(png_ptr,info_ptr); // write the file header information // set transformation // prepare image lines=malloc(h*sizeof(*lines)); if( !lines ) { png_destroy_write_struct(&png_ptr,&info_ptr); fclose(fp); return 1; } for( i=0; i