/*
    Halftonee nin birusti
*/

#include <math.h>
#include "halftone.h"

#define SIZEE 12
#define HDIST 9
#define VDIST 9
#define SAMPL 2
#define VER (256/(SIZEE/2))

static unsigned int dist[SIZEE*SIZEE];
static int yk,xk;

void halftone_init(int xs,int ys)
{
    int x,y,dx,dy,tmp=SIZEE/2;
    double d;

    yk=ys;
    xk=xs;
    for(y=0;y<SIZEE;y++)
        for(x=0;x<SIZEE;x++)
        {
            dx=x-SIZEE/2;
            dy=y-SIZEE/2;
            d=sqrt(dx*dx+dy*dy);
            if(d>SIZEE/2+0.5)
                dist[y*SIZEE+x]=-1;
            else
            {
                dist[y*SIZEE+x]=(int)((d+0.25)/tmp*256.0);
                //if(dist[y][x]==0)
                //    dist[y][x]=(256/SIZEE)/2;
            }
        }
}

void halftone(buftype *dest,unsigned *src,int inten)
{
    int x,y,px,py,rx,ry,d,n,c,samples,i,j,sum,
        vuaro=0,tau[256];
    unsigned char cp[4],*dp,varm[256];
    unsigned tmp[4];

    for(n=0;n<256;n++)
        tau[n]=((VER-n)<<8)/VER;
    for(n=0;n<256;n++)
    {
        if((n*inten>>8)>255)
            varm[n]=255;
        else
            varm[n]=n*inten>>8;
    }

    for(y=0;y<yk;y+=VDIST)
    {
        for(x=vuaro*HDIST/2;x<xk;x+=HDIST)
        {
            samples=0;
            tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
            for(py=0;py<SAMPL;py++)
            {
                ry=y+(SIZEE/SAMPL)*py-SIZEE/2;
                if(ry<0) continue;
                if(ry>=yk) break;
                for(px=0;px<SAMPL;px++)
                {
                    rx=x+(SIZEE/SAMPL)*px-SIZEE/2;
                    if(rx<0) continue;
                    if(rx>=xk) break;
                    dp=(unsigned char *)&src[ry*xk+rx];
                    tmp[0]+=dp[0];
                    tmp[1]+=dp[1];
                    tmp[2]+=dp[2];
                    tmp[3]+=dp[3];
                    samples++;
                }
            }
#ifdef QUICKTONE
            cp[0]=tmp[0]/(SAMPL*SAMPL);
            cp[1]=tmp[1]/(SAMPL*SAMPL);
            cp[2]=tmp[2]/(SAMPL*SAMPL);
            cp[3]=tmp[3]/(SAMPL*SAMPL);

            sum=cp[0]+cp[1]+cp[2]+cp[3];
            sum>>=2;
#else
            cp[0]=tmp[0]/samples;
            cp[1]=tmp[1]/samples;
            cp[2]=tmp[2]/samples;
            cp[3]=tmp[3]/samples;

            sum=cp[0]+cp[1]+cp[2]+cp[3];
            sum>>=2;
#endif

#ifdef HICOLOR
            unsigned short couleur;

            couleur=(cp[0]>>3)+(cp[1]>>2<<5)+(cp[2]>>3<<11);
#endif

            for(py=j=0;py<SIZEE;py++,j+=SIZEE)
            {
                ry=py+y-SIZEE/2;
                if(ry<0) continue;
                if(ry>=yk) break;

                i=ry*xk+x-SIZEE/2;
                for(px=0;px<SIZEE;px++,i++)
                {
                    rx=px+x-SIZEE/2;
                    if(rx<0) continue;
                    if(rx>=xk) break;

                    if((d=dist[j+px])!=-1)
                    {
#ifdef HICOLOR
                        c=varm[sum];
                        if(d<c+VER)
                            dest[i]=couleur;
#else
#ifdef QUICKTONE
                        dp=(unsigned char *)&dest[i];
                        c=varm[sum];
                        if(d<c+VER)
                        {
                            dp[0]=cp[0];
                            dp[1]=cp[1];
                            dp[2]=cp[2];
                            dp[3]=cp[3];
                        }
#else
                        dp=(unsigned char *)&dest[i];
                        for(n=0;n<4;n++)
                        {
                            c=varm[cp[n]];
                            if(d<c && c>SIZEE/2)
                                dp[n]=255;
                            else
                            {
                                if(d<c+VER && c>SIZEE/2)
                                {
                                    c=tau[d-c];
                                    if(dp[n]+c<256)
                                        dp[n]+=c;
                                    else
                                        dp[n]=255;
                                }
                            }
                        }
#endif
#endif
                    }
                }
            }
        }
        vuaro=1-vuaro;
    }
}
