Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Jun 22, 2023 15:26:30 GMT
PNG32, ICO32 and BMP32 saveimage functions Only 32-bit images work! Not tested on other bit-depths. WIP - colours are not correct - Fixed (__P was not LONG) QB64 couldn't load image - Fixed (credit - visionmercer )
image.bm (5.2 KB)
|
|
|
Post by bee on Jun 22, 2023 15:48:35 GMT
Is this code different than one in Wiki?
|
|
|
Post by bee again on Jun 22, 2023 15:52:06 GMT
|
|
Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Jun 22, 2023 16:11:07 GMT
Not much as the wiki one only supports 24-bit bitmaps
|
|
|
Post by bee again on Jun 22, 2023 18:18:19 GMT
So the missing byte are alpha's? That need _Unsigned Long type to do full range of _RGB32?
|
|
Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Jun 22, 2023 21:41:22 GMT
Yes
|
|
|
Post by visionmercer on Jun 23, 2023 17:50:51 GMT
Nice bit of coding, it's great to be able to save an image without having to include a library that is 50 times bigger than the program itself.
The png routine produces files with errors and the images can't be loaded in qb64. Here is a fix:
Sub PNG (__FILENAME As String, __IMAGE As Long) Dim As String __PNGHEADER, __IHDR, __IDAT, __IMAGDATA, __IEND Dim As String __EXT dim as string chunksize Dim As Long __WIDTH, __HEIGHT, __F, __SOURCE, __OFFSET, __X, __Y,__P __PNGHEADER$ = Chr$(&H89) + Chr$(&H50) + Chr$(&H4E) + Chr$(&H47) + Chr$(&H0D) + Chr$(&H0A) + Chr$(&H1A) + Chr$(&H0A) __WIDTH = _Width(__IMAGE): __HEIGHT = _Height(__IMAGE) __F = FreeFile If LCase$(Right$(__FILENAME, 4)) <> ".png" Then __EXT = ".png" Else __EXT = "" Open __FILENAME + __EXT For Output As #__F: Close #__F Open __FILENAME + __EXT For Binary As #__F Put #__F, , __PNGHEADER$ __IHDR = "IHDR" + Reverse$(MKL$(__WIDTH)) + Reverse$(MKL$(__HEIGHT)) + Chr$(&H08) + Chr$(&H06) + String$(3, 0) __IHDR = Reverse$(MKL$(&H0D)) + __IHDR + Reverse$(MKL$(crc32(__IHDR))) Put #__F, , __IHDR __SOURCE = _Source _Source __IMAGE __IMAGDATA = Space$(__HEIGHT * __WIDTH * 4 + __HEIGHT) __OFFSET = 1 For __Y = 1 To __HEIGHT Step 1 Mid$(__IMAGDATA, __OFFSET, 1) = Chr$(&H00) __OFFSET = __OFFSET + 1 For __X = 1 To __WIDTH Step 1 __P = Point(__X - 1, __Y - 1) Mid$(__IMAGDATA, __OFFSET, 4) = Chr$(_Red32(__P)) + Chr$(_Green32(__P)) + Chr$(_Blue32(__P)) + Chr$(_Alpha32(__P)) __OFFSET = __OFFSET + 4 Next __X Next __Y _Source __SOURCE __IDAT = _Deflate$(__IMAGDATA) chunksize = Reverse$(MKL$(Len(__IDAT))) __IDAT = "IDAT" + __IDAT __IDAT = __IDAT + Reverse$(MKL$(crc32(__IDAT))) __IDAT = chunksize + __IDAT Put #__F, , __IDAT __IEND = Reverse$(MKL$(&H00)) + "IEND" + Reverse$(MKL$(&HAE426082)) Put #__F, , __IEND Close #__F End Sub
|
|
Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Jun 24, 2023 2:46:29 GMT
Thanks. I may have forgot that "IDAT" will not by counted in the length. I have updated the attachment.
|
|
|
Post by bplus on Jun 24, 2023 19:28:17 GMT
Hey that's great! It does load for me? _Title "Test Image Save" ' b+ 2023-06-24 Screen _NewImage(800, 600, 32) save& = _NewImage(800, 600, 32) Color &HFF000000, &HFFFFFFFF: Cls For i = 1 To 100 Line (Rnd * _Width, Rnd * _Height)-(Rnd * _Width, Rnd * _Height), _RGB32(Rnd * 255, Rnd * 255, Rnd * 255, Rnd * 150), BF Next _PutImage , 0, save& PNG "test save", save& Print "File is ready, zzz" Sleep Cls Print "zzz..." Sleep load& = _LoadImage("test save.PNG") 'check load of image _PutImage , load&, 0 Print "Image loaded from file."
'$include:'image.bm' ![](//storage.proboards.com/7292714/thumbnailer/lSNyPYTsdgjsnxXdYIvn.png) Attachments:Test SaveImage.zip (1.89 KB)
|
|
|
Post by bplus on Jun 25, 2023 13:38:31 GMT
Aaditya Parashar and visionmercer a couple of questions: What are all those underlines for? IMHO they mess up code but I learned to appreciate QB64 differentiating between QB64 new things with starting underline and old QB code keywords. I would like permission to share this code at other forums for QB64 extremely helpful subs and functions and intend to credit names with you both. I like the condensed PNG code which is a lot smaller and I think easier to use that what I use now. I have a couple more tests to check saving images according to size and taking sections from screen. Wouldn't it be nice if this were built into QB64? hint, hint ;-))
|
|
Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Jun 26, 2023 5:51:11 GMT
bplusThose underlines are used as they would be less likely to occur in shared variables. You can post this code at other forums, modify it, do whatever you want... Sorry for bad english, its not my native language.
|
|
|
Post by bplus on Jun 26, 2023 11:59:34 GMT
bplus Those underlines are used as they would be less likely to occur in shared variables. You can post this code at other forums, modify it, do whatever you want... Sorry for bad english, its not my native language. Ah, less likely to be shared. OK Thankyou ![:)](//storage.proboards.com/forum/images/smiley/smiley.png)
|
|
|
Post by Spriggsy on Jun 26, 2023 12:33:39 GMT
I do the same thing. I'll make some variables have two underscores in front to make sure that someone else won't be using the same name in another library or in their main code. Also, it makes sure that the code is $NOPREFIX compatible, if the variable has the same name as a reserved keyword.
|
|
|
Post by bplus on Jun 26, 2023 16:22:25 GMT
I've thought about making variables unique by using the GoSub, Sub/Function name as Prefix to variable. I think that is how it's done when translating our bas file code over to C++.
Is there a trick for counting the number of underlines you have? I ask because my eyes get crossed reading that underlined crazy code. That's just reading it, I am imaging coding it as nightmare but so too whole GoSub, Sub/Function name prefixes.
|
|
Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Jun 26, 2023 18:15:25 GMT
Evey character has a fixed width in qb64 font, just compare it with the line above you will get the number of underscores. I mostly use 2 for variables.
|
|