How can I map Photoshop's level adjustment to a Core Image filter?
Asked Answered
P

1

7

I'm mapping several photoshop elements to CIFilter, the only one I'm having trouble with is this Levels Adjustment:

screen shot from current version of photoshop

Which CI Filter (or combination of filters) would let me utilize the 16, 1.73, 239 & 39/245 above in the first example or the 31, 1.25, 255 30/255 in the second example. I believe this is a kind of shadow/black and white level adjustment.

Any help appreciated.

Pretended answered 7/12, 2012 at 14:46 Comment(0)
E
10

By adapting the formula from this page: http://http.developer.nvidia.com/GPUGems/gpugems_ch22.html, I believe you can do this using a combination of CIColorMatrix, CIGammaAdjust and another CIColorMatrix.

Let's call the input levels inBlack, inGamma and inWhite respectively, and the output levels outBlack and outWhite. Note that Photoshop color are between 0 and 255 while CI colors are between 0 and 1 so you need to divide the Photoshop values (except inGamma!) by 255 before putting them into the following formulas.

The input mapping is pixel = (inPixel-inBlack)/(inWhite-inBlack), which means your first matrix will be

red = [1/(inWhite-inBlack) 0 0 0]
green = [0 1/(inWhite-inBlack) 0 0]
blue = [0 0 1/(inWhite-inBlack) 0]
alpha = [0 0 0 1]
bias = [-inBlack/(inWhite-inBlack), -inBlack/(inWhite-inBlack),-inBlack/(inWhite-inBlack), 0]

Then you apply gamma correction using CIGammaAdjust and the inGamma number (I had to use the inverse 1/inGamma when doing my calculations, try that too!).

Finally the output mapping is pixel = gammaCorrectedPixel * (outWhite - outBlack) + outBlack, giving you the final matrix

red = [(outWhite - outBlack) 0 0 0]
green = [0 (outWhite - outBlack) 0 0]
blue = [0 0 (outWhite - outBlack) 0]
alpha = [0 0 0 1]
bias = [outBlack outBlack outBlack 0]

I haven't actually tried this using CoreImage, but the calculations work out nicely!

Ectoenzyme answered 14/12, 2012 at 3:38 Comment(3)
can you explain this part? pixel = gammaCorrectedPixel * (outWhite - outBlack) + outBlackPretended
The formula from the link was outPixel = (pow(((inPixel * 255.0) - inBlack) / (inWhite - inBlack), inGamma) * (outWhite - outBlack) + outBlack) / 255.0 so that is the part outside of the pow (the formula is deconstructed into one matrix multiplication applied before the pow, the pow itself applied as gamma correction and then a matrix for everything happening after the pow).Ectoenzyme
so, inBlack = 31, inGamma = 1.25 and inWhite = 255? Then, we have to divide these values by 255 ?Doctrine

© 2022 - 2024 — McMap. All rights reserved.