src/Foundation/Media/Gimela.Media.Imaging/Filters/Morphology/SpecificOptimizations/BinaryDilatation3x3.cs
/// <summary>
/// Process the filter on the specified image.
/// </summary>
/// 
/// <param name="sourceData">Source image data.</param>
/// <param name="destinationData">Destination image data.</param>
/// <param name="rect">Image rectangle for processing by the filter.</param>
/// 
/// <exception cref="InvalidImagePropertiesException">Processing rectangle mast be at least 3x3 in size.</exception>
/// 
Process the filter on the specified image.
Process the filter on the specified image .
protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData, Rectangle rect)
    {
      if ((rect.Width < 3) || (rect.Height < 3))
      {
        throw new InvalidImagePropertiesException("Processing rectangle mast be at least 3x3 in size.");
      }
      int startX = rect.Left + 1;
      int startY = rect.Top + 1;
      int stopX = rect.Right - 1;
      int stopY = rect.Bottom - 1;
      int dstStride = destinationData.Stride;
      int srcStride = sourceData.Stride;
      int dstOffset = dstStride - rect.Width + 1;
      int srcOffset = srcStride - rect.Width + 1;
      byte* src = (byte*)sourceData.ImageData.ToPointer();
      byte* dst = (byte*)destinationData.ImageData.ToPointer();
      src += (startX - 1) + (startY - 1) * srcStride;
      dst += (startX - 1) + (startY - 1) * dstStride;
      *dst = (byte)(*src | src[1] | src[srcStride] | src[srcStride + 1]);
      src++;
      dst++;
      for (int x = startX; x < stopX; x++, src++, dst++)
      {
        *dst = (byte)(*src | src[-1] | src[1] |
            src[srcStride] | src[srcStride - 1] | src[srcStride + 1]);
      }
      *dst = (byte)(*src | src[-1] | src[srcStride] | src[srcStride - 1]);
      src += srcOffset;
      dst += dstOffset;
      for (int y = startY; y < stopY; y++)
      {
        *dst = (byte)(*src | src[1] |
            src[-srcStride] | src[-srcStride + 1] |
            src[srcStride] | src[srcStride + 1]);
        src++;
        dst++;
        for (int x = startX; x < stopX; x++, src++, dst++)
        {
          *dst = (byte)(*src | src[-1] | src[1] |
              src[-srcStride] | src[-srcStride - 1] | src[-srcStride + 1] |
              src[srcStride] | src[srcStride - 1] | src[srcStride + 1]);
        }
        *dst = (byte)(*src | src[-1] |
            src[-srcStride] | src[-srcStride - 1] |
            src[srcStride] | src[srcStride - 1]);
        src += srcOffset;
        dst += dstOffset;
      }
      *dst = (byte)(*src | src[1] | src[-srcStride] | src[-srcStride + 1]);
      src++;
      dst++;
      for (int x = startX; x < stopX; x++, src++, dst++)
      {
        *dst = (byte)(*src | src[-1] | src[1] |
            src[-srcStride] | src[-srcStride - 1] | src[-srcStride + 1]);
      }
      *dst = (byte)(*src | src[-1] | src[-srcStride] | src[-srcStride - 1]);
    }
protected override unsafe void ProcessFilter ( UnmanagedImage sourceData , UnmanagedImage destinationData , Rectangle rect ) { if ( ( rect . Width < 3 ) || ( rect . Height < 3 ) ) { throw new InvalidImagePropertiesException ( " " ) ; } int startX = rect . Left + 1 ; int startY = rect . Top + 1 ; int stopX = rect . Right - 1 ; int stopY = rect . Bottom - 1 ; int dstStride = destinationData . Stride ; int srcStride = sourceData . Stride ; int dstOffset = dstStride - rect . Width + 1 ; int srcOffset = srcStride - rect . Width + 1 ; byte * src = ( byte * ) sourceData . ImageData . ToPointer ( ) ; byte * dst = ( byte * ) destinationData . ImageData . ToPointer ( ) ; src += ( startX - 1 ) + ( startY - 1 ) * srcStride ; dst += ( startX - 1 ) + ( startY - 1 ) * dstStride ; * dst = ( byte ) ( * src | src [ 1 ] | src [ srcStride ] | src [ srcStride + 1 ] ) ; src ++ ; dst ++ ; for ( int x = startX ; x < stopX ; x ++ , src ++ , dst ++ ) { * dst = ( byte ) ( * src | src [ - 1 ] | src [ 1 ] | src [ srcStride ] | src [ srcStride - 1 ] | src [ srcStride + 1 ] ) ; } * dst = ( byte ) ( * src | src [ - 1 ] | src [ srcStride ] | src [ srcStride - 1 ] ) ; src += srcOffset ; dst += dstOffset ; for ( int y = startY ; y < stopY ; y ++ ) { * dst = ( byte ) ( * src | src [ 1 ] | src [ - srcStride ] | src [ - srcStride + 1 ] | src [ srcStride ] | src [ srcStride + 1 ] ) ; src ++ ; dst ++ ; for ( int x = startX ; x < stopX ; x ++ , src ++ , dst ++ ) { * dst = ( byte ) ( * src | src [ - 1 ] | src [ 1 ] | src [ - srcStride ] | src [ - srcStride - 1 ] | src [ - srcStride + 1 ] | src [ srcStride ] | src [ srcStride - 1 ] | src [ srcStride + 1 ] ) ; } * dst = ( byte ) ( * src | src [ - 1 ] | src [ - srcStride ] | src [ - srcStride - 1 ] | src [ srcStride ] | src [ srcStride - 1 ] ) ; src += srcOffset ; dst += dstOffset ; } * dst = ( byte ) ( * src | src [ 1 ] | src [ - srcStride ] | src [ - srcStride + 1 ] ) ; src ++ ; dst ++ ; for ( int x = startX ; x < stopX ; x ++ , src ++ , dst ++ ) { * dst = ( byte ) ( * src | src [ - 1 ] | src [ 1 ] | src [ - srcStride ] | src [ - srcStride - 1 ] | src [ - srcStride + 1 ] ) ; } * dst = ( byte ) ( * src | src [ - 1 ] | src [ - srcStride ] | src [ - srcStride - 1 ] ) ; }
Process the filter on the specified image.
Process the filter on the specified image .
// processing start and stop X,Y positions // image pointers // allign pointers by X and Y // --- process the first line // for each pixel // --- process all lines except the last one // for each pixel // --- process the last line // for each pixel
Processing rectangle mast be at least 3x3 in size.
Processing rectangle mast be at least 3x3 in size .
sourceData
UnmanagedImage
Source image data.
Source image data .
destinationData
UnmanagedImage
Destination image data.
Destination image data .
rect
Rectangle
Image rectangle for processing by the filter.
Image rectangle for processing by the filter .