added country flags to language selectors
[twcon.git] / src / tools / dilate.cpp
blobeb770a908fbfe82ed739274e910fd0064998ed4b
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 <base/math.h>
5 #include <engine/external/pnglite/pnglite.h>
7 typedef struct
9 unsigned char r, g, b, a;
10 } CPixel;
12 static void Dilate(int w, int h, CPixel *pSrc, CPixel *pDest)
14 int ix, iy;
15 const int xo[] = {0, -1, 1, 0};
16 const int yo[] = {-1, 0, 0, 1};
18 int m = 0;
19 for(int y = 0; y < h; y++)
21 for(int x = 0; x < w; x++, m++)
23 pDest[m] = pSrc[m];
24 if(pSrc[m].a)
25 continue;
27 for(int c = 0; c < 4; c++)
29 ix = clamp(x + xo[c], 0, w-1);
30 iy = clamp(y + yo[c], 0, h-1);
31 int k = iy*w+ix;
32 if(pSrc[k].a)
34 pDest[m] = pSrc[k];
35 pDest[m].a = 255;
36 break;
43 static void CopyAlpha(int w, int h, CPixel *pSrc, CPixel *pDest)
45 int m = 0;
46 for(int y = 0; y < h; y++)
47 for(int x = 0; x < w; x++, m++)
48 pDest[m].a = pSrc[m].a;
51 int DilateFile(const char *pFileName)
53 png_t Png;
54 CPixel *pBuffer[3] = {0,0,0};
56 png_init(0, 0);
57 png_open_file(&Png, pFileName);
59 if(Png.color_type != PNG_TRUECOLOR_ALPHA)
61 dbg_msg("dilate", "%s: not an RGBA image", pFileName);
62 return 1;
65 pBuffer[0] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1);
66 pBuffer[1] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1);
67 pBuffer[2] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1);
68 png_get_data(&Png, (unsigned char *)pBuffer[0]);
69 png_close_file(&Png);
71 int w = Png.width;
72 int h = Png.height;
74 Dilate(w, h, pBuffer[0], pBuffer[1]);
75 for(int i = 0; i < 5; i++)
77 Dilate(w, h, pBuffer[1], pBuffer[2]);
78 Dilate(w, h, pBuffer[2], pBuffer[1]);
81 CopyAlpha(w, h, pBuffer[0], pBuffer[1]);
83 // save here
84 png_open_file_write(&Png, pFileName);
85 png_set_data(&Png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]);
86 png_close_file(&Png);
88 return 0;
91 int main(int argc, const char **argv)
93 dbg_logger_stdout();
94 if(argc == 1)
96 dbg_msg("Usage", "%s FILE1 [ FILE2... ]", argv[0]);
97 return -1;
100 for(int i = 1; i < argc; i++)
101 DilateFile(argv[i]);
102 return 0;