1//
2//  PRCBriCon.m
3//  PRICE
4//
5//  Created by Riccardo Mottola on Thu Mar 3 2005.
6//  Copyright (c) 2005-2010 Carduus. 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// for values > 1
12// the minimum is from 126 to 127: 1.008
13// 125 to 127: 1.015
14// the maximum is from 1 to 127: 127
15
16#include <stdlib.h>
17
18#import "PRCBriCon.h"
19#import "PRBriCon.h"
20#import "MyDocument.h"
21
22#include <limits.h>
23
24#define HALF_CHAR (UCHAR_MAX >> 1)
25
26@implementation PRCBriCon
27
28- (id)init
29{
30    if ((self = [super init]))
31    {
32        filter = [[PRBriCon alloc] init];
33    }
34    return self;
35}
36
37- (IBAction)showFilter:(id)sender
38{
39    [super showFilter:sender];
40
41    if (!briconWindow)
42        [NSBundle loadNibNamed:@"BriCon" owner:self];
43    [briconWindow makeKeyAndOrderFront:nil];
44
45    [self parametersChanged:self];
46}
47
48
49- (NSArray *)encodeParameters
50{
51    NSArray  *parameters;
52    float    contrast;
53    int      level;
54
55    level = [conVal intValue];
56    contrast = 1 + (float)abs(level)/HALF_CHAR;
57    if (level < 0)
58        contrast = 1 / contrast;
59
60    /* encode parameters */
61    parameters = [NSArray arrayWithObjects:
62        [NSNumber numberWithInt:[briVal intValue]],
63        [NSNumber numberWithFloat:contrast],
64        nil];
65
66    return parameters;
67}
68
69- (void)closeFilterPanel
70{
71    [briconWindow performClose:nil];
72}
73
74- (IBAction)briconReset:(id)sender
75{
76    [briSlider setIntValue:0];
77    [briStep setIntValue:0];
78    [briVal setIntValue:0];
79    [conSlider setFloatValue:0.0];
80    [conStep setFloatValue:0.0];
81    [conVal setFloatValue:0.0];
82    [self parametersChanged:sender];
83}
84
85- (IBAction)changeBri:(id)sender
86{
87    if (sender == briVal)
88    {
89        int tempVal;
90
91        tempVal = [sender intValue];
92        if (tempVal > 255)
93        {
94            tempVal = 255;
95            [briVal setIntValue:tempVal];
96        } else if (tempVal < -255)
97        {
98            tempVal = -255;
99            [briVal setIntValue:tempVal];
100        }
101        [briSlider setIntValue:tempVal];
102        [briStep setIntValue:tempVal];
103    } else if (sender == briStep)
104    {
105        [briSlider setIntValue:[sender intValue]];
106        [briVal setIntValue:[sender intValue]];
107    } else if (sender == briSlider)
108    {
109        [briVal setIntValue:[sender intValue]];
110        [briStep setIntValue:[sender intValue]];
111    } else
112        NSLog(@"undexpected sender value in changeBri");
113    [self parametersChanged:sender];
114}
115
116- (IBAction)changeCon:(id)sender
117{
118    if (sender == conVal)
119    {
120        int tempVal;
121
122        tempVal = [sender intValue];
123        if (tempVal > 127)
124        {
125            tempVal = 127;
126            [briVal setIntValue:tempVal];
127        } else if (tempVal < -127)
128        {
129            tempVal = -127;
130            [briVal setIntValue:tempVal];
131        }
132        [conSlider setIntValue:tempVal];
133        [conStep setIntValue:tempVal];
134    } else if (sender == conStep)
135    {
136        [conSlider setIntValue:[sender intValue]];
137        [conVal setIntValue:[sender intValue]];
138    } else if (sender == conSlider)
139    {
140        [conVal setIntValue:[sender intValue]];
141        [conStep setIntValue:[sender intValue]];
142    } else
143        NSLog(@"undexpected sender value in changeCon");
144    [self parametersChanged:sender];
145}
146
147@end
148