void edgeDetectSobel( const ChannelT<T> &srcChannel, const Area &srcArea, const Vec2i &dstLT, ChannelT<T> *dstChannel ) { std::pair<Area,Vec2i> srcDst = clippedSrcDst( srcChannel.getBounds(), srcArea, dstChannel->getBounds(), dstLT ); const Area &area( srcDst.first ); const Vec2i &dstOffset( srcDst.second ); typename CHANTRAIT<T>::Sum sumX, sumY; int32_t srcRowBytes = srcChannel.getRowBytes(); int8_t srcPixelBytes = srcChannel.getIncrement() * sizeof(T); int8_t dstPixelBytes = dstChannel->getIncrement() * sizeof(T); const T maxValue = CHANTRAIT<T>::max(); for( int32_t y = 1; y < area.getHeight() - 1; ++y ) { const uint8_t *srcLine = reinterpret_cast<const uint8_t*>( srcChannel.getData( area.getX1() + 1, area.getY1() + y ) ); uint8_t *dstLine = reinterpret_cast<uint8_t*>( dstChannel->getData( dstOffset.x + area.getX1() + 1, dstOffset.y + y ) ); for( int32_t x = area.getX1() + 1; x < area.getX2() - 1; ++x ) { // sumX = -srcLine[-srcRowPixels-srcPixelStride] + srcLine[-srcRowPixels+srcPixelStride] - 2 * srcLine[-srcPixelStride] + 2 * srcLine[srcPixelStride] - srcLine[srcRowPixels-srcPixelStride] + srcLine[srcRowPixels+srcPixelStride]; sumX = -*(T*)(srcLine-srcRowBytes-srcPixelBytes) + *(T*)(srcLine-srcRowBytes+srcPixelBytes) - 2 * *(T*)(srcLine-srcPixelBytes) + 2 * *(T*)(srcLine+srcPixelBytes) - *(T*)(srcLine+srcRowBytes-srcPixelBytes) + *(T*)(srcLine+srcRowBytes+srcPixelBytes); // sumY = srcLine[-srcRowPixels-srcPixelStride] + 2 * srcLine[-srcRowPixels] + srcLine[-srcRowPixels + srcPixelStride] - srcLine[srcRowPixels-srcPixelStride] - 2 * srcLine[srcRowPixels] - srcLine[srcRowPixels+srcPixelStride]; sumY = *(T*)(srcLine-srcRowBytes-srcPixelBytes) + 2 * *(T*)(srcLine-srcRowBytes) + *(T*)(srcLine-srcRowBytes+srcPixelBytes) - *(T*)(srcLine+srcRowBytes-srcPixelBytes) - 2 * *(T*)(srcLine+srcPixelBytes) - *(T*)(srcLine+srcRowBytes+srcPixelBytes); sumX = static_cast<typename CHANTRAIT<T>::Sum>( math<float>::sqrt( float( sumX * sumX + sumY * sumY ) ) ); if( sumX > maxValue ) sumX = maxValue; *dstLine = static_cast<uint8_t>( sumX ); dstLine += dstPixelBytes; srcLine += srcPixelBytes; } } }
void calculateIntegralImage( const ChannelT<T> &channel, typename CHANTRAIT<T>::Accum *integralImage ) { int32_t imageWidth = channel.getWidth(), imageHeight = channel.getHeight(); ptrdiff_t srcRowBytes = channel.getRowBytes(); uint8_t srcInc = channel.getIncrement(); const T *src = channel.getData(); /*for( int32_t i = 0; i < imageWidth; i++ ) { // reset this column sum typename CHANTRAIT<T>::Accum sum = 0; for( int32_t j = 0; j < imageHeight; j++ ) { uint32_t index = j * imageWidth + i; sum += src[j*srcRowBytes+i*srcInc]; if( i == 0 ) integralImage[index] = sum; else integralImage[index] = integralImage[index-1] + sum; } }*/ for( int32_t j = 0; j < imageHeight; j++ ) { // reset this column sum typename CHANTRAIT<T>::Accum sum = 0; for( int32_t i = 0; i < imageWidth; i++ ) { uint32_t index = j * imageWidth + i; sum += src[j*srcRowBytes+i*srcInc]; if( j == 0 ) integralImage[index] = sum; else integralImage[index] = integralImage[index-imageWidth] + sum; } } }
ImageSourceChannel( const ChannelT<T> &channel ) : ImageSource() { mWidth = channel.getWidth(); mHeight = channel.getHeight(); setColorModel( ImageIo::CM_GRAY ); setChannelOrder( ImageIo::Y ); if( channel.getIncrement() != 1 ) setCustomPixelInc( channel.getIncrement() ); if( boost::is_same<T,uint8_t>::value ) { setDataType( ImageIo::UINT8 ); mChannel8u = *reinterpret_cast<const Channel8u*>( &channel ); // register reference to 'channel' } else if( boost::is_same<T,uint16_t>::value ) { setDataType( ImageIo::UINT16 ); mChannel16u = *reinterpret_cast<const Channel16u*>( &channel ); // register reference to 'channel' } else if( boost::is_same<T,float>::value ) { setDataType( ImageIo::FLOAT32 ); mChannel32f = *reinterpret_cast<const Channel32f*>( &channel ); // register reference to 'channel' } else throw; // this channel seems to be a type we've never met mRowBytes = channel.getRowBytes(); mData = reinterpret_cast<const uint8_t*>( channel.getData() ); }
void ChannelT<T>::copyFrom( const ChannelT<T> &srcChannel, const Area &srcArea, const ivec2 &relativeOffset ) { std::pair<Area,ivec2> srcDst = clippedSrcDst( srcChannel.getBounds(), srcArea, getBounds(), srcArea.getUL() + relativeOffset ); int32_t srcRowBytes = srcChannel.getRowBytes(); uint8_t srcIncrement = srcChannel.getIncrement(); uint8_t increment = mIncrement; int32_t width = srcDst.first.getWidth(); for( int32_t y = 0; y < srcArea.getHeight(); ++y ) { const T *src = reinterpret_cast<const T*>( reinterpret_cast<const uint8_t*>( srcChannel.mData + srcArea.x1 * srcIncrement ) + ( srcArea.y1 + y ) * srcRowBytes ); T *dst = reinterpret_cast<T*>( reinterpret_cast<uint8_t*>( mData + srcDst.second.x * mIncrement ) + ( y + srcDst.second.y ) * mRowBytes ); for( int32_t x = 0; x < width; ++x ) { *dst = *src; src += srcIncrement; dst += increment; } } }