开发者

How to recreate the math behind photoshop curves

开发者 https://www.devze.com 2023-01-28 17:56 出处:网络
Basically, what I want to do is understand how to calculate the values along a \'curve\' as represented here in the photoshop curves box:

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

How to recreate the math behind 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 t开发者_运维知识库his in C if possible


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


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号