GetPixel() public method

public GetPixel ( int x, int y ) : uint
x int
y int
return uint
Exemplo n.º 1
0
        public void IsRowBlank()
        {
            Assert.IsFalse(src.IsBlank(0.05));

            for (int j = 0; j < src.Height; j++)
            {
                for (int i = 0; i < src.Width; i++)
                {
                    var clr = src.GetPixel(i, j);

                    src.SetPixel(i, j, Color.FromArgb(0, clr.R, clr.G, clr.B));
                }
            }

            Assert.IsTrue(src.IsBlank(0.05));
        }
Exemplo n.º 2
0
            private ColorHLS GetColorAt(int x, int y)
            {
                ColorHLS color;

                _pixBuffer.GetPixel(x, y, out color);
                return(color);
            }
        private static void RasterizeBlockingSplines(TerrainComponent terrain, TerrainVegetationComponent component, PixelBuffer mask, int maskChannel, float terrainOffset)
        {
            foreach (var spline in component.BlockingSplines)
            {
                var vertices = new FastList <Splines.VertexPositionNormalTangentColorTexture>();
                var indices  = new FastList <int>();
                Splines.SplineMeshBuilder.CreateSplineMesh(spline, vertices, indices);

                for (var i = 0; i < indices.Count; i += 3)
                {
                    var vt1f = vertices[indices[i + 0]].Position;
                    var vt2f = vertices[indices[i + 1]].Position;
                    var vt3f = vertices[indices[i + 2]].Position;

                    vt1f = (vt1f + terrainOffset) / terrain.Size * mask.Width;
                    vt2f = (vt2f + terrainOffset) / terrain.Size * mask.Width;
                    vt3f = (vt3f + terrainOffset) / terrain.Size * mask.Width;

                    // Just ignore Y axis, works well enoguh for splines but if we
                    // support generic volumes in the future then we might want to project it a bit more properly
                    // or maybe just do an offscreen render pass on the gpu ...
                    var vt1 = new Int2((int)vt1f.X, (int)vt1f.Z);
                    var vt2 = new Int2((int)vt2f.X, (int)vt2f.Z);
                    var vt3 = new Int2((int)vt3f.X, (int)vt3f.Z);

                    // Calculate bounding box
                    var maxX = Math.Max(vt1.X, Math.Max(vt2.X, vt3.X));
                    var minX = Math.Min(vt1.X, Math.Min(vt2.X, vt3.X));
                    var maxY = Math.Max(vt1.Y, Math.Max(vt2.Y, vt3.Y));
                    var minY = Math.Min(vt1.Y, Math.Min(vt2.Y, vt3.Y));

                    // Triangle intersection
                    var vs1 = vt2 - vt1;
                    var vs2 = vt3 - vt1;

                    for (var x = minX; x <= maxX; x++)
                    {
                        for (var y = minY; y < maxY; y++)
                        {
                            var q = new Int2(x - vt1.X, y - vt1.Y);

                            var s = (float)CrossProduct(q, vs2) / CrossProduct(vs1, vs2);
                            var t = (float)CrossProduct(vs1, q) / CrossProduct(vs1, vs2);

                            if (s >= 0 && t >= 0 && (s + t) <= 1)
                            {
                                var currentPixel = mask.GetPixel <Color>(x, y);
                                currentPixel[maskChannel] = 0;

                                mask.SetPixel <Color>(x, y, currentPixel);
                            }
                        }
                    }
                }
            }
        }
 private static void PrepareHeightMap8Bit(MyHeightmapFace map, PixelBuffer imageData)
 {
     for (int y = 0; y < map.Resolution; y++)
     {
         for (int x = 0; x < map.Resolution; x++)
         {
             map.SetValue(x, y, (ushort)(imageData.GetPixel <byte>(x, y) * 256));
         }
     }
 }
Exemplo n.º 5
0
        public HeightDataSource(PixelBuffer pixelBuffer)
        {
            Columns = pixelBuffer.Width;
            Rows    = pixelBuffer.Height;
            heights = new float[pixelBuffer.Width * pixelBuffer.Height];

            foreach (var coord in GetCoordinates())
            {
                heights[coord.index] = pixelBuffer.GetPixel <Color>(coord.x, coord.y).ToRgb();
            }
        }
Exemplo n.º 6
0
        void Mouse_MouseMove(InputEventArgs e)
        {
            Color clr;
            Point pt = new Point(e.MousePosition.X - imageLocation.X,
                                 e.MousePosition.Y - imageLocation.Y);

            if (buffer.IsPointValid(pt) == false)
            {
                frm.lblPixelColor.Text = "No Pixel";
                return;
            }

            if (Mouse.Buttons[Mouse.MouseButtons.Primary])
            {
                // do a circle of radius 3
                for (int y = -3; y <= 3; y++)
                {
                    for (int x = -3; x <= 3; x++)
                    {
                        // if we're out of the circle radius, go to the next iteration.
                        if (x * x + y * y > 9)
                        {
                            continue;
                        }

                        Point newpt = new Point(pt.X + x, pt.Y + y);

                        if (newpt.X < 0 || newpt.X >= buffer.Width)
                        {
                            continue;
                        }
                        if (newpt.Y < 0 || newpt.Y >= buffer.Height)
                        {
                            continue;
                        }

                        buffer.SetPixel(newpt.X, newpt.Y, Color.FromArgb(frm.btnColor.BackColor.ToArgb()));
                    }
                }
                image.WritePixels(buffer);
            }

            clr = buffer.GetPixel(e.MousePosition.X - imageLocation.X,
                                  e.MousePosition.Y - imageLocation.Y);

            frm.lblPixelColor.Text =
                string.Format("R: {0}  G: {1}\r\nB: {2}  A: {3}",
                              FormatComponent(clr.R), FormatComponent(clr.G),
                              FormatComponent(clr.B), FormatComponent(clr.A));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates a normal map by interpreting the pixel data in the passed buffer
        /// as a height map.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="normalStr">The weight for the x-y components of the normals in
        /// the normal map.  Smaller values make the normal map more subtle.
        /// Values in the range 0.5f to 3.0f are reasonable.</param>
        /// <returns></returns>
        public static PixelBuffer NormalMapFromHeightMap(PixelBuffer buffer, float normalStr)
        {
            int[,] heights = new int[buffer.Width, buffer.Height];

            for (int j = 0; j < buffer.Height; j++)
            {
                for (int i = 0; i < buffer.Width; i++)
                {
                    heights[i, j] = (int)(buffer.GetPixel(i, j).Intensity *short.MaxValue);
                }
            }

            PixelBuffer result = new PixelBuffer(buffer.PixelFormat, buffer.Size);

            int[,] square = new int[3, 3];

            for (int j = 0; j < result.Height; j++)
            {
                for (int i = 0; i < result.Width; i++)
                {
                    GetSquare(square, heights, i, j);

                    // sobel operator:
                    int diffx = square[0, 0] - square[2, 0] +
                                2 * (square[0, 1] - square[2, 1]) +
                                square[0, 2] - square[2, 0];

                    int diffy = square[0, 0] + 2 * square[1, 0] + square[0, 2] +
                                -square[0, 2] - 2 * square[1, 2] - square[2, 2];

                    Vector3f vec = new Vector3f(diffx / (float)short.MaxValue, diffy / (float)short.MaxValue, 0);
                    vec  *= normalStr;
                    vec.Z = 1;

                    vec    = vec.Normalize();
                    vec    = vec / 2;
                    vec.X += 0.5f; vec.Y += 0.5f; vec.Z += 0.5f;
                    Color clr = Color.FromRgb((int)(vec.X * 255),
                                              (int)(vec.Y * 255),
                                              (int)(vec.Z * 255));

                    result.SetPixel(i, j, clr);
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        private static void PrepareHeightMap8Bit(MyHeightmapFace map, PixelBuffer imageData)
        {
            int y = 0;

            while (y < map.Resolution)
            {
                int x = 0;
                while (true)
                {
                    if (x >= map.Resolution)
                    {
                        y++;
                        break;
                    }
                    map.SetValue(x, y, (ushort)(imageData.GetPixel <byte>(x, y) * 0x100));
                    x++;
                }
            }
        }
Exemplo n.º 9
0
        private void VerifyCopyResult(PixelBuffer result, PixelBuffer srcBuffer, Point destPt)
        {
            for (int j = 0; j < srcBuffer.Height; j++)
            {
                for (int i = 0; i < srcBuffer.Width; i++)
                {
                    int x = i + destPt.X;
                    int y = j + destPt.Y;

                    var resultPx = result.GetPixel(x, y);
                    var srcPx    = srcBuffer.GetPixel(i, j);

                    if (resultPx.A == 0 && srcPx.A == 0)
                    {
                        continue;
                    }

                    Assert.AreEqual(srcPx, resultPx);
                }
            }
        }
 private static void PrepareHeightMap8Bit(MyHeightmapFace map, PixelBuffer imageData)
 {
     for (int y = 0; y < map.Resolution; y++)
     {
         for (int x = 0; x < map.Resolution; x++)
         {
             map.SetValue(x, y, (ushort)(imageData.GetPixel<byte>(x, y) * 256));
         }
     }
 }
Exemplo n.º 11
0
        private void FillVertices(Vector3[] vertices, Vector3[] normal, Vector2[] texture, short[] indices)
        {
            for (int j = 0; j < pixels.Height; j++)
            {
                for (int i = 0; i < pixels.Width; i++)
                {
                    Color  clr  = pixels.GetPixel(i, j);
                    double peak = clr.Intensity;

                    vertices[i + j * pixels.Width] = new Vector3(
                        i / (float)pixels.Width * Width,
                        j / (float)pixels.Height * Height,
                        peak * MaxPeak);

                    texture[i + j * pixels.Width] = new Vector2(
                        i / (float)pixels.Width,
                        j / (float)pixels.Height);
                }
            }

            int index = 0;

            for (int j = 0; j < pixels.Height - 1; j++)
            {
                for (int i = 0; i < pixels.Width - 1; i++)
                {
                    indices[index++] = (short)(i + j * pixels.Width);
                    indices[index++] = (short)(i + 1 + j * pixels.Width);
                    indices[index++] = (short)(i + 1 + (j + 1) * pixels.Width);
                    indices[index++] = (short)(i + j * pixels.Width);
                    indices[index++] = (short)(i + 1 + (j + 1) * pixels.Width);
                    indices[index++] = (short)(i + (j + 1) * pixels.Width);

                    Vector3 n1 = Vector3.CrossProduct(
                        vertices[(i + 1) + j * pixels.Width] - vertices[i + j * pixels.Width],
                        vertices[(i + 1) + (j + 1) * pixels.Width] - vertices[i + j * pixels.Width]);

                    Vector3 n2 = Vector3.CrossProduct(
                        vertices[i + (j + 1) * pixels.Width] - vertices[i + j * pixels.Width],
                        vertices[(i + 1) + (j + 1) * pixels.Width] - vertices[i + j * pixels.Width]);

                    normal[i + j * pixels.Width]           += n1;
                    normal[i + 1 + j * pixels.Width]       += n1;
                    normal[i + 1 + (j + 1) * pixels.Width] += n1;

                    normal[i + j * pixels.Width]           += n2;
                    normal[i + (j + 1) * pixels.Width]     += n2;
                    normal[i + 1 + (j + 1) * pixels.Width] += n2;
                }
            }

            for (int i = 0; i < normal.Length; i++)
            {
                normal[i] = normal[i].Normalize();
            }

            for (int j = 1; j < pixels.Height - 1; j++)
            {
                for (int i = 1; i < pixels.Width - 1; i++)
                {
                    normal[i + j * pixels.Width] = new Vector3(
                        i / (float)pixels.Width * Width,
                        j / (float)pixels.Height * Height,
                        pixels.GetPixel(i, j).Intensity *MaxPeak).Normalize();
                }
            }
        }
Exemplo n.º 12
0
        private static void PostProcessFont(BitmapFontOptions options, System.Drawing.Bitmap bmp)
        {
            if (options.EdgeOptions == BitmapFontEdgeOptions.None)
            {
                return;
            }

            Drawing.Imaging.BitmapData data = bmp.LockBits(
                new Drawing.Rectangle(Drawing.Point.Empty, bmp.Size),
                Drawing.Imaging.ImageLockMode.ReadWrite, Drawing.Imaging.PixelFormat.Format32bppArgb);

            PixelFormat bitmapFormat = PixelFormat.BGRA8888;

            PixelBuffer buffer = new PixelBuffer(bitmapFormat, Interop.Convert(bmp.Size),
                                                 data.Scan0, bitmapFormat, data.Stride);

            // now convert pixels to gray scale.
            for (int j = 0; j < buffer.Height; j++)
            {
                for (int i = 0; i < buffer.Width; i++)
                {
                    Color clr = buffer.GetPixel(i, j);
                    if (clr.ToArgb() == 0)
                    {
                        continue;
                    }

                    int  alpha     = clr.A;
                    int  intensity = (int)Math.Round(0.30 * clr.R + 0.59 * clr.G + 0.11 * clr.B);
                    byte value     = (byte)intensity;

                    System.Diagnostics.Debug.Assert(0 <= intensity && intensity <= 255);
                    if (intensity < 0)
                    {
                        intensity = 0;
                    }
                    if (intensity > 255)
                    {
                        intensity = 255;
                    }


                    switch (options.EdgeOptions)
                    {
                    case BitmapFontEdgeOptions.IntensityAlphaWhite:
                        clr = Color.FromArgb(value, Color.White);
                        break;

                    case BitmapFontEdgeOptions.IntensityAlphaColor:
                        clr = Color.FromArgb(value, clr);
                        break;
                    }

                    buffer.SetPixel(i, j, clr);
                }
            }


            System.Runtime.InteropServices.Marshal.Copy(
                buffer.Data, 0, data.Scan0, buffer.Data.Length);

            bmp.UnlockBits(data);
        }
Exemplo n.º 13
0
        public void Run(string[] args)
        {
            using (new DisplayWindowBuilder(args)
                   .BackbufferSize(800, 600)
                   .QuitOnClose()
                   .Build())
            {
                PixelBuffer pbMaskBg     = PixelBuffer.FromFile("mask_bg-bricks.png");
                PixelBuffer pbBg         = PixelBuffer.FromFile("bg-bricks.png");
                PixelBuffer pbMaskCircle = PixelBuffer.FromFile("mask_circle.png");

                Surface surfRealBg = new Surface(pbMaskBg.Size);


                for (int x = 0; x < pbMaskBg.Width; x++)
                {
                    for (int y = 0; y < pbMaskBg.Height; y++)
                    {
                        if (pbMaskBg.GetPixel(x, y) == Color.FromArgb(255, 0, 0, 0))
                        {
                            pbBg.SetPixel(x, y, Color.FromArgb(0, 0, 0, 0));
                        }
                    }
                }

                surfRealBg.WritePixels(pbBg);

                bool mouseDown = false;

                Input.Unhandled.MouseDown += (sender, e) =>
                {
                    if (e.MouseButton == MouseButton.Primary)
                    {
                        mouseDown = true;
                    }
                };
                Input.Unhandled.MouseUp += (sender, e) =>
                {
                    if (e.MouseButton == MouseButton.Primary)
                    {
                        mouseDown = false;
                    }
                };
                Input.Unhandled.MouseMove += (sender, e) => mouse = e.MousePosition;

                while (AgateApp.IsAlive)
                {
                    Display.CurrentWindow.Title = Display.FramesPerSecond.ToString();
                    Display.BeginFrame();

                    if (Input.Unhandled.Keys[KeyCode.Escape])
                    {
                        return;
                    }

                    if (mouseDown)
                    {
                        int mX = mouse.X;
                        int mY = mouse.Y;

                        Rectangle rect = new Rectangle(mX, mY, pbMaskCircle.Width, pbMaskCircle.Height);
                        Point     p    = new Point(mX, mY);

                        for (int x = 0; x < pbMaskCircle.Width; x++)
                        {
                            if (mX + x >= pbBg.Width)
                            {
                                break;
                            }

                            for (int y = 0; y < pbMaskCircle.Height; y++)
                            {
                                if (mY + y >= pbBg.Height)
                                {
                                    break;
                                }

                                if (pbMaskCircle.GetPixel(x, y) == Color.FromArgb(255, 0, 0, 0))
                                {
                                    pbBg.SetPixel(mX + x, mY + y, Color.FromArgb(0, 0, 0, 0));
                                }
                            }
                        }

                        surfRealBg.WritePixels(pbBg, rect, p);
                    }

                    Display.Clear(Color.Blue);
                    surfRealBg.Draw();

                    Display.EndFrame();
                    AgateApp.KeepAlive();
                }
            }
        }
Exemplo n.º 14
0
 private ColorHLS GetColorAt(int x, int y)
 {
     return(new ColorHLS(_pixBuffer.GetPixel(x, y)));
 }