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