2009-10-04 130 views
-2

我想利用這個代碼...c函數調用

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; 

    } 
    } 
} 

並替換函數調用accumulate_總和,與此代碼...

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; 
} 

現在完成的代碼我想出了,其中包括我試圖取代函數調用總和的實際代碼總和看起來像這樣...

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; 

    } 
    } 
} 

現在,我認爲我做對了,但我不斷收到錯誤,這阻止了我編寫代碼......如果有人能告訴我我做錯了什麼,那會很好。我在某處丟失括號嗎?我是否應該在某處添加更多的代碼......任何能夠幫助我在未來完成此任務的建議或示例都將不勝感激。謝謝。

其實我現在認識到,我弄亂的東西了試圖與實際的代碼替換函數調用時accumulate_sum ...任何建議...也許它的支架是我搞砸了......

+0

我不想用內聯函數我只是想用實際的代碼替換函數調用accumulate_sum ...... – 2009-10-04 09:03:10

+2

正確縮進代碼後,就容易多了,看問題 – 2009-10-04 09:10:11

+0

@約翰爲什麼好奇你不想使用內聯。恕我直言內聯結合速度優勢和更好的可讀性? – fvu 2009-10-04 09:18:50

回答

5

確實這樣擰上大括號{},就像您通過格式化程序(如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; 

      } 
     } 
} 

直到你有一些經驗,對所有for循環使用花括號,並且可能會減少錯誤。