Image noise: Difference between revisions

m
(Added solution for Action!)
 
(13 intermediate revisions by 9 users not shown)
Line 20:
Easy6502's built-in RNG allows for random pixels to be displayed in an infinite loop. Unfortunately these pixels are far too large to
effectively display an FPS counter, although the unused Y register in the program below could theoretically output that.
<langsyntaxhighlight lang="6502asm">define sysRandom $fe
 
Snow:
Line 40:
 
inx ;next X
jmp Snow ;loop forever</langsyntaxhighlight>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE TEXT_SIZE="40"
Line 152:
OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Image_noise.png Screenshot from Atari 8-bit computer]
Line 159:
{{libheader|Lumen}}
noise.ads:
<langsyntaxhighlight Adalang="ada">with Lumen.Image;
 
package Noise is
Line 165:
function Create_Image (Width, Height : Natural) return Lumen.Image.Descriptor;
 
end Noise;</langsyntaxhighlight>
 
noise.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
 
package body Noise is
Line 195:
end Create_Image;
 
end Noise;</langsyntaxhighlight>
 
test_noise.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Calendar;
with Ada.Text_IO;
with System.Address_To_Access_Conversions;
Line 376:
when Program_End =>
null;
end Test_Noise;</langsyntaxhighlight>
 
=={{header|Axe}}==
Line 387:
This example gets steady 48 FPS on a TI-84 Plus Silver Edition running at 15 MHz. It gets 26 FPS when running at 6 MHz. Some pixels appear gray because the duration of each frame is shorter than the screen's notoriously slow response time. If a pixel is toggled very quickly, it never has time to fully transition to white or black.
 
<langsyntaxhighlight lang="axe">.Enable 15 MHz full speed mode
Full
.Disable memory access delays (adds 1 FPS)
Line 429:
Lbl FPS
N++
Return</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> dx% = 320
dy% = 240
images% = 100000
Line 468:
SYS "SetWindowText", @hwnd%, "BBC BASIC: " + STR$(frame%*100 DIV TIME) + " fps"
ENDIF
UNTIL FALSE</langsyntaxhighlight>
[[File:image_noise_bbc.jpg]]
 
Line 475:
{{libheader|SDL}}
 
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Line 524:
surf = SDL_SetVideoMode(320, 240, 8, SDL_DOUBLEBUF | SDL_HWSURFACE);
blit_noise(surf);
}</langsyntaxhighlight>
===Fast OpenGL method===
{{libheader|GLUT}}
Depending on your hardware, you might be able to get thousands of frames per second. Compiled with <code>gcc -lglut -lGL -g -Wall -O2</code>.
<langsyntaxhighlight lang="c">#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
Line 574:
glutMainLoop();
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Max 185 FPS on .NET 4.0/Windows 7 64-bit on Athlon II X4 620 - ATI Radeon X1200.
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.ComponentModel;
Line 691:
Application.Run();
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
===Version 1 (windows.h)===
[[File:noise_cpp.png]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <sstream>
Line 927 ⟶ 928:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
 
===Version 2 (SDL2)===
{{libheader|SDL2}}
'''Source:''' https://gist.github.com/KatsumiKougen/58c53e77dc45e4e3a13661ff7b38c1ea
<syntaxhighlight lang="cpp">
// Standard C++ stuff
#include <iostream>
#include <string>
#include <random>
 
// SDL2 stuff
#include "SDL2/SDL.h"
 
// Other crazy stuffs
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define COLOUR_BLACK 0, 0, 0, 0xff
#define COLOUR_WHITE 0xff, 0xff, 0xff, 0xff
 
// Compile: g++ -std=c++20 -Wall -Wextra -pedantic ImageNoise.cpp -o ImageNoise -lSDL2
 
template <class RandomGenerator>
void BlitNoise(SDL_Renderer *r, int width, int height, auto &dist, RandomGenerator &generator) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// Use the random number generator in C++ Standard Library to determine draw colour
if (dist(generator) == 0)
// Set colour to black
SDL_SetRenderDrawColor(r, COLOUR_BLACK);
else if (dist(generator) == 1)
// Set colour to white
SDL_SetRenderDrawColor(r, COLOUR_WHITE);
// Go through every scanline, and put pixels
SDL_RenderDrawPoint(r, x, y);
}
}
}
 
void UpdateFPS(unsigned &frame_count, unsigned &timer, SDL_Window *window) {
static Uint64 LastTime = 0;
std::string NewWindowTitle;
Uint64 Time = SDL_GetTicks(),
Delta = Time - LastTime;
timer += Delta;
if (timer > 1000) {
unsigned ElapsedTime = timer / 1000;
NewWindowTitle = "Image noise - " + std::to_string((int)((float)frame_count/(float)ElapsedTime)) + " FPS"; // Dirty string trick
SDL_SetWindowTitle(window, const_cast<char*>(NewWindowTitle.c_str()));
timer = 0;
frame_count = 0;
NewWindowTitle.clear();
}
LastTime = Time;
}
 
int main() {
std::random_device Device; // Random number device
std::mt19937_64 Generator(Device()); // Random number generator
std::uniform_int_distribution ColourState(0, 1); // Colour state
unsigned Frames = 0,
Timer = 0;
SDL_Window *Window = NULL; // Define window
SDL_Renderer *Renderer = NULL; // Define renderer
// Init everything just for sure
SDL_Init(SDL_INIT_EVERYTHING);
// Set window size to 320x240, always shown
Window = SDL_CreateWindow("Image noise - ?? FPS", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
// Set background colour to white
SDL_SetRenderDrawColor(Renderer, COLOUR_WHITE);
SDL_RenderClear(Renderer);
// Create an event handler and a "quit" flag
SDL_Event e;
bool KillWindow = false;
// The window runs until the "quit" flag is set to true
while (!KillWindow) {
while (SDL_PollEvent(&e) != 0) {
// Go through the events in the queue
switch (e.type) {
// Event: user hits a key
case SDL_QUIT: case SDL_KEYDOWN:
// Destroy window
KillWindow = true;
break;
}
}
// Render the noise
BlitNoise(Renderer, SCREEN_WIDTH, SCREEN_HEIGHT, ColourState, Generator);
SDL_RenderPresent(Renderer);
// Increment the "Frames" variable.
// Then show the FPS count in the window title.
++Frames;
UpdateFPS(Frames, Timer, Window);
}
// Destroy renderer and window
SDL_DestroyRenderer(Renderer);
SDL_DestroyWindow(Window);
SDL_Quit();
return 0;
}
</syntaxhighlight>
 
{{output}}
<center>[[File:C++ image noise SDL2.gif|280px]]</center>
 
=={{header|Common Lisp}}==
{{libheader|lispbuilder-sdl}}
noise_sdl.lisp:
<langsyntaxhighlight lang="lisp">;; (require :lispbuilder-sdl)
 
(defun draw-noise (surface)
Line 971 ⟶ 1,084:
 
(main)
</syntaxhighlight>
</lang>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight Dlang="d">import std.stdio, std.random, sdl.SDL;
 
void main() {
Line 998 ⟶ 1,111:
lastTime = time;
}
}</langsyntaxhighlight>
This D version shows about 155 FPS, while on the same PC the C version shows about 180 FPS.
 
Line 1,010 ⟶ 1,123:
{{libheader| System.Classes}}
{{libheader| Vcl.ExtCtrls}}
<syntaxhighlight lang="delphi">
<lang Delphi>
unit Main;
 
Line 1,081 ⟶ 1,194:
end;
 
end.</langsyntaxhighlight>
Resources form:
<syntaxhighlight lang="delphi">
<lang Delphi>
object fmNoise: TfmNoise
Left = 0
Line 1,100 ⟶ 1,213:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
1400 to 1600 FPS
</pre>
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=XZDBqoMwEEX3fsWhi0dLaRp1UbPw/Yi4kDSCUI3EUPTvH9EE+roImTl3Zm4ys7OaeTGelQ2NQGSAti/r0CEc7duwcqcUBdtxB+6M9khRhpOJzE500zB23gSxd1xr8hAOPcu2+GE03PCS38jT6FzyqCI4bG9FTHeLSlLI//p3f5V0b1YfvO+cPz0v/HCin5dTqpPU6VUR9Y6afU5aQNNS0yClRClFu//LOtZQiLeUuUrN1rElnKuE4WO19tW4bnoOk6doY4E4/ET2Bw== Run it]
 
<syntaxhighlight>
proc pset x y c . .
color c
move x / 3.2 y / 3.2
rect 0.3 0.3
.
on animate
fr += 1
if systime - t0 >= 1
move 10 78
color -2
rect 80 20
color -1
move 10 80
text fr / (systime - t0) & " fps"
t0 = systime
fr = 0
.
col[] = [ 000 999 ]
for x = 0 to 319
for y = 0 to 199
pset x y col[randint 2]
.
.
.
</syntaxhighlight>
 
=={{header|Euler Math Toolbox}}==
 
Currently, Euler Math Toolbox does not have optimized routines to plot matrices with color information. The frames per second are consequently not really good.
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function noiseimg () ...
$aspect(320,240); clg;
Line 1,123 ⟶ 1,266:
>noiseimg
2.73544353263
</syntaxhighlight>
</lang>
 
=={{header|F Sharp|F#}}==
This implementation includes two methods to update the pixels values. One uses unsafe methods and can do 350 fps on my machine, the second uses safe code to marshal the new values onto the bitmap data and can do 240 fps on the same machine.
<langsyntaxhighlight lang="fsharp">open System.Windows.Forms
open System.Drawing
open System.Drawing.Imaging
Line 1,200 ⟶ 1,343:
Application.Run()
0
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Line 1,206 ⟶ 1,349:
~150 FPS
 
<langsyntaxhighlight lang="factor">USING: accessors calendar images images.viewer kernel math
math.parser models models.arrow random sequences threads timers
ui ui.gadgets ui.gadgets.labels ui.gadgets.packs ;
Line 1,275 ⟶ 1,418:
MAIN-WINDOW: open-noise-window
{ { title "Black and White noise" } }
<bw-noise-gadget> with-fps >>gadgets ;</langsyntaxhighlight>
 
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
{{libheader|SDL2}}
 
<syntaxhighlight lang="Forth">\ Bindings to SDL2 functions
s" SDL2" add-lib
\c #include <SDL2/SDL.h>
c-function sdl-init SDL_Init n -- n
c-function sdl-quit SDL_Quit -- void
c-function sdl-createwindow SDL_CreateWindow a n n n n n -- a
c-function sdl-createrenderer SDL_CreateRenderer a n n -- a
c-function sdl-setdrawcolor SDL_SetRenderDrawColor a n n n n -- n
c-function sdl-drawpoint SDL_RenderDrawPoint a n n -- n
c-function sdl-renderpresent SDL_RenderPresent a -- void
c-function sdl-delay SDL_Delay n -- void
c-function sdl-ticks SDL_GetTicks -- n
 
require random.fs
 
0 value window
0 value renderer
240 constant height
320 constant width
 
: black 0 0 0 255 ;
: white 255 255 255 255 ;
: black-or-white 2 random if black else white then ;
 
: init-image ( -- )
$20 sdl-init drop
s\" Rosetta Task : Image Noise\x0" drop 0 0 width height $0 sdl-createwindow to window
window -1 $2 sdl-createrenderer to renderer
page
;
 
: image-noise-generate
height 0 do
width 0 do
renderer black-or-white sdl-setdrawcolor drop
renderer i j sdl-drawpoint drop
loop
loop
;
 
: .FPS swap - 1000 swap / 0 0 at-xy . ." FPS" ;
 
: image-noise
init-image
100 0 do
sdl-ticks
image-noise-generate renderer sdl-renderpresent
sdl-ticks .FPS
loop
sdl-quit
;
 
image-noise</syntaxhighlight>
 
 
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 13-07-2018
' compile with: fbc -s console
' or: fbc -s gui
Line 1,313 ⟶ 1,518:
WindowTitle "fps = " + Str(fps)
 
Wend</langsyntaxhighlight>
 
 
=={{header|FutureBasic}}==
FB has native image noise filters.
<syntaxhighlight lang="futurebasic">
 
_window = 1
begin enum output 1
_noiseView
_fpsLabel
end enum
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 340, 300 )
window _window, @"Rosetta Code Image Noise", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
r = fn CGRectMake( 10, 50, 320, 240 )
imageview _noiseView, YES, , r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameNone, _window
r = fn CGRectMake( 20, 10, 280, 24 )
textlabel _fpsLabel, @"Estimated FPS: 60", r, _window
ControlSetAlignment( _fpsLabel, NSTextAlignmentCenter )
end fn
 
local fn CIImageToImageRef( ciImage as CIImageRef ) as ImageRef
CIImageRepRef rep = fn CIImageRepWithCIImage( ciImage )
CGSize size = fn ImageRepSize( rep )
ImageRef image = fn ImageWithSize( size )
ImageAddRepresentation( image, rep )
end fn = image
 
local fn NoiseCIImage as CIImageRef
CIFilterRef filter = fn CIFilterWithName( @"CIPhotoEffectNoir" )
ObjectSetValueForKey( filter, fn ObjectValueForKey( fn CIFilterWithName(@"CIRandomGenerator"), @"outputImage" ), @"inputImage" )
CIFilterSetDefaults( filter )
CIImageRef outputCIImage = fn CIImageByCroppingToRect( fn CIFilterOutputImage( filter ), fn CGRectMake( rnd(1000), rnd(1000), 500, 500 ) )
end fn = outputCIImage
 
local fn BuildNoiseView
block NSInteger renderedFrames
renderedFrames = 0
timerbegin, 1.0/60, YES
CIImageRef noiseCIImage = fn NoiseCIImage
ImageRef noiseImage = fn CIImageToImageRef( noiseCIImage )
ImageViewSetImage( _noiseView, noiseImage )
renderedFrames++
if( renderedFrames == 60 )
ControlSetStringValue( _fpsLabel, fn StringWithFormat( @"Estimated FPS: %ld", renderedFrames ) )
renderedFrames = 0
end if
timerend
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _windowWillClose : end
end select
end fn
 
 
randomize
 
on dialog fn DoDialog
 
fn BuildWindow
fn BuildNoiseView
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:Image Noise.png]]
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,403 ⟶ 1,682:
}
}
</syntaxhighlight>
</lang>
===Optimized example===
A second example that is somewhat more optimized but maybe more complicated.
(~3000fps on a Thinkpad x220 laptop)
<langsyntaxhighlight lang="go">package main
 
/* Note, the x-go-binding/ui/x11 lib is under development and has as a temp solution
Line 1,505 ⟶ 1,784:
}
}
</syntaxhighlight>
</lang>
[[File:GoNoise.png]]
 
Line 1,511 ⟶ 1,790:
This uses the GLFW-b bindings for GLFW 3 support:
<pre>cabal install glfw-b</pre>
<langsyntaxhighlight Haskelllang="haskell">import qualified Graphics.Rendering.OpenGL as GL
import qualified Graphics.UI.GLFW as GLFW
import qualified Foreign as F
Line 1,578 ⟶ 1,857:
case key of
GLFW.Key'Q -> GLFW.setWindowShouldClose win True
_ -> return ()</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,588 ⟶ 1,867:
* Using DrawImage to draw a randomly constructed bi-level images(see pg 157 of the graphics book, speed ~= 10x)
 
<langsyntaxhighlight Iconlang="icon">link printf
 
procedure main()
Line 1,606 ⟶ 1,885:
Event() # wait for any window event
close(&window)
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 1,612 ⟶ 1,891:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">coclass'example'
(coinsert[require)'jzopengl'
Line 1,659 ⟶ 1,938:
)
p_run''</langsyntaxhighlight>
 
The script auto-starts when run (that last line <code><nowiki>p_run''</nowiki></code> is responsible for the auto-start.
Line 1,679 ⟶ 1,958:
 
- Very fast: 1000+ FPS on a 2.8 GHz Core Duo (with 64-bit JRE). This is capped because the maximum resolution of the timers available is 1 ms
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
Line 1,798 ⟶ 2,077:
});
}
}</langsyntaxhighlight>[[File:javanoise2.png]]
 
=={{header|JavaScript}}==
[http://jsfiddle.net/bZJvr/ jsFiddle Demo]
 
<langsyntaxhighlight lang="javascript"><body>
<canvas id='c'></canvas>
 
Line 1,851 ⟶ 2,130:
animate();
</script>
</body></langsyntaxhighlight>
About 59 frames/second on Firefox 4.
 
=={{header|Julia}}==
With Gtk, gets about 80 frames per second on a older, dual core 3 Ghz processor.
<langsyntaxhighlight lang="julia">using Gtk, GtkUtilities
 
function randbw(ctx, w, h)
Line 1,895 ⟶ 2,174:
 
wait(cond)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import java.awt.*
Line 2,015 ⟶ 2,294:
}
})
}</langsyntaxhighlight>
 
{{out}}
Line 2,024 ⟶ 2,303:
=={{header|Liberty BASIC}}==
Generates the random bitmap programmatically, then chops it each time in a different way.
<syntaxhighlight lang="lb">
<lang lb>
WindowWidth =411
w =320
Line 2,097 ⟶ 2,376:
close #w
end
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
*Note, you will need to insert a plot component to get this to work.
<syntaxhighlight lang="maple">
<lang Maple>
a:=0;
t:=time[real]();
Line 2,112 ⟶ 2,391:
 
end do:
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
<lang Mathematica>
time = AbsoluteTime[]; Animate[
Column[{Row[{"FPS: ", Round[n/(AbsoluteTime[] - time)]}],
RandomImage[1, {320, 240}]}], {n, 1, Infinity, 1}]
</syntaxhighlight>
</lang>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
try destroydialog testRollout catch ()
Line 2,156 ⟶ 2,435:
createdialog testrollout
</syntaxhighlight>
</lang>
 
=={{header|MiniScript}}==
This implementation is for use with [http://miniscript.org/MiniMicro Mini Micro].
<syntaxhighlight lang="miniscript">
clear
tileSetLength = 32
width = 320
height = 240
cellSize = [960/width, 600/height]
colors = [color.black, color.white]
 
newImg = function
img = Image.create(tileSetLength, 1)
for i in range(0, tileSetLength - 1)
img.setPixel i, 0, colors[rnd * 2]
end for
return img
end function
 
display(5).mode = displayMode.tile
td = display(5)
td.cellSize = cellSize
td.extent = [width, height]
td.tileSetTileSize = 1
for x in range(0, width - 1)
for y in range(0, height - 1)
td.setCell x, y, rnd * tileSetLength
end for
end for
frames = 0
startTime = time
while true
td.tileSet = newImg
frames += 1
dt = time - startTime
if dt >= 1 then
text.row = 25; print "FPS: " + round(frames / dt, 2)
frames = 0
startTime = time
end if
end while
</syntaxhighlight>
[[File:Minimicro-noise-32.gif]]
 
=={{header|Nim}}==
Line 2,167 ⟶ 2,489:
On a small laptop running Manjaro and without a dedicated graphic card, we get 550-600 FPS.
 
<langsyntaxhighlight Nimlang="nim">import times
 
import opengl
Line 2,222 ⟶ 2,544:
start = getTime()
last = start
glutMainLoop()</langsyntaxhighlight>
 
===Using library "Rapid"===
Line 2,232 ⟶ 2,554:
Naive implementation:
 
<langsyntaxhighlight lang="nim">import random
 
import rapid/gfx
Line 2,254 ⟶ 2,576:
echo 1 / (step / 60)
update step:
discard step</langsyntaxhighlight>
 
Optimized implementation using shaders:
 
<langsyntaxhighlight lang="nim">import random
 
import rapid/gfx
Line 2,292 ⟶ 2,614:
echo 1 / (step / 60)
update step:
discard step</langsyntaxhighlight>
 
On a Ryzen 5 1600 and a Vega 56 the naive implementation struggles to maintain 10 FPS. The GPU-powered version, however, reaches up to 12000-13000 FPS.
Line 2,299 ⟶ 2,621:
{{libheader|OCamlSDL}}
with the [[OCamlSDL|ocaml-sdl]] bindings:
<langsyntaxhighlight lang="ocaml">let frames =
{ contents = 0 }
 
Line 2,347 ⟶ 2,669:
in
Sys.catch_break true;
blit_noise surf</langsyntaxhighlight>
 
compile to native-code with:
Line 2,365 ⟶ 2,687:
 
In a more idiomatic way, using the modules Graphics and Unix from the standard OCaml library:
<langsyntaxhighlight lang="ocaml">open Graphics
 
let white = (rgb 255 255 255)
Line 2,391 ⟶ 2,713:
with _ ->
flush stdout;
close_graph ()</langsyntaxhighlight>
 
run this script with:
Line 2,408 ⟶ 2,730:
{{libheader|SDL}}
It is SDL-internally limited to 200 fps.
<langsyntaxhighlight lang="pascal">Program ImageNoise;
 
uses
Line 2,449 ⟶ 2,771:
lastTime := time;
end;
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use Gtk3 '-init';
use Glib qw/TRUE FALSE/;
use Time::HiRes qw/ tv_interval gettimeofday/;
Line 2,491 ⟶ 2,813:
printf "fps: %.1f\n", $frames/$elapsed if $frames > 5;
return TRUE;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
Not optimised, gets about 130 fps.
<langsyntaxhighlight Phixlang="phix">-- demo\rosetta\ImageNoise.exw
include pGUI.e
 
Line 2,558 ⟶ 2,880:
end procedure
 
main()</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
This solution works on ErsatzLisp, the Java version of PicoLisp. It creates a 'JFrame' window, and calls inlined Java code to handle the image.
<langsyntaxhighlight PicoLisplang="picolisp">(javac "ImageNoise" "JPanel" NIL
"java.util.*"
"java.awt.*" "java.awt.image.*" "javax.swing.*" )
Line 2,612 ⟶ 2,934:
 
# Start with 25 frames per second
(imageNoise 320 240 25)</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="text">Image_Noise: procedure options (main); /* 3 November 2013 */
declare (start_time, end_time) float (18);
declare (frame, m, n) fixed binary;
Line 2,641 ⟶ 2,963:
end display;
 
end Image_Noise;</langsyntaxhighlight>
 
=={{header|Processing}}==
<langsyntaxhighlight lang="processing">color black = color(0);
color white = color(255);
 
Line 2,666 ⟶ 2,988:
fill(255);
text(frameRate, 5,15);
}</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
<langsyntaxhighlight lang="python">black = color(0)
white = color(255)
 
Line 2,689 ⟶ 3,011:
rect(0, 0, 60, 20)
fill(255)
text(frameRate, 5, 15)</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#filter=0.2 ; Filter parameter for the FPS-calculation
#UpdateFreq=100 ; How often to update the FPS-display
 
Line 2,727 ⟶ 3,049:
Until Not Event
EndIf
ForEver</langsyntaxhighlight>
[[Image:Image_Noise_in_PureBasic.png‎]]
 
Line 2,734 ⟶ 3,056:
{{libheader|PIL}}
 
<langsyntaxhighlight lang="python">import time
import random
import Tkintertkinter
from PIL import Image, ImageTk # PIL libray
from PIL import ImageTk
 
 
class App(object):
class App:
def __init__(self, size, root):
self.root = root
self.root.title("Image Noise Test")
 
self.img = Image.new("RGB1", size)
self.label = Tkintertkinter.Label(root)
self.label.pack()
 
Line 2,751 ⟶ 3,075:
self.frames = 0
self.size = size
self.n_pixels = self.size[0] * self.size[1]
 
self.loop()
 
def loop(self):
self.tastart_time = time.time()
# 13 FPS boost. half integer idea from C#.
rnd = random.random
white = (255, 255, 255)
black = (0, 0, 0)
npixels = self.size[0] * self.size[1]
data = [white if rnd() > 0.5 else black for i in xrange(npixels)]
self.img.putdata(data)
self.pimg = ImageTk.PhotoImage(self.img)
self.label["image"] = self.pimg
self.tb = time.time()
 
self.time += img.putdata(self.tb - self.ta)
[255 if b > 127 else 0 for b in random.randbytes(self.n_pixels)]
)
 
self.bitmap_image = ImageTk.BitmapImage(self.img)
self.label["image"] = self.bitmap_image
 
end_time = time.time()
self.time += end_time - start_time
self.frames += 1
 
if self.frames == 30:
try:
self.fps = self.frames / self.time
except ZeroDivisionError:
self.fps = "INSTANT"
 
print ("%d frames in %3.2f seconds (%s FPS)" %
print(f"{self.frames,} frames in {self.time,:3.2f} self.seconds ({fps} FPS)")
self.time = 0
self.frames = 0
 
self.root.after(1, self.loop)
 
 
def main():
root = Tkintertkinter.Tk()
app = App((320, 240), root)
root.mainloop()
 
 
main()</lang>
if __name__ == "__main__":
About 28 FPS max, Python 2.6.6.
main()
</syntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require 2htdp/image 2htdp/universe)
Line 2,817 ⟶ 3,144:
[on-draw draw]
[on-tick handle-tick (/ 1. 120)])
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,823 ⟶ 3,150:
Variant of a script packaged with the SDL2::Raw module.
 
<syntaxhighlight lang="raku" perl6line>use NativeCall;
use SDL2::Raw;
 
Line 2,898 ⟶ 3,225:
}
$fps
}</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program times (elapsed) the generation of 100 frames of random black&white image.*/
parse arg sw sd im . /*obtain optional args from the C.L. */
if sw=='' | sw=="," then sw=320 /*SW specified? No, then use default.*/
Line 2,923 ⟶ 3,250:
$=$ || _ /*append row to $ string.*/
end /*y*/ /* [↑] build the image. */
return</langsyntaxhighlight>
 
=={{header|RPL}}==
RPL can only handle 131x64 screens. Despite such a small size, displaying graphics is very slow: FPH (frames Per Hour) would be a more appropriate unit.
====HP 28/48 version====
≪ TICKS (0,0) PMIN (130,64) PMAX
'''DO'''
CLLCD
'''IF''' TICKS OVER - 8192 / B→R '''THEN'''
LAST INV 3 DISP '''END'''
0 130 '''FOR''' x
0 63 '''FOR''' y
'''IF''' RAND 0.5 > '''THEN''' x y R→C PIXEL '''END'''
'''NEXT NEXT'''
'''UNTIL''' 0 '''END'''
≫ '<span style="color:blue">NOISE</span>' STO
HP-28 users shall replace <code>TICKS</code> by a <code>SYSEVAL</code> call, which address value depends on their ROM version.
====HP-48G version====
From the HP-48G model, the graphics system has been completely reviewed, and so the commands:
≪ TICKS
'''DO'''
ERASE
'''IF''' TICKS OVER - 8192 / B→R '''THEN'''
LASTARG INV 1 →GROB PICT RCL {#0h #42h} ROT GOR PICT STO {#0 #0} PVIEW '''END'''
0 130 '''FOR''' x
0 63 '''FOR''' y
'''IF''' RAND 0.5 > '''THEN''' x R→B y R→B 2 →LIST PIXON '''END'''
'''NEXT NEXT'''
{#0 #0} PVIEW
'''UNTIL''' 0 '''END'''
≫ '<span style="color:blue">NOISE</span>' STO
FPS = 0.0095
 
=={{header|Ruby}}==
Line 2,929 ⟶ 3,287:
{{libheader|ruby-opengl}}
 
<langsyntaxhighlight lang="ruby">require 'rubygems'
require 'gl'
require 'glut'
Line 2,951 ⟶ 3,309:
 
Glut.glutCreateWindow "noise"
Glut.glutMainLoop</langsyntaxhighlight>
 
{{libheader|rubygems}}
{{libheader|JRubyArt}}
JRubyArt is a port of processing to ruby.
<langsyntaxhighlight lang="ruby">
PALLETE = %w[#000000 #FFFFFF]
attr_reader :black, :dim, :white
Line 2,979 ⟶ 3,337:
text(frame_rate, 5, 15)
end
</syntaxhighlight>
</lang>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">begSec = time$("seconds")
graphic #g, 320,240
tics = 320 * 240
Line 2,994 ⟶ 3,352:
print "Seconds;";totSec;" Count:";tics;" Tics / sec:";tics/totSec;" fps:";1/totSec
render #g
#g "flush"</langsyntaxhighlight>
 
=={{header|Scala}}==
This is basically the same as the Java version, except without using BufferedImage.
<langsyntaxhighlight lang="scala">
import java.awt.event.{ActionEvent, ActionListener}
import swing.{Panel, MainFrame, SimpleSwingApplication}
Line 3,044 ⟶ 3,402:
repainter.start()
framerateChecker.start()
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
 
proc generate {img width height} {
Line 3,082 ⟶ 3,440:
pack [label .l -image noise]
update
looper</langsyntaxhighlight>
 
{{omit from|PARI/GP}}
Line 3,089 ⟶ 3,447:
Windows Forms Application.
 
<langsyntaxhighlight lang="vbnet">Imports System.Drawing.Imaging
 
Public Class frmSnowExercise
Line 3,231 ⟶ 3,589:
End Sub
End Class
</syntaxhighlight>
</lang>
Sample:<BR>
[[Image:SHSnowExercise.jpg]]
Line 3,237 ⟶ 3,595:
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window
import "graphics" for Canvas, Color
import "random" for Random
Line 3,268 ⟶ 3,626:
__t = t
}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
Line 3,274 ⟶ 3,632:
intrinsic could display this image at 327 FPS if Ran(2) was faster.)
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int CpuReg, \address of CPU register array (from GetReg)
FPS, \frames per second, the display's update rate
Line 3,299 ⟶ 3,657:
until KeyHit;
SetVid(3); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
Line 3,310 ⟶ 3,668:
* During the main game loop, the frame timer is also used to select a random graphics tile from among 16 possibilities, and draws it to a randomly selected tile on screen using a simple "xor-shift" routine.
 
<langsyntaxhighlight lang="z80">forever:
ld hl,frametimer
ld a,(hl)
Line 3,367 ⟶ 3,725:
 
jp forever</langsyntaxhighlight>
 
And the interrupt handler located at memory address <code>&A000</code>:
<langsyntaxhighlight lang="z80">TV_Static_FX:
;this code runs every time the game boy finishes drawing one row of pixels, unless the interrupt is disabled as described above.
push hl
Line 3,388 ⟶ 3,746:
pop hl
reti ;return to main program
TV_Static_FX_End:</langsyntaxhighlight>
 
{{out}}
1,983

edits