void VFilter( const TwoDArray<unsigned char>& pic_data, TwoDArray<unsigned char>& out_data, const OneDArray<int>& filter, const int bits ) { const int offset = (1<<(bits-1)); int sum, j, jout; // Do the first bit for (j=0, jout=0; j<filter.Last(); j+=2, jout++) { for (int i=0; i<pic_data.LengthX(); ++i) { sum = offset; for (int k=filter.Last(); k>=filter.First(); --k) sum += filter[k]*static_cast<int>( pic_data[std::max(j-k,0)][i] ); sum >>= bits; sum = std::min( 255, std::max( 0, sum) ); out_data[jout][i] = static_cast<unsigned char>( sum ); }// i }// j // Do the middle bit for (; j<=pic_data.LastY()+filter.First(); j+=2, jout++) { for (int i=0; i<pic_data.LengthX(); ++i) { sum = offset; for (int k=filter.Last(); k>=filter.First(); --k) sum += filter[k]*static_cast<int>( pic_data[j-k][i] ); sum >>= bits; sum = std::min( 255, std::max( 0, sum) ); out_data[jout][i] = static_cast<unsigned char>( sum ); }// i }// j // Do the last bit for (; j<pic_data.LengthY(); j+=2, jout++) { for (int i=0; i<pic_data.LengthX(); ++i) { sum = offset; for (int k=filter.Last(); k>=filter.First(); --k) sum += filter[k]*static_cast<int>( pic_data[std::min(j-k,pic_data.LastY())][i] ); sum >>= bits; sum = std::min( 255, std::max( 0, sum) ); out_data[jout][i] = static_cast<unsigned char>( sum ); }// i }// j }
void setup2DArray (TwoDArray<int> &arr, int dimx, int dimy, int start_val) { arr.Resize(dimx, dimy); int value =start_val; int err_count = 0; for (int i =arr.FirstY(); i <= arr.LastY(); i++) { for (int j =arr.FirstX(); j <= arr.LastX(); j++) { arr[i][j] = ++value; } } value = start_val; for (int i =arr.FirstY(); i <= arr.LastY(); i++) { for (int j =arr.FirstX(); j <= arr.LastX(); j++) { ++value; if (arr[i][j] != value) err_count++; } } CPPUNIT_ASSERT_EQUAL (err_count, 0); }