How to resolve the algorithm Image noise step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Image noise step by step in the Delphi programming language

Table of Contents

Problem Statement

Generate a random black and white   320x240   image continuously, showing FPS (frames per second).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Image noise step by step in the Delphi programming language

Source code in the delphi programming language

unit Main;

interface

uses
  Winapi.Windows, System.SysUtils, Vcl.Forms, Vcl.Graphics, Vcl.Controls,
  System.Classes, Vcl.ExtCtrls;

type
  TfmNoise = class(TForm)
    tmr1sec: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure tmr1secTimer(Sender: TObject);
  end;

var
  fmNoise: TfmNoise;
  Surface: TBitmap;
  FrameCount: Cardinal = 0;

implementation

{$R *.dfm}

procedure TfmNoise.FormCreate(Sender: TObject);
begin
  Surface := TBitmap.Create;
  Surface.SetSize(320, 240);
  Surface.PixelFormat := pf1bit;
  Randomize;
end;

procedure TfmNoise.FormDestroy(Sender: TObject);
begin
  Surface.Free;
end;

type
  PDWordArray = ^TDWordArray;

  TDWordArray = array[0..16383] of DWord;

procedure TfmNoise.FormPaint(Sender: TObject);
var
  x, y: Integer;
  line: PWordArray;
begin
  with Surface do
    for y := 0 to Height - 1 do
    begin
      line := Surface.ScanLine[y];
      for x := 0 to (Width div 16) - 1 do
        line[x] := Random($FFFF);
      // Fill 16 pixels at same time
    end;

  Canvas.Draw(0, 0, Surface);
  Inc(FrameCount);
  Application.ProcessMessages;
  Invalidate;
end;

procedure TfmNoise.tmr1secTimer(Sender: TObject);
begin
  Caption := 'FPS: ' + FrameCount.ToString;
  FrameCount := 0;
end;

end.


object fmNoise: TfmNoise
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu]
  BorderStyle = bsSingle
  Caption = 'fmNoise'
  ClientHeight = 240
  ClientWidth = 320
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  OnPaint = FormPaint
  object tmr1sec: TTimer
    Interval = 2000
    OnTimer = tmr1secTimer
  end
end


  

You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Read a file character by character/UTF8 step by step in the Action! programming language
You may also check:How to resolve the algorithm Empty string step by step in the Quite BASIC programming language
You may also check:How to resolve the algorithm Send email step by step in the Fortran programming language
You may also check:How to resolve the algorithm Polynomial long division step by step in the Go programming language