Author Topic: Save Image Library  (Read 3989 times)

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Save Image Library
« on: March 05, 2016, 06:10:22 pm »
Some of you may have already noticed the PNG Export library I posted earlier...  It's now gone completely.

Instead, I now offer the Save Image Library, for your free use and enjoyment!!

"Why the new library?", you ask.

Because it's now does a whole lot more than just PNG files!



Format for the command is the same (actually simpler, as we now auto-detect alpha channels and use RGBA or RGB automagically as needed), and looks like the following:

Result = SaveImage(exportimage1$, 0, 0, 0, _WIDTH, _HEIGHT)

The actual function is the following:   FUNCTION SaveImage (file$, image&, x1%, y1%, x2%, y2%)

So, how is this different than before?

First, it saves both PNG and BMP files for us.  Just specify the extension for which you want in the filename.

exportimage1$ = "testimage.png"
exportimage2$ = "testimage.bmp"

With the above, the first filename will export a PNG file, while the second will export a BMP file.   Change the extension, change the output format.  It's that simple.

Second, this now works in ALL the QB64 screen modes for both PNG and BMP export images.  That's right:  It can even export a screen capture of a SCREEN 0, TEXT screen for you!!

As a side note, the PNG side of things does a better job error-checking itself and all, since it also does file compression and all that stuff, so don't expect to get any error messages from saving a BMP file. 



PNG files can return the following error messages for you:

    'PNGExport results:
    '-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.

BMP files will always return a -1.  If they fail, it'll be a standard QB64 error message of some sort which will pop up.

Grab the attachment below, extract to your QB64 folder, and give it a go and see how it works for you.  From my testing, this works for all screen modes, in both SDL and GL versions, and hasn't died on me yet. 

All code is free to use, alter, delete, edit, and annoy Clippy with.  No credit expected, nor needed.  If you can use it, use it.  If you can sell it and make $$$ with it, do that too!  Just be warned:  If it somehow melts your computer or starts a zombie takeover, I take no responsibility for that either.  Code is "As is -- As is, you use it however you want, but you're responsible for whatever happens when you do."   :D



Usage is:

'$Include:'SaveImage.BI'    <--- Top of your code
Result = SaveImage(exportimage1$, 0, 0, 0, _WIDTH, _HEIGHT)  <--- Inside the code somewhere
'$INCLUDE:'SaveImage.BM'    <---  Bottom of your code
« Last Edit: April 02, 2016, 07:39:13 am by SMcNeill »
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

Pete

  • Hero Member
  • *****
  • Posts: 7431
  • Cuz I sez so varmint!
Re: Save Image Library
« Reply #1 on: March 05, 2016, 08:44:37 pm »
Quote
It can even export a screen capture of a SCREEN 0, TEXT screen for you!!

But I only work with jpegs.

Seriously, nice work, Steve.

Pete  ;D
Give a man a program, and he does one thing for a day. Teach a man to program, and he does one thing for a lifetime.

Clippy

  • Hero Member
  • *****
  • Posts: 19407
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Save Image Library
« Reply #2 on: March 05, 2016, 10:18:59 pm »
WHO gave you the right to call it SAVE IMAGE? Rob? We already had that for bitmaps.

Name it Farmer Image Library for all I care, you will only create confusion naming it that!
QB64 WIKI: Main Page
Download Q-Basics Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
QB64 SDL

TerryRitchie

  • Hero Member
  • *****
  • Posts: 2806
  • Press any key to continue or any other key to quit
Re: Save Image Library
« Reply #3 on: March 05, 2016, 10:40:36 pm »
Awesome library! This will be put to good use. Thank you!

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #4 on: March 06, 2016, 04:11:24 am »
Update to the library:

Since this includes the zlib1.dll which we use to compress the data for PNG files, I repacked a set of STRING compression tools into the library for us as well.

There's a demo included in the archive now, but it's a really straight forward process to use:

'$include:'saveimage.bi'    <-- Top of your code

de$ = Deflate(text$)    <--- To compress a string
t$ = Inflate(de$)   <--- To decompress it back to its original string size


'$include:'saveimage.bm'   <--  Bottom of your code



Note, you could easily use this to compress ANY data which you wanted with a simple _MEM or GET/PUT routine.

DIM Array(1 to 10000)  <-- an array
DIM m AS _MEM
m = _MEM(Array())
t$ = SPACE$(10000 * 4) 'The size of the array (10000 elements) * the data size (SINGLE by default so 4 bytes)
_MEMGET m, m.offset, t$ 'Put the array into a temp string

de$ = Deflate(t$)  '  Deflate that array now that we have it as a basic string

temp$ = Inflate(t$) ' Inflate it the same as always

_MEMPUT m, m.offset, temp$ 'Put it back into the array

_MEMFREE m  'Don't forget to free the memblock once finished with it
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #5 on: March 06, 2016, 04:19:07 am »
WHO gave you the right to call it SAVE IMAGE? Rob?

Not that I think anyone needs permission to use an variable/sub name that's the same as anyone elses, BUT...

Yep, he did.  Rob's SaveImage routine is free for public use, alteration, and modifications.  I altered it by ignoring it, and recreating it from scratch, which is well within the rights he granted the public with his work...

Is there a permission form somewhere that I can fill out to use variable names as documented in the wiki somewhere?  My program has a whole lot of loops which look like the following:

FOR i = 1 to 10

I never asked permission to use that variable "i" in my work before.  Should I have?



Library Checklist:
  • Functional
  • Easy to Use
  • Flexible
  • Something People Might Find Useful
  • Annoys Clippy

Check, Check, Check, Check, Check!

It's a 5-star library!  WOOT!
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #6 on: March 06, 2016, 07:42:21 am »
Updated to v1.2:

Some basic speed optimization done.

Added a TextScreenToImage function, in case anyone ever simply wants to convert their SCREEN 0 text screens over to a 256 color screen for some other purpose.

Usage is as simple as:

NewScreenHandle = TextScreenToImage(ScreenHandleToConvert)

or, more simply:

x = TextScreenToImage(0) 'And a quick example of how to turn a text screen into a 256 color image screen
SCREEN x 'swap screens
CIRCLE (320, 240), 100, 40 'And draw a circle on it
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

Billbo

  • Hero Member
  • *****
  • Posts: 1143
Re: Save Image Library
« Reply #7 on: March 06, 2016, 02:10:44 pm »
Steve,

Just downloaded and ran your new library. I says that the export of the png fails, but it's there. Look at the top-left corner of the attached bmp that created. The png is the one it's saying failed.

Bill

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #8 on: March 06, 2016, 02:17:24 pm »
Steve,

Just downloaded and ran your new library. I says that the export of the png fails, but it's there. Look at the top-left corner of the attached bmp that created. The png is the one it's saying failed.

Bill

You need the latest dirty build, or version from the repo for it to work.   It's trying to load the image, and QB64 won't do that without the latest changes which corrected a _LOADIMAGE glitch. 

666 is the error code for "Export worked, Import failed", so it's telling you what it should be.  :)

Grab the latest version of QB64 from http://www.qb64.net/ and you can import the image as well as export it.
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

Clippy

  • Hero Member
  • *****
  • Posts: 19407
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Save Image Library
« Reply #9 on: March 06, 2016, 03:40:14 pm »
MORE CONFUSION! A DIRTY build AND a library that is named the SAME as Rob's SaveImage SUB procedure that was actually supposed to become a QB64 keyword at one time!

PS: FREE code procedures DO NOT give others the right to just STEAL THE NAME as your own!
QB64 WIKI: Main Page
Download Q-Basics Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
QB64 SDL

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #10 on: March 06, 2016, 04:37:20 pm »
MORE CONFUSION! A DIRTY build AND a library that is named the SAME as Rob's SaveImage SUB procedure that was actually supposed to become a QB64 keyword at one time!

PS: FREE code procedures DO NOT give others the right to just STEAL THE NAME as your own!

There's a dirty build called "SaveImage"??   Or are you complaining that QB64 got fixed and now loads image files that it couldn't before??  Break your whine down into segments, if you will.

What's the issue with the dirty build being fixed so _LOADIMAGE works better?

What's the issue with a new routine replacing an old one with the same name?

People can tell the difference in $RESIZE, _RESIZE (sub), and _RESIZE (function); what makes you think they'll be confused and unable to tell the difference between a library found in the forums, and a sub found in the wiki??

And, regarding having the same name:   PISS OFF!  Do you think anyone is going to pay attention to someone WHO STOLE A REGISTERED TRADEMARK NAME, used it for their own, and then whines when someone else uses a freely available name...

Here's a solution for you:  Remove the obsolete version from the wiki and change it to a stub to point to this quicker, more flexible routine.  No more possible confusion then.  :P
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

Clippy

  • Hero Member
  • *****
  • Posts: 19407
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Save Image Library
« Reply #11 on: March 06, 2016, 05:29:48 pm »
No problem fixing _LOADIMAGE if you actually did and did not just patch it to work with PNG's in your "library".Couldn't be a problem with your "library"?

Wanna make it a QB64 statement or function (having true returned might be nice instead of an error) with an underscore?

Get permission from Rob and write it up in the WIKI. Then there is no confusion!
QB64 WIKI: Main Page
Download Q-Basics Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
QB64 SDL

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #12 on: March 06, 2016, 05:50:36 pm »
No problem fixing _LOADIMAGE if you actually did and did not just patch it to work with PNG's in your "library".Couldn't be a problem with your "library"?

Wanna make it a QB64 statement or function (having true returned might be nice instead of an error) with an underscore?

Get permission from Rob and write it up in the WIKI. Then there is no confusion!

Let's see:

1) You're right.  I altered QB64 just to break it so it'd match my broken library.  I also broke the SDL version, MS-Paint, and every other image loading program out there so they'd load those broken files...

2) Make it a keyword?  NEVER!!  Are you suggesting we bloat the language with new keywords?  BLASPHEMOUS!!

3) Write it in the wiki?  WHY?  Terry's menu library isn't there.  QBDbase isn't there.  The old PNGExport wasn't there.  They're here, in the forums where people can find them, post questions about them, report any errors they encounter, and get the latest version/update.

You know, if you'd just take your medication, there wouldn't be any confusion, as you're the only person who seems to confuse the library here with the routine in the wiki...
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

Clippy

  • Hero Member
  • *****
  • Posts: 19407
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
Re: Save Image Library
« Reply #13 on: March 06, 2016, 06:37:41 pm »
After you've already added a dozen useless ones, why not add one more?

With Galleon's permission of course. Are your $5 an hour immigrants too busy?
QB64 WIKI: Main Page
Download Q-Basics Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
QB64 SDL

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6241
Re: Save Image Library
« Reply #14 on: March 06, 2016, 09:54:29 pm »
After you've already added a dozen useless ones, why not add one more?

With Galleon's permission of course. Are your $5 an hour immigrants too busy?

Go pester Galleon for permission and then we'll talk.  Zlib1.dll was a library that used to come packaged with the SDL version.  Galleon removed it when he stripped out SDL and went the way of GL stand alone programs.  Adding *.dll files to QB64 that he personally removed seems quite disrespectful to me, and isn't something I'm going to do.

It's here.  It's an easy to include library.  That's good enough for me.

YOU want more?  You have my permission to alter whatever you'd like with it.  Feel free to work up the C-equivelant and add it to the language.  That's the joy of QB64 -- Galleon has a very open development policy.  Feel free to make whatever adjustments you need for your own use.  ;)
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.