December 03, 2020, 08:18:20 PM

Author Topic: [bb] ScaleImageFast by BlitzSupport [ 1+ years ago ]  (Read 374 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] ScaleImageFast by BlitzSupport [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : ScaleImageFast
Author : BlitzSupport
Posted : 1+ years ago

Description : This code (NOT written by me -- see source) is pretty fast, and very usable for realtime scaling depending on image size, what else is going on, etc.

Code :
Code: BlitzBasic
  1. ; These functions were NOT written by me! Someone posted them somewhere ages ago,
  2. ; but didn't put their name in the source. I have a vague feeling the guy's name
  3. ; might have been Daniel something, but I may just be hallucinating. If you wrote
  4. ; these, let me know!
  5.  
  6. ; I have just added two lines to each function to store and restore the current
  7. ; graphics buffer and made this crappy little demo -- use the mouse buttons
  8. ; to big up/smallify...
  9.  
  10. adder = 16
  11.  
  12. Graphics 640,480
  13. SetBuffer BackBuffer ()
  14.  
  15. image = LoadImage ("F:My DocumentsDevelopmentBlitz 2D Sourcesoing.bmp")
  16.  
  17. scalex# = ImageWidth (image)
  18. scaley# = ImageHeight (image)
  19.  
  20. While Not KeyHit(1)
  21.        
  22.     Cls
  23.  
  24.     If MouseDown(1)
  25.                 scalex = scalex + adder
  26.                 scaley = scaley + adder
  27.         Else
  28.             If MouseDown(2)
  29.                         scalex = scalex - adder: If scalex <= 2 Then scalex = 2
  30.                         scaley = scaley - adder: If scaley <= 2 Then scaley = 2
  31.                 EndIf
  32.         EndIf
  33.  
  34.     scaledimage = ScaleImageFast (image, scalex, scaley)
  35.  
  36.         MidHandle scaledimage
  37.     DrawImage scaledimage, MouseX (), MouseY ()
  38.     FreeImage scaledimage
  39.  
  40.     Flip
  41.  
  42. Wend
  43.  
  44. ;___________________________________________________________________
  45.  
  46. Function ScaleImageFast(image,newwidth,newheight,frame=0)
  47.  
  48.         tbuffer = GraphicsBuffer ()
  49.        
  50.     oldwidth = ImageWidth(image)
  51.     oldheight = ImageHeight(image)
  52.  
  53.     newwidth=newwidth-1
  54.     newheight=newheight-1
  55.     ni=CreateImage(newwidth+1,oldheight)
  56.     dest = CreateImage(newwidth+1,newheight+1)
  57.     SetBuffer ImageBuffer(ni)
  58.     For x = 0 To newwidth
  59.         LineRef = Floor((oldwidth*x)/newwidth)
  60.         DrawBlockRect image,x,0,LineRef,0,1,oldheight,frame
  61.     Next
  62.     SetBuffer ImageBuffer(dest)
  63.     For y = 0 To newheight
  64.         LineRef = Floor((oldheight*y)/newheight)
  65.         DrawBlockRect ni,0,y,0,LineRef,newwidth,1
  66.     Next
  67.     FreeImage ni
  68.  
  69.         SetBuffer tbuffer
  70.        
  71.     Return dest
  72. End Function
  73.  
  74. Function ScaleImageFast1(image,newwidth#,newheight#,frame=0)
  75.  
  76.         tbuffer = GraphicsBuffer ()
  77.        
  78.     oldwidth# = ImageWidth(image)
  79.     oldheight# = ImageHeight(image)
  80.  
  81.     newwidth=newwidth-1
  82.     newheight=newheight-1
  83.     ni=CreateImage(newwidth+1,oldheight)
  84.     dest = CreateImage(newwidth+1,newheight+1)
  85.     SetBuffer ImageBuffer(ni)
  86.     For x# = 0 To newwidth
  87.         LineRef# = Floor((oldwidth*x)/newwidth)
  88.         DrawBlockRect image,x,0,LineRef,0,1,oldheight,frame
  89.     Next
  90.     SetBuffer ImageBuffer(dest)
  91.     For y# = 0 To newheight
  92.         LineRef = Floor((oldheight*y)/newheight)
  93.         DrawBlockRect ni,0,y,0,LineRef,newwidth,1
  94.     Next
  95.     FreeImage ni
  96.         SetBuffer tbuffer
  97.        
  98.     Return dest
  99. End Function


Comments :


Megalomanic(Posted 1+ years ago)

 On non-square pictures this code didn't calculate the right aspect ratio.To fix this, replace the adder variable with precalculated aspect ratio.


Megalomanic(Posted 1+ years ago)

 SOLUTION:; These functions were NOT written by me! Someone posted them somewhere ages ago,; but didn't put their name in the source. I have a vague feeling the guy's name; might have been Daniel something, but I may just be hallucinating. If you wrote; these, let me know!; I have just added two lines to each function to store and restore the current; graphics buffer and made this crappy little demo -- use the mouse buttons; to big up/smallify...Graphics 1920,1200,32,1SetBuffer BackBuffer ()image = LoadImage ("Test.png")scalex# = ImageWidth (image)scaley# = ImageHeight (image)If scalex#>scaley#   aspectratio#=scalex#/scaley#   mode=1ElseIf scaley#>scalex#   aspectratio#=scaley#/scalex#   mode=2ElseIf scalex#=scaley#   aspectratio#=1.0   mode=3EndIfmagnitute=100While Not KeyHit(1)       Cls    If MouseDown(1)      magnitute=magnitute-5      If magnitute<5 Then magnitute=5   EndIf    If MouseDown(2)      magnitute=magnitute+5      If magnitute>3000 Then magnitute=3000   EndIf   If mode=1      scalex#=aspectratio#*magnitute      scaley#=magnitute   ElseIf mode=2      scaley#=aspectratio#*magnitute      scalex#=magnitute   ElseIf mode=3      scalex#=magnitute      scaley#=magnitute   EndIf    scaledimage = ScaleImageFast (image, scalex#, scaley#)   MidHandle scaledimage    DrawImage scaledimage, MouseX (), MouseY ()    FreeImage scaledimage    FlipWend;___________________________________________________________________Function ScaleImageFast(image,newwidth,newheight,frame=0)   tbuffer = GraphicsBuffer ()       oldwidth = ImageWidth(image)    oldheight = ImageHeight(image)    newwidth=newwidth-1    newheight=newheight-1    ni=CreateImage(newwidth+1,oldheight)    dest = CreateImage(newwidth+1,newheight+1)    SetBuffer ImageBuffer(ni)    For x = 0 To newwidth        LineRef = Floor((oldwidth*x)/newwidth)        DrawBlockRect image,x,0,LineRef,0,1,oldheight,frame    Next    SetBuffer ImageBuffer(dest)    For y = 0 To newheight        LineRef = Floor((oldheight*y)/newheight)        DrawBlockRect ni,0,y,0,LineRef,newwidth,1    Next     FreeImage ni   SetBuffer tbuffer       Return destEnd FunctionFunction ScaleImageFast1(image,newwidth#,newheight#,frame=0)   tbuffer = GraphicsBuffer ()       oldwidth# = ImageWidth(image)    oldheight# = ImageHeight(image)    newwidth=newwidth-1    newheight=newheight-1    ni=CreateImage(newwidth+1,oldheight)    dest = CreateImage(newwidth+1,newheight+1)    SetBuffer ImageBuffer(ni)    For x# = 0 To newwidth        LineRef# = Floor((oldwidth*x)/newwidth)        DrawBlockRect image,x,0,LineRef,0,1,oldheight,frame    Next    SetBuffer ImageBuffer(dest)    For y# = 0 To newheight        LineRef = Floor((oldheight*y)/newheight)        DrawBlockRect ni,0,y,0,LineRef,newwidth,1    Next     FreeImage ni   SetBuffer tbuffer       Return destEnd Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal