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
78 /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to
\r
79 /// "0" if the maximum does not matter.
\r
82 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
83 public Size PresetMaximumResolution
\r
85 get { return new Size(_presetMaximumWidth, _presetMaximumHeight); }
\r
88 _presetMaximumWidth = value.Width;
\r
89 _presetMaximumHeight = value.Height;
\r
91 if (value.Width != 0 && value.Height != 0)
\r
92 lbl_max.Text = "Max Width / Height";
\r
93 else if (value.Width != 0)
\r
94 lbl_max.Text = "Max Width";
\r
95 else if (value.Height != 0)
\r
96 lbl_max.Text = "Max Height";
\r
102 // Picture Controls
\r
103 private void text_width_ValueChanged(object sender, EventArgs e)
\r
105 if (Properties.Settings.Default.disableResCalc)
\r
108 if (_preventChangingWidth)
\r
111 // Make sure the new value doesn't exceed the maximum
\r
112 if (Source != null)
\r
113 if (text_width.Value > Source.Resolution.Width)
\r
114 text_width.Value = Source.Resolution.Width;
\r
116 switch (drp_anamorphic.SelectedIndex)
\r
119 if (check_KeepAR.Checked && Source != null)
\r
121 _preventChangingHeight = true;
\r
123 int width = (int)text_width.Value;
\r
125 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
126 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
128 if (SourceAspect.Width == 0 && SourceAspect.Height == 0)
\r
131 double newHeight = ((double)width * Source.Resolution.Width * SourceAspect.Height * crop_height) /
\r
132 (Source.Resolution.Height * SourceAspect.Width * crop_width);
\r
133 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
135 _preventChangingHeight = false;
\r
139 if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)
\r
141 if (_preventChangingCustom)
\r
144 _preventChangingDisplayWidth = true;
\r
145 updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;
\r
146 _preventChangingDisplayWidth = false;
\r
148 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
151 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
153 updownParWidth.Value = updownDisplayWidth.Value;
\r
154 updownParHeight.Value = text_width.Value;
\r
158 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
162 _preventChangingWidth = false;
\r
164 private void text_height_ValueChanged(object sender, EventArgs e)
\r
166 if (Properties.Settings.Default.disableResCalc)
\r
169 if (_preventChangingHeight)
\r
172 if (Source != null)
\r
173 if (text_height.Value > Source.Resolution.Height)
\r
174 text_height.Value = Source.Resolution.Height;
\r
176 switch (drp_anamorphic.SelectedIndex)
\r
179 if (check_KeepAR.Checked && Source != null)
\r
181 _preventChangingWidth = true;
\r
183 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
184 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
186 double new_width = ((double)text_height.Value * Source.Resolution.Height * SourceAspect.Width * crop_width) /
\r
187 (Source.Resolution.Width * SourceAspect.Height * crop_height);
\r
189 text_width.Value = (decimal)GetModulusValue(new_width);
\r
191 _preventChangingWidth = false;
\r
195 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
197 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
199 // - Changes DISPLAY WIDTH to keep DAR
\r
200 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
201 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
202 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
204 double rawCalculatedDisplayWidth = (double)text_height.Value * _cachedDar;
\r
206 _preventChangingDisplayWidth = true; // Start Guards
\r
207 _preventChangingWidth = true;
\r
209 updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;
\r
210 updownParWidth.Value = updownDisplayWidth.Value;
\r
211 updownParHeight.Value = text_width.Value;
\r
213 _preventChangingWidth = false; // Reset Guards
\r
214 _preventChangingDisplayWidth = false;
\r
219 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
223 _preventChangingHeight = false;
\r
225 private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
\r
227 if (Properties.Settings.Default.disableResCalc)
\r
230 //Force TextWidth to recalc height
\r
231 if (check_KeepAR.Checked)
\r
232 text_width_ValueChanged(this, new EventArgs());
\r
234 // Disable the Custom Anamorphic Par Controls if checked.
\r
235 if (drp_anamorphic.SelectedIndex == 3)
\r
237 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
238 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
241 // Raise the Picture Settings Changed Event
\r
242 if (PictureSettingsChanged != null)
\r
243 PictureSettingsChanged(this, new EventArgs());
\r
245 private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)
\r
247 if (Properties.Settings.Default.disableResCalc)
\r
250 if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)
\r
252 _preventChangingCustom = true;
\r
253 updownParWidth.Value = updownDisplayWidth.Value;
\r
254 updownParHeight.Value = text_width.Value;
\r
255 _preventChangingCustom = false;
\r
258 if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)
\r
260 // - Changes HEIGHT to keep DAR
\r
261 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
262 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
263 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
265 // Calculate new Height Value
\r
267 int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus);
\r
269 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / _cachedDar);
\r
270 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);
\r
273 _preventChangingHeight = true;
\r
274 text_height.Value = (decimal)modulusHeight;
\r
275 updownParWidth.Value = updownDisplayWidth.Value;
\r
276 updownParHeight.Value = text_width.Value;
\r
277 _preventChangingHeight = false;
\r
282 // Anamorphic Controls
\r
283 private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
\r
285 switch (drp_anamorphic.SelectedIndex)
\r
288 text_width.Enabled = true;
\r
289 text_height.Enabled = true;
\r
290 check_KeepAR.Enabled = true;
\r
292 SetCustomAnamorphicOptionsVisible(false);
\r
293 labelStaticDisplaySize.Visible = false;
\r
294 labelDisplaySize.Visible = false;
\r
296 check_KeepAR.Checked = true;
\r
297 drp_modulus.SelectedIndex = 0;
\r
299 if (check_KeepAR.Checked)
\r
300 text_width_ValueChanged(this, new EventArgs());
\r
301 // Don't update display size if we're not using anamorphic
\r
304 text_width.Enabled = false;
\r
305 text_height.Enabled = false;
\r
306 check_KeepAR.Enabled = false;
\r
308 SetCustomAnamorphicOptionsVisible(false);
\r
309 labelStaticDisplaySize.Visible = true;
\r
310 labelDisplaySize.Visible = true;
\r
311 drp_modulus.SelectedIndex = 0;
\r
313 check_KeepAR.Checked = true;
\r
316 text_width.Enabled = true;
\r
317 text_height.Enabled = false;
\r
318 check_KeepAR.Enabled = false;
\r
320 SetCustomAnamorphicOptionsVisible(false);
\r
321 labelStaticDisplaySize.Visible = true;
\r
322 labelDisplaySize.Visible = true;
\r
323 drp_modulus.SelectedIndex = 0;
\r
325 check_KeepAR.Checked = true;
\r
328 text_width.Enabled = true;
\r
329 text_height.Enabled = true;
\r
330 check_KeepAR.Enabled = true;
\r
332 SetCustomAnamorphicOptionsVisible(true);
\r
333 labelStaticDisplaySize.Visible = true;
\r
334 labelDisplaySize.Visible = true;
\r
336 check_KeepAR.Checked = true;
\r
340 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
342 if (check_KeepAR.Checked)
\r
343 text_width_ValueChanged(this, new EventArgs());
\r
345 if (PictureSettingsChanged != null)
\r
346 PictureSettingsChanged(this, new EventArgs());
\r
348 private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
\r
350 _preventChangingWidth = true;
\r
351 _preventChangingHeight = true;
\r
353 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
354 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
356 _preventChangingWidth = false;
\r
357 _preventChangingHeight = false;
\r
359 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
360 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
362 if (PictureSettingsChanged != null)
\r
363 PictureSettingsChanged(this, new EventArgs());
\r
366 // Cropping Controls
\r
367 private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
\r
369 crop_top.Enabled = check_customCrop.Checked;
\r
370 crop_bottom.Enabled = check_customCrop.Checked;
\r
371 crop_left.Enabled = check_customCrop.Checked;
\r
372 crop_right.Enabled = check_customCrop.Checked;
\r
374 private void crop_ValueChanged(object sender, EventArgs e)
\r
376 text_width_ValueChanged(this, new EventArgs());
\r
380 private void SetCustomAnamorphicOptionsVisible(bool visible)
\r
382 lbl_modulus.Visible = visible;
\r
383 lbl_displayWidth.Visible = visible;
\r
384 lbl_parWidth.Visible = visible;
\r
385 lbl_parHeight.Visible = visible;
\r
387 drp_modulus.Visible = visible;
\r
388 updownDisplayWidth.Visible = visible;
\r
389 updownParWidth.Visible = visible;
\r
390 updownParHeight.Visible = visible;
\r
393 // Calculation Functions
\r
394 private Size SourceAspect
\r
398 if (Source != null) // display aspect = (width * par_width) / (height * par_height)
\r
399 return new Size((Source.ParVal.Width * Source.Resolution.Width), (Source.ParVal.Height * Source.Resolution.Height));
\r
401 return new Size(0, 0); // Fall over to 16:9 and hope for the best
\r
404 private Size CalculateAnamorphicSizes()
\r
406 if (Source != null)
\r
408 /* Set up some variables to make the math easier to follow. */
\r
409 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
410 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
411 double storage_aspect = (double)cropped_width / cropped_height;
\r
413 /* Figure out what width the source would display at. */
\r
414 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;
\r
417 3 different ways of deciding output dimensions:
\r
418 - 1: Strict anamorphic, preserve source dimensions
\r
419 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
420 - 3: Power user anamorphic, specify everything
\r
422 double width, height;
\r
423 switch (drp_anamorphic.SelectedIndex)
\r
427 /* Strict anamorphic */
\r
428 double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);
\r
429 displayWidth = Math.Round(displayWidth, 0);
\r
430 Size output = new Size((int)displayWidth, cropped_height);
\r
433 /* "Loose" anamorphic.
\r
434 - Uses mod16-compliant dimensions,
\r
435 - Allows users to set the width
\r
437 width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
\r
438 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
440 height = (width / storage_aspect) + 0.5;
\r
441 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
443 /* The film AR is the source's display width / cropped source height.
\r
444 The output display width is the output height * film AR.
\r
445 The output PAR is the output display width / output storage width. */
\r
446 double pixel_aspect_width = height * source_display_width / cropped_height;
\r
447 double pixel_aspect_height = width;
\r
449 double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);
\r
450 return new Size((int)disWidthLoose, (int)height);
\r
453 // Get the User Interface Values
\r
454 double UIdisplayWidth;
\r
455 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
457 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
458 height = GetModulusValue((double)text_height.Value);
\r
460 if (check_KeepAR.Checked)
\r
461 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
463 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
467 // Return a default value of 0,0 to indicate failure
\r
468 return new Size(0, 0);
\r
470 private double GetModulusValue(double value)
\r
472 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
473 double remainder = value % mod;
\r
475 if (remainder == 0)
\r
478 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
480 private static int GetCropMod2Clean(int value)
\r
482 int remainder = value % 2;
\r
483 if (remainder == 0) return value;
\r
484 return (value + remainder);
\r