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 if (this.check_customCrop.Checked)
\r
86 lbl_presetCropWarning.Visible = true;
\r
87 if (selectedPreset != null && selectedPreset.CropSettings == false) lbl_presetCropWarning.Visible = false;
\r
88 else if (selectedPreset == null) lbl_presetCropWarning.Visible = false;
\r
92 lbl_presetCropWarning.Visible = false;
\r
97 /// Setup the UI. The Source has just changed.
\r
99 private void NewSourceSet()
\r
101 // Set the Aspect Ratio
\r
102 lbl_src_res.Text = sourceTitle.Resolution.Width + " x " + sourceTitle.Resolution.Height;
\r
104 // Set the Recommended Cropping values, but only if a preset doesn't have hard set picture settings.
\r
105 if ((CurrentlySelectedPreset != null && CurrentlySelectedPreset.CropSettings == false) || CurrentlySelectedPreset == null)
\r
107 crop_top.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Top);
\r
108 crop_bottom.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Bottom);
\r
109 crop_left.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Left);
\r
110 crop_right.Value = GetCropMod2Clean(sourceTitle.AutoCropDimensions.Right);
\r
113 SetPresetCropWarningLabel(CurrentlySelectedPreset);
\r
115 // Set the Resolution Boxes
\r
116 if (drp_anamorphic.SelectedIndex == 0)
\r
118 int width = sourceTitle.Resolution.Width;
\r
120 if (width > PresetMaximumResolution.Width && PresetMaximumResolution.Width != 0) // If the preset has a Max width set, don't use a width larger than it.
\r
121 width = PresetMaximumResolution.Width;
\r
123 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
124 text_width.Value = width;
\r
126 check_KeepAR.Checked = true; // Forces Resolution to be correct.
\r
130 int width = sourceTitle.Resolution.Width;
\r
131 if (width > PresetMaximumResolution.Width && PresetMaximumResolution.Width != 0) // If the preset has a Max width set, don't use a width larger than it.
\r
132 width = PresetMaximumResolution.Width;
\r
134 int height = sourceTitle.Resolution.Height;
\r
135 if (height > PresetMaximumResolution.Height && PresetMaximumResolution.Height != 0) // If the preset has a Max height set, don't use a width larger than it.
\r
136 height = PresetMaximumResolution.Height;
\r
138 text_width.Value = width;
\r
139 text_height.Value = height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
141 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
144 updownParWidth.Value = sourceTitle.ParVal.Width;
\r
145 updownParHeight.Value = sourceTitle.ParVal.Height;
\r
147 Size croppedDar = CalculateAnamorphicSizes();
\r
148 cachedDar = (double)croppedDar.Width / croppedDar.Height;
\r
149 updownDisplayWidth.Value = croppedDar.Width;
\r
152 // Picture Controls
\r
153 private void TextWidthValueChanged(object sender, EventArgs e)
\r
155 if (preventChangingWidth)
\r
158 // Make sure the new value doesn't exceed the maximum
\r
159 if (Source != null)
\r
160 if (text_width.Value > Source.Resolution.Width)
\r
161 text_width.Value = Source.Resolution.Width;
\r
163 switch (drp_anamorphic.SelectedIndex)
\r
166 if (check_KeepAR.Checked && Source != null)
\r
168 preventChangingHeight = true;
\r
170 int width = (int)text_width.Value;
\r
172 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
173 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
175 if (SourceAspect.Width == 0 && SourceAspect.Height == 0)
\r
178 double newHeight = ((double)width * Source.Resolution.Width * SourceAspect.Height * crop_height) /
\r
179 (Source.Resolution.Height * SourceAspect.Width * crop_width);
\r
180 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
182 preventChangingHeight = false;
\r
186 if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)
\r
188 if (preventChangingCustom)
\r
191 preventChangingDisplayWidth = true;
\r
192 updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;
\r
193 preventChangingDisplayWidth = false;
\r
195 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
198 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
200 updownParWidth.Value = updownDisplayWidth.Value;
\r
201 updownParHeight.Value = text_width.Value;
\r
205 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
209 preventChangingWidth = false;
\r
212 private void TextHeightValueChanged(object sender, EventArgs e)
\r
214 if (preventChangingHeight)
\r
217 if (Source != null)
\r
218 if (text_height.Value > Source.Resolution.Height)
\r
219 text_height.Value = Source.Resolution.Height;
\r
221 switch (drp_anamorphic.SelectedIndex)
\r
224 if (check_KeepAR.Checked && Source != null)
\r
226 preventChangingWidth = true;
\r
228 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
229 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
231 double new_width = ((double)text_height.Value * Source.Resolution.Height * SourceAspect.Width *
\r
233 (Source.Resolution.Width * SourceAspect.Height * crop_height);
\r
235 text_width.Value = (decimal)GetModulusValue(new_width);
\r
237 preventChangingWidth = false;
\r
241 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
243 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
245 // - Changes DISPLAY WIDTH to keep DAR
\r
246 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
247 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
248 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
250 double rawCalculatedDisplayWidth = (double)text_height.Value * cachedDar;
\r
252 preventChangingDisplayWidth = true; // Start Guards
\r
253 preventChangingWidth = true;
\r
255 updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;
\r
256 updownParWidth.Value = updownDisplayWidth.Value;
\r
257 updownParHeight.Value = text_width.Value;
\r
259 preventChangingWidth = false; // Reset Guards
\r
260 preventChangingDisplayWidth = false;
\r
265 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
269 preventChangingHeight = false;
\r
272 private void CheckKeepArCheckedChanged(object sender, EventArgs e)
\r
274 // Force TextWidth to recalc height
\r
275 if (check_KeepAR.Checked)
\r
276 TextWidthValueChanged(this, new EventArgs());
\r
278 // Disable the Custom Anamorphic Par Controls if checked.
\r
279 if (drp_anamorphic.SelectedIndex == 3)
\r
281 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
282 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
285 // Raise the Picture Settings Changed Event
\r
286 if (PictureSettingsChanged != null)
\r
287 PictureSettingsChanged(this, new EventArgs());
\r
290 private void UpdownDisplayWidthValueChanged(object sender, EventArgs e)
\r
292 if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)
\r
294 preventChangingCustom = true;
\r
295 updownParWidth.Value = updownDisplayWidth.Value;
\r
296 updownParHeight.Value = text_width.Value;
\r
297 preventChangingCustom = false;
\r
300 if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)
\r
302 // - Changes HEIGHT to keep DAR
\r
303 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
304 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
305 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
307 // Calculate new Height Value
\r
309 if (!int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus))
\r
312 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / cachedDar);
\r
313 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);
\r
316 preventChangingHeight = true;
\r
317 text_height.Value = (decimal)modulusHeight;
\r
318 updownParWidth.Value = updownDisplayWidth.Value;
\r
319 updownParHeight.Value = text_width.Value;
\r
320 preventChangingHeight = false;
\r
324 // Anamorphic Controls
\r
325 private void DrpAnamorphicSelectedIndexChanged(object sender, EventArgs e)
\r
327 switch (drp_anamorphic.SelectedIndex)
\r
330 text_width.Enabled = true;
\r
331 text_height.Enabled = true;
\r
332 check_KeepAR.Enabled = true;
\r
334 SetCustomAnamorphicOptionsVisible(false);
\r
335 labelStaticDisplaySize.Visible = false;
\r
336 labelDisplaySize.Visible = false;
\r
337 drp_modulus.Visible = true;
\r
338 lbl_modulus.Visible = true;
\r
340 // check_KeepAR.Checked = true;
\r
342 if (check_KeepAR.Checked)
\r
343 TextWidthValueChanged(this, new EventArgs());
\r
344 // Don't update display size if we're not using anamorphic
\r
347 text_width.Enabled = false;
\r
348 text_height.Enabled = false;
\r
349 check_KeepAR.Enabled = false;
\r
351 SetCustomAnamorphicOptionsVisible(false);
\r
352 labelStaticDisplaySize.Visible = true;
\r
353 labelDisplaySize.Visible = true;
\r
355 check_KeepAR.Checked = true;
\r
358 text_width.Enabled = true;
\r
359 text_height.Enabled = false;
\r
360 check_KeepAR.Enabled = false;
\r
362 SetCustomAnamorphicOptionsVisible(false);
\r
363 labelStaticDisplaySize.Visible = true;
\r
364 labelDisplaySize.Visible = true;
\r
365 drp_modulus.Visible = true;
\r
366 lbl_modulus.Visible = true;
\r
368 check_KeepAR.Checked = true;
\r
371 text_width.Enabled = true;
\r
372 text_height.Enabled = true;
\r
373 check_KeepAR.Enabled = true;
\r
375 SetCustomAnamorphicOptionsVisible(true);
\r
376 labelStaticDisplaySize.Visible = true;
\r
377 labelDisplaySize.Visible = true;
\r
379 check_KeepAR.Checked = true;
\r
380 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
381 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
385 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
387 if (check_KeepAR.Checked)
\r
388 TextWidthValueChanged(this, new EventArgs());
\r
390 if (PictureSettingsChanged != null)
\r
391 PictureSettingsChanged(this, new EventArgs());
\r
394 private void DrpModulusSelectedIndexChanged(object sender, EventArgs e)
\r
396 preventChangingWidth = true;
\r
397 preventChangingHeight = true;
\r
399 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
400 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
402 preventChangingWidth = false;
\r
403 preventChangingHeight = false;
\r
405 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
406 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
408 if (PictureSettingsChanged != null)
\r
409 PictureSettingsChanged(this, new EventArgs());
\r
412 // Cropping Controls
\r
413 private void CheckAutoCropCheckedChanged(object sender, EventArgs e)
\r
415 crop_top.Enabled = check_customCrop.Checked;
\r
416 crop_bottom.Enabled = check_customCrop.Checked;
\r
417 crop_left.Enabled = check_customCrop.Checked;
\r
418 crop_right.Enabled = check_customCrop.Checked;
\r
420 if (Source != null)
\r
422 crop_top.Value = Source.AutoCropDimensions.Top;
\r
423 crop_bottom.Value = Source.AutoCropDimensions.Bottom;
\r
424 crop_left.Value = Source.AutoCropDimensions.Left;
\r
425 crop_right.Value = Source.AutoCropDimensions.Right;
\r
429 private void CropValueChanged(object sender, EventArgs e)
\r
431 TextWidthValueChanged(this, new EventArgs());
\r
435 private void SetCustomAnamorphicOptionsVisible(bool visible)
\r
437 lbl_modulus.Visible = visible;
\r
438 lbl_displayWidth.Visible = visible;
\r
439 lbl_parWidth.Visible = visible;
\r
440 lbl_parHeight.Visible = visible;
\r
442 drp_modulus.Visible = visible;
\r
443 updownDisplayWidth.Visible = visible;
\r
444 updownParWidth.Visible = visible;
\r
445 updownParHeight.Visible = visible;
\r
448 // Calculation Functions
\r
449 private Size SourceAspect
\r
453 if (Source != null) // display aspect = (width * par_width) / (height * par_height)
\r
454 return new Size((Source.ParVal.Width * Source.Resolution.Width),
\r
455 (Source.ParVal.Height * Source.Resolution.Height));
\r
457 return new Size(0, 0); // Fall over to 16:9 and hope for the best
\r
461 private Size CalculateAnamorphicSizes()
\r
463 if (Source != null)
\r
465 /* Set up some variables to make the math easier to follow. */
\r
466 int croppedWidth = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
467 int croppedHeight = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
468 double storageAspect = (double)croppedWidth / croppedHeight;
\r
470 /* Figure out what width the source would display at. */
\r
471 double sourceDisplayWidth = (double)croppedWidth * Source.ParVal.Width / Source.ParVal.Height;
\r
474 3 different ways of deciding output dimensions:
\r
475 - 1: Strict anamorphic, preserve source dimensions
\r
476 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
477 - 3: Power user anamorphic, specify everything
\r
479 double width, height;
\r
480 switch (drp_anamorphic.SelectedIndex)
\r
484 /* Strict anamorphic */
\r
485 double displayWidth = ((double)croppedWidth * Source.ParVal.Width / Source.ParVal.Height);
\r
486 displayWidth = Math.Round(displayWidth, 0);
\r
487 Size output = new Size((int)displayWidth, croppedHeight);
\r
490 /* "Loose" anamorphic.
\r
491 - Uses mod16-compliant dimensions,
\r
492 - Allows users to set the width
\r
494 width = (int)text_width.Value;
\r
495 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
497 height = (width / storageAspect) + 0.5;
\r
498 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
500 /* The film AR is the source's display width / cropped source height.
\r
501 The output display width is the output height * film AR.
\r
502 The output PAR is the output display width / output storage width. */
\r
503 double pixelAspectWidth = height * sourceDisplayWidth / croppedHeight;
\r
504 double pixelAspectHeight = width;
\r
506 double disWidthLoose = (width * pixelAspectWidth / pixelAspectHeight);
\r
507 if (double.IsNaN(disWidthLoose))
\r
509 return new Size((int)disWidthLoose, (int)height);
\r
512 // Get the User Interface Values
\r
513 double UIdisplayWidth;
\r
514 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
516 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
517 height = GetModulusValue((double)text_height.Value);
\r
519 if (check_KeepAR.Checked)
\r
520 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
522 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
526 // Return a default value of 0,0 to indicate failure
\r
527 return new Size(0, 0);
\r
530 private double GetModulusValue(double value)
\r
532 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
533 double remainder = value % mod;
\r
535 if (remainder == 0)
\r
538 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
541 private static int GetCropMod2Clean(int value)
\r
543 int remainder = value % 2;
\r
544 if (remainder == 0) return value;
\r
545 return (value + remainder);
\r