BSAVE: Difference between revisions
Jump to navigation
Jump to search
Code by Ted Weissgerber
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(8 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
: [[BSAVE]] {{Parameter|saveFile$}}, [[VARPTR]]({{Parameter|array(index)}}), {{Parameter|fileSize&}} | : [[BSAVE]] {{Parameter|saveFile$}}, [[VARPTR]]({{Parameter|array(index)}}), {{Parameter|fileSize&}} | ||
=== Legacy support === | |||
* '''QB64''' can save larger arrays directly to binary files using [[PUT]] # and [[GET]] # without '''BSAVE'''. For that reason, '''BSAVE''' isn't recommended practice anymore and is supported to maintain compatibility with legacy code. | |||
{{PageParameters}} | |||
{{ | |||
* {{Parameter|saveFile$}} is the STRING file name of the file designated to be created. | * {{Parameter|saveFile$}} is the STRING file name of the file designated to be created. | ||
* {{Parameter|array(index)}} is the image [[arrays|array]] that already holds the [[GET (graphics statement)|GET]] image data. | * {{Parameter|array(index)}} is the image [[arrays|array]] that already holds the [[GET (graphics statement)|GET]] image data. | ||
Line 18: | Line 17: | ||
{{PageDescription}} | {{PageDescription}} | ||
* To place image data into the array, use [[GET (graphics statement)|GET]] to store a box area image of the screen. | * To place image data into the array, use [[GET (graphics statement)|GET]] to store a box area image of the screen. | ||
* [[SCREEN]] 12 can only GET 1/3 of the screen image at one time using a 26K array. | * [[SCREEN]] 12 can only GET 1/3 of the screen image at one time using a 26K array. | ||
* Image arrays are [[DIM]]ensioned as [[INTEGER]]. Use [[DEFINT]] when working with large graphic arrays. | * Image arrays are [[DIM]]ensioned as [[INTEGER]]. Use [[DEFINT]] when working with large graphic arrays. | ||
* Any arrays can be saved, but image arrays are most common. | * Any arrays can be saved, but image arrays are most common. | ||
Line 28: | Line 27: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' Saving array data to a file quickly. | ''Example 1:'' Saving array data to a file quickly. | ||
{{CodeStart}} | {{CodeStart}} | ||
LB% = {{Cl|LBOUND}}(Array) | LB% = {{Cl|LBOUND}}(Array) | ||
bytes% = {{Cl|LEN}}(Array(LB%)) | bytes% = {{Cl|LEN}}(Array(LB%)) | ||
filesize& = (({{Cl|UBOUND}}(Array) - LB%) + 1) * bytes% | filesize& = (({{Cl|UBOUND}}(Array) - LB%) + 1) * bytes% | ||
{{Cl|DEF SEG}} = {{Cl|VARSEG}}(Array(0)) | {{Cl|DEF SEG}} = {{Cl|VARSEG}}(Array(0)) | ||
{{Cl|BSAVE}} filename$, {{Cl|VARPTR}}(Array(LB%)), filesize& ' changeable index | {{Cl|BSAVE}} filename$, {{Cl|VARPTR}}(Array(LB%)), filesize& ' changeable index | ||
{{Cl|DEF SEG}} | {{Cl|DEF SEG}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
: ''Explanation:'' Procedure determines the filesize from the array size automatically. [[LBOUND]] is used with [[UBOUND]] to determine array size and byte size. Works with any type of array except variable-length strings. Used for saving program data fast. | : ''Explanation:'' Procedure determines the filesize from the array size automatically. [[LBOUND]] is used with [[UBOUND]] to determine array size and byte size. Works with any type of array except variable-length strings. Used for saving program data fast. | ||
Line 40: | Line 39: | ||
''Example 2:'' [[BSAVE]]ing a bitmap and calculating the file size | ''Example 2:'' [[BSAVE]]ing a bitmap and calculating the file size | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|DEF SEG}} = {{Cl|VARSEG}}(Image(0)) | {{Cl|DEF SEG}} = {{Cl|VARSEG}}(Image(0)) | ||
{{Cl|PSET}}(BMPHead.PWidth - 1, BMPHead.PDepth - 1) 'color lower right corner if black | {{Cl|PSET}}(BMPHead.PWidth - 1, BMPHead.PDepth - 1) 'color lower right corner if black | ||
Line 48: | Line 47: | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
{{Cl|BSAVE}} SaveName$, {{Cl|VARPTR}}(Image(0)), (2 * ArraySize&) + 200 'file size | {{Cl|BSAVE}} SaveName$, {{Cl|VARPTR}}(Image(0)), (2 * ArraySize&) + 200 'file size | ||
{{Cl|DEF SEG}} | {{Cl|DEF SEG}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 68: | Line 67: | ||
{{Cl|IF...THEN|IF}} Graphic%(i%) <> 0 {{Cl|THEN}} {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}} 'find image color not black | {{Cl|IF...THEN|IF}} Graphic%(i%) <> 0 {{Cl|THEN}} {{Cl|EXIT}} {{Cl|FOR...NEXT|FOR}} 'find image color not black | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
size% = i% + 4 'size plus 2 integers(4 bytes) for dimensions | size% = i% + 4 'size plus 2 integers(4 bytes) for dimensions | ||
{{Cl|REDIM}} {{Cl|_PRESERVE}} Graphic%(size%) 'resize existing array in QB64 only! | {{Cl|REDIM}} {{Cl|_PRESERVE}} Graphic%(size%) 'resize existing array in QB64 only! | ||
Line 82: | Line 81: | ||
{{Cl|CLOSE}} | {{Cl|CLOSE}} | ||
K$ = {{Cl|INPUT$}}(1) 'Press any key | K$ = {{Cl|INPUT$}}(1) 'Press any key | ||
{{Cl|FOR...NEXT|FOR}} i = 0 {{Cl|TO}} 20 'read all 3 arrays | {{Cl|FOR...NEXT|FOR}} i = 0 {{Cl|TO}} 20 'read all 3 arrays | ||
{{Cl|PRINT}} Graphic%(i); CopyBin%(i) | {{Cl|PRINT}} Graphic%(i); CopyBin%(i) | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
{{Cl|PRINT}} "Array:"; size%, "File:"; fsize% | {{Cl|PRINT}} "Array:"; size%, "File:"; fsize% | ||
{{CodeEnd}}{{ | {{CodeEnd}} | ||
{{Small|Code by Ted Weissgerber}} | |||
: ''Explanation:'' A 10 by 10 pixel box is saved to an array using the [[GET (graphics statement)]] and written to a BINARY file using [[PUT]] #1. Then [[GET]] #1 places the file contents into another INTEGER array and places it on the screen with the [[PUT (graphics statement)]]. | : ''Explanation:'' A 10 by 10 pixel box is saved to an array using the [[GET (graphics statement)]] and written to a BINARY file using [[PUT]] #1. Then [[GET]] #1 places the file contents into another INTEGER array and places it on the screen with the [[PUT (graphics statement)]]. | ||
: The array contents: 88 is the width in the GET array for [[SCREEN]] 13 which needs divided by 8 in that mode only. The area is actually 11 X 11. The array size needed can be found by looping backwards through the array until a color value is found. '''{{ | : The array contents: 88 is the width in the GET array for [[SCREEN]] 13 which needs divided by 8 in that mode only. The area is actually 11 X 11. The array size needed can be found by looping backwards through the array until a color value is found. '''{{Text|IF array(i) <> 0 THEN EXIT FOR|green}}''' (66 integers) or by dividing the created BINARY file size in half (134 bytes) when known to be array sized already. | ||
Line 96: | Line 96: | ||
* [[GET (graphics statement)]], [[PUT (graphics statement)]] | * [[GET (graphics statement)]], [[PUT (graphics statement)]] | ||
* [[BLOAD]], [[OPEN]], [[BINARY]] | * [[BLOAD]], [[OPEN]], [[BINARY]] | ||
* [[GET]], [[PUT]] {{ | * [[GET]], [[PUT]] {{Text|(file statements)}} | ||
* [[VARSEG]], [[VARPTR]] | * [[VARSEG]], [[VARPTR]] | ||
* [[DEF SEG]], [[TYPE]] | * [[DEF SEG]], [[TYPE]] |
Latest revision as of 22:15, 11 February 2023
BSAVE saves the contents of an image array to a BINARY file.
Syntax
Legacy support
- QB64 can save larger arrays directly to binary files using PUT # and GET # without BSAVE. For that reason, BSAVE isn't recommended practice anymore and is supported to maintain compatibility with legacy code.
Parameters
- saveFile$ is the STRING file name of the file designated to be created.
- array(index) is the image array that already holds the GET image data.
- fileSize& must be a bit over twice the size of the elements used in an INTEGER array.
Description
- To place image data into the array, use GET to store a box area image of the screen.
- SCREEN 12 can only GET 1/3 of the screen image at one time using a 26K array.
- Image arrays are DIMensioned as INTEGER. Use DEFINT when working with large graphic arrays.
- Any arrays can be saved, but image arrays are most common.
- DEF SEG = VARSEG must be used to designate the array segment position in memory.
- VARPTR returns the array index offset of the memory segment. Array sizes are limited to 32767 Integer elements due to the use of VARPTR in QBasic and QB64's emulated conventional memory.
- BSAVE files can later be opened with BLOAD.
Examples
Example 1: Saving array data to a file quickly.
LB% = LBOUND(Array) bytes% = LEN(Array(LB%)) filesize& = ((UBOUND(Array) - LB%) + 1) * bytes% DEF SEG = VARSEG(Array(0)) BSAVE filename$, VARPTR(Array(LB%)), filesize& ' changeable index DEF SEG |
- Explanation: Procedure determines the filesize from the array size automatically. LBOUND is used with UBOUND to determine array size and byte size. Works with any type of array except variable-length strings. Used for saving program data fast.
Example 2: BSAVEing a bitmap and calculating the file size
DEF SEG = VARSEG(Image(0)) PSET(BMPHead.PWidth - 1, BMPHead.PDepth - 1) 'color lower right corner if black GET (0, 0)-(BMPHead.PWidth - 1, BMPHead.PDepth - 1), Image(NColors * 3) ' for 16 or 256 colors FOR a& = 26000 TO 0 STEP -1 IF Image(a&) THEN ArraySize& = a&: EXIT FOR NEXT BSAVE SaveName$, VARPTR(Image(0)), (2 * ArraySize&) + 200 'file size DEF SEG |
- Explanation: The FOR loop reads backwards through the image array until it finds a value not 0. The LONG ArraySize& value is doubled and 200 is added. BMPhead.PWidth and BMPhead.PDepth are found by reading the bitmap's information header using a TYPE definition. See Bitmaps.
Example 3: Using PUT and GET to write and read array data from a file without using BSAVE or BLOAD:
KILL "example2.BIN" 'removes old image file! SCREEN 13 OPTION BASE 0 REDIM Graphic%(1001) 'REDIM makes array resize-able later LINE (0, 0)-(10, 10), 12, B 'create image GET(0, 0)-STEP(10, 10), Graphic%() 'get image to array FOR i% = 1000 TO 0 STEP -1 'reverse read array for size needed IF Graphic%(i%) <> 0 THEN EXIT FOR 'find image color not black NEXT size% = i% + 4 'size plus 2 integers(4 bytes) for dimensions REDIM _PRESERVE Graphic%(size%) 'resize existing array in QB64 only! OPEN "example2.BIN" FOR BINARY AS #1 ' PUT to a file PUT #1, , Graphic%() CLOSE OPEN "example2.BIN" FOR BINARY AS #2 'GET array and PUT to screen DIM CopyBin%(LOF(2) \ 2) 'create new array sized by half of file size GET #2, , CopyBin%() PUT(100, 100), CopyBin%(), PSET fsize% = LOF(2) CLOSE K$ = INPUT$(1) 'Press any key FOR i = 0 TO 20 'read all 3 arrays PRINT Graphic%(i); CopyBin%(i) NEXT PRINT "Array:"; size%, "File:"; fsize% |
- Explanation: A 10 by 10 pixel box is saved to an array using the GET (graphics statement) and written to a BINARY file using PUT #1. Then GET #1 places the file contents into another INTEGER array and places it on the screen with the PUT (graphics statement).
- The array contents: 88 is the width in the GET array for SCREEN 13 which needs divided by 8 in that mode only. The area is actually 11 X 11. The array size needed can be found by looping backwards through the array until a color value is found. IF array(i) <> 0 THEN EXIT FOR (66 integers) or by dividing the created BINARY file size in half (134 bytes) when known to be array sized already.
See also
- GET (graphics statement), PUT (graphics statement)
- BLOAD, OPEN, BINARY
- GET, PUT (file statements)
- VARSEG, VARPTR
- DEF SEG, TYPE
- Text Using Graphics