views:

205

answers:

1

I'm trying to take this code ...

char mysmooth1_ descr[] = "my smooth1: My smooth1 replaced avg() func. and assign sum to pixel";

void mysmooth1 (int dim, pixel *src, pixel *dst) 
{
    int i, j;
    int ii, jj;
    pixel_ sum sum;
    pixel current_ pixel;

    for (i = 0; i < dim; i++)
 for (j = 0; j < dim; j++)
 {
    initialize_pixel_sum(&sum);
    for(ii = max(i-1, 0); ii <= min(i+1, dim-1); ii++) 
 for(jj = max(j-1, 0); jj <= min(j+1, dim-1); jj++) 
     accumulate_sum(&sum, src[RIDX(ii, jj, dim)]);
 {
    current_ pixel.red = (unsigned short) (sum.red/sum.num);
    current_ pixel.green = (unsigned short) (sum.green/sum.num);
    current_ pixel.blue = (unsigned short) (sum.blue/sum.num);    
    dst[RIDX(i, j, dim)] = current_pixel;

    }
    }
}

and replace the function call to accumulate_ sum, with this code...

static void accumulate_ sum (pixel_sum *sum, pixel p) 

{

    sum->red += (int) p.red;

    sum->green += (int) p.green;

    sum->blue += (int) p.blue;

    sum->num++;

    return;
}

Now the completed code that I came up with, which includes me trying to replace the function call to sum with the actual code of sum looks like this...

char mysmooth2_descr[] = "my smooth2: My smooth1 replaced avg() func. and assign sum to pixel and accumulate sum func.";

void mysmooth2(int dim, pixel *src, pixel *dst) 

{
    int i, j, num;
    int ii, jj;
    pixel_sum sum;
    pixel current_pixel;

    for (i = 0; i < dim; i++)
 for (j = 0; j < dim; j++)
 {
    initialize_pixel_sum(&sum);
    for(ii = max(i-1, 0); ii <= min(i+1, dim-1); ii++) 
 for(jj = max(j-1, 0); jj <= min(j+1, dim-1); jj++) 
    sum.red += (int) p.red;
    sum.green += (int) p.green;
    sum.blue += (int) p.blue;
    sum.num++;
 {
    current_ pixel.red = (unsigned short) (sum.red/sum.num);
    current_ pixel.green = (unsigned short) (sum.green/sum.num);
    current_ pixel.blue = (unsigned short) (sum.blue/sum.num);    
    dst[RIDX(i, j, dim)] = current_pixel;

    }
    }
}

Now I thought I did it right but I keep getting errors which prevent me from compiling the code...If someone could tell me what I am doing wrong that would be great. Am I missing brackets somewhere? Am I supposed to add more code somewhere...any advice or examples which would help me do this in the future would be greatly appreciated. Thank You.

Actually I realize now that I messed something up when trying to replace the function call accumulate_sum with the actual code...any suggestions...maybe its brackets that I screwed up...

+5  A: 

You have indeed screwed up the braces { }, as you would have seen if you run the code through a formatter such as astyle.

void mysmooth2(int dim, pixel *src, pixel *dst)

{
    int i, j, num;
    int ii, jj;
    pixel_sum sum;
    pixel current_pixel;

    for (i = 0; i < dim; i++)
        for (j = 0; j < dim; j++) {
            initialize_pixel_sum(&sum);
            for (ii = max(i - 1, 0); ii <= min(i + 1, dim - 1); ii++)
                for (jj = max(j - 1, 0); jj <= min(j + 1, dim - 1); jj++)
                    sum.red += (int) p.red;
            sum.green += (int) p.green;
            sum.blue += (int) p.blue;
            sum.num++;
            {
                current_ pixel.red = (unsigned short) (sum.red / sum.num);
                current_ pixel.green = (unsigned short) (sum.green / sum.num);
                current_ pixel.blue = (unsigned short) (sum.blue / sum.num);
                dst[RIDX(i, j, dim)] = current_pixel;

            }
        }
}

Until you've a bit of experience, use braces for all the for loops and you might make fewer errors.

Pete Kirkham