December 03, 2020, 07:39:08 PM

Author Topic: [bmx] Resize image fast by Filax [ 1+ years ago ]  (Read 785 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Resize image fast by Filax [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Resize image fast
Author : Filax
Posted : 1+ years ago

Description : How resize an image ? It's a little bit rock n roll under bmax :)
but this is a simple method :)


Code :
Code: BlitzMax
  1. ' ------------
  2. ' Open graphic
  3. ' ------------
  4. Graphics 1024,768,0
  5.  
  6. Global MyIMage01:Timage=LoadImage("colourblind.jpeg")
  7.  
  8. Global MyIMage02:Timage=ResizeImage(MyIMage01,256,256)
  9.  
  10.  
  11. ' -------------
  12. ' The main loop
  13. ' -------------
  14. While Not KeyHit(KEY_ESCAPE)
  15.         Cls
  16.  
  17.         DrawImage MyIMage02,0,0
  18.        
  19.         ' ------------------------
  20.         ' Swap buffer and flushmem
  21.         ' ------------------------
  22.         Flip
  23. Wend
  24.  
  25.  
  26. Function ResizeImage:Timage(Image:TImage,Tx:Int,Ty:Int)
  27.         Local x
  28.         Local y
  29.         Local c
  30.         Local Pixmap:TPixmap
  31.         Local Duplicate:TPixmap
  32.                
  33.         Local Output:Timage
  34.  
  35.         Duplicate=CreatePixmap(ImageWidth(Image),ImageHeight(Image),PF_RGB888)
  36.         Pixmap = LockImage(Image)
  37.        
  38.         For x = 0 To ImageWidth(Image) - 1
  39.                 For y = 0 To ImageHeight(Image) - 1
  40.                        
  41.                         c=ReadPixel(Pixmap, x, y)
  42.                         WritePixel(Duplicate, x,y,c)
  43.                        
  44.                 Next                   
  45.         Next
  46.  
  47.         UnlockImage(Image)
  48.         Pixmap= Null
  49.        
  50.        
  51.         Resized=ResizePixmap(Duplicate,Tx,Ty)
  52.        
  53.         Output=CreateImage(Tx,Ty)
  54.         Pixmap=LockImage(Output)
  55.        
  56.         For x = 0 To Tx-1
  57.                 For y = 0 To Ty-1
  58.                         c=ReadPixel(Resized, x, y)
  59.                         WritePixel(Pixmap, x,y,c)
  60.                 Next                   
  61.         Next
  62.  
  63.         UnlockImage(Output)
  64.        
  65.         Duplicate= Null
  66.         Pixmap= Null
  67.        
  68.         Return Output
  69. End Function


Comments :


skidracer(Posted 1+ years ago)

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



Kuron(Posted 1+ years ago)

 Thanks, Skid :c)


tonyg(Posted 1+ years ago)

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


bruZard(Posted 1+ years ago)

 @tonyg: of course...
Code: [Select]
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



tonyg(Posted 1+ years ago)

 Looks like that has the same problem BruZard.
Code: [Select]
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.


bruZard(Posted 1+ years ago)

 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...


tonyg(Posted 1+ years ago)

 Yep, sorry. It looks good. No idea what I was doing before. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal