OSDN Git Service

41ab263a05ebd20ef80d0fbf1805bf011adb29f0
[eliscolors/main.git] / ElisMedia.m
1 //
2 //  ElisMedia.m
3 //  Elis Colors
4 //
5 //  Created by 柳 on 09/09/12.
6 //  Copyright 2009 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "ElisMedia.h"
10
11
12 @implementation ElisMedia
13
14 // QuickTime X を試すためにかなり書き換えた。
15 - (id)initWithMovieFile:(NSString*)path
16 {
17     image = nil;
18     
19     movie = [[QTMovie alloc] initWithFile:path error:nil];   
20     [movie gotoBeginning];
21
22     QTOpenGLTextureContextCreate(kCFAllocatorDefault,
23                                  [[mainView openGLContext] CGLContextObj],
24                                  [[mainView pixelFormat] CGLPixelFormatObj],
25                                  NULL,                  
26                                  &textureContext);
27         
28     SetMovieVisualContext([movie quickTimeMovie], textureContext);
29 //    [mainView setContext:textureContext];
30     
31     attr = [NSDictionary dictionaryWithObjectsAndKeys:
32             QTMovieFrameImageTypeCIImage, QTMovieFrameImageType,
33             [NSNumber numberWithBool:YES], QTMovieFrameImageSessionMode,  // 内部でキャッシュしろ
34             [NSNumber numberWithBool:YES], QTMovieFrameImageHighQuality,  // 画質低下を許さない
35             [movie attributeForKey:QTMovieNaturalSizeAttribute], QTMovieFrameImageSize, // 勝手にリサイズするな
36             [NSNumber numberWithBool:NO], QTMovieFrameImageSingleField, // インターレース解除?
37             nil];
38 //    [attr retain];
39     
40     speed = 1.0;
41     _path = path;
42     playing = NO;
43     
44     return self;
45 }
46
47 - (id)initWithImageFile:(NSString*)path
48 {
49     movie = nil;
50     textureContext = nil;
51     sound = nil;
52
53     image = [[CIImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path]];
54
55     speed = 1.0;
56     _path = path;
57     return self;
58 }
59
60 - (id)initWithSoundFile:(NSString*)path
61 {
62     movie = nil;
63     image = nil;
64     textureContext = nil;
65     sound = [[QTMovie alloc] initWithFile:path error:nil];
66     [sound setVolume:0.5]; // あまりにうるさい。
67     speed = 1.0;
68     _path = path;
69     return self;
70 }
71
72 - (id)initWithText:(NSString*)t
73 {
74     NSImage* im;
75     movie = nil;
76     sound = nil;
77     text = t;
78     attr = [[NSMutableDictionary alloc] init];
79 //    [attr retain];
80     [attr setValue:[NSFont fontWithName:@"HiraKakuPro-W3" size:24.0f] forKey:NSFontAttributeName];
81     [attr setValue:[NSColor colorWithCalibratedRed:255 green:255 blue:255 alpha:255] forKey:NSForegroundColorAttributeName];
82     im = [[NSImage alloc] initWithSize:NSMakeSize(640, 480)];
83     [im lockFocus];
84     [t drawInRect:NSMakeRect(0, 0, 640, 480) withAttributes:attr];
85     [im unlockFocus];
86     image = [[CIImage alloc] initWithData:[im TIFFRepresentation]];
87 //    [im release];
88     speed = 1.0;
89     return self;
90 }
91
92 - (id)initWithQuartzFile:(NSString*)path
93 {
94     return [self initWithMovieFile:path];
95 }    
96
97 // レガシーなので捨てました。
98 // もう一度試してる。
99 - (CIImage*)getFrameForTime:(CVTimeStamp*)timeStamp
100 {
101     CVImageBufferRef currentFrame;
102     CIImage* ret;
103     
104     if(movie){
105         QTVisualContextCopyImageForTime(textureContext, NULL, timeStamp, &currentFrame);
106         ret = [CIImage imageWithCVImageBuffer:currentFrame];
107         CVOpenGLTextureRelease(currentFrame);
108         return ret;
109     }else if(image){
110         return image;
111     }
112     return nil;
113 }
114
115 - (CIImage*)getFrameForQTTime:(QTTime)time
116 {
117     // 64ビットでframeImageAtTimeメソッドは使えないみたい。どうしろと。
118     time.timeValue *= speed;
119     if(movie)
120         return [movie frameImageAtTime:time withAttributes:attr error:nil];
121     else if(image)
122         return image;
123     return nil;
124 }
125
126 - (void)releaseContext
127 {
128     if(movie)
129         QTVisualContextTask(textureContext);
130 }
131
132 - (void)setCurrentTime:(QTTime)time
133 {
134     //    time.timeValue *= speed;
135     if(movie)
136         [movie setAttribute:[NSValue valueWithQTTime:time] forKey:QTMovieCurrentTimeAttribute];
137     if(sound)
138         [sound setCurrentTime:time];
139 }
140
141 - (void)play
142 {
143     if(movie && !playing){
144         [movie play];
145         playing = YES;
146     }
147     if(sound && !playing){
148         [sound play];
149         playing = YES;
150     }
151 }
152
153 - (void)stop
154 {
155     if(movie && YES){
156         [movie stop];
157         playing = NO;
158     }
159     if(sound && playing){
160         [sound stop];
161         playing = NO;
162     }
163 }
164
165 - (QTTime)duration
166 {
167     if(movie)
168         return [movie duration];
169 }
170
171 - (void)finalize
172 {
173     if(movie){
174         NSLog(@"movie finalize");
175         [movie stop];
176         SetMovieVisualContext([movie quickTimeMovie], NULL);
177         movie = nil;
178         CFRelease(textureContext);
179     }
180     
181     [super finalize];
182 }
183
184 @end