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