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, _preventChangingCustom, _preventChangingDisplayWidth;
\r
16 private int _presetMaximumWidth, _presetMaximumHeight;
\r
17 private double _cachedDar;
\r
18 private Title _sourceTitle;
\r
20 public PictureSettings()
\r
22 InitializeComponent();
\r
24 drp_anamorphic.SelectedIndex = 1;
\r
25 drp_modulus.SelectedIndex = 0;
\r
29 /// Gets or sets the source media used by this control.
\r
32 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
35 private get { return _sourceTitle; }
\r
38 _sourceTitle = value;
\r
39 Enabled = _sourceTitle != null;
\r
41 // Set the Aspect Ratio
\r
42 lbl_Aspect.Text = _sourceTitle.AspectRatio.ToString(Culture);
\r
43 lbl_src_res.Text = _sourceTitle.Resolution.Width + " x " + _sourceTitle.Resolution.Height;
\r
45 // Set the Recommended Cropping values
\r
46 crop_top.Value = GetCropMod2Clean(_sourceTitle.AutoCropDimensions[0]);
\r
47 crop_bottom.Value = GetCropMod2Clean(_sourceTitle.AutoCropDimensions[1]);
\r
48 crop_left.Value = GetCropMod2Clean(_sourceTitle.AutoCropDimensions[2]);
\r
49 crop_right.Value = GetCropMod2Clean(_sourceTitle.AutoCropDimensions[3]);
\r
51 // Set the Resolution Boxes
\r
52 if (drp_anamorphic.SelectedIndex == 0)
\r
54 if (text_width.Value == 0) // Only update the values if the fields don't already have values.
\r
55 text_width.Value = _sourceTitle.Resolution.Width;
\r
57 check_KeepAR.Checked = true; // Forces Resolution to be correct.
\r
61 if (text_width.Value == 0 && text_height.Value == 0)// Only update the values if the fields don't already have values.
\r
63 text_width.Value = _sourceTitle.Resolution.Width;
\r
64 text_height.Value = _sourceTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
67 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
70 //updownDisplayWidth.Value = CalculateAnamorphicSizes().Width;
\r
71 updownParWidth.Value = _sourceTitle.ParVal.Width;
\r
72 updownParHeight.Value = _sourceTitle.ParVal.Height;
\r
73 //_cachedDar = (double)updownDisplayWidth.Value / (double)text_height.Value;
\r
76 Size croppedDar = CalculateAnamorphicSizes();
\r
77 _cachedDar = (double) croppedDar.Width/croppedDar.Height;
\r
78 updownDisplayWidth.Value = croppedDar.Width;
\r
83 /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to
\r
84 /// "0" if the maximum does not matter.
\r
87 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
88 public Size PresetMaximumResolution
\r
90 get { return new Size(_presetMaximumWidth, _presetMaximumHeight); }
\r
93 _presetMaximumWidth = value.Width;
\r
94 _presetMaximumHeight = value.Height;
\r
96 if (value.Width != 0 && value.Height != 0)
\r
97 lbl_max.Text = "Max Width / Height";
\r
98 else if (value.Width != 0)
\r
99 lbl_max.Text = "Max Width";
\r
100 else if (value.Height != 0)
\r
101 lbl_max.Text = "Max Height";
\r
107 // Picture Controls
\r
108 private void text_width_ValueChanged(object sender, EventArgs e)
\r
110 if (Properties.Settings.Default.disableResCalc)
\r
113 if (_preventChangingWidth)
\r
116 // Make sure the new value doesn't exceed the maximum
\r
117 if (Source != null)
\r
118 if (text_width.Value > Source.Resolution.Width)
\r
119 text_width.Value = Source.Resolution.Width;
\r
121 switch (drp_anamorphic.SelectedIndex)
\r
124 if (check_KeepAR.Checked && Source != null)
\r
126 _preventChangingHeight = true;
\r
128 int width = (int)text_width.Value;
\r
130 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
131 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
133 if (SourceAspect.Width == 0 && SourceAspect.Height == 0)
\r
136 double newHeight = ((double)width * Source.Resolution.Width * SourceAspect.Height * crop_height) /
\r
137 (Source.Resolution.Height * SourceAspect.Width * crop_width);
\r
138 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
140 _preventChangingHeight = false;
\r
144 if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)
\r
146 if (_preventChangingCustom)
\r
149 _preventChangingDisplayWidth = true;
\r
150 updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;
\r
151 _preventChangingDisplayWidth = false;
\r
153 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
156 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
158 updownParWidth.Value = updownDisplayWidth.Value;
\r
159 updownParHeight.Value = text_width.Value;
\r
163 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
167 _preventChangingWidth = false;
\r
169 private void text_height_ValueChanged(object sender, EventArgs e)
\r
171 if (Properties.Settings.Default.disableResCalc)
\r
174 if (_preventChangingHeight)
\r
177 if (Source != null)
\r
178 if (text_height.Value > Source.Resolution.Height)
\r
179 text_height.Value = Source.Resolution.Height;
\r
181 switch (drp_anamorphic.SelectedIndex)
\r
184 if (check_KeepAR.Checked && Source != null)
\r
186 _preventChangingWidth = true;
\r
188 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
189 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
191 double new_width = ((double)text_height.Value * Source.Resolution.Height * SourceAspect.Width * crop_width) /
\r
192 (Source.Resolution.Width * SourceAspect.Height * crop_height);
\r
194 text_width.Value = (decimal)GetModulusValue(new_width);
\r
196 _preventChangingWidth = false;
\r
200 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
202 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
204 // - Changes DISPLAY WIDTH to keep DAR
\r
205 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
206 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
207 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
209 double rawCalculatedDisplayWidth = (double)text_height.Value * _cachedDar;
\r
211 _preventChangingDisplayWidth = true; // Start Guards
\r
212 _preventChangingWidth = true;
\r
214 updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;
\r
215 updownParWidth.Value = updownDisplayWidth.Value;
\r
216 updownParHeight.Value = text_width.Value;
\r
218 _preventChangingWidth = false; // Reset Guards
\r
219 _preventChangingDisplayWidth = false;
\r
224 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
228 _preventChangingHeight = false;
\r
230 private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
\r
232 if (Properties.Settings.Default.disableResCalc)
\r
235 //Force TextWidth to recalc height
\r
236 if (check_KeepAR.Checked)
\r
237 text_width_ValueChanged(this, new EventArgs());
\r
239 // Disable the Custom Anamorphic Par Controls if checked.
\r
240 if (drp_anamorphic.SelectedIndex == 3)
\r
242 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
243 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
246 // Raise the Picture Settings Changed Event
\r
247 if (PictureSettingsChanged != null)
\r
248 PictureSettingsChanged(this, new EventArgs());
\r
250 private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)
\r
252 if (Properties.Settings.Default.disableResCalc)
\r
255 if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)
\r
257 _preventChangingCustom = true;
\r
258 updownParWidth.Value = updownDisplayWidth.Value;
\r
259 updownParHeight.Value = text_width.Value;
\r
260 _preventChangingCustom = false;
\r
263 if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)
\r
265 // - Changes HEIGHT to keep DAR
\r
266 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
267 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
268 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
270 // Calculate new Height Value
\r
272 if(!int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus))
\r
275 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / _cachedDar);
\r
276 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);
\r
279 _preventChangingHeight = true;
\r
280 text_height.Value = (decimal)modulusHeight;
\r
281 updownParWidth.Value = updownDisplayWidth.Value;
\r
282 updownParHeight.Value = text_width.Value;
\r
283 _preventChangingHeight = false;
\r
288 // Anamorphic Controls
\r
289 private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
\r
291 switch (drp_anamorphic.SelectedIndex)
\r
294 text_width.Enabled = true;
\r
295 text_height.Enabled = true;
\r
296 check_KeepAR.Enabled = true;
\r
298 SetCustomAnamorphicOptionsVisible(false);
\r
299 labelStaticDisplaySize.Visible = false;
\r
300 labelDisplaySize.Visible = false;
\r
302 check_KeepAR.Checked = true;
\r
303 drp_modulus.SelectedIndex = 0;
\r
305 if (check_KeepAR.Checked)
\r
306 text_width_ValueChanged(this, new EventArgs());
\r
307 // Don't update display size if we're not using anamorphic
\r
310 text_width.Enabled = false;
\r
311 text_height.Enabled = false;
\r
312 check_KeepAR.Enabled = false;
\r
314 SetCustomAnamorphicOptionsVisible(false);
\r
315 labelStaticDisplaySize.Visible = true;
\r
316 labelDisplaySize.Visible = true;
\r
317 drp_modulus.SelectedIndex = 0;
\r
319 check_KeepAR.Checked = true;
\r
322 text_width.Enabled = true;
\r
323 text_height.Enabled = false;
\r
324 check_KeepAR.Enabled = false;
\r
326 SetCustomAnamorphicOptionsVisible(false);
\r
327 labelStaticDisplaySize.Visible = true;
\r
328 labelDisplaySize.Visible = true;
\r
329 drp_modulus.SelectedIndex = 0;
\r
331 check_KeepAR.Checked = true;
\r
334 text_width.Enabled = true;
\r
335 text_height.Enabled = true;
\r
336 check_KeepAR.Enabled = true;
\r
338 SetCustomAnamorphicOptionsVisible(true);
\r
339 labelStaticDisplaySize.Visible = true;
\r
340 labelDisplaySize.Visible = true;
\r
342 check_KeepAR.Checked = true;
\r
343 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
344 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
349 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
351 if (check_KeepAR.Checked)
\r
352 text_width_ValueChanged(this, new EventArgs());
\r
354 if (PictureSettingsChanged != null)
\r
355 PictureSettingsChanged(this, new EventArgs());
\r
357 private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
\r
359 _preventChangingWidth = true;
\r
360 _preventChangingHeight = true;
\r
362 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
363 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
365 _preventChangingWidth = false;
\r
366 _preventChangingHeight = false;
\r
368 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
369 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
371 if (PictureSettingsChanged != null)
\r
372 PictureSettingsChanged(this, new EventArgs());
\r
375 // Cropping Controls
\r
376 private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
\r
378 crop_top.Enabled = check_customCrop.Checked;
\r
379 crop_bottom.Enabled = check_customCrop.Checked;
\r
380 crop_left.Enabled = check_customCrop.Checked;
\r
381 crop_right.Enabled = check_customCrop.Checked;
\r
383 crop_top.Value = Source.AutoCropDimensions[0];
\r
384 crop_bottom.Value = Source.AutoCropDimensions[1];
\r
385 crop_left.Value = Source.AutoCropDimensions[2];
\r
386 crop_right.Value = Source.AutoCropDimensions[3];
\r
388 private void crop_ValueChanged(object sender, EventArgs e)
\r
390 text_width_ValueChanged(this, new EventArgs());
\r
394 private void SetCustomAnamorphicOptionsVisible(bool visible)
\r
396 lbl_modulus.Visible = visible;
\r
397 lbl_displayWidth.Visible = visible;
\r
398 lbl_parWidth.Visible = visible;
\r
399 lbl_parHeight.Visible = visible;
\r
401 drp_modulus.Visible = visible;
\r
402 updownDisplayWidth.Visible = visible;
\r
403 updownParWidth.Visible = visible;
\r
404 updownParHeight.Visible = visible;
\r
407 // Calculation Functions
\r
408 private Size SourceAspect
\r
412 if (Source != null) // display aspect = (width * par_width) / (height * par_height)
\r
413 return new Size((Source.ParVal.Width * Source.Resolution.Width), (Source.ParVal.Height * Source.Resolution.Height));
\r
415 return new Size(0, 0); // Fall over to 16:9 and hope for the best
\r
418 private Size CalculateAnamorphicSizes()
\r
420 if (Source != null)
\r
422 /* Set up some variables to make the math easier to follow. */
\r
423 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
424 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
425 double storage_aspect = (double)cropped_width / cropped_height;
\r
427 /* Figure out what width the source would display at. */
\r
428 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;
\r
431 3 different ways of deciding output dimensions:
\r
432 - 1: Strict anamorphic, preserve source dimensions
\r
433 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
434 - 3: Power user anamorphic, specify everything
\r
436 double width, height;
\r
437 switch (drp_anamorphic.SelectedIndex)
\r
441 /* Strict anamorphic */
\r
442 double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);
\r
443 displayWidth = Math.Round(displayWidth, 0);
\r
444 Size output = new Size((int)displayWidth, cropped_height);
\r
447 /* "Loose" anamorphic.
\r
448 - Uses mod16-compliant dimensions,
\r
449 - Allows users to set the width
\r
451 width = (int) text_width.Value;
\r
452 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
454 height = (width / storage_aspect) + 0.5;
\r
455 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
457 /* The film AR is the source's display width / cropped source height.
\r
458 The output display width is the output height * film AR.
\r
459 The output PAR is the output display width / output storage width. */
\r
460 double pixel_aspect_width = height * source_display_width / cropped_height;
\r
461 double pixel_aspect_height = width;
\r
463 double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);
\r
464 if (double.IsNaN(disWidthLoose))
\r
466 return new Size((int)disWidthLoose, (int)height);
\r
469 // Get the User Interface Values
\r
470 double UIdisplayWidth;
\r
471 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
473 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
474 height = GetModulusValue((double)text_height.Value);
\r
476 if (check_KeepAR.Checked)
\r
477 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
479 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
483 // Return a default value of 0,0 to indicate failure
\r
484 return new Size(0, 0);
\r
486 private double GetModulusValue(double value)
\r
488 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
489 double remainder = value % mod;
\r
491 if (remainder == 0)
\r
494 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
496 private static int GetCropMod2Clean(int value)
\r
498 int remainder = value % 2;
\r
499 if (remainder == 0) return value;
\r
500 return (value + remainder);
\r
503 // Hidden UI feature to drop the MaxWidth / Height with the MaxWidth/Height label is double clicked
\r
504 private void lbl_max_DoubleClick(object sender, EventArgs e)
\r
506 PresetMaximumResolution = new Size(0,0);
\r
507 if (PictureSettingsChanged != null)
\r
508 PictureSettingsChanged(this, new EventArgs());
\r