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