I'm drawing several grayscale images in iOS. The values of grayscale image have minimum and maximum i.e. for 8 bit values in range [41,244].
I want to change minimum value or maximum value. I want to know how to set a filter in this list to change minimum value and how to set a filter in this list to change maximum value?
Maybe the follow of drawing will be good to see :
[1. Read raw grayscale data] -> [2. Create CGImageRef] -> [3. Load ref in GPU]
and by now, in iOS 6 can apply filter in GPU in real-time. Manipulate on 1 or 2 is very slow for me because in one second I should draw 100 images. So I need to use CIFilter in GPU. I know how to filter image. But what filter can do my job?
Some sample code is good for me.
CIToneCurve should be good for this kind of thing.
self.context
can be a CPU or GPU (EAGL) context. The points describe a tone curve. X is input value, Y is output value. In this example we are reducing the slope, so reducing contrast. You could just as well keep the slope the same, but cut off the extremes so reducing your maxima and minima.Here are some measurements processing 100 images on an iPad mini (average of four readings per setting). You will see that the GPU is no magic bullet, and around 60-65% of the time is absorbed in just moving image data around. These examples start and end with UIImage. I get very similar results with CGImage.
update
For Window and Level adjustments with zero values for inputs outside of the window range, you want to achieve something like this:
In practice this will not work. The points describe a spline curve, and it doesn't behave well with sharp changes of direction such as b-c-d. (Also points c and d (2 and 3) cannot share the same x value, so in any case you would have to increment slightly so that d.x = c.x*1.01)
You can get your result if you use a multi-step filter. The first filter uses
CIToneCurve
apprpriately (this is the correct window/level algorithm without attempting to knock your max levels down to zero).We make a copy of the filter as we'll need it again in the last step:
Apply the CIColorControls filter to maximise contrast and brightness on the result
Now posterize to a 1-bit palette
Invert the result
Now we use this result as a mask with the window/levelled image so that all max-white levels get reduced to black.
original
filter1
CIToneCurve
filter2
CIColorControls
filter3
CIColorPosterize
filter4
CIColorInvert
filter5
CIDarkenBlendMode
If you take a look at Brad Larson's
GPUImage
you should find thatGPUImageLuminanceThresholdFilter
will better replace filters 2->5.For every effect in CIFilter we have minimum and maximum values . Either we can fix that value in code else if you want to fix ater drawing we done it by using
UISlider
for slider given minimum and maximum values as range of that filter then we can give action to that slider so when user moves effect also changes automatically.