Mulit-dimensional array edge/border conditions

Posted by kirbuchi on Stack Overflow See other posts from Stack Overflow or by kirbuchi
Published on 2010-05-31T06:39:05Z Indexed on 2010/05/31 6:42 UTC
Read the original article Hit count: 256

Hi, I'm iterating over a 3 dimensional array (which is an image with 3 values for each pixel) to apply a 3x3 filter to each pixel as follows:

//For each value on the image    
for (i=0;i<3*width*height;i++){
    //For each filter value
 for (j=0;j<9;j++){
          if (notOutsideEdgesCondition){
             *(**(outArray)+i)+= *(**(pixelArray)+i-1+(j%3)) * (*(filter+j));
          } 
    }
}

I'm using pointer arithmetic because if I used array notation I'd have 4 loops and I'm trying to have the least possible number of loops. My problem is my notOutsideEdgesCondition is getting quite out of hands because I have to consider 8 border cases. I have the following handled conditions

  • Left Column: ((i%width)==0) && (j%3==0)
  • Right Column: ((i-1)%width ==0) && (i>1) && (j%3==2)
  • Upper Row: (i<width) && (j<2)
  • Lower Row: (i>(width*height-width)) && (j>5)

and still have to consider the 4 corner cases which will have longer expressions. At this point I've stopped and asked myself if this is the best way to go because If I have a 5 line long conditional evaluation it'll not only be truly painful to debug but will slow the inner loop. That's why I come to you to ask if there's a known algorithm to handle this cases or if there's a better approach for my problem. Thanks a lot.

© Stack Overflow or respective owner

Related posts about c

    Related posts about multidimensional-array