I am the Forgotten Coder and after searching this forum i couldn't find a simple sub that loads a png file into an image. Maybe i didn't search enough ...

So my challenge to this community of coders is to "Make a sub with the name LoadPNG to load PNG files to an image".Made some starting code for this challenge to avoid people saying "We do the work and this guy does nothing" LOL.

I think this challenge will benefit everyone is this community. I am doing this not just for me. :)

Thanks for tips and advices. I will look for that fbimage library and test it.

For now i am enjoying this idea of making this sub. Testing first bytes of a png file 32x32 made with gimp application. A black limited square painted in yellow. I know this will get more complicated from now on. Need help. Maybe we can make this sub/function for the next version of FB. LOL Just kiding i am not the boss of anything in here. :)

If Open(file For Binary Access Read As #1) = 0 Then print "open file with sucess"

passed=0

'********************************** 'read png header and validate it '********************************** get #1, ,ub if hex(ub)="89" then passed=passed+1 get #1, ,ub if hex(ub)="50" then passed=passed+1 get #1, ,ub if hex(ub)="4E" then passed=passed+1 get #1, ,ub if hex(ub)="47" then passed=passed+1 get #1, ,ub if hex(ub)="D" then passed=passed+1 get #1, ,ub if hex(ub)="A" then passed=passed+1 get #1, ,ub if hex(ub)="1A" then passed=passed+1 get #1, ,ub if hex(ub)="A" then passed=passed+1

if passed=8 then print "it's a png file" else print "this is not a png file" endif print

i think creating the entire PNG loading routine would be pretty involved, especially considering the various(?) possible compression techniques... though you have a good start on the header already... still would suggest just using the library... MUCH simpler and you can continue on with your program instead of being sidetracked (something that occurs to me all the time...)

#include "file.bi"'An idea from UEZ in another thread.Function Pload(Picture as String,byref i as any ptr=0) as long Dim As uinteger TMP GDIPLUS.GdiplusStartup(@TMP,@type<GDIPLUS.GDIPLUSSTARTUPINPUT>(1),0) Dim as any Ptr Img if GDIPLUS.GdipLoadImageFromFile(Picture,@Img)>0 then return 0 Dim As Single w,h GDIPLUS.GdipGetImageDimension(Img,@w,@h) if w*h=0 then return 0 Dim As GDIPLUS.BitmapData Pdata Dim As Rect R=Type(0,0,w-1,h-1) GDIPLUS.GdipBitmapLockBits(Img,Cast(Any Ptr,@R),GDIPLUS.ImageLockModeRead,PixelFormat32bppARGB,@Pdata) For y as long = 0 To h-1 For x as long = 0 To w-1 pset i,(x,y),Cast(ulong Ptr,Pdata.Scan0)[y*w+x] Next Nextreturn w*hEnd Function

dodicat wrote:If you use windows, then keeping things as simple as possible, please try this.Does other formats also.

What?! So... if someone is using Windows, then this simple routine will work instead of having to force folks to use a library (FBimage)? which makes the program complete unto itself... never seen this in all my years here... will give it a go... does it matter 32 or 64 bit?

What other formats will it load?

Last edited by leopardpm on Jan 12, 2019 23:04, edited 1 time in total.

Most of the time i am using libs ,ike cairo, fltk-c or fbimage where .png is included, so no need to care for this. And they all are platform independent. Maybe you decide one day to switch to linux or mac like i did. When only surfing or programming with geany i simply not notice a real difference in the view, because they are the same programs. But Linux Mint boots up much faster and it is a pain to hear windows rattling all the time on the HD. So why programming windows only if we can do it platform independent with the same effort?

I forgot to download Imagemagick on my win10, so I'll have to. Anyway, in my opinion the easiest way is to shellexecute a Imagemagick command to do the conversion to a BMP. There is some test to do maybe because I don't know what bmps know about transparency. However all the code for conversion is already done and in any platform it will/should work.https://imagemagick.org/script/convert.php

Use the convert program to convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. SeeCommand Line Processing for advice on how to structure your convert command...

In .../examples/files/libpng there is png.basThis uses png.bi [ #include once "png.bi" ]

If I run this it get:* ... incompatible linbpng version ... (in the grahics window)* libpng warning: Application was compiled with png.h from libpng-1.6.18* libpng warning: Application is running with png.c from libpng-1.2.54

So, I could add #define __LIBPNG_VERSION 12 in png.bas, before #include once "png.biOr change this is png.biOr just replace #include once "png.bi" with #include once "png12.bi"

And it works here. You do need however "libz" (or zlib) and 'libpng", else you get:ld: cannot find -lpngld: cannot find -lzCompilation failed.