Page 1 of 1

Resize images faster

Posted: Fri Feb 22, 2008 7:15 am
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

Posted: Fri Feb 22, 2008 7:39 am
by Kaeru Gaman
this seems to be not a trick-or-tip but a question, isn't it?

Posted: Fri Feb 22, 2008 7:58 am
by Mistrel
It's a trick but a question to see if someone can make it even faster.

Re: Resize images faster

Posted: Fri Feb 22, 2008 9:37 am
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. ;)

Removed

Posted: Sat Feb 23, 2008 3:40 am
by Dr_Wildrick
Removed

Posted: Wed Apr 08, 2009 10:36 am
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

Posted: Wed Apr 08, 2009 4:11 pm
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.

Posted: Wed Apr 08, 2009 5:09 pm
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

Posted: Wed Apr 08, 2009 5:16 pm
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: