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