Ooops
February 27, 2021, 04:18:29 AM

Author Topic: [bb] Contrast Adjuster by Pete Rigz [ 1+ years ago ]  (Read 413 times)

Offline BlitzBot

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

Description : This simple bit of code will take an image and adjust its contrast based on the contrast value and threshold u pass it.

I made it to make my procedural colour maps abit more vibrant.

You'll need your own graphic for the example to work.


Code :
Code: BlitzBasic
  1. ;----------Quick test of adjust contrast function
  2. Graphics3D 800,600,0,2
  3.  
  4. SetBuffer FrontBuffer()
  5.  
  6. test=LoadImage("grass.png")
  7. test2=LoadImage("grass.png")
  8.  
  9. DrawImage test,5,5
  10.  
  11. DrawImage adjustcontrast(test,50,32),275,5
  12. DrawImage adjustcontrast(test2,-50,64),5,275
  13.  
  14. WaitKey()
  15.  
  16. ;-----------
  17.  
  18. Function AdjustContrast(image,level#,threshold#=64)
  19.  
  20.         ;Contrast adjuster by Peter Rigby
  21.  
  22.         ;set the level to a value between -100 to 100
  23.  
  24.         ;threshold is a colour value between 0-255. Contrast works by exaggerating
  25.         ;the colour depending on which side of the threshold it is.
  26.  
  27.         Local pixel#
  28.         Local red#,green#,blue#
  29.         Local r,g,b
  30.         Local pix
  31.  
  32.         LockBuffer ImageBuffer(image)
  33.  
  34.         Local contrast#=(100+level)/100 ;set up the contrast to a value between 0-4
  35.         contrast=contrast*contrast
  36.        
  37.         threshold=threshold/255.0               ;normalise the threshold
  38.  
  39.  
  40.        
  41.         For x = 0 To ImageWidth(image)-1
  42.                 For y = 0 To ImageHeight(image)-1
  43.                
  44.                         pix = ReadPixelFast(X,Y,ImageBuffer(image))    
  45.                         red#=(pix Shr 16) And $ff                       ;extract the colour values
  46.                         green#=(pix Shr 8) And $ff
  47.                         blue#=pix And $ff
  48.                        
  49.                         pixel = red/255.0                                       ;for each colour, set to a value between 0-1
  50.                         pixel=pixel-threshold                           ;take away the threshold
  51.                         pixel=pixel*contrast                            ;if the pixel colour becomes negative then the colour will become darker, otherwise it'll lighten
  52.                         pixel=pixel+threshold                           ;add the threshold back again
  53.                         pixel=pixel*255                                         ;convert back to noram colour value
  54.                         If pixel < 0 Then pixel = 0             ;deal with abnormal colour values
  55.                         If pixel > 255 Then pixel = 255
  56.                         r=pixel
  57.                                                                                                 ;repeat for the other 2 colours
  58.                         pixel = green/255.0
  59.                         pixel=pixel-threshold
  60.                         pixel=pixel*contrast
  61.                         pixel=pixel+threshold
  62.                         pixel=pixel*255
  63.                         If pixel < 0 Then pixel = 0
  64.                         If pixel > 255 Then pixel = 255
  65.                         g=pixel
  66.                        
  67.                         pixel = blue/255.0
  68.                         pixel=pixel-threshold
  69.                         pixel=pixel*contrast
  70.                         pixel=pixel+threshold
  71.                         pixel=pixel*255
  72.                         If pixel < 0 Then pixel = 0
  73.                         If pixel > 255 Then pixel = 255
  74.                         b=pixel
  75.                        
  76.                         pix=(b Or (g Shl 8) Or (r Shl 16) Or ($ff000000))
  77.                         WritePixelFast x,y,pix,ImageBuffer(image)       ;write pixel to image
  78.            Next
  79.         Next
  80.         UnlockBuffer ImageBuffer(image)
  81.         Return image
  82. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal