1 /* PictureSetting.cs $
\r
2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace Handbrake.Controls
\r
9 using System.Drawing;
\r
10 using System.Globalization;
\r
11 using System.Windows.Forms;
\r
13 using HandBrake.ApplicationServices.Parsing;
\r
18 /// The Picture Settings Panel
\r
20 public partial class PictureSettings : UserControl
\r
22 private readonly CultureInfo culture = new CultureInfo("en-US", false);
\r
23 private bool preventChangingWidth;
\r
24 private bool preventChangingHeight;
\r
25 private bool preventChangingCustom;
\r
26 private bool preventChangingDisplayWidth;
\r
27 private double cachedDar;
\r
28 private Title sourceTitle;
\r
31 /// Initializes a new instance of the <see cref="PictureSettings"/> class.
\r
32 /// Creates a new instance of the Picture Settings Class
\r
34 public PictureSettings()
\r
36 InitializeComponent();
\r
38 drp_anamorphic.SelectedIndex = 1;
\r
39 drp_modulus.SelectedIndex = 0;
\r
43 /// Picture Settings Changed Event Handler
\r
45 public event EventHandler PictureSettingsChanged;
\r
48 /// Gets or sets the source media used by this control.
\r
59 sourceTitle = value;
\r
60 Enabled = sourceTitle != null;
\r
66 /// Gets or sets the currently selected preset.
\r
68 public Preset CurrentlySelectedPreset { get; set; }
\r
71 /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to
\r
72 /// "0" if the maximum does not matter.
\r
74 public Size PresetMaximumResolution { get; set; }
\r
77 /// Set the Preset Crop Warning Label
\r
79 /// <param name="selectedPreset">
\r
80 /// The Selected preset
\r
82 public void SetPresetCropWarningLabel(Preset selectedPreset)
\r
84 lbl_presetCropWarning.Visible = true;
\r
85 if (selectedPreset != null && selectedPreset.PictureSettings == false)
\r
86 lbl_presetCropWarning.Visible = false;
\r
87 else if (selectedPreset == null)
\r
88 lbl_presetCropWarning.Visible = false;
\r
92 /// Setup the UI. The Source has just changed.
\r
94 private void NewSourceSet()
\r
96 // Set the Aspect Ratio
\r
97 lbl_src_res.Text = sourceTitle.Resolution.Width + " x " + sourceTitle.Resolution.Height;
\r
99 // Set the Recommended Cropping values, but only if a preset doesn't have hard set picture settings.
\r
100 if ((CurrentlySelectedPreset != null && CurrentlySelectedPreset.PictureSettings == false) || CurrentlySelectedPreset == null)
\r
102 crop_top.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Top);
\r
103 crop_bottom.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Bottom);
\r
104 crop_left.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Left);
\r
105 crop_right.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Right);
\r
108 SetPresetCropWarningLabel(CurrentlySelectedPreset);
\r
110 // Set the Resolution Boxes
\r
111 if (drp_anamorphic.SelectedIndex == 0)
\r
113 int width = sourceTitle.Resolution.Width;
\r
115 if (width > PresetMaximumResolution.Width && PresetMaximumResolution.Width != 0) // If the preset has a Max width set, don't use a width larger than it.
\r
116 width = PresetMaximumResolution.Width;
\r
118 if (text_width.Value == 0 || (text_width.Value > PresetMaximumResolution.Width && PresetMaximumResolution.Width != 0)) // Only update the values if the fields don't already have values.
\r
119 text_width.Value = width;
\r
121 check_KeepAR.Checked = true; // Forces Resolution to be correct.
\r
125 int width = sourceTitle.Resolution.Width;
\r
126 if (width > PresetMaximumResolution.Width && PresetMaximumResolution.Width != 0) // If the preset has a Max width set, don't use a width larger than it.
\r
127 width = PresetMaximumResolution.Width;
\r
129 int height = sourceTitle.Resolution.Height;
\r
130 if (height > PresetMaximumResolution.Height && PresetMaximumResolution.Height != 0) // If the preset has a Max height set, don't use a width larger than it.
\r
131 height = PresetMaximumResolution.Height;
\r
133 text_width.Value = width;
\r
134 text_height.Value = height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
136 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
139 updownParWidth.Value = sourceTitle.ParVal.Width;
\r
140 updownParHeight.Value = sourceTitle.ParVal.Height;
\r
142 Size croppedDar = CalculateAnamorphicSizes();
\r
143 cachedDar = (double)croppedDar.Width / croppedDar.Height;
\r
144 updownDisplayWidth.Value = croppedDar.Width;
\r
147 // Picture Controls
\r
148 private void TextWidthValueChanged(object sender, EventArgs e)
\r
150 if (Properties.Settings.Default.disableResCalc)
\r
153 if (preventChangingWidth)
\r
156 // Make sure the new value doesn't exceed the maximum
\r
157 if (Source != null)
\r
158 if (text_width.Value > Source.Resolution.Width)
\r
159 text_width.Value = Source.Resolution.Width;
\r
161 switch (drp_anamorphic.SelectedIndex)
\r
164 if (check_KeepAR.Checked && Source != null)
\r
166 preventChangingHeight = true;
\r
168 int width = (int)text_width.Value;
\r
170 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
171 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
173 if (SourceAspect.Width == 0 && SourceAspect.Height == 0)
\r
176 double newHeight = ((double)width * Source.Resolution.Width * SourceAspect.Height * crop_height) /
\r
177 (Source.Resolution.Height * SourceAspect.Width * crop_width);
\r
178 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
180 preventChangingHeight = false;
\r
184 if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)
\r
186 if (preventChangingCustom)
\r
189 preventChangingDisplayWidth = true;
\r
190 updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;
\r
191 preventChangingDisplayWidth = false;
\r
193 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
196 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
198 updownParWidth.Value = updownDisplayWidth.Value;
\r
199 updownParHeight.Value = text_width.Value;
\r
203 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
207 preventChangingWidth = false;
\r
210 private void TextHeightValueChanged(object sender, EventArgs e)
\r
212 if (Properties.Settings.Default.disableResCalc)
\r
215 if (preventChangingHeight)
\r
218 if (Source != null)
\r
219 if (text_height.Value > Source.Resolution.Height)
\r
220 text_height.Value = Source.Resolution.Height;
\r
222 switch (drp_anamorphic.SelectedIndex)
\r
225 if (check_KeepAR.Checked && Source != null)
\r
227 preventChangingWidth = true;
\r
229 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
230 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
232 double new_width = ((double)text_height.Value * Source.Resolution.Height * SourceAspect.Width *
\r
234 (Source.Resolution.Width * SourceAspect.Height * crop_height);
\r
236 text_width.Value = (decimal)GetModulusValue(new_width);
\r
238 preventChangingWidth = false;
\r
242 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
244 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
246 // - Changes DISPLAY WIDTH to keep DAR
\r
247 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
248 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
249 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
251 double rawCalculatedDisplayWidth = (double)text_height.Value * cachedDar;
\r
253 preventChangingDisplayWidth = true; // Start Guards
\r
254 preventChangingWidth = true;
\r
256 updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;
\r
257 updownParWidth.Value = updownDisplayWidth.Value;
\r
258 updownParHeight.Value = text_width.Value;
\r
260 preventChangingWidth = false; // Reset Guards
\r
261 preventChangingDisplayWidth = false;
\r
266 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
270 preventChangingHeight = false;
\r
273 private void CheckKeepArCheckedChanged(object sender, EventArgs e)
\r
275 if (Properties.Settings.Default.disableResCalc)
\r
278 // Force TextWidth to recalc height
\r
279 if (check_KeepAR.Checked)
\r
280 TextWidthValueChanged(this, new EventArgs());
\r
282 // Disable the Custom Anamorphic Par Controls if checked.
\r
283 if (drp_anamorphic.SelectedIndex == 3)
\r
285 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
286 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
289 // Raise the Picture Settings Changed Event
\r
290 if (PictureSettingsChanged != null)
\r
291 PictureSettingsChanged(this, new EventArgs());
\r
294 private void UpdownDisplayWidthValueChanged(object sender, EventArgs e)
\r
296 if (Properties.Settings.Default.disableResCalc)
\r
299 if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)
\r
301 preventChangingCustom = true;
\r
302 updownParWidth.Value = updownDisplayWidth.Value;
\r
303 updownParHeight.Value = text_width.Value;
\r
304 preventChangingCustom = false;
\r
307 if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)
\r
309 // - Changes HEIGHT to keep DAR
\r
310 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
311 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
312 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
314 // Calculate new Height Value
\r
316 if (!int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus))
\r
319 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / cachedDar);
\r
320 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);
\r
323 preventChangingHeight = true;
\r
324 text_height.Value = (decimal)modulusHeight;
\r
325 updownParWidth.Value = updownDisplayWidth.Value;
\r
326 updownParHeight.Value = text_width.Value;
\r
327 preventChangingHeight = false;
\r
331 // Anamorphic Controls
\r
332 private void DrpAnamorphicSelectedIndexChanged(object sender, EventArgs e)
\r
334 switch (drp_anamorphic.SelectedIndex)
\r
337 text_width.Enabled = true;
\r
338 text_height.Enabled = true;
\r
339 check_KeepAR.Enabled = true;
\r
341 SetCustomAnamorphicOptionsVisible(false);
\r
342 labelStaticDisplaySize.Visible = false;
\r
343 labelDisplaySize.Visible = false;
\r
344 drp_modulus.Visible = true;
\r
345 lbl_modulus.Visible = true;
\r
347 // check_KeepAR.Checked = true;
\r
349 if (check_KeepAR.Checked)
\r
350 TextWidthValueChanged(this, new EventArgs());
\r
351 // Don't update display size if we're not using anamorphic
\r
354 text_width.Enabled = false;
\r
355 text_height.Enabled = false;
\r
356 check_KeepAR.Enabled = false;
\r
358 SetCustomAnamorphicOptionsVisible(false);
\r
359 labelStaticDisplaySize.Visible = true;
\r
360 labelDisplaySize.Visible = true;
\r
362 check_KeepAR.Checked = true;
\r
365 text_width.Enabled = true;
\r
366 text_height.Enabled = false;
\r
367 check_KeepAR.Enabled = false;
\r
369 SetCustomAnamorphicOptionsVisible(false);
\r
370 labelStaticDisplaySize.Visible = true;
\r
371 labelDisplaySize.Visible = true;
\r
372 drp_modulus.Visible = true;
\r
373 lbl_modulus.Visible = true;
\r
375 check_KeepAR.Checked = true;
\r
378 text_width.Enabled = true;
\r
379 text_height.Enabled = true;
\r
380 check_KeepAR.Enabled = true;
\r
382 SetCustomAnamorphicOptionsVisible(true);
\r
383 labelStaticDisplaySize.Visible = true;
\r
384 labelDisplaySize.Visible = true;
\r
386 check_KeepAR.Checked = true;
\r
387 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
388 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
392 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
394 if (check_KeepAR.Checked)
\r
395 TextWidthValueChanged(this, new EventArgs());
\r
397 if (PictureSettingsChanged != null)
\r
398 PictureSettingsChanged(this, new EventArgs());
\r
401 private void DrpModulusSelectedIndexChanged(object sender, EventArgs e)
\r
403 preventChangingWidth = true;
\r
404 preventChangingHeight = true;
\r
406 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
407 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
409 preventChangingWidth = false;
\r
410 preventChangingHeight = false;
\r
412 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
413 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
415 if (PictureSettingsChanged != null)
\r
416 PictureSettingsChanged(this, new EventArgs());
\r
419 // Cropping Controls
\r
420 private void CheckAutoCropCheckedChanged(object sender, EventArgs e)
\r
422 crop_top.Enabled = check_customCrop.Checked;
\r
423 crop_bottom.Enabled = check_customCrop.Checked;
\r
424 crop_left.Enabled = check_customCrop.Checked;
\r
425 crop_right.Enabled = check_customCrop.Checked;
\r
427 if (Source != null)
\r
429 crop_top.Value = Source.AutoCropDimensions.Top;
\r
430 crop_bottom.Value = Source.AutoCropDimensions.Bottom;
\r
431 crop_left.Value = Source.AutoCropDimensions.Left;
\r
432 crop_right.Value = Source.AutoCropDimensions.Right;
\r
436 private void CropValueChanged(object sender, EventArgs e)
\r
438 TextWidthValueChanged(this, new EventArgs());
\r
442 private void SetCustomAnamorphicOptionsVisible(bool visible)
\r
444 lbl_modulus.Visible = visible;
\r
445 lbl_displayWidth.Visible = visible;
\r
446 lbl_parWidth.Visible = visible;
\r
447 lbl_parHeight.Visible = visible;
\r
449 drp_modulus.Visible = visible;
\r
450 updownDisplayWidth.Visible = visible;
\r
451 updownParWidth.Visible = visible;
\r
452 updownParHeight.Visible = visible;
\r
455 // Calculation Functions
\r
456 private Size SourceAspect
\r
460 if (Source != null) // display aspect = (width * par_width) / (height * par_height)
\r
461 return new Size((Source.ParVal.Width * Source.Resolution.Width),
\r
462 (Source.ParVal.Height * Source.Resolution.Height));
\r
464 return new Size(0, 0); // Fall over to 16:9 and hope for the best
\r
468 private Size CalculateAnamorphicSizes()
\r
470 if (Source != null)
\r
472 /* Set up some variables to make the math easier to follow. */
\r
473 int croppedWidth = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
474 int croppedHeight = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
475 double storageAspect = (double)croppedWidth / croppedHeight;
\r
477 /* Figure out what width the source would display at. */
\r
478 double sourceDisplayWidth = (double)croppedWidth * Source.ParVal.Width / Source.ParVal.Height;
\r
481 3 different ways of deciding output dimensions:
\r
482 - 1: Strict anamorphic, preserve source dimensions
\r
483 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
484 - 3: Power user anamorphic, specify everything
\r
486 double width, height;
\r
487 switch (drp_anamorphic.SelectedIndex)
\r
491 /* Strict anamorphic */
\r
492 double displayWidth = ((double)croppedWidth * Source.ParVal.Width / Source.ParVal.Height);
\r
493 displayWidth = Math.Round(displayWidth, 0);
\r
494 Size output = new Size((int)displayWidth, croppedHeight);
\r
497 /* "Loose" anamorphic.
\r
498 - Uses mod16-compliant dimensions,
\r
499 - Allows users to set the width
\r
501 width = (int)text_width.Value;
\r
502 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
504 height = (width / storageAspect) + 0.5;
\r
505 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
507 /* The film AR is the source's display width / cropped source height.
\r
508 The output display width is the output height * film AR.
\r
509 The output PAR is the output display width / output storage width. */
\r
510 double pixelAspectWidth = height * sourceDisplayWidth / croppedHeight;
\r
511 double pixelAspectHeight = width;
\r
513 double disWidthLoose = (width * pixelAspectWidth / pixelAspectHeight);
\r
514 if (double.IsNaN(disWidthLoose))
\r
516 return new Size((int)disWidthLoose, (int)height);
\r
519 // Get the User Interface Values
\r
520 double UIdisplayWidth;
\r
521 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
523 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
524 height = GetModulusValue((double)text_height.Value);
\r
526 if (check_KeepAR.Checked)
\r
527 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
529 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
533 // Return a default value of 0,0 to indicate failure
\r
534 return new Size(0, 0);
\r
537 private double GetModulusValue(double value)
\r
539 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
540 double remainder = value % mod;
\r
542 if (remainder == 0)
\r
545 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
548 private static int GetCropMod2Clean(int value)
\r
550 int remainder = value % 2;
\r
551 if (remainder == 0) return value;
\r
552 return (value + remainder);
\r