种子漫水填充,但是重复油漆桶的过程中,出现漏填,代码没错

nabasasun 2013-10-24 01:03:52

编译成DEBUG的时候没有问题,关键是Release版本

下面是图片和代码








public Bitmap FloodFill(Bitmap src, Point location, Color fillColor, int threshould)
{
try
{
Bitmap a = new Bitmap(src);
int w = a.Width;
int h = a.Height;

Stack<Point> fillPoints = new Stack<Point>(w * h);

System.Drawing.Imaging.BitmapData bmpData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

IntPtr ptr = bmpData.Scan0;
int stride = bmpData.Stride;
int bytes = bmpData.Stride * a.Height;



int r = fillColor.R;
int g=fillColor.G;
int b=fillColor.B;


byte[] grayValues = new byte[bytes];

System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte[] temp = (byte[])grayValues.Clone();

Color backColor = Color.FromArgb(temp[location.X * 3 + 2 + location.Y * stride], temp[location.X * 3 + 1 + location.Y * stride], temp[location.X * 3 + location.Y * stride]);

int gray = (int)((backColor.R + backColor.G + backColor.B) / 3);
if (location.X < 0 || location.X >= w || location.Y < 0 || location.Y >= h) return null;
fillPoints.Push(new Point(location.X, location.Y));

int[,] mask = new int[w, h];






while (fillPoints.Count > 0)
{
Point p = fillPoints.Pop();
mask[p.X, p.Y] = 1;
temp[3 * p.X + p.Y * stride] = (byte)fillColor.B;
temp[3 * p.X + 1 + p.Y * stride] = (byte)fillColor.G;
temp[3 * p.X + 2 + p.Y * stride] = (byte)fillColor.R;

if (p.X > 0 && (Math.Abs(gray - (int)((temp[3 * (p.X - 1) + p.Y * stride] + temp[3 * (p.X - 1) + 1 + p.Y * stride] + temp[3 * (p.X - 1) + 2 + p.Y * stride]) / 3)) < threshould) && (mask[p.X - 1, p.Y] != 1))
{
temp[3 * (p.X - 1) + p.Y * stride] = (byte)fillColor.B;
temp[3 * (p.X - 1) + 1 + p.Y * stride] = (byte)fillColor.G;
temp[3 * (p.X - 1) + 2 + p.Y * stride] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X - 1, p.Y));
mask[p.X - 1, p.Y] = 1;
}

if (p.X < w - 1 && (Math.Abs(gray - (int)((temp[3 * (p.X + 1) + p.Y * stride] + temp[3 * (p.X + 1) + 1 + p.Y * stride] + temp[3 * (p.X + 1) + 2 + p.Y * stride]) / 3)) < threshould) && (mask[p.X + 1, p.Y] != 1))
{
temp[3 * (p.X + 1) + p.Y * stride] = (byte)fillColor.B;
temp[3 * (p.X + 1) + 1 + p.Y * stride] = (byte)fillColor.G;
temp[3 * (p.X + 1) + 2 + p.Y * stride] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X + 1, p.Y));
mask[p.X + 1, p.Y] = 1;
}

if (p.Y > 0 && (Math.Abs(gray - (int)((temp[3 * p.X + (p.Y - 1) * stride] + temp[3 * p.X + 1 + (p.Y - 1) * stride] + temp[3 * p.X + 2 + (p.Y - 1) * stride]) / 3)) < threshould) && (mask[p.X, p.Y - 1] != 1))
{
temp[3 * p.X + (p.Y - 1) * stride] = (byte)fillColor.B;
temp[3 * p.X + 1 + (p.Y - 1) * stride] = (byte)fillColor.G;
temp[3 * p.X + 2 + (p.Y - 1) * stride] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X, p.Y - 1));
mask[p.X, p.Y - 1] = 1;
}

if (p.Y < h - 1 && (Math.Abs(gray - (int)((temp[3 * p.X + (p.Y + 1) * stride] + temp[3 * p.X + 1 + (p.Y + 1) * stride] + temp[3 * p.X + 2 + (p.Y + 1) * stride]) / 3)) < threshould) && (mask[p.X, p.Y + 1] != 1))
{
temp[3 * p.X + (p.Y + 1) * stride] = (byte)fillColor.B;
temp[3 * p.X + 1 + (p.Y + 1) * stride] = (byte)fillColor.G;
temp[3 * p.X + 2 + (p.Y + 1) * stride] = (byte)fillColor.R;
fillPoints.Push(new Point(p.X, p.Y + 1));
mask[p.X, p.Y + 1] = 1;
}
}




fillPoints.Clear();
grayValues = (byte[])temp.Clone();
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
a.UnlockBits(bmpData);
return a;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
return null;
}
}

...全文
252 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nabasasun 2013-10-25
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
经验表明,debug能正常,release不正常,一定和指针操作、内存拷贝有关。
我也问过C++的,C++的内存拷贝是下面的 memcpy C#的是 System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
threenewbee 2013-10-24
  • 打赏
  • 举报
回复
引用 5 楼 nabasasun 的回复:
[quote=引用 4 楼 jiaoshiyao 的回复:] 这种东西你必须自己找 能够问的都是技能点 你这个属于不能问的范畴内
那什么东西可以问[/quote] 问当然可以问,你就是问如何能中500万大奖也未尝不可。他的意思是,如果你提问方式不对,回答你问题的人就比较少,回答你问题的针对性也不强,那么你获得满意答案就更费劲。所以提问要注意策略。直接丢出一段代码就显得不太明智。
threenewbee 2013-10-24
  • 打赏
  • 举报
回复
经验表明,debug能正常,release不正常,一定和指针操作、内存拷贝有关。
nabasasun 2013-10-24
  • 打赏
  • 举报
回复
引用 4 楼 jiaoshiyao 的回复:
这种东西你必须自己找 能够问的都是技能点 你这个属于不能问的范畴内
那什么东西可以问
jiaoshiyao 2013-10-24
  • 打赏
  • 举报
回复
这种东西你必须自己找 能够问的都是技能点 你这个属于不能问的范畴内
jiaoshiyao 2013-10-24
  • 打赏
  • 举报
回复
偶也没心情看了!
huwei001982 2013-10-24
  • 打赏
  • 举报
回复
引用 1 楼 wddw1986 的回复:
要有多么大闲心的人会去研究你的代码...
哈哈
cheng2005 2013-10-24
  • 打赏
  • 举报
回复
要有多么大闲心的人会去研究你的代码...

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧