OSDN Git Service

LinGui: don't remove srt subs when changing from mkv to mp4
[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                 Size croppedDar = CalculateAnamorphicSizes();\r
77                 _cachedDar = (double) croppedDar.Width/croppedDar.Height;\r
78                 updownDisplayWidth.Value = croppedDar.Width;\r
79             }\r
80         }\r
81 \r
82         /// <summary>\r
83         /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to\r
84         /// "0" if the maximum does not matter.\r
85         /// </summary>\r
86         [Browsable(false)]\r
87         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
88         public Size PresetMaximumResolution\r
89         {\r
90             get { return new Size(_presetMaximumWidth, _presetMaximumHeight); }\r
91             set\r
92             {\r
93                 _presetMaximumWidth = value.Width;\r
94                 _presetMaximumHeight = value.Height;\r
95 \r
96                 if (value.Width != 0 && value.Height != 0)\r
97                     lbl_max.Text = "Max Width / Height";\r
98                 else if (value.Width != 0)\r
99                     lbl_max.Text = "Max Width";\r
100                 else if (value.Height != 0)\r
101                     lbl_max.Text = "Max Height";\r
102                 else\r
103                     lbl_max.Text = "";\r
104             }\r
105         }\r
106 \r
107         // Picture Controls\r
108         private void text_width_ValueChanged(object sender, EventArgs e)\r
109         {\r
110             if (Properties.Settings.Default.disableResCalc)\r
111                 return;\r
112 \r
113             if (_preventChangingWidth)\r
114                 return;\r
115 \r
116             // Make sure the new value doesn't exceed the maximum\r
117             if (Source != null)\r
118                 if (text_width.Value > Source.Resolution.Width)\r
119                     text_width.Value = Source.Resolution.Width;\r
120 \r
121             switch (drp_anamorphic.SelectedIndex)\r
122             {\r
123                 case 0:\r
124                     if (check_KeepAR.Checked && Source != null)\r
125                     {\r
126                         _preventChangingHeight = true;\r
127 \r
128                         int width = (int)text_width.Value;\r
129 \r
130                         double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
131                         double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
132 \r
133                         if (SourceAspect.Width == 0 && SourceAspect.Height == 0)\r
134                             break;\r
135 \r
136                         double newHeight = ((double)width * Source.Resolution.Width * SourceAspect.Height * crop_height) /\r
137                                            (Source.Resolution.Height * SourceAspect.Width * crop_width);\r
138                         text_height.Value = (decimal)GetModulusValue(newHeight);\r
139 \r
140                         _preventChangingHeight = false;\r
141                     }\r
142                     break;\r
143                 case 3:\r
144                     if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)\r
145                     {\r
146                         if (_preventChangingCustom)\r
147                             break;\r
148 \r
149                         _preventChangingDisplayWidth = true;\r
150                         updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;\r
151                         _preventChangingDisplayWidth = false;\r
152 \r
153                         labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
154                     }\r
155 \r
156                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
157                     {\r
158                         updownParWidth.Value = updownDisplayWidth.Value;\r
159                         updownParHeight.Value = text_width.Value;\r
160                     }\r
161                     break;\r
162                 default:\r
163                     labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
164                     break;\r
165             }\r
166 \r
167             _preventChangingWidth = false;\r
168         }\r
169         private void text_height_ValueChanged(object sender, EventArgs e)\r
170         {\r
171             if (Properties.Settings.Default.disableResCalc)\r
172                 return;\r
173 \r
174             if (_preventChangingHeight)\r
175                 return;\r
176 \r
177             if (Source != null)\r
178                 if (text_height.Value > Source.Resolution.Height)\r
179                     text_height.Value = Source.Resolution.Height;\r
180 \r
181             switch (drp_anamorphic.SelectedIndex)\r
182             {\r
183                 case 0:\r
184                     if (check_KeepAR.Checked && Source != null)\r
185                     {\r
186                         _preventChangingWidth = true;\r
187 \r
188                         double crop_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
189                         double crop_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
190 \r
191                         double new_width = ((double)text_height.Value * Source.Resolution.Height * SourceAspect.Width * crop_width) /\r
192                                             (Source.Resolution.Width * SourceAspect.Height * crop_height);\r
193 \r
194                         text_width.Value = (decimal)GetModulusValue(new_width);\r
195 \r
196                         _preventChangingWidth = false;\r
197                     }\r
198                     break;\r
199                 case 3:\r
200                     labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
201 \r
202                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
203                     {\r
204                         // - Changes DISPLAY WIDTH to keep DAR\r
205                         // - Changes PIXEL WIDTH to new DISPLAY WIDTH\r
206                         // - Changes PIXEL HEIGHT to STORAGE WIDTH\r
207                         // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
208 \r
209                         double rawCalculatedDisplayWidth = (double)text_height.Value * _cachedDar;\r
210 \r
211                         _preventChangingDisplayWidth = true; // Start Guards\r
212                         _preventChangingWidth = true;\r
213 \r
214                         updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;\r
215                         updownParWidth.Value = updownDisplayWidth.Value;\r
216                         updownParHeight.Value = text_width.Value;\r
217 \r
218                         _preventChangingWidth = false; // Reset Guards\r
219                         _preventChangingDisplayWidth = false;\r
220                     }\r
221 \r
222                     break;\r
223                 default:\r
224                     labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
225                     break;\r
226             }\r
227 \r
228             _preventChangingHeight = false;\r
229         }\r
230         private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
231         {\r
232             if (Properties.Settings.Default.disableResCalc)\r
233                 return;\r
234 \r
235             //Force TextWidth to recalc height\r
236             if (check_KeepAR.Checked)\r
237                 text_width_ValueChanged(this, new EventArgs());\r
238 \r
239             // Disable the Custom Anamorphic Par Controls if checked.\r
240             if (drp_anamorphic.SelectedIndex == 3)\r
241             {\r
242                 updownParWidth.Enabled = !check_KeepAR.Checked;\r
243                 updownParHeight.Enabled = !check_KeepAR.Checked;\r
244             }\r
245 \r
246             // Raise the Picture Settings Changed Event\r
247             if (PictureSettingsChanged != null)\r
248                 PictureSettingsChanged(this, new EventArgs());\r
249         }\r
250         private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)\r
251         {\r
252             if (Properties.Settings.Default.disableResCalc)\r
253                 return;\r
254 \r
255             if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)\r
256             {\r
257                 _preventChangingCustom = true;\r
258                 updownParWidth.Value = updownDisplayWidth.Value;\r
259                 updownParHeight.Value = text_width.Value;\r
260                 _preventChangingCustom = false;\r
261             }\r
262 \r
263             if (_preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)\r
264             {\r
265                 // - Changes HEIGHT to keep DAR\r
266                 // - Changes PIXEL WIDTH to new DISPLAY WIDTH\r
267                 // - Changes PIXEL HEIGHT to STORAGE WIDTH\r
268                 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
269 \r
270                 // Calculate new Height Value\r
271                 int modulus;\r
272                 if(!int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus))\r
273                     modulus = 16;\r
274 \r
275                 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / _cachedDar);\r
276                 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);\r
277 \r
278                 // Update value\r
279                 _preventChangingHeight = true;\r
280                 text_height.Value = (decimal)modulusHeight;\r
281                 updownParWidth.Value = updownDisplayWidth.Value;\r
282                 updownParHeight.Value = text_width.Value;\r
283                 _preventChangingHeight = false;\r
284             }\r
285 \r
286         }\r
287 \r
288         // Anamorphic Controls\r
289         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
290         {\r
291             switch (drp_anamorphic.SelectedIndex)\r
292             {\r
293                 case 0:\r
294                     text_width.Enabled = true;\r
295                     text_height.Enabled = true;\r
296                     check_KeepAR.Enabled = true;\r
297 \r
298                     SetCustomAnamorphicOptionsVisible(false);\r
299                     labelStaticDisplaySize.Visible = false;\r
300                     labelDisplaySize.Visible = false;\r
301 \r
302                     check_KeepAR.Checked = true;\r
303                     drp_modulus.SelectedIndex = 0;\r
304 \r
305                     if (check_KeepAR.Checked)\r
306                         text_width_ValueChanged(this, new EventArgs());\r
307                     // Don't update display size if we're not using anamorphic\r
308                     return;\r
309                 case 1:\r
310                     text_width.Enabled = false;\r
311                     text_height.Enabled = false;\r
312                     check_KeepAR.Enabled = false;\r
313 \r
314                     SetCustomAnamorphicOptionsVisible(false);\r
315                     labelStaticDisplaySize.Visible = true;\r
316                     labelDisplaySize.Visible = true;\r
317                     drp_modulus.SelectedIndex = 0;\r
318 \r
319                     check_KeepAR.Checked = true;\r
320                     break;\r
321                 case 2:\r
322                     text_width.Enabled = true;\r
323                     text_height.Enabled = false;\r
324                     check_KeepAR.Enabled = false;\r
325 \r
326                     SetCustomAnamorphicOptionsVisible(false);\r
327                     labelStaticDisplaySize.Visible = true;\r
328                     labelDisplaySize.Visible = true;\r
329                     drp_modulus.SelectedIndex = 0;\r
330 \r
331                     check_KeepAR.Checked = true;\r
332                     break;\r
333                 case 3:\r
334                     text_width.Enabled = true;\r
335                     text_height.Enabled = true;\r
336                     check_KeepAR.Enabled = true;\r
337 \r
338                     SetCustomAnamorphicOptionsVisible(true);\r
339                     labelStaticDisplaySize.Visible = true;\r
340                     labelDisplaySize.Visible = true;\r
341 \r
342                     check_KeepAR.Checked = true;\r
343                     break;\r
344             }\r
345 \r
346             labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
347 \r
348             if (check_KeepAR.Checked)\r
349                 text_width_ValueChanged(this, new EventArgs());\r
350 \r
351             if (PictureSettingsChanged != null)\r
352                 PictureSettingsChanged(this, new EventArgs());\r
353         }\r
354         private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
355         {\r
356             _preventChangingWidth = true;\r
357             _preventChangingHeight = true;\r
358 \r
359             text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
360             text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
361 \r
362             _preventChangingWidth = false;\r
363             _preventChangingHeight = false;\r
364 \r
365             text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
366             text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
367 \r
368             if (PictureSettingsChanged != null)\r
369                 PictureSettingsChanged(this, new EventArgs());\r
370         }\r
371 \r
372         // Cropping Controls\r
373         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
374         {\r
375             crop_top.Enabled = check_customCrop.Checked;\r
376             crop_bottom.Enabled = check_customCrop.Checked;\r
377             crop_left.Enabled = check_customCrop.Checked;\r
378             crop_right.Enabled = check_customCrop.Checked;\r
379         }\r
380         private void crop_ValueChanged(object sender, EventArgs e)\r
381         {\r
382             text_width_ValueChanged(this, new EventArgs());\r
383         }\r
384 \r
385         // GUI Functions\r
386         private void SetCustomAnamorphicOptionsVisible(bool visible)\r
387         {\r
388             lbl_modulus.Visible = visible;\r
389             lbl_displayWidth.Visible = visible;\r
390             lbl_parWidth.Visible = visible;\r
391             lbl_parHeight.Visible = visible;\r
392 \r
393             drp_modulus.Visible = visible;\r
394             updownDisplayWidth.Visible = visible;\r
395             updownParWidth.Visible = visible;\r
396             updownParHeight.Visible = visible;\r
397         }\r
398 \r
399         // Calculation Functions\r
400         private Size SourceAspect\r
401         {\r
402             get\r
403             {\r
404                 if (Source != null) // display aspect = (width * par_width) / (height * par_height)\r
405                     return new Size((Source.ParVal.Width * Source.Resolution.Width), (Source.ParVal.Height * Source.Resolution.Height));\r
406 \r
407                 return new Size(0, 0); // Fall over to 16:9 and hope for the best\r
408             }\r
409         }\r
410         private Size CalculateAnamorphicSizes()\r
411         {\r
412             if (Source != null)\r
413             {\r
414                 /* Set up some variables to make the math easier to follow. */\r
415                 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
416                 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
417                 double storage_aspect = (double)cropped_width / cropped_height;\r
418 \r
419                 /* Figure out what width the source would display at. */\r
420                 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;\r
421 \r
422                 /*\r
423                      3 different ways of deciding output dimensions:\r
424                       - 1: Strict anamorphic, preserve source dimensions\r
425                       - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio\r
426                       - 3: Power user anamorphic, specify everything\r
427                   */\r
428                 double width, height;\r
429                 switch (drp_anamorphic.SelectedIndex)\r
430                 {\r
431                     default:\r
432                     case 1:\r
433                         /* Strict anamorphic */\r
434                         double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);\r
435                         displayWidth = Math.Round(displayWidth, 0);\r
436                         Size output = new Size((int)displayWidth, cropped_height);\r
437                         return output;\r
438                     case 2:\r
439                         /* "Loose" anamorphic.\r
440                             - Uses mod16-compliant dimensions,\r
441                             - Allows users to set the width\r
442                         */\r
443                         width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
444                         width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/\r
445 \r
446                         height = (width / storage_aspect) + 0.5;\r
447                         height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
448 \r
449                         /* The film AR is the source's display width / cropped source height.\r
450                            The output display width is the output height * film AR.\r
451                            The output PAR is the output display width / output storage width. */\r
452                         double pixel_aspect_width = height * source_display_width / cropped_height;\r
453                         double pixel_aspect_height = width;\r
454 \r
455                         double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
456                         return new Size((int)disWidthLoose, (int)height);\r
457                     case 3:\r
458 \r
459                         // Get the User Interface Values\r
460                         double UIdisplayWidth;\r
461                         double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
462 \r
463                         /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
464                         height = GetModulusValue((double)text_height.Value);\r
465 \r
466                         if (check_KeepAR.Checked)\r
467                             return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
468 \r
469                         return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
470                 }\r
471             }\r
472 \r
473             // Return a default value of 0,0 to indicate failure\r
474             return new Size(0, 0);\r
475         }\r
476         private double GetModulusValue(double value)\r
477         {\r
478             int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
479             double remainder = value % mod;\r
480 \r
481             if (remainder == 0)\r
482                 return value;\r
483 \r
484             return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
485         }\r
486         private static int GetCropMod2Clean(int value)\r
487         {\r
488             int remainder = value % 2;\r
489             if (remainder == 0) return value;\r
490             return (value + remainder);\r
491         }\r
492 \r
493         // Hidden UI feature to drop the MaxWidth / Height with the MaxWidth/Height label is double clicked\r
494         private void lbl_max_DoubleClick(object sender, EventArgs e)\r
495         {\r
496             PresetMaximumResolution = new Size(0,0);\r
497             if (PictureSettingsChanged != null)\r
498                 PictureSettingsChanged(this, new EventArgs());\r
499         }\r
500     }\r
501 }