diff options
| author | Choupom <andycootlapin@hotmail.fr> | 2011-03-17 17:38:43 +0100 |
|---|---|---|
| committer | oy <Tom_Adams@web.de> | 2011-03-31 22:58:49 +0200 |
| commit | c2ed5f70936e0f379046e02f894fd2f6635f6b28 (patch) | |
| tree | a1f7ef12786e16676ebd3601ffa53dd3e9ca004e /src | |
| parent | 979eca0095ecfb40a160636752eaf0e61bdc4ca9 (diff) | |
| download | zcatch-c2ed5f70936e0f379046e02f894fd2f6635f6b28.tar.gz zcatch-c2ed5f70936e0f379046e02f894fd2f6635f6b28.zip | |
refactored tileset_borderfix
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/tileset_borderfix.c | 85 | ||||
| -rw-r--r-- | src/tools/tileset_borderfix.cpp | 84 |
2 files changed, 84 insertions, 85 deletions
diff --git a/src/tools/tileset_borderfix.c b/src/tools/tileset_borderfix.c deleted file mode 100644 index a4e6f73e..00000000 --- a/src/tools/tileset_borderfix.c +++ /dev/null @@ -1,85 +0,0 @@ -/* (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; -} diff --git a/src/tools/tileset_borderfix.cpp b/src/tools/tileset_borderfix.cpp new file mode 100644 index 00000000..9541919f --- /dev/null +++ b/src/tools/tileset_borderfix.cpp @@ -0,0 +1,84 @@ +/* (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 <base/system.h> +#include <engine/external/pnglite/pnglite.h> + +typedef struct +{ + unsigned char r, g, b, a; +} CPixel; + +static CPixel Sample(int x, int y, int w, int h, CPixel *pData, int Pitch, float u, float v) +{ + x = x + (int)(w*u); + y = y + (int)(h*v); + return pData[y*Pitch+x]; +} + +static void TilesetBorderfix(int w, int h, CPixel *pSrc, CPixel *pDest) +{ + int TileW = w/16; + int TileH = h/16; + + mem_zero(pDest, sizeof(CPixel)*w*h); + + for(int ty = 0; ty < 16; ty++) + { + for(int tx = 0; tx < 16; tx++) + { + for(int y = 0; y < TileH-2; y++) + { + for(int x = 0; x < TileW-2; x++) + { + float u = 0.5f/TileW + x/(float)(TileW-2); + float v = 0.5f/TileH + y/(float)(TileH-2); + int k = (ty*TileH+1+y)*w + tx*TileW+x+1; + pDest[k] = Sample(tx*TileW, ty*TileH, TileW, TileH, pSrc, w, u, v); + + if(x == 0) pDest[k-1] = pDest[k]; + if(x == TileW-2-1) pDest[k+1] = pDest[k]; + if(y == 0) pDest[k-w] = pDest[k]; + if(y == TileH-2-1) pDest[k+w] = pDest[k]; + + if(x == 0 && y == 0) pDest[k-w-1] = pDest[k]; + if(x == TileW-2-1 && y == 0) pDest[k-w+1] = pDest[k]; + if(x == 0 && y == TileH-2-1) pDest[k+w-1] = pDest[k]; + if(x == TileW-2-1 && y == TileH-2-1) pDest[k+w+1] = pDest[k]; + } + } + } + } +} + + +int main(int argc, char **argv) +{ + png_t Png; + CPixel *pBuffer[2] = {0,0}; + + png_init(0, 0); + png_open_file(&Png, argv[1]); + + if(Png.color_type != PNG_TRUECOLOR_ALPHA) + { + dbg_msg("dilate", "not an RGBA image"); + return -1; + } + + int w = Png.width; + int h = Png.height; + + pBuffer[0] = (CPixel*)mem_alloc(w*h*sizeof(CPixel), 1); + pBuffer[1] = (CPixel*)mem_alloc(w*h*sizeof(CPixel), 1); + png_get_data(&Png, (unsigned char *)pBuffer[0]); + png_close_file(&Png); + + TilesetBorderfix(w, h, pBuffer[0], pBuffer[1]); + + // save here + png_open_file_write(&Png, argv[1]); + png_set_data(&Png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]); + png_close_file(&Png); + + return 0; +} |