OSDN Git Service

first commit
[eliscolors/main.git] / ElisWriter.m
1 //
2 //  ElisWriter.m
3 //  Elis Colors
4 //
5 //  Created by 柳 on 09/09/17.
6 //  Copyright 2009 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "ElisWriter.h"
10 #import "ElisController.h"
11
12 @implementation ElisWriter
13
14 - (id)init
15 {
16     fps = 30;
17     imageCodec = @"mp4v";
18     imageQuality = [NSNumber numberWithLong:codecMaxQuality];
19     gamma_table = malloc(sizeof(unsigned char) * 256);
20     
21     return self;
22 }
23
24 - (void)setView:(id)v
25 {
26     _mainView = v;
27 }
28
29 - (void)setContoller:(id)c
30 {
31     _mainController = c;
32 }
33
34 - (void)write:(NSString*)path
35 {
36     int frame, i, size, totalFrames;
37     NSImage* image;
38     float seconds;
39     int w, h;
40     unsigned char *buffer, *pixels;
41     QTTime now;
42     NSRect rect;
43     NSBitmapImageRep* rep;
44     
45     [self initializeMovie:path];
46     seconds = [_mainController getHipTime];
47     totalFrames = seconds * fps;
48     w = ProjectMovieSize.size.width;
49     h = ProjectMovieSize.size.height;
50     rect = *(NSRect*)&ProjectMovieSize;
51     
52     buffer = malloc(w*h*3*sizeof(unsigned char));
53     pixels = malloc(w*h*3*sizeof(unsigned char));
54     
55     attrs = [NSDictionary dictionaryWithObjectsAndKeys:imageCodec, QTAddImageCodecType,
56              imageQuality, QTAddImageCodecQuality, 
57              nil];
58     
59     [self readyGammmaTable];
60     
61     for(frame = 1; frame < 300/*totalFrames*/; frame++){
62         NSLog(@"%d", frame);
63         now = QTMakeTime(frame, fps);
64         [_mainView getFrameForQTTime:now];
65         [_mainView getCurrentPixelData:rect buffer:buffer];
66         
67         for(i = 0; i < h; i++) // 富豪的に上下反転。テラ強引
68             memcpy(&pixels[i*w*3], &buffer[(h-1-i)*w*3], w*3*sizeof(unsigned char));
69         
70         [self gammaAdjust:pixels size:w*h*3]; // 色味がシフトする分をガンマ補正。これはひどい。
71         
72         rep = [[NSBitmapImageRep alloc] // RGB RGB RGB ... なバイト列からBitMapImageRepをつくる。
73                initWithBitmapDataPlanes:&pixels
74                pixelsWide:w
75                pixelsHigh:h
76                bitsPerSample:8
77                samplesPerPixel:3
78                hasAlpha:NO
79                isPlanar:NO
80                colorSpaceName:NSCalibratedRGBColorSpace
81                bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
82                bytesPerRow:w*3 
83                bitsPerPixel:24];
84         
85         image = [[NSImage alloc] init];
86         [image addRepresentation:rep];
87         [outputMovie addImage:image forDuration:QTMakeTime(1, fps) withAttributes:attrs];        
88     }
89     
90     free(buffer);
91     free(pixels);
92     
93     [outputMovie updateMovieFile];
94 }
95
96 - (void)initializeMovie:(NSString*)path
97 {
98     outputMovie = [[QTMovie alloc] initToWritableFile:path error:nil];
99 }
100
101 - (void)finalize
102 {
103     free(gamma_table);
104     [super finalize];
105 }
106
107 - (void)readyGammmaTable
108 {
109     int i;
110     
111     for(i = 0; i < 256; i++)
112         gamma_table[i] = (unsigned char)255.0 * pow(i/255.0, 1.0/GAMMA);
113 }
114
115 - (void)gammaAdjust:(unsigned char*)pixels size:(int)s
116 {
117     int i;
118     for(i = 0; i < s; i++)
119         pixels[i] = gamma_table[pixels[i]];
120 }
121
122 @end