PageRenderTime 42ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/AForge.NET/Sources/Imaging/Filters/Base classes/FilterGrayToGray.cs

https://bitbucket.org/VahidN/neural-net-digit-recognition
C# | 154 lines | 48 code | 19 blank | 87 comment | 4 complexity | 07364ea669f53aaf46974555e58ffed3 MD5 | raw file
  1. // AForge Image Processing Library
  2. // AForge.NET framework
  3. //
  4. // Copyright © Andrew Kirillov, 2005-2007
  5. // andrew.kirillov@gmail.com
  6. //
  7. namespace AForge.Imaging.Filters
  8. {
  9. using System;
  10. using System.Drawing;
  11. using System.Drawing.Imaging;
  12. /// <summary>
  13. /// Base class for filtering grayscale images without changing pixel format.
  14. /// </summary>
  15. ///
  16. /// <remarks>The abstract class is the base class for all filters, which can
  17. /// be applied to grayscale images without changing their pixel format and image
  18. /// dimension. The base class is used for filters, which can be applied as
  19. /// directly to the specified image modifying it, as to the specified image
  20. /// returning new image, which represents result of image processing filter.
  21. /// </remarks>
  22. ///
  23. public abstract class FilterGrayToGray : IFilter, IInPlaceFilter
  24. {
  25. /// <summary>
  26. /// Apply filter to an image.
  27. /// </summary>
  28. ///
  29. /// <param name="image">Source image to apply filter to.</param>
  30. ///
  31. /// <returns>Returns filter's result obtained by applying the filter to
  32. /// the source image.</returns>
  33. ///
  34. /// <exception cref="ArgumentException">The source image has incorrect pixel format.</exception>
  35. ///
  36. /// <remarks>The method keeps the source image unchanged and returns the
  37. /// the result of image processing filter as new image.</remarks>
  38. ///
  39. public Bitmap Apply( Bitmap image )
  40. {
  41. // lock source bitmap data
  42. BitmapData srcData = image.LockBits(
  43. new Rectangle( 0, 0, image.Width, image.Height ),
  44. ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed );
  45. // apply the filter
  46. Bitmap dstImage = Apply( srcData );
  47. // unlock source image
  48. image.UnlockBits( srcData );
  49. return dstImage;
  50. }
  51. /// <summary>
  52. /// Apply filter to an image.
  53. /// </summary>
  54. ///
  55. /// <param name="imageData">Source image to apply filter to.</param>
  56. ///
  57. /// <returns>Returns filter's result obtained by applying the filter to
  58. /// the source image.</returns>
  59. ///
  60. /// <exception cref="ArgumentException">The source image has incorrect pixel format.</exception>
  61. ///
  62. /// <remarks>The filter accepts bitmap data as input and returns the result
  63. /// of image processing filter as new image. The source image data are kept
  64. /// unchanged.</remarks>
  65. ///
  66. public Bitmap Apply( BitmapData imageData )
  67. {
  68. if ( imageData.PixelFormat != PixelFormat.Format8bppIndexed )
  69. throw new ArgumentException( "The filter can be applied to graysclae (8bpp indexed) image only" );
  70. // get image dimension
  71. int width = imageData.Width;
  72. int height = imageData.Height;
  73. // create new image
  74. Bitmap dstImage = AForge.Imaging.Image.CreateGrayscaleImage( width, height );
  75. // lock destination bitmap data
  76. BitmapData dstData = dstImage.LockBits(
  77. new Rectangle( 0, 0, width, height ),
  78. ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed );
  79. // copy image
  80. Win32.memcpy( dstData.Scan0, imageData.Scan0, imageData.Stride * height );
  81. // process the filter
  82. ProcessFilter( dstData );
  83. // unlock destination images
  84. dstImage.UnlockBits( dstData );
  85. return dstImage;
  86. }
  87. /// <summary>
  88. /// Apply filter to an image.
  89. /// </summary>
  90. ///
  91. /// <param name="image">Image to apply filter to.</param>
  92. ///
  93. /// <exception cref="ArgumentException">The source image has incorrect pixel format.</exception>
  94. ///
  95. /// <remarks>The method applies the filter directly to the provided
  96. /// image.</remarks>
  97. ///
  98. public void ApplyInPlace( Bitmap image )
  99. {
  100. // lock source bitmap data
  101. BitmapData data = image.LockBits(
  102. new Rectangle( 0, 0, image.Width, image.Height ),
  103. ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed );
  104. // apply the filter
  105. ApplyInPlace( data );
  106. // unlock image
  107. image.UnlockBits( data );
  108. }
  109. /// <summary>
  110. /// Apply filter to an image.
  111. /// </summary>
  112. ///
  113. /// <param name="imageData">Image to apply filter to.</param>
  114. ///
  115. /// <exception cref="ArgumentException">The source image has incorrect pixel format.</exception>
  116. ///
  117. /// <remarks>The method applies the filter directly to the provided
  118. /// image data.</remarks>
  119. ///
  120. public void ApplyInPlace( BitmapData imageData )
  121. {
  122. if ( imageData.PixelFormat != PixelFormat.Format8bppIndexed )
  123. throw new ArgumentException( "The filter can be applied to graysclae (8bpp indexed) image only" );
  124. // process the filter
  125. ProcessFilter( imageData );
  126. }
  127. /// <summary>
  128. /// Process the filter on the specified image.
  129. /// </summary>
  130. ///
  131. /// <param name="imageData">Image data.</param>
  132. ///
  133. protected abstract unsafe void ProcessFilter( BitmapData imageData );
  134. }
  135. }