OSDN Git Service

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