-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMovieView_Capture.m
279 lines (252 loc) · 10.3 KB
/
MMovieView_Capture.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
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <[email protected]>
//
// This file is part of Movist.
//
// Movist 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 3 of the License, or
// (at your option) any later version.
//
// Movist 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.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "MMovieView.h"
#import "AppController.h" // for NSApp's delegate
@implementation MMovieView (Capture)
- (void)setCaptureFormat:(int)format { _captureFormat = format; }
- (void)setIncludeLetterBoxOnCapture:(BOOL)include { _includeLetterBoxOnCapture = include; }
- (NSRect)rectForCapture:(BOOL)alternative
{
if (_includeLetterBoxOnCapture) {
return (alternative) ? *(NSRect*)&_movieRect : [self bounds];
}
else {
return (alternative) ? [self bounds] : *(NSRect*)&_movieRect;
}
}
- (NSImage*)captureRect:(NSRect)rect
{
float width = MAX(rect.size.width, _movieRect.size.width);
NSBitmapImageRep* imageRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:0
pixelsWide:rect.size.width pixelsHigh:rect.size.height
bitsPerSample:8 samplesPerPixel:4 hasAlpha:TRUE isPlanar:FALSE
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:width * 4 bitsPerPixel:0] autorelease];
[_drawLock lock];
[[self openGLContext] makeCurrentContext];
glReadPixels((int)rect.origin.x, (int)rect.origin.y,
(int)rect.size.width, (int)rect.size.height,
GL_RGBA, GL_UNSIGNED_BYTE, [imageRep bitmapData]);
[NSOpenGLContext clearCurrentContext];
[_drawLock unlock];
NSImage* image = [[NSImage alloc] initWithSize:rect.size];
[image addRepresentation:imageRep];
// image is flipped. so, flip again. teach me better idea...
NSImage* imageFlipped = [[NSImage alloc] initWithSize:rect.size];
[imageFlipped lockFocus];
[image setFlipped:TRUE];
[image drawAtPoint:NSMakePoint(0, 0) fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
[image release];
[imageFlipped unlockFocus];
return [imageFlipped autorelease];
}
- (NSData*)dataWithImage:(NSImage*)image
{
NSBitmapImageFileType fileType = NSTIFFFileType;
NSMutableDictionary* properties = [NSMutableDictionary dictionary];
if (_captureFormat == CAPTURE_FORMAT_TIFF) {
fileType = NSTIFFFileType;
//[properties setObject:??? forKey:NSImageColorSyncProfileData];
//[properties setObject:??? forKey:NSImageCompressionMethod];
}
else if (_captureFormat == CAPTURE_FORMAT_JPEG) {
fileType = NSJPEGFileType;
//[properties setObject:??? forKey:NSImageColorSyncProfileData];
//[properties setObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
//[properties setObject:??? forKey:NSImageProgressive];
}
else if (_captureFormat == CAPTURE_FORMAT_PNG) {
fileType = NSPNGFileType;
//[properties setObject:??? forKey:NSImageColorSyncProfileData];
//[properties setObject:??? forKey:NSImageGamma];
//[properties setObject:??? forKey:NSImageInterlaced];
}
else if (_captureFormat == CAPTURE_FORMAT_BMP) {
fileType = NSBMPFileType;
}
else if (_captureFormat == CAPTURE_FORMAT_GIF) {
fileType = NSGIFFileType;
//[properties setObject:??? forKey:NSImageColorSyncProfileData];
//[properties setObject:??? forKey:NSImageDitherTransparency];
//[properties setObject:??? forKey:NSImageRGBColorTable];
}
return [[NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]
representationUsingType:fileType properties:properties];
}
- (NSString*)fileExtensionForCaptureFormat:(int)format
{
/*
NSString * NSFileTypeForHFSTypeCode(OSType hfsFileTypeCode);
*/
return (format == CAPTURE_FORMAT_JPEG) ? @"jpeg" :
(format == CAPTURE_FORMAT_PNG) ? @"png" :
(format == CAPTURE_FORMAT_BMP) ? @"bmp" :
(format == CAPTURE_FORMAT_GIF) ? @"gif" :
/* CAPTURE_FORMAT_TIFF */ @"tiff";
}
- (NSString*)capturePathAtDirectory:(NSString*)directory
{
NSString* name = [[[[NSApp delegate] movieURL] path] lastPathComponent];
NSString* ext = [self fileExtensionForCaptureFormat:_captureFormat];
directory = [[directory stringByExpandingTildeInPath]
stringByAppendingPathComponent:[name stringByDeletingPathExtension]];
int i = 1;
NSString* path;
NSFileManager* fileManager = [NSFileManager defaultManager];
while (TRUE) {
path = [directory stringByAppendingFormat:@" %d.%@", i++, ext];
if (![fileManager fileExistsAtPath:path]) {
break;
}
}
return path;
}
- (void)copyCurrentImage:(BOOL)alternative
{
NSImage* image = [self captureRect:[self rectForCapture:alternative]];
NSPasteboard* pboard = [NSPasteboard generalPasteboard];
[pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:self];
[pboard setData:[self dataWithImage:image] forType:NSTIFFPboardType];
}
- (void)saveCurrentImage:(BOOL)alternative
{
NSImage* image = [self captureRect:[self rectForCapture:alternative]];
NSString* path = [self capturePathAtDirectory:@"~/Desktop"];
[[self dataWithImage:image] writeToFile:path atomically:TRUE];
}
- (IBAction)copy:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self copyCurrentImage:[sender tag] != 0];
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma view drag action
- (void)setViewDragAction:(int)action { _viewDragAction = action; }
- (int)viewDragActionWithModifierFlags:(unsigned int)flags
{
return (flags & NSControlKeyMask) ? VIEW_DRAG_ACTION_MOVE_WINDOW :
(flags & NSAlternateKeyMask) ? VIEW_DRAG_ACTION_CAPTURE_MOVIE :
_viewDragAction;
}
- (NSSize)thumbnailSizeForImageSize:(NSSize)imageSize
{
const float MAX_SIZE = 192;
return (imageSize.width < imageSize.height) ?
NSMakeSize(MAX_SIZE * imageSize.width / imageSize.height, MAX_SIZE) :
NSMakeSize(MAX_SIZE, MAX_SIZE * imageSize.height / imageSize.width);
}
- (void)mouseDragged:(NSEvent*)event
{
int action = [self viewDragActionWithModifierFlags:[event modifierFlags]];
if (action == VIEW_DRAG_ACTION_MOVE_WINDOW) {
if (![[NSApp delegate] isFullScreen]) {
[[self window] mouseDragged:event];
}
}
else if (action == VIEW_DRAG_ACTION_CAPTURE_MOVIE) {
BOOL alt = ([event modifierFlags] & NSShiftKeyMask) ? TRUE : FALSE;
_captureImage = [[self captureRect:[self rectForCapture:alt]] retain];
//#define _REAL_SIZE_DRAGGING
#if defined(_REAL_SIZE_DRAGGING)
_draggingPoint = [event locationInWindow];
_draggingPoint.x -= [self frame].origin.x;
_draggingPoint.y -= [self frame].origin.y;
NSRect rect;
rect.size = [_captureImage size];
rect.origin = NSMakePoint(0, 0);
#else
NSRect rect;
rect.size = [self thumbnailSizeForImageSize:[_captureImage size]];
rect.origin = [self convertPoint:[event locationInWindow] fromView:nil];
rect.origin.x -= rect.size.width / 2;
rect.origin.y -= rect.size.height / 2;
#endif
NSString* ext = [self fileExtensionForCaptureFormat:_captureFormat];
[self dragPromisedFilesOfTypes:[NSArray arrayWithObject:ext]
fromRect:rect source:self slideBack:TRUE event:event];
}
}
- (void)dragImage:(NSImage*)image at:(NSPoint)imageLoc offset:(NSSize)mouseOffset
event:(NSEvent*)event pasteboard:(NSPasteboard*)pboard
source:(id)sourceObject slideBack:(BOOL)slideBack
{
#if defined(_REAL_SIZE_DRAGGING)
NSSize size = [_captureImage size];
#else
NSSize size = [self thumbnailSizeForImageSize:[_captureImage size]];
#endif
NSImage* dragImage = [[[NSImage alloc] initWithSize:size] autorelease];
[dragImage lockFocus];
[dragImage setBackgroundColor:[NSColor clearColor]];
[_captureImage drawInRect:NSMakeRect(0, 0, size.width, size.height) fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:0.5];
[dragImage unlockFocus];
[super dragImage:dragImage at:imageLoc offset:mouseOffset
event:event pasteboard:pboard source:sourceObject slideBack:slideBack];
}
- (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL*)dropDestination
{
_capturePath = [[self capturePathAtDirectory:[dropDestination path]] retain];
return [NSArray arrayWithObject:_capturePath];
}
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)forLocal
{
return (forLocal) ? NSDragOperationNone : NSDragOperationCopy;
}
/*
#if defined(_REAL_SIZE_DRAGGING)
- (void)draggedImage:(NSImage*)image movedTo:(NSPoint)screenPoint
{
screenPoint.x += _draggingPoint.x;
screenPoint.y += _draggingPoint.y;
NSSize size;
if (NSPointInRect(screenPoint, [[self window] frame])) {
size = [_captureImage size];
}
else {
size = [self thumbnailSizeForImageSize:[_captureImage size]];
}
if (!NSEqualSizes([image size], size)) {
// FIXME: I don't know how to change image size???
TRACE(@"change to %@",NSEqualSizes([_captureImage size], size) ?
@"real size" : @"thumbnail size");
[image lockFocus];
[image setSize:size];
[image setBackgroundColor:[NSColor clearColor]];
[_captureImage drawInRect:NSMakeRect(0, 0, size.width, size.height) fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:0.5];
[image unlockFocus];
}
}
#endif
*/
- (void)draggedImage:(NSImage*)image
endedAt:(NSPoint)point operation:(NSDragOperation)operation
{
[[self dataWithImage:_captureImage] writeToFile:_capturePath atomically:TRUE];
[_capturePath release], _capturePath = nil;
[_captureImage release], _captureImage = nil;
}
@end