FloodFiller.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Copyright (c) 2014, Lars Brubaker, Kevin Pope
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. The views and conclusions contained in the software and documentation are those
  22. of the authors and should not be interpreted as representing official policies,
  23. either expressed or implied, of the FreeBSD Project.
  24. */
  25. using MatterHackers.Agg.Image;
  26. namespace MatterHackers.Agg
  27. {
  28. public class FloodFill
  29. {
  30. protected byte[] destBuffer = null;
  31. protected int imageStride = 0;
  32. protected bool[] pixelsChecked;
  33. private ImageBuffer destImage;
  34. private FillingRule fillRule;
  35. private FirstInFirstOutQueue<Range> ranges = new FirstInFirstOutQueue<Range>(9);
  36. public FloodFill(Color fillColor)
  37. {
  38. fillRule = new ExactMatch(fillColor);
  39. }
  40. public FloodFill(Color fillColor, int tolerance0To255)
  41. {
  42. if (tolerance0To255 > 0)
  43. {
  44. fillRule = new ToleranceMatch(fillColor, tolerance0To255);
  45. }
  46. else
  47. {
  48. fillRule = new ExactMatch(fillColor);
  49. }
  50. }
  51. public FloodFill(FillingRule fillRule)
  52. {
  53. this.fillRule = fillRule;
  54. }
  55. public void Fill(ImageBuffer bufferToFillOn, int x, int y)
  56. {
  57. unchecked // this way we can overflow the uint on negative and get a big number
  58. {
  59. if ((uint)x > bufferToFillOn.Width || (uint)y > bufferToFillOn.Height)
  60. {
  61. return;
  62. }
  63. }
  64. destImage = bufferToFillOn;
  65. imageStride = destImage.StrideInBytes();
  66. destBuffer = destImage.GetBuffer();
  67. int imageWidth = destImage.Width;
  68. int imageHeight = destImage.Height;
  69. pixelsChecked = new bool[destImage.Width * destImage.Height];
  70. int startColorBufferOffset = destImage.GetBufferOffsetXY(x, y);
  71. fillRule.SetStartColor(new Color(destImage.GetBuffer()[startColorBufferOffset + 2], destImage.GetBuffer()[startColorBufferOffset + 1], destImage.GetBuffer()[startColorBufferOffset]));
  72. LinearFill(x, y);
  73. while (ranges.Count > 0)
  74. {
  75. Range range = ranges.Dequeue();
  76. int downY = range.y - 1;
  77. int upY = range.y + 1;
  78. int downPixelOffset = (imageWidth * (range.y - 1)) + range.startX;
  79. int upPixelOffset = (imageWidth * (range.y + 1)) + range.startX;
  80. for (int rangeX = range.startX; rangeX <= range.endX; rangeX++)
  81. {
  82. if (range.y > 0)
  83. {
  84. if (!pixelsChecked[downPixelOffset])
  85. {
  86. int bufferOffset = destImage.GetBufferOffsetXY(rangeX, downY);
  87. if (fillRule.CheckPixel(destBuffer, bufferOffset))
  88. {
  89. LinearFill(rangeX, downY);
  90. }
  91. }
  92. }
  93. if (range.y < (imageHeight - 1))
  94. {
  95. if (!pixelsChecked[upPixelOffset])
  96. {
  97. int bufferOffset = destImage.GetBufferOffsetXY(rangeX, upY);
  98. if (fillRule.CheckPixel(destBuffer, bufferOffset))
  99. {
  100. LinearFill(rangeX, upY);
  101. }
  102. }
  103. }
  104. upPixelOffset++;
  105. downPixelOffset++;
  106. }
  107. }
  108. }
  109. private void LinearFill(int x, int y)
  110. {
  111. int bytesPerPixel = destImage.GetBytesBetweenPixelsInclusive();
  112. int imageWidth = destImage.Width;
  113. int leftFillX = x;
  114. int bufferOffset = destImage.GetBufferOffsetXY(x, y);
  115. int pixelOffset = (imageWidth * y) + x;
  116. while (true)
  117. {
  118. fillRule.SetPixel(destBuffer, bufferOffset);
  119. pixelsChecked[pixelOffset] = true;
  120. leftFillX--;
  121. pixelOffset--;
  122. bufferOffset -= bytesPerPixel;
  123. if (leftFillX <= 0 || (pixelsChecked[pixelOffset]) || !fillRule.CheckPixel(destBuffer, bufferOffset))
  124. {
  125. break;
  126. }
  127. }
  128. leftFillX++;
  129. int rightFillX = x;
  130. bufferOffset = destImage.GetBufferOffsetXY(x, y);
  131. pixelOffset = (imageWidth * y) + x;
  132. while (true)
  133. {
  134. fillRule.SetPixel(destBuffer, bufferOffset);
  135. pixelsChecked[pixelOffset] = true;
  136. rightFillX++;
  137. pixelOffset++;
  138. bufferOffset += bytesPerPixel;
  139. if (rightFillX >= imageWidth || pixelsChecked[pixelOffset] || !fillRule.CheckPixel(destBuffer, bufferOffset))
  140. {
  141. break;
  142. }
  143. }
  144. rightFillX--;
  145. ranges.Enqueue(new Range(leftFillX, rightFillX, y));
  146. }
  147. private struct Range
  148. {
  149. public int endX;
  150. public int startX;
  151. public int y;
  152. public Range(int startX, int endX, int y)
  153. {
  154. this.startX = startX;
  155. this.endX = endX;
  156. this.y = y;
  157. }
  158. }
  159. public class ExactMatch : FillingRule
  160. {
  161. public ExactMatch(Color fillColor)
  162. : base(fillColor)
  163. {
  164. }
  165. public override bool CheckPixel(byte[] destBuffer, int bufferOffset)
  166. {
  167. return (destBuffer[bufferOffset] == startColor.red) &&
  168. (destBuffer[bufferOffset + 1] == startColor.green) &&
  169. (destBuffer[bufferOffset + 2] == startColor.blue);
  170. }
  171. }
  172. public abstract class FillingRule
  173. {
  174. protected Color fillColor;
  175. protected Color startColor;
  176. protected FillingRule(Color fillColor)
  177. {
  178. this.fillColor = fillColor;
  179. }
  180. public abstract bool CheckPixel(byte[] destBuffer, int bufferOffset);
  181. public virtual void SetPixel(byte[] destBuffer, int bufferOffset)
  182. {
  183. destBuffer[bufferOffset] = fillColor.blue;
  184. destBuffer[bufferOffset + 1] = fillColor.green;
  185. destBuffer[bufferOffset + 2] = fillColor.red;
  186. }
  187. public void SetStartColor(Color startColor)
  188. {
  189. this.startColor = startColor;
  190. }
  191. }
  192. public class ToleranceMatch : FillingRule
  193. {
  194. private int tolerance0To255;
  195. public ToleranceMatch(Color fillColor, int tolerance0To255)
  196. : base(fillColor)
  197. {
  198. this.tolerance0To255 = tolerance0To255;
  199. }
  200. public override bool CheckPixel(byte[] destBuffer, int bufferOffset)
  201. {
  202. return (destBuffer[bufferOffset] >= (startColor.red - tolerance0To255)) && destBuffer[bufferOffset] <= (startColor.red + tolerance0To255) &&
  203. (destBuffer[bufferOffset + 1] >= (startColor.green - tolerance0To255)) && destBuffer[bufferOffset + 1] <= (startColor.green + tolerance0To255) &&
  204. (destBuffer[bufferOffset + 2] >= (startColor.blue - tolerance0To255)) && destBuffer[bufferOffset + 2] <= (startColor.blue + tolerance0To255);
  205. }
  206. }
  207. }
  208. }