OSDN Git Service

CLI: add iso639-2 language code to audio track information display
[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;\r
16         private int _PresetMaximumWidth, _PresetMaximumHeight;\r
17         private Title _SourceTitle;\r
18 \r
19         public PictureSettings()\r
20         {\r
21             InitializeComponent();\r
22 \r
23             drp_anamorphic.SelectedIndex = 1;\r
24             drp_modulus.SelectedIndex = 0;\r
25         }\r
26 \r
27         /// <summary>\r
28         /// Gets or sets the source media used by this control.\r
29         /// </summary>\r
30         [Browsable(false)]\r
31         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
32         public Title Source\r
33         {\r
34             get { return _SourceTitle; }\r
35             set\r
36             {\r
37                 _SourceTitle = value;\r
38                 Enabled = _SourceTitle != null;\r
39 \r
40                 // Set the Aspect Ratio\r
41                 lbl_Aspect.Text = _SourceTitle.AspectRatio.ToString(Culture);\r
42                 lbl_src_res.Text = _SourceTitle.Resolution.Width + " x " + _SourceTitle.Resolution.Height;\r
43 \r
44                 // Set the Recommended Cropping values\r
45                 crop_top.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[0]);\r
46                 crop_bottom.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[1]);\r
47                 crop_left.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[2]);\r
48                 crop_right.Value = GetCropMod2Clean(_SourceTitle.AutoCropDimensions[3]);\r
49 \r
50                 // Set the Resolution Boxes\r
51                 text_width.Value = _SourceTitle.Resolution.Width;\r
52 \r
53                 if (drp_anamorphic.SelectedIndex == 0)\r
54                 {\r
55                     int width = _SourceTitle.Resolution.Width;\r
56 \r
57                     double crop_width = _SourceTitle.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
58                     double crop_height = _SourceTitle.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
59                     double newHeight = (width * _SourceTitle.Resolution.Width * sourceAspect.Height * crop_height) / (_SourceTitle.Resolution.Height * sourceAspect.Width * crop_width);\r
60                     text_height.Value = (decimal)GetModulusValue(newHeight);\r
61                 }\r
62                 else\r
63                 {\r
64                     text_height.Value = _SourceTitle.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
65                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
66                 }\r
67 \r
68                 updownParWidth.Value = _SourceTitle.ParVal.Width;\r
69                 updownParHeight.Value = _SourceTitle.ParVal.Height;\r
70                 updownDisplayWidth.Value = calculateAnamorphicSizes().Width;\r
71 \r
72             }\r
73         }\r
74 \r
75         /// <summary>\r
76         /// Gets or sets the maximum allowable size for the encoded resolution. Set a value to\r
77         /// "0" if the maximum does not matter.\r
78         /// </summary>\r
79         [Browsable(false)]\r
80         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
81         public Size PresetMaximumResolution\r
82         {\r
83             get { return new Size(_PresetMaximumWidth, _PresetMaximumHeight); }\r
84             set\r
85             {\r
86                 _PresetMaximumWidth = value.Width;\r
87                 _PresetMaximumHeight = value.Height;\r
88 \r
89                 if (value.Width != 0 && value.Height != 0)\r
90                     lbl_max.Text = "Max Width / Height";\r
91                 else if (value.Width != 0)\r
92                     lbl_max.Text = "Max Width";\r
93                 else if (value.Height != 0)\r
94                     lbl_max.Text = "Max Height";\r
95                 else\r
96                     lbl_max.Text = "";\r
97             }\r
98         }\r
99 \r
100         // Picture Controls\r
101         private void text_width_ValueChanged(object sender, EventArgs e)\r
102         {\r
103             if (preventChangingWidth)\r
104                 return;\r
105 \r
106             // Make sure the new value doesn't exceed the maximum\r
107             if (Source != null)\r
108                 if (text_width.Value > Source.Resolution.Width)\r
109                     text_width.Value = Source.Resolution.Width;\r
110 \r
111             switch (drp_anamorphic.SelectedIndex)\r
112             {\r
113                 case 0:\r
114                     if (check_KeepAR.Checked && Source != null)\r
115                     {\r
116                         preventChangingHeight = true;\r
117 \r
118                         int width = (int)text_width.Value;\r
119 \r
120                         double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
121                         double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
122                         double newHeight = (width * Source.Resolution.Width * sourceAspect.Height * crop_height) /\r
123                                            (Source.Resolution.Height * sourceAspect.Width * crop_width);\r
124                         text_height.Value = (decimal)GetModulusValue(newHeight);\r
125 \r
126                         preventChangingHeight = false;\r
127                     }\r
128                     break;\r
129                 default:\r
130                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
131                     break;\r
132             }\r
133 \r
134             preventChangingWidth = false;\r
135         }\r
136         private void text_height_ValueChanged(object sender, EventArgs e)\r
137         {\r
138             if (preventChangingHeight)\r
139                 return;\r
140 \r
141             if (Source != null)\r
142                 if (text_height.Value > Source.Resolution.Height)\r
143                     text_height.Value = Source.Resolution.Height;\r
144 \r
145             switch (drp_anamorphic.SelectedIndex)\r
146             {\r
147                 case 0:\r
148                     if (check_KeepAR.Checked && Source != null)\r
149                     {\r
150                         preventChangingWidth = true;\r
151 \r
152                         double crop_width = Source.Resolution.Width - (double)crop_left.Value - (double)crop_right.Value;\r
153                         double crop_height = Source.Resolution.Height - (double)crop_top.Value - (double)crop_bottom.Value;\r
154 \r
155                         double new_width = ((double)text_height.Value * Source.Resolution.Height * sourceAspect.Width * crop_width) /\r
156                                             (Source.Resolution.Width * sourceAspect.Height * crop_height);\r
157 \r
158                         text_width.Value = (decimal)GetModulusValue(new_width);\r
159 \r
160                         preventChangingWidth = false;\r
161                     }\r
162                     break;\r
163                 default:\r
164                     labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
165                     break;\r
166             }\r
167 \r
168             preventChangingHeight = false;\r
169         }\r
170         private void check_KeepAR_CheckedChanged(object sender, EventArgs e)\r
171         {\r
172             //Force TextWidth to recalc height\r
173             if (check_KeepAR.Checked)\r
174                 text_width_ValueChanged(this, new EventArgs());\r
175 \r
176             // Disable the Custom Anamorphic Par Controls if checked.\r
177             if (drp_anamorphic.SelectedIndex == 3)\r
178             {\r
179                 updownParWidth.Enabled = !check_KeepAR.Checked;\r
180                 updownParHeight.Enabled = !check_KeepAR.Checked;\r
181             }\r
182 \r
183             // Raise the Picture Settings Changed Event\r
184             if (PictureSettingsChanged != null)\r
185                 PictureSettingsChanged(this, new EventArgs());\r
186         }\r
187 \r
188         // Anamorphic Controls\r
189         private void drp_anamorphic_SelectedIndexChanged(object sender, EventArgs e)\r
190         {\r
191             switch (drp_anamorphic.SelectedIndex)\r
192             {\r
193                 case 0:\r
194                     text_width.Enabled = true;\r
195                     text_height.Enabled = true;\r
196                     check_KeepAR.Enabled = true;\r
197 \r
198                     setCustomAnamorphicOptionsVisible(false);\r
199                     labelStaticDisplaySize.Visible = false;\r
200                     labelDisplaySize.Visible = false;\r
201 \r
202                     check_KeepAR.Checked = true;\r
203                     drp_modulus.SelectedIndex = 0;\r
204 \r
205                     if (check_KeepAR.Checked)\r
206                         text_width_ValueChanged(this, new EventArgs());\r
207                     // Don't update display size if we're not using anamorphic\r
208                     return;\r
209                 case 1:\r
210                     text_width.Enabled = false;\r
211                     text_height.Enabled = false;\r
212                     check_KeepAR.Enabled = false;\r
213 \r
214                     setCustomAnamorphicOptionsVisible(false);\r
215                     labelStaticDisplaySize.Visible = true;\r
216                     labelDisplaySize.Visible = true;\r
217                     drp_modulus.SelectedIndex = 0;\r
218 \r
219                     check_KeepAR.Checked = true;\r
220                     break;\r
221                 case 2:\r
222                     text_width.Enabled = true;\r
223                     text_height.Enabled = false;\r
224                     check_KeepAR.Enabled = false;\r
225 \r
226                     setCustomAnamorphicOptionsVisible(false);\r
227                     labelStaticDisplaySize.Visible = true;\r
228                     labelDisplaySize.Visible = true;\r
229                     drp_modulus.SelectedIndex = 0;\r
230 \r
231                     check_KeepAR.Checked = true;\r
232                     break;\r
233                 case 3:\r
234                     text_width.Enabled = true;\r
235                     text_height.Enabled = true;\r
236                     check_KeepAR.Enabled = true;\r
237 \r
238                     setCustomAnamorphicOptionsVisible(true);\r
239                     labelStaticDisplaySize.Visible = true;\r
240                     labelDisplaySize.Visible = true;\r
241 \r
242                     check_KeepAR.Checked = true;\r
243                     break;\r
244             }\r
245 \r
246             labelDisplaySize.Text = calculateAnamorphicSizes().Width + "x" + calculateAnamorphicSizes().Height;\r
247 \r
248             if (check_KeepAR.Checked)\r
249                 text_width_ValueChanged(this, new EventArgs());\r
250 \r
251             if (PictureSettingsChanged != null)\r
252                 PictureSettingsChanged(this, new EventArgs());\r
253         }\r
254         private void drp_modulus_SelectedIndexChanged(object sender, EventArgs e)\r
255         {\r
256             preventChangingWidth = true;\r
257             preventChangingHeight = true;\r
258 \r
259             text_width.Value = (decimal)GetModulusValue((double)text_width.Value);\r
260             text_height.Value = (decimal)GetModulusValue((double)text_height.Value);\r
261 \r
262             preventChangingWidth = false;\r
263             preventChangingHeight = false;\r
264 \r
265             text_width.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
266             text_height.Increment = int.Parse(drp_modulus.SelectedItem.ToString());\r
267 \r
268             if (PictureSettingsChanged != null)\r
269                 PictureSettingsChanged(this, new EventArgs());\r
270         }\r
271 \r
272         // Cropping Controls\r
273         private void check_autoCrop_CheckedChanged(object sender, EventArgs e)\r
274         {\r
275             crop_top.Enabled = check_customCrop.Checked;\r
276             crop_bottom.Enabled = check_customCrop.Checked;\r
277             crop_left.Enabled = check_customCrop.Checked;\r
278             crop_right.Enabled = check_customCrop.Checked;\r
279         }\r
280         private void crop_ValueChanged(object sender, EventArgs e)\r
281         {\r
282             text_width_ValueChanged(this, new EventArgs());\r
283         }\r
284 \r
285         // GUI Functions\r
286         private void setCustomAnamorphicOptionsVisible(bool visible)\r
287         {\r
288             lbl_modulus.Visible = visible;\r
289             lbl_displayWidth.Visible = visible;\r
290             lbl_parWidth.Visible = visible;\r
291             lbl_parHeight.Visible = visible;\r
292 \r
293             drp_modulus.Visible = visible;\r
294             updownDisplayWidth.Visible = visible;\r
295             updownParWidth.Visible = visible;\r
296             updownParHeight.Visible = visible;\r
297         }\r
298 \r
299         // Calculation Functions\r
300         private Size sourceAspect\r
301         {\r
302             get\r
303             {\r
304                 if (Source != null)\r
305                 {\r
306                     if (Source.AspectRatio == 1.78F)\r
307                         return new Size(16, 9);\r
308                     if (Source.AspectRatio == 1.33F)\r
309                         return new Size(4, 3);\r
310                 }\r
311                 return new Size(0, 0);\r
312             }\r
313         }\r
314         private Size calculateAnamorphicSizes()\r
315         {\r
316             if (Source != null)\r
317             {\r
318                 /* Set up some variables to make the math easier to follow. */\r
319                 int cropped_width = Source.Resolution.Width - (int)crop_left.Value - (int)crop_right.Value;\r
320                 int cropped_height = Source.Resolution.Height - (int)crop_top.Value - (int)crop_bottom.Value;\r
321                 double storage_aspect = (double)cropped_width / cropped_height;\r
322 \r
323                 /* Figure out what width the source would display at. */\r
324                 double source_display_width = (double)cropped_width * Source.ParVal.Width / Source.ParVal.Height;\r
325 \r
326                 /*\r
327                      3 different ways of deciding output dimensions:\r
328                       - 1: Strict anamorphic, preserve source dimensions\r
329                       - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio\r
330                       - 3: Power user anamorphic, specify everything\r
331                   */\r
332                 double width, height;\r
333                 switch (drp_anamorphic.SelectedIndex)\r
334                 {\r
335                     case 1:\r
336                         /* Strict anamorphic */\r
337                         double displayWidth = ((double)cropped_width * Source.ParVal.Width / Source.ParVal.Height);\r
338                         displayWidth = Math.Round(displayWidth, 0);\r
339                         Size output = new Size((int)displayWidth, cropped_height);\r
340                         return output;\r
341                     case 2:\r
342                         /* "Loose" anamorphic.\r
343                             - Uses mod16-compliant dimensions,\r
344                             - Allows users to set the width\r
345                         */\r
346                         width = (int)text_width.Value - (int)crop_left.Value - (int)crop_right.Value;\r
347                         width = GetModulusValue(width); /* Time to get picture width that divide cleanly.*/\r
348 \r
349                         height = (width / storage_aspect) + 0.5;\r
350                         height = GetModulusValue(height); /* Time to get picture height that divide cleanly.*/\r
351 \r
352                         /* The film AR is the source's display width / cropped source height.\r
353                            The output display width is the output height * film AR.\r
354                            The output PAR is the output display width / output storage width. */\r
355                         double pixel_aspect_width = height * source_display_width / cropped_height;\r
356                         double pixel_aspect_height = width;\r
357 \r
358                         double disWidthLoose = (width * pixel_aspect_width / pixel_aspect_height);\r
359                         return new Size((int)disWidthLoose, (int)height);\r
360                     case 3:\r
361 \r
362                         // Get the User Interface Values\r
363                         double UIdisplayWidth;\r
364                         double.TryParse(updownDisplayWidth.Text, out UIdisplayWidth);\r
365 \r
366                         /* Anamorphic 3: Power User Jamboree - Set everything based on specified values */\r
367                         height = GetModulusValue((double)text_height.Value);\r
368 \r
369                         if (check_KeepAR.Checked)\r
370                             return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
371 \r
372                         return new Size((int)Math.Truncate(UIdisplayWidth), (int)height);\r
373                 }\r
374             }\r
375 \r
376             // Return a default value of 0,0 to indicate failure\r
377             return new Size(0, 0);\r
378         }\r
379         private double GetModulusValue(double value)\r
380         {\r
381             int mod = int.Parse(drp_modulus.SelectedItem.ToString());\r
382             double remainder = value % mod;\r
383 \r
384             if (remainder == 0)\r
385                 return value;\r
386 \r
387             return remainder >= ((double)mod / 2) ? value + (mod - remainder) : value - remainder;\r
388         }\r
389         private static int GetCropMod2Clean(int value)\r
390         {\r
391             int remainder = value % 2;\r
392             if (remainder == 0) return value;\r
393             return (value + remainder);\r
394         }\r
395     }\r
396 }