Hi everyone!
These days, I have been creating two functions: Compress/uncompress memblocks and I have finally finished and I have decided to share it with you!
I think that the memblocks are an important feature, but there is one problem... their size. An image of 640x480x32 has 100-300 Kb in png format, but the memblock of this image is around 1.2Mb (640*480*4bytes)... and I think that this size is very big (especially for mobile).
Compressing memblock
The function needs the image filename and the "loss factor" (%error).
The function compares the different bytes. I have named "loss factor" to the difference between a byte and the initial byte of that serie. If two bytes are practically similar and the loss factor is the appropriate, then I consider that the bytes are identical. I have seen that a good "loss factor" is between 10-20.
This is the code...
function compress(image$,factor)
img=loadimage(image$)
mem=creatememblockfromimage(img)
file=opentowrite(left(image$,len(image$)-3)+"img",0)
for i=0 to 8 step 4
writeinteger(file,getmemblockint(mem,i))
next i
for i=0 to 3
for j=12+i to getmemblocksize(mem)-1 step 4
if current<getmemblockbyte(mem,j)-floor(25.0*factor/100.0) or current>getmemblockbyte(mem,j)+floor(25.0*factor/100.0) or i=j-12 or counter=255
if i<>j-12 then writebyte(file,counter)
writebyte(file,getmemblockbyte(mem,j))
current=getmemblockbyte(mem,j)
counter=0
else
inc counter
endif
next j
writebyte(file,counter)
counter=0
next i
closefile(file)
deletememblock(mem)
endfunction
Uncompressing memblock
You need the compressed file with the previous function (I have used the extension .img) The function returns the ID of the image.
file=opentoread(file$)
x=readinteger(file)
y=readinteger(file)
bits=readinteger(file)
size=x*y*4+12
mem=creatememblock(size)
setmemblockint(mem,0,x):setmemblockint(mem,4,y):setmemblockint(mem,8,bits)
repeat
value=readbyte(file)
counter=readbyte(file)
if (counter>0 and (last+1)*4+12+i>size-1) or (counter=0 and last*4+12+i>size-1)
last=0
inc i
endif
for j=last to last+counter
setmemblockbyte(mem,j*4+12+i,value)
next j
inc last, counter+1
until i>3
closefile(file)
img=createimagefrommemblock(mem)
deletememblock(mem)
With this code, I have gotten memblocks of 250 k (image of 640x376).
It's evident that you can use it in your applications (free cost hehe, free royalty and more...)
I hope that you like it!
Edit: I'm sorry, I have corrected the code... there were some errors. There is another problem... the load time...