+
+static void encodeMonochromeImage(RGBA*data, int width, int height, RGBA*colors)
+{
+ int t;
+ int len = width*height;
+
+ U32* img = (U32*)data;
+ U32 color1 = img[0];
+ U32 color2 = 0;
+ for(t=1;t<len;t++) {
+ if(img[t] != color1) {
+ color2 = img[t];
+ break;
+ }
+ }
+ *(U32*)&colors[0] = color1;
+ *(U32*)&colors[1] = color2;
+ for(t=0;t<len;t++) {
+ if(img[t] == color1) {
+ img[t] = 0;
+ } else {
+ img[t] = 0xffffffff;
+ }
+ }
+}
+
+static void decodeMonochromeImage(RGBA*data, int width, int height, RGBA*colors)
+{
+ int t;
+ int len = width*height;
+
+ for(t=0;t<len;t++) {
+ U32 m = data[t].r;
+ data[t].r = (colors[0].r * (255-m) + colors[1].r * m) >> 8;
+ data[t].g = (colors[0].g * (255-m) + colors[1].g * m) >> 8;
+ data[t].b = (colors[0].b * (255-m) + colors[1].b * m) >> 8;
+ data[t].a = (colors[0].a * (255-m) + colors[1].a * m) >> 8;
+ }
+}
+
+static void blurImage(RGBA*src, int width, int height, int r)
+{
+ int e = 2; // r times e is the sampling interval
+ double*gauss = (double*)malloc(r*e*sizeof(double));
+ double sum=0;
+ int x;
+ for(x=0;x<r*e;x++) {
+ double t = (x - r*e/2.0)/r;
+ gauss[x] = exp(-0.5*t*t);
+ sum += gauss[x];
+ }
+ int*weights = (int*)malloc(r*e*sizeof(int));
+ for(x=0;x<r*e;x++) {
+ weights[x] = (int)(gauss[x]*65536.0001/sum);
+ }
+ int range = r*e/2;
+
+ RGBA*tmp = malloc(sizeof(RGBA)*width*height);
+
+ int y;
+ for(y=0;y<height;y++) {
+ RGBA*s = &src[y*width];
+ RGBA*d = &tmp[y*width];
+ for(x=0;x<range;x++) {
+ d[x] = s[x];
+ }
+ for(x=range;x<width-range;x++) {
+ int r=0;
+ int g=0;
+ int b=0;
+ int a=0;
+ int*f = weights;
+ int xx;
+ for(xx=x-range;xx<x+range;xx++) {
+ r += s[xx].r * f[0];
+ g += s[xx].g * f[0];
+ b += s[xx].b * f[0];
+ a += s[xx].a * f[0];
+ f++;
+ }
+ d[x].r = r >> 16;
+ d[x].g = g >> 16;
+ d[x].b = b >> 16;
+ d[x].a = a >> 16;
+ }
+ for(x=width-range;x<width;x++) {
+ d[x] = s[x];
+ }
+ }
+
+ for(x=0;x<width;x++) {
+ RGBA*s = &tmp[x];
+ RGBA*d = &src[x];
+ int yy=0;
+ for(y=0;y<range;y++) {
+ d[yy] = s[yy];
+ yy+=width;
+ }
+ for(y=range;y<height-range;y++) {
+ int r=0;
+ int g=0;
+ int b=0;
+ int a=0;
+ int*f = weights;
+ int cy,cyy=yy-range*width;
+ for(cy=y-range;cy<y+range;cy++) {
+ r += s[cyy].r * f[0];
+ g += s[cyy].g * f[0];
+ b += s[cyy].b * f[0];
+ a += s[cyy].a * f[0];
+ cyy += width;
+ f++;
+ }
+ d[yy].r = r >> 16;
+ d[yy].g = g >> 16;
+ d[yy].b = b >> 16;
+ d[yy].a = a >> 16;
+ yy += width;
+ }
+ for(y=0;y<range;y++) {
+ d[yy] = s[yy];
+ yy += width;
+ }
+ }
+
+ free(tmp);
+ free(weights);
+ free(gauss);
+}
+
+