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