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>
8 unsigned char r
, g
, b
, a
;
11 static CPixel
Sample(int x
, int y
, int w
, int h
, CPixel
*pData
, int Pitch
, float u
, float v
)
15 return pData
[y
*Pitch
+x
];
18 static void TilesetBorderfix(int w
, int h
, CPixel
*pSrc
, CPixel
*pDest
)
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
)
57 CPixel
*pBuffer
[2] = {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
);
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]);
76 TilesetBorderfix(w
, h
, pBuffer
[0], pBuffer
[1]);
79 png_open_file_write(&Png
, pFileName
);
80 png_set_data(&Png
, w
, h
, 8, PNG_TRUECOLOR_ALPHA
, (unsigned char *)pBuffer
[1]);
86 int main(int argc
, const char **argv
)
91 dbg_msg("Usage", "%s FILE1 [ FILE2... ]", argv
[0]);
95 for(int i
= 1; i
< argc
; i
++)