Desaturate a graphics surface
#1
This routine will apply a desaturation effect to a graphics surface.  You pass the handle
and a number from 0 to 1, where 0 does not desaturate at all, and 1 turns everything to greyscale.


Code: (Select All)
sub desaturate(d~&, rate)
' Desaturate image surface d~& at a rate of 0 (no desaturation) to 1 (full greyscale)

preserve1& = source
preserve2& = dest
_source d~&
_dest  d~&

for y = 0 to _width: for x = 0 to _height
      h& = point(x, y)
      r = _red(h&): g = _green(h&): b = _blue(h&)
      grey = int((r + g + b) * 0.333)

      r = r + ((grey - r) * rate)
      g = g + ((grey - g) * rate)
      b = b + ((grey - b) * rate)
      pset(x, y), _rgb(r, g, b)
next x: next y

_source preserve1&
_dest  preserve2&

end sub

Limitations:

- The desaturation is rather expensive, so you may have performance issues if you are doing this
many times per second.  The way I've used this in my own project is to desaturate a background
that isn't going to change, then store the desaturated version for use in redrawing the screen,
so the desaturation effect only needs to be applied once.

- This is currently using _rgb(), which means it's not accounting for alpha values.  I haven't
tested extensively how point() interacts with alpha, and I seem to remember having some issues
in the past, so I have no plans to update this to use _rgba() or _rgba32().  So if you use this
on a graphics surface, expect to lose any transparency.
Reply




Users browsing this thread: 1 Guest(s)