• Welcome to Dark Bit Factory & Gravity.
 

How to use Get.

Started by Clyde, July 25, 2006

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Clyde

I'm playing with the wonderful tool box asset "PLoader".
And as I do alot of rendering using arrays, I'd like to be able to get the data from a loaded png and store it into an Array.
However I am running into difficulties, as there isnt alot to go on in the help docs.

I am doing something majorly wrong, but my brain cell can't quite work out what. Please use any image you like, for this I used a full screen png of 640x480


'
' Requires Ploader Created By Thrawn
'
#define use_ploadlib_bas

#ifdef use_ploadlib_bas
#include "ploadlib.bas"
#else
#include "ploadlib.bi"
#endif


Const XRES=640
Const YRES=480

Const ARES=XRES*YRES

Dim as any ptr img 'will contain the image loaded from the PNG

Declare Sub DrawScreen()


screenres XRES, YRES, 32,0,1
Setmouse 0,0,0

Dim Shared ScreenBuffer
ScreenBuffer= ScreenPtr()

Dim Shared ImageArray(XRES*YRES)
Dim Shared Images(XRES,YRES)

img = PLoad("media\640x480Image.png")

if img then
'    put (0,0), img, pset
   
    get img, step (0,0)- step (0,YRES), imagearray(0)
   
    imagedestroy img
else
    print "PNG 1 failed to load: Error " & Pload_GetError()
end if


Im not too sure on how the Get command works, I think it's something along the lines of setting out like a square / rectangle. Could be wrong, as the example the help file directs you to is the other command of Put, which isnt what I want to know about.

If you can put me out of my misery, thankyou kindly.
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

relsoft

#1
Try this:

get img,(0,0)- (XRES,YRES), @imagearray(0)

And decompressed image is 32 bits right? If so then

this:
Dim Shared ImageArray(XRES*YRES)
Should be changed to this:

Dim Shared  as ubyte  ImageArray(XRES*YRES * len(uinteger) + 4)
or

Dim Shared  ImageArray(XRES*YRES  + 4)
But the secon one wastes 12 bytes. :*(
The +4 is used to store the width and the height.




Challenge Trophies Won:

Clyde

Thanks Relsoft :)

This is my code so far, but it's not getting the pixel info. I omitted the +4 as the PNG image is a 640x480 ( fullscreen image )


'example program to illustrate how to use PLoadLib

'uncomment the next line to use the uncompiled ploadlib.bas file instead of
'the compiled library:

#define use_ploadlib_bas

#ifdef use_ploadlib_bas
#include "ploadlib.bas"
#else
#include "ploadlib.bi"
#endif


Const XRES=640
Const YRES=480

Const ARES=XRES*YRES

Dim Shared As Any Ptr img 'will contain the image loaded from the PNG
Dim Shared As UByte Ptr ScreenBuffer

Dim Shared As Ubyte ImageArray(ARES* len(uinteger) )'+ 4)
Dim Shared Images(XRES,YRES)

Declare Sub DrawScreen()
Declare Sub InitializeTest()
Declare Sub RunTest()

InitializeTest()
RunTest()
End


Sub InitializeTest()

    ScreenRes XRES, YRES, 32, 2, 1
    ScreenSet 1,0
    Setmouse 0,0,0

    ScreenBuffer= ScreenPtr()

    img = PLoad("media\640x480Image.png")

    If img Then
       
        Get img,(0,0) - (XRES,YRES), @imagearray(0)
       
        ImageDestroy img
   
    Else
   
        Print "PNG 1 failed to load: Error " & Pload_GetError()
   
    End if

    '
    ' Convert to 2 Dimensional array.
    '
    Dim x,y
   
    For y=0 To YRES-1
        For x=0 To XRES-1
       
            Images(x,y)=ImageArray( x + ( y * XRES ))
           
        Next
    Next

End Sub


Sub RunTest()

    Dim Key As String

    While Key<>Chr(27)
       
        ScreenLock()
        DrawScreen()
        ScreenUnlock()
       
        Key=Inkey()
       
        ScreenCopy 1,0
       
    Wend

End Sub
   


Sub DrawScreen()
     
     Dim x,y,col
     
     For y=0 To YRES-1
        For x=0 To XRES-1
         
            Col=Images(x,y)
     
            Poke Integer, ScreenBuffer + (( y *  XRES + x ) Shl 2), Col
   
        Next
    Next
   
End Sub
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

relsoft

AS long as you use GET you need the +4.
Challenge Trophies Won:

Clyde

#4
Does it work your end?

Just tried it some more with the the +4, and still a black screen dude.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Clyde

Greatest, thanks to a little chat with Stonemonkey sorted it.

If anyone is going to incorparate my method of rendering with this, here are the amendments:

The ImageArray should be:


Dim Shared ImageArray(ARES* len(uinteger) + 4)



Get img,(0,0) - (XRES,YRES), @imagearray(0)


Should be:

Get img,(0,0) - (XRES-1,YRES-1), @imagearray(0)


All should work.

Cheers,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

DrewPee

Hello!
Noticed this code and wondered if a finished piece of code could be published - Very useful if it does what i think it does?
Does it work something like the blitter used to on the Amiga? i.e. rip parts of a screen (in memory) and put it anywhere on the visual screen.
Something like a map maker using tiles?

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

psygate

I. as a FreeBASIC coder would suggest to you, that you shouldn't use an array, instead use the imagecreate command! It's faster and easier to access. :||
He who controlles the minds commands the many.
He who commands the many, conqueres the minds.

Clyde

What this routine does, is to instead of using Put to make an image out of the data fed to PLoad, and instead it bungs the info into an array for use with per pixel effects.

If I've time this week I'll post up an example.

Cheers,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

DrewPee

Thanks Clyde -  ;D

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

thrawn89

Quote from: psygate on July 31, 2006
I. as a FreeBASIC coder would suggest to you, that you shouldn't use an array, instead use the imagecreate command! It's faster and easier to access. :||

I. as the main developer of Pload tell you thats what it uses to make the Image buffer :-p

You can also do pixel effects on Image buffers pretty easily, however if you insist on pload returning an array, I wrote a wrapper for pload:

Screen 19, 32

DefInt A-Z
Option Explicit

#include "ploadlib.bi"

Declare Function ploadArray(Byval Filename As String, ReturnArray() As UByte) As Integer

ReDim As UByte ImageArray(0)

ploadArray("alphatest.png", ImageArray())

Put(0,0), @ImageArray(0), Alpha

Do: Loop While Inkey$ = ""
System

Function ploadArray(Byval Filename As String, ReturnArray() As UByte) As Integer
    Dim As Any Ptr Image
    Dim As Integer w, h, i
   
    Image = Pload(Filename)
   
    w = cptr(short ptr, Image)[0] SHR 3
    h = cptr(short ptr, Image)[1]
   
    ReDim ReturnArray((w * h * 4) + 4) As UByte
   
    For i = 0 To Ubound(ReturnArray)
        ReturnArray(i) = cptr(UByte ptr, Image)[i]
    Next i
   
    ImageDestroy(Image)
   
    Return (-1)
End Function


As you see its pretty simple to use, you dont even need to know how big the Image is...the function figures out everything for you...now Im not that experianced of a coder, and so Im sure theres a better way of dumping buffer data into the array than a for loop, but this works...

You know you could have saved yourself so much time if you used the email provided in the help docs of pload [this wrapper took me 5 mins to code]...its there for a reason lol...you have a better chance of reaching me there ;-)

Anyways, happy coding,
Thrawn

Shockwave

The lib is cool Thrawn, thanks for being around to give assistance where needed :)
Shockwave ^ Codigos
Challenge Trophies Won:

thrawn89

A little edit to the code...

Get rid of the For loop in the function and add this instead:

memcpy(@ReturnArray(0), Image, UBound(ReturnArray) + 1)

Also put this in the top:
#inlcude "crt.bi"

It should be faster ;-),
Thrawn

DrewPee

Thrawn,
Thanks for clearing that up - and like shockwave said above - thank you for being around!

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Shockwave

Nice tip to speed the loading up there Thrawn.
Shockwave ^ Codigos
Challenge Trophies Won:

ninogenio

cool i hadn`t noticed you can memcpy in freebasic!
Challenge Trophies Won:

thrawn89

lol Yeah, Freebasic can be as efficent and as powerful as C, sometimes even more so because its easier for the coder, gives you more options and more flexibility...it really gives C a run for its money...not to mention the basic syntax ;-)

Thrawn

Shockwave

Yep, this is true but it really desperately needs a tiny sound player like mini fmod as far as I'm concerned. Having to add the complete fmod dll to play a tracker track blows.
Shockwave ^ Codigos
Challenge Trophies Won:

thrawn89

http://www.freebasic.net/forum/viewtopic.php?t=3993&start=0 ;-) [it is called FBSound]

Also I remember about a month ago someone was talking about porting mini fmod...I tryied to find the topic but I couldnt find it...

Thrawn

Shockwave

Thanks Thrawn :)
It may be useful to play sound samples for now. Mini Fmod would indeed be the one as far as I'm concerned though.
Shockwave ^ Codigos
Challenge Trophies Won: