src/Foundation/Media/Gimela.Media.Imaging/Helpers/DrawingHelper.cs
/// <summary>
/// Draw a line on the specified image.
/// </summary>
/// 
/// <param name="image">Source image to draw on.</param>
/// <param name="point1">The first point to connect.</param>
/// <param name="point2">The second point to connect.</param>
/// <param name="color">Line's color.</param>
/// 
/// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
/// 
Draw a line on the specified image.
Draw a line on the specified image .
public static unsafe void Line(UnmanagedImage image, IntPoint point1, IntPoint point2, Color color)
    {
      CheckPixelFormat(image.PixelFormat);
      int pixelSize = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
      int imageWidth = image.Width;
      int imageHeight = image.Height;
      int stride = image.Stride;
      if (
          ((point1.X < 0) && (point2.X < 0)) ||
          ((point1.Y < 0) && (point2.Y < 0)) ||
          ((point1.X >= imageWidth) && (point2.X >= imageWidth)) ||
          ((point1.Y >= imageHeight) && (point2.Y >= imageHeight)))
      {
        return;
      }
      CheckEndPoint(imageWidth, imageHeight, point1, ref point2);
      CheckEndPoint(imageWidth, imageHeight, point2, ref point1);
      if (
          ((point1.X < 0) && (point2.X < 0)) ||
          ((point1.Y < 0) && (point2.Y < 0)) ||
          ((point1.X >= imageWidth) && (point2.X >= imageWidth)) ||
          ((point1.Y >= imageHeight) && (point2.Y >= imageHeight)))
      {
        return;
      }
      int startX = point1.X;
      int startY = point1.Y;
      int stopX = point2.X;
      int stopY = point2.Y;
      byte gray = 0;
      if (image.PixelFormat == PixelFormat.Format8bppIndexed)
      {
        gray = (byte)(0.2125 * color.R + 0.7154 * color.G + 0.0721 * color.B);
      }
      int dx = stopX - startX;
      int dy = stopY - startY;
      if (Math.Abs(dx) >= Math.Abs(dy))
      {
        float slope = (dx != 0) ? (float)dy / dx : 0;
        int step = (dx > 0) ? 1 : -1;
        dx += step;
        if (image.PixelFormat == PixelFormat.Format8bppIndexed)
        {
          for (int x = 0; x != dx; x += step)
          {
            int px = startX + x;
            int py = (int)((float)startY + (slope * (float)x));
            byte* ptr = (byte*)image.ImageData.ToPointer() + py * stride + px;
            *ptr = gray;
          }
        }
        else
        {
          for (int x = 0; x != dx; x += step)
          {
            int px = startX + x;
            int py = (int)((float)startY + (slope * (float)x));
            byte* ptr = (byte*)image.ImageData.ToPointer() + py * stride + px * pixelSize;
            ptr[RGB.R] = color.R;
            ptr[RGB.G] = color.G;
            ptr[RGB.B] = color.B;
          }
        }
      }
      else
      {
        float slope = (dy != 0) ? (float)dx / dy : 0;
        int step = (dy > 0) ? 1 : -1;
        dy += step;
        if (image.PixelFormat == PixelFormat.Format8bppIndexed)
        {
          for (int y = 0; y != dy; y += step)
          {
            int px = (int)((float)startX + (slope * (float)y));
            int py = startY + y;
            byte* ptr = (byte*)image.ImageData.ToPointer() + py * stride + px;
            *ptr = gray;
          }
        }
        else
        {
          for (int y = 0; y != dy; y += step)
          {
            int px = (int)((float)startX + (slope * (float)y));
            int py = startY + y;
            byte* ptr = (byte*)image.ImageData.ToPointer() + py * stride + px * pixelSize;
            ptr[RGB.R] = color.R;
            ptr[RGB.G] = color.G;
            ptr[RGB.B] = color.B;
          }
        }
      }
    }
public static unsafe void Line ( UnmanagedImage image , IntPoint point1 , IntPoint point2 , Color color ) { CheckPixelFormat ( image . PixelFormat ) ; int pixelSize = System . Drawing . Image . GetPixelFormatSize ( image . PixelFormat ) / 8 ; int imageWidth = image . Width ; int imageHeight = image . Height ; int stride = image . Stride ; if ( ( ( point1 . X < 0 ) && ( point2 . X < 0 ) ) || ( ( point1 . Y < 0 ) && ( point2 . Y < 0 ) ) || ( ( point1 . X >= imageWidth ) && ( point2 . X >= imageWidth ) ) || ( ( point1 . Y >= imageHeight ) && ( point2 . Y >= imageHeight ) ) ) { return ; } CheckEndPoint ( imageWidth , imageHeight , point1 , ref point2 ) ; CheckEndPoint ( imageWidth , imageHeight , point2 , ref point1 ) ; if ( ( ( point1 . X < 0 ) && ( point2 . X < 0 ) ) || ( ( point1 . Y < 0 ) && ( point2 . Y < 0 ) ) || ( ( point1 . X >= imageWidth ) && ( point2 . X >= imageWidth ) ) || ( ( point1 . Y >= imageHeight ) && ( point2 . Y >= imageHeight ) ) ) { return ; } int startX = point1 . X ; int startY = point1 . Y ; int stopX = point2 . X ; int stopY = point2 . Y ; byte gray = 0 ; if ( image . PixelFormat == PixelFormat . Format8bppIndexed ) { gray = ( byte ) ( 0.2125 * color . R + 0.7154 * color . G + 0.0721 * color . B ) ; } int dx = stopX - startX ; int dy = stopY - startY ; if ( Math . Abs ( dx ) >= Math . Abs ( dy ) ) { float slope = ( dx != 0 ) ? ( float ) dy / dx : 0 ; int step = ( dx > 0 ) ? 1 : - 1 ; dx += step ; if ( image . PixelFormat == PixelFormat . Format8bppIndexed ) { for ( int x = 0 ; x != dx ; x += step ) { int px = startX + x ; int py = ( int ) ( ( float ) startY + ( slope * ( float ) x ) ) ; byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + py * stride + px ; * ptr = gray ; } } else { for ( int x = 0 ; x != dx ; x += step ) { int px = startX + x ; int py = ( int ) ( ( float ) startY + ( slope * ( float ) x ) ) ; byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + py * stride + px * pixelSize ; ptr [ RGB . R ] = color . R ; ptr [ RGB . G ] = color . G ; ptr [ RGB . B ] = color . B ; } } } else { float slope = ( dy != 0 ) ? ( float ) dx / dy : 0 ; int step = ( dy > 0 ) ? 1 : - 1 ; dy += step ; if ( image . PixelFormat == PixelFormat . Format8bppIndexed ) { for ( int y = 0 ; y != dy ; y += step ) { int px = ( int ) ( ( float ) startX + ( slope * ( float ) y ) ) ; int py = startY + y ; byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + py * stride + px ; * ptr = gray ; } } else { for ( int y = 0 ; y != dy ; y += step ) { int px = ( int ) ( ( float ) startX + ( slope * ( float ) y ) ) ; int py = startY + y ; byte * ptr = ( byte * ) image . ImageData . ToPointer ( ) + py * stride + px * pixelSize ; ptr [ RGB . R ] = color . R ; ptr [ RGB . G ] = color . G ; ptr [ RGB . B ] = color . B ; } } } }
Draw a line on the specified image.
Draw a line on the specified image .
// TODO: faster line drawing algorithm may be implemented with integer math // image dimension // check if there is something to draw // nothing to draw // check again if there is something to draw // nothing to draw // compute pixel for grayscale image // draw the line // the line is more horizontal, we'll plot along the X axis // correct dx so last point is included as well // grayscale image // color image // the line is more vertical, we'll plot along the y axis. // correct dy so last point is included as well // grayscale image // color image
The source image has incorrect pixel format.
The source image has incorrect pixel format .
image
UnmanagedImage
Source image to draw on.
Source image to draw on .
point1
IntPoint
The first point to connect.
The first point to connect .
point2
IntPoint
The second point to connect.
The second point to connect .
color
Color
None