Image noise: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Processing)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 435:
glutMainLoop();
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
Max 185 FPS on .NET 4.0/Windows 7 64-bit on Athlon II X4 620 - ATI Radeon X1200.
 
<lang csharp>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
class Program
{
static Size size = new Size(320, 240);
static Rectangle rectsize = new Rectangle(new Point(0, 0), size);
static int numpixels = size.Width * size.Height;
static int numbytes = numpixels * 3;
 
static PictureBox pb;
static BackgroundWorker worker;
 
static double time = 0;
static double frames = 0;
static Random rand = new Random();
 
static byte tmp;
static byte white = 255;
static byte black = 0;
static int halfmax = int.MaxValue / 2; // more voodoo! calling Next() is faster than Next(2)!
 
static IEnumerable<byte> YieldVodoo()
{
// Yield 3 times same number (i.e 255 255 255) for numpixels times.
 
for (int i = 0; i < numpixels; i++)
{
tmp = rand.Next() < halfmax ? black : white; // no more lists!
 
// no more loops! yield! yield! yield!
yield return tmp;
yield return tmp;
yield return tmp;
}
}
 
static Image Randimg()
{
// Low-level bitmaps
var bitmap = new Bitmap(size.Width, size.Height);
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
 
Marshal.Copy(
YieldVodoo().ToArray<byte>(),// source
0, // start
data.Scan0, // scan0 is a pointer to low-level bitmap data
numbytes); // number of bytes in source
 
bitmap.UnlockBits(data);
return bitmap;
}
 
[STAThread]
static void Main()
{
var form = new Form();
 
form.AutoSize = true;
form.Size = new Size(0, 0);
form.Text = "Test";
 
form.FormClosed += delegate
{
Application.Exit();
};
 
worker = new BackgroundWorker();
 
worker.DoWork += delegate
{
System.Threading.Thread.Sleep(500); // remove try/catch, just wait a bit before looping
 
while (true)
{
var a = DateTime.Now;
pb.Image = Randimg();
var b = DateTime.Now;
 
time += (b - a).TotalSeconds;
frames += 1;
 
if (frames == 30)
{
Console.WriteLine("{0} frames in {1:0.000} seconds. ({2:0} FPS)", frames, time, frames / time);
 
time = 0;
frames = 0;
}
}
};
 
worker.RunWorkerAsync();
 
FlowLayoutPanel flp = new FlowLayoutPanel();
form.Controls.Add(flp);
 
pb = new PictureBox();
pb.Size = size;
 
flp.AutoSize = true;
flp.Controls.Add(pb);
 
form.Show();
Application.Run();
}
}</lang>
 
Line 672 ⟶ 789:
//--------------------------------------------------------------------------------------------------
</lang>
 
=={{header|C sharp|C#}}==
Max 185 FPS on .NET 4.0/Windows 7 64-bit on Athlon II X4 620 - ATI Radeon X1200.
 
<lang csharp>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
class Program
{
static Size size = new Size(320, 240);
static Rectangle rectsize = new Rectangle(new Point(0, 0), size);
static int numpixels = size.Width * size.Height;
static int numbytes = numpixels * 3;
 
static PictureBox pb;
static BackgroundWorker worker;
 
static double time = 0;
static double frames = 0;
static Random rand = new Random();
 
static byte tmp;
static byte white = 255;
static byte black = 0;
static int halfmax = int.MaxValue / 2; // more voodoo! calling Next() is faster than Next(2)!
 
static IEnumerable<byte> YieldVodoo()
{
// Yield 3 times same number (i.e 255 255 255) for numpixels times.
 
for (int i = 0; i < numpixels; i++)
{
tmp = rand.Next() < halfmax ? black : white; // no more lists!
 
// no more loops! yield! yield! yield!
yield return tmp;
yield return tmp;
yield return tmp;
}
}
 
static Image Randimg()
{
// Low-level bitmaps
var bitmap = new Bitmap(size.Width, size.Height);
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
 
Marshal.Copy(
YieldVodoo().ToArray<byte>(),// source
0, // start
data.Scan0, // scan0 is a pointer to low-level bitmap data
numbytes); // number of bytes in source
 
bitmap.UnlockBits(data);
return bitmap;
}
 
[STAThread]
static void Main()
{
var form = new Form();
 
form.AutoSize = true;
form.Size = new Size(0, 0);
form.Text = "Test";
 
form.FormClosed += delegate
{
Application.Exit();
};
 
worker = new BackgroundWorker();
 
worker.DoWork += delegate
{
System.Threading.Thread.Sleep(500); // remove try/catch, just wait a bit before looping
 
while (true)
{
var a = DateTime.Now;
pb.Image = Randimg();
var b = DateTime.Now;
 
time += (b - a).TotalSeconds;
frames += 1;
 
if (frames == 30)
{
Console.WriteLine("{0} frames in {1:0.000} seconds. ({2:0} FPS)", frames, time, frames / time);
 
time = 0;
frames = 0;
}
}
};
 
worker.RunWorkerAsync();
 
FlowLayoutPanel flp = new FlowLayoutPanel();
form.Controls.Add(flp);
 
pb = new PictureBox();
pb.Size = size;
 
flp.AutoSize = true;
flp.Controls.Add(pb);
 
form.Show();
Application.Run();
}
}</lang>
 
=={{header|Common Lisp}}==
Line 833:
(main)
</lang>
 
 
=={{header|D}}==
Line 1,036 ⟶ 1,035:
{ { title "Black and White noise" } }
<bw-noise-gadget> with-fps >>gadgets ;</lang>
 
 
=={{header|FreeBASIC}}==
Line 1,370 ⟶ 1,368:
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides a family of print formatting routines]
 
=={{header|J}}==
Line 2,096 ⟶ 2,094:
 
[[Image_Noise/OCaml/OpenGL|Equivalent]] of the C-OpenGL method.
 
=={{header|Perl}}==
<lang perl>use Gtk3 '-init';
use Glib qw/TRUE FALSE/;
use Time::HiRes qw/ tv_interval gettimeofday/;
 
my $time0 = [gettimeofday];
my $frames = -8; # account for set-up steps before drawing
 
my $window = Gtk3::Window->new();
$window->set_default_size(320, 240);
$window->set_border_width(0);
$window->set_title("Image_noise");
$window->set_app_paintable(TRUE);
 
my $da = Gtk3::DrawingArea->new();
$da->signal_connect('draw' => \&draw_in_drawingarea);
$window->add($da);
$window->show_all();
Glib::Timeout->add (1, \&update);
 
Gtk3->main;
 
sub draw_in_drawingarea {
my ($widget, $cr, $data) = @_;
$cr->set_line_width(1);
for $x (1..320) {
for $y (1..240) {
int rand 2 ? $cr->set_source_rgb(0, 0, 0) : $cr->set_source_rgb(1, 1, 1);
$cr->rectangle( $x, $y, 1, 1);
$cr->stroke;
}
}
}
 
sub update {
$da->queue_draw;
my $elapsed = tv_interval( $time0, [gettimeofday] );
$frames++;
printf "fps: %.1f\n", $frames/$elapsed if $frames > 5;
return TRUE;
}</lang>
 
=={{header|Perl 6}}==
Variant of a script packaged with the SDL2::Raw module.
 
<lang perl6>use NativeCall;
use SDL2::Raw;
 
my int ($w, $h) = 320, 240;
 
SDL_Init(VIDEO);
 
my SDL_Window $window = SDL_CreateWindow(
"White Noise - Perl 6",
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
$w, $h,
RESIZABLE
);
 
my SDL_Renderer $renderer = SDL_CreateRenderer( $window, -1, ACCELERATED +| TARGETTEXTURE );
 
my $noise_texture = SDL_CreateTexture($renderer, %PIXELFORMAT<RGB332>, STREAMING, $w, $h);
 
my $pixdatabuf = CArray[int64].new(0, $w, $h, $w);
 
sub render {
my int $pitch;
my int $cursor;
 
# work-around to pass the pointer-pointer.
my $pixdata = nativecast(Pointer[int64], $pixdatabuf);
SDL_LockTexture($noise_texture, SDL_Rect, $pixdata, $pitch);
 
$pixdata = nativecast(CArray[int8], Pointer.new($pixdatabuf[0]));
 
loop (my int $row; $row < $h; $row = $row + 1) {
loop (my int $col; $col < $w; $col = $col + 1) {
$pixdata[$cursor + $col] = Bool.roll ?? 0xff !! 0x0;
}
$cursor = $cursor + $pitch;
}
 
SDL_UnlockTexture($noise_texture);
 
SDL_RenderCopy($renderer, $noise_texture, SDL_Rect, SDL_Rect);
SDL_RenderPresent($renderer);
}
 
my $event = SDL_Event.new;
 
main: loop {
 
while SDL_PollEvent($event) {
my $casted_event = SDL_CastEvent($event);
 
given $casted_event {
when *.type == QUIT {
last main;
}
}
}
 
render();
print fps;
}
 
say '';
 
sub fps {
state $fps-frames = 0;
state $fps-now = now;
state $fps = '';
$fps-frames++;
if now - $fps-now >= 1 {
$fps = [~] "\b" x 40, ' ' x 20, "\b" x 20 ,
sprintf "FPS: %5.2f ", ($fps-frames / (now - $fps-now)).round(.01);
$fps-frames = 0;
$fps-now = now;
}
$fps
}</lang>
 
=={{header|Pascal}}==
Line 2,265 ⟶ 2,141:
end;
end.</lang>
 
=={{header|Perl}}==
<lang perl>use Gtk3 '-init';
use Glib qw/TRUE FALSE/;
use Time::HiRes qw/ tv_interval gettimeofday/;
 
my $time0 = [gettimeofday];
my $frames = -8; # account for set-up steps before drawing
 
my $window = Gtk3::Window->new();
$window->set_default_size(320, 240);
$window->set_border_width(0);
$window->set_title("Image_noise");
$window->set_app_paintable(TRUE);
 
my $da = Gtk3::DrawingArea->new();
$da->signal_connect('draw' => \&draw_in_drawingarea);
$window->add($da);
$window->show_all();
Glib::Timeout->add (1, \&update);
 
Gtk3->main;
 
sub draw_in_drawingarea {
my ($widget, $cr, $data) = @_;
$cr->set_line_width(1);
for $x (1..320) {
for $y (1..240) {
int rand 2 ? $cr->set_source_rgb(0, 0, 0) : $cr->set_source_rgb(1, 1, 1);
$cr->rectangle( $x, $y, 1, 1);
$cr->stroke;
}
}
}
 
sub update {
$da->queue_draw;
my $elapsed = tv_interval( $time0, [gettimeofday] );
$frames++;
printf "fps: %.1f\n", $frames/$elapsed if $frames > 5;
return TRUE;
}</lang>
 
=={{header|Phix}}==
Line 2,416 ⟶ 2,334:
 
end Image_Noise;</lang>
 
 
=={{header|Processing}}==
Line 2,442 ⟶ 2,359:
text(frameRate, 5,15);
}</lang>
 
 
=={{header|PureBasic}}==
Line 2,571 ⟶ 2,487:
[on-tick handle-tick (/ 1. 120)])
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Variant of a script packaged with the SDL2::Raw module.
 
<lang perl6>use NativeCall;
use SDL2::Raw;
 
my int ($w, $h) = 320, 240;
 
SDL_Init(VIDEO);
 
my SDL_Window $window = SDL_CreateWindow(
"White Noise - Perl 6",
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
$w, $h,
RESIZABLE
);
 
my SDL_Renderer $renderer = SDL_CreateRenderer( $window, -1, ACCELERATED +| TARGETTEXTURE );
 
my $noise_texture = SDL_CreateTexture($renderer, %PIXELFORMAT<RGB332>, STREAMING, $w, $h);
 
my $pixdatabuf = CArray[int64].new(0, $w, $h, $w);
 
sub render {
my int $pitch;
my int $cursor;
 
# work-around to pass the pointer-pointer.
my $pixdata = nativecast(Pointer[int64], $pixdatabuf);
SDL_LockTexture($noise_texture, SDL_Rect, $pixdata, $pitch);
 
$pixdata = nativecast(CArray[int8], Pointer.new($pixdatabuf[0]));
 
loop (my int $row; $row < $h; $row = $row + 1) {
loop (my int $col; $col < $w; $col = $col + 1) {
$pixdata[$cursor + $col] = Bool.roll ?? 0xff !! 0x0;
}
$cursor = $cursor + $pitch;
}
 
SDL_UnlockTexture($noise_texture);
 
SDL_RenderCopy($renderer, $noise_texture, SDL_Rect, SDL_Rect);
SDL_RenderPresent($renderer);
}
 
my $event = SDL_Event.new;
 
main: loop {
 
while SDL_PollEvent($event) {
my $casted_event = SDL_CastEvent($event);
 
given $casted_event {
when *.type == QUIT {
last main;
}
}
}
 
render();
print fps;
}
 
say '';
 
sub fps {
state $fps-frames = 0;
state $fps-now = now;
state $fps = '';
$fps-frames++;
if now - $fps-now >= 1 {
$fps = [~] "\b" x 40, ' ' x 20, "\b" x 20 ,
sprintf "FPS: %5.2f ", ($fps-frames / (now - $fps-now)).round(.01);
$fps-frames = 0;
$fps-now = now;
}
$fps
}</lang>
 
=={{header|REXX}}==
Line 2,689 ⟶ 2,686:
framerateChecker.start()
}</lang>
 
=={{header|Tcl}}==
{{libheader|Tk}}
10,333

edits