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