15

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

sotangochips
  • 2,700
  • 6
  • 28
  • 38

5 Answers5

10

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

Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
  • Works really well for me. Beware, if you have Chrome translate the page, it jacks up the code, so copy the code while it's still in French and then translate if you want to read the explanation. Took me forever to figure out that's why the code was messed up. – Sam Soffes Oct 05 '13 at 21:03
  • but when it show us the graph and we adjust it for our result , how can we get those values on which we adjust the graph ? if we dont want to appear that curve next time , we just want to put only those values which we tested before – AHF May 12 '14 at 14:08
  • So are these cubic splines, or not? And if yes, which one? Natural cubic splines? – ArchLinuxTux Feb 08 '18 at 06:58
3

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.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 4
    Photoshop doesn't use a Catmull-Rom spline. Those splines are local: if you have points A-B-C-D-E-F-G, and you move point B around, the curve between E-F doesn't change. In Photoshop's curves, modifying any segment causes the whole curve to change. (Google says PS uses bicubic splines, but I havn't confirmed that.) – Glenn Maynard Apr 06 '11 at 02:15
  • I think Glenn is right and it's not a catmump-rom spline. Here's a link to some code that appears to implement the same: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=68577&lngWId=-1 – joshcartme Nov 10 '11 at 02:49
  • @Jack but this curve work as if we drop down it increase cyan and if we drag up it increase red , can we deal with both at the same time – AHF Apr 29 '14 at 15:38
2

Photoshop uses an interpolating cubic spline for the curve, as explored on a separate Math StackExchange thread.

Dan
  • 9,912
  • 18
  • 49
  • 70
2

Here's a link to a vbscript that appears to implement the cubic-spline curves photoshop uses.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=68577&lngWId=-1

joshcartme
  • 2,717
  • 1
  • 22
  • 34
0

I'm not familiar with C-R, but if it's like beziers, than the T parameter varies from 0 at one endpoint of a curve section to one at the other end point. It's how you "step along" the curve. So you can't just plug an x value you in. You can either sample a long at some arbitrary interval or you can use some sort of goal seeking algorithm to approach a given x value to whatever amount of precision you deem necessary.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • I'd say more like Spline than Bezier, as the line passes through the control points. – ysap Dec 05 '10 at 17:41
  • The actual curve can be the same in some cases, but you are right, the control points are different. If you want the curve stuck to the control points, use a spline. – comingstorm Dec 06 '10 at 18:20