~ubuntu-branches/ubuntu/trusty/teeworlds/trusty-updates

« back to all changes in this revision

Viewing changes to src/tools/tileset_borderfix.c

  • Committer: Bazaar Package Importer
  • Author(s): Jack Coulter
  • Date: 2008-04-13 18:48:12 UTC
  • Revision ID: james.westby@ubuntu.com-20080413184812-efc80waq2er6p1bs
Tags: upstream-0.4.2
ImportĀ upstreamĀ versionĀ 0.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
 
2
 
 
3
#include "../engine/external/pnglite/pnglite.c"
 
4
 
 
5
typedef struct pixel_t
 
6
{
 
7
        unsigned char r, g, b, a;
 
8
} pixel;
 
9
 
 
10
static pixel sample(int x, int y, int w, int h, pixel *data, int pitch, float u, float v)
 
11
{
 
12
        x = x + (int)(w*u);
 
13
        y = y + (int)(h*v);
 
14
        return data[y*pitch+x];
 
15
}
 
16
 
 
17
static void tilemap_borderfix(int w, int h, pixel *src, pixel *dst)
 
18
{
 
19
        int tilew = w/16;
 
20
        int tileh = h/16;
 
21
        int tx, ty;
 
22
        int x, y;
 
23
        int k;
 
24
        float u, v;
 
25
        
 
26
        memset(dst, 0, sizeof(pixel)*w*h);
 
27
                                
 
28
        for(ty = 0; ty < 16; ty++)
 
29
                for(tx = 0; tx < 16; tx++)
 
30
                {
 
31
                        for(y = 0; y < tileh-2; y++)
 
32
                                for(x = 0; x < tilew-2; x++)
 
33
                                {
 
34
                                        u = 0.5f/tilew + x/(float)(tilew-2);
 
35
                                        v = 0.5f/tileh + y/(float)(tileh-2);
 
36
                                        k = (ty*tileh+1+y)*w + tx*tilew+x+1;
 
37
                                        dst[k] = sample(tx*tilew, ty*tileh, tilew, tileh, src, w, u, v);
 
38
                                        
 
39
                                        if(x == 0) dst[k-1] = dst[k];
 
40
                                        if(x == tilew-2-1) dst[k+1] = dst[k];
 
41
                                        if(y == 0) dst[k-w] = dst[k];
 
42
                                        if(y == tileh-2-1) dst[k+w] = dst[k];
 
43
                                        
 
44
                                        if(x == 0 && y == 0) dst[k-w-1] = dst[k];
 
45
                                        if(x == tilew-2-1 && y == 0) dst[k-w+1] = dst[k];
 
46
                                        if(x == 0 && y == tileh-2-1) dst[k+w-1] = dst[k];
 
47
                                        if(x == tilew-2-1 && y == tileh-2-1) dst[k+w+1] = dst[k];
 
48
                                }
 
49
                }
 
50
}
 
51
 
 
52
 
 
53
int main(int argc, char **argv)
 
54
{
 
55
        png_t png;
 
56
        pixel *buffer[2] = {0,0};
 
57
        int w, h;
 
58
        
 
59
        png_init(0,0);
 
60
        png_open_file(&png, argv[1]);
 
61
        
 
62
        if(png.color_type != PNG_TRUECOLOR_ALPHA)
 
63
        {
 
64
                printf("not an RGBA image\n");
 
65
                return -1;
 
66
        }
 
67
        
 
68
        w = png.width;
 
69
        h = png.height;
 
70
        
 
71
        buffer[0] = (pixel*)malloc(w*h*sizeof(pixel));
 
72
        buffer[1] = (pixel*)malloc(w*h*sizeof(pixel));
 
73
        png_get_data(&png, (unsigned char *)buffer[0]);
 
74
        png_close_file(&png);
 
75
        
 
76
        tilemap_borderfix(w, h, buffer[0], buffer[1]);
 
77
        
 
78
        // save here
 
79
        png_open_file_write(&png, argv[1]);
 
80
        png_set_data(&png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)buffer[1]);
 
81
        png_close_file(&png);
 
82
                
 
83
        return 0;
 
84
}