about summary refs log tree commit diff
path: root/src/tools/tileset_borderfix.c
blob: a4e6f73e23ffe22ec179257a99b3e2e95e7ba781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* (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 "./engine/external/pnglite/pnglite.c"

typedef struct pixel_t
{
	unsigned char r, g, b, a;
} pixel;

static pixel sample(int x, int y, int w, int h, pixel *data, int pitch, float u, float v)
{
	x = x + (int)(w*u);
	y = y + (int)(h*v);
	return data[y*pitch+x];
}

static void tilemap_borderfix(int w, int h, pixel *src, pixel *dst)
{
	int tilew = w/16;
	int tileh = h/16;
	int tx, ty;
	int x, y;
	int k;
	float u, v;
	
	memset(dst, 0, sizeof(pixel)*w*h);
				
	for(ty = 0; ty < 16; ty++)
		for(tx = 0; tx < 16; tx++)
		{
			for(y = 0; y < tileh-2; y++)
				for(x = 0; x < tilew-2; x++)
				{
					u = 0.5f/tilew + x/(float)(tilew-2);
					v = 0.5f/tileh + y/(float)(tileh-2);
					k = (ty*tileh+1+y)*w + tx*tilew+x+1;
					dst[k] = sample(tx*tilew, ty*tileh, tilew, tileh, src, w, u, v);
					
					if(x == 0) dst[k-1] = dst[k];
					if(x == tilew-2-1) dst[k+1] = dst[k];
					if(y == 0) dst[k-w] = dst[k];
					if(y == tileh-2-1) dst[k+w] = dst[k];
					
					if(x == 0 && y == 0) dst[k-w-1] = dst[k];
					if(x == tilew-2-1 && y == 0) dst[k-w+1] = dst[k];
					if(x == 0 && y == tileh-2-1) dst[k+w-1] = dst[k];
					if(x == tilew-2-1 && y == tileh-2-1) dst[k+w+1] = dst[k];
				}
		}
}


int main(int argc, char **argv)
{
	png_t png;
	pixel *buffer[2] = {0,0};
	int w, h;
	
	png_init(0,0);
	png_open_file(&png, argv[1]);
	
	if(png.color_type != PNG_TRUECOLOR_ALPHA)
	{
		printf("not an RGBA image\n");
		return -1;
	}
	
	w = png.width;
	h = png.height;
	
	buffer[0] = (pixel*)malloc(w*h*sizeof(pixel));
	buffer[1] = (pixel*)malloc(w*h*sizeof(pixel));
	png_get_data(&png, (unsigned char *)buffer[0]);
	png_close_file(&png);
	
	tilemap_borderfix(w, h, buffer[0], buffer[1]);
	
	// save here
	png_open_file_write(&png, argv[1]);
	png_set_data(&png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)buffer[1]);
	png_close_file(&png);
		
	return 0;
}