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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Image noise step by step in the Phix 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 Phix programming language

Source code in the phix programming language

-- demo\rosetta\ImageNoise.exw
include pGUI.e

Ihandle dlg, canvas, timer
cdCanvas cddbuffer, cdcanvas

constant TITLE = "Image noise"

integer fps = 129   -- (typical value)

function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
    integer {w, h} = IupGetIntInt(canvas, "DRAWSIZE")
    cdCanvasActivate(cddbuffer)
    sequence bw = repeat(0,w*h)
    for x=0 to w-1 do
        for y=0 to h-1 do
            if rand(2)=2 then bw[x*h+y+1] = 255 end if
        end for
    end for
    cdCanvasPutImageRectRGB(cddbuffer, w, h, {bw,bw,bw})
    cdCanvasFlush(cddbuffer)
    fps += 1
    return IUP_DEFAULT
end function

atom t1 = time()

function timer_cb(Ihandle /*ih*/)
    if time()>t1 then
        IupSetStrAttribute(dlg, "TITLE", "%s [%g FPS])",{TITLE,fps})
        fps = 0
        t1 = time()+1
    end if
    IupUpdate(canvas)
    return IUP_IGNORE
end function

function map_cb(Ihandle ih)
    cdcanvas = cdCreateCanvas(CD_IUP, ih)
    cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
    return IUP_DEFAULT
end function

procedure main()
    IupOpen()

    canvas = IupCanvas(NULL)
    IupSetAttribute(canvas, "RASTERSIZE", "320x240")
    IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
    IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))

    timer = IupTimer(Icallback("timer_cb"), 10)

    dlg = IupDialog(canvas)
    IupSetAttribute(dlg, "TITLE", TITLE)

    IupShow(dlg)
    IupSetAttribute(canvas, "RASTERSIZE", NULL)
    IupMainLoop()
    IupClose()
end procedure

main()


  

You may also check:How to resolve the algorithm Filter step by step in the Prolog programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the D programming language
You may also check:How to resolve the algorithm ABC problem step by step in the Astro programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the PHP programming language