OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Controls / PictureSettings.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using Handbrake.Parsing;\r
5 \r
6 namespace Handbrake.Controls\r
7 {\r
8     // TODO\r
9     // - Tie in the cropping controls.\r
10     \r
11     public partial class PictureSettings : UserControl\r
12     {\r
13         // Globals\r
14         public int maxWidth, maxHeight;\r
15         int widthVal, heightVal;\r
16         private double darValue;\r
17         private double storageAspect; // Storage Aspect Cache for current source and title\r
18         public Title selectedTitle { private get; set; }\r
19         private Boolean heightChangeGuard;\r
20         private Boolean looseAnamorphicHeightGuard;\r
21         private Boolean heightModJumpGaurd;\r
22 \r
23         // Window Setup\r
24         public PictureSettings()\r
25         {\r
26             InitializeComponent();\r
27             lbl_max.Text = "";\r
28             drop_modulus.SelectedIndex = 0;\r
29             storageAspect = 0;\r
30         }\r
31         public void setComponentsAfterScan(Title st)\r
32         {\r
33             storageAspect = 0;\r
34             selectedTitle = st;\r
35             // Set the Aspect Ratio\r
36             lbl_Aspect.Text = selectedTitle.AspectRatio.ToString();\r
37             lbl_src_res.Text = selectedTitle.Resolution.Width + " x " + selectedTitle.Resolution.Height;\r
38 \r
39             // Set the Recommended Cropping values\r
40             crop_top.Text = selectedTitle.AutoCropDimensions[0].ToString();\r
41             crop_bottom.Text = selectedTitle.AutoCropDimensions[1].ToString();\r
42             crop_left.Text = selectedTitle.AutoCropDimensions[2].ToString();\r
43             crop_right.Text = selectedTitle.AutoCropDimensions[3].ToString();\r
44 \r
45 \r
46             // Set the Resolution Boxes\r
47             text_width.Value = selectedTitle.Resolution.Width;\r
48 \r
49             if (drp_anamorphic.SelectedIndex == 0)\r
50                 text_height.Value = cacluateHeight(selectedTitle.Resolution.Width);\r
51             else if (drp_anamorphic.SelectedIndex == 1 || drp_anamorphic.SelectedIndex == 3)\r
52             {\r
53                 heightModJumpGaurd = true;\r
54                 text_height.Value = selectedTitle.Resolution.Height - (int) crop_top.Value - (int) crop_bottom.Value;\r
55             }\r
56             else if (drp_anamorphic.SelectedIndex == 2)\r
57             {\r
58                 heightModJumpGaurd = false;\r
59                 text_height.Value = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
60             }\r
61 \r
62             if (drp_anamorphic.SelectedIndex == 3)\r
63             {\r
64                 txt_parWidth.Text = selectedTitle.ParVal.Width.ToString();\r
65                 txt_parHeight.Text = selectedTitle.ParVal.Height.ToString();\r
66                 txt_displayWidth.Text = displayWidth().ToString();\r
67             }\r
68 \r
69             setMax();\r
70         }\r
71         public void setMax()\r
72         {\r
73             if (maxWidth != 0 && maxHeight != 0)\r
74                 lbl_max.Text = "Max Width / Height";\r
75             else if (maxWidth != 0)\r
76                 lbl_max.Text = "Max Width";\r
77             else\r
78                 lbl_max.Text = "";\r
79         }\r
80 \r
81         // Basic Picture Setting Controls\r
82         private void text_width_ValueChanged(object sender, EventArgs e)\r
83         {\r
84             maxWidth = 0;\r
85             setMax();\r
86             \r
87             // Get the Modulus\r
88             int mod;\r
89             int.TryParse(drop_modulus.SelectedItem.ToString(), out mod);\r
90 \r
91             // Increase or decrease value by the correct mod.\r
92             text_width.Value = widthChangeMod(mod);\r
93 \r
94             // Mode Switch\r
95             switch (drp_anamorphic.SelectedIndex)\r
96             {\r
97                 case 0:\r
98                     if (calculateUnchangeValue(true) != -1 && check_KeepAR.Checked)\r
99                         text_height.Value = calculateUnchangeValue(true);\r
100                     break;\r
101                 case 1:\r
102                     lbl_anamorphic.Text = strictAnamorphic();\r
103                     break;\r
104                 case 2:\r
105                     lbl_anamorphic.Text = looseAnamorphic();\r
106                     break;\r
107                 case 3:\r
108                     customAnamorphic(text_width);\r
109                     break;\r
110             }\r
111                 \r
112         }\r
113         private void text_height_ValueChanged(object sender, EventArgs e)\r
114         {\r
115             maxHeight = 0;\r
116             setMax();\r
117 \r
118             // Get the Modulus\r
119             int mod;\r
120             int.TryParse(drop_modulus.SelectedItem.ToString(), out mod);\r
121 \r
122             // Increase or decrease value by the correct mod.\r
123             if (drp_anamorphic.SelectedIndex != 2 && !heightModJumpGaurd)\r
124             {\r
125                 decimal val = heightChangeMod(mod);\r
126                 heightChangeGuard = true;\r
127                 if (text_height.Value != val)\r
128                 {\r
129                     heightChangeGuard = false;\r
130                     text_height.Value = val;\r
131                 }\r
132             }\r
133 \r
134             // Mode Switch\r
135             switch (drp_anamorphic.SelectedIndex)\r
136             {\r
137                 case 0:\r
138                     if (drp_anamorphic.SelectedIndex != 3)\r
139                     {\r
140                         if (calculateUnchangeValue(false) != -1)\r
141                             text_width.Value = calculateUnchangeValue(false);\r
142                     }\r
143                     break;\r
144                 case 1:\r
145                     lbl_anamorphic.Text = strictAnamorphic();\r
146                     break;\r
147                 case 2:\r
148                     if (!looseAnamorphicHeightGuard)\r
149                         lbl_anamorphic.Text = looseAnamorphic();\r
150                     break;\r
151                 case 3:\r
152                     if (!heightChangeGuard)\r
153                         customAnamorphic(text_height);\r
154                     break;\r
155             }\r
156             heightChangeGuard = false;\r
157             looseAnamorphicHeightGuard = false;\r
158             heightModJumpGaurd = false;\r
159         }\r
160         private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
161         {\r
162             // Recalculate Height based on width when enabled\r
163             if (drp_anamorphic.SelectedIndex != 3 && check_KeepAR.Checked && selectedTitle != null)\r
164                 text_height.Value = cacluateHeight(widthVal);\r
165 \r
166             // Enable Par Width/Height Check boxes if Keep AR is enabled, otherwise disable them\r
167             if (check_KeepAR.Checked)\r
168             {\r
169                 txt_parWidth.Enabled = false;\r
170                 txt_parHeight.Enabled = false;\r
171             }\r
172             else\r
173             {\r
174                 txt_parWidth.Enabled = true;\r
175                 txt_parHeight.Enabled = true;\r
176             }\r
177         }\r
178         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
179         {\r
180             switch (drp_anamorphic.SelectedIndex)\r
181             {\r
182                 case 0: // None\r
183                     text_height.Enabled = true;\r
184                     text_width.Enabled = true;\r
185                     check_KeepAR.CheckState = CheckState.Checked;\r
186                     check_KeepAR.Enabled = true;\r
187                     disableCustomAnaControls();\r
188                     if (selectedTitle != null)\r
189                     {\r
190                         text_width.Value = selectedTitle.Resolution.Width;\r
191                         text_height.Value = selectedTitle.Resolution.Height;\r
192                     }\r
193                     check_KeepAR.Enabled = true;\r
194                     lbl_anamorphic.Text = "";\r
195                     lbl_anamprohicLbl.Visible = false;\r
196                     break;\r
197                 case 1: // Strict\r
198                     if (selectedTitle != null)\r
199                     {\r
200                         heightModJumpGaurd = true;\r
201                         text_width.Value = selectedTitle.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
202                         text_height.Value = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
203                     }\r
204                     text_height.Enabled = false;\r
205                     text_width.Enabled = false;\r
206                     check_KeepAR.CheckState = CheckState.Unchecked;\r
207                     check_KeepAR.Enabled = false;\r
208                     disableCustomAnaControls();\r
209                     lbl_anamorphic.Text = strictAnamorphic();\r
210                     lbl_anamprohicLbl.Visible = true;\r
211                     break;\r
212                 case 2: // Loose\r
213                     disableCustomAnaControls();\r
214                     storageAspect = 0;\r
215                     text_height.Enabled = false;\r
216                     text_width.Enabled = true;\r
217                     if (selectedTitle != null)\r
218                     {\r
219                         heightModJumpGaurd = true;\r
220                         text_width.Value = selectedTitle.Resolution.Width;\r
221                         text_height.Value = selectedTitle.Resolution.Height - (int) crop_top.Value - (int) crop_bottom.Value;\r
222                     }\r
223                     lbl_anamorphic.Text = looseAnamorphic();\r
224                     lbl_anamprohicLbl.Visible = true;\r
225                     break;\r
226                 case 3: // Custom\r
227 \r
228                     // Display Elements\r
229                     enableCustomAnaControls();\r
230                     text_height.Enabled = true;\r
231                     text_width.Enabled = true;\r
232 \r
233                     // Actual Work  \r
234                     if (selectedTitle != null)\r
235                     {\r
236                         heightModJumpGaurd = true;\r
237                         widthVal = selectedTitle.Resolution.Width;\r
238                         text_width.Value = selectedTitle.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
239                         text_height.Value = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
240                         txt_parWidth.Text = selectedTitle.ParVal.Width.ToString();\r
241                         txt_parHeight.Text = selectedTitle.ParVal.Height.ToString();\r
242                         txt_displayWidth.Text = displayWidth().ToString();\r
243                     }\r
244    \r
245                     darValue = calculateDar();\r
246 \r
247                     check_KeepAR.CheckState = CheckState.Checked;\r
248                     check_KeepAR.Enabled = true;\r
249                     lbl_anamprohicLbl.Visible = true;\r
250 \r
251                     break;\r
252             }\r
253         }\r
254 \r
255         // Custom Anamorphic Controls\r
256         private void txt_displayWidth_Keyup(object sender, KeyEventArgs e)\r
257         {\r
258             if (e.KeyCode == Keys.Enter)\r
259                 customAnamorphic(txt_displayWidth);\r
260         }\r
261         private void txt_parHeight_Keyup(object sender, KeyEventArgs e)\r
262         {\r
263             if (e.KeyCode == Keys.Enter)\r
264                 customAnamorphic(txt_parHeight);\r
265         }\r
266         private void txt_parWidth_Keyup(object sender, KeyEventArgs e)\r
267         {\r
268             if (e.KeyCode == Keys.Enter)\r
269                 customAnamorphic(txt_parWidth);\r
270         }\r
271         \r
272         // Cropping Controls\r
273         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
274         {\r
275             crop_left.Enabled = false;\r
276             crop_right.Enabled = false;\r
277             crop_top.Enabled = false;\r
278             crop_bottom.Enabled = false;\r
279         }\r
280         private void check_customCrop_CheckedChanged(object sender, EventArgs e)\r
281         {\r
282             crop_left.Enabled = true;\r
283             crop_right.Enabled = true;\r
284             crop_top.Enabled = true;\r
285             crop_bottom.Enabled = true;\r
286             if (selectedTitle != null)\r
287             {\r
288                 crop_top.Text = selectedTitle.AutoCropDimensions[0].ToString();\r
289                 crop_bottom.Text = selectedTitle.AutoCropDimensions[1].ToString();\r
290                 crop_left.Text = selectedTitle.AutoCropDimensions[2].ToString();\r
291                 crop_right.Text = selectedTitle.AutoCropDimensions[3].ToString();\r
292             }\r
293             else\r
294             {\r
295                 crop_left.Text = "0";\r
296                 crop_right.Text = "0";\r
297                 crop_top.Text = "0";\r
298                 crop_bottom.Text = "0";\r
299             }\r
300         }\r
301         private void crop_left_ValueChanged(object sender, EventArgs e)\r
302         {\r
303             if (crop_left.Value % 2 != 0)\r
304                 crop_left.Value++;\r
305         }\r
306         private void crop_right_ValueChanged(object sender, EventArgs e)\r
307         {\r
308             if (crop_right.Value % 2 != 0)\r
309                 crop_right.Value++;\r
310         }\r
311         private void crop_top_ValueChanged(object sender, EventArgs e)\r
312         {\r
313             if (crop_top.Value % 2 != 0)\r
314                 crop_top.Value++;\r
315         }\r
316         private void crop_bottom_ValueChanged(object sender, EventArgs e)\r
317         {\r
318             if (crop_bottom.Value % 2 != 0)\r
319                 crop_bottom.Value++;\r
320         }\r
321 \r
322         // Custom Anamorphic Code\r
323         private void customAnamorphic(Control control)\r
324         {\r
325             // Get and parse all the required values\r
326             int cropLeft = (int)crop_left.Value;\r
327             int cropRight = (int)crop_right.Value;\r
328 \r
329             int width = (int)text_width.Value;\r
330             int cropped_width = width - cropLeft - cropRight;\r
331 \r
332             int mod = 16;\r
333             int.TryParse(drop_modulus.SelectedItem.ToString(), out mod);\r
334 \r
335             int parW, parH;\r
336             double displayWidth;\r
337             int.TryParse(txt_parWidth.Text, out parW);\r
338             int.TryParse(txt_parHeight.Text, out parH);\r
339             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
340 \r
341             /* NOT KEEPING DISPLAY ASPECT\r
342              * Changing STORAGE WIDTH changes DISPLAY WIDTH to STORAGE WIDTH * PIXEL WIDTH / PIXEL HEIGHT\r
343              * Changing PIXEL dimensions changes DISPLAY WIDTH to STORAGE WIDTH * PIXEL WIDTH / PIXEL HEIGHT\r
344              * Changing DISPLAY WIDTH changes PIXEL WIDTH to DISPLAY WIDTH and PIXEL HEIGHT to STORAGE WIDTH\r
345              * Changing HEIGHT just....changes the height.\r
346              */\r
347             if (!check_KeepAR.Checked)\r
348             {\r
349                 switch (control.Name)\r
350                 {\r
351                     case "text_width":\r
352                         double dw = (double)cropped_width * parW / parH;\r
353                         dw = Math.Round(dw, 2);\r
354                         txt_displayWidth.Text = dw.ToString();\r
355                         break;\r
356                     case "txt_parWidth":\r
357                         double dwpw = (double)cropped_width * parW / parH;\r
358                         dwpw = Math.Round(dwpw, 2);\r
359                         txt_displayWidth.Text = dwpw.ToString();\r
360                         break;\r
361                     case "txt_parHeight":\r
362                         double dwph = (double)cropped_width * parW / parH;\r
363                         dwph = Math.Round(dwph, 2);\r
364                         txt_displayWidth.Text = dwph.ToString();\r
365                         break;\r
366                     case "txt_displayWidth":\r
367                         txt_parWidth.Text = Math.Round(displayWidth, 0).ToString();\r
368                         txt_parHeight.Text = text_width.Text;\r
369                         break;\r
370                 }\r
371             }\r
372 \r
373             /*\r
374              * KEEPING DISPLAY ASPECT RATIO\r
375              * DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
376              * Disable editing: PIXEL WIDTH, PIXEL HEIGHT\r
377              * Changing DISPLAY WIDTH:\r
378              *     Changes HEIGHT to keep DAR\r
379              *     Changes PIXEL WIDTH to new DISPLAY WIDTH\r
380              *     Changes PIXEL HEIGHT to STORAGE WIDTH\r
381              * Changing HEIGHT\r
382              *     Changes DISPLAY WIDTH to keep DAR\r
383              *     Changes PIXEL WIDTH to new DISPLAY WIDTH\r
384              *     Changes PIXEL HEIGHT to STORAGE WIDTH\r
385              * Changing STORAGE_WIDTH:\r
386              *     Changes PIXEL WIDTH to DISPLAY WIDTH\r
387              *     Changes PIXEL HEIGHT to new STORAGE WIDTH \r
388              */\r
389 \r
390             if (check_KeepAR.Checked)\r
391             {\r
392                 switch (control.Name)\r
393                 {\r
394                     case "txt_displayWidth":\r
395                         heightChangeGuard = true;\r
396                         text_height.Value = (decimal)getHeightKeepDar();  //Changes HEIGHT to keep DAR\r
397                         //darValue = calculateDar(); // Cache the dar value\r
398                         txt_parWidth.Text = txt_displayWidth.Text;\r
399                         txt_parHeight.Text = cropped_width.ToString();\r
400                         break;\r
401                     case "text_height":\r
402                         heightChangeGuard = true;\r
403                         txt_displayWidth.Text = getDisplayWidthKeepDar().ToString();  //Changes DISPLAY WIDTH to keep DAR\r
404                         txt_parWidth.Text = txt_displayWidth.Text;\r
405                         txt_parHeight.Text = cropped_width.ToString();\r
406                         break; \r
407                     case "text_width":\r
408                         txt_parWidth.Text = txt_displayWidth.Text;\r
409                         txt_parHeight.Text = cropped_width.ToString();\r
410                         break;\r
411                 }\r
412             }\r
413         }\r
414         private double getDisplayWidthKeepDar()\r
415         {\r
416             double displayWidth;\r
417             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
418             double currentDar = calculateDar();\r
419             double newDwValue = displayWidth;\r
420 \r
421             // Correct display width up or down to correct for dar.           \r
422             if (currentDar > darValue)\r
423             {\r
424                 while (currentDar > darValue)\r
425                 {\r
426                     displayWidth--;\r
427                     newDwValue = displayWidth;\r
428                     currentDar = calculateDarByVal(text_height.Value, displayWidth);\r
429                 }\r
430             }\r
431             else\r
432             {\r
433                 while (currentDar < darValue)\r
434                 {\r
435                     displayWidth++;\r
436                     newDwValue = displayWidth;\r
437                     currentDar = calculateDarByVal(text_height.Value, displayWidth);\r
438                 }\r
439             }\r
440 \r
441             return Math.Round(newDwValue, 2);\r
442         }\r
443         private double getHeightKeepDar()\r
444         {\r
445             double displayWidth;\r
446             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
447             double currentDar = calculateDar();\r
448             double newHeightVal = heightVal;\r
449 \r
450             // Correct display width up or down.\r
451             if (currentDar > darValue)\r
452             {\r
453                 while (currentDar > darValue)\r
454                 {\r
455                     heightVal++;\r
456                     newHeightVal = heightVal;\r
457                     currentDar = calculateDarByVal(heightVal, displayWidth);\r
458                 }\r
459             }\r
460             else\r
461             {\r
462                 while (currentDar < darValue)\r
463                 {\r
464                     heightVal--;\r
465                     newHeightVal = heightVal;\r
466                     currentDar = calculateDarByVal(heightVal, displayWidth);\r
467                 }\r
468             }\r
469 \r
470             return newHeightVal;\r
471         }\r
472         private double calculateDar()\r
473         {\r
474             // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
475             double displayWidth;\r
476             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
477 \r
478             double calculatedDar = displayWidth / (int)text_height.Value;\r
479 \r
480             return calculatedDar;\r
481         }\r
482         private double calculateDarByVal(decimal croppedHeight, double displayWidth)\r
483         {\r
484             // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
485             double calculatedDar = darValue;\r
486             if (croppedHeight > 0)\r
487                 calculatedDar = displayWidth / (double)croppedHeight;\r
488 \r
489             return calculatedDar;\r
490         }\r
491         private int displayWidth()\r
492         {\r
493             if (selectedTitle != null)\r
494             {\r
495                 int actualWidth = (int)text_width.Value;\r
496                 int displayWidth = 0;\r
497                 int parW, parH;\r
498 \r
499                 int.TryParse(txt_parWidth.Text, out parW);\r
500                 int.TryParse(txt_parHeight.Text, out parH);\r
501 \r
502                 if (drp_anamorphic.SelectedIndex != 3)\r
503                     displayWidth = (actualWidth * selectedTitle.ParVal.Width / selectedTitle.ParVal.Height);\r
504                 else if (parW > 0 && parH > 0)\r
505                     displayWidth = (actualWidth * parW / parH);\r
506 \r
507                 return displayWidth;\r
508             }\r
509             return -1;\r
510         }\r
511 \r
512         // Resolution calculation and controls\r
513         private decimal widthChangeMod(int mod)\r
514         {\r
515             // Increase or decrease the height based on the users input.\r
516             decimal returnVal = text_width.Value > widthVal ? getResolutionJump(mod, text_width.Value, true) : getResolutionJump(mod, text_width.Value, false);\r
517 \r
518             // Make sure we don't go above source value\r
519             if (selectedTitle != null)\r
520                 if (selectedTitle.Resolution.Width < returnVal)\r
521                     returnVal = selectedTitle.Resolution.Width;\r
522 \r
523             // Set the global tracker\r
524             widthVal = (int)returnVal;\r
525 \r
526             return returnVal;\r
527         }\r
528         private decimal heightChangeMod(int mod)\r
529         {\r
530             // Increase or decrease the height based on the users input.\r
531             decimal returnVal = text_height.Value > heightVal ? getResolutionJump(mod, text_height.Value, true) : getResolutionJump(mod, text_height.Value, false);\r
532 \r
533             // Make sure we don't go above source value\r
534             if (selectedTitle != null)\r
535                 if (selectedTitle.Resolution.Height < returnVal)\r
536                     returnVal = selectedTitle.Resolution.Height;\r
537 \r
538             // Set the global tracker\r
539             heightVal = (int)returnVal;     // TODO THIS IS CAUSING PROBLEM\r
540 \r
541             return returnVal;\r
542         }\r
543         private decimal calculateUnchangeValue(Boolean widthChangeFromControl)\r
544         {\r
545             decimal newValue = -1;\r
546             if (selectedTitle != null && drp_anamorphic.SelectedIndex != 3 && drp_anamorphic.SelectedIndex != 2)\r
547                 if (widthChangeFromControl)\r
548                     newValue = cacluateHeight(widthVal);\r
549                 else\r
550                 {\r
551                     if (check_KeepAR.Checked)\r
552                         newValue = cacluateWidth(heightVal);\r
553                 }\r
554 \r
555             return newValue;\r
556         }\r
557         private int getResolutionJump(int mod, decimal value, Boolean up)\r
558         {\r
559             if (up)\r
560                 while ((value % mod) != 0)\r
561                     value++;\r
562             else\r
563                 while ((value % mod) != 0)\r
564                     value--;\r
565 \r
566             return (int)value;\r
567         }\r
568         private double getModulusAuto(int mod, double value)\r
569         {\r
570             int modDiv2 = mod / 2;\r
571 \r
572             if ((value % mod) != 0)\r
573             {\r
574                 double modVal = (int)value % mod;\r
575                 if (modVal >= modDiv2)\r
576                 {\r
577                     modVal = 16 - modVal;\r
578                     value = (int)value + (int)modVal;\r
579                 }\r
580                 else\r
581                 {\r
582                     value = (int)value - (int)modVal;\r
583                 }\r
584             }\r
585             return value;\r
586         }\r
587         private int cacluateHeight(int width)\r
588         {\r
589             if (selectedTitle != null)\r
590             {\r
591                 int aw = 0;\r
592                 int ah = 0;\r
593                 if (selectedTitle.AspectRatio.ToString() == "1.78")\r
594                 {\r
595                     aw = 16;\r
596                     ah = 9;\r
597                 }\r
598                 else if (selectedTitle.AspectRatio.ToString() == "1.33")\r
599                 {\r
600                     aw = 4;\r
601                     ah = 3;\r
602                 }\r
603 \r
604                 if (aw != 0)\r
605                 {\r
606                     // Crop_Width = Title->Width - crop_Left - crop_right\r
607                     // Crop_Height = Title->Height - crop_top - crop_bottom\r
608                     double crop_width = selectedTitle.Resolution.Width - (double) crop_left.Value -\r
609                                         (double) crop_right.Value;\r
610                     double crop_height = selectedTitle.Resolution.Height - (double) crop_top.Value -\r
611                                          (double) crop_bottom.Value;\r
612 \r
613                     double new_height = (width*selectedTitle.Resolution.Width*ah*crop_height)/\r
614                                         (selectedTitle.Resolution.Height*aw*crop_width);\r
615 \r
616                     if (drp_anamorphic.SelectedIndex == 3)\r
617                         new_height = getModulusAuto(int.Parse(drop_modulus.SelectedItem.ToString()), new_height);\r
618                     else\r
619                         new_height = getModulusAuto(16, new_height);\r
620 \r
621                     //16 * (421 / 16)\r
622                     //double z = ( 16 * (( y + 8 ) / 16 ) );\r
623                     int x = int.Parse(new_height.ToString());\r
624                     if (x < 64)\r
625                         x = 64;\r
626                     return x;\r
627                 }\r
628             }\r
629             return 0;\r
630         }\r
631         private int cacluateWidth(int height)\r
632         {\r
633             int aw = 0;\r
634             int ah = 0;\r
635             if (selectedTitle.AspectRatio.ToString() == "1.78")\r
636             {\r
637                 aw = 16;\r
638                 ah = 9;\r
639             }\r
640             else if (selectedTitle.AspectRatio.ToString() == "1.33")\r
641             {\r
642                 aw = 4;\r
643                 ah = 3;\r
644             }\r
645 \r
646             if (aw != 0)\r
647             {\r
648 \r
649                 double crop_width = selectedTitle.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
650                 double crop_height = selectedTitle.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
651 \r
652                 double new_width = (height * selectedTitle.Resolution.Height * aw * crop_width) /\r
653                                     (selectedTitle.Resolution.Width * ah * crop_height);\r
654 \r
655                 if (drp_anamorphic.SelectedIndex == 3)\r
656                     new_width = getModulusAuto(int.Parse(drop_modulus.SelectedItem.ToString()), new_width);\r
657                 else\r
658                     new_width = getModulusAuto(16, new_width);\r
659 \r
660                 //16 * (421 / 16)\r
661                 //double z = ( 16 * (( y + 8 ) / 16 ) );\r
662                 int x = int.Parse(new_width.ToString());\r
663                 return x;\r
664             }\r
665             return 0;\r
666         }\r
667 \r
668         // Calculate Resolution for Anamorphic functions\r
669         private string strictAnamorphic()\r
670         {\r
671             // TODO Make sure cropping is Mod2\r
672             if (selectedTitle != null)\r
673             {\r
674                 // Calculate the Actual Height\r
675                 int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value; ;\r
676                 int actualHeight = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
677 \r
678                 // Calculate Actual Width\r
679                 double displayWidth = ((double)actualWidth * selectedTitle.ParVal.Width / selectedTitle.ParVal.Height);\r
680                 return Math.Round(displayWidth, 0) + "x" + actualHeight;\r
681             }\r
682             return "Select a Title";\r
683         }\r
684         private string looseAnamorphic()\r
685         {\r
686             if (selectedTitle != null)\r
687             {\r
688                 // Get some values\r
689                 int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
690 \r
691                 int source_display_width = selectedTitle.Resolution.Width * selectedTitle.ParVal.Width / selectedTitle.ParVal.Height;\r
692                 int source_cropped_height = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
693 \r
694                 // Calculate storage Aspect and cache it for reuse\r
695                 if (storageAspect == 0)\r
696                     storageAspect = (double)actualWidth / source_cropped_height;               \r
697 \r
698                 // Calculate the new height based on the input cropped width\r
699                 double hcalc = (actualWidth / storageAspect) + 0.5;\r
700                 double newHeight = getModulusAuto(16, hcalc);\r
701                 looseAnamorphicHeightGuard = true;\r
702 \r
703                 if (newHeight < 64)\r
704                     newHeight = 64;\r
705                 text_height.Value = (decimal)newHeight;   // BUG Out of Range Exception with Width too low here.\r
706 \r
707                 // Calculate the anamorphic width\r
708                 double parW = newHeight * source_display_width / source_cropped_height;\r
709                 double parH = actualWidth;\r
710                 double displayWidth = (actualWidth * parW / parH);\r
711 \r
712                 // Now correct DisplayWidth to maintain Aspect ratio.  ActualHeight was mod16'd and thus AR is slightly different than the worked out displayWidths\r
713                 return Math.Truncate(displayWidth) + "x" + newHeight;  \r
714             }\r
715             return "Select a Title";\r
716 \r
717         }\r
718         \r
719         // GUI\r
720         private void disableCustomAnaControls()\r
721         {\r
722             // Disable Custom Anamorphic Stuff\r
723             lbl_modulus.Visible = false;\r
724             lbl_displayWidth.Visible = false;\r
725             lbl_parWidth.Visible = false;\r
726             lbl_parHeight.Visible = false;\r
727             drop_modulus.Visible = false;\r
728             txt_displayWidth.Visible = false;\r
729             txt_parWidth.Visible = false;\r
730             txt_parHeight.Visible = false;\r
731             check_KeepAR.Enabled = false;\r
732         }\r
733         private void enableCustomAnaControls()\r
734         {\r
735             // Disable Custom Anamorphic Stuff\r
736             lbl_modulus.Visible = true;\r
737             lbl_displayWidth.Visible = true;\r
738             lbl_parWidth.Visible = true;\r
739             lbl_parHeight.Visible = true;\r
740             drop_modulus.Visible = true;\r
741             txt_displayWidth.Visible = true;\r
742             txt_parWidth.Visible = true;\r
743             txt_parHeight.Visible = true;\r
744             check_KeepAR.Enabled = true;\r
745         }\r
746 \r
747     }\r
748 }