1//
2//  PRCScale.m
3//  PRICE
4//
5//  Created by Riccardo Mottola on Wed Jan 19 2005.
6//  Copyright (c) 2005-2014 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#include <math.h>
12#import "PRCScale.h"
13#import "PRScale.h"
14#import "MyDocument.h"
15
16
17@implementation PRCScale
18
19- (id)init
20{
21    if ((self = [super init]))
22    {
23        filter = [[PRScale alloc] init];
24    }
25    return self;
26}
27
28- (IBAction)changePixelsX:(id)sender
29{
30  pixelsX = [pixelsXField intValue];
31  [percentXField setFloatValue:((float)pixelsX / originalWidth *100)];
32  if ([uniformToggle intValue])
33    {
34      pixelsY = (int)rint(pixelsX * ratio);
35      [pixelsYField setIntValue:pixelsY];
36      [percentYField setFloatValue:[percentXField floatValue]];
37    }
38  [self parametersChanged:self];
39}
40
41- (IBAction)changePixelsY:(id)sender
42{
43  pixelsY = [pixelsYField intValue];
44  [percentYField setFloatValue:((float)pixelsY / originalHeight *100)];
45  if ([uniformToggle intValue])
46    {
47      pixelsX = (int)rint(pixelsY / ratio);
48      [pixelsXField setIntValue:pixelsX];
49      [percentXField setFloatValue:[percentYField floatValue]];
50    }
51  [self parametersChanged:self];
52}
53
54- (IBAction)changePercentX:(id)sender
55{
56  float percentX;
57
58  percentX = [percentXField floatValue];
59  pixelsX = (int)rint(originalWidth * (percentX / 100));
60  [pixelsXField setIntValue:pixelsX];
61  if ([uniformToggle intValue])
62    {
63      pixelsY = (int)rint(pixelsX * ratio);
64      [pixelsYField setIntValue:pixelsY];
65      [percentYField setFloatValue:percentX];
66    }
67  [self parametersChanged:self];
68}
69
70- (IBAction)changePercentY:(id)sender
71{
72  float percentY;
73
74  percentY = [percentYField floatValue];
75  pixelsY = (int)rint(originalHeight * (percentY / 100));
76  [pixelsYField setIntValue:pixelsY];
77  if ([uniformToggle intValue])
78    {
79      pixelsX = (int)rint(pixelsY / ratio);
80      [pixelsXField setIntValue:pixelsX];
81      [percentXField setFloatValue:percentY];
82    }
83  [self parametersChanged:self];
84}
85
86- (IBAction)showFilter:(id)sender
87{
88    [super showFilter:sender];
89
90    if (!scaleWindow)
91        [NSBundle loadNibNamed:@"Scale" owner:self];
92    [scaleWindow makeKeyAndOrderFront:nil];
93    pixelsX = [[[[NSDocumentController sharedDocumentController] currentDocument] activeImage] width];
94    pixelsY = [[[[NSDocumentController sharedDocumentController] currentDocument] activeImage] height];
95    originalWidth = pixelsX;
96    originalHeight = pixelsY;
97    ratio = (float)pixelsY / (float)pixelsX;
98    [pixelsXField setIntValue:pixelsX];
99    [pixelsYField setIntValue:pixelsY];
100    [percentXField setFloatValue:100];
101    [percentYField setFloatValue:100];
102
103    [self parametersChanged:self];
104}
105
106- (NSArray *)encodeParameters
107{
108    int      method;
109    NSArray  *parameters;
110
111    switch ([[methodSelect selectedItem] tag])
112    {
113        case 0:
114            method = NEAREST_NEIGHBOUR;
115            break;
116        case 1:
117            method = BILINEAR;
118            break;
119        default:
120            method = NEAREST_NEIGHBOUR;
121            NSLog(@"Unexpected value for method in scale");
122    }
123
124
125
126    /* read pixels value again */
127    pixelsX = [pixelsXField intValue];
128    pixelsY = [pixelsYField intValue];
129
130    /* encode parameters */
131    parameters = [NSArray arrayWithObjects:
132        [NSNumber numberWithInt:pixelsX],
133        [NSNumber numberWithInt:pixelsY],
134        [NSNumber numberWithInt:method],
135        nil];
136
137    return parameters;
138}
139
140- (void)closeFilterPanel
141{
142    [scaleWindow performClose:nil];
143}
144
145@end
146