Unless somebody reports issues with anything, SaveImage v2.0 is now here.
Inside the archive below is a couple of quick demos which illustrate how to save a QB64 screen image in either BMP or PNG format; how one can easily turn a text screen into a 256 color graphic screen with one simple command; and how to compress and inflate strings or programs quickly and easily.
Commands should be simple enough that everyone can figure them out without any real effort involved.
To Deflate/Inflate a string/file:
'$include:'saveimage.bi'
text$ = "Whatever the heck it is that we might want to compress, be that a long string of input, a binary file (just load the whole file into a single string and then deflate it), or whatever else is wanted)."
de$ = Deflate(text$) ' <-- This is how simple it is to deflate the string
t$ = Inflate(de$) ' <-- This is how simple it is to inflate that deflated string
'$include:'saveimage.bm'
To turn a text screen into a graphic screen:
'$include:'saveimage.bi'
PRINT "Hello World. This is a SCREEN 0 screen which I'm printing on." x = TextScreenToImage256(0) 'And a quick example of how to turn a text screen into a 256 color image screen
CIRCLE (320, 240), 100, 40 'And draw a circle on it LOCATE 2,1:
PRINT "And now it's been converted to a 256 color graphical screen. See the circle on it??" '$include:'saveimage.bm'
To save an image (from any QB64 screen mode -- even SCREEN 0 text screens work!):
'$Include:'SaveImage.BI'
InitialImage$ = "Volcano Logo.jpg"
exportimage1$ = "testimage.png"
exportimage2$ = "testimage.bmp"
IF Result
= 1 THEN 'file already found on drive KILL exportimage1$
'delete the old file Result
= SaveImage
(exportimage1$
, 0, 0, 0, _WIDTH, _HEIGHT) 'save the new one againPRINT "Our initial Image"
IF Result
= 1 THEN 'file already found on drive KILL exportimage2$
'delete the old file Result
= SaveImage
(exportimage2$
, 0, 0, 0, _WIDTH, _HEIGHT) 'save the new one againPRINT "Our initial Image"
zz&
= _LOADIMAGE(exportimage1$
, 32) 'Even though we saved them in 256 color mode, we currently have to force load them as 32 bit images as _LOADIMAGE doesn't support 256 color pictures yet PRINT "Image Handle: "; zz&
, exportimage1$
PRINT "Successful Import using _LOADIMAGE" PRINT "ERROR - Not Loading the new image with _LOADIMAGE."
zz&
= _LOADIMAGE(exportimage1$
, 32) 'Even though we saved them in 256 color mode, we currently have to force load them as 32 bit images as _LOADIMAGE doesn't support 256 color pictures yet PRINT "Image Handle: "; zz&
, exportimage2$
PRINT "Successful Import using _LOADIMAGE" PRINT "ERROR - Not Loading the new image with _LOADIMAGE."
'$INCLUDE:'SaveImage.BM'
So what does v2.0 bring us that the last v1.7 didn't do?
* Minor bug fix when trying to save 32-bit PNG images of a SCREEN 0 text screen.
* Some additional error checking for... everything really.
* The new ability to save images in *.JPG format.
There's a lot of stuff packaged inside the library here, but the main ones which a user might decide to call is the following:
FUNCTION SaveImage (file$, image&, x1%, y1%, x2%, y2%)
SUB SaveFullImage (filename$)
SUB SaveFullBMP (filename$)
SUB SaveFullJPG (filename$)
SUB SaveBMP (filename$, image&, x1%, y1%, x2%, y2%)
FUNCTION TextScreenToImage256& (image&)
FUNCTION TextScreenToImage32& (image&)
FUNCTION Deflate$ (text$)
FUNCTION Inflate$ (text$)
FUNCTION PNGExport (file$, imagehandle%, x1%, y1%, x2%, y2%)
SUB SaveJPG (file$, image&, startx, starty, finishx, finishy)
As you can see, most of these routines can easily be handled by simply using
FUNCTION SaveImage, which then decides if it needs to convert from a text screen to a graphic screen, and which save routine to make use of. I mainly list them separately, in case anybody may need to call upon the main save routines directly to save a wee bit of processing time. (Why call a routine to check graphic mode and do a lot of error checking, which then simply hands off the process to another routine, when you can call the end routine correctly and save a few steps each cycle?)
FUNCTION Deflate$ compresses a string which we send it. Inflate turns that compressed string back into its original state for us.
FUNCTION SaveImage (file$, image&, x1%, y1%, x2%, y2%) is the main wrapper which handles our image processing.
file$ is the name of the file you want to save to on the disk. The filename needs to end with with ".jpg", ".bmp", "png" (capitalized, or not, it doesn't matter generally), so the routine knows what format you're wanting to save to on the disk.
image& is the handle of the image you want to save.
The other 4 values are the X/Y region you want to save to the disk.
FUNCTION TextScreenToImage256& converts a SCREEN 0 style text screen into a 256 color image for us.
FUNCTION TextScreenToImage32& works about the same as the above, except it converts that text screen to a 32-bit color image for us.
Error codes which the program might generate for you are listed below:
'-1 All is good. We think we exported a proper PNG file.
' 0 Compression failed. File is probably corrupt.
' 1 File Already Exists. As these are Binary files, we probably don't want to just overwrite the same file.
' 2 Incorrect Alpha settings. Check for Alpha mode, and Color mode, and look for conflicts.
' 3 Bad GrabMode
' 4 Bad x1 coordinate
' 5 Bad y1 coordinate
' 6 Bad x2 coordinate
' 7 Bad y2 coordinate
' 8 x2 < x1 -- correct this to proceed
' 9 y2 < y1 -- correct this to proceed
'10 Bad color mode. Either use 256 or 32 color mode.
'11 Attempted to export a text screen which will not work.
'12 PNG save is impossible on non-Windows systems. Can only use BMP or JPG
'13 Invalid extension, or lack of extension
Note that not all methods are going to report all the error codes. For example, it's impossible to get error 12 when saving a BMP or JPG file. If you do get an error message, take a look at the above and see if it helps you sort out what's wrong. If the error code doesn't help, kindly post the issue for me, and I'll take a look into the situation and see if I can help diagnose the problem and add a better message to the library for future releases.
If the included demo seems complicated, it's really not. For most people, the usage would be as simple as:
[code=qb64]
'$INCLUDE:'SaveImage.BI'
'Your stuff here
IF result
= 1 THEN 'file already exists on the drive KILL "filename.BMP" 'if this is what you want to do with it, to get rid of it. Else handle the problem differently result = SaveImage("filename.BMP, 0, 0, 0, _WIDTH, _HEIGHT) 'now try and resave the file, since we got rid of the previous version
END IF
'$INCLUDE:'SaveImage.BM'
[/code]
And I believe that's about it. If there's any questions, glitches, comments, or feedback, leave your concerns below and I'd address them ASAP for you. ;D