forked from CocoaBob/xee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXeeControllerImageActions.m
356 lines (280 loc) · 9.08 KB
/
XeeControllerImageActions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#import "XeeControllerImageActions.h"
#import "XeeSavePanel.h"
#import "XeeImageSaver.h"
#import "XeeView.h"
#import "XeeMoveTool.h"
#import "XeeCropTool.h"
#import "XeeImageSource.h"
#import <QuickTime/ImageCompression.h>
#import <QuickTime/QuickTimeComponents.h>
float XeeZoomLevels[]={0.03125,0.044,0.0625,0.09,0.125,0.18,0.25,0.35,0.5,0.70,1,1.5,2,3,4,6,8,11,16,23,32};
int XeeNumberOfZoomLevels=21;
@implementation XeeController (ImageActions)
-(IBAction)copy:(id)sender
{
if(currimage)
{
CGImageRef cgimage=[currimage createCGImage];
if(cgimage)
{
[[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObjects:NSTIFFPboardType,NSPICTPboardType,nil] owner:self];
copiedcgimage=cgimage;
[self retain];
}
else NSBeep();
}
else NSBeep();
}
-(void)pasteboard:(NSPasteboard *)pboard provideDataForType:(NSString *)type
{
if(!copiedcgimage) return;
if([type isEqual:NSTIFFPboardType])
{
NSMutableData *data=[NSMutableData data];
CGImageDestinationRef dest=CGImageDestinationCreateWithData((CFMutableDataRef)data,kUTTypeTIFF,1,NULL);
if(!dest) { NSBeep(); return; }
CGImageDestinationAddImage(dest,copiedcgimage,NULL);
if(!CGImageDestinationFinalize(dest)) NSBeep();
CFRelease(dest);
[pboard setData:data forType:type];
}
else if([type isEqual:NSPICTPboardType])
{
BOOL res=NO;
Handle outhandle=NewHandle(0);
if(outhandle)
{
GraphicsExportComponent exporter;
if(OpenADefaultComponent(GraphicsExporterComponentType,kQTFileTypePicture,&exporter)==noErr)
{
GraphicsExportSetInputCGImage(exporter,copiedcgimage);
GraphicsExportSetOutputHandle(exporter,outhandle);
//GraphicsExportSetOutputDataReference(exporter,dataRef, dataRefType);
unsigned long size;
if(GraphicsExportDoExport(exporter,&size)==noErr)
{
NSData *data=[NSData dataWithBytes:*outhandle+512 length:size-512];
NSLog(@"%@",[data subdataWithRange:NSMakeRange(0,2*1024)]);
[pboard setData:data forType:type];
res=YES;
}
CloseComponent(exporter);
}
DisposeHandle(outhandle);
}
if(!res) NSBeep();
}
}
-(void)pasteboardChangedOwner:(NSPasteboard *)pboard
{
CGImageRelease(copiedcgimage);
copiedcgimage=NULL;
[self release];
}
-(IBAction)save:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
[source beginSavingImage:currimage];
[self detachBackgroundTaskWithMessage:NSLocalizedString(@"Saving...",@"Message when saving an image")
selector:@selector(saveTask:) target:self object:[currimage retain]];
}
-(void)saveTask:(XeeImage *)saveimage
{
NSString *filename=[saveimage filename];
if(![saveimage losslessSaveTo:filename flags:XeeRetainUntransformableBlocksFlag])
{
NSAlert *alert=[[[NSAlert alloc] init] autorelease];
[alert setMessageText:NSLocalizedString(@"Image saving failed",@"Title of the file saving failure dialog")];
[alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Couldn't save the file \"%@\".",
@"Content of the file saving failure dialog"),[filename lastPathComponent]]];
[alert addButtonWithTitle:NSLocalizedString(@"OK",@"OK button")];
[alert performSelectorOnMainThread:@selector(runModal) withObject:nil waitUntilDone:NO];
[source performSelectorOnMainThread:@selector(endSavingImage:) withObject:saveimage waitUntilDone:YES];
[saveimage release];
}
else
{
[self performSelectorOnMainThread:@selector(finishSave:) withObject:saveimage waitUntilDone:YES];
}
}
-(void)finishSave:(XeeImage *)saveimage
{
if(currimage==saveimage) [undo removeAllActions];
[source endSavingImage:saveimage];
[saveimage release];
}
-(IBAction)saveAs:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
[XeeSavePanel runSavePanelForImage:currimage controller:self];
}
-(IBAction)frameSkipNext:(id)sender
{
[self setResizeBlockFromSender:sender];
if(currimage)
{
int frame=[currimage frame];
int frames=[currimage frames];
[self setFrame:(frame+1)%frames];
}
[self setResizeBlock:NO];
}
-(IBAction)frameSkipPrev:(id)sender
{
[self setResizeBlockFromSender:sender];
if(currimage)
{
int frame=[currimage frame];
int frames=[currimage frames];
[self setFrame:(frame+frames-1)%frames];
}
[self setResizeBlock:NO];
}
-(IBAction)toggleAnimation:(id)sender
{
if(!currimage||![currimage animated]) return;
[currimage setAnimating:![currimage animating]];
}
-(IBAction)zoomIn:(id)sender
{
[self setResizeBlockFromSender:sender];
int i;
for(i=0;i<XeeNumberOfZoomLevels-1;i++) if(XeeZoomLevels[i]>zoom) break;
[self setZoom:XeeZoomLevels[i]];
[self setResizeBlock:NO];
}
-(IBAction)zoomOut:(id)sender
{
[self setResizeBlockFromSender:sender];
int i;
for(i=XeeNumberOfZoomLevels-1;i>0;i--) if(XeeZoomLevels[i]<zoom) break;
[self setZoom:XeeZoomLevels[i]];
[self setResizeBlock:NO];
}
-(IBAction)zoomActual:(id)sender
{
[self setResizeBlockFromSender:sender];
[self setZoom:1];
[self setResizeBlock:NO];
}
-(IBAction)zoomFit:(id)sender
{
[self setResizeBlockFromSender:sender];
NSSize maxsize=[self maxViewSize];
float imgWidthInPoints = (float)[currimage width];
float imgHeightInPoints = (float)[currimage height];
// Retina Support
NSScreen *currentScreen = [imageview.window screen];
if ([currentScreen respondsToSelector:@selector(backingScaleFactor)]) {
float scaleFactor = [currentScreen backingScaleFactor];
imgWidthInPoints/=scaleFactor;
imgHeightInPoints/=scaleFactor;
}
float horiz_zoom=maxsize.width/imgWidthInPoints;
float vert_zoom=maxsize.height/imgHeightInPoints;
[self setZoom:horiz_zoom<vert_zoom?horiz_zoom:vert_zoom];
[self setResizeBlock:NO];
}
-(IBAction)setAutoZoom:(id)sender
{
[self setResizeBlockFromSender:sender];
[self setStandardImageSize];
[self updateStatusBar];
[self setResizeBlock:NO];
}
-(void)setOrientation:(int)orientation
{
[[undo prepareWithInvocationTarget:self] setOrientation:[currimage orientation]];
[currimage setOrientation:orientation];
}
-(IBAction)rotateCW:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:XeeCombineTransformations([currimage orientation],XeeRotateCWTransformation)];
[undo setActionName:NSLocalizedString(@"Rotate Clockwise",@"Name of rotate clockwise action in undo list")];
[self setResizeBlock:NO];
}
-(IBAction)rotateCCW:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:XeeCombineTransformations([currimage orientation],XeeRotateCCWTransformation)];
[undo setActionName:NSLocalizedString(@"Rotate Counter-clockwise",@"Name of rotate counter-clockwise action in undo list")];
[self setResizeBlock:NO];
}
-(IBAction)rotate180:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:XeeCombineTransformations([currimage orientation],XeeRotate180Transformation)];
[undo setActionName:NSLocalizedString(@"Rotate 180",@"Name of rotate 180 action in undo list")];
[self setResizeBlock:NO];
}
-(IBAction)autoRotate:(id)sender
{
if(!currimage) return;
XeeTransformation orientation=[currimage correctOrientation];
if(orientation==XeeUnknownTransformation) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:orientation];
[undo setActionName:NSLocalizedString(@"Automatic Orientation",@"Name of automatic orientation action in undo list")];
[self setResizeBlock:NO];
}
-(IBAction)rotateActual:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:XeeNoTransformation];
[undo setActionName:NSLocalizedString(@"Actual Orientation",@"Name of actual orientation action in undo list")];
[self setResizeBlock:NO];
}
-(IBAction)mirrorHorizontal:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:XeeCombineTransformations([currimage orientation],XeeMirrorHorizontalTransformation)];
[undo setActionName:NSLocalizedString(@"Mirror Horizontal",@"Name of mirror horizontal action in undo list")];
[self setResizeBlock:NO];
}
-(IBAction)mirrorVertical:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
[self setOrientation:XeeCombineTransformations([currimage orientation],XeeMirrorVerticalTransformation)];
[undo setActionName:NSLocalizedString(@"Mirror Vertical",@"Name of mirror vertical action in undo list")];
[self setResizeBlock:NO];
}
-(void)setCroppingRect:(NSRect)rect
{
[[undo prepareWithInvocationTarget:self] setCroppingRect:[currimage croppingRect]];
[currimage setCroppingRect:rect];
}
-(BOOL)isCropping
{
return [imageview tool]==croptool;
}
-(IBAction)crop:(id)sender
{
if(!currimage) return;
[self setResizeBlockFromSender:sender];
if([self isCropping])
{
NSRect currcrop=[currimage croppingRect];
NSRect newcrop=[croptool croppingRect];
newcrop.origin.x+=currcrop.origin.x;
newcrop.origin.y+=currcrop.origin.y;
[self setCroppingRect:newcrop];
[undo setActionName:NSLocalizedString(@"Crop",@"Name of crop action in undo list")];
[imageview setTool:movetool];
croptool=nil;
}
else
{
croptool=(XeeCropTool *)[XeeCropTool toolForView:imageview];
[imageview setTool:croptool];
}
[[window toolbar] validateVisibleItems];
[self setResizeBlock:NO];
}
@end