lundi 27 juin 2016

Why does it gauss blur bmp picture in wrong way?

This program should load a 8 bit bmp picture and blur it, I have no idea what I did wrong here, but result of this blur is not good and program only work on small bitmaps, so propably i made mistake somewhere in this function, but i can't really find this, so thanks a lot for help. I attached picture before and after blur below.

void save_file_bmp(struct FileHeader *fheader, struct MapHeader *mheader, char *input_name, char *output_name, int radius)
{
FILE *fp1, *fp2;
fp1 =fopen(input_name,"rb");
fp2 =fopen(output_name,"wb");

long int i, j, padding, rs;
unsigned long long const int dimensions =mheader->height*mheader->width;
long double mask[dimensions], map[dimensions];
rs = radius*2.57;

//Padding
padding =(mheader->width*3)/4 *4;
//Pixelmap reading
if(fp1)
{
    fseek(fp1, fheader->offset, SEEK_SET);
    for(i =0; i<mheader->height; i++, fseek(fp1, padding, SEEK_CUR))
    {
        for(j =0; j<mheader->width; j++)
        {
            map[i*mheader->height+j]=fgetc(fp1);
        }
    }
}
else
{
    printf("File open error while creating pixelmap");
    exit(1);
}
if(fp2)
{
    long int ix, iy, x, y, tmp, minx, maxx, miny, maxy;
    long double normalization, normalization_value, n_normalization, dsq ,wght;

    fseek(fp1, 0, SEEK_END);
    tmp = ftell(fp1);

    fseek(fp1, 0, SEEK_SET);
    for(i =0; i <tmp; i++)
    {
        fputc(fgetc(fp1), fp2);
    }

    fseek(fp2, fheader->offset, SEEK_SET);

    for(i=0; i<mheader->height; i++)
    {
        for(j=0; j<mheader->width; j++)
        {
            normalization_value =0, n_normalization =0;
            for(iy =i-rs; iy<=i+rs; iy++)
            {
                for(ix =j-rs; ix<=j+rs; ix++)
                {
                    //x
                    if(0<ix)
                    {
                        maxx =ix;
                    }
                    else
                    {
                        maxx =0;
                    }
                    if((mheader->width-1)<maxx)
                    {
                        minx =mheader->width-1;
                    }
                    else
                    {
                        minx =maxx;
                    }
                    //y
                    if(0<iy)
                    {
                        maxy =iy;
                    }
                    else
                    {
                        maxy =0;
                    }
                    if((mheader->height-1)<maxy)
                    {
                        miny =mheader->height-1;
                    }
                    else
                    {
                        miny =maxy;
                    }
                    x =minx;
                    y =miny;
                    dsq =(ix-j)*(ix-j)+(iy-i)*(iy-i);
                    wght =exp(-dsq/(2*radius*radius))/(M_PI*2*radius*radius);
                    normalization_value +=map[y*mheader->szerokosc+x]*wght;
                    n_normalization +=wght;
                }
            }
            normalization =normalization_value/n_normalization;
            mask[i*mheader->width+j] =normalization;
        }
    }
    for(i =0; i<mheader->height; i++, fseek(fp2, padding, SEEK_CUR))
    {
        for(j =0; j<mheader->width; j++)
        {
            map[i*mheader->width+j]=mask[i*mheader->width+j];
            fputc(map[i],fp2);
            fputc(map[i],fp2);
            fputc(map[i],fp2);
        }
    }
}
else
{
    printf("Save file open error");
    exit(1);
}
fclose(fp1);
fclose(fp2);
}

before

after

enter image description here

enter image description here

Aucun commentaire:

Enregistrer un commentaire