diff options
| author | Choupom <andycootlapin@hotmail.fr> | 2011-03-17 17:38:21 +0100 |
|---|---|---|
| committer | oy <Tom_Adams@web.de> | 2011-03-31 22:58:06 +0200 |
| commit | e9ab42795a47dc6fab3112602a85780bcc1aecfb (patch) | |
| tree | 0e8e0f0352f3e632b1114403d58b3a22b727bbbb | |
| parent | 32876b9ba737e6eaf75761889ded51fded8db13a (diff) | |
| download | zcatch-e9ab42795a47dc6fab3112602a85780bcc1aecfb.tar.gz zcatch-e9ab42795a47dc6fab3112602a85780bcc1aecfb.zip | |
refactored dilate
| -rw-r--r-- | bam.lua | 2 | ||||
| -rw-r--r-- | src/tools/dilate.c | 101 | ||||
| -rw-r--r-- | src/tools/dilate.cpp | 89 |
3 files changed, 90 insertions, 102 deletions
diff --git a/bam.lua b/bam.lua index 9b2661f5..fb03fe65 100644 --- a/bam.lua +++ b/bam.lua @@ -231,7 +231,7 @@ function build(settings) tools = {} for i,v in ipairs(tools_src) do toolname = PathFilename(PathBase(v)) - tools[i] = Link(settings, toolname, Compile(settings, v), engine, zlib) + tools[i] = Link(settings, toolname, Compile(settings, v), engine, zlib, pnglite) end -- build client, server, version server and master server diff --git a/src/tools/dilate.c b/src/tools/dilate.c deleted file mode 100644 index 27c32ed2..00000000 --- a/src/tools/dilate.c +++ /dev/null @@ -1,101 +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 int clamp(int v, int min, int max) -{ - if(v > max) - return max; - if(v < min) - return min; - return v; -} - -static void dilate(int w, int h, pixel *src, pixel *dst) -{ - int x, y, k, m, c; - int ix, iy; - const int xo[] = {0, -1, 1, 0}; - const int yo[] = {-1, 0, 0, 1}; - - m = 0; - for(y = 0; y < h; y++) - { - for(x = 0; x < w; x++, m++) - { - dst[m] = src[m]; - if(src[m].a) - continue; - - for(c = 0; c < 4; c++) - { - ix = clamp(x + xo[c], 0, w-1); - iy = clamp(y + yo[c], 0, h-1); - k = iy*w+ix; - if(src[k].a) - { - dst[m] = src[k]; - dst[m].a = 255; - break; - } - } - } - } -} - -static void copy_alpha(int w, int h, pixel *src, pixel *dst) -{ - int x, y, m; - - m = 0; - for(y = 0; y < h; y++) - for(x = 0; x < w; x++, m++) - dst[m].a = src[m].a; -} - -int main(int argc, char **argv) -{ - png_t png; - pixel *buffer[3] = {0,0,0}; - int i, 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; - } - - buffer[0] = (pixel*)malloc(png.width*png.height*sizeof(pixel)); - buffer[1] = (pixel*)malloc(png.width*png.height*sizeof(pixel)); - buffer[2] = (pixel*)malloc(png.width*png.height*sizeof(pixel)); - png_get_data(&png, (unsigned char *)buffer[0]); - png_close_file(&png); - - w = png.width; - h = png.height; - - dilate(w, h, buffer[0], buffer[1]); - for(i = 0; i < 5; i++) - { - dilate(w, h, buffer[1], buffer[2]); - dilate(w, h, buffer[2], buffer[1]); - } - - copy_alpha(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/dilate.cpp b/src/tools/dilate.cpp new file mode 100644 index 00000000..b1b60ac3 --- /dev/null +++ b/src/tools/dilate.cpp @@ -0,0 +1,89 @@ +/* (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 <base/math.h> +#include <engine/external/pnglite/pnglite.h> + +typedef struct +{ + unsigned char r, g, b, a; +} CPixel; + +static void Dilate(int w, int h, CPixel *pSrc, CPixel *pDest) +{ + int ix, iy; + const int xo[] = {0, -1, 1, 0}; + const int yo[] = {-1, 0, 0, 1}; + + int m = 0; + for(int y = 0; y < h; y++) + { + for(int x = 0; x < w; x++, m++) + { + pDest[m] = pSrc[m]; + if(pSrc[m].a) + continue; + + for(int c = 0; c < 4; c++) + { + ix = clamp(x + xo[c], 0, w-1); + iy = clamp(y + yo[c], 0, h-1); + int k = iy*w+ix; + if(pSrc[k].a) + { + pDest[m] = pSrc[k]; + pDest[m].a = 255; + break; + } + } + } + } +} + +static void CopyAlpha(int w, int h, CPixel *pSrc, CPixel *pDest) +{ + int m = 0; + for(int y = 0; y < h; y++) + for(int x = 0; x < w; x++, m++) + pDest[m].a = pSrc[m].a; +} + +int main(int argc, char **argv) +{ + png_t Png; + CPixel *pBuffer[3] = {0,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; + } + + pBuffer[0] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); + pBuffer[1] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); + pBuffer[2] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); + png_get_data(&Png, (unsigned char *)pBuffer[0]); + png_close_file(&Png); + + int w = Png.width; + int h = Png.height; + + Dilate(w, h, pBuffer[0], pBuffer[1]); + for(int i = 0; i < 5; i++) + { + Dilate(w, h, pBuffer[1], pBuffer[2]); + Dilate(w, h, pBuffer[2], pBuffer[1]); + } + + CopyAlpha(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; +} |