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