GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Photoshop-like Levels

Open iamcam opened this issue 14 years ago • 5 comments

I didn't see any filters that mimic the Photoshop Levels function. Is this something that can/should be achieved with the ToneCurve, or could we get better results using a separate filter?

I found some OGSL shaders, including the levels shader, at http://001.vade.info/?page_id=20, but some of the licenses will have to be sorted out before we could use them. Here's the shader code, for educational purposes.

// define our rectangular texture samplers 
uniform sampler2DRect tex0;
uniform sampler1D tex1;

// define our varying texture coordinates 
varying vec2 texcoord0;
varying vec2 texcoord1;

void main (void) 
{ 
    vec4 inputColor = texture2DRect(tex0, texcoord0);

    vec4 colorout;

    colorout.r = texture1D(tex1, inputColor.r).r;
    colorout.g = texture1D(tex1, inputColor.g).g;
    colorout.b = texture1D(tex1, inputColor.b).b;
    colorout.a = inputColor.a;

    gl_FragColor = colorout;
}

I tried porting it to GPUImage as a custom filter, but I wasn't sure what to do with sampler1D text1. I've ported some of the other filters with success, but those pretty much only relied on the sampler2D(Rect) and vec2 texture coordinate.

There are a few different authors and licenses that come with the above filters, but I think any unmarked filter is assumed as

 * Andrew Benson - [email protected]
 * Copyright 2005 - Cycling '74
    Original Code : Video Mixers and Effects 

    Creative Commons Attribution-NonCommercial-ShareAlike 3.0
    see : http://creativecommons.org/licenses/by-nc-sa/3.0/

    Most 3D Shaders are ports, modifications and optimizations of existing code, examples
    and tutorials found on the web. Please see the source code for individual shaders
    for licenses. License texts can be found in the Licenses folder.

    For included assets (media, 3D models and textures), see the included readme file 
    for each asset in its respective folder.

iamcam avatar May 31 '12 17:05 iamcam

thank you ,the shader code is very helpful !

fangyuxi avatar Jun 01 '12 10:06 fangyuxi

A separate filter to do this is needed. Please don't copy any code from those shaders that have the NonCommercial Creative Commons licensing, since that is not compatible with the license of this framework (which is free to use for any applications, not just non-commercial ones). Instead, do a cleanroom reimplementation based on their algorithms if you wish to use a process they've performed.

I imagine a levels filter will be similar in structure to what's done for the tone curve adjustment. Instead of populating the lookup texture with values calculated from a spline curve, you just need to figure out what those color channel intensity mappings are for adjusted levels in each channel. Use the tone curve filter as a basis and replace the curve calculations with what you need for this.

BradLarson avatar Jun 01 '12 15:06 BradLarson

I was playing around with PS and you can get part of the Levels using curves by setting the inputs at {inputA,0} and {inputB,255}, but the midrange is a little bit tricky. It appears to be some kind of exponential curve, ranging from 0.01 to 9.99, where 1.0 is the middle of the range. From what I've read, it adjusts Gamma.

I think this might be a good starting point http://mouaif.wordpress.com/2009/01/28/levels-control-shader/ , with links to http://mouaif.wordpress.com/2009/01/05/photoshop-math-with-glsl-shaders/

iamcam avatar Jun 01 '12 16:06 iamcam

how to change levels to curves?

vinxu avatar Sep 21 '12 08:09 vinxu

@vinux you can probably piece it together from the two links I provided (mouaif.wordpress.com). i haven't had a chance to come back to this (and probably won't for some time), so do let us know if you come up with something.

iamcam avatar Sep 21 '12 16:09 iamcam