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             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 (preventChangingWidth)\r
106                 return;\r
107 \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
112 \r
113             switch (drp_anamorphic.SelectedIndex)\r
114             {\r
115                 case 0:\r
116                     if (check_KeepAR.Checked && Source != null)\r
117                     {\r
118                         preventChangingHeight = true;\r
119 \r
120                         int width = (int)text_width.Value;\r
121 \r
122                         double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
123                         double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
124                         double newHeight = (width * Source.Resolution.Width * sourceAspect.Height * crop_height) /\r
125                                            (Source.Resolution.Height * sourceAspect.Width * crop_width);\r
126                         text_height.Value = (decimal)GetModulusValue(newHeight);\r
127 \r
128                         preventChangingHeight = false;\r
129                     }\r
130                     break;\r
131                 case 3:\r
132                     if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)\r
133                     {\r
134                         if (preventChangingCustom)\r
135                             break;\r
136 \r
137                         preventChangingDisplayWidth = true;\r
138                         updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;\r
139                         preventChangingDisplayWidth = false;\r
140 \r
141                         labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
142                     }\r
143 \r
144                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
145                     {\r
146                         updownParWidth.Value = updownDisplayWidth.Value;\r
147                         updownParHeight.Value = text_width.Value;\r
148                     }\r
149                     break;\r
150                 default:\r
151                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
152                     break;\r
153             }\r
154 \r
155             preventChangingWidth = false;\r
156         }\r
157         private void text_height_ValueChanged(object sender, EventArgs e)\r
158         {\r
159             if (preventChangingHeight)\r
160                 return;\r
161 \r
162             if (Source != null)\r
163                 if (text_height.Value > Source.Resolution.Height)\r
164                     text_height.Value = Source.Resolution.Height;\r
165 \r
166             switch (drp_anamorphic.SelectedIndex)\r
167             {\r
168                 case 0:\r
169                     if (check_KeepAR.Checked && Source != null)\r
170                     {\r
171                         preventChangingWidth = true;\r
172 \r
173                         double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
174                         double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
175 \r
176                         double new_width = ((double)text_height.Value * Source.Resolution.Height * sourceAspect.Width * crop_width) /\r
177                                             (Source.Resolution.Width * sourceAspect.Height * crop_height);\r
178 \r
179                         text_width.Value = (decimal)GetModulusValue(new_width);\r
180 \r
181                         preventChangingWidth = false;\r
182                     }\r
183                     break;\r
184                 case 3:\r
185                     labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
186 \r
187                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
188                     {\r
189                         // - Changes DISPLAY WIDTH to keep DAR\r
190                         // - Changes PIXEL WIDTH to new DISPLAY WIDTH\r
191                         // - Changes PIXEL HEIGHT to STORAGE WIDTH\r
192                         // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
193 \r
194                         double rawCalculatedDisplayWidth = (double)text_height.Value * cachedDar;\r
195 \r
196                         preventChangingDisplayWidth = true; // Start Guards\r
197                         preventChangingWidth = true;\r
198 \r
199                         updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;\r
200                         updownParWidth.Value = updownDisplayWidth.Value;\r
201                         updownParHeight.Value = text_width.Value;\r
202 \r
203                         preventChangingWidth = false; // Reset Guards\r
204                         preventChangingDisplayWidth = false;\r
205                     }\r
206 \r
207                     break;\r
208                 default:\r
209                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
210                     break;\r
211             }\r
212 \r
213             preventChangingHeight = false;\r
214         }\r
215         private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
216         {\r
217             //Force TextWidth to recalc height\r
218             if (check_KeepAR.Checked)\r
219                 text_width_ValueChanged(this, new EventArgs());\r
220 \r
221             // Disable the Custom Anamorphic Par Controls if checked.\r
222             if (drp_anamorphic.SelectedIndex == 3)\r
223             {\r
224                 updownParWidth.Enabled = !check_KeepAR.Checked;\r
225                 updownParHeight.Enabled = !check_KeepAR.Checked;\r
226             }\r
227 \r
228             // Raise the Picture Settings Changed Event\r
229             if (PictureSettingsChanged != null)\r
230                 PictureSettingsChanged(this, new EventArgs());\r
231         }\r
232         private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)\r
233         {\r
234             if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)\r
235             {\r
236                 preventChangingCustom = true;\r
237                 updownParWidth.Value = updownDisplayWidth.Value;\r
238                 updownParHeight.Value = text_width.Value;\r
239                 preventChangingCustom = false;\r
240             }\r
241 \r
242             if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Checked)\r
243             {\r
244                 // - Changes HEIGHT to keep DAR\r
245                 // - Changes PIXEL WIDTH to new DISPLAY WIDTH\r
246                 // - Changes PIXEL HEIGHT to STORAGE WIDTH\r
247                 // DAR = DISPLAY WIDTH / DISPLAY HEIGHT (cache after every modification)\r
248 \r
249                 // Calculate new Height Value\r
250                 int modulus = 16;\r
251                 int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus);\r
252 \r
253                 int rawCalculatedHeight = (int)((int)updownDisplayWidth.Value / cachedDar);\r
254                 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);\r
255 \r
256                 // Update value\r
257                 preventChangingHeight = true;\r
258                 text_height.Value = (decimal)modulusHeight;\r
259                 updownParWidth.Value = updownDisplayWidth.Value;\r
260                 updownParHeight.Value = text_width.Value;\r
261                 preventChangingHeight = false;\r
262             }\r
263 \r
264         }\r
265 \r
266         // Anamorphic Controls\r
267         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
268         {\r
269             switch (drp_anamorphic.SelectedIndex)\r
270             {\r
271                 case 0:\r
272                     text_width.Enabled = true;\r
273                     text_height.Enabled = true;\r
274                     check_KeepAR.Enabled = true;\r
275 \r
276                     setCustomAnamorphicOptionsVisible(false);\r
277                     labelStaticDisplaySize.Visible = false;\r
278                     labelDisplaySize.Visible = false;\r
279 \r
280                     check_KeepAR.Checked = true;\r
281                     drp_modulus.SelectedIndex = 0;\r
282 \r
283                     if (check_KeepAR.Checked)\r
284                         text_width_ValueChanged(this, new EventArgs());\r
285                     // Don't update display size if we're not using anamorphic\r
286                     return;\r
287                 case 1:\r
288                     text_width.Enabled = false;\r
289                     text_height.Enabled = false;\r
290                     check_KeepAR.Enabled = false;\r
291 \r
292                     setCustomAnamorphicOptionsVisible(false);\r
293                     labelStaticDisplaySize.Visible = true;\r
294                     labelDisplaySize.Visible = true;\r
295                     drp_modulus.SelectedIndex = 0;\r
296 \r
297                     check_KeepAR.Checked = true;\r
298                     break;\r
299                 case 2:\r
300                     text_width.Enabled = true;\r
301                     text_height.Enabled = false;\r
302                     check_KeepAR.Enabled = false;\r
303 \r
304                     setCustomAnamorphicOptionsVisible(false);\r
305                     labelStaticDisplaySize.Visible = true;\r
306                     labelDisplaySize.Visible = true;\r
307                     drp_modulus.SelectedIndex = 0;\r
308 \r
309                     check_KeepAR.Checked = true;\r
310                     break;\r
311                 case 3:\r
312                     text_width.Enabled = true;\r
313                     text_height.Enabled = true;\r
314                     check_KeepAR.Enabled = true;\r
315 \r
316                     setCustomAnamorphicOptionsVisible(true);\r
317                     labelStaticDisplaySize.Visible = true;\r
318                     labelDisplaySize.Visible = true;\r
319 \r
320                     check_KeepAR.Checked = true;\r
321                     break;\r
322             }\r
323 \r
324             labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
325 \r
326             if (check_KeepAR.Checked)\r
327                 text_width_ValueChanged(this, new EventArgs());\r
328 \r
329             if (PictureSettingsChanged != null)\r
330                 PictureSettingsChanged(this, new EventArgs());\r
331         }\r
332         private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
333         {\r
334             preventChangingWidth = true;\r
335             preventChangingHeight = true;\r
336 \r
337             text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
338             text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
339 \r
340             preventChangingWidth = false;\r
341             preventChangingHeight = false;\r
342 \r
343             text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
344             text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
345 \r
346             if (PictureSettingsChanged != null)\r
347                 PictureSettingsChanged(this, new EventArgs());\r
348         }\r
349 \r
350         // Cropping Controls\r
351         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
352         {\r
353             crop_top.Enabled = check_customCrop.Checked;\r
354             crop_bottom.Enabled = check_customCrop.Checked;\r
355             crop_left.Enabled = check_customCrop.Checked;\r
356             crop_right.Enabled = check_customCrop.Checked;\r
357         }\r
358         private void crop_ValueChanged(object sender, EventArgs e)\r
359         {\r
360             text_width_ValueChanged(this, new EventArgs());\r
361         }\r
362 \r
363         // GUI Functions\r
364         private void setCustomAnamorphicOptionsVisible(bool visible)\r
365         {\r
366             lbl_modulus.Visible = visible;\r
367             lbl_displayWidth.Visible = visible;\r
368             lbl_parWidth.Visible = visible;\r
369             lbl_parHeight.Visible = visible;\r
370 \r
371             drp_modulus.Visible = visible;\r
372             updownDisplayWidth.Visible = visible;\r
373             updownParWidth.Visible = visible;\r
374             updownParHeight.Visible = visible;\r
375         }\r
376 \r
377         // Calculation Functions\r
378         private Size sourceAspect\r
379         {\r
380             get\r
381             {\r
382                 if (Source != null)\r
383                 {\r
384                     if (Source.AspectRatio == 1.78F)\r
385                         return new Size(16, 9);\r
386                     if (Source.AspectRatio == 1.33F)\r
387                         return new Size(4, 3);\r
388                 }\r
389                 return new Size(0, 0);\r
390             }\r
391         }\r
392         private Size calculateAnamorphicSizes()\r
393         {\r
394             if (Source != null)\r
395             {\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
400 \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
403 \r
404                 /*\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
409                   */\r
410                 double width, height;\r
411                 switch (drp_anamorphic.SelectedIndex)\r
412                 {\r
413                     case 1:\r
414                         /* Strict anamorphic */\r
415                         double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);\r
416                         displayWidth = Math.Round(displayWidth, 0);\r
417                         Size output = new Size((int)displayWidth, cropped_height);\r
418                         return output;\r
419                     case 2:\r
420                         /* "Loose" anamorphic.\r
421                             - Uses mod16-compliant dimensions,\r
422                             - Allows users to set the width\r
423                         */\r
424                         width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
425                         width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/\r
426 \r
427                         height = (width / storage_aspect) + 0.5;\r
428                         height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
429 \r
430                         /* The film AR is the source's display width / cropped source height.\r
431                            The output display width is the output height * film AR.\r
432                            The output PAR is the output display width / output storage width. */\r
433                         double pixel_aspect_width = height * source_display_width / cropped_height;\r
434                         double pixel_aspect_height = width;\r
435 \r
436                         double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
437                         return new Size((int)disWidthLoose, (int)height);\r
438                     case 3:\r
439 \r
440                         // Get the User Interface Values\r
441                         double UIdisplayWidth;\r
442                         double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
443 \r
444                         /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
445                         height = GetModulusValue((double)text_height.Value);\r
446 \r
447                         if (check_KeepAR.Checked)\r
448                             return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
449 \r
450                         return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
451                 }\r
452             }\r
453 \r
454             // Return a default value of 0,0 to indicate failure\r
455             return new Size(0, 0);\r
456         }\r
457         private double GetModulusValue(double value)\r
458         {\r
459             int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
460             double remainder = value % mod;\r
461 \r
462             if (remainder == 0)\r
463                 return value;\r
464 \r
465             return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
466         }\r
467         private static int GetCropMod2Clean(int value)\r
468         {\r
469             int remainder = value % 2;\r
470             if (remainder == 0) return value;\r
471             return (value + remainder);\r
472         }\r
473     }\r
474 }