Resize images faster

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Resize images faster

Post by Mistrel »

You can resize your images faster by creating them again with new dimensions instead of using ResizeImage(). This function can be slow because it has to spend time scaling each pixel.

Does anyone know of how this might be made even faster with API?

Code: Select all

OpenWindow(0,0,0,320,240,"WinTitle",#PB_Window_SizeGadget)
CreateGadgetList(WindowID(0))
CreateImage(image,20,20,32)
ImageGadget(imagegadget,0,0,50,50,ImageID(image))

ResizeImage(image,40,40)
SetGadgetState(imagegadget,ImageID(image))

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
	Result=#PB_ProcessPureBasicEvents
	Select uMsg
		Case #WM_SIZING
			width=WindowWidth(0)
			height=WindowHeight(0)
			If width<=0
				width=1
			EndIf
			If height<=0
				height=1
			EndIf
			;ResizeImage(image,width,height) ; slow
			CreateImage(image,width,height)  ; fast
			StartDrawing(ImageOutput(image))
			Box(0,0,width,height,RGB(79,79,79))
			StopDrawing()
			SetGadgetState(imagegadget,ImageID(image))
	EndSelect
	ProcedureReturn Result
EndProcedure

SetWindowCallback(@WinCallback())
Repeat
	WaitWindowEvent()
ForEver
Last edited by Mistrel on Fri Feb 22, 2008 7:59 am, edited 3 times in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

this seems to be not a trick-or-tip but a question, isn't it?
oh... and have a nice day.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

It's a trick but a question to see if someone can make it even faster.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Resize images faster

Post by PB »

> ;ResizeImage(image,width,height) ; slow
> CreateImage(image,width,height) ; fast
> StartDrawing(ImageOutput(image))
> Box(0,0,width,height,RGB(79,79,79))
> StopDrawing()

This is only faster when resizing your own "drawed" images. It won't work
if you want to resize a BMP or JPG image, for example. Just to clarify. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dr_Wildrick
User
User
Posts: 36
Joined: Fri Feb 23, 2007 8:00 pm
Location: New York

Removed

Post by Dr_Wildrick »

Removed
User avatar
Michael Vogel
Addict
Addict
Posts: 2799
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Fast image functions would be really fine, for instance a resize function, where one side wont change (e.g. the images height have to be resized)

I'm wondering, why this does not work (DrawImage should allow to resize on the fly)...

Code: Select all

OpenWindow(0,0,0,320,240,"WinTitle",#PB_Window_SizeGadget)

Enumeration
	#imagebase
	#imagegadget
	#image
EndEnumeration

CreateImage(#imagebase,60,60,32)
StartDrawing(ImageOutput(#imagebase))
For i=0 To 29
	Box(i,i,60-i<<1,60-i<<1,Random($ffffff))
Next i
StopDrawing()
ImageGadget(#imagegadget,0,0,60,60,ImageID(#imagebase))

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
	Result=#PB_ProcessPureBasicEvents
	Select uMsg
	Case #WM_SIZING
		width=WindowWidth(0)
		height=WindowHeight(0)
		If width<=0
			width=1
		EndIf
		If height<=0
			height=1
		EndIf

		If 1
			ResizeImage(#imagebase,width,height) ; slow
			SetGadgetState(#imagegadget,ImageID(#imagebase))
		Else
			CreateImage(#image,width,height)  ; fast
			StartDrawing(ImageOutput(#image))
			DrawImage(ImageID(#imagebase),0,0,width,heigth)
			StopDrawing()
			SetGadgetState(#imagegadget,ImageID(#image))
		EndIf
	EndSelect
	ProcedureReturn Result
EndProcedure

SetWindowCallback(@WinCallback())
Repeat
	WaitWindowEvent()
ForEver
User avatar
Demivec
Addict
Addict
Posts: 4264
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Michael Vogel wrote:I'm wondering, why this does not work (DrawImage should allow to resize on the fly)...
It does work, you have mispelled the 'height' variable in the DrawImage() call. Also, you didn't make it possible to end your program, when it is not being debugged, without the task manager.
User avatar
Michael Vogel
Addict
Addict
Posts: 2799
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Demivec wrote:[...]you have mispelled the 'height' [...]
correct, what a shame :shock:
Demivec wrote:[...]you didn't make it possible to end your program
right again, but hopefully not that big issue - we're experts :lol:

So that's it now...

Code: Select all

OpenWindow(0,0,0,320,240,"WinTitle",#PB_Window_SizeGadget)

Enumeration
	#imagebase
	#imagegadget
	#image
EndEnumeration

CreateImage(#imagebase,60,60,32)
StartDrawing(ImageOutput(#imagebase))
For i=0 To 29
	Box(i,i,60-i<<1,60-i<<1,Random($ffffff))
Next i
StopDrawing()
ImageGadget(#imagegadget,0,0,60,60,ImageID(#imagebase))

Procedure WinCallback(hWnd, uMsg, wParam, lParam)
	Result=#PB_ProcessPureBasicEvents
	Select uMsg
	Case #WM_SIZING
		width=WindowWidth(0)
		height=WindowHeight(0)
		If width<=0
			width=1
		EndIf
		If height<=0
			height=1
		EndIf

		If GetAsyncKeyState_(#VK_SHIFT)&$80
			ResizeImage(#imagebase,width,height,#PB_Image_Raw);	"slow"
			SetGadgetState(#imagegadget,ImageID(#imagebase))
		Else
			CreateImage(#image,width,height);							"fast"
			StartDrawing(ImageOutput(#image))
			DrawImage(ImageID(#imagebase),0,0,width,height)
			StopDrawing()
			SetGadgetState(#imagegadget,ImageID(#image))
		EndIf
	EndSelect
	ProcedureReturn Result
EndProcedure

SetWindowCallback(@WinCallback())
Repeat : Until WaitWindowEvent()=#WM_CHAR

And because DrawImage does the resizing "brute force", I've changed the ResizeImage mode as well -- I think, both methods need about the same time now...

So a faster smooth resizing is still not found :?

Michael
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Michael Vogel wrote:
Demivec wrote:[...]you have mispelled the 'height' [...]
correct, what a shame :shock:
You should consider using EnableExplicit in the future. :wink:
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply