Image noise: Difference between revisions

Content added Content deleted
(perfomance improvement too! from 111 fps to 130 fps.)
Line 7: Line 7:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<lang csharp>using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Linq;
using System.Linq;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Windows.Forms;


class Program
class Program
{
{
static int[] colors = { 0, 255 };
static byte[] colors = { 0, 255 };
static Size size = new Size(320, 240);
static Size size = new Size(320, 240);
static Rectangle rectsize = new Rectangle(new Point(0, 0), size);
static Rectangle rectsize = new Rectangle(new Point(0, 0), size);
static int numbytes = size.Width * size.Height * 3;
static int numpixels = size.Width * size.Height;
static int numbytes = numpixels * 3;


static PictureBox pb;
static PictureBox pb;
Line 29: Line 30:
static Random rand = new Random();
static Random rand = new Random();


static IEnumerable<byte> YieldVodoo(int numpixels)
static IEnumerable<byte> YieldVodoo()
{
{
// Yield 3 times same number (i.e 255 255 255) for numpixels/3 times.
// Yield 3 times same number (i.e 255 255 255) for numpixels times.
// Numpixels is number of pixels, it's divided by 3 because it's a RGB image.


for (int i = 0; i < numpixels / 3; i++)
for (int i = 0; i < numpixels; i++)
{
{
var tmp = colors[rand.Next(colors.Length)];
var tmp = colors[rand.Next(2)];


for (int j = 0; j < 3; j++)
for (int j = 0; j < 3; j++)
{
{
yield return (byte)tmp;
yield return tmp;
}
}
}
}
Line 47: Line 47:
static Image Randimg()
static Image Randimg()
{
{
// Low-level bitmaps
// Low-level bitmaps
var bitmap = new Bitmap(size.Width, size.Height);

var bitmap = new Bitmap(size.Width, size.Height);
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
var yielder = YieldVodoo(numbytes);

Marshal.Copy(
Marshal.Copy(
yielder.ToArray<byte>(), // source
YieldVodoo().ToArray<byte>(),// source
0, // start
0, // start
data.Scan0, // scan0 is a pointer to low-level bitmap data
data.Scan0, // scan0 is a pointer to low-level bitmap data
numbytes /* number of bytes to write*/);
numbytes); // number of bytes in source


bitmap.UnlockBits(data);
bitmap.UnlockBits(data);
Line 65: Line 63:
static void Main()
static void Main()
{
{
// TODO: Fix form size.

var form = new Form();
var form = new Form();
form.AutoSize = true;
form.AutoSize = true;
form.Size = new Size(0, 0);
form.Text = "Test";
form.Text = "Test";

form.FormClosed += delegate
form.FormClosed += delegate
{
{
Line 76: Line 75:


worker = new BackgroundWorker();
worker = new BackgroundWorker();

worker.DoWork += delegate
worker.DoWork += delegate
{
{
Line 98: Line 98:
}
}
catch
catch
{
{
// Just pass.
// Just pass.
}
}
}
}
};
};

worker.RunWorkerAsync();
worker.RunWorkerAsync();

FlowLayoutPanel flp = new FlowLayoutPanel();
form.Controls.Add(flp);


pb = new PictureBox();
pb = new PictureBox();
pb.Size = size;
pb.Size = size;
//pb.Dock = DockStyle.Bottom;


form.Controls.Add(pb);
flp.AutoSize = true;
form.Show();
flp.Controls.Add(pb);


form.Show();
Application.Run();
Application.Run();
}
}