OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Controls / PictureSettings.cs
1 using System;\r
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
7 \r
8 namespace Handbrake.Controls\r
9 {\r
10     public partial class PictureSettings : UserControl\r
11     {\r
12         private readonly CultureInfo Culture = new CultureInfo("en-US", false);\r
13         public event EventHandler PictureSettingsChanged;\r
14 \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
19 \r
20         public PictureSettings()\r
21         {\r
22             InitializeComponent();\r
23 \r
24             drp_anamorphic.SelectedIndex = 1;\r
25             drp_modulus.SelectedIndex = 0;\r
26         }\r
27 \r
28         /// <summary>\r
29         /// Gets or sets the source media used by this control.\r
30         /// </summary>\r
31         [Browsable(false)]\r
32         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
33         public Title Source\r
34         {\r
35             private get { return _sourceTitle; }\r
36             set\r
37             {\r
38                 _sourceTitle = value;\r
39                 Enabled = _sourceTitle != null;\r
40 \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
44 \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
50 \r
51                 // Set the Resolution Boxes\r
52                 if (drp_anamorphic.SelectedIndex == 0)\r
53                 {\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
56 \r
57                     check_KeepAR.Checked = true; // Forces Resolution to be correct.\r
58                 }\r
59                 else\r
60                 {\r
61                     if (text_width.Value == 0 && text_height.Value == 0)// Only update the values if the fields don't already have values.\r
62                     {\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
65                     }\r
66 \r
67                     labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
68                 }\r
69 \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
74             }\r
75         }\r
76 \r
77         /// <summary>\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
80         /// </summary>\r
81         [Browsable(false)]\r
82         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
83         public Size PresetMaximumResolution\r
84         {\r
85             get { return new Size(_presetMaximumWidth, _presetMaximumHeight); }\r
86             set\r
87             {\r
88                 _presetMaximumWidth = value.Width;\r
89                 _presetMaximumHeight = value.Height;\r
90 \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
97                 else\r
98                     lbl_max.Text = "";\r
99             }\r
100         }\r
101 \r
102         // Picture Controls\r
103         private void text_width_ValueChanged(object sender, EventArgs e)\r
104         {\r
105             if (Properties.Settings.Default.disableResCalc)\r
106                 return;\r
107 \r
108             if (_preventChangingWidth)\r
109                 return;\r
110 \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
115 \r
116             switch (drp_anamorphic.SelectedIndex)\r
117             {\r
118                 case 0:\r
119                     if (check_KeepAR.Checked && Source != null)\r
120                     {\r
121                         _preventChangingHeight = true;\r
122 \r
123                         int width = (int)text_width.Value;\r
124 \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
127 \r
128                         if (SourceAspect.Width == 0 && SourceAspect.Height == 0)\r
129                             break;\r
130 \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
134 \r
135                         _preventChangingHeight = false;\r
136                     }\r
137                     break;\r
138                 case 3:\r
139                     if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)\r
140                     {\r
141                         if (_preventChangingCustom)\r
142                             break;\r
143 \r
144                         _preventChangingDisplayWidth = true;\r
145                         updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;\r
146                         _preventChangingDisplayWidth = false;\r
147 \r
148                         labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
149                     }\r
150 \r
151                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
152                     {\r
153                         updownParWidth.Value = updownDisplayWidth.Value;\r
154                         updownParHeight.Value = text_width.Value;\r
155                     }\r
156                     break;\r
157                 default:\r
158                     labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
159                     break;\r
160             }\r
161 \r
162             _preventChangingWidth = false;\r
163         }\r
164         private void text_height_ValueChanged(object sender, EventArgs e)\r
165         {\r
166             if (Properties.Settings.Default.disableResCalc)\r
167                 return;\r
168 \r
169             if (_preventChangingHeight)\r
170                 return;\r
171 \r
172             if (Source != null)\r
173                 if (text_height.Value > Source.Resolution.Height)\r
174                     text_height.Value = Source.Resolution.Height;\r
175 \r
176             switch (drp_anamorphic.SelectedIndex)\r
177             {\r
178                 case 0:\r
179                     if (check_KeepAR.Checked && Source != null)\r
180                     {\r
181                         _preventChangingWidth = true;\r
182 \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
185 \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
188 \r
189                         text_width.Value = (decimal)GetModulusValue(new_width);\r
190 \r
191                         _preventChangingWidth = false;\r
192                     }\r
193                     break;\r
194                 case 3:\r
195                     labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
196 \r
197                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
198                     {\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
203 \r
204                         double rawCalculatedDisplayWidth = (double)text_height.Value * _cachedDar;\r
205 \r
206                         _preventChangingDisplayWidth = true; // Start Guards\r
207                         _preventChangingWidth = true;\r
208 \r
209                         updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;\r
210                         updownParWidth.Value = updownDisplayWidth.Value;\r
211                         updownParHeight.Value = text_width.Value;\r
212 \r
213                         _preventChangingWidth = false; // Reset Guards\r
214                         _preventChangingDisplayWidth = false;\r
215                     }\r
216 \r
217                     break;\r
218                 default:\r
219                     labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
220                     break;\r
221             }\r
222 \r
223             _preventChangingHeight = false;\r
224         }\r
225         private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
226         {\r
227             if (Properties.Settings.Default.disableResCalc)\r
228                 return;\r
229 \r
230             //Force TextWidth to recalc height\r
231             if (check_KeepAR.Checked)\r
232                 text_width_ValueChanged(this, new EventArgs());\r
233 \r
234             // Disable the Custom Anamorphic Par Controls if checked.\r
235             if (drp_anamorphic.SelectedIndex == 3)\r
236             {\r
237                 updownParWidth.Enabled = !check_KeepAR.Checked;\r
238                 updownParHeight.Enabled = !check_KeepAR.Checked;\r
239             }\r
240 \r
241             // Raise the Picture Settings Changed Event\r
242             if (PictureSettingsChanged != null)\r
243                 PictureSettingsChanged(this, new EventArgs());\r
244         }\r
245         private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)\r
246         {\r
247             if (Properties.Settings.Default.disableResCalc)\r
248                 return;\r
249 \r
250             if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)\r
251             {\r
252                 _preventChangingCustom = true;\r
253                 updownParWidth.Value = updownDisplayWidth.Value;\r
254                 updownParHeight.Value = text_width.Value;\r
255                 _preventChangingCustom = false;\r
256             }\r
257 \r
258             if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)\r
259             {\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
264 \r
265                 // Calculate new Height Value\r
266                 int modulus = 16;\r
267                 int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus);\r
268 \r
269                 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / _cachedDar);\r
270                 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);\r
271 \r
272                 // Update value\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
278             }\r
279 \r
280         }\r
281 \r
282         // Anamorphic Controls\r
283         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
284         {\r
285             switch (drp_anamorphic.SelectedIndex)\r
286             {\r
287                 case 0:\r
288                     text_width.Enabled = true;\r
289                     text_height.Enabled = true;\r
290                     check_KeepAR.Enabled = true;\r
291 \r
292                     SetCustomAnamorphicOptionsVisible(false);\r
293                     labelStaticDisplaySize.Visible = false;\r
294                     labelDisplaySize.Visible = false;\r
295 \r
296                     check_KeepAR.Checked = true;\r
297                     drp_modulus.SelectedIndex = 0;\r
298 \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
302                     return;\r
303                 case 1:\r
304                     text_width.Enabled = false;\r
305                     text_height.Enabled = false;\r
306                     check_KeepAR.Enabled = false;\r
307 \r
308                     SetCustomAnamorphicOptionsVisible(false);\r
309                     labelStaticDisplaySize.Visible = true;\r
310                     labelDisplaySize.Visible = true;\r
311                     drp_modulus.SelectedIndex = 0;\r
312 \r
313                     check_KeepAR.Checked = true;\r
314                     break;\r
315                 case 2:\r
316                     text_width.Enabled = true;\r
317                     text_height.Enabled = false;\r
318                     check_KeepAR.Enabled = false;\r
319 \r
320                     SetCustomAnamorphicOptionsVisible(false);\r
321                     labelStaticDisplaySize.Visible = true;\r
322                     labelDisplaySize.Visible = true;\r
323                     drp_modulus.SelectedIndex = 0;\r
324 \r
325                     check_KeepAR.Checked = true;\r
326                     break;\r
327                 case 3:\r
328                     text_width.Enabled = true;\r
329                     text_height.Enabled = true;\r
330                     check_KeepAR.Enabled = true;\r
331 \r
332                     SetCustomAnamorphicOptionsVisible(true);\r
333                     labelStaticDisplaySize.Visible = true;\r
334                     labelDisplaySize.Visible = true;\r
335 \r
336                     check_KeepAR.Checked = true;\r
337                     break;\r
338             }\r
339 \r
340             labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
341 \r
342             if (check_KeepAR.Checked)\r
343                 text_width_ValueChanged(this, new EventArgs());\r
344 \r
345             if (PictureSettingsChanged != null)\r
346                 PictureSettingsChanged(this, new EventArgs());\r
347         }\r
348         private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
349         {\r
350             _preventChangingWidth = true;\r
351             _preventChangingHeight = true;\r
352 \r
353             text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
354             text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
355 \r
356             _preventChangingWidth = false;\r
357             _preventChangingHeight = false;\r
358 \r
359             text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
360             text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
361 \r
362             if (PictureSettingsChanged != null)\r
363                 PictureSettingsChanged(this, new EventArgs());\r
364         }\r
365 \r
366         // Cropping Controls\r
367         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
368         {\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
373         }\r
374         private void crop_ValueChanged(object sender, EventArgs e)\r
375         {\r
376             text_width_ValueChanged(this, new EventArgs());\r
377         }\r
378 \r
379         // GUI Functions\r
380         private void SetCustomAnamorphicOptionsVisible(bool visible)\r
381         {\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
386 \r
387             drp_modulus.Visible = visible;\r
388             updownDisplayWidth.Visible = visible;\r
389             updownParWidth.Visible = visible;\r
390             updownParHeight.Visible = visible;\r
391         }\r
392 \r
393         // Calculation Functions\r
394         private Size SourceAspect\r
395         {\r
396             get\r
397             {\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
400 \r
401                 return new Size(0, 0); // Fall over to 16:9 and hope for the best\r
402             }\r
403         }\r
404         private Size CalculateAnamorphicSizes()\r
405         {\r
406             if (Source != null)\r
407             {\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
412 \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
415 \r
416                 /*\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
421                   */\r
422                 double width, height;\r
423                 switch (drp_anamorphic.SelectedIndex)\r
424                 {\r
425                     default:\r
426                     case 1:\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
431                         return output;\r
432                     case 2:\r
433                         /* "Loose" anamorphic.\r
434                             - Uses mod16-compliant dimensions,\r
435                             - Allows users to set the width\r
436                         */\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
439 \r
440                         height = (width / storage_aspect) + 0.5;\r
441                         height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
442 \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
448 \r
449                         double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
450                         return new Size((int)disWidthLoose, (int)height);\r
451                     case 3:\r
452 \r
453                         // Get the User Interface Values\r
454                         double UIdisplayWidth;\r
455                         double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
456 \r
457                         /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
458                         height = GetModulusValue((double)text_height.Value);\r
459 \r
460                         if (check_KeepAR.Checked)\r
461                             return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
462 \r
463                         return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
464                 }\r
465             }\r
466 \r
467             // Return a default value of 0,0 to indicate failure\r
468             return new Size(0, 0);\r
469         }\r
470         private double GetModulusValue(double value)\r
471         {\r
472             int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
473             double remainder = value % mod;\r
474 \r
475             if (remainder == 0)\r
476                 return value;\r
477 \r
478             return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
479         }\r
480         private static int GetCropMod2Clean(int value)\r
481         {\r
482             int remainder = value % 2;\r
483             if (remainder == 0) return value;\r
484             return (value + remainder);\r
485         }\r
486     }\r
487 }