Alternative to using read/Write Data Files

Started by Kman1011, June 03, 2007, 01:27:38 PM

Previous topic - Next topic

Kman1011

I have been trying to use data files in my program but have been getting alot of unexpected results. It seems have alot of intermittent problems.  :(

I used an alternative way to store data to disk a way back when I had my Amiga500. I used a method I called the grapic data method. It's simple. Render your information to an image in the form of pixels. Organize the data and make each pixel represent a byte of information. Use 'DotC' to write the information and 'n=Point(x,y)' to read the information. The image must be drawn to the screen in order to be read. (Only downside)
To store strings you have to be more creative. You have to convert your text components to Ascii using 'Mid(name$,place of letter in name) in order to write it. And convert back to text using 'Chr$($)'.

Seems inefficient but you will never get nasty runtime errors that make no sense. 

Here is some code that will convert a string and a number to a graphic data file then back again.

; PROJECT : Graphic_Data
; AUTHOR  : Kman
; CREATED : 6/2/2007
; EDITED  : 6/2/2007
; ---------------------------------------------------------------------
#Include "SaveBitmap"

CreateImage 1,11,10
name$="Kman1011"
Lenstr=Len(name$)
Ink RGB(255, 255, 0)
Print "Name$="+name$
Print "Length of string="+Str$(lenstr)
Print "Rendering information to bitmap..."
Print "Hit any key."
RenderToImage 1
DotC 0,0,lenstr
For i=1 To lenstr
c=Mid(name$,i)
DotC i,0,c
Next i
WaitKey
WaitNoKey

saveBitmap "infodata.bmp",1
DeleteImage 1
name$="":lenstr=0

RenderToScreen
Ink RGB(128, 255, 0) 

Print "filesaved!"
Print "Clearing information..."
Print "Name$="+name$
Print "Length of string="+Str$(lenstr)
Print "Hit Any key."
Sync
WaitKey
WaitNoKey
Ink RGB(0, 255, 255)
Print "loadingdata..."
LoadImage "infodata.bmp",1
DrawImage 1,0,0,1
lenstr=Point(0,0)
For i=1 To lenstr
name$=name$+Chr$(Point(i,0))
Next i
Print "Name$="+name$
Print "Length of string="+Str$(lenstr)
Sync
WaitKey


You will need the 'SaveBitmap' Library for this program

Try it out. Any suggestion and comments are welcome ;D
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin

#1
 Yeah this can work fine, but you know this it'll only work on 24/32bit screen modes ?    You should be using Banks!

Kman1011

I can't imagine using anything less than a 24bit screen mode. What aspect of the program cannot use other screen modes? SaveBitmap lib?

Ok. If I use banks. How do I store the banks to disk?


Kman ;)
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin

#3
QuoteI can't imagine using anything less than a 24bit screen mode. What aspect of the program cannot use other screen modes? SaveBitmap lib?

   When  you write a pixel to any surface, the pixel is transformed to the surfaces pixel format. When the pixel is read back, it's remapped to a 32bit format.   In a 16bit mode surface though (of which there are 3 RGB types - 565, 555 and 444) each channel has a lower resolution. So the channel returned will be truncated.

    For example, in 565 mode (the most common format today), this means that the 16bits used to represent one pixel, are arrange like this, top 5bits use for red, middle 6 bits used for green and low 5 bits used for blue.  

    16bit 565 =   RRRRRGGG  GGGBBBBBB
    16bit 555 =   -RRRRRGG GGGBBBBBB
    16bit 444 =   ----RRRR GGGGBBBBB

  (-) is an used bit.  

     These bits represent the high bits of each R,G,B channel from original RGB colour.   So in the case blue,  then blue channel is being truncated from  an 8bit value to a 5bit value to be drawn.. When it's read back, the value only retains 5bit accuracy.    So 3 bits are lost.

 
PlayBASIC Code: [Select]
;   OpenScreen 800,600,32,2      ; Works 
OpenScreen 800,600,16,2 ; won't work, as low bits 0,1,2 don't exist in these modes,
; B channel is 5 bit assuming the video card support a 565
; or 555 display mode


S$="Hello World"

; write characters to the current surface
For lp=1 to Len(s$)
ThisByte=mid(s$,lp)
dotc lp,0,rgb(255,0,Thisbyte) ; The RGB statemnent does not actually need to be here
b$=b$+digits$(thisbyte,3)+","
next

; decode charcters from surface
Xpos=1
repeat
ThisPixel =Point(Xpos,0)
ThisByte =rgbB(ThisPixel)
if ThisByte
s2$=s2$+ChR$(Thisbyte)
b2$=b2$+digits$(thisbyte,3)+","
endif
inc Xpos
until ThisByte=0


; show results
Setcursor 0,50
Print s$
print trimright$(b$,",")

Print s2$
print trimright$(b2$,",")

Sync
waitkey


 




QuoteOk. If I use banks. How do I store the banks to disk?

The same way as you're doing with SaveBitmap  -  Read & Write it - how else.

Load/Save Saving String Data Via Banks
Load/Save Banks


Kman1011

Yeah Ok. I see.

However I noticed you were only storing values in the Blue attribute of the RGB whereas I was storing the value as a whole. I believe the range is 0-16,777,215. for 16 bit would be (i would think) 0-65535. I could be wrong but you right there. The number would be truncated which would have to be taken into consideration. Just thought i would point that out.

I'll be looking into using 'Writememory/Readmemory' which I overlooked in the docs. Thanx for the sample.

L8R ;)
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

Kman1011



EDIT: Just tried exmaple for 'Reading_Writing_Memory_to_files' in the 'Examples' and got the old 'Attempting to read data beyond the end of this file' error message. Got v 1.63e. >:(
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin

Quote
However I noticed you were only storing values in the Blue attribute of the RGB whereas I was storing the value as a whole. I believe the range is 0-16,777,215. for 16 bit would be (i would think) 0-65535.

That's not what your code above does.  I think you've assuming the DOT = poke surface, it isn't !


Quote
EDIT: Just tried exmaple for 'Reading_Writing_Memory_to_files' in the 'Examples' and got the old 'Attempting to read data beyond the end of this file' error message. Got v 1.63e.

works here.