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