Memory banks - expanded memblocks.
For any kind of manipulation of data, you'll soon start to wonder how you did without them. In addition to the standard ability to write different basic data types to the banks, you can also:
- write double integers, double floats and strings too
- copy blocks of data between banks or even memblocks
- share memory between different programs
- map a file into memory (effectively load a file into a bank, but any changes you make are reflected in the file).
A long while ago, I wrote a function that would allow you to write a DBPro bitmap to a bmp file using standard file functions. I've written a small program that uses that function, but also does the same thing using a mapped file - run it yourself to see the speed increase:
#constant TEST_WIDTH 300
#constant TEST_HEIGHT 400
#constant SAVEFILE ".\test.bmp"
sync on
sync rate 0
` ************************************
` **** Create a randomly filled bitmap
` ************************************
create bitmap 1, TEST_WIDTH, TEST_HEIGHT
` Fill with coloured squares
for b=1 to 40
x = rnd(TEST_WIDTH - 1)
y = rnd(TEST_HEIGHT - 1)
wx = rnd(TEST_WIDTH - 1 - x)
wy = rnd(TEST_HEIGHT - 1 - y)
c = rgb(rnd(4)*64, rnd(4)*64, rnd(4*64))
box x, y, x + wx, y + wy, c, c, c, c
next b
` Get a copy for comparison later
get image 1, 0, 0, TEST_WIDTH, TEST_HEIGHT, 1
set current bitmap 0
` *******************************************
` **** Run the 'file' version of the function
` *******************************************
Start = timer()
SaveBitmapFile(SAVEFILE, 1)
Finish = timer()
` load the saved image
load image SAVEFILE, 2, 1
` Place the original and the saved version side by side
paste image 1, 0, 0
paste image 2, TEST_WIDTH + 8, 0
` Show the timings
set cursor 0, TEST_HEIGHT + 8
print "File commands - Elapsed time : "; Finish - Start
sync off
wait key
sync on
cls
` *******************************************
` **** Run the 'bank' version of the function
` *******************************************
Start = timer()
SaveBitmapBank(SAVEFILE, 1)
Finish = timer()
` load the saved image
load image SAVEFILE, 2, 1
` Place the original and the saved version side by side
paste image 1, 0, 0
paste image 2, TEST_WIDTH + 8, 0
` Show the timings
set cursor 0, TEST_HEIGHT + 8
print "Bank commands - Elapsed time : "; Finish - Start
sync off
wait key
end
` **********************************************
` **** The original file version of the function
` **********************************************
function SaveBitmapFile(File as string, Bmp as integer)
local x as integer
local y as integer
local Colour as dword
local OldBmp as integer
local Width as integer
local Height as integer
local PadSize as integer
` Save the current bitmap and switch to the one that needs saving
OldBmp=current bitmap()
set current bitmap Bmp
Width=bitmap width()
Height=bitmap height()
` Calculate the padding required at the end of each line
PadSize=4-((Width*3) mod 4)
if PadSize=4 then PadSize=0
` Clear the file if it already exists
if file exist(File) then delete file File
` Open the file
open to write 1, File
` Write the bitmap header
write byte 1, asc("B")
write byte 1, asc("M")
write long 1, 26+( ((Width*3)+PadSize) * Height )
write word 1, 0
write word 1, 0
write long 1, 26
write long 1, 12
write word 1, Width
write word 1, Height
write word 1, 1
write word 1, 24
Width=Width-1
Height=Height-1
lock pixels
` Copy the bitmap from the bottom up to the file including padding at
` the end of the line when required.
for y=Height to 0 step -1
for x=0 to Width
Colour=point(x,y)
write byte 1, Colour
write byte 1, Colour >> 8
write byte 1, Colour >> 16
next x
if PadSize > 0
for x=1 to PadSize
write byte 1,0
next x
endif
next y
unlock pixels
` Close the file and return to the original bitmap
close file 1
set current bitmap OldBmp
endfunction
` *****************************************
` **** The new bank version of the function
` *****************************************
function SaveBitmapBank(File as string, Bmp as integer)
local x as integer
local y as integer
local Colour as dword
local OldBmp as integer
local Width as integer
local Height as integer
local PadSize as integer
` Save the current bitmap and switch to the one that needs saving
OldBmp=current bitmap()
set current bitmap Bmp
Width=bitmap width()
Height=bitmap height()
` Calculate the padding required at the end of each line
PadSize=4-((Width*3) mod 4)
if PadSize=4 then PadSize=0
` Clear the file if it already exists
if file exist(File) then delete file File
` Map the file to the bank, setting the minimum size to the
` size of file required (header size + data size)
map file to bank File, 1, 26+( ((Width*3)+PadSize) * Height )
` Write the bitmap header
write bank string 1, 0, "BM", 2
write bank integer 1, 2, get bank size(1)
write bank word 1, 6, 0
write bank word 1, 8, 0
write bank integer 1, 10, 26
write bank integer 1, 14, 12
write bank word 1, 18, Width
write bank word 1, 20, Height
write bank word 1, 22, 1
write bank word 1, 24, 24
` Copy the bitmap from the bottom up to the bank including padding at
` the end of the line when required.
lock pixels
Pos = 26
for y=Height-1 to 0 step -1
for x=0 to Width-1
Colour=point(x,y)
write bank byte 1, Pos, Colour
write bank byte 1, Pos+1, (Colour >> 8)
write bank byte 1, Pos+2, (Colour >> 16)
inc Pos, 3
next x
if PadSize > 0
for x=1 to PadSize
write bank byte 1, Pos, 0
inc Pos
next x
endif
next y
unlock pixels
` Delete the bank and return to the original bitmap
delete bank 1
set current bitmap OldBmp
endfunction