Fixes loss of alpha after adjusting brightness/contrast/hue/saturation for rasters

Also cleans and optimises code.