evanw / glfx.js

An image effects library for JavaScript using WebGL
https://evanw.github.io/glfx.js/
MIT License
3.26k stars 402 forks source link

Levels filter #35

Open srdjanarsic opened 8 years ago

srdjanarsic commented 8 years ago

I am impressed with this library. What it's missing for me, is a levels filter https://docs.gimp.org/en/gimp-tool-levels.html Do you plan to add this kind of filter?

srdjanarsic commented 8 years ago

I found exact code in this class. https://github.com/talentedmrjones/Javascript-Canvas-Tools/blob/master/canvasTools.js

So the parameters for this filter are: gama(0-1), input(0-255) and output(0-255). This is the exact photoshop like levels adjustment, so it will be very useful.

This peace of code do a magic:

`

,Adjustments:{
    levels:{
        defaults:{
            gamma:1
            ,input:{
                min:0
                ,max:255
            },
            output:{
                min:0
                ,max:255
            }
        }
        ,method:function(o,r,g,b,a){
            var minInput = o.input.min/255
            ,maxInput = o.input.max/255
            ,minOutput = o.output.min/255
            ,maxOutput = o.output.max/255
            p=[]
            ;
            // in these cases, DRY code adversely affected performance by adding yet another function call.
            // profiling shows that calling the calculation directly is significantly faster
            p[0]=(minOutput+(maxOutput-minOutput)*Pow(Min(Max((r/255)-minInput, 0.0) / (maxInput-minInput), 1.0),(1/o.gamma)))*255;
            p[1]=(minOutput+(maxOutput-minOutput)*Pow(Min(Max((g/255)-minInput, 0.0) / (maxInput-minInput), 1.0),(1/o.gamma)))*255;
            p[2]=(minOutput+(maxOutput-minOutput)*Pow(Min(Max((b/255)-minInput, 0.0) / (maxInput-minInput), 1.0),(1/o.gamma)))*255;
            p[3]=a;

            return p;
        }
    }// levels  
} // Adjustments

`

the-didi commented 9 months ago

https://github.com/evanw/glfx.js/pull/62