2 using System.ComponentModel;
\r
3 using System.Drawing;
\r
4 using System.Globalization;
\r
5 using System.Windows.Forms;
\r
6 using Handbrake.Parsing;
\r
8 namespace Handbrake.Controls
\r
10 public partial class PictureSettings : UserControl
\r
12 private readonly CultureInfo Culture = new CultureInfo("en-US", false);
\r
13 public event EventHandler PictureSettingsChanged;
\r
15 private Boolean preventChangingWidth, preventChangingHeight;
\r
16 private int _PresetMaximumWidth, _PresetMaximumHeight;
\r
17 private Title _SourceTitle;
\r
19 public PictureSettings()
\r
21 InitializeComponent();
\r
23 drp_anamorphic.SelectedIndex = 1;
\r
24 drp_modulus.SelectedIndex = 0;
\r
28 /// Gets or sets the source media used by this control.
\r
31 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
34 get { return _SourceTitle; }
\r
37 _SourceTitle = value;
\r
38 Enabled = _SourceTitle != null;
\r
40 // Set the Aspect Ratio
\r
41 lbl_Aspect.Text = _SourceTitle.AspectRatio.ToString(Culture);
\r
42 lbl_src_res.Text = _SourceTitle.Resolution.Width + " x " + _SourceTitle.Resolution.Height;
\r
44 // Set the Recommended Cropping values
\r
45 crop_top.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[0]);
\r
46 crop_bottom.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[1]);
\r
47 crop_left.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[2]);
\r
48 crop_right.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[3]);
\r
50 // Set the Resolution Boxes
\r
51 if (drp_anamorphic.SelectedIndex == 0)
\r
53 if (text_width.Value == 0) // Only update the values if the fields don't already have values.
\r
54 text_width.Value = _SourceTitle.Resolution.Width;
\r
56 check_KeepAR.Checked = true; // Forces Resolution to be correct.
\r
60 text_width.Value = _SourceTitle.Resolution.Width;
\r
61 text_height.Value = _SourceTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
62 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
65 updownParWidth.Value = _SourceTitle.ParVal.Width;
\r
66 updownParHeight.Value = _SourceTitle.ParVal.Height;
\r
67 updownDisplayWidth.Value = calculateAnamorphicSizes().Width;
\r
72 /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to
\r
73 /// "0" if the maximum does not matter.
\r
76 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
77 public Size PresetMaximumResolution
\r
79 get { return new Size(_PresetMaximumWidth, _PresetMaximumHeight); }
\r
82 _PresetMaximumWidth = value.Width;
\r
83 _PresetMaximumHeight = value.Height;
\r
85 if (value.Width != 0 && value.Height != 0)
\r
86 lbl_max.Text = "Max Width / Height";
\r
87 else if (value.Width != 0)
\r
88 lbl_max.Text = "Max Width";
\r
89 else if (value.Height != 0)
\r
90 lbl_max.Text = "Max Height";
\r
97 private void text_width_ValueChanged(object sender, EventArgs e)
\r
99 if (preventChangingWidth)
\r
102 // Make sure the new value doesn't exceed the maximum
\r
103 if (Source != null)
\r
104 if (text_width.Value > Source.Resolution.Width)
\r
105 text_width.Value = Source.Resolution.Width;
\r
107 switch (drp_anamorphic.SelectedIndex)
\r
110 if (check_KeepAR.Checked && Source != null)
\r
112 preventChangingHeight = true;
\r
114 int width = (int)text_width.Value;
\r
116 double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
117 double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
118 double newHeight = (width * Source.Resolution.Width * sourceAspect.Height * crop_height) /
\r
119 (Source.Resolution.Height * sourceAspect.Width * crop_width);
\r
120 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
122 preventChangingHeight = false;
\r
126 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
130 preventChangingWidth = false;
\r
132 private void text_height_ValueChanged(object sender, EventArgs e)
\r
134 if (preventChangingHeight)
\r
137 if (Source != null)
\r
138 if (text_height.Value > Source.Resolution.Height)
\r
139 text_height.Value = Source.Resolution.Height;
\r
141 switch (drp_anamorphic.SelectedIndex)
\r
144 if (check_KeepAR.Checked && Source != null)
\r
146 preventChangingWidth = true;
\r
148 double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
149 double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
151 double new_width = ((double)text_height.Value * Source.Resolution.Height * sourceAspect.Width * crop_width) /
\r
152 (Source.Resolution.Width * sourceAspect.Height * crop_height);
\r
154 text_width.Value = (decimal)GetModulusValue(new_width);
\r
156 preventChangingWidth = false;
\r
160 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
164 preventChangingHeight = false;
\r
166 private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
\r
168 //Force TextWidth to recalc height
\r
169 if (check_KeepAR.Checked)
\r
170 text_width_ValueChanged(this, new EventArgs());
\r
172 // Disable the Custom Anamorphic Par Controls if checked.
\r
173 if (drp_anamorphic.SelectedIndex == 3)
\r
175 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
176 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
179 // Raise the Picture Settings Changed Event
\r
180 if (PictureSettingsChanged != null)
\r
181 PictureSettingsChanged(this, new EventArgs());
\r
184 // Anamorphic Controls
\r
185 private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
\r
187 switch (drp_anamorphic.SelectedIndex)
\r
190 text_width.Enabled = true;
\r
191 text_height.Enabled = true;
\r
192 check_KeepAR.Enabled = true;
\r
194 setCustomAnamorphicOptionsVisible(false);
\r
195 labelStaticDisplaySize.Visible = false;
\r
196 labelDisplaySize.Visible = false;
\r
198 check_KeepAR.Checked = true;
\r
199 drp_modulus.SelectedIndex = 0;
\r
201 if (check_KeepAR.Checked)
\r
202 text_width_ValueChanged(this, new EventArgs());
\r
203 // Don't update display size if we're not using anamorphic
\r
206 text_width.Enabled = false;
\r
207 text_height.Enabled = false;
\r
208 check_KeepAR.Enabled = false;
\r
210 setCustomAnamorphicOptionsVisible(false);
\r
211 labelStaticDisplaySize.Visible = true;
\r
212 labelDisplaySize.Visible = true;
\r
213 drp_modulus.SelectedIndex = 0;
\r
215 check_KeepAR.Checked = true;
\r
218 text_width.Enabled = true;
\r
219 text_height.Enabled = false;
\r
220 check_KeepAR.Enabled = false;
\r
222 setCustomAnamorphicOptionsVisible(false);
\r
223 labelStaticDisplaySize.Visible = true;
\r
224 labelDisplaySize.Visible = true;
\r
225 drp_modulus.SelectedIndex = 0;
\r
227 check_KeepAR.Checked = true;
\r
230 text_width.Enabled = true;
\r
231 text_height.Enabled = true;
\r
232 check_KeepAR.Enabled = true;
\r
234 setCustomAnamorphicOptionsVisible(true);
\r
235 labelStaticDisplaySize.Visible = true;
\r
236 labelDisplaySize.Visible = true;
\r
238 check_KeepAR.Checked = true;
\r
242 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
244 if (check_KeepAR.Checked)
\r
245 text_width_ValueChanged(this, new EventArgs());
\r
247 if (PictureSettingsChanged != null)
\r
248 PictureSettingsChanged(this, new EventArgs());
\r
250 private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
\r
252 preventChangingWidth = true;
\r
253 preventChangingHeight = true;
\r
255 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
256 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
258 preventChangingWidth = false;
\r
259 preventChangingHeight = false;
\r
261 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
262 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
264 if (PictureSettingsChanged != null)
\r
265 PictureSettingsChanged(this, new EventArgs());
\r
268 // Cropping Controls
\r
269 private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
\r
271 crop_top.Enabled = check_customCrop.Checked;
\r
272 crop_bottom.Enabled = check_customCrop.Checked;
\r
273 crop_left.Enabled = check_customCrop.Checked;
\r
274 crop_right.Enabled = check_customCrop.Checked;
\r
276 private void crop_ValueChanged(object sender, EventArgs e)
\r
278 text_width_ValueChanged(this, new EventArgs());
\r
282 private void setCustomAnamorphicOptionsVisible(bool visible)
\r
284 lbl_modulus.Visible = visible;
\r
285 lbl_displayWidth.Visible = visible;
\r
286 lbl_parWidth.Visible = visible;
\r
287 lbl_parHeight.Visible = visible;
\r
289 drp_modulus.Visible = visible;
\r
290 updownDisplayWidth.Visible = visible;
\r
291 updownParWidth.Visible = visible;
\r
292 updownParHeight.Visible = visible;
\r
295 // Calculation Functions
\r
296 private Size sourceAspect
\r
300 if (Source != null)
\r
302 if (Source.AspectRatio == 1.78F)
\r
303 return new Size(16, 9);
\r
304 if (Source.AspectRatio == 1.33F)
\r
305 return new Size(4, 3);
\r
307 return new Size(0, 0);
\r
310 private Size calculateAnamorphicSizes()
\r
312 if (Source != null)
\r
314 /* Set up some variables to make the math easier to follow. */
\r
315 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
316 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
317 double storage_aspect = (double)cropped_width / cropped_height;
\r
319 /* Figure out what width the source would display at. */
\r
320 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;
\r
323 3 different ways of deciding output dimensions:
\r
324 - 1: Strict anamorphic, preserve source dimensions
\r
325 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
326 - 3: Power user anamorphic, specify everything
\r
328 double width, height;
\r
329 switch (drp_anamorphic.SelectedIndex)
\r
332 /* Strict anamorphic */
\r
333 double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);
\r
334 displayWidth = Math.Round(displayWidth, 0);
\r
335 Size output = new Size((int)displayWidth, cropped_height);
\r
338 /* "Loose" anamorphic.
\r
339 - Uses mod16-compliant dimensions,
\r
340 - Allows users to set the width
\r
342 width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
\r
343 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
345 height = (width / storage_aspect) + 0.5;
\r
346 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
348 /* The film AR is the source's display width / cropped source height.
\r
349 The output display width is the output height * film AR.
\r
350 The output PAR is the output display width / output storage width. */
\r
351 double pixel_aspect_width = height * source_display_width / cropped_height;
\r
352 double pixel_aspect_height = width;
\r
354 double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);
\r
355 return new Size((int)disWidthLoose, (int)height);
\r
358 // Get the User Interface Values
\r
359 double UIdisplayWidth;
\r
360 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
362 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
363 height = GetModulusValue((double)text_height.Value);
\r
365 if (check_KeepAR.Checked)
\r
366 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
368 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
372 // Return a default value of 0,0 to indicate failure
\r
373 return new Size(0, 0);
\r
375 private double GetModulusValue(double value)
\r
377 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
378 double remainder = value % mod;
\r
380 if (remainder == 0)
\r
383 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
385 private static int GetCropMod2Clean(int value)
\r
387 int remainder = value % 2;
\r
388 if (remainder == 0) return value;
\r
389 return (value + remainder);
\r