ddnet/src/tools/map_replace_image.cpp

187 lines
5 KiB
C++
Raw Normal View History

2017-08-29 22:21:41 +00:00
/* (c) DDNet developers. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <base/system.h>
#include <base/math.h>
#include <engine/shared/datafile.h>
#include <engine/storage.h>
#include <engine/graphics.h>
#include <game/mapitems.h>
#include <pnglite.h>
/*
2017-08-29 22:34:14 +00:00
Usage: map_replace_image <source map filepath> <dest map filepath> <current image name> <new image filepath>
Notes: map filepath must be relative to user default teeworlds folder
new image filepath must be absolute or relative to the current position
*/
2017-08-29 22:34:14 +00:00
CDataFileReader g_DataReader;
CDataFileWriter g_DataWriter;
// global new image data (set by ReplaceImageItem)
2017-08-29 22:21:41 +00:00
int g_NewNameID = -1;
char g_aNewName[128];
int g_NewDataID = -1;
int g_NewDataSize = 0;
void *g_pNewData = 0;
2017-08-29 22:21:41 +00:00
int LoadPNG(CImageInfo *pImg, const char *pFilename)
{
2017-08-29 22:34:14 +00:00
unsigned char *pBuffer;
png_t Png;
int Error = png_open_file(&Png, pFilename);
if(Error != PNG_NO_ERROR)
{
dbg_msg("map_replace_image", "failed to open image file. filename='%s'", pFilename);
if(Error != PNG_FILE_ERROR)
png_close_file(&Png);
return 0;
}
if(Png.depth != 8 || (Png.color_type != PNG_TRUECOLOR && Png.color_type != PNG_TRUECOLOR_ALPHA) || Png.width > (2<<12) || Png.height > (2<<12))
{
dbg_msg("map_replace_image", "invalid image format. filename='%s'", pFilename);
png_close_file(&Png);
return 0;
}
pBuffer = (unsigned char *)malloc(Png.width * Png.height * Png.bpp);
2017-08-29 22:34:14 +00:00
png_get_data(&Png, pBuffer);
png_close_file(&Png);
pImg->m_Width = Png.width;
pImg->m_Height = Png.height;
if(Png.color_type == PNG_TRUECOLOR)
pImg->m_Format = CImageInfo::FORMAT_RGB;
else if(Png.color_type == PNG_TRUECOLOR_ALPHA)
pImg->m_Format = CImageInfo::FORMAT_RGBA;
pImg->m_pData = pBuffer;
return 1;
}
2017-08-29 22:21:41 +00:00
void *ReplaceImageItem(void *pItem, int Type, const char *pImgName, const char *pImgFile, CMapItemImage *pNewImgItem)
{
2017-08-29 22:21:41 +00:00
if(Type != MAPITEMTYPE_IMAGE)
return pItem;
CMapItemImage *pImgItem = (CMapItemImage *)pItem;
2017-08-29 22:34:14 +00:00
char *pName = (char *)g_DataReader.GetData(pImgItem->m_ImageName);
2017-08-29 22:21:41 +00:00
if(str_comp(pImgName, pName) != 0)
return pItem;
2017-08-29 22:21:41 +00:00
dbg_msg("map_replace_image", "found image '%s'", pImgName);
2017-08-29 22:21:41 +00:00
CImageInfo ImgInfo;
if(!LoadPNG(&ImgInfo, pImgFile))
return 0;
2017-08-29 22:21:41 +00:00
*pNewImgItem = *pImgItem;
2017-08-29 22:21:41 +00:00
pNewImgItem->m_Width = ImgInfo.m_Width;
pNewImgItem->m_Height = ImgInfo.m_Height;
int PixelSize = ImgInfo.m_Format == CImageInfo::FORMAT_RGB ? 3 : 4;
2017-08-29 22:21:41 +00:00
g_NewNameID = pImgItem->m_ImageName;
IStorage::StripPathAndExtension(pImgFile, g_aNewName, sizeof(g_aNewName));
2017-08-29 22:21:41 +00:00
g_NewDataID = pImgItem->m_ImageData;
g_pNewData = ImgInfo.m_pData;
g_NewDataSize = ImgInfo.m_Width * ImgInfo.m_Height * PixelSize;
2017-08-29 22:21:41 +00:00
return (void *)pNewImgItem;
}
int main(int argc, const char **argv)
{
2017-08-29 22:21:41 +00:00
dbg_logger_stdout();
IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_BASIC, argc, argv);
if(argc != 5)
{
2017-08-29 22:34:14 +00:00
dbg_msg("map_replace_image", "Invalid arguments");
dbg_msg("map_replace_image", "Usage: map_replace_image <source map filepath> <dest map filepath> <current image name> <new image filepath>");
dbg_msg("map_replace_image", "Notes: map filepath must be relative to user default teeworlds folder");
dbg_msg("map_replace_image", " new image filepath must be absolute or relative to the current position");
2017-08-29 22:21:41 +00:00
return -1;
}
if (!pStorage)
{
2017-08-29 22:34:14 +00:00
dbg_msg("map_replace_image", "error loading storage");
2017-08-29 22:21:41 +00:00
return -1;
}
const char *pSourceFileName = argv[1];
const char *pDestFileName = argv[2];
const char *pImageName = argv[3];
const char *pImageFile = argv[4];
int ID = 0;
2017-08-29 22:34:14 +00:00
int Type = 0;
int Size = 0;
2017-08-29 22:21:41 +00:00
void *pItem = 0;
void *pData = 0;
2017-08-29 22:34:14 +00:00
if(!g_DataReader.Open(pStorage, pSourceFileName, IStorage::TYPE_ALL))
2017-08-29 22:21:41 +00:00
{
dbg_msg("map_replace_image", "failed to open source map. filename='%s'", pSourceFileName);
return -1;
}
2017-08-29 22:34:14 +00:00
if(!g_DataWriter.Open(pStorage, pDestFileName))
2017-08-29 22:21:41 +00:00
{
dbg_msg("map_replace_image", "failed to open destination map. filename='%s'", pDestFileName);
return -1;
}
2017-08-29 22:34:14 +00:00
png_init(0,0);
2017-08-29 22:21:41 +00:00
// add all items
2017-08-29 22:34:14 +00:00
for(int Index = 0; Index < g_DataReader.NumItems(); Index++)
2017-08-29 22:21:41 +00:00
{
CMapItemImage NewImageItem;
2017-08-29 22:34:14 +00:00
pItem = g_DataReader.GetItem(Index, &Type, &ID);
Size = g_DataReader.GetItemSize(Index);
2017-08-29 22:21:41 +00:00
pItem = ReplaceImageItem(pItem, Type, pImageName, pImageFile, &NewImageItem);
if(!pItem)
return -1;
2017-08-29 22:34:14 +00:00
g_DataWriter.AddItem(Type, ID, Size, pItem);
2017-08-29 22:21:41 +00:00
}
if(g_NewDataID == -1)
{
dbg_msg("map_replace_image", "image '%s' not found on source map '%s'.", pImageName, pSourceFileName);
return -1;
}
// add all data
2017-08-29 22:34:14 +00:00
for(int Index = 0; Index < g_DataReader.NumItems(); Index++)
2017-08-29 22:21:41 +00:00
{
if(Index == g_NewDataID)
{
pData = g_pNewData;
Size = g_NewDataSize;
2017-08-29 22:34:14 +00:00
}
2017-08-29 22:21:41 +00:00
else if (Index == g_NewNameID)
{
pData = (void *)g_aNewName;
Size = str_length(g_aNewName) + 1;
}
else
{
2017-08-29 22:34:14 +00:00
pData = g_DataReader.GetData(Index);
Size = g_DataReader.GetDataSize(Index);
2017-08-29 22:21:41 +00:00
}
2017-08-29 22:34:14 +00:00
g_DataWriter.AddData(Size, pData);
2017-08-29 22:21:41 +00:00
}
2017-08-29 22:34:14 +00:00
g_DataReader.Close();
g_DataWriter.Finish();
2017-08-29 22:21:41 +00:00
dbg_msg("map_replace_image", "image '%s' replaced", pImageName);
return 0;
}