added country flags to language selectors
[twcon.git] / src / tools / tileset_borderfix.cpp
blob0facb9a3d3138a71866be04498f7493f36fc47a3
1 /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2 /* If you are missing that file, acquire a complete release at teeworlds.com. */
3 #include <base/system.h>
4 #include <engine/external/pnglite/pnglite.h>
6 typedef struct
8 unsigned char r, g, b, a;
9 } CPixel;
11 static CPixel Sample(int x, int y, int w, int h, CPixel *pData, int Pitch, float u, float v)
13 x = x + (int)(w*u);
14 y = y + (int)(h*v);
15 return pData[y*Pitch+x];
18 static void TilesetBorderfix(int w, int h, CPixel *pSrc, CPixel *pDest)
20 int TileW = w/16;
21 int TileH = h/16;
23 mem_zero(pDest, sizeof(CPixel)*w*h);
25 for(int ty = 0; ty < 16; ty++)
27 for(int tx = 0; tx < 16; tx++)
29 for(int y = 0; y < TileH-2; y++)
31 for(int x = 0; x < TileW-2; x++)
33 float u = 0.5f/TileW + x/(float)(TileW-2);
34 float v = 0.5f/TileH + y/(float)(TileH-2);
35 int k = (ty*TileH+1+y)*w + tx*TileW+x+1;
36 pDest[k] = Sample(tx*TileW, ty*TileH, TileW, TileH, pSrc, w, u, v);
38 if(x == 0) pDest[k-1] = pDest[k];
39 if(x == TileW-2-1) pDest[k+1] = pDest[k];
40 if(y == 0) pDest[k-w] = pDest[k];
41 if(y == TileH-2-1) pDest[k+w] = pDest[k];
43 if(x == 0 && y == 0) pDest[k-w-1] = pDest[k];
44 if(x == TileW-2-1 && y == 0) pDest[k-w+1] = pDest[k];
45 if(x == 0 && y == TileH-2-1) pDest[k+w-1] = pDest[k];
46 if(x == TileW-2-1 && y == TileH-2-1) pDest[k+w+1] = pDest[k];
54 int FixFile(const char *pFileName)
56 png_t Png;
57 CPixel *pBuffer[2] = {0,0};
59 png_init(0, 0);
60 png_open_file(&Png, pFileName);
62 if(Png.color_type != PNG_TRUECOLOR_ALPHA)
64 dbg_msg("tileset_borderfix", "%s: not an RGBA image", pFileName);
65 return 1;
68 int w = Png.width;
69 int h = Png.height;
71 pBuffer[0] = (CPixel*)mem_alloc(w*h*sizeof(CPixel), 1);
72 pBuffer[1] = (CPixel*)mem_alloc(w*h*sizeof(CPixel), 1);
73 png_get_data(&Png, (unsigned char *)pBuffer[0]);
74 png_close_file(&Png);
76 TilesetBorderfix(w, h, pBuffer[0], pBuffer[1]);
78 // save here
79 png_open_file_write(&Png, pFileName);
80 png_set_data(&Png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]);
81 png_close_file(&Png);
83 return 0;
86 int main(int argc, const char **argv)
88 dbg_logger_stdout();
89 if(argc == 1)
91 dbg_msg("Usage", "%s FILE1 [ FILE2... ]", argv[0]);
92 return -1;
95 for(int i = 1; i < argc; i++)
96 FixFile(argv[i]);
97 return 0;