DecodeImage for EncodeImage

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
tj1010
Enthusiast
Enthusiast
Posts: 716
Joined: Mon Feb 25, 2013 5:51 pm

DecodeImage for EncodeImage

Post by tj1010 »

Since this doesn't work and there is a EncodeImage function that's not actually useful for anything practical.

Code: Select all

InitNetwork()
*buffer=ReceiveHTTPMemory("http://www.purebasic.fr/english/styles/subsilverPlus/imageset/purebasic_logo.png")
Debug CatchImage(0,*buffer,MemorySize(*buffer))
FreeMemory(*buffer)
Game server stored profile avatars is one use for it. Without having to build and manage a tmp cache and loading.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DecodeImage for EncodeImage

Post by wilbert »

Maybe a stupid question but did you add UsePNGImageDecoder() ?
I see no other reason why your code wouldn't work.
Windows (x64)
Raspberry Pi OS (Arm64)
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: DecodeImage for EncodeImage

Post by Thade »

As wilbert said

Code: Select all

UsePNGImageDecoder()

InitNetwork()

WinWidth=800
WinHeight=600
WinTitle.s="Test"

window1= OpenWindow(-1, 4, 4, WinWidth, WinHeight, WinTitle, #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
If window1
	Start1=ButtonGadget(#PB_Any, 5, 5, 190, 20,"Start")
	Ende1=ButtonGadget(#PB_Any, 405, 5, 190, 20,"Ende")
	Aktuell1=StringGadget(#PB_Any, 5, 60, WinWidth-20,20,"")
	
	
	Quit = 0
	
	Repeat
		EventID = WindowEvent()
		If EventID=#PB_Event_Gadget
			GadgetNumber=EventGadget()
			Select GadgetNumber
				Case Start1
					*buffer=ReceiveHTTPMemory("http://www.purebasic.fr/english/styles/prosilver/imageset/pblogobb.png")
					If *buffer
						img=CatchImage(-1, *buffer)
						FreeMemory(*buffer)
						If img
							img1=ImageGadget(-1, 0, 100, ImageWidth(img), ImageHeight(img), ImageID(img))
							SetGadgetText(Aktuell1, Str(img))
						EndIf
					EndIf
					
				Case Ende1
					Quit=1
					
			EndSelect
		Else
			Delay(1)
		EndIf
		
		If EventID = #PB_Event_CloseWindow
			Quit = 1
		EndIf
		
	Until Quit = 1
	
EndIf

End

--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
tj1010
Enthusiast
Enthusiast
Posts: 716
Joined: Mon Feb 25, 2013 5:51 pm

Re: DecodeImage for EncodeImage

Post by tj1010 »

You guys are right it was me just not realizing I didn't include the dependency. It still would be a nice function.

This is especially useful for network stored bitmaps. I store them in Base64 in JSON objects on a game server for one project.

Code: Select all

UsePNGImageDecoder()

If ReadFile(0,"b64.txt");base64 of image in a text file
  *buffer=AllocateMemory(Lof(0))
  If ReadData(0,*buffer,Lof(0))
    CloseFile(0)
    *output=AllocateMemory(MemorySize(*buffer)-((MemorySize(*buffer)*25)/100))
    Base64Decoder(*buffer,MemorySize(*buffer),*output,MemorySize(*output))
    FreeMemory(*buffer)
    If CatchImage(0,*output)
      FreeMemory(*output)
      If OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
        ImageGadget(0,0,0,ImageWidth(0),ImageHeight(0),ImageID(0))
        Repeat
          event=WaitWindowEvent()
        Until event=#PB_Event_CloseWindow
      EndIf
      FreeImage(0)
    EndIf
  EndIf
EndIf
End
tj1010
Enthusiast
Enthusiast
Posts: 716
Joined: Mon Feb 25, 2013 5:51 pm

Re: DecodeImage for EncodeImage

Post by tj1010 »

After revisiting a project that uses this I realize you have to use SaveImage() to write CreateImage() memory to disk then load back in with ReadData() to encode. The request is still kind of valid. Of course that's without writing API code for each platform.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DecodeImage for EncodeImage

Post by wilbert »

tj1010 wrote:After revisiting a project that uses this I realize you have to use SaveImage() to write CreateImage() memory to disk then load back in with ReadData() to encode.
Can you explain more in detail ? I don't exactly understand the problem.
You can use EncodeImage() to encode an image in memory and if you need it to be Base64, you can use the result of EncodeImage() as input for Base64Encoder() .
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply