OSDN Git Service

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