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 (_preventChangingWidth)
\r
108 // Make sure the new value doesn't exceed the maximum
\r
109 if (Source != null)
\r
110 if (text_width.Value > Source.Resolution.Width)
\r
111 text_width.Value = Source.Resolution.Width;
\r
113 switch (drp_anamorphic.SelectedIndex)
\r
116 if (check_KeepAR.Checked && Source != null)
\r
118 _preventChangingHeight = true;
\r
120 int width = (int)text_width.Value;
\r
122 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
123 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
125 if (SourceAspect.Width == 0 && SourceAspect.Height == 0)
\r
128 double newHeight = ((double)width * Source.Resolution.Width * SourceAspect.Height * crop_height) /
\r
129 (Source.Resolution.Height * SourceAspect.Width * crop_width);
\r
130 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
132 _preventChangingHeight = false;
\r
136 if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)
\r
138 if (_preventChangingCustom)
\r
141 _preventChangingDisplayWidth = true;
\r
142 updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;
\r
143 _preventChangingDisplayWidth = false;
\r
145 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
148 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
150 updownParWidth.Value = updownDisplayWidth.Value;
\r
151 updownParHeight.Value = text_width.Value;
\r
155 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
159 _preventChangingWidth = false;
\r
161 private void text_height_ValueChanged(object sender, EventArgs e)
\r
163 if (_preventChangingHeight)
\r
166 if (Source != null)
\r
167 if (text_height.Value > Source.Resolution.Height)
\r
168 text_height.Value = Source.Resolution.Height;
\r
170 switch (drp_anamorphic.SelectedIndex)
\r
173 if (check_KeepAR.Checked && Source != null)
\r
175 _preventChangingWidth = true;
\r
177 double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
178 double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
180 double new_width = ((double)text_height.Value * Source.Resolution.Height * SourceAspect.Width * crop_width) /
\r
181 (Source.Resolution.Width * SourceAspect.Height * crop_height);
\r
183 text_width.Value = (decimal)GetModulusValue(new_width);
\r
185 _preventChangingWidth = false;
\r
189 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
191 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
193 // - Changes DISPLAY WIDTH to keep DAR
\r
194 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
195 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
196 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
198 double rawCalculatedDisplayWidth = (double)text_height.Value * _cachedDar;
\r
200 _preventChangingDisplayWidth = true; // Start Guards
\r
201 _preventChangingWidth = true;
\r
203 updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;
\r
204 updownParWidth.Value = updownDisplayWidth.Value;
\r
205 updownParHeight.Value = text_width.Value;
\r
207 _preventChangingWidth = false; // Reset Guards
\r
208 _preventChangingDisplayWidth = false;
\r
213 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
217 _preventChangingHeight = false;
\r
219 private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
\r
221 //Force TextWidth to recalc height
\r
222 if (check_KeepAR.Checked)
\r
223 text_width_ValueChanged(this, new EventArgs());
\r
225 // Disable the Custom Anamorphic Par Controls if checked.
\r
226 if (drp_anamorphic.SelectedIndex == 3)
\r
228 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
229 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
232 // Raise the Picture Settings Changed Event
\r
233 if (PictureSettingsChanged != null)
\r
234 PictureSettingsChanged(this, new EventArgs());
\r
236 private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)
\r
238 if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)
\r
240 _preventChangingCustom = true;
\r
241 updownParWidth.Value = updownDisplayWidth.Value;
\r
242 updownParHeight.Value = text_width.Value;
\r
243 _preventChangingCustom = false;
\r
246 if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)
\r
248 // - Changes HEIGHT to keep DAR
\r
249 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
250 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
251 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
253 // Calculate new Height Value
\r
255 int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus);
\r
257 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / _cachedDar);
\r
258 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);
\r
261 _preventChangingHeight = true;
\r
262 text_height.Value = (decimal)modulusHeight;
\r
263 updownParWidth.Value = updownDisplayWidth.Value;
\r
264 updownParHeight.Value = text_width.Value;
\r
265 _preventChangingHeight = false;
\r
270 // Anamorphic Controls
\r
271 private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
\r
273 switch (drp_anamorphic.SelectedIndex)
\r
276 text_width.Enabled = true;
\r
277 text_height.Enabled = true;
\r
278 check_KeepAR.Enabled = true;
\r
280 SetCustomAnamorphicOptionsVisible(false);
\r
281 labelStaticDisplaySize.Visible = false;
\r
282 labelDisplaySize.Visible = false;
\r
284 check_KeepAR.Checked = true;
\r
285 drp_modulus.SelectedIndex = 0;
\r
287 if (check_KeepAR.Checked)
\r
288 text_width_ValueChanged(this, new EventArgs());
\r
289 // Don't update display size if we're not using anamorphic
\r
292 text_width.Enabled = false;
\r
293 text_height.Enabled = false;
\r
294 check_KeepAR.Enabled = false;
\r
296 SetCustomAnamorphicOptionsVisible(false);
\r
297 labelStaticDisplaySize.Visible = true;
\r
298 labelDisplaySize.Visible = true;
\r
299 drp_modulus.SelectedIndex = 0;
\r
301 check_KeepAR.Checked = true;
\r
304 text_width.Enabled = true;
\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 = true;
\r
318 check_KeepAR.Enabled = true;
\r
320 SetCustomAnamorphicOptionsVisible(true);
\r
321 labelStaticDisplaySize.Visible = true;
\r
322 labelDisplaySize.Visible = true;
\r
324 check_KeepAR.Checked = true;
\r
328 labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;
\r
330 if (check_KeepAR.Checked)
\r
331 text_width_ValueChanged(this, new EventArgs());
\r
333 if (PictureSettingsChanged != null)
\r
334 PictureSettingsChanged(this, new EventArgs());
\r
336 private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
\r
338 _preventChangingWidth = true;
\r
339 _preventChangingHeight = true;
\r
341 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
342 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
344 _preventChangingWidth = false;
\r
345 _preventChangingHeight = false;
\r
347 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
348 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
350 if (PictureSettingsChanged != null)
\r
351 PictureSettingsChanged(this, new EventArgs());
\r
354 // Cropping Controls
\r
355 private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
\r
357 crop_top.Enabled = check_customCrop.Checked;
\r
358 crop_bottom.Enabled = check_customCrop.Checked;
\r
359 crop_left.Enabled = check_customCrop.Checked;
\r
360 crop_right.Enabled = check_customCrop.Checked;
\r
362 private void crop_ValueChanged(object sender, EventArgs e)
\r
364 text_width_ValueChanged(this, new EventArgs());
\r
368 private void SetCustomAnamorphicOptionsVisible(bool visible)
\r
370 lbl_modulus.Visible = visible;
\r
371 lbl_displayWidth.Visible = visible;
\r
372 lbl_parWidth.Visible = visible;
\r
373 lbl_parHeight.Visible = visible;
\r
375 drp_modulus.Visible = visible;
\r
376 updownDisplayWidth.Visible = visible;
\r
377 updownParWidth.Visible = visible;
\r
378 updownParHeight.Visible = visible;
\r
381 // Calculation Functions
\r
382 private Size SourceAspect
\r
386 if (Source != null) // display aspect = (width * par_width) / (height * par_height)
\r
387 return new Size((Source.ParVal.Width * Source.Resolution.Width), (Source.ParVal.Height * Source.Resolution.Height));
\r
389 return new Size(0, 0); // Fall over to 16:9 and hope for the best
\r
392 private Size CalculateAnamorphicSizes()
\r
394 if (Source != null)
\r
396 /* Set up some variables to make the math easier to follow. */
\r
397 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
398 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
399 double storage_aspect = (double)cropped_width / cropped_height;
\r
401 /* Figure out what width the source would display at. */
\r
402 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;
\r
405 3 different ways of deciding output dimensions:
\r
406 - 1: Strict anamorphic, preserve source dimensions
\r
407 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
408 - 3: Power user anamorphic, specify everything
\r
410 double width, height;
\r
411 switch (drp_anamorphic.SelectedIndex)
\r
415 /* Strict anamorphic */
\r
416 double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);
\r
417 displayWidth = Math.Round(displayWidth, 0);
\r
418 Size output = new Size((int)displayWidth, cropped_height);
\r
421 /* "Loose" anamorphic.
\r
422 - Uses mod16-compliant dimensions,
\r
423 - Allows users to set the width
\r
425 width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
\r
426 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
428 height = (width / storage_aspect) + 0.5;
\r
429 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
431 /* The film AR is the source's display width / cropped source height.
\r
432 The output display width is the output height * film AR.
\r
433 The output PAR is the output display width / output storage width. */
\r
434 double pixel_aspect_width = height * source_display_width / cropped_height;
\r
435 double pixel_aspect_height = width;
\r
437 double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);
\r
438 return new Size((int)disWidthLoose, (int)height);
\r
441 // Get the User Interface Values
\r
442 double UIdisplayWidth;
\r
443 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
445 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
446 height = GetModulusValue((double)text_height.Value);
\r
448 if (check_KeepAR.Checked)
\r
449 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
451 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
455 // Return a default value of 0,0 to indicate failure
\r
456 return new Size(0, 0);
\r
458 private double GetModulusValue(double value)
\r
460 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
461 double remainder = value % mod;
\r
463 if (remainder == 0)
\r
466 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
468 private static int GetCropMod2Clean(int value)
\r
470 int remainder = value % 2;
\r
471 if (remainder == 0) return value;
\r
472 return (value + remainder);
\r