void HFilter( const TwoDArray<unsigned char>& pic_data, TwoDArray<unsigned char>& out_data, const OneDArray<int>& filter, const int bits ) { unsigned char* line_data; const int offset = (1<<(bits-1)); int sum; int i, iout; for (int j=0; j<pic_data.LengthY(); ++j) { line_data = out_data[j]; // Do the first bit for (i=0, iout=0; i<filter.Last(); i+=2, iout++) { sum = offset; for (int k=filter.Last(); k>=filter.First(); --k) sum += filter[k]*static_cast<int>(pic_data[j][std::max(i-k,0)]); sum >>= bits; sum = std::min( 255, std::max( 0, sum) ); line_data[iout] = static_cast<unsigned char>( sum ); }// i // Do the middle bit for (; i<=pic_data.LastX()+filter.First(); i+=2, iout++) { sum = offset; for (int k=filter.Last(); k>=filter.First(); --k) sum += filter[k]*static_cast<int>( pic_data[j][i-k] ); sum >>= bits; sum = std::min( 255, std::max( 0, sum) ); line_data[iout] = static_cast<unsigned char>( sum ); }// i // Do the last bit for (; i<pic_data.LengthX(); i+=2, iout++) { sum = offset; for (int k=filter.Last(); k>=filter.First(); --k) sum += filter[k]*static_cast<int>( pic_data[j][std::min(i-k,pic_data.LastX())] ); sum >>= bits; sum = std::min( 255, std::max( 0, sum) ); line_data[iout] = 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); }