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                     check_KeepAR.CheckState = CheckState.Checked;\r
197                     anamorphicWidgetContoller(0);\r
198                     if (selectedTitle != null)\r
199                     {\r
200                         text_width.Value = maxWidth != 0 ? maxWidth : selectedTitle.Resolution.Width;\r
201                         text_height.Value = maxHeight != 0 ? maxHeight : selectedTitle.Resolution.Height;\r
202                         setMax();\r
203                     }\r
204                     lbl_anamorphic.Text = "";\r
205                     break;\r
206                 case 1: // Strict\r
207                     if (selectedTitle != null)\r
208                     {\r
209                         heightModJumpGaurd = true;\r
210                         text_width.Value = selectedTitle.Resolution.Width - (int)crop_left.Value -\r
211                                            (int)crop_right.Value;\r
212                         text_height.Value = selectedTitle.Resolution.Height - (int)crop_top.Value -\r
213                                             (int)crop_bottom.Value;\r
214                     }\r
215                     anamorphicWidgetContoller(1);\r
216                     check_KeepAR.CheckState = CheckState.Unchecked;\r
217                     lbl_anamorphic.Text = strictAnamorphic();\r
218                     break;\r
219                 case 2: // Loose\r
220                     anamorphicWidgetContoller(2);\r
221                     storageAspect = 0;\r
222                     if (selectedTitle != null)\r
223                     {\r
224                         heightModJumpGaurd = true;\r
225                         text_width.Value = selectedTitle.Resolution.Width;\r
226                         text_height.Value = selectedTitle.Resolution.Height - (int)crop_top.Value -\r
227                                             (int)crop_bottom.Value;\r
228                     }\r
229                     lbl_anamorphic.Text = looseAnamorphic();\r
230                     break;\r
231                 case 3: // Custom\r
232                     anamorphicWidgetContoller(3);  // Display Elements\r
233 \r
234                     // Actual Work  \r
235                     if (selectedTitle != null)\r
236                     {\r
237                         heightModJumpGaurd = true;\r
238                         widthVal = selectedTitle.Resolution.Width;\r
239                         text_width.Value = selectedTitle.Resolution.Width - (int)crop_left.Value -\r
240                                            (int)crop_right.Value;\r
241                         text_height.Value = selectedTitle.Resolution.Height - (int)crop_top.Value -\r
242                                             (int)crop_bottom.Value;\r
243                         txt_parWidth.Text = selectedTitle.ParVal.Width.ToString();\r
244                         txt_parHeight.Text = selectedTitle.ParVal.Height.ToString();\r
245                         txt_displayWidth.Text = displayWidth().ToString(Culture);\r
246                     }\r
247 \r
248                     darValue = calculateDar();\r
249                     check_KeepAR.CheckState = CheckState.Checked;\r
250                     break;\r
251             }\r
252 \r
253             // A Picture Setting has changed so raise a PictureSettingsChanged event.\r
254             if (this.PictureSettingsChanged != null)\r
255                 this.PictureSettingsChanged(this, new EventArgs());\r
256         }\r
257 \r
258         // Custom Anamorphic Controls\r
259         private void txt_displayWidth_Keyup(object sender, KeyEventArgs e)\r
260         {\r
261             if (e.KeyCode == Keys.Enter)\r
262                 customAnamorphic(txt_displayWidth);\r
263         }\r
264         private void txt_parHeight_Keyup(object sender, KeyEventArgs e)\r
265         {\r
266             if (e.KeyCode == Keys.Enter)\r
267                 customAnamorphic(txt_parHeight);\r
268         }\r
269         private void txt_parWidth_Keyup(object sender, KeyEventArgs e)\r
270         {\r
271             if (e.KeyCode == Keys.Enter)\r
272                 customAnamorphic(txt_parWidth);\r
273         }\r
274 \r
275         // Cropping Controls\r
276         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
277         {\r
278             crop_left.Enabled = false;\r
279             crop_right.Enabled = false;\r
280             crop_top.Enabled = false;\r
281             crop_bottom.Enabled = false;\r
282         }\r
283         private void check_customCrop_CheckedChanged(object sender, EventArgs e)\r
284         {\r
285             crop_left.Enabled = true;\r
286             crop_right.Enabled = true;\r
287             crop_top.Enabled = true;\r
288             crop_bottom.Enabled = true;\r
289             if (selectedTitle != null)\r
290             {\r
291                 crop_top.Value = selectedTitle.AutoCropDimensions[0];\r
292                 crop_bottom.Value = selectedTitle.AutoCropDimensions[1];\r
293                 crop_left.Value = selectedTitle.AutoCropDimensions[2];\r
294                 crop_right.Value = selectedTitle.AutoCropDimensions[3];\r
295             }\r
296             else\r
297             {\r
298                 crop_left.Value = 0;\r
299                 crop_right.Value = 0;\r
300                 crop_top.Value = 0;\r
301                 crop_bottom.Value = 0;\r
302             }\r
303         }\r
304         private void crop_left_ValueChanged(object sender, EventArgs e)\r
305         {\r
306             if (crop_left.Value % 2 != 0)\r
307                 crop_left.Value++;\r
308         }\r
309         private void crop_right_ValueChanged(object sender, EventArgs e)\r
310         {\r
311             if (crop_right.Value % 2 != 0)\r
312                 crop_right.Value++;\r
313         }\r
314         private void crop_top_ValueChanged(object sender, EventArgs e)\r
315         {\r
316             if (crop_top.Value % 2 != 0)\r
317                 crop_top.Value++;\r
318         }\r
319         private void crop_bottom_ValueChanged(object sender, EventArgs e)\r
320         {\r
321             if (crop_bottom.Value % 2 != 0)\r
322                 crop_bottom.Value++;\r
323         }\r
324 \r
325         // Custom Anamorphic Code\r
326         private void customAnamorphic(Control control)\r
327         {\r
328             // Get and parse all the required values\r
329             int cropLeft = (int)crop_left.Value;\r
330             int cropRight = (int)crop_right.Value;\r
331 \r
332             int width = (int)text_width.Value;\r
333             int cropped_width = width - cropLeft - cropRight;\r
334 \r
335             int mod = 16;\r
336             int.TryParse(drop_modulus.SelectedItem.ToString(), out mod);\r
337 \r
338             int parW, parH;\r
339             double displayWidth;\r
340             int.TryParse(txt_parWidth.Text, out parW);\r
341             int.TryParse(txt_parHeight.Text, out parH);\r
342             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
343 \r
344             /* NOT KEEPING DISPLAY ASPECT\r
345              * Changing STORAGE WIDTH changes DISPLAY WIDTH to STORAGE WIDTH * PIXEL WIDTH / PIXEL HEIGHT\r
346              * Changing PIXEL dimensions changes DISPLAY WIDTH to STORAGE WIDTH * PIXEL WIDTH / PIXEL HEIGHT\r
347              * Changing DISPLAY WIDTH changes PIXEL WIDTH to DISPLAY WIDTH and PIXEL HEIGHT to STORAGE WIDTH\r
348              * Changing HEIGHT just....changes the height.\r
349              */\r
350             if (!check_KeepAR.Checked)\r
351             {\r
352                 switch (control.Name)\r
353                 {\r
354                     case "text_width":\r
355                         double dw = (double)cropped_width * parW / parH;\r
356                         dw = Math.Round(dw, 2);\r
357                         txt_displayWidth.Text = dw.ToString(Culture);\r
358                         break;\r
359                     case "txt_parWidth":\r
360                         double dwpw = (double)cropped_width * parW / parH;\r
361                         dwpw = Math.Round(dwpw, 2);\r
362                         txt_displayWidth.Text = dwpw.ToString(Culture);\r
363                         break;\r
364                     case "txt_parHeight":\r
365                         double dwph = (double)cropped_width * parW / parH;\r
366                         dwph = Math.Round(dwph, 2);\r
367                         txt_displayWidth.Text = dwph.ToString(Culture);\r
368                         break;\r
369                     case "txt_displayWidth":\r
370                         txt_parWidth.Text = Math.Round(displayWidth, 0).ToString();\r
371                         txt_parHeight.Text = text_width.Text;\r
372                         break;\r
373                 }\r
374             }\r
375 \r
376             /*\r
377              * KEEPING DISPLAY ASPECT RATIO\r
378              * DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
379              * Disable editing: PIXEL WIDTH, PIXEL HEIGHT\r
380              * Changing DISPLAY WIDTH:\r
381              *     Changes HEIGHT to keep DAR\r
382              *     Changes PIXEL WIDTH to new DISPLAY WIDTH\r
383              *     Changes PIXEL HEIGHT to STORAGE WIDTH\r
384              * Changing HEIGHT\r
385              *     Changes DISPLAY WIDTH to keep DAR\r
386              *     Changes PIXEL WIDTH to new DISPLAY WIDTH\r
387              *     Changes PIXEL HEIGHT to STORAGE WIDTH\r
388              * Changing STORAGE_WIDTH:\r
389              *     Changes PIXEL WIDTH to DISPLAY WIDTH\r
390              *     Changes PIXEL HEIGHT to new STORAGE WIDTH \r
391              */\r
392 \r
393             if (check_KeepAR.Checked)\r
394             {\r
395                 switch (control.Name)\r
396                 {\r
397                     case "txt_displayWidth":\r
398                         heightChangeGuard = true;\r
399                         text_height.Value = (decimal)getHeightKeepDar();  //Changes HEIGHT to keep DAR\r
400                         //darValue = calculateDar(); // Cache the dar value\r
401                         txt_parWidth.Text = txt_displayWidth.Text;\r
402                         txt_parHeight.Text = cropped_width.ToString();\r
403                         break;\r
404                     case "text_height":\r
405                         heightChangeGuard = true;\r
406                         txt_displayWidth.Text = getDisplayWidthKeepDar().ToString(Culture);  //Changes DISPLAY WIDTH to keep DAR\r
407                         txt_parWidth.Text = txt_displayWidth.Text;\r
408                         txt_parHeight.Text = cropped_width.ToString();\r
409                         break;\r
410                     case "text_width":\r
411                         txt_parWidth.Text = txt_displayWidth.Text;\r
412                         txt_parHeight.Text = cropped_width.ToString();\r
413                         break;\r
414                 }\r
415             }\r
416         }\r
417         private double getDisplayWidthKeepDar()\r
418         {\r
419             double displayWidth;\r
420             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
421             double currentDar = calculateDar();\r
422             double newDwValue = displayWidth;\r
423 \r
424             // Correct display width up or down to correct for dar.           \r
425             if (currentDar > darValue)\r
426             {\r
427                 while (currentDar > darValue)\r
428                 {\r
429                     displayWidth--;\r
430                     newDwValue = displayWidth;\r
431                     currentDar = calculateDarByVal(text_height.Value, displayWidth);\r
432                 }\r
433             }\r
434             else\r
435             {\r
436                 while (currentDar < darValue)\r
437                 {\r
438                     displayWidth++;\r
439                     newDwValue = displayWidth;\r
440                     currentDar = calculateDarByVal(text_height.Value, displayWidth);\r
441                 }\r
442             }\r
443 \r
444             return Math.Round(newDwValue, 2);\r
445         }\r
446         private double getHeightKeepDar()\r
447         {\r
448             double displayWidth;\r
449             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
450             double currentDar = calculateDar();\r
451             double newHeightVal = heightVal;\r
452 \r
453             // Correct display width up or down.\r
454             if (currentDar > darValue)\r
455             {\r
456                 while (currentDar > darValue)\r
457                 {\r
458                     heightVal++;\r
459                     newHeightVal = heightVal;\r
460                     currentDar = calculateDarByVal(heightVal, displayWidth);\r
461                 }\r
462             }\r
463             else\r
464             {\r
465                 while (currentDar < darValue)\r
466                 {\r
467                     heightVal--;\r
468                     newHeightVal = heightVal;\r
469                     currentDar = calculateDarByVal(heightVal, displayWidth);\r
470                 }\r
471             }\r
472 \r
473             return newHeightVal;\r
474         }\r
475         private double calculateDar()\r
476         {\r
477             // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
478             double displayWidth;\r
479             double.TryParse(txt_displayWidth.Text, out displayWidth);\r
480 \r
481             double calculatedDar = displayWidth / (int)text_height.Value;\r
482 \r
483             return calculatedDar;\r
484         }\r
485         private double calculateDarByVal(decimal croppedHeight, double displayWidth)\r
486         {\r
487             // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
488             double calculatedDar = darValue;\r
489             if (croppedHeight > 0)\r
490                 calculatedDar = displayWidth / (double)croppedHeight;\r
491 \r
492             return calculatedDar;\r
493         }\r
494         private int displayWidth()\r
495         {\r
496             if (selectedTitle != null)\r
497             {\r
498                 int actualWidth = (int)text_width.Value;\r
499                 int displayWidth = 0;\r
500                 int parW, parH;\r
501 \r
502                 int.TryParse(txt_parWidth.Text, out parW);\r
503                 int.TryParse(txt_parHeight.Text, out parH);\r
504 \r
505                 if (drp_anamorphic.SelectedIndex != 3)\r
506                     displayWidth = (actualWidth * selectedTitle.ParVal.Width / selectedTitle.ParVal.Height);\r
507                 else if (parW > 0 && parH > 0)\r
508                     displayWidth = (actualWidth * parW / parH);\r
509 \r
510                 return displayWidth;\r
511             }\r
512             return -1;\r
513         }\r
514 \r
515         // Resolution calculation and controls\r
516         private decimal widthChangeMod(int mod)\r
517         {\r
518             // Increase or decrease the height based on the users input.\r
519             decimal returnVal = text_width.Value > widthVal ? getResolutionJump(mod, text_width.Value, true) : getResolutionJump(mod, text_width.Value, false);\r
520 \r
521             // Make sure we don't go above source value\r
522             if (selectedTitle != null)\r
523                 if (selectedTitle.Resolution.Width < returnVal)\r
524                     returnVal = selectedTitle.Resolution.Width;\r
525 \r
526             /*if (returnVal < 64)\r
527                 returnVal = 64; */\r
528 \r
529             // Set the global tracker\r
530             widthVal = (int)returnVal;\r
531 \r
532             return returnVal;\r
533         }\r
534         private decimal heightChangeMod(int mod)\r
535         {\r
536             // Increase or decrease the height based on the users input.\r
537             decimal returnVal = text_height.Value > heightVal ? getResolutionJump(mod, text_height.Value, true) : getResolutionJump(mod, text_height.Value, false);\r
538 \r
539             // Make sure we don't go above source value\r
540             if (selectedTitle != null)\r
541                 if (selectedTitle.Resolution.Height < returnVal)\r
542                     returnVal = selectedTitle.Resolution.Height;\r
543 \r
544             /*if (returnVal < 64)\r
545                 returnVal = 64;*/\r
546 \r
547             // Set the global tracker\r
548             heightVal = (int)returnVal;\r
549 \r
550             return returnVal;\r
551         }\r
552         private decimal calculateUnchangeValue(Boolean widthChangeFromControl)\r
553         {\r
554             decimal newValue = -1;\r
555             if (selectedTitle != null && drp_anamorphic.SelectedIndex != 3 && drp_anamorphic.SelectedIndex != 2)\r
556                 if (widthChangeFromControl)\r
557                     newValue = cacluateHeight(widthVal);\r
558                 else\r
559                 {\r
560                     if (check_KeepAR.Checked)\r
561                         newValue = cacluateWidth(heightVal);\r
562                 }\r
563 \r
564             return newValue;\r
565         }\r
566         private static int getResolutionJump(int mod, decimal value, Boolean up)\r
567         {\r
568             if (up)\r
569                 while ((value % mod) != 0)\r
570                     value++;\r
571             else\r
572                 while ((value % mod) != 0)\r
573                     value--;\r
574 \r
575             return (int)value;\r
576         }\r
577         private static double getModulusAuto(int mod, double value)\r
578         {\r
579             int modDiv2 = mod / 2;\r
580 \r
581             if ((value % mod) != 0)\r
582             {\r
583                 double modVal = (int)value % mod;\r
584                 if (modVal >= modDiv2)\r
585                 {\r
586                     modVal = 16 - modVal;\r
587                     value = (int)value + (int)modVal;\r
588                 }\r
589                 else\r
590                 {\r
591                     value = (int)value - (int)modVal;\r
592                 }\r
593             }\r
594             return value;\r
595         }\r
596         private int cacluateHeight(int width)\r
597         {\r
598             if (selectedTitle != null)\r
599             {\r
600                 int aw = 0;\r
601                 int ah = 0;\r
602                 if (selectedTitle.AspectRatio == 1.78F)\r
603                 {\r
604                     aw = 16;\r
605                     ah = 9;\r
606                 }\r
607                 if (selectedTitle.AspectRatio == 1.33F)\r
608                 {\r
609                     aw = 4;\r
610                     ah = 3;\r
611                 }\r
612 \r
613                 if (aw != 0)\r
614                 {\r
615                     // Crop_Width = Title->Width - crop_Left - crop_right\r
616                     // Crop_Height = Title->Height - crop_top - crop_bottom\r
617                     double crop_width = selectedTitle.Resolution.Width - (double)crop_left.Value -\r
618                                         (double)crop_right.Value;\r
619                     double crop_height = selectedTitle.Resolution.Height - (double)crop_top.Value -\r
620                                          (double)crop_bottom.Value;\r
621 \r
622                     double new_height = (width * selectedTitle.Resolution.Width * ah * crop_height) /\r
623                                         (selectedTitle.Resolution.Height * aw * crop_width);\r
624 \r
625                     new_height = drp_anamorphic.SelectedIndex == 3 ? getModulusAuto(int.Parse(drop_modulus.SelectedItem.ToString()), new_height) : getModulusAuto(16, new_height);\r
626 \r
627                     int x = int.Parse(new_height.ToString());\r
628                     /*if (x < 64)\r
629                         x = 64; */\r
630                     return x;\r
631                 }\r
632             }\r
633             return 0;\r
634         }\r
635         private int cacluateWidth(int height)\r
636         {\r
637             int aw = 0;\r
638             int ah = 0;\r
639             if (selectedTitle.AspectRatio == 1.78F)\r
640             {\r
641                 aw = 16;\r
642                 ah = 9;\r
643             }\r
644             if (selectedTitle.AspectRatio == 1.33F)\r
645             {\r
646                 aw = 4;\r
647                 ah = 3;\r
648             }\r
649 \r
650             if (aw != 0)\r
651             {\r
652 \r
653                 double crop_width = selectedTitle.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
654                 double crop_height = selectedTitle.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
655 \r
656                 double new_width = (height * selectedTitle.Resolution.Height * aw * crop_width) /\r
657                                     (selectedTitle.Resolution.Width * ah * crop_height);\r
658 \r
659                 if (drp_anamorphic.SelectedIndex == 3)\r
660                     new_width = getModulusAuto(int.Parse(drop_modulus.SelectedItem.ToString()), new_width);\r
661                 else\r
662                     new_width = getModulusAuto(16, new_width);\r
663 \r
664 \r
665                 int x = int.Parse(new_width.ToString());\r
666 \r
667                 if (x < 64)\r
668                     x = 64;\r
669 \r
670                 return x;\r
671             }\r
672             return 64;\r
673         }\r
674 \r
675         // Calculate Resolution for Anamorphic functions\r
676         private string strictAnamorphic()\r
677         {\r
678             if (selectedTitle != null)\r
679             {\r
680                 // Calculate the Actual Height\r
681                 int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value; ;\r
682                 int actualHeight = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
683 \r
684                 // Calculate Actual Width\r
685                 double displayWidth = ((double)actualWidth * selectedTitle.ParVal.Width / selectedTitle.ParVal.Height);\r
686                 return Math.Round(displayWidth, 0) + "x" + actualHeight;\r
687             }\r
688             return "Select a Title";\r
689         }\r
690         private string looseAnamorphic()\r
691         {\r
692             if (selectedTitle != null)\r
693             {\r
694                 // Get some values\r
695                 int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
696 \r
697                 int source_display_width = selectedTitle.Resolution.Width * selectedTitle.ParVal.Width / selectedTitle.ParVal.Height;\r
698                 int source_cropped_height = selectedTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
699 \r
700                 // Calculate storage Aspect and cache it for reuse\r
701                 if (storageAspect == 0)\r
702                     storageAspect = (double)actualWidth / source_cropped_height;\r
703 \r
704                 // Calculate the new height based on the input cropped width\r
705                 double hcalc = (actualWidth / storageAspect) + 0.5;\r
706                 double newHeight = getModulusAuto(16, hcalc);\r
707                 looseAnamorphicHeightGuard = true;\r
708 \r
709                 if (newHeight < 64)\r
710                     newHeight = 64;\r
711                 text_height.Value = (decimal)newHeight; \r
712 \r
713                 // Calculate the anamorphic width\r
714                 double parW = newHeight * source_display_width / source_cropped_height;\r
715                 double parH = actualWidth;\r
716                 double displayWidth = (actualWidth * parW / parH);\r
717 \r
718                 // Now correct DisplayWidth to maintain Aspect ratio.  ActualHeight was mod16'd and thus AR is slightly different than the worked out displayWidths\r
719                 return Math.Truncate(displayWidth) + "x" + newHeight;\r
720             }\r
721             return "Select a Title";\r
722         }\r
723 \r
724         // Function to control the enable / disable property of the display controls.\r
725         private void anamorphicWidgetContoller(int mode)\r
726         {\r
727 \r
728             switch (mode)\r
729             {\r
730                 case 0: // None\r
731                     text_height.Enabled = true;\r
732                     text_width.Enabled = true;\r
733                     check_KeepAR.Enabled = true;\r
734                     lbl_anamprohicLbl.Visible = false;\r
735                     break;\r
736                 case 1: // Strict\r
737                     text_height.Enabled = false;\r
738                     text_width.Enabled = false;\r
739                     check_KeepAR.Enabled = false;\r
740                     lbl_anamprohicLbl.Visible = true;\r
741                     break;\r
742                 case 2: // Loose\r
743                     text_height.Enabled = false;\r
744                     text_width.Enabled = true;\r
745                     check_KeepAR.Enabled = false;\r
746                     lbl_anamprohicLbl.Visible = true;\r
747                     break;\r
748                 case 3: // Custom\r
749                     text_height.Enabled = true;\r
750                     text_width.Enabled = true;\r
751                     check_KeepAR.Enabled = true;\r
752                     // Labels\r
753                     lbl_anamprohicLbl.Visible = true;\r
754                     lbl_modulus.Visible = true;\r
755                     lbl_displayWidth.Visible = true;\r
756                     lbl_parWidth.Visible = true;\r
757                     lbl_parHeight.Visible = true;\r
758                     // Controls\r
759                     drop_modulus.Visible = true;\r
760                     txt_displayWidth.Visible = true;\r
761                     txt_parWidth.Visible = true;\r
762                     txt_parHeight.Visible = true;\r
763                     break;\r
764             }\r
765 \r
766             // Disable All the custom anamorphic controls if not required.\r
767             if (mode != 3)\r
768             {\r
769                 // Labels\r
770                 lbl_modulus.Visible = false;\r
771                 lbl_displayWidth.Visible = false;\r
772                 lbl_parWidth.Visible = false;\r
773                 lbl_parHeight.Visible = false;\r
774 \r
775                 // Controls\r
776                 drop_modulus.Visible = false;\r
777                 txt_displayWidth.Visible = false;\r
778                 txt_parWidth.Visible = false;\r
779                 txt_parHeight.Visible = false;\r
780 \r
781             }\r
782         }\r
783 \r
784     }\r
785 }