best way to clear an image (ImageCreate)

General FreeBASIC programming questions.
Post Reply
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

best way to clear an image (ImageCreate)

Post by SamL »

I have an image created with ImageCreate.
I'm having trouble understanding how to access the pixel data directly.
so..
I'm clearing the image in a sub like this. Is there a faster way to do this, maybe by clearing the pixel data directly it could be faster.

Code: Select all

sub clear_graph_full( img as FB.IMAGE ptr, _color as ulong )
    Dim As Integer w, h
    ImageInfo(img, w, h)
    Line img, (0, 0)-(w, h), _color, bf
end sub
SamL
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: best way to clear an image (ImageCreate)

Post by badidea »

Using line to clear is already efficient I think (no benchmarks done).
Note that 0 to w-1/h-1 is sufficient.
Example using pixel data pointer added.

Code: Select all

#include "fbgfx.bi"

sub clear_graph_full(pImg as FB.IMAGE ptr, _color as ulong )
	dim as integer w, h
	imageinfo(pImg, w, h)
	line pImg, (0, 0)-(w - 1, h - 1), _color, bf 'note -1
end sub

sub clear_graph_pixels(pImg as FB.IMAGE ptr, _color as ulong )
	dim as integer w, h, bypp, pitch
	dim as ulong ptr pPixdata 'for 32-bit color mode!
	imageinfo(pImg, w, h, bypp, pitch, pPixdata)
	for y as integer = 0 to h - 1
		for x as integer = 0 to w - 1
			pPixdata[x] = _color
		next
		pPixdata += (pitch \ 4) 'pitch in bytes, but ulong pointer, so \ 4
	next
end sub

screenres 800, 600, 32
'create image (with default transparent color)
dim as FB.IMAGE ptr pImg = imagecreate(100, 100)

put(10,10), pImg, pset
sleep 500

clear_graph_full(pImg, &hffff00) 'yellow
put(10,10), pImg, pset
sleep 500

clear_graph_pixels(pImg, &h00ff00) 'green
put(10,10), pImg, pset
sleep 500

getkey()
You can also do:

Code: Select all

pPixdata[x + y * (pitch \ 4)] = _color
but that will probably be slower, unless the compiler optimizes it again.
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: best way to clear an image (ImageCreate)

Post by SamL »

Great!!, your right I missed the -1, thanks for pointing that out.
SamL
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: best way to clear an image (ImageCreate)

Post by badidea »

SamL wrote:Great!!, your right I missed the -1, thanks for pointing that out.
It is not a big problem because line() will not draw 'outside' the image. So no real performance difference I think.

Note that I am still on an older fbc version (1.07.0) and that some changes were done to Imageinfo() recently, but no problem for the code above I expect.
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: best way to clear an image (ImageCreate)

Post by SamL »

also using Line I can skip calling ImageInfo by accessing FB.IMAGE directly for that information.

Code: Select all

sub clear_graph_full( img as FB.IMAGE ptr, _color as ulong )
    Line img, (0, 0)-(img->width-1, img->height-1), _color, bf
end sub
SamL
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: best way to clear an image (ImageCreate)

Post by SamL »

badidea wrote:It is not a big problem because line() will not draw 'outside' the image. So no real performance difference I think.
I was wondering about that, but I was not getting errors writing over the edges ( doing so in other parts of the program )
I'm using 1.08 and its working good.

SamL
Post Reply