DLL question about create image

Just starting out? Need help? Post your questions and find answers here.
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

DLL question about create image

Post by marc_256 »

Hi,

I want to create a .DLL to include all my cad/cam controls.
I never used .dll files, so ?? :oops: :?

Also all my image stuff,
example:
for create an image with backcolor
data to send to the DLL
1) image number
2) image width
3) image height
4) image backcolor

ms_create_image(1, 800, 600, $00000000)

inside the DLL i will use the procedure ms_create_image(number.w, width.w, height.w, backcolor.l)

Q1)
is it possible to create an image inside DLL, and how to receive this image data back inside PB main program ?


thanks,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: DLL question about create image

Post by Joris »

One way is to preserve a memory-block and pass the adress to the dll in which you then do the drawings and recieve that after the drawings are finished.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: DLL question about create image

Post by netmaestro »

DLL code:

Code: Select all

ProcedureDLL.i ReturnImage(w, h, depth, backcolor)
  tmp=CreateImage(#PB_Any, w, h, depth, backcolor)
  UsePNGImageEncoder()
  Global *this
  *this = EncodeImage(tmp,#PB_ImagePlugin_PNG)
  ProcedureReturn *this
EndProcedure
Calling Code:

Code: Select all

Prototype ReturnImage(w,h,d,bkg)
OpenLibrary(0, "test.dll")
Global ReturnImage.ReturnImage = GetFunction(0, "ReturnImage")
*this = ReturnImage(64,64,24,#Red)
UsePNGImageDecoder()
CatchImage(0, *this)
OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,ImageID(0))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
Guaranteed to possibly work, maybe if you hold your mouth juuuust.. right.

Nah, it works.
BERESHEIT
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: DLL question about create image

Post by luis »

Nice idea netmaestro, the encoding in the DLL to be able to catch it from the client and get this way a valid PB image bypassing the DLL barrier.
I didn't think of that, can be useful.

Uhm... on closer inspection while the idea is valid I see a couple of problems.

The first one is easy to fix ... you should just free the image in the DLL after the encoding.

The other one is the buffer allocated in the DLL to encode the image is never released, so one should make available a freememory() alias callable from the client.

Code: Select all

ProcedureDLL.i ReturnImage (w, h, depth, backcolor)
 Protected *this
 Protected tmp = CreateImage(#PB_Any, w, h, depth, backcolor)
 
 UsePNGImageEncoder()       
 *this = EncodeImage(tmp, #PB_ImagePlugin_PNG)
 FreeImage(tmp)
 ProcedureReturn *this
EndProcedure

ProcedureDLL FreeMemoryDLL (*add)
 FreeMemory(*add)   
EndProcedure

Code: Select all

Prototype ReturnImage(w,h,d,bkg)
Prototype FreeMemoryDLL(*add)

UsePNGImageDecoder()

OpenLibrary(0, "dll.dll")
Global ReturnImage.ReturnImage = GetFunction(0, "ReturnImage")
Global FreeMemoryDLL.FreeMemoryDLL = GetFunction(0, "FreeMemoryDLL")

*this = ReturnImage(256,256,24,#Red)
CatchImage(0, *this)
FreeMemoryDLL(*this)


OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,ImageID(0))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: DLL question about create image

Post by luis »

Even if in this case I prefer netmaestro's suggestion, this is another possible approach which could be useful in other cases.
I'll use the image example anyway to make it more easily comparable.

In the above the image is created in the DLL and then re-created from the encoded data format in the client, in the following the image is created directly in the client instead, by passing the address of an helper client function to the DLL.

Code: Select all


Prototype.i CreateImageClient(n, w, h, depth, back)
Global CreateImageClient.CreateImageClient

Structure DLL_INIT
 CreateImageClient.i
EndStructure

ProcedureDLL.i ReturnImage (w, h, depth, backcolor) 
 Protected r, img = CreateImageClient(#PB_Any, w, h, depth, backcolor)

 StartDrawing(ImageOutput(img))
    If w > h
        r = h /2
    Else
        r = w /2
    EndIf
    Circle(w/2, h/2, r, #White) 
 StopDrawing()

 ProcedureReturn img
EndProcedure

ProcedureDLL InitDLL (*init.DLL_INIT)
 CreateImageClient = *init\CreateImageClient
EndProcedure

Code: Select all

Structure DLL_INIT ; this way you can communicate further stuff to the dll if required 
 CreateImageClient.i
EndStructure : Global DI.DLL_INIT

Prototype ReturnImage(w,h,d,bkg)
Prototype InitDLL (*init.DLL_INIT)

Procedure.i CreateImageClient (n, w, h, depth, back)
 ProcedureReturn CreateImage (n, w, h, depth, back)
EndProcedure

OpenLibrary(0, "dll.dll")
Global ReturnImage.ReturnImage = GetFunction(0, "ReturnImage")
Global InitDLL.InitDLL = GetFunction(0, "InitDLL")

DI\CreateImageClient = @CreateImageClient()
InitDLL(@DI)

Define img = ReturnImage(256,256,24,#Red)

OpenWindow(0,0,0,ImageWidth(img),ImageHeight(img),"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,ImageID(img))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
"Have you tried turning it off and on again ?"
A little PureBasic review
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: DLL question about create image

Post by marc_256 »

hi netmaestro and luis,

I work nights now, so, just home...
thanks for the wonderful programs.
After some hours of sleep I will test them.

THANKS,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Post Reply