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                     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
64                 }\r
65 \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
70             }\r
71         }\r
72 \r
73         /// <summary>\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
76         /// </summary>\r
77         [Browsable(false)]\r
78         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
79         public Size PresetMaximumResolution\r
80         {\r
81             get { return new Size(_PresetMaximumWidth, _PresetMaximumHeight); }\r
82             set\r
83             {\r
84                 _PresetMaximumWidth = value.Width;\r
85                 _PresetMaximumHeight = value.Height;\r
86 \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
93                 else\r
94                     lbl_max.Text = "";\r
95             }\r
96         }\r
97 \r
98         // Picture Controls\r
99         private void text_width_ValueChanged(object sender, EventArgs e)\r
100         {\r
101             if (preventChangingWidth)\r
102                 return;\r
103 \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
108 \r
109             switch (drp_anamorphic.SelectedIndex)\r
110             {\r
111                 case 0:\r
112                     if (check_KeepAR.Checked && Source != null)\r
113                     {\r
114                         preventChangingHeight = true;\r
115 \r
116                         int width = (int)text_width.Value;\r
117 \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
123 \r
124                         preventChangingHeight = false;\r
125                     }\r
126                     break;\r
127                 case 3:\r
128                     if (check_KeepAR.CheckState == CheckState.Unchecked && Source != null)\r
129                     {\r
130                         if (preventChangingCustom)\r
131                             break;\r
132 \r
133                         preventChangingDisplayWidth = true; \r
134                         updownDisplayWidth.Value = text_width.Value * updownParWidth.Value / updownParHeight.Value;\r
135                         preventChangingDisplayWidth = false;\r
136 \r
137                         labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
138                     }\r
139 \r
140                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
141                     {\r
142                         updownParWidth.Value = updownDisplayWidth.Value;\r
143                         updownParHeight.Value = text_width.Value;\r
144                     }\r
145                     break;\r
146                 default:\r
147                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
148                     break;\r
149             }\r
150 \r
151             preventChangingWidth = false;\r
152         }\r
153         private void text_height_ValueChanged(object sender, EventArgs e)\r
154         {\r
155             if (preventChangingHeight)\r
156                 return;\r
157 \r
158             if (Source != null)\r
159                 if (text_height.Value > Source.Resolution.Height)\r
160                     text_height.Value = Source.Resolution.Height;\r
161 \r
162             switch (drp_anamorphic.SelectedIndex)\r
163             {\r
164                 case 0:\r
165                     if (check_KeepAR.Checked && Source != null)\r
166                     {\r
167                         preventChangingWidth = true;\r
168 \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
171 \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
174 \r
175                         text_width.Value = (decimal)GetModulusValue(new_width);\r
176 \r
177                         preventChangingWidth = false;\r
178                     }\r
179                     break;\r
180                 case 3:\r
181                     labelDisplaySize.Text = Math.Truncate(updownDisplayWidth.Value) + "x" + text_height.Value;\r
182 \r
183                     if (check_KeepAR.CheckState == CheckState.Checked && Source != null)\r
184                     {\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
189 \r
190                         double rawCalculatedDisplayWidth = (double)text_height.Value * cachedDar;\r
191 \r
192                         preventChangingDisplayWidth = true; // Start Guards\r
193                         preventChangingWidth = true;\r
194 \r
195                         updownDisplayWidth.Value = (decimal)rawCalculatedDisplayWidth;\r
196                         updownParWidth.Value = updownDisplayWidth.Value;\r
197                         updownParHeight.Value = text_width.Value;\r
198 \r
199                         preventChangingWidth = false; // Reset Guards\r
200                         preventChangingDisplayWidth = false;\r
201                     }\r
202 \r
203                     break;\r
204                 default:\r
205                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
206                     break;\r
207             }\r
208 \r
209             preventChangingHeight = false;\r
210         }\r
211         private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
212         {\r
213             //Force TextWidth to recalc height\r
214             if (check_KeepAR.Checked)\r
215                 text_width_ValueChanged(this, new EventArgs());\r
216 \r
217             // Disable the Custom Anamorphic Par Controls if checked.\r
218             if (drp_anamorphic.SelectedIndex == 3)\r
219             {\r
220                 updownParWidth.Enabled = !check_KeepAR.Checked;\r
221                 updownParHeight.Enabled = !check_KeepAR.Checked;\r
222             }\r
223 \r
224             // Raise the Picture Settings Changed Event\r
225             if (PictureSettingsChanged != null)\r
226                 PictureSettingsChanged(this, new EventArgs());\r
227         }\r
228         private void updownDisplayWidth_ValueChanged(object sender, EventArgs e)\r
229         {\r
230             if (preventChangingDisplayWidth == false && check_KeepAR.CheckState == CheckState.Unchecked)\r
231             {\r
232                 preventChangingCustom = true;\r
233                 updownParWidth.Value = updownDisplayWidth.Value;\r
234                 updownParHeight.Value = text_width.Value;\r
235                 preventChangingCustom = false;\r
236             }\r
237 \r
238             if (preventChangingDisplayWidth == false  && check_KeepAR.CheckState == CheckState.Checked)\r
239             {\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
244 \r
245                 // Calculate new Height Value\r
246                 int modulus = 16;\r
247                 int.TryParse(drp_modulus.SelectedItem.ToString(), out modulus);\r
248 \r
249                 int rawCalculatedHeight = (int) ((int)updownDisplayWidth.Value/cachedDar);\r
250                 int modulusHeight = rawCalculatedHeight - (rawCalculatedHeight % modulus);\r
251 \r
252                 // Update value\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
258             }\r
259            \r
260         }\r
261 \r
262         // Anamorphic Controls\r
263         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
264         {\r
265             switch (drp_anamorphic.SelectedIndex)\r
266             {\r
267                 case 0:\r
268                     text_width.Enabled = true;\r
269                     text_height.Enabled = true;\r
270                     check_KeepAR.Enabled = true;\r
271 \r
272                     setCustomAnamorphicOptionsVisible(false);\r
273                     labelStaticDisplaySize.Visible = false;\r
274                     labelDisplaySize.Visible = false;\r
275 \r
276                     check_KeepAR.Checked = true;\r
277                     drp_modulus.SelectedIndex = 0;\r
278 \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
282                     return;\r
283                 case 1:\r
284                     text_width.Enabled = false;\r
285                     text_height.Enabled = false;\r
286                     check_KeepAR.Enabled = false;\r
287 \r
288                     setCustomAnamorphicOptionsVisible(false);\r
289                     labelStaticDisplaySize.Visible = true;\r
290                     labelDisplaySize.Visible = true;\r
291                     drp_modulus.SelectedIndex = 0;\r
292 \r
293                     check_KeepAR.Checked = true;\r
294                     break;\r
295                 case 2:\r
296                     text_width.Enabled = true;\r
297                     text_height.Enabled = false;\r
298                     check_KeepAR.Enabled = false;\r
299 \r
300                     setCustomAnamorphicOptionsVisible(false);\r
301                     labelStaticDisplaySize.Visible = true;\r
302                     labelDisplaySize.Visible = true;\r
303                     drp_modulus.SelectedIndex = 0;\r
304 \r
305                     check_KeepAR.Checked = true;\r
306                     break;\r
307                 case 3:\r
308                     text_width.Enabled = true;\r
309                     text_height.Enabled = true;\r
310                     check_KeepAR.Enabled = true;\r
311 \r
312                     setCustomAnamorphicOptionsVisible(true);\r
313                     labelStaticDisplaySize.Visible = true;\r
314                     labelDisplaySize.Visible = true;\r
315 \r
316                     check_KeepAR.Checked = true;\r
317                     break;\r
318             }\r
319 \r
320             labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
321 \r
322             if (check_KeepAR.Checked)\r
323                 text_width_ValueChanged(this, new EventArgs());\r
324 \r
325             if (PictureSettingsChanged != null)\r
326                 PictureSettingsChanged(this, new EventArgs());\r
327         }\r
328         private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
329         {\r
330             preventChangingWidth = true;\r
331             preventChangingHeight = true;\r
332 \r
333             text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
334             text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
335 \r
336             preventChangingWidth = false;\r
337             preventChangingHeight = false;\r
338 \r
339             text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
340             text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
341 \r
342             if (PictureSettingsChanged != null)\r
343                 PictureSettingsChanged(this, new EventArgs());\r
344         }\r
345 \r
346         // Cropping Controls\r
347         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
348         {\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
353         }\r
354         private void crop_ValueChanged(object sender, EventArgs e)\r
355         {\r
356             text_width_ValueChanged(this, new EventArgs());\r
357         }\r
358 \r
359         // GUI Functions\r
360         private void setCustomAnamorphicOptionsVisible(bool visible)\r
361         {\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
366 \r
367             drp_modulus.Visible = visible;\r
368             updownDisplayWidth.Visible = visible;\r
369             updownParWidth.Visible = visible;\r
370             updownParHeight.Visible = visible;\r
371         }\r
372 \r
373         // Calculation Functions\r
374         private Size sourceAspect\r
375         {\r
376             get\r
377             {\r
378                 if (Source != null)\r
379                 {\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
384                 }\r
385                 return new Size(0, 0);\r
386             }\r
387         }\r
388         private Size calculateAnamorphicSizes()\r
389         {\r
390             if (Source != null)\r
391             {\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
396 \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
399 \r
400                 /*\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
405                   */\r
406                 double width, height;\r
407                 switch (drp_anamorphic.SelectedIndex)\r
408                 {\r
409                     case 1:\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
414                         return output;\r
415                     case 2:\r
416                         /* "Loose" anamorphic.\r
417                             - Uses mod16-compliant dimensions,\r
418                             - Allows users to set the width\r
419                         */\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
422 \r
423                         height = (width / storage_aspect) + 0.5;\r
424                         height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
425 \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
431 \r
432                         double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
433                         return new Size((int)disWidthLoose, (int)height);\r
434                     case 3:\r
435 \r
436                         // Get the User Interface Values\r
437                         double UIdisplayWidth;\r
438                         double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
439 \r
440                         /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
441                         height = GetModulusValue((double)text_height.Value);\r
442 \r
443                         if (check_KeepAR.Checked)\r
444                             return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
445 \r
446                         return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
447                 }\r
448             }\r
449 \r
450             // Return a default value of 0,0 to indicate failure\r
451             return new Size(0, 0);\r
452         }\r
453         private double GetModulusValue(double value)\r
454         {\r
455             int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
456             double remainder = value % mod;\r
457 \r
458             if (remainder == 0)\r
459                 return value;\r
460 \r
461             return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
462         }\r
463         private static int GetCropMod2Clean(int value)\r
464         {\r
465             int remainder = value % 2;\r
466             if (remainder == 0) return value;\r
467             return (value + remainder);\r
468         }\r
469     }\r
470 }