Adjustable threshold (Hydra filter)
From Adobe Labs
This AIF filter calculates the threshold of an image, separating pixel values between dark and bright colors. Unlike a straight threshold filter, you can set the black and white threshold, and everything in-between those two values is featured as a grayscale point - that ends up allowing for a kind of anti-aliased threshold, or a very strong levels adjustement. Here's a sample (before and after):
The black threshold must always be lower than the white threshold. More information in this post.
Flash-compatible code:
kernel AdjustableThreshold {
parameter float blackThreshold <
minValue:float(0);
maxValue:float(1);
defaultValue:float(0.4);
>;
parameter float whiteThreshold <
minValue:float(0);
maxValue:float(1);
defaultValue:float(0.5);
>;
void evaluatePixel(in image4 src, out pixel4 dst) {
float4 inputColor = sampleNearest(src, outCoord());
float brightness = (inputColor.r + inputColor.g + inputColor.b) / 3.0;
dst = inputColor;
float grayscale = (brightness - blackThreshold) / (whiteThreshold - blackThreshold);
float ifAboveBlack = brightness > whiteThreshold ? 1.0 : grayscale;
dst.r = dst.g = dst.b = brightness < blackThreshold ? 0.0 : ifAboveBlack;
}
}
More generic code (same result):
kernel AdjustableThreshold {
parameter float blackThreshold <
minValue:float(0);
maxValue:float(1);
defaultValue:float(0.4);
>;
parameter float whiteThreshold <
minValue:float(0);
maxValue:float(1);
defaultValue:float(0.5);
>;
void evaluatePixel(in image4 src, out pixel4 dst) {
float4 inputColor = sampleNearest(src, outCoord());
float brightness = (inputColor.r + inputColor.g + inputColor.b) / 3.0;
dst = inputColor;
if (brightness < blackThreshold) {
// Below threshold
dst.r = dst.g = dst.b = 0.0;
} else if (brightness > whiteThreshold) {
// Above threshold
dst.r = dst.g = dst.b = 1.0;
} else {
// Between the threshold
dst.r = dst.g = dst.b = (brightness - blackThreshold) / (whiteThreshold - blackThreshold);
}
}
}

