How do I clear an image?

New to FreeBASIC? Post your questions here.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How do I clear an image?

Post by fxm »

Instead of that:

Code: Select all

'clear graphic
   for y as integer = 0 to 617 'height of graphic
      'address of first pixel/row = graphic+sizeof(fb.IMAGE)
      dim as ulong ptr row = cast(ulong ptr, cast(ubyte ptr, graphic)+sizeof(fb.IMAGE)+y*graphic->pitch)
      for x as integer = 0 to gw-1 'holds graphic's width
         row[x] = rgba(0, 0, 0, 0)
      next
   next
Can you try with these two lines alone:

Code: Select all

'clear graphic
   Dim As Ulong Ptr myBufPix = Cast( Ulong Ptr, Cast( Ubyte Ptr, graphic ) + Sizeof(FB.IMAGE) )
   Clear *myBufPix, 00, graphic->width * graphic->height * graphic->bpp
If the area to be cleared is not necessarily the entire image, as is assumed in your example:

Code: Select all

'clear graphic
   for y as integer = 0 to 617 'height of graphic
      'address of first pixel/row = graphic+sizeof(fb.IMAGE)
      dim as ulong ptr row = cast(ulong ptr, cast(ubyte ptr, graphic)+sizeof(fb.IMAGE)+y*graphic->pitch)
      clear *row, 00, gw * graphic->bpp
   next
Last edited by fxm on May 26, 2017 13:50, edited 2 times in total.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: How do I clear an image?

Post by thesanman112 »

i guess it would depend on what you are trying to do, Im not sure how important speed is for image create because it should be already all done at program startup, so...when original image is initialised, make another 'magic pink' image at the same time, and at anytime you can just 'get' that magic pink image into the original image, but if you start with a max size buffer thats a magic pink sheet, you can 'get' any size image from it in to an already created image's buffer.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: How do I clear an image?

Post by thesanman112 »

i should mention that FXM's method of casting should quick or quicker, but you can just get any image into any buffer from another buffer, just specify
the page to read from and assign the array to the get statement. i belive its screenset work_page,visible_page, and make your work page an entire screen of magic pink, when you then use 'get work_page(1,1)-(sizex,sizey),array_to_overwrite. use CLS in combination with color forground,background to erase work page with new magic pink. and all of this work can be done on screen or image buffers that have already been created at startup.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How do I clear an image?

Post by dodicat »

You could try wiping a pointer to the image.

Code: Select all

Dim As Integer w,h
screeninfo w,h
Screenres w,h,32
Dim As ulong Ptr im=ImageCreate(w,h)
Dim As Integer size
Dim As ULong Ptr pi
Imageinfo im,,,,,pi,size

Function framecounter() As Long
    var t2=timer
    Static As Double t3,frames,answer
    frames=frames+1
    If (t2-t3)>=1 Then
        t3=t2
        answer=frames
        frames=0
    End If
    Return answer
End Function

#macro wipe(p)
For z As long=0 To (size)\4
	pi[z]=0
Next z
#EndMacro

Do
	ScreenLock
For n As Long=1 To 2
circle im,(Rnd*w,Rnd*h),50,RGB(Rnd*255,Rnd*255,Rnd*255),,,,f
Next n
Draw String im,(20,20),"FPS = " &framecounter
Put(0,0),im,PSet
wipe(pi)
ScreenUnLock
Sleep 1,1
Loop Until Len(InKey)
sleep
imagedestroy im
 
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How do I clear an image?

Post by MrSwiss »

There was a little mistake in my previously posted Sub:

Code: Select all

Put pimg, (x, y), @clr, PSet  ' @clr was simply: clr (but, put wants a Ptr)
Since I've noted, that While/Wend loops are seemingly faster, than For/Next loops, at least
when using FBC 64 (GCC), I've recoded the CLR_IMG Sub and added some testing code:

Code: Select all

' ClearImage.bas -- 2017-05-26, by MrSwiss (compile: -s gui)
'
Sub CLR_IMG(ByVal img As Any Ptr, ByVal wth As ULong, ByVal hgt As ULong)
    Dim As UInteger xc = 0, yc = 0      ' counter(s)
    Dim As ULong    clr = &h00FF00FF    ' magic-pink (32bit)

    While yc < hgt + 1  ' while/wend seems faster, than for/next in FBC 64bit
        While xc < wth + 1
            Put img, (xc, yc), @clr, PSet
            xc += 1
        Wend
        yc += 1
    Wend
End Sub

Const As ULong  w_win = 1024, h_win = 768, iw = w_win-1, ih = h_win-1, _
                red = &h3FFF0000, yellow = &hCFCFCF00, blue = &h3F007FCF

' proof of concept code ... only!
ScreenRes(w_win, h_win, 32, 2)  ' 32bit, double buffer
ScreenSet(1, 0)                 ' working/visible page
'Width w_win\8, h_win\16 ' only needed for using: Locate & Print

Dim As Any Ptr  bg_img = ImageCreate(3200, 1800, red, 32)    ' all bg
Line bg_img, (50, 25)-Step(199, 149), yellow, BF    ' proof of concept only
Circle bg_img, (600, 450), 120, blue,,,, F          ' proof of concept only
Dim As Any Ptr  buffer = ImageCreate(w_win, h_win,, 32) ' screen portion bg
Dim As Long     x_offs = 100, y_offs = 50, cnt = 0  ' run time variable(s)

Do
    CLR_IMG(buffer, iw, ih)         ' isn't really needed (in this case!)
    x_offs += 1 : y_offs += 1       ' move start pos(x,y), of portion 'to get'
    Get bg_img, (x_offs, y_offs)-Step(iw, ih), buffer   ' get portion of bg
    Put (0, 0), buffer, Alpha : cnt += 1    ' blit it to working page
    Flip                            ' copy to visible page
    Sleep 10, 1                     ' give CPU a break
    If cnt = 180 Then cnt = 0 : x_offs = 100 : y_offs = 50  ' re-set
Loop Until Len(InKey)               ' quit on user action (keyboard/mouse)
' clean up ...
ImageDestroy(bg_img) : bg_img = 0   ' release MEM | invalidate Ptr
ImageDestroy(buffer) : buffer = 0   ' as above
' ----- EOF -----
Post Reply