OSDN Git Service

MacGui: Add auto crop value to main window
[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
77     [fWidthStepper      setMaxValue: title->width];
78     [fWidthStepper      setIntValue: job->width];
79     [fWidthField        setIntValue: job->width];
80     [fHeightStepper     setMaxValue: title->height];
81     [fHeightStepper     setIntValue: job->height];
82     [fHeightField       setIntValue: job->height];
83     [fRatioCheck        setState:    job->keep_ratio ? NSOnState : NSOffState];
84     [fCropTopStepper    setMaxValue: title->height/2-2];
85     [fCropBottomStepper setMaxValue: title->height/2-2];
86     [fCropLeftStepper   setMaxValue: title->width/2-2];
87     [fCropRightStepper  setMaxValue: title->width/2-2];
88     [fDeinterlaceCheck  setState:    job->deinterlace ? NSOnState : NSOffState];
89         [fPARCheck  setState:    job->pixel_ratio ? NSOnState : NSOffState];
90         if ([fAutoCropMainWindow  intValue] == 0)
91         {
92         [fCropMatrix  selectCellAtRow: 1 column:0];
93         }
94         else
95         {
96         [fCropMatrix  selectCellAtRow: 0 column:0];
97         }
98     MaxOutputWidth = job->width;
99         MaxOutputHeight = job->height;
100     fPicture = 0;
101     [self SettingsChanged: nil];
102 }
103
104 - (void) Display: (int) anim
105 {
106     hb_get_preview( fHandle, fTitle, fPicture, fBuffer );
107
108     /* Backup previous picture (for effects) */
109     memcpy( fTexBuf[1], fTexBuf[0], fTexBufSize );
110
111     if( fHasQE )
112     {
113         /* Simply copy */
114         memcpy( fTexBuf[0], fBuffer, fTexBufSize );
115     }
116     else
117     {
118         /* Copy line by line */
119         uint8_t * in  = fBuffer;
120         uint8_t * out = fTexBuf[0];
121                 
122         for( int i = fTitle->height + 2; i--; )
123         {
124             memcpy( out, in, 4 * ( fTitle->width + 2 ) );
125             in  += 4 * ( fTitle->width + 2 );
126             out += 4 * GetAlignedSize( fTitle->width + 2 );
127         }
128         
129     }
130
131     if( [fEffectsCheck state] == NSOffState )
132     {
133         anim = HB_ANIMATE_NONE;
134     }
135     else if( [[NSApp currentEvent] modifierFlags] & NSShiftKeyMask )
136     {
137         anim |= HB_ANIMATE_SLOW;
138     }
139
140     [fPictureGLView Display: anim buffer1: fTexBuf[0]
141         buffer2: fTexBuf[1] width: ( fTitle->width + 2 )
142         height: ( fTitle->height + 2 )];
143         
144         /* Set the Output Display below the Preview Picture*/
145         int titlewidth = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
146         int arpwidth = fTitle->job->pixel_aspect_width;
147         int arpheight = fTitle->job->pixel_aspect_height;
148         int displayparwidth = titlewidth * arpwidth / arpheight;
149         int displayparheight = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
150         if (fTitle->job->pixel_ratio == 1)
151         {
152         
153         [fInfoField setStringValue: [NSString stringWithFormat:
154         @"Source: %dx%d, Output: %dx%d, Anamorphic: %dx%d", fTitle->width, fTitle->height,
155         MaxOutputWidth, displayparheight, displayparwidth,
156         displayparheight]];
157         
158         
159         }
160         else
161         {
162         [fInfoField setStringValue: [NSString stringWithFormat:
163         @"Source: %dx%d, Output: %dx%d", fTitle->width, fTitle->height,
164         fTitle->job->width, fTitle->job->height]];      
165         }
166
167
168     [fPrevButton setEnabled: ( fPicture > 0 )];
169     [fNextButton setEnabled: ( fPicture < 9 )];
170 }
171
172 - (IBAction) SettingsChanged: (id) sender
173 {
174     hb_job_t * job = fTitle->job;
175     
176         if ([fPARCheck state] == 1 )
177         {
178         [fWidthStepper      setIntValue: MaxOutputWidth];
179         [fWidthField        setIntValue: MaxOutputWidth];
180         
181         /* This will show correct anamorphic height values, but
182         show distorted preview picture ratio */
183         [fHeightStepper      setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
184         [fHeightField        setIntValue: fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1]];
185         
186         /* This will show wrong anamorphic height values, but
187         show proper preview picture ratio */
188         //[fHeightStepper      setIntValue: MaxOutputHeight];
189         //[fHeightField        setIntValue: MaxOutputHeight];
190         [fRatioCheck        setState: 0];
191
192         [fWidthStepper setEnabled: NO];
193         [fWidthField setEnabled: NO];
194         [fHeightStepper setEnabled: NO];
195         [fHeightField setEnabled: NO];
196         [fRatioCheck setEnabled: NO];
197         
198         
199         }
200         else
201         {
202         [fWidthStepper setEnabled: YES];
203         [fWidthField setEnabled: YES];
204         [fHeightStepper setEnabled: YES];
205         [fHeightField setEnabled: YES];
206         [fRatioCheck setEnabled: YES];
207         }
208         
209         
210         
211     job->width       = [fWidthStepper  intValue];
212     job->height      = [fHeightStepper intValue];
213     job->keep_ratio  = ( [fRatioCheck state] == NSOnState );
214     job->deinterlace = ( [fDeinterlaceCheck state] == NSOnState );
215         job->pixel_ratio = ( [fPARCheck state] == NSOnState );
216
217
218
219     bool autocrop = ( [fCropMatrix selectedRow] == 0 );
220     [fCropTopStepper    setEnabled: !autocrop];
221     [fCropBottomStepper setEnabled: !autocrop];
222     [fCropLeftStepper   setEnabled: !autocrop];
223     [fCropRightStepper  setEnabled: !autocrop];
224         [fAutoCropMainWindow  setStringValue: [NSString stringWithFormat:@"%d",autocrop]];
225     if( autocrop )
226     {
227         memcpy( job->crop, fTitle->crop, 4 * sizeof( int ) );
228     }
229     else
230     {
231         job->crop[0] = [fCropTopStepper    intValue];
232         job->crop[1] = [fCropBottomStepper intValue];
233         job->crop[2] = [fCropLeftStepper   intValue];
234         job->crop[3] = [fCropRightStepper  intValue];
235     }
236
237     if( job->keep_ratio )
238     {
239         if( sender == fWidthStepper || sender == fRatioCheck ||
240             sender == fCropTopStepper || sender == fCropBottomStepper )
241         {
242             hb_fix_aspect( job, HB_KEEP_WIDTH );
243             if( job->height > fTitle->height )
244             {
245                 job->height = fTitle->height;
246                 hb_fix_aspect( job, HB_KEEP_HEIGHT );
247             }
248         }
249         else
250         {
251             hb_fix_aspect( job, HB_KEEP_HEIGHT );
252             if( job->width > fTitle->width )
253             {
254                 job->width = fTitle->width;
255                 hb_fix_aspect( job, HB_KEEP_WIDTH );
256             }
257         }
258     }
259     
260     [fWidthStepper      setIntValue: job->width];
261     [fWidthField        setIntValue: job->width];
262     [fHeightStepper     setIntValue: job->height];
263     [fHeightField       setIntValue: job->height];
264     [fCropTopStepper    setIntValue: job->crop[0]];
265     [fCropTopField      setIntValue: job->crop[0]];
266     [fCropBottomStepper setIntValue: job->crop[1]];
267     [fCropBottomField   setIntValue: job->crop[1]];
268     [fCropLeftStepper   setIntValue: job->crop[2]];
269     [fCropLeftField     setIntValue: job->crop[2]];
270     [fCropRightStepper  setIntValue: job->crop[3]];
271     [fCropRightField    setIntValue: job->crop[3]];
272     [self Display: HB_ANIMATE_NONE];
273 }
274
275 - (IBAction) PreviousPicture: (id) sender
276 {   
277     if( fPicture <= 0 )
278     {
279         return;
280     }
281     fPicture--;
282     [self Display: HB_ANIMATE_BACKWARD];
283 }
284
285 - (IBAction) NextPicture: (id) sender
286 {
287     if( fPicture >= 9 )
288     {
289         return;
290     }
291     fPicture++;
292     [self Display: HB_ANIMATE_FORWARD];
293 }
294
295 - (IBAction) ClosePanel: (id) sender
296 {
297
298         [NSApp stopModal];
299 }
300
301 @end