示例#1
0
        static int _DilateColor(this IBitmapSampler target, IBitmapSampler mask, float alphaThreshold = 0)
        {
            int count = 0;

            var rows = new Dictionary <int, Vector4[]>();

            for (int y = 0; y < mask.Height; ++y)
            {
                var row = rows[y] = new Vector4[mask.Width];

                for (int x = 0; x < mask.Width; ++x)
                {
                    row[x] = mask._GetDilatedTexel(x, y, alphaThreshold);
                }

                foreach (var k in rows.Keys.Where(item => (y - item) > 2).ToList())
                {
                    count += target._ApplyRow(k, rows[k], true);
                    mask._ApplyRow(k, rows[k], false);
                    rows.Remove(k);
                }
            }

            // apply remaining rows
            foreach (var k in rows.Keys.ToList())
            {
                count += target._ApplyRow(k, rows[k], true);
                mask._ApplyRow(k, rows[k], false);
                rows.Remove(k);
            }

            return(count);
        }
示例#2
0
 private static void _ApplyAlphaPremultiply(this IBitmapSampler source)
 {
     for (int y = 0; y < source.Height; ++y)
     {
         for (int x = 0; x < source.Width; ++x)
         {
             source[x, y] = source[x, y].Premultiply();
         }
     }
 }
示例#3
0
 private static void _FillRGB(this IBitmapSampler source, Vector4 rgba)
 {
     for (int y = 0; y < source.Height; ++y)
     {
         for (int x = 0; x < source.Width; ++x)
         {
             rgba.W       = source[x, y].W;
             source[x, y] = rgba;
         }
     }
 }
示例#4
0
        public _PixelTextureSampler(IBitmapSampler source, bool normalizedUV)
        {
            _Source = source;

            if (normalizedUV)
            {
                _Size  = new SizeF(1, 1);
                _Scale = new Vector2(_Source.Width, source.Height);
            }
            else
            {
                _Size  = new SizeF(_Source.Width, _Source.Height);
                _Scale = Vector2.One;
            }

            _Offset = -Vector2.One / 2;
        }
示例#5
0
        private static int _ApplyRow(this IBitmapSampler target, int y, Vector4[] row, bool ignoreAlpha)
        {
            int count = 0;

            for (int x = 0; x < row.Length; ++x)
            {
                var c = row[x]; if (c.W == 0)
                {
                    continue;
                }

                if (ignoreAlpha)
                {
                    c.W = 0;
                }

                target[x, y] = c;

                ++count;
            }

            return(count);
        }
示例#6
0
 public static bool Contains(this IBitmapSampler image, Point p)
 {
     return(image.Contains(p.X, p.Y));
 }
示例#7
0
 public static bool Contains(this IBitmapSampler texture, int x, int y)
 {
     return(texture.Bounds().Contains(x, y));
 }
示例#8
0
 public static Rectangle Bounds(this IBitmapSampler tex)
 {
     return(new Rectangle(0, 0, tex.Width, tex.Height));
 }
示例#9
0
        /// <summary>
        /// Gets a sampling of the 9 neighbour pixels
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="alphaThreshold"></param>
        /// <returns>
        /// If the central pixel is not transparent, returns the pixel itself, else it returns a sampling.
        /// </returns>
        static Vector4 _GetDilatedTexel(this IBitmapSampler texture, int x, int y, float alphaThreshold = 0)
        {
            if (texture[x, y].W > alphaThreshold)
            {
                return(Vector4.Zero);
            }

            var t1 = texture[x + 1, y];
            var t2 = texture[x - 1, y];
            var t3 = texture[x, y + 1];
            var t4 = texture[x, y - 1];

            if (t1.W <= alphaThreshold &&
                t2.W <= alphaThreshold &&
                t3.W <= alphaThreshold &&
                t4.W <= alphaThreshold
                )
            {
                return(Vector4.Zero);
            }

            var t5 = texture[x - 1, y - 1];
            var t6 = texture[x + 1, y - 1];
            var t7 = texture[x - 1, y + 1];
            var t8 = texture[x + 1, y + 1];

            var c = Vector4.Zero;

            if (t1.W > alphaThreshold)
            {
                c += t1.WithW(1);
            }
            if (t2.W > alphaThreshold)
            {
                c += t2.WithW(1);
            }
            if (t3.W > alphaThreshold)
            {
                c += t3.WithW(1);
            }
            if (t4.W > alphaThreshold)
            {
                c += t4.WithW(1);
            }
            if (t5.W > alphaThreshold)
            {
                c += t5.WithW(1);
            }
            if (t6.W > alphaThreshold)
            {
                c += t6.WithW(1);
            }
            if (t7.W > alphaThreshold)
            {
                c += t7.WithW(1);
            }
            if (t8.W > alphaThreshold)
            {
                c += t8.WithW(1);
            }

            if (c.W > 0)
            {
                c /= c.W; c = c.Saturate();
            }

            return(c);
        }
示例#10
0
 public static ITextureSampler <Vector4> ToTextureSampler(this IBitmapSampler sampler, bool normalizedUV)
 {
     return(new _PixelTextureSampler(sampler, normalizedUV));
 }