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 flipVertical( const ChannelT<T> &srcChannel, ChannelT<T> *destChannel ) { std::pair<Area,ivec2> srcDst = clippedSrcDst( srcChannel.getBounds(), destChannel->getBounds(), destChannel->getBounds(), ivec2(0,0) ); if( srcChannel.isPlanar() && destChannel->isPlanar() ) { // both channels are planar, so do a series of memcpy()'s const int32_t srcPixelInc = srcChannel.getIncrement(); const size_t copyBytes = srcDst.first.getWidth() * srcPixelInc * sizeof(T); for( int32_t y = 0; y < srcDst.first.getHeight(); ++y ) { const T *srcPtr = srcChannel.getData( ivec2( 0, y ) ); T *dstPtr = destChannel->getData( ivec2( 0, srcDst.first.getHeight() - y - 1 ) ); memcpy( dstPtr, srcPtr, copyBytes ); } } else { const int8_t srcInc = srcChannel.getIncrement(); const int8_t destInc = destChannel->getIncrement(); const int32_t width = srcDst.first.getWidth(); for( int y = 0; y < srcDst.first.getHeight(); ++y ) { const T* src = srcChannel.getData( 0, y ); T* dest = destChannel->getData( 0, srcDst.first.getHeight() - 1 - y ); for ( int x = 0; x < width; ++x ) { *dest = *src; src += srcInc; dest += destInc; } } } }
void thresholdImpl( const ChannelT<T> &srcChannel, T value, const Area &srcArea, const ivec2 &dstLT, ChannelT<T> *dstChannel ) { std::pair<Area,ivec2> srcDst = clippedSrcDst( srcChannel.getBounds(), srcArea, dstChannel->getBounds(), dstLT ); const Area &area( srcDst.first ); const ivec2 &dstOffset( srcDst.second ); uint8_t srcInc = srcChannel.getIncrement(); uint8_t dstInc = dstChannel->getIncrement(); const T maxValue = CHANTRAIT<T>::max(); for( int32_t y = 0; y < area.getHeight(); ++y ) { T *dstPtr = dstChannel->getData( ivec2( area.getX1(), y ) + dstOffset ); const T *srcPtr = srcChannel.getData( ivec2( area.getX1(), y ) ); for( int32_t x = area.getX1(); x < area.getX2(); ++x ) { *dstPtr = ( *srcPtr > value ) ? maxValue : 0; dstPtr += dstInc; srcPtr += srcInc; } } }
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; } } }
void threshold( const ChannelT<T> &srcChannel, T value, ChannelT<T> *dstChannel ) { thresholdImpl( srcChannel, value, srcChannel.getBounds(), ivec2(), dstChannel ); }
void edgeDetectSobel( const ChannelT<T> &srcChannel, ChannelT<T> *dstChannel ) { edgeDetectSobel( srcChannel, srcChannel.getBounds(), Vec2i::zero(), dstChannel ); }