add VideoTune plugin
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / VideoTune / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Components.Sources.CanvasSource import CanvasSource
4 from Components.ActionMap import ActionMap
5 from enigma import gFont
6 from enigma import RT_HALIGN_LEFT, RT_HALIGN_CENTER, RT_VALIGN_CENTER, RT_WRAP
7
8 def RGB(r,g,b):
9         return (r<<16)|(g<<8)|b
10
11 class VideoFinetune(Screen):
12         skin = """
13                 <screen position="0,0" size="720,576">
14                         <widget source="Canvas" render="Canvas" position="0,0" size="720,576" />
15                 </screen>"""
16
17         def __init__(self, session):
18                 Screen.__init__(self, session)
19                 self["Canvas"] = CanvasSource()
20
21                 self.basic_colors = [RGB(255, 255, 255), RGB(255, 255, 0), RGB(0, 255, 255), RGB(0, 255, 0), RGB(255, 0, 255), RGB(255, 0, 0), RGB(0, 0, 255), RGB(0, 0, 0)]
22
23                 self["actions"] = ActionMap(["InputActions", "OkCancelActions"],
24                 {
25                         "1": self.testpic_brightness,
26                         "2": self.testpic_contrast,
27                         "3": self.testpic_colors,
28                         "4": self.testpic_filter,
29                         "5": self.testpic_gamma,
30                         "6": self.testpic_fubk,
31                         "ok": self.close,
32                         "cancel": self.close,
33                 })
34                 self.testpic_brightness()
35
36         def bbox(self, x, y, width, height, col, xx, yy):
37                 c = self["Canvas"]
38                 c.fill(x, y, xx, yy, col)
39                 c.fill(x + width - xx, y, xx, yy, col)
40                 c.fill(x, y + height - yy, xx, yy, col)
41                 c.fill(x + width - xx, y + height - yy, xx, yy, col)
42
43         def testpic_brightness(self):
44                 c = self["Canvas"]
45
46                 xres, yres = 720, 576
47
48                 bbw, bbh = xres / 192, yres / 192
49                 c.fill(0, 0, xres, yres, RGB(0,0,0))
50
51                 for i in range(15):
52                         col = i * 116 / 14
53                         width = xres - xres/5
54                         ew = width / 15
55                         offset = xres/10 + ew * i
56                         y = yres * 2 / 3
57                         height = yres / 6
58
59                         c.fill(offset, y, ew, height, RGB(col, col, col))
60
61                         if col == 0 or col == 16 or col == 116:
62                                 self.bbox(offset, y, ew, height, RGB(255,255,255), bbw, bbh)
63
64                 for i in range(8):
65                         col = (7-i) * 255 / 7
66                         height = yres / 3
67                         eh = height / 8
68                         offset = yres/6 + eh * i
69                         x = xres * 2 / 3
70                         width = yres / 6
71
72                         c.fill(x, offset, width, eh, RGB(col, col, col))
73                         if col == 0 or col == 36:
74                                 self.bbox(x, offset, width, eh, RGB(255,255,255), bbw, bbh)
75
76                 c.writeText(xres / 10, yres / 6 - 40, xres * 3 / 5, 40, RGB(128,255,255), RGB(0,0,0), gFont("Regular", 40), 
77                         _("Brightness"))
78                 c.writeText(xres / 10, yres / 6, xres * 4 / 7, yres / 6, RGB(255,255,255), RGB(0,0,0), gFont("Regular", 20),
79                         ("If your TV has a brightness or contrast enhancement, disable it. If there is something called \"dynamic\", "
80                                 "set it to standard. Adjust the backlight level to your favourite value. "
81                                 "Turn down contrast on your TV as much as possible.\nThen turn the brightness setting as"
82                                 "low as possible, but make sure that the two lowermost shades of gray are still distinguishable.\n"
83                                 "Do not care for the bright shades now, they will be set up in the next step.\n"
84                                 "If you are happy with the result, press OK."),
85                                 RT_WRAP)
86
87                 c.flush()
88
89         def testpic_contrast(self):
90                 c = self["Canvas"]
91
92                 xres, yres = 720, 576
93
94                 bbw, bbh = xres / 192, yres / 192
95                 c.fill(0, 0, xres, yres, RGB(0,0,0))
96
97                 bbw = xres / 192
98                 bbh = yres / 192
99                 c.fill(0, 0, xres, yres, RGB(255,255,255))
100
101                 for i in range(15):
102                         col = 185 + i * 5
103                         width = xres - xres/5
104                         ew = width / 15
105                         offset = xres/10 + ew * i
106                         y = yres * 2 / 3
107                         height = yres / 6
108
109                         c.fill(offset, y, ew, height, RGB(col, col, col))
110
111                         if col == 185 or col == 235 or col == 255:
112                                 self.bbox(offset, y, ew, height, RGB(0,0,0), bbw, bbh)
113
114                 for i in range(8):
115                         col = (7-i) * 255 / 7
116                         height = yres / 3
117                         eh = height / 8
118                         offset = yres/6 + eh * i
119                         x = xres * 2 / 3
120                         width = yres / 6
121
122                         c.fill(x, offset, width, eh, RGB(col, col, col))
123                         if col == 0 or col == 36:
124                                 self.bbox(x, offset, width, eh, RGB(255,255,255), bbw, bbh);
125                         if col == 255:
126                                 self.bbox(x, offset, width, eh, RGB(0,0,0), bbw, bbh);
127
128                 c.writeText(xres / 10, yres / 6 - 40, xres * 3 / 5, 40, RGB(128,0,0), RGB(255,255,255), gFont("Regular", 40), 
129                         _("Contrast"))
130                 c.writeText(xres / 10, yres / 6, xres / 2, yres / 6, RGB(0,0,0), RGB(255,255,255), gFont("Regular", 20),
131                         ("Now, use the contrast setting to turn up the brightness of the background as much as possible, "
132                                 "but make sure that you can still see the difference between the two brightest level of shades."
133                                 "If you have done that, press OK."),
134                                 RT_WRAP)
135
136                 c.flush()
137
138         def testpic_colors(self):
139                 c = self["Canvas"]
140
141                 xres, yres = 720, 576
142
143                 bbw = xres / 192
144                 bbh = yres / 192
145                 c.fill(0, 0, xres, yres, RGB(255,255,255))
146
147                 for i in range(33):
148                         col = i * 255 / 32;
149                         width = xres - xres/5;
150                         ew = width / 33;
151                         offset = xres/10 + ew * i;
152                         y = yres * 2 / 3;
153                         height = yres / 20;
154                         o = yres / 60;
155
156                         if i < 16:
157                                 c1 = 0xFF;
158                                 c2 = 0xFF - (0xFF * i / 16);
159                         else:
160                                 c1 = 0xFF - (0xFF * (i - 16) / 16);
161                                 c2 = 0;
162
163                         c.fill(offset, y, ew, height, RGB(c1, c2, c2))
164                         c.fill(offset, y + (height + o) * 1, ew, height, RGB(c2, c1, c2))
165                         c.fill(offset, y + (height + o) * 2, ew, height, RGB(c2, c2, c1))
166                         c.fill(offset, y + (height + o) * 3, ew, height, RGB(col, col, col))
167
168                         if i == 0:
169                                 self.bbox(offset, y, ew, height, RGB(0,0,0), bbw, bbh);
170                                 self.bbox(offset, y + (height + o) * 1, ew, height, RGB(0,0,0), bbw, bbh);
171                                 self.bbox(offset, y + (height + o) * 2, ew, height, RGB(0,0,0), bbw, bbh);
172
173                         for i in range(8):
174                                 height = yres / 3;
175                                 eh = height / 8;
176                                 offset = yres/6 + eh * i;
177                                 x = xres * 2 / 3;
178                                 width = yres / 6;
179
180                                 c.fill(x, offset, width, eh, self.basic_colors[i])
181                                 if i == 0:
182                                         self.bbox(x, offset, width, eh, RGB(0,0,0), bbw, bbh)
183
184                 c.writeText(xres / 10, yres / 6 - 40, xres * 3 / 5, 40, RGB(128,0,0), RGB(255,255,255), gFont("Regular", 40), 
185                         ("Color"))
186                 c.writeText(xres / 10, yres / 6, xres / 2, yres / 6, RGB(0,0,0), RGB(255,255,255), gFont("Regular", 20),
187                         ("Adjust the color settings so that all the color shades are distinguishable, but appear as saturated as possible. "
188                                 "If you are happy with the result, press OK to close the video finetuning, or use the number keys to select other test screens."),
189                                 RT_WRAP)
190
191                 c.flush()
192
193         def testpic_filter(self):
194                 c = self["Canvas"]
195
196                 xres, yres = 720, 576
197
198                 c.fill(0, 0, xres, yres, RGB(64, 64, 64))
199
200                 width = xres - xres/5
201                 offset = xres/10
202                 yb = yres * 2 / 3
203                 height = yres / 20
204                 o = yres / 60
205                 border = xres / 60
206
207                 g1 = 255
208                 g2 = 128
209
210                 c.fill(offset - border, yb - border, border * 2 + width, border * 2 + (height * 3 + o * 2), RGB(g1, g1, g1))
211
212                 for x in xrange(0, width, 2):
213                         c.fill(offset + x, yb, 1, height, RGB(g2,g2,g2))
214
215                 for x in xrange(0, width, 4):
216                         c.fill(offset + x, yb + (o + height), 2, height, RGB(g2,g2,g2))
217
218                 for x in xrange(0, width, 8):
219                         c.fill(offset + x, yb + (o + height) * 2, 4, height, RGB(g2,g2,g2))
220
221                 c.flush()
222
223         def testpic_gamma(self):
224                 c = self["Canvas"]
225
226                 xres, yres = 720, 576
227
228                 c.fill(0, 0, xres, yres, RGB(0, 0, 0))
229
230                 width = xres - xres/5
231                 offset_x = xres/10
232
233                 height = yres - yres/5
234                 offset_y = yres/10
235
236                 for y in xrange(0, height, 4):
237                         c.fill(offset_x, offset_y + y, width/2, 2, RGB(255,255,255))
238
239                 l = 0
240                 import math
241                 for i in xrange(1, 14):
242                         y = i * height / 14
243                         h = y - l
244                         l = y
245                         gamma = 0.6 + i * 0.2
246                         col = int(math.pow(.5, 1.0/gamma) * 256.0)
247                         c.fill(offset_x + width/2, offset_y + y, width/2, h, RGB(col,col,col))
248
249                 c.flush()
250
251         def testpic_fubk(self):
252                 # TODO:
253                 # this test currently only works for 4:3 aspect.
254                 # also it's hardcoded to 720,576
255                 c = self["Canvas"]
256
257                 xres, yres = 720, 576
258
259                 c.fill(0, 0, xres, yres, RGB(128, 128, 128))
260
261                 for x in xrange(6, xres, 44):
262                         c.fill(x, 0, 3, yres, RGB(255,255,255))
263
264                 for y in xrange(34, yres, 44):
265                         c.fill(0, y, xres, 3, RGB(255,255,255))
266
267                 for i in range(8):
268                         c.fill(140+i*55, 80, 55, 80, self.basic_colors[i])
269                         g = i * 255 / 7
270                         c.fill(140+i*55, 160, 55, 80, RGB(g,g,g))
271
272                 x = 0
273                 phase = 0
274
275                 while x < 440:
276                         freq = (440 - x) / 44 + 1
277                         if phase:
278                                 col = RGB(255,255,255)
279                         else:
280                                 col = RGB(0,0,0)
281                         print x, freq, col
282                         c.fill(140+x, 320, freq, 160, col)
283                         x += freq
284                         phase = not phase
285
286                 c.flush()
287
288 def videoFinetuneMain(session, **kwargs):
289         session.open(VideoFinetune)
290
291 def startSetup(menuid):
292         if menuid != "system": 
293                 return [ ]
294
295         return [(("Video Finetune"), videoFinetuneMain, "video_finetune", None)]
296
297 def Plugins(**kwargs):
298         return [
299                 PluginDescriptor(name=("Video Finetune"), description=("Fine-tune your video"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) 
300         ]