Code archives/Graphics/Resize image fast

This code has been declared by its author to be Public Domain code.

Download source code

Resize image fast by Filax2005
How resize an image ? It's a little bit rock n roll under bmax :)
but this is a simple method :)
' ------------
' Open graphic
' ------------
Graphics 1024,768,0

Global MyIMage01:Timage=LoadImage("colourblind.jpeg")

Global MyIMage02:Timage=ResizeImage(MyIMage01,256,256)


' -------------
' The main loop
' -------------
While Not KeyHit(KEY_ESCAPE)
	Cls

	DrawImage MyIMage02,0,0
	
	' ------------------------
	' Swap buffer and flushmem
	' ------------------------
	Flip
Wend 


Function ResizeImage:Timage(Image:TImage,Tx:Int,Ty:Int)
	Local x
	Local y
	Local c
	Local Pixmap:TPixmap
	Local Duplicate:TPixmap
		
	Local Output:Timage

	Duplicate=CreatePixmap(ImageWidth(Image),ImageHeight(Image),PF_RGB888)
	Pixmap = LockImage(Image)
	
	For x = 0 To ImageWidth(Image) - 1
		For y = 0 To ImageHeight(Image) - 1
			
			c=ReadPixel(Pixmap, x, y)
			WritePixel(Duplicate, x,y,c)
			
		Next			
	Next

	UnlockImage(Image)
	Pixmap= Null
	
	
	Resized=ResizePixmap(Duplicate,Tx,Ty)
	
	Output=CreateImage(Tx,Ty)
	Pixmap=LockImage(Output)
	
	For x = 0 To Tx-1
		For y = 0 To Ty-1 
			c=ReadPixel(Resized, x, y)
			WritePixel(Pixmap, x,y,c)
		Next			
	Next

	UnlockImage(Output) 
	
	Duplicate= Null	
	Pixmap= Null
	
	Return Output
End Function

Comments

skidracer2005
or even simpler:
Function ResizeImage:TImage(image:TImage,width,height)
	Local pix:TPixmap=LockImage(image)
	pix=ResizePixmap(pix,width,height)
	UnlockImage(image)	
	Return LoadImage(pix)
End Function



Kuron2005
Thanks, Skid :c)


tonyg2005
Can it be done without a 'size up' making the image more blurry each time?


bruZard2005
@tonyg: of course...
Strict

Framework BRL.GlMax2d

Import BRL.JpgLoader
Import BRL.Retro

SetGraphicsDriver GLMax2DDriver()
Graphics 800,600,0,0
SetBlend ALPHABLEND

Global pic:TImage = LoadImage("pic.jpg")
SeedRnd MilliSecs()

Repeat
	Cls
	
	If KeyHit(KEY_SPACE)
		Local width:Short = Rnd(320,800)
		pic = ResizeImage(pic,width)
	EndIf
	
	DrawImage pic,0,0
	
	Flip
	FlushMem()
Until KeyHit(KEY_ESCAPE)
End


Function ResizeImage:TImage(image:TImage,new_width:Short)
	Global backup:TIMage
	If backup = Null Then backup = image
	
	Local new_height:Short = (backup.height*new_width/backup.width)
	
	Local pix:TPixmap=LockImage(backup)
	pix=ResizePixmap(pix,new_width,new_height)
	UnlockImage(backup)
		
	Return LoadImage(pix)
End Function



tonyg2005
Looks like that has the same problem BruZard.
Function ResizeImage:TImage(image:TImage,width,height)
	Local pix:TPixmap=LockImage(image)
	pix=ResizePixmap(pix,width,height)
	UnlockImage(image)	
	Return LoadImage(pix)
End Function
SetGraphicsDriver GLMax2DDriver()
Graphics 800,600,0
image:TImage=LoadImage("max.png",filteredimage)
While Not KeyHit(key_escape)
  Cls
  If KeyHit(key_space)
     If ImageWidth(image)=256 
        image=resizeimage(image,128,128)
     Else
        image=resizeimage(image,256,256)
     EndIf
  EndIf
  DrawImage image,0,0
  Flip
  FlushMem
Wend

Keep pressing spacebar and notice the 256*256 image gets progressively more blurry.


bruZard2005
No, have a look on my post ... i have made a backup from the image and store that as global. After the first resize i ever use the backup and not the image.

excuse my bad english...


tonyg2005
Yep, sorry. It looks good. No idea what I was doing before.


Code Archives Forum