ddnet/src/tools/tileset_borderset.cpp
heinrich5991 f8277267a0 Remove mem_alloc and mem_free, use standard C functions instead
Replace `mem_free` by `free`, and `mem_alloc` by `malloc` or `calloc`
(the latter one being used to allocate a zeroed array of elements,
sometimes, this makes a call to `mem_zero` superfluous).

This results in having to remove `mem_stats` which previously recorded
the number of allocations and their size that the Teeworlds code did
directly.

Remove OOM handling in `src/game/client/components/binds.cpp`.

Remove needless copying in the CSV code in
`src/game/client/components/statboard.cpp`.
2018-04-09 11:56:39 +02:00

80 lines
1.7 KiB
C++

/* (c) Magnus Auvinen. 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/math.h>
#include <base/system.h>
#include <pnglite.h>
typedef struct
{
unsigned char r, g, b, a;
} CPixel;
static void TilesetBorderset(int w, int h, CPixel *pSrc, CPixel *pDest)
{
int TileW = w/16;
int TileH = h/16;
for(int tx = 0; tx < 16; tx++)
{
for(int ty = 0; ty < 16; ty++)
{
for(int x = 0; x < TileW; x++)
{
for(int y = 0; y < TileH; y++)
{
#define TILE_INDEX(tx_, ty_, x_, y_) (((ty_) * TileH + (y_)) * w + (tx_) * TileW + (x_))
pDest[TILE_INDEX(tx, ty, x, y)] = pSrc[TILE_INDEX(tx, ty, clamp(x, 2, TileW - 3), clamp(y, 2, TileH - 3))];
}
}
}
}
}
int FixFile(const char *pFileName)
{
png_t Png;
CPixel *pBuffer[2] = {0,0};
png_init(0, 0);
png_open_file(&Png, pFileName);
if(Png.color_type != PNG_TRUECOLOR_ALPHA)
{
dbg_msg("tileset_borderset", "%s: not an RGBA image", pFileName);
return 1;
}
int w = Png.width;
int h = Png.height;
pBuffer[0] = (CPixel *)malloc(w * h * sizeof(CPixel));
pBuffer[1] = (CPixel *)malloc(w * h * sizeof(CPixel));
png_get_data(&Png, (unsigned char *)pBuffer[0]);
png_close_file(&Png);
TilesetBorderset(w, h, pBuffer[0], pBuffer[1]);
// save here
png_open_file_write(&Png, pFileName);
png_set_data(&Png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]);
png_close_file(&Png);
return 0;
}
int main(int argc, const char **argv)
{
dbg_logger_stdout();
if(argc == 1)
{
dbg_msg("usage", "%s FILE1 [ FILE2... ]", argv[0]);
return -1;
}
for(int i = 1; i < argc; i++)
FixFile(argv[i]);
return 0;
}