1//
2//  PRCCurves.m
3//  PRICE
4//
5//  Created by Riccardo Mottola on 7 August 2011.
6//  Copyright 2011-2014 Riccardo Mottola. All rights reserved.
7//
8// This application is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10
11#import "MyDocument.h"
12#import "PRCCurves.h"
13#import "PRCurves.h"
14
15
16@implementation PRCCurves
17
18- (id)init
19{
20  if ((self = [super init]))
21    {
22      filter = [[PRCurves alloc] init];
23
24    }
25  return self;
26}
27
28
29- (IBAction)showFilter:(id)sender
30{
31  [super showFilter:sender];
32
33  if (!curvesWindow)
34    [NSBundle loadNibNamed:@"Curves" owner:self];
35  [curvesView setFilterController: self];
36
37
38  currPath = [curvesView luminancePath];
39//  [self curvesReset: self];
40  PRImage *image = [[[NSDocumentController sharedDocumentController] currentDocument] activeImage];
41  [curvesView calculateHistogram: image];
42
43  [curvesWindow makeKeyAndOrderFront:nil];
44  [self parametersChanged:self];
45}
46
47- (NSArray *)encodeParameters
48{
49  NSArray           *parameters;
50  NSMutableArray    *arrayL;
51  NSMutableArray    *arrayR;
52  NSMutableArray    *arrayG;
53  NSMutableArray    *arrayB;
54  unsigned i;
55
56  if ([curvesView hasColor])
57    {
58      arrayR = [[NSMutableArray alloc] initWithCapacity: UCHAR_MAX];
59      arrayG = [[NSMutableArray alloc] initWithCapacity: UCHAR_MAX];
60      arrayB = [[NSMutableArray alloc] initWithCapacity: UCHAR_MAX];
61
62      for (i = 0; i <= UCHAR_MAX; i++)
63	{
64	  [arrayR addObject: [NSNumber numberWithUnsignedInt: curvesView->funL[i]]];
65	  [arrayG addObject: [NSNumber numberWithUnsignedInt: curvesView->funL[i]]];
66	  [arrayB addObject: [NSNumber numberWithUnsignedInt: curvesView->funL[i]]];
67	}
68      /* encode parameters */
69      parameters = [NSArray arrayWithObjects:
70			      arrayR,
71			    arrayG,
72			    arrayB,
73			    nil];
74    }
75  else
76    {
77      arrayL = [[NSMutableArray alloc] initWithCapacity: UCHAR_MAX];
78
79      for (i = 0; i <= UCHAR_MAX; i++)
80	[arrayL addObject: [NSNumber numberWithUnsignedInt: curvesView->funL[i]]];
81
82      /* encode parameters */
83      parameters = [NSArray arrayWithObjects:
84			      arrayL,
85			    nil];
86    }
87
88  return parameters;
89}
90
91- (void)closeFilterPanel
92{
93  [curvesWindow performClose:nil];
94}
95
96- (IBAction)recalculateCurves:(id)sender
97{
98  [curvesView calculateTransformedHistograms];
99}
100
101- (IBAction)parametersChanged:(id)sender
102{
103  [self recalculateCurves:sender];
104
105  [super parametersChanged: sender];
106  [curvesView setNeedsDisplay: YES];
107}
108
109- (IBAction)curvesReset:(id)sender
110{
111  /* first reconstruct the curves from scratch */
112  [curvesView initPathLumi];
113  [curvesView initPathR];
114  [curvesView initPathG];
115  [curvesView initPathB];
116
117  /* make white and black points coherent */
118  [self setWhitePointValue: 255];
119  [curvesView setWhitePoint: 255];
120
121  [self setBlackPointValue: 0];
122  [curvesView setBlackPoint: 0];
123
124  [self parametersChanged:sender];
125}
126
127- (void)setBlackPointValue:(int)val
128{
129  [blackPointField setIntValue: val];
130  [blackPointStepper setIntValue: val];
131  NSLog(@"black point: %d", val);
132}
133
134- (void)setWhitePointValue:(int)val
135{
136  [whitePointField setIntValue: val];
137  [whitePointStepper setIntValue: val];
138  NSLog(@"white point: %d", val);
139}
140
141- (IBAction)setBlackPoint:(id)sender
142{
143  int point;
144
145  point = [sender intValue];
146  if (point > 254)
147    point = 254;
148  else if (point < 0)
149    point = 0;
150  else if (point >= [whitePointField intValue])
151    point = [whitePointField intValue];
152  [self setBlackPointValue:point];
153
154  [curvesView setBlackPoint: point];
155  [self parametersChanged:sender];
156}
157
158- (IBAction)setWhitePoint:(id)sender
159{
160  int point;
161
162  point = [sender intValue];
163  if (point > 255)
164    point = 255;
165  else if (point < 1)
166    point = 1;
167  else if (point <= [blackPointField intValue])
168    point = [blackPointField intValue];
169  [self setWhitePointValue: point];
170
171  [curvesView setWhitePoint: point];
172  [self parametersChanged:sender];
173}
174
175
176@end
177