Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recreate the math behind photoshop curves

Tags:

math

graphics

Basically, what I want to do is understand how to calculate the values along a 'curve' as represented here in the photoshop curves box:

Photoshop Curves

So, given N points with x between 0 and 1 and y between 0 and 1 - we'll create a curve that passes through all these points. Given this curve, I'd like to be able to calculate all values of the curve for any given X.

In other words, I'd like to modify values of color just like the curves box does, but programmatically.

I've read that these are "catmull-rom splines" -- but all I see is a function that relies upon a parametric T -- I want to be able to look up for values of x. I'd like to do this in C if possible

like image 746
sotangochips Avatar asked Dec 05 '10 01:12

sotangochips


People also ask

How do you make curves in Photoshop?

To access this tool, select Enhance > Adjust Color > Adjust Color Curves. You can then click and drag the sliders to adjust the curve.

What's the difference between levels and curves in Photoshop?

Levels provides enhancement of contrast only by setting highlight and shadow points. Curves allows you to adjust contrast by setting and positioning control points along the entire tonal range.


2 Answers

This code appears to match Photoshop's curves exactly (not my code): http://www.developpez.net/forums/d331608-3/autres-langages/algorithmes/contribuez/image-interpolation-spline-cubique/#post3513925

like image 110
Glenn Maynard Avatar answered Sep 17 '22 22:09

Glenn Maynard


A Catmull-Rom Spline is used because it's a kind of spline that represents a curve in which you can add control points and refine the curve itself (that is what you do on Photoshop when you click to add a new point), with the particularity to have the curve pass by every control point you specify.

In any case you just need a function that taken a value (float in 0..1 or int in 0..255 or whatever color space you have) will produce another one.

float fun(float x) {
  y = /* something */
  return y;
}

This can be done with whatever kind of function of course. The most basic one is the default one that is an identity function

float fun(float x) {
  y = x;
  return y;
}

Any other function can be calculated with curves and it will be ok but more complex to develop, I'd suggest you to start from simple examples like a Bezier curve. In any case the t parameter is used because these are parametric curves, you need to understand some of the mathematical background of curves before digging into development, take a look here.

like image 43
Jack Avatar answered Sep 18 '22 22:09

Jack