OSDN Git Service

translation Controls and EncodeQueue
[handbrake-jp/handbrake-jp.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 = "\8dÅ\91å\95\9d / \8d\82\82³";\r
98                 else if (value.Width != 0)\r
99                     lbl_max.Text = "\8dÅ\91å\95\9d";\r
100                 else if (value.Height != 0)\r
101                     lbl_max.Text = "\8dÅ\91å\8d\82\82³";\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                     updownParWidth.Enabled = !check_KeepAR.Checked;\r
344                     updownParHeight.Enabled = !check_KeepAR.Checked;\r
345                     break;\r
346 \r
347             }\r
348 \r
349             labelDisplaySize.Text = CalculateAnamorphicSizes().Width + "x" + CalculateAnamorphicSizes().Height;\r
350 \r
351             if (check_KeepAR.Checked)\r
352                 text_width_ValueChanged(this, new EventArgs());\r
353 \r
354             if (PictureSettingsChanged != null)\r
355                 PictureSettingsChanged(this, new EventArgs());\r
356         }\r
357         private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
358         {\r
359             _preventChangingWidth = true;\r
360             _preventChangingHeight = true;\r
361 \r
362             text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
363             text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
364 \r
365             _preventChangingWidth = false;\r
366             _preventChangingHeight = false;\r
367 \r
368             text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
369             text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
370 \r
371             if (PictureSettingsChanged != null)\r
372                 PictureSettingsChanged(this, new EventArgs());\r
373         }\r
374 \r
375         // Cropping Controls\r
376         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
377         {\r
378             crop_top.Enabled = check_customCrop.Checked;\r
379             crop_bottom.Enabled = check_customCrop.Checked;\r
380             crop_left.Enabled = check_customCrop.Checked;\r
381             crop_right.Enabled = check_customCrop.Checked;\r
382 \r
383             crop_top.Value = Source.AutoCropDimensions[0];\r
384             crop_bottom.Value = Source.AutoCropDimensions[1];\r
385             crop_left.Value = Source.AutoCropDimensions[2];\r
386             crop_right.Value = Source.AutoCropDimensions[3];\r
387         }\r
388         private void crop_ValueChanged(object sender, EventArgs e)\r
389         {\r
390             text_width_ValueChanged(this, new EventArgs());\r
391         }\r
392 \r
393         // GUI Functions\r
394         private void SetCustomAnamorphicOptionsVisible(bool visible)\r
395         {\r
396             lbl_modulus.Visible = visible;\r
397             lbl_displayWidth.Visible = visible;\r
398             lbl_parWidth.Visible = visible;\r
399             lbl_parHeight.Visible = visible;\r
400 \r
401             drp_modulus.Visible = visible;\r
402             updownDisplayWidth.Visible = visible;\r
403             updownParWidth.Visible = visible;\r
404             updownParHeight.Visible = visible;\r
405         }\r
406 \r
407         // Calculation Functions\r
408         private Size SourceAspect\r
409         {\r
410             get\r
411             {\r
412                 if (Source != null) // display aspect = (width * par_width) / (height * par_height)\r
413                     return new Size((Source.ParVal.Width * Source.Resolution.Width), (Source.ParVal.Height * Source.Resolution.Height));\r
414 \r
415                 return new Size(0, 0); // Fall over to 16:9 and hope for the best\r
416             }\r
417         }\r
418         private Size CalculateAnamorphicSizes()\r
419         {\r
420             if (Source != null)\r
421             {\r
422                 /* Set up some variables to make the math easier to follow. */\r
423                 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
424                 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
425                 double storage_aspect = (double)cropped_width / cropped_height;\r
426 \r
427                 /* Figure out what width the source would display at. */\r
428                 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;\r
429 \r
430                 /*\r
431                      3 different ways of deciding output dimensions:\r
432                       - 1: Strict anamorphic, preserve source dimensions\r
433                       - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio\r
434                       - 3: Power user anamorphic, specify everything\r
435                   */\r
436                 double width, height;\r
437                 switch (drp_anamorphic.SelectedIndex)\r
438                 {\r
439                     default:\r
440                     case 1:\r
441                         /* Strict anamorphic */\r
442                         double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);\r
443                         displayWidth = Math.Round(displayWidth, 0);\r
444                         Size output = new Size((int)displayWidth, cropped_height);\r
445                         return output;\r
446                     case 2:\r
447                         /* "Loose" anamorphic.\r
448                             - Uses mod16-compliant dimensions,\r
449                             - Allows users to set the width\r
450                         */\r
451                         width = (int) text_width.Value;\r
452                         width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/\r
453 \r
454                         height = (width / storage_aspect) + 0.5;\r
455                         height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
456 \r
457                         /* The film AR is the source's display width / cropped source height.\r
458                            The output display width is the output height * film AR.\r
459                            The output PAR is the output display width / output storage width. */\r
460                         double pixel_aspect_width = height * source_display_width / cropped_height;\r
461                         double pixel_aspect_height = width;\r
462 \r
463                         double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
464                         if (double.IsNaN(disWidthLoose))\r
465                             disWidthLoose = 0;\r
466                         return new Size((int)disWidthLoose, (int)height);\r
467                     case 3:\r
468 \r
469                         // Get the User Interface Values\r
470                         double UIdisplayWidth;\r
471                         double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
472 \r
473                         /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
474                         height = GetModulusValue((double)text_height.Value);\r
475 \r
476                         if (check_KeepAR.Checked)\r
477                             return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
478 \r
479                         return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
480                 }\r
481             }\r
482 \r
483             // Return a default value of 0,0 to indicate failure\r
484             return new Size(0, 0);\r
485         }\r
486         private double GetModulusValue(double value)\r
487         {\r
488             int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
489             double remainder = value % mod;\r
490 \r
491             if (remainder == 0)\r
492                 return value;\r
493 \r
494             return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
495         }\r
496         private static int GetCropMod2Clean(int value)\r
497         {\r
498             int remainder = value % 2;\r
499             if (remainder == 0) return value;\r
500             return (value + remainder);\r
501         }\r
502 \r
503         // Hidden UI feature to drop the MaxWidth / Height with the MaxWidth/Height label is double clicked\r
504         private void lbl_max_DoubleClick(object sender, EventArgs e)\r
505         {\r
506             PresetMaximumResolution = new Size(0,0);\r
507             if (PictureSettingsChanged != null)\r
508                 PictureSettingsChanged(this, new EventArgs());\r
509         }\r
510     }\r
511 }