Skip to content

Commit

Permalink
Merge pull request BradLarson#1071 from karlvr/tonecurvefixes
Browse files Browse the repository at this point in the history
Tone curve filter: fixes missing last point and incorrect application of RGB curve in conduction with individual channels
  • Loading branch information
BradLarson committed Jul 29, 2013
2 parents 5a1ea83 + 07ba35e commit 33305c3
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions framework/Source/GPUImageToneCurveFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ - (NSArray *)getPreparedSplineCurve:(NSArray *)points

// Insert points similarly at the end, if necessary.
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
CGPoint lastSplinePoint = [[splinePoints objectAtIndex:([splinePoints count] - 1)] CGPointValue];
CGPoint lastSplinePoint = [[splinePoints lastObject] CGPointValue];

if (lastSplinePoint.x < 255) {
for (int i = lastSplinePoint.x + 1; i <= 255; i++) {
Expand All @@ -312,7 +312,7 @@ - (NSArray *)getPreparedSplineCurve:(NSArray *)points
}
}
#else
NSPoint lastSplinePoint = [[splinePoints objectAtIndex:([splinePoints count] - 1)] pointValue];
NSPoint lastSplinePoint = [[splinePoints lastObject] pointValue];

if (lastSplinePoint.x < 255) {
for (int i = lastSplinePoint.x + 1; i <= 255; i++) {
Expand Down Expand Up @@ -354,8 +354,7 @@ - (NSMutableArray *)splineCurve:(NSArray *)points
{
NSMutableArray *sdA = [self secondDerivative:points];

// Is [points count] equal to [sdA count]?
// int n = [points count];
// [points count] is equal to [sdA count]
NSInteger n = [sdA count];
if (n < 1)
{
Expand Down Expand Up @@ -408,16 +407,14 @@ - (NSMutableArray *)splineCurve:(NSArray *)points
}
}

// If the last point is (255, 255) it doesn't get added.
if ([output count] == 255) {
[output addObject:[points lastObject]];
}
// The above always misses the last point because the last point is the last next, so we approach but don't equal it.
[output addObject:[points lastObject]];
return output;
}

- (NSMutableArray *)secondDerivative:(NSArray *)points
{
NSInteger n = [points count];
const NSInteger n = [points count];
if ((n <= 0) || (n == 1))
{
return nil;
Expand Down Expand Up @@ -513,9 +510,12 @@ - (void)updateToneCurveTexture;
for (unsigned int currentCurveIndex = 0; currentCurveIndex < 256; currentCurveIndex++)
{
// BGRA for upload to texture
toneCurveByteArray[currentCurveIndex * 4] = fmin(fmax(currentCurveIndex + [[_blueCurve objectAtIndex:currentCurveIndex] floatValue] + [[_rgbCompositeCurve objectAtIndex:currentCurveIndex] floatValue], 0), 255);
toneCurveByteArray[currentCurveIndex * 4 + 1] = fmin(fmax(currentCurveIndex + [[_greenCurve objectAtIndex:currentCurveIndex] floatValue] + [[_rgbCompositeCurve objectAtIndex:currentCurveIndex] floatValue], 0), 255);
toneCurveByteArray[currentCurveIndex * 4 + 2] = fmin(fmax(currentCurveIndex + [[_redCurve objectAtIndex:currentCurveIndex] floatValue] + [[_rgbCompositeCurve objectAtIndex:currentCurveIndex] floatValue], 0), 255);
GLubyte b = fmin(fmax(currentCurveIndex + [[_blueCurve objectAtIndex:currentCurveIndex] floatValue], 0), 255);
toneCurveByteArray[currentCurveIndex * 4] = fmin(fmax(b + [[_rgbCompositeCurve objectAtIndex:b] floatValue], 0), 255);
GLubyte g = fmin(fmax(currentCurveIndex + [[_greenCurve objectAtIndex:currentCurveIndex] floatValue], 0), 255);
toneCurveByteArray[currentCurveIndex * 4 + 1] = fmin(fmax(g + [[_rgbCompositeCurve objectAtIndex:g] floatValue], 0), 255);
GLubyte r = fmin(fmax(currentCurveIndex + [[_redCurve objectAtIndex:currentCurveIndex] floatValue], 0), 255);
toneCurveByteArray[currentCurveIndex * 4 + 2] = fmin(fmax(r + [[_rgbCompositeCurve objectAtIndex:r] floatValue], 0), 255);
toneCurveByteArray[currentCurveIndex * 4 + 3] = 255;
}

Expand Down

0 comments on commit 33305c3

Please sign in to comment.