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;
\r
16 private int _PresetMaximumWidth, _PresetMaximumHeight;
\r
17 private Title _SourceTitle;
\r
19 public PictureSettings()
\r
21 InitializeComponent();
\r
23 drp_anamorphic.SelectedIndex = 1;
\r
24 drp_modulus.SelectedIndex = 0;
\r
28 /// Gets or sets the source media used by this control.
\r
31 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
34 get { return _SourceTitle; }
\r
37 _SourceTitle = value;
\r
38 Enabled = _SourceTitle != null;
\r
40 // Set the Aspect Ratio
\r
41 lbl_Aspect.Text = _SourceTitle.AspectRatio.ToString(Culture);
\r
42 lbl_src_res.Text = _SourceTitle.Resolution.Width + " x " + _SourceTitle.Resolution.Height;
\r
44 // Set the Recommended Cropping values
\r
45 crop_top.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[0]);
\r
46 crop_bottom.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[1]);
\r
47 crop_left.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[2]);
\r
48 crop_right.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[3]);
\r
50 // Set the Resolution Boxes
\r
51 text_width.Value = _SourceTitle.Resolution.Width;
\r
53 if (drp_anamorphic.SelectedIndex == 0)
\r
55 int width = _SourceTitle.Resolution.Width;
\r
57 double crop_width = _SourceTitle.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
58 double crop_height = _SourceTitle.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
59 double newHeight = (width * _SourceTitle.Resolution.Width * sourceAspect.Height * crop_height) / (_SourceTitle.Resolution.Height * sourceAspect.Width * crop_width);
\r
60 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
64 text_height.Value = _SourceTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
65 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
68 updownParWidth.Value = _SourceTitle.ParVal.Width;
\r
69 updownParHeight.Value = _SourceTitle.ParVal.Height;
\r
70 updownDisplayWidth.Value = calculateAnamorphicSizes().Width;
\r
76 /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to
\r
77 /// "0" if the maximum does not matter.
\r
80 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
81 public Size PresetMaximumResolution
\r
83 get { return new Size(_PresetMaximumWidth, _PresetMaximumHeight); }
\r
86 _PresetMaximumWidth = value.Width;
\r
87 _PresetMaximumHeight = value.Height;
\r
89 if (value.Width != 0 && value.Height != 0)
\r
90 lbl_max.Text = "Max Width / Height";
\r
91 else if (value.Width != 0)
\r
92 lbl_max.Text = "Max Width";
\r
93 else if (value.Height != 0)
\r
94 lbl_max.Text = "Max Height";
\r
100 // Picture Controls
\r
101 private void text_width_ValueChanged(object sender, EventArgs e)
\r
103 if (preventChangingWidth)
\r
106 // Make sure the new value doesn't exceed the maximum
\r
107 if (Source != null)
\r
108 if (text_width.Value > Source.Resolution.Width)
\r
109 text_width.Value = Source.Resolution.Width;
\r
111 switch (drp_anamorphic.SelectedIndex)
\r
114 if (check_KeepAR.Checked && Source != null)
\r
116 preventChangingHeight = true;
\r
118 int width = (int)text_width.Value;
\r
120 double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
121 double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
122 double newHeight = (width * Source.Resolution.Width * sourceAspect.Height * crop_height) /
\r
123 (Source.Resolution.Height * sourceAspect.Width * crop_width);
\r
124 text_height.Value = (decimal)GetModulusValue(newHeight);
\r
126 preventChangingHeight = false;
\r
130 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
134 preventChangingWidth = false;
\r
136 private void text_height_ValueChanged(object sender, EventArgs e)
\r
138 if (preventChangingHeight)
\r
141 if (Source != null)
\r
142 if (text_height.Value > Source.Resolution.Height)
\r
143 text_height.Value = Source.Resolution.Height;
\r
145 switch (drp_anamorphic.SelectedIndex)
\r
148 if (check_KeepAR.Checked && Source != null)
\r
150 preventChangingWidth = true;
\r
152 double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;
\r
153 double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;
\r
155 double new_width = ((double)text_height.Value * Source.Resolution.Height * sourceAspect.Width * crop_width) /
\r
156 (Source.Resolution.Width * sourceAspect.Height * crop_height);
\r
158 text_width.Value = (decimal)GetModulusValue(new_width);
\r
160 preventChangingWidth = false;
\r
164 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
168 preventChangingHeight = false;
\r
170 private void check_KeepAR_CheckedChanged(object sender, EventArgs e)
\r
172 //Force TextWidth to recalc height
\r
173 if (check_KeepAR.Checked)
\r
174 text_width_ValueChanged(this, new EventArgs());
\r
176 // Disable the Custom Anamorphic Par Controls if checked.
\r
177 if (drp_anamorphic.SelectedIndex == 3)
\r
179 updownParWidth.Enabled = !check_KeepAR.Checked;
\r
180 updownParHeight.Enabled = !check_KeepAR.Checked;
\r
183 // Raise the Picture Settings Changed Event
\r
184 if (PictureSettingsChanged != null)
\r
185 PictureSettingsChanged(this, new EventArgs());
\r
188 // Anamorphic Controls
\r
189 private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)
\r
191 switch (drp_anamorphic.SelectedIndex)
\r
194 text_width.Enabled = true;
\r
195 text_height.Enabled = true;
\r
196 check_KeepAR.Enabled = true;
\r
198 setCustomAnamorphicOptionsVisible(false);
\r
199 labelStaticDisplaySize.Visible = false;
\r
200 labelDisplaySize.Visible = false;
\r
202 check_KeepAR.Checked = true;
\r
203 drp_modulus.SelectedIndex = 0;
\r
205 if (check_KeepAR.Checked)
\r
206 text_width_ValueChanged(this, new EventArgs());
\r
207 // Don't update display size if we're not using anamorphic
\r
210 text_width.Enabled = false;
\r
211 text_height.Enabled = false;
\r
212 check_KeepAR.Enabled = false;
\r
214 setCustomAnamorphicOptionsVisible(false);
\r
215 labelStaticDisplaySize.Visible = true;
\r
216 labelDisplaySize.Visible = true;
\r
217 drp_modulus.SelectedIndex = 0;
\r
219 check_KeepAR.Checked = true;
\r
222 text_width.Enabled = true;
\r
223 text_height.Enabled = false;
\r
224 check_KeepAR.Enabled = false;
\r
226 setCustomAnamorphicOptionsVisible(false);
\r
227 labelStaticDisplaySize.Visible = true;
\r
228 labelDisplaySize.Visible = true;
\r
229 drp_modulus.SelectedIndex = 0;
\r
231 check_KeepAR.Checked = true;
\r
234 text_width.Enabled = true;
\r
235 text_height.Enabled = true;
\r
236 check_KeepAR.Enabled = true;
\r
238 setCustomAnamorphicOptionsVisible(true);
\r
239 labelStaticDisplaySize.Visible = true;
\r
240 labelDisplaySize.Visible = true;
\r
242 check_KeepAR.Checked = true;
\r
246 labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;
\r
248 if (check_KeepAR.Checked)
\r
249 text_width_ValueChanged(this, new EventArgs());
\r
251 if (PictureSettingsChanged != null)
\r
252 PictureSettingsChanged(this, new EventArgs());
\r
254 private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)
\r
256 preventChangingWidth = true;
\r
257 preventChangingHeight = true;
\r
259 text_width.Value = (decimal)GetModulusValue((double)text_width.Value);
\r
260 text_height.Value = (decimal)GetModulusValue((double)text_height.Value);
\r
262 preventChangingWidth = false;
\r
263 preventChangingHeight = false;
\r
265 text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
266 text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());
\r
268 if (PictureSettingsChanged != null)
\r
269 PictureSettingsChanged(this, new EventArgs());
\r
272 // Cropping Controls
\r
273 private void check_autoCrop_CheckedChanged(object sender, EventArgs e)
\r
275 crop_top.Enabled = check_customCrop.Checked;
\r
276 crop_bottom.Enabled = check_customCrop.Checked;
\r
277 crop_left.Enabled = check_customCrop.Checked;
\r
278 crop_right.Enabled = check_customCrop.Checked;
\r
280 private void crop_ValueChanged(object sender, EventArgs e)
\r
282 text_width_ValueChanged(this, new EventArgs());
\r
286 private void setCustomAnamorphicOptionsVisible(bool visible)
\r
288 lbl_modulus.Visible = visible;
\r
289 lbl_displayWidth.Visible = visible;
\r
290 lbl_parWidth.Visible = visible;
\r
291 lbl_parHeight.Visible = visible;
\r
293 drp_modulus.Visible = visible;
\r
294 updownDisplayWidth.Visible = visible;
\r
295 updownParWidth.Visible = visible;
\r
296 updownParHeight.Visible = visible;
\r
299 // Calculation Functions
\r
300 private Size sourceAspect
\r
304 if (Source != null)
\r
306 if (Source.AspectRatio == 1.78F)
\r
307 return new Size(16, 9);
\r
308 if (Source.AspectRatio == 1.33F)
\r
309 return new Size(4, 3);
\r
311 return new Size(0, 0);
\r
314 private Size calculateAnamorphicSizes()
\r
316 if (Source != null)
\r
318 /* Set up some variables to make the math easier to follow. */
\r
319 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;
\r
320 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;
\r
321 double storage_aspect = (double)cropped_width / cropped_height;
\r
323 /* Figure out what width the source would display at. */
\r
324 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;
\r
327 3 different ways of deciding output dimensions:
\r
328 - 1: Strict anamorphic, preserve source dimensions
\r
329 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
\r
330 - 3: Power user anamorphic, specify everything
\r
332 double width, height;
\r
333 switch (drp_anamorphic.SelectedIndex)
\r
336 /* Strict anamorphic */
\r
337 double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);
\r
338 displayWidth = Math.Round(displayWidth, 0);
\r
339 Size output = new Size((int)displayWidth, cropped_height);
\r
342 /* "Loose" anamorphic.
\r
343 - Uses mod16-compliant dimensions,
\r
344 - Allows users to set the width
\r
346 width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;
\r
347 width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/
\r
349 height = (width / storage_aspect) + 0.5;
\r
350 height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/
\r
352 /* The film AR is the source's display width / cropped source height.
\r
353 The output display width is the output height * film AR.
\r
354 The output PAR is the output display width / output storage width. */
\r
355 double pixel_aspect_width = height * source_display_width / cropped_height;
\r
356 double pixel_aspect_height = width;
\r
358 double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);
\r
359 return new Size((int)disWidthLoose, (int)height);
\r
362 // Get the User Interface Values
\r
363 double UIdisplayWidth;
\r
364 double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);
\r
366 /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */
\r
367 height = GetModulusValue((double)text_height.Value);
\r
369 if (check_KeepAR.Checked)
\r
370 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
372 return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);
\r
376 // Return a default value of 0,0 to indicate failure
\r
377 return new Size(0, 0);
\r
379 private double GetModulusValue(double value)
\r
381 int mod = int.Parse(drp_modulus.SelectedItem.ToString());
\r
382 double remainder = value % mod;
\r
384 if (remainder == 0)
\r
387 return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;
\r
389 private static int GetCropMod2Clean(int value)
\r
391 int remainder = value % 2;
\r
392 if (remainder == 0) return value;
\r
393 return (value + remainder);
\r