OSDN Git Service

f4e9d51d9ca71a9a6dbdb271804a8fe1e506d5ca
[handbrake-jp/handbrake-jp-git.git] / macosx / PictureController.mm
1 /* $Id: PictureController.mm,v 1.11 2005/08/01 15:10:44 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "PictureController.h"
8
9 static int GetAlignedSize( int size )
10 {
11     int result = 1;
12     while( result < size )
13     {
14         result *= 2;
15     }
16     return result;
17 }
18
19 @implementation PictureController
20
21 - (void) SetHandle: (hb_handle_t *) handle
22 {
23     fHandle = handle;
24
25     fHasQE = CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay );
26
27     fBuffer     = NULL;
28     fBufferSize = 0;
29     fTexBuf[0]  = NULL;
30     fTexBuf[1]  = NULL;
31     fTexBufSize = 0;
32
33     [fWidthStepper  setValueWraps: NO];
34     [fWidthStepper  setIncrement: 16];
35     [fWidthStepper  setMinValue: 16];
36     [fHeightStepper setValueWraps: NO];
37     [fHeightStepper setIncrement: 16];
38     [fHeightStepper setMinValue: 16];
39
40     [fCropTopStepper    setIncrement: 2];
41     [fCropTopStepper    setMinValue:  0];
42     [fCropBottomStepper setIncrement: 2];
43     [fCropBottomStepper setMinValue:  0];
44     [fCropLeftStepper   setIncrement: 2];
45     [fCropLeftStepper   setMinValue:  0];
46     [fCropRightStepper  setIncrement: 2];
47     [fCropRightStepper  setMinValue:  0];
48 }
49
50 - (void) SetTitle: (hb_title_t *) title
51 {
52     hb_job_t * job = title->job;
53
54     fTitle = title;
55
56     /* Make sure we have big enough buffers */
57     int newSize;
58     newSize = ( title->width + 2 ) * (title->height + 2 ) * 4;
59     if( fBufferSize < newSize )
60     {
61         fBufferSize = newSize;
62         fBuffer     = (uint8_t *) realloc( fBuffer, fBufferSize );
63     }
64     if( !fHasQE )
65     {
66         newSize = ( GetAlignedSize( title->width + 2 ) *
67             GetAlignedSize( title->height + 2 ) * 4 );
68     }
69     if( fTexBufSize < newSize )
70     {
71         fTexBufSize = newSize;
72         fTexBuf[0]  = (uint8_t *) realloc( fTexBuf[0], fTexBufSize );
73         fTexBuf[1]  = (uint8_t *) realloc( fTexBuf[1], fTexBufSize );
74     }
75
76     [fWidthStepper      setMaxValue: title->width];
77     [fWidthStepper      setIntValue: job->width];
78     [fWidthField        setIntValue: job->width];
79     [fHeightStepper     setMaxValue: title->height];
80     [fHeightStepper     setIntValue: job->height];
81     [fHeightField       setIntValue: job->height];
82     [fRatioCheck        setState:    job->keep_ratio ? NSOnState : NSOffState];
83     [fCropTopStepper    setMaxValue: title->height/2-2];
84     [fCropBottomStepper setMaxValue: title->height/2-2];
85     [fCropLeftStepper   setMaxValue: title->width/2-2];
86     [fCropRightStepper  setMaxValue: title->width/2-2];
87     [fDeinterlaceCheck  setState:    job->deinterlace ? NSOnState : NSOffState];
88
89     fPicture = 0;
90     [self SettingsChanged: nil];
91 }
92
93 - (void) Display: (int) anim
94 {
95     hb_get_preview( fHandle, fTitle, fPicture, fBuffer );
96
97     /* Backup previous picture (for effects) */
98     memcpy( fTexBuf[1], fTexBuf[0], fTexBufSize );
99
100     if( fHasQE )
101     {
102         /* Simply copy */
103         memcpy( fTexBuf[0], fBuffer, fTexBufSize );
104     }
105     else
106     {
107         /* Copy line by line */
108         uint8_t * in  = fBuffer;
109         uint8_t * out = fTexBuf[0];
110         for( int i = fTitle->height + 2; i--; )
111         {
112             memcpy( out, in, 4 * ( fTitle->width + 2 ) );
113             in  += 4 * ( fTitle->width + 2 );
114             out += 4 * GetAlignedSize( fTitle->width + 2 );
115         }
116     }
117
118     if( [fEffectsCheck state] == NSOffState )
119     {
120         anim = HB_ANIMATE_NONE;
121     }
122     else if( [[NSApp currentEvent] modifierFlags] & NSShiftKeyMask )
123     {
124         anim |= HB_ANIMATE_SLOW;
125     }
126
127     [fPictureGLView Display: anim buffer1: fTexBuf[0]
128         buffer2: fTexBuf[1] width: ( fTitle->width + 2 )
129         height: ( fTitle->height + 2 )];
130
131     [fInfoField setStringValue: [NSString stringWithFormat:
132         @"Source %dx%d, output %dx%d", fTitle->width, fTitle->height,
133         fTitle->job->width, fTitle->job->height]];
134
135     [fPrevButton setEnabled: ( fPicture > 0 )];
136     [fNextButton setEnabled: ( fPicture < 9 )];
137 }
138
139 - (IBAction) SettingsChanged: (id) sender
140 {
141     hb_job_t * job = fTitle->job;
142
143     job->width       = [fWidthStepper  intValue];
144     job->height      = [fHeightStepper intValue];
145     job->keep_ratio  = ( [fRatioCheck state] == NSOnState );
146     job->deinterlace = ( [fDeinterlaceCheck state] == NSOnState );
147
148     bool autocrop = ( [fCropMatrix selectedRow] == 0 );
149     [fCropTopStepper    setEnabled: !autocrop];
150     [fCropBottomStepper setEnabled: !autocrop];
151     [fCropLeftStepper   setEnabled: !autocrop];
152     [fCropRightStepper  setEnabled: !autocrop];
153     if( autocrop )
154     {
155         memcpy( job->crop, fTitle->crop, 4 * sizeof( int ) );
156     }
157     else
158     {
159         job->crop[0] = [fCropTopStepper    intValue];
160         job->crop[1] = [fCropBottomStepper intValue];
161         job->crop[2] = [fCropLeftStepper   intValue];
162         job->crop[3] = [fCropRightStepper  intValue];
163     }
164
165     if( job->keep_ratio )
166     {
167         if( sender == fWidthStepper || sender == fRatioCheck ||
168             sender == fCropTopStepper || sender == fCropBottomStepper )
169         {
170             hb_fix_aspect( job, HB_KEEP_WIDTH );
171             if( job->height > fTitle->height )
172             {
173                 job->height = fTitle->height;
174                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
175             }
176         }
177         else
178         {
179             hb_fix_aspect( job, HB_KEEP_HEIGHT );
180             if( job->width > fTitle->width )
181             {
182                 job->width = fTitle->width;
183                 hb_fix_aspect( job, HB_KEEP_WIDTH );
184             }
185         }
186     }
187     
188     [fWidthStepper      setIntValue: job->width];
189     [fWidthField        setIntValue: job->width];
190     [fHeightStepper     setIntValue: job->height];
191     [fHeightField       setIntValue: job->height];
192     [fCropTopStepper    setIntValue: job->crop[0]];
193     [fCropTopField      setIntValue: job->crop[0]];
194     [fCropBottomStepper setIntValue: job->crop[1]];
195     [fCropBottomField   setIntValue: job->crop[1]];
196     [fCropLeftStepper   setIntValue: job->crop[2]];
197     [fCropLeftField     setIntValue: job->crop[2]];
198     [fCropRightStepper  setIntValue: job->crop[3]];
199     [fCropRightField    setIntValue: job->crop[3]];
200     [self Display: HB_ANIMATE_NONE];
201 }
202
203 - (IBAction) PreviousPicture: (id) sender
204 {   
205     if( fPicture <= 0 )
206     {
207         return;
208     }
209     fPicture--;
210     [self Display: HB_ANIMATE_BACKWARD];
211 }
212
213 - (IBAction) NextPicture: (id) sender
214 {
215     if( fPicture >= 9 )
216     {
217         return;
218     }
219     fPicture++;
220     [self Display: HB_ANIMATE_FORWARD];
221 }
222
223 - (IBAction) ClosePanel: (id) sender
224 {
225     [NSApp stopModal];
226 }
227
228 @end