OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Controls / PictureSettings.cs
index 096ccad..d3196ae 100644 (file)
@@ -1,6 +1,7 @@
 using System;\r
 using System.ComponentModel;\r
 using System.Drawing;\r
+using System.Globalization;\r
 using System.Windows.Forms;\r
 using Handbrake.Parsing;\r
 \r
@@ -8,13 +9,13 @@ namespace Handbrake.Controls
 {\r
     public partial class PictureSettings : UserControl\r
     {\r
-        private bool _preventChangingWidth, _preventChangingHeight;\r
-        private int _maxWidth, _maxHeight, _lastEncodeWidth, _lastEncodeHeight;\r
-        private double _anamorphicRatio, _displayRatio;\r
-        private Title _title;\r
-        private double storageAspect;\r
+        private readonly CultureInfo Culture = new CultureInfo("en-US", false);\r
         public event EventHandler PictureSettingsChanged;\r
 \r
+        private Boolean preventChangingWidth, preventChangingHeight;\r
+        private int _PresetMaximumWidth, _PresetMaximumHeight;\r
+        private Title _SourceTitle;\r
+\r
         public PictureSettings()\r
         {\r
             InitializeComponent();\r
@@ -30,356 +31,161 @@ namespace Handbrake.Controls
         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
         public Title Source\r
         {\r
-            get { return _title; }\r
+            get { return _SourceTitle; }\r
             set\r
             {\r
-                _title = value;\r
-                _displayRatio = ((double)_title.Resolution.Width * _title.ParVal.Width / _title.ParVal.Height) / _title.Resolution.Height;\r
-\r
-                Enabled = _title != null;\r
-\r
-                MaximumWidth = _title.Resolution.Width;\r
-                MaximumHeight = _title.Resolution.Height;\r
-\r
-                updownParWidth.Value = _title.ParVal.Width;\r
-                updownParHeight.Value = _title.ParVal.Height;\r
-\r
-                // Set the source resolution\r
-                lbl_src_res.Text = Source.Resolution.Width + " x " + Source.Resolution.Height;\r
-\r
-                // Set ratios\r
-                _anamorphicRatio = (double)Source.Resolution.Width / Source.Resolution.Height;\r
-\r
-                // Set the encode width and height\r
-                EncodeWidth = _title.Resolution.Width;\r
-                EncodeHeight = _title.Resolution.Height;\r
-\r
-                _lastEncodeWidth = _title.Resolution.Width;\r
-                _lastEncodeHeight = _title.Resolution.Height;\r
-\r
-                // Set cropping\r
-                CroppingValues = _title.AutoCropDimensions;\r
-\r
-                lbl_Aspect.Text = _title.AspectRatio.ToString();\r
-\r
-                UpdateAnamorphicValue();\r
-            }\r
-        }\r
+                _SourceTitle = value;\r
+                Enabled = _SourceTitle != null;\r
 \r
-        /// <summary>\r
-        /// Gets or sets the resolution of the displayed video.\r
-        /// </summary>\r
-        public Size DisplayResolution { get; set; }\r
+                // Set the Aspect Ratio\r
+                lbl_Aspect.Text = _SourceTitle.AspectRatio.ToString(Culture);\r
+                lbl_src_res.Text = _SourceTitle.Resolution.Width + " x " + _SourceTitle.Resolution.Height;\r
 \r
-        public int EncodeWidth { get { return (int)text_width.Value; } set { text_width.Value = value; } }\r
+                // Set the Recommended Cropping values\r
+                crop_top.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[0]);\r
+                crop_bottom.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[1]);\r
+                crop_left.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[2]);\r
+                crop_right.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[3]);\r
 \r
-        public int EncodeHeight { get { return (int)text_height.Value; } set { text_height.Value = value; } }\r
+                // Set the Resolution Boxes\r
+                text_width.Value = _SourceTitle.Resolution.Width;\r
 \r
-        public int[] CroppingValues\r
-        {\r
-            get\r
-            {\r
-                return new int[4]\r
+                if (drp_anamorphic.SelectedIndex == 0)\r
                 {\r
-                    (int)crop_top.Value,\r
-                    (int)crop_bottom.Value,\r
-                    (int)crop_left.Value,\r
-                    (int)crop_right.Value\r
-                };\r
-            }\r
-            set\r
-            {\r
-                if (value.Length != 4)\r
+                    int width = _SourceTitle.Resolution.Width;\r
+\r
+                    double crop_width = _SourceTitle.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
+                    double crop_height = _SourceTitle.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
+                    double newHeight = (width * _SourceTitle.Resolution.Width * sourceAspect.Height * crop_height) / (_SourceTitle.Resolution.Height * sourceAspect.Width * crop_width);\r
+                    text_height.Value = (decimal)GetModulusValue(newHeight);\r
+                }\r
+                else\r
                 {\r
-                    throw new ArgumentException("The cropping values given must have a length of 4.");\r
+                    text_height.Value = _SourceTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
+                    labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
                 }\r
 \r
-                crop_top.Value = value[0];\r
-                crop_bottom.Value = value[1];\r
-                crop_left.Value = value[2];\r
-                crop_right.Value = value[3];\r
-            }\r
-        }\r
+                updownParWidth.Value = _SourceTitle.ParVal.Width;\r
+                updownParHeight.Value = _SourceTitle.ParVal.Height;\r
+                updownDisplayWidth.Value = calculateAnamorphicSizes().Width;\r
 \r
-        /// <summary>\r
-        /// Gets or sets the maximum allowable width of the encoded video.\r
-        /// </summary>\r
-        public int MaximumWidth\r
-        {\r
-            get { return _maxWidth; }\r
-            set\r
-            {\r
-                _maxWidth = value > 0 ? value : (Source != null ? Source.Resolution.Width : 2560);\r
             }\r
         }\r
 \r
         /// <summary>\r
-        /// Gets or sets the maximum allowable height of the encoded video.\r
+        /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to\r
+        /// "0" if the maximum does not matter.\r
         /// </summary>\r
-        public int MaximumHeight\r
+        [Browsable(false)]\r
+        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
+        public Size PresetMaximumResolution\r
         {\r
-            get { return _maxHeight; }\r
+            get { return new Size(_PresetMaximumWidth, _PresetMaximumHeight); }\r
             set\r
             {\r
-                _maxHeight = value > 0 ? value : (Source != null ? Source.Resolution.Height : 2560);\r
+                _PresetMaximumWidth = value.Width;\r
+                _PresetMaximumHeight = value.Height;\r
+\r
+                if (value.Width != 0 && value.Height != 0)\r
+                    lbl_max.Text = "Max Width / Height";\r
+                else if (value.Width != 0)\r
+                    lbl_max.Text = "Max Width";\r
+                else if (value.Height != 0)\r
+                    lbl_max.Text = "Max Height";\r
+                else\r
+                    lbl_max.Text = "";\r
             }\r
-        }      \r
-\r
-        public void setMax()\r
-        {\r
-\r
         }\r
 \r
-        /// <summary>\r
-        /// Updates the anamorphic value shown as the display width.\r
-        /// </summary>\r
-        private void UpdateAnamorphicValue()\r
+        // Picture Controls\r
+        private void text_width_ValueChanged(object sender, EventArgs e)\r
         {\r
-            if (_title == null || _title.ParVal.IsEmpty)\r
+            if (preventChangingWidth)\r
                 return;\r
 \r
-            // Set globally useful values\r
-            double width;\r
-            double par;\r
+            // Make sure the new value doesn't exceed the maximum\r
+            if (Source != null)\r
+                if (text_width.Value > Source.Resolution.Width)\r
+                    text_width.Value = Source.Resolution.Width;\r
 \r
             switch (drp_anamorphic.SelectedIndex)\r
             {\r
-                case 2:\r
-                    int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
-                    int source_display_width = Source.Resolution.Width * Source.ParVal.Width / Source.ParVal.Height;\r
-                    int source_cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
-                    par = ((double) text_height.Value*source_display_width/source_cropped_height)/actualWidth;\r
-                    width = (actualWidth * par);\r
-                    width = Math.Truncate(width);\r
-                    break;\r
-                default:\r
+                case 0:\r
+                    if (check_KeepAR.Checked && Source != null)\r
                     {\r
-                        if (drp_anamorphic.SelectedIndex == 1) // Strict\r
-                            par = (double)Source.ParVal.Width / Source.ParVal.Height;\r
-                        else // Custom\r
-                            par = (double)updownParWidth.Value / (double)updownParHeight.Value;\r
+                        preventChangingHeight = true;\r
+\r
+                        int width = (int)text_width.Value;\r
 \r
-                        // Store the latest DAR\r
-                        double rawWidth = (double)text_width.Value * par;\r
-                        _displayRatio = rawWidth / (double)text_height.Value;\r
+                        double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
+                        double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
+                        double newHeight = (width * Source.Resolution.Width * sourceAspect.Height * crop_height) /\r
+                                           (Source.Resolution.Height * sourceAspect.Width * crop_width);\r
+                        text_height.Value = (decimal)GetModulusValue(newHeight);\r
 \r
-                        width = (int)Math.Round(rawWidth);\r
-                        break;\r
+                        preventChangingHeight = false;\r
                     }\r
+                    break;\r
+                default:\r
+                    labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
+                    break;\r
             }\r
 \r
-            labelDisplaySize.Text = width + " x " + text_height.Value;\r
-\r
-            updownDisplayWidth.Value = (decimal)width;\r
-            updownParWidth.Value = (decimal)width;\r
-            updownParHeight.Value = text_width.Value;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Sets the visibility of the advanced anamorphic options.\r
-        /// </summary>\r
-        /// <param name="value">Whether or not the options should be visible.</param>\r
-        private void SetCustomAnamorphicOptionsVisible(bool visible)\r
-        {\r
-            lbl_modulus.Visible = visible;\r
-            lbl_displayWidth.Visible = visible;\r
-            lbl_parWidth.Visible = visible;\r
-            lbl_parHeight.Visible = visible;\r
-\r
-            drp_modulus.Visible = visible;\r
-            updownDisplayWidth.Visible = visible;\r
-            updownParWidth.Visible = visible;\r
-            updownParHeight.Visible = visible;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Gets the normalized value from one given by the user that is divisible by the number\r
-        /// set in <see cref="_modulus"/>.\r
-        /// </summary>\r
-        /// <param name="value">The value to normalize</param>\r
-        /// <returns>A number that is divisible by <see cref="_modulus"/>.</returns>\r
-        /// <remarks>\r
-        /// The way that some video codecs, such as x264, compress video is by creating "macroblocks" \r
-        /// that are seperated into defined squares, often 16x16 pixels. Because of this, if the width \r
-        /// and height of the encoded video are not each divisible by the modulus defined, video quality\r
-        /// will suffer. This method takes the supplied value and normalizes it to the nearest mutliple\r
-        /// of <see cref="_modulus"/>.\r
-        /// </remarks>\r
-        private int GetModulusValue(int value)\r
-        {\r
-            int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
-            int remainder = value % mod;\r
-\r
-            if (remainder == 0)\r
-                return value;\r
-\r
-            return remainder >= mod / 2 ? value + (mod - remainder) : value - remainder;\r
-        }\r
-\r
-        private void ApplyStrictAnamorphic()\r
-        {\r
-            if (_anamorphicRatio == 0)\r
-                return;\r
-\r
-            _preventChangingWidth = true;\r
-            _preventChangingHeight = true;\r
-\r
-            text_width.Value = _title.Resolution.Width;\r
-            text_height.Value = _title.Resolution.Height;\r
-\r
-            _preventChangingWidth = false;\r
-            _preventChangingHeight = false;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Loosely anamorphs encode width and height values.\r
-        /// </summary>\r
-        private void ApplyLooseAnamorphic()\r
-        {\r
-            // Prevents DivideByZeroExceptions\r
-            if (_anamorphicRatio == 0)\r
-                return;\r
-\r
-            int actualWidth = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
-            int source_cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
-\r
-            if (storageAspect == 0)\r
-                storageAspect = (double)actualWidth / source_cropped_height;\r
-            double hcalc = (actualWidth / storageAspect) + 0.5;\r
-            double newHeight = GetModulusValue((int)hcalc);\r
-\r
-            text_width.Value = GetModulusValue((int)text_width.Value);\r
-            text_height.Value = (decimal)newHeight;\r
-\r
-            UpdateAnamorphicValue();\r
-        }\r
-\r
-        /// <summary>\r
-        /// Anamorphs encode width and height based on the custom options specified.\r
-        /// </summary>\r
-        private void ApplyCustomAnamorphic(Control ctrlUpdated)\r
-        {\r
-            // Make sure the PAR values are set correctly\r
-            if (updownParWidth.Value == 0)\r
-                updownParWidth.Value = Source.ParVal.Width;\r
-            if (updownParHeight.Value == 0)\r
-                updownParHeight.Value = Source.ParVal.Height;\r
-\r
-            // Set various values\r
-            int parWidth = (int)updownParWidth.Value;\r
-            int parHeight = (int)updownParHeight.Value;\r
-\r
-            if (!check_KeepAR.Checked)\r
-            {\r
-                switch (ctrlUpdated.Name)\r
-                {\r
-                    case "text_width":\r
-                    case "updownParWidth":\r
-                    case "updownParHeight":\r
-                        updownDisplayWidth.Value = Math.Round(text_width.Value * parWidth / parHeight);\r
-                        break;\r
-                    case "updownDisplayWidth":\r
-                        updownParWidth.Value = updownDisplayWidth.Value;\r
-                        updownParHeight.Value = text_width.Value;\r
-                        break;\r
-                }\r
-            }\r
-            else\r
-            {\r
-                switch (ctrlUpdated.Name)\r
-                {\r
-                    case "updownDisplayWidth":\r
-                        _preventChangingHeight = true;\r
-\r
-                        text_height.Value = GetModulusValue((int)((double)updownDisplayWidth.Value / _displayRatio));\r
-\r
-                        _preventChangingHeight = false;\r
-                        goto case "text_width";\r
-                    case "text_height":\r
-                        updownDisplayWidth.Value = GetModulusValue((int)((double)text_width.Value * _anamorphicRatio * _displayRatio));\r
-                        goto case "text_width";\r
-                    case "text_width":\r
-                        updownParWidth.Value = updownDisplayWidth.Value;\r
-                        updownParHeight.Value = text_width.Value;\r
-                        break;\r
-                }\r
-            }\r
+            preventChangingWidth = false;\r
         }\r
-\r
-\r
-\r
-        private void text_width_ValueChanged(object sender, EventArgs e)\r
+        private void text_height_ValueChanged(object sender, EventArgs e)\r
         {\r
-            if (_preventChangingWidth)\r
+            if (preventChangingHeight)\r
                 return;\r
 \r
-            _preventChangingWidth = true;\r
-\r
-            if (text_width.Value > MaximumWidth)\r
-            {\r
-                text_width.Value = MaximumWidth;\r
-            }\r
+            if (Source != null)\r
+                if (text_height.Value > Source.Resolution.Height)\r
+                    text_height.Value = Source.Resolution.Height;\r
 \r
             switch (drp_anamorphic.SelectedIndex)\r
             {\r
                 case 0:\r
-                    if (check_KeepAR.Checked)\r
+                    if (check_KeepAR.Checked && Source != null)\r
                     {\r
-                        _preventChangingHeight = true;\r
+                        preventChangingWidth = true;\r
+\r
+                        double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
+                        double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
 \r
-                        decimal newHeight = text_width.Value / (decimal)_anamorphicRatio;\r
-                        text_height.Value = newHeight > MaximumHeight ? MaximumHeight : newHeight;\r
+                        double new_width = ((double)text_height.Value * Source.Resolution.Height * sourceAspect.Width * crop_width) /\r
+                                            (Source.Resolution.Width * sourceAspect.Height * crop_height);\r
 \r
-                        _preventChangingHeight = false;\r
+                        text_width.Value = (decimal)GetModulusValue(new_width);\r
+\r
+                        preventChangingWidth = false;\r
                     }\r
                     break;\r
-                case 1:\r
-                    ApplyStrictAnamorphic();\r
-                    break;\r
-                case 2:\r
-                    ApplyLooseAnamorphic();\r
-                    break;\r
-                case 3:\r
-                    ApplyCustomAnamorphic((Control)sender);\r
+                default:\r
+                    labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
                     break;\r
             }\r
 \r
-            _preventChangingWidth = false;\r
+            preventChangingHeight = false;\r
         }\r
-\r
-        private void text_height_ValueChanged(object sender, EventArgs e)\r
+        private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
         {\r
-            if (_preventChangingHeight)\r
-                return;\r
-\r
-            _preventChangingHeight = true;\r
-\r
-            if (text_height.Value > MaximumHeight)\r
-            {\r
-                text_height.Value = MaximumHeight;\r
-            }\r
+            //Force TextWidth to recalc height\r
+            if (check_KeepAR.Checked)\r
+                text_width_ValueChanged(this, new EventArgs());\r
 \r
-            switch (drp_anamorphic.SelectedIndex)\r
+            // Disable the Custom Anamorphic Par Controls if checked.\r
+            if (drp_anamorphic.SelectedIndex == 3)\r
             {\r
-                case 0:\r
-                    if (check_KeepAR.Checked)\r
-                    {\r
-                        _preventChangingWidth = true;\r
-\r
-                        decimal newWidth = text_height.Value * (decimal)_anamorphicRatio;\r
-                        text_width.Value = newWidth > MaximumWidth ? MaximumWidth : newWidth;\r
-\r
-                        _preventChangingWidth = false;\r
-                    }\r
-                    break;\r
-                case 3:\r
-                    ApplyCustomAnamorphic((Control)sender);\r
-                    break;\r
+                updownParWidth.Enabled = !check_KeepAR.Checked;\r
+                updownParHeight.Enabled = !check_KeepAR.Checked;\r
             }\r
 \r
-            _preventChangingHeight = false;\r
+            // Raise the Picture Settings Changed Event\r
+            if (PictureSettingsChanged != null)\r
+                PictureSettingsChanged(this, new EventArgs());\r
         }\r
 \r
+        // Anamorphic Controls\r
         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
             switch (drp_anamorphic.SelectedIndex)\r
@@ -389,20 +195,26 @@ namespace Handbrake.Controls
                     text_height.Enabled = true;\r
                     check_KeepAR.Enabled = true;\r
 \r
-                    SetCustomAnamorphicOptionsVisible(false);\r
+                    setCustomAnamorphicOptionsVisible(false);\r
                     labelStaticDisplaySize.Visible = false;\r
                     labelDisplaySize.Visible = false;\r
 \r
                     check_KeepAR.Checked = true;\r
-                    break;\r
+                    drp_modulus.SelectedIndex = 0;\r
+\r
+                    if (check_KeepAR.Checked)\r
+                        text_width_ValueChanged(this, new EventArgs());\r
+                    // Don't update display size if we're not using anamorphic\r
+                    return;\r
                 case 1:\r
                     text_width.Enabled = false;\r
                     text_height.Enabled = false;\r
                     check_KeepAR.Enabled = false;\r
 \r
-                    SetCustomAnamorphicOptionsVisible(false);\r
+                    setCustomAnamorphicOptionsVisible(false);\r
                     labelStaticDisplaySize.Visible = true;\r
                     labelDisplaySize.Visible = true;\r
+                    drp_modulus.SelectedIndex = 0;\r
 \r
                     check_KeepAR.Checked = true;\r
                     break;\r
@@ -411,9 +223,10 @@ namespace Handbrake.Controls
                     text_height.Enabled = false;\r
                     check_KeepAR.Enabled = false;\r
 \r
-                    SetCustomAnamorphicOptionsVisible(false);\r
+                    setCustomAnamorphicOptionsVisible(false);\r
                     labelStaticDisplaySize.Visible = true;\r
                     labelDisplaySize.Visible = true;\r
+                    drp_modulus.SelectedIndex = 0;\r
 \r
                     check_KeepAR.Checked = true;\r
                     break;\r
@@ -422,7 +235,7 @@ namespace Handbrake.Controls
                     text_height.Enabled = true;\r
                     check_KeepAR.Enabled = true;\r
 \r
-                    SetCustomAnamorphicOptionsVisible(true);\r
+                    setCustomAnamorphicOptionsVisible(true);\r
                     labelStaticDisplaySize.Visible = true;\r
                     labelDisplaySize.Visible = true;\r
 \r
@@ -430,30 +243,33 @@ namespace Handbrake.Controls
                     break;\r
             }\r
 \r
-            UpdateAnamorphicValue();\r
-        }\r
+            labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
 \r
-        private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
-        {\r
-            if (drp_anamorphic.SelectedIndex != 3)\r
-            {\r
-                if (check_KeepAR.Checked)\r
-                {\r
-                    text_width_ValueChanged(this, new EventArgs());\r
-                }\r
-            }\r
-            else\r
-            {\r
-                updownParWidth.Enabled = !check_KeepAR.Checked;\r
-                updownParHeight.Enabled = !check_KeepAR.Checked;\r
-            }\r
-        }\r
+            if (check_KeepAR.Checked)\r
+                text_width_ValueChanged(this, new EventArgs());\r
 \r
-        private void crop_ValueChanged(object sender, EventArgs e)\r
+            if (PictureSettingsChanged != null)\r
+                PictureSettingsChanged(this, new EventArgs());\r
+        }\r
+        private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            text_width_ValueChanged(this, new EventArgs());\r
+            preventChangingWidth = true;\r
+            preventChangingHeight = true;\r
+\r
+            text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
+            text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
+\r
+            preventChangingWidth = false;\r
+            preventChangingHeight = false;\r
+\r
+            text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
+            text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
+\r
+            if (PictureSettingsChanged != null)\r
+                PictureSettingsChanged(this, new EventArgs());\r
         }\r
 \r
+        // Cropping Controls\r
         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
         {\r
             crop_top.Enabled = check_customCrop.Checked;\r
@@ -461,11 +277,120 @@ namespace Handbrake.Controls
             crop_left.Enabled = check_customCrop.Checked;\r
             crop_right.Enabled = check_customCrop.Checked;\r
         }\r
+        private void crop_ValueChanged(object sender, EventArgs e)\r
+        {\r
+            text_width_ValueChanged(this, new EventArgs());\r
+        }\r
 \r
-        private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
+        // GUI Functions\r
+        private void setCustomAnamorphicOptionsVisible(bool visible)\r
         {\r
-            text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
-            text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
+            lbl_modulus.Visible = visible;\r
+            lbl_displayWidth.Visible = visible;\r
+            lbl_parWidth.Visible = visible;\r
+            lbl_parHeight.Visible = visible;\r
+\r
+            drp_modulus.Visible = visible;\r
+            updownDisplayWidth.Visible = visible;\r
+            updownParWidth.Visible = visible;\r
+            updownParHeight.Visible = visible;\r
+        }\r
+\r
+        // Calculation Functions\r
+        private Size sourceAspect\r
+        {\r
+            get\r
+            {\r
+                if (Source != null)\r
+                {\r
+                    if (Source.AspectRatio == 1.78F)\r
+                        return new Size(16, 9);\r
+                    if (Source.AspectRatio == 1.33F)\r
+                        return new Size(4, 3);\r
+                }\r
+                return new Size(0, 0);\r
+            }\r
+        }\r
+        private Size calculateAnamorphicSizes()\r
+        {\r
+            if (Source != null)\r
+            {\r
+                /* Set up some variables to make the math easier to follow. */\r
+                int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
+                int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
+                double storage_aspect = (double)cropped_width / cropped_height;\r
+\r
+                /* Figure out what width the source would display at. */\r
+                double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;\r
+\r
+                /*\r
+                     3 different ways of deciding output dimensions:\r
+                      - 1: Strict anamorphic, preserve source dimensions\r
+                      - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio\r
+                      - 3: Power user anamorphic, specify everything\r
+                  */\r
+                double width, height;\r
+                switch (drp_anamorphic.SelectedIndex)\r
+                {\r
+                    case 1:\r
+                        /* Strict anamorphic */\r
+                        double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);\r
+                        displayWidth = Math.Round(displayWidth, 0);\r
+                        Size output = new Size((int)displayWidth, cropped_height);\r
+                        return output;\r
+                    case 2:\r
+                        /* "Loose" anamorphic.\r
+                            - Uses mod16-compliant dimensions,\r
+                            - Allows users to set the width\r
+                        */\r
+                        width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
+                        width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/\r
+\r
+                        height = (width / storage_aspect) + 0.5;\r
+                        height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
+\r
+                        /* The film AR is the source's display width / cropped source height.\r
+                           The output display width is the output height * film AR.\r
+                           The output PAR is the output display width / output storage width. */\r
+                        double pixel_aspect_width = height * source_display_width / cropped_height;\r
+                        double pixel_aspect_height = width;\r
+\r
+                        double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
+                        return new Size((int)disWidthLoose, (int)height);\r
+                    case 3:\r
+\r
+                        // Get the User Interface Values\r
+                        double UIdisplayWidth;\r
+                        double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
+\r
+                        /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
+                        height = GetModulusValue((double)text_height.Value);\r
+\r
+                        if (check_KeepAR.Checked)\r
+                            return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
+\r
+                        return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
+                }\r
+            }\r
+\r
+            // Return a default value of 0,0 to indicate failure\r
+            return new Size(0, 0);\r
+        }\r
+        private double GetModulusValue(double value)\r
+        {\r
+            int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
+            double remainder = value % mod;\r
+\r
+            if (remainder == 0)\r
+                return value;\r
+\r
+            return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
+        }\r
+        private static int GetCropMod2Clean(int value)\r
+        {\r
+            int remainder = value % 2;\r
+            if (remainder == 0) return value;\r
+            return (value + remainder);\r
         }\r
     }\r
-}\r
+}
\ No newline at end of file