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 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 text_width.Value = _SourceTitle.Resolution.Width;
\r
62 text_height.Value = _SourceTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
63 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
66 updownDisplayWidth.Value = calculateAnamorphicSizes().Width;
\r
67 updownParWidth.Value = _SourceTitle.ParVal.Width;
\r
68 updownParHeight.Value = _SourceTitle.ParVal.Height;
\r
69 cachedDar = (double)updownDisplayWidth.Value / (double)text_height.Value;
\r
74 /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to
\r
75 /// "0" if the maximum does not matter.
\r
78 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
79 public Size PresetMaximumResolution
\r
81 get { return new Size(_PresetMaximumWidth, _PresetMaximumHeight); }
\r
84 _PresetMaximumWidth = value.Width;
\r
85 _PresetMaximumHeight = value.Height;
\r
87 if (value.Width != 0 && value.Height != 0)
\r
88 lbl_max.Text = "Max Width / Height";
\r
89 else if (value.Width != 0)
\r
90 lbl_max.Text = "Max Width";
\r
91 else if (value.Height != 0)
\r
92 lbl_max.Text = "Max Height";
\r
99 private void text_width_ValueChanged(object sender, EventArgs e)
\r
101 if (preventChangingWidth)
\r
104 // Make sure the new value doesn't exceed the maximum
\r
105 if (Source != null)
\r
106 if (text_width.Value > Source.Resolution.Width)
\r
107 text_width.Value = Source.Resolution.Width;
\r
109 switch (drp_anamorphic.SelectedIndex)
\r
112 if (check_KeepAR.Checked && Source != null)
\r
114 preventChangingHeight = true;
\r
116 int width = (int)text_width.Value;
\r
118 double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
119 double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
120 double newHeight = (width * Source.Resolution.Width * sourceAspect.Height * crop_height) /
\r
121 (Source.Resolution.Height * sourceAspect.Width * crop_width);
\r
122 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
124 preventChangingHeight = false;
\r
128 if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)
\r
130 if (preventChangingCustom)
\r
133 preventChangingDisplayWidth = true;
\r
134 updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;
\r
135 preventChangingDisplayWidth = false;
\r
137 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
140 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
142 updownParWidth.Value = updownDisplayWidth.Value;
\r
143 updownParHeight.Value = text_width.Value;
\r
147 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
151 preventChangingWidth = false;
\r
153 private void text_height_ValueChanged(object sender, EventArgs e)
\r
155 if (preventChangingHeight)
\r
158 if (Source != null)
\r
159 if (text_height.Value > Source.Resolution.Height)
\r
160 text_height.Value = Source.Resolution.Height;
\r
162 switch (drp_anamorphic.SelectedIndex)
\r
165 if (check_KeepAR.Checked && Source != null)
\r
167 preventChangingWidth = true;
\r
169 double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
170 double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
172 double new_width = ((double)text_height.Value * Source.Resolution.Height * sourceAspect.Width * crop_width) /
\r
173 (Source.Resolution.Width * sourceAspect.Height * crop_height);
\r
175 text_width.Value = (decimal)GetModulusValue(new_width);
\r
177 preventChangingWidth = false;
\r
181 labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;
\r
183 if (check_KeepAR.CheckState == CheckState.Checked && Source != null)
\r
185 // - Changes DISPLAY WIDTH to keep DAR
\r
186 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
187 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
188 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
190 double rawCalculatedDisplayWidth = (double)text_height.Value * cachedDar;
\r
192 preventChangingDisplayWidth = true; // Start Guards
\r
193 preventChangingWidth = true;
\r
195 updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;
\r
196 updownParWidth.Value = updownDisplayWidth.Value;
\r
197 updownParHeight.Value = text_width.Value;
\r
199 preventChangingWidth = false; // Reset Guards
\r
200 preventChangingDisplayWidth = false;
\r
205 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
209 preventChangingHeight = false;
\r
211 private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
\r
213 //Force TextWidth to recalc height
\r
214 if (check_KeepAR.Checked)
\r
215 text_width_ValueChanged(this, new EventArgs());
\r
217 // Disable the Custom Anamorphic Par Controls if checked.
\r
218 if (drp_anamorphic.SelectedIndex == 3)
\r
220 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
221 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
224 // Raise the Picture Settings Changed Event
\r
225 if (PictureSettingsChanged != null)
\r
226 PictureSettingsChanged(this, new EventArgs());
\r
228 private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)
\r
230 if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)
\r
232 preventChangingCustom = true;
\r
233 updownParWidth.Value = updownDisplayWidth.Value;
\r
234 updownParHeight.Value = text_width.Value;
\r
235 preventChangingCustom = false;
\r
238 if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)
\r
240 // - Changes HEIGHT to keep DAR
\r
241 // - Changes PIXEL WIDTH to new DISPLAY WIDTH
\r
242 // - Changes PIXEL HEIGHT to STORAGE WIDTH
\r
243 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)
\r
245 // Calculate new Height Value
\r
247 int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus);
\r
249 int rawCalculatedHeight = (int) ((int)updownDisplayWidth.Value/cachedDar);
\r
250 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);
\r
253 preventChangingHeight = true;
\r
254 text_height.Value = (decimal)modulusHeight;
\r
255 updownParWidth.Value = updownDisplayWidth.Value;
\r
256 updownParHeight.Value = text_width.Value;
\r
257 preventChangingHeight = false;
\r
262 // Anamorphic Controls
\r
263 private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
\r
265 switch (drp_anamorphic.SelectedIndex)
\r
268 text_width.Enabled = true;
\r
269 text_height.Enabled = true;
\r
270 check_KeepAR.Enabled = true;
\r
272 setCustomAnamorphicOptionsVisible(false);
\r
273 labelStaticDisplaySize.Visible = false;
\r
274 labelDisplaySize.Visible = false;
\r
276 check_KeepAR.Checked = true;
\r
277 drp_modulus.SelectedIndex = 0;
\r
279 if (check_KeepAR.Checked)
\r
280 text_width_ValueChanged(this, new EventArgs());
\r
281 // Don't update display size if we're not using anamorphic
\r
284 text_width.Enabled = false;
\r
285 text_height.Enabled = false;
\r
286 check_KeepAR.Enabled = false;
\r
288 setCustomAnamorphicOptionsVisible(false);
\r
289 labelStaticDisplaySize.Visible = true;
\r
290 labelDisplaySize.Visible = true;
\r
291 drp_modulus.SelectedIndex = 0;
\r
293 check_KeepAR.Checked = true;
\r
296 text_width.Enabled = true;
\r
297 text_height.Enabled = false;
\r
298 check_KeepAR.Enabled = false;
\r
300 setCustomAnamorphicOptionsVisible(false);
\r
301 labelStaticDisplaySize.Visible = true;
\r
302 labelDisplaySize.Visible = true;
\r
303 drp_modulus.SelectedIndex = 0;
\r
305 check_KeepAR.Checked = true;
\r
308 text_width.Enabled = true;
\r
309 text_height.Enabled = true;
\r
310 check_KeepAR.Enabled = true;
\r
312 setCustomAnamorphicOptionsVisible(true);
\r
313 labelStaticDisplaySize.Visible = true;
\r
314 labelDisplaySize.Visible = true;
\r
316 check_KeepAR.Checked = true;
\r
320 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
322 if (check_KeepAR.Checked)
\r
323 text_width_ValueChanged(this, new EventArgs());
\r
325 if (PictureSettingsChanged != null)
\r
326 PictureSettingsChanged(this, new EventArgs());
\r
328 private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
\r
330 preventChangingWidth = true;
\r
331 preventChangingHeight = true;
\r
333 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
334 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
336 preventChangingWidth = false;
\r
337 preventChangingHeight = false;
\r
339 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
340 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
342 if (PictureSettingsChanged != null)
\r
343 PictureSettingsChanged(this, new EventArgs());
\r
346 // Cropping Controls
\r
347 private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
\r
349 crop_top.Enabled = check_customCrop.Checked;
\r
350 crop_bottom.Enabled = check_customCrop.Checked;
\r
351 crop_left.Enabled = check_customCrop.Checked;
\r
352 crop_right.Enabled = check_customCrop.Checked;
\r
354 private void crop_ValueChanged(object sender, EventArgs e)
\r
356 text_width_ValueChanged(this, new EventArgs());
\r
360 private void setCustomAnamorphicOptionsVisible(bool visible)
\r
362 lbl_modulus.Visible = visible;
\r
363 lbl_displayWidth.Visible = visible;
\r
364 lbl_parWidth.Visible = visible;
\r
365 lbl_parHeight.Visible = visible;
\r
367 drp_modulus.Visible = visible;
\r
368 updownDisplayWidth.Visible = visible;
\r
369 updownParWidth.Visible = visible;
\r
370 updownParHeight.Visible = visible;
\r
373 // Calculation Functions
\r
374 private Size sourceAspect
\r
378 if (Source != null)
\r
380 if (Source.AspectRatio == 1.78F)
\r
381 return new Size(16, 9);
\r
382 if (Source.AspectRatio == 1.33F)
\r
383 return new Size(4, 3);
\r
385 return new Size(0, 0);
\r
388 private Size calculateAnamorphicSizes()
\r
390 if (Source != null)
\r
392 /* Set up some variables to make the math easier to follow. */
\r
393 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
394 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
395 double storage_aspect = (double)cropped_width / cropped_height;
\r
397 /* Figure out what width the source would display at. */
\r
398 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;
\r
401 3 different ways of deciding output dimensions:
\r
402 - 1: Strict anamorphic, preserve source dimensions
\r
403 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
404 - 3: Power user anamorphic, specify everything
\r
406 double width, height;
\r
407 switch (drp_anamorphic.SelectedIndex)
\r
410 /* Strict anamorphic */
\r
411 double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);
\r
412 displayWidth = Math.Round(displayWidth, 0);
\r
413 Size output = new Size((int)displayWidth, cropped_height);
\r
416 /* "Loose" anamorphic.
\r
417 - Uses mod16-compliant dimensions,
\r
418 - Allows users to set the width
\r
420 width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
\r
421 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
423 height = (width / storage_aspect) + 0.5;
\r
424 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
426 /* The film AR is the source's display width / cropped source height.
\r
427 The output display width is the output height * film AR.
\r
428 The output PAR is the output display width / output storage width. */
\r
429 double pixel_aspect_width = height * source_display_width / cropped_height;
\r
430 double pixel_aspect_height = width;
\r
432 double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);
\r
433 return new Size((int)disWidthLoose, (int)height);
\r
436 // Get the User Interface Values
\r
437 double UIdisplayWidth;
\r
438 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
440 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
441 height = GetModulusValue((double)text_height.Value);
\r
443 if (check_KeepAR.Checked)
\r
444 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
446 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
450 // Return a default value of 0,0 to indicate failure
\r
451 return new Size(0, 0);
\r
453 private double GetModulusValue(double value)
\r
455 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
456 double remainder = value % mod;
\r
458 if (remainder == 0)
\r
461 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
463 private static int GetCropMod2Clean(int value)
\r
465 int remainder = value % 2;
\r
466 if (remainder == 0) return value;
\r
467 return (value + remainder);
\r