November 28, 2020, 02:31:58 AM

Author Topic: [bb] BlitzMax Desaturation fader by xlsior [ 1+ years ago ]  (Read 599 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : BlitzMax Desaturation fader
Author : xlsior
Posted : 1+ years ago

Description : Fade a screen from color to Black-and-white, and back again

Code :
Code: BlitzBasic
  1. ' BlitzMax Color-to-B&W Fade Routine
  2. ' Takes a full screen image, and smoothly desaturates it.
  3. '
  4. ' *Great* for using when pausing a game!
  5. '
  6. ' Freeware, (C) 2005 by xlsior/Marc van den Dikkenberg
  7. '
  8. ' - Press any key to start the fade effect.
  9. ' - Press Escape to exit.
  10.  
  11. Strict
  12.  
  13. Graphics 800,600,0
  14.  
  15. Local pixcolor:Long     ' Pixel colors as read from the pixmap
  16. Local pixred:Int
  17. Local pixgreen:Int
  18. Local pixblue:Int    
  19. Local pixavg:Int      ' Averaged RGB values
  20. Local orgformat:Int   ' Keep track of the original PixelMap format, for conversion
  21. Local monopix:Int     ' Colors in ARGB format
  22. Local workimg:timage
  23. Local workpix:tpixmap
  24. Local bwimg:timage
  25. Local sourceimg:timage
  26.  
  27. sourceimg:timage=LoadImage("d:/working/background3.jpg",DYNAMICIMAGE)
  28. workimg:timage=CreateImage (GraphicsWidth(),GraphicsHeight(),1,DYNAMICIMAGE)
  29.  
  30. DrawImage sourceimg,0,0
  31. '
  32. ' blah blah blah, run a program, draw things to the backbuffer, until
  33. ' you want to invoke the effect:
  34. '
  35.  
  36. GrabImage (workimg:timage,0,0)
  37. Flip
  38.  
  39. workpix:tpixmap=LockImage(workimg:timage)
  40.  
  41. orgformat=PixmapFormat(workpix)
  42. If orgformat<>PF_RGB888 Then
  43.         ' if the Pixmap in a different format than these routined expect, then convert:
  44.         workpix:tpixmap=ConvertPixmap:tpixmap(workpix,PF_RGB888)
  45. End If
  46.  
  47. For Local xx=0 To GraphicsWidth()-1
  48.         For Local yy=0 To GraphicsHeight()-1
  49.                 pixcolor=ReadPixel(workpix,xx,yy)
  50.                 pixred=(pixcolor & $00FF0000) Shr 16
  51.                 pixgreen=(pixcolor & $FF00) Shr 8
  52.                 pixblue=(pixcolor & $FF)
  53.                 pixavg=(pixred*0.299)+(pixgreen*0.587)+(pixblue*0.114)
  54.                 pixred=pixavg Shl 16
  55.                 pixgreen=pixavg Shl 8
  56.                 pixblue=pixavg
  57.                 monopix=pixred | pixgreen | pixblue
  58.                 WritePixel(workpix,xx,yy,monopix)
  59.         Next
  60. Next
  61.  
  62. If orgformat<>PF_RGB888 Then
  63.         ' The pixmap was converted to a different format originally.
  64.         ' Now convert it back to what it was.
  65.         workpix:tpixmap=ConvertPixmap:tpixmap(workpix,orgformat)
  66. End If
  67.  
  68. bwimg:timage=LoadImage(workpix:tpixmap)
  69. UnlockImage(workimg:timage)
  70.  
  71. ' At this point we have the following:
  72. ' workpix     - A pixmap containing a black-and-white version
  73. ' bwimg       - An image containing a black-and-white version
  74. ' workimg     - The screen grabbed full-color version
  75. ' sourceimg   - The original image loaded from disk
  76.  
  77. WaitKey()
  78.  
  79. For Local counter=0 To 100 Step 2
  80.         crossfade(workimg:timage,bwimg:timage,counter)
  81.         FlushMem
  82.         Flip
  83.         If KeyDown (key_Escape) Then Exit
  84. Next
  85.  
  86. WaitKey()
  87.  
  88. For Local counter=0 To 100 Step 2
  89.         crossfade(bwimg:timage,workimg:timage,counter)
  90.         FlushMem
  91.         Flip
  92.         If KeyDown (key_Escape) Then Exit
  93. Next
  94.  
  95. WaitKey()
  96.  
  97. Function CrossFade(pic1:timage,pic2:timage,perc:Float)
  98.         ' Generic Crossfade routine, to fade between the full color and
  99.         ' Black-and-white versions of the image.
  100.         ' Parameters: sourcepic, destination pic, percentage faded
  101.         If perc<1 Then
  102.                 perc=1
  103.         ElseIf perc>100 Then
  104.                 perc=100
  105.         End If
  106.         SetBlend (SolidBlend)
  107.         SetColor 255,255,255
  108.         DrawImage (pic1,0,0)
  109.         SetBlend (Alphablend)
  110.         SetAlpha (perc/100)
  111.         DrawImage (pic2,0,0)
  112. End Function


Comments :


Clyde(Posted 1+ years ago)

 Welldone again!btw - you might want to change this to a BMX sample and not a BB one


xlsior(Posted 1+ years ago)

 <div class="quote"> btw - you might want to change this to a BMX sample and not a BB one  </div>I tried, but it appears to be impossible to change between bb/bmx after submitting something... You can change everything else except that. Maybe a moderator can help out?** UPDATED CODE **  minor speed optimization.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal