2

I tried to create Fill and Eraser Tools by GDI but this way is too slow for windows phone devices and work with large photos with this method is too hard for this divices. I search for Alternative soulation for Image Processing and find Win2D and Sharpdx but not sure These Api can help me for create these tools.

this is Fill tool in winrtxamltoolkit

 public static void FloodFill(this WriteableBitmap target, int x, int y, int outlineColor, int fillColor, byte maxdiff)
    {
        var width = target.PixelWidth;
        var height = target.PixelHeight;
        var queue = new List<Pnt>();

        using (var context = target.GetBitmapContext(ReadWriteMode.ReadWrite))
        {

            queue.Add(new Pnt { X = x, Y = y });

            while (queue.Count > 0)
            {
                var p = queue[queue.Count - 1];
                queue.RemoveAt(queue.Count - 1);
                if (p.X == -1) continue;
                if (p.X == width) continue;
                if (p.Y == -1) continue;
                if (p.Y == height) continue;
                if (context.Pixels[width * p.Y + p.X] == outlineColor) continue;
                if (context.Pixels[width * p.Y + p.X] == fillColor) continue;
                if (context.Pixels[width * p.Y + p.X].MaxDiff(outlineColor) > maxdiff)
                {
                    context.Pixels[width * p.Y + p.X] = fillColor;
                }
                else
                {
                    continue;
                }
                context.Pixels[width * p.Y + p.X] = fillColor;
                queue.Add(new Pnt { X = p.X, Y = p.Y - 1 });
                queue.Add(new Pnt { X = p.X + 1, Y = p.Y });
                queue.Add(new Pnt { X = p.X, Y = p.Y + 1 });
                queue.Add(new Pnt { X = p.X - 1, Y = p.Y });
            }

            target.Invalidate();

        }
    }

and this is Ereaser tool in WriteableBitmapEx

public static void FillEllipseCenteredTrsnceparent(this WriteableBitmap bmp, int xc, int yc, int xr, int yr, Color color)
    {
        using (BitmapContext context = bmp.GetBitmapContext())
        {
            Func<Color, int> toInt32 = c =>
            {
                var i = ((((c.A << 0x18) | (((c.R * c.A + 1) >> 8) << 0x10)) | (((c.G * c.A + 1) >> 8) << 8)) | ((c.B * c.A + 1) >> 8));
                return i;
            };
            int[] pixels = context.Pixels;
            int width = context.Width;
            int height = context.Height;
            if ((xr >= 1) && (yr >= 1))
            {
                int num3;
                int num4;
                int num5;
                int num6;
                int num7;
                int num8;
                int num9 = xr;
                int num10 = 0;
                int num11 = (xr * xr) << 1;
                int num12 = (yr * yr) << 1;
                int num13 = (yr * yr) * (1 - (xr << 1));
                int num14 = xr * xr;
                int num15 = 0;
                int num16 = num12 * xr;
                int num17 = 0;
                //int sa = (color >> 0x18) & 0xff;
                //int sr = (color >> 0x10) & 0xff;
                //int sg = (color >> 8) & 0xff;
                //int sb = color & 0xff;
                //bool flag = !doAlphaBlend || (sa == 0xff);
                while (num16 >= num17)
                {
                    num5 = yc + num10;
                    num6 = yc - num10;
                    if (num5 < 0)
                    {
                        num5 = 0;
                    }
                    if (num5 >= height)
                    {
                        num5 = height - 1;
                    }
                    if (num6 < 0)
                    {
                        num6 = 0;
                    }
                    if (num6 >= height)
                    {
                        num6 = height - 1;
                    }
                    num3 = num5 * width;
                    num4 = num6 * width;
                    num8 = xc + num9;
                    num7 = xc - num9;
                    if (num8 < 0)
                    {
                        num8 = 0;
                    }
                    if (num8 >= width)
                    {
                        num8 = width - 1;
                    }
                    if (num7 < 0)
                    {
                        num7 = 0;
                    }
                    if (num7 >= width)
                    {
                        num7 = width - 1;
                    }
                    for (int i = num7; i <= num8; i++)
                    {
                        pixels[i + num3] = toInt32(color);
                        pixels[i + num4] = toInt32(color);
                    }
                    num10++;
                    num17 += num11;
                    num15 += num14;
                    num14 += num11;
                    if ((num13 + (num15 << 1)) > 0)
                    {
                        num9--;
                        num16 -= num12;
                        num15 += num13;
                        num13 += num12;
                    }
                }
                num9 = 0;
                num10 = yr;
                num5 = yc + num10;
                num6 = yc - num10;
                if (num5 < 0)
                {
                    num5 = 0;
                }
                if (num5 >= height)
                {
                    num5 = height - 1;
                }
                if (num6 < 0)
                {
                    num6 = 0;
                }
                if (num6 >= height)
                {
                    num6 = height - 1;
                }
                num3 = num5 * width;
                num4 = num6 * width;
                num13 = yr * yr;
                num14 = (xr * xr) * (1 - (yr << 1));
                num15 = 0;
                num16 = 0;
                num17 = num11 * yr;
                while (num16 <= num17)
                {
                    num8 = xc + num9;
                    num7 = xc - num9;
                    if (num8 < 0)
                    {
                        num8 = 0;
                    }
                    if (num8 >= width)
                    {
                        num8 = width - 1;
                    }
                    if (num7 < 0)
                    {
                        num7 = 0;
                    }
                    if (num7 >= width)
                    {
                        num7 = width - 1;
                    }
                    for (int j = num7; j <= num8; j++)
                    {
                        pixels[j + num3] = toInt32(color);
                        pixels[j + num4] = toInt32(color);
                    }
                    num9++;
                    num16 += num12;
                    num15 += num13;
                    num13 += num12;
                    if ((num14 + (num15 << 1)) > 0)
                    {
                        num10--;
                        num5 = yc + num10;
                        num6 = yc - num10;
                        if (num5 < 0)
                        {
                            num5 = 0;
                        }
                        if (num5 >= height)
                        {
                            num5 = height - 1;
                        }
                        if (num6 < 0)
                        {
                            num6 = 0;
                        }
                        if (num6 >= height)
                        {
                            num6 = height - 1;
                        }
                        num3 = num5 * width;
                        num4 = num6 * width;
                        num17 -= num11;
                        num15 += num14;
                        num14 += num11;
                    }
                }
            }
        }
    }

Is there a way to conver this code to win2d or Sharpdx?

0 Answers0