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