6286bc5879d9d2615c2f80fc83940abcb28f9472
[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_LEFT, RT_HALIGN_RIGHT, RT_VALIGN_CENTER, 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                         "4": self.testpic_filter,
28                         "5": self.testpic_gamma,
29                         "6": 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
100                 c = self["Canvas"]
101
102                 xres, yres = 720, 576
103
104                 bbw, bbh = xres / 192, yres / 192
105                 c.fill(0, 0, xres, yres, RGB(0,0,0))
106
107                 bbw = xres / 192
108                 bbh = yres / 192
109                 c.fill(0, 0, xres, yres, RGB(255,255,255))
110
111 #               for i in range(15):
112 #                       col = 185 + i * 5
113 #                       width = xres - xres/5
114 #                       ew = width / 15
115 #                       offset = xres/10 + ew * i
116 #                       y = yres * 2 / 3
117 #                       height = yres / 6
118 #
119 #                       c.fill(offset, y, ew, height, RGB(col, col, col))
120 #
121 #                       if col == 185 or col == 235 or col == 255:
122 #                               self.bbox(offset, y, ew, height, RGB(0,0,0), bbw, bbh)
123
124                 for i in range(15):
125 #                       col = (7-i) * 255 / 7
126                         col = 185 + i * 5
127                         height = yres / 3
128                         eh = height / 8
129                         offset = yres/6 + eh * i
130                         x = xres * 2 / 3
131                         width = yres / 6
132
133                         c.fill(x, offset, width, eh, RGB(col, col, col))
134 #                       if col == 0 or col == 36:
135 #                               self.bbox(x, offset, width, eh, RGB(255,255,255), bbw, bbh);
136 #                       if col == 255:
137 #                               self.bbox(x, offset, width, eh, RGB(0,0,0), bbw, bbh);
138                         if col == 185 or col == 235 or col == 255:
139                                 c.fill(x, offset, width, 2, RGB(0,0,0)) 
140                         if i >= 13:
141                                 c.writeText(x + width, offset, width, eh, RGB(0, 0, 0), RGB(255, 255, 255), gFont("Regular", 20), "%d." % (i-13+1))
142
143                 c.writeText(xres / 10, yres / 6 - 40, xres * 3 / 5, 40, RGB(128,0,0), RGB(255,255,255), gFont("Regular", 40), 
144                         _("Contrast"))
145                 c.writeText(xres / 10, yres / 6, xres / 2, yres / 6, RGB(0,0,0), RGB(255,255,255), gFont("Regular", 20),
146                         _("Now, use the contrast setting to turn up the brightness of the background as much as possible, "
147                                 "but make sure that you can still see the difference between the two brightest levels of shades."
148                                 "If you have done that, press OK."),
149                                 RT_WRAP)
150
151                 c.flush()
152
153         def testpic_colors(self):
154                 self.next = self.close
155
156                 c = self["Canvas"]
157
158                 xres, yres = 720, 576
159
160                 bbw = xres / 192
161                 bbh = yres / 192
162                 c.fill(0, 0, xres, yres, RGB(255,255,255))
163
164                 for i in range(33):
165                         col = i * 255 / 32;
166                         width = xres - xres/5;
167                         ew = width / 33;
168                         offset = xres/10 + ew * i;
169                         y = yres * 2 / 3;
170                         height = yres / 20;
171                         o = yres / 60;
172
173                         if i < 16:
174                                 c1 = 0xFF;
175                                 c2 = 0xFF - (0xFF * i / 16);
176                         else:
177                                 c1 = 0xFF - (0xFF * (i - 16) / 16);
178                                 c2 = 0;
179
180                         c.fill(offset, y, ew, height, RGB(c1, c2, c2))
181                         c.fill(offset, y + (height + o) * 1, ew, height, RGB(c2, c1, c2))
182                         c.fill(offset, y + (height + o) * 2, ew, height, RGB(c2, c2, c1))
183                         c.fill(offset, y + (height + o) * 3, ew, height, RGB(col, col, col))
184
185                         if i == 0:
186                                 self.bbox(offset, y, ew, height, RGB(0,0,0), bbw, bbh);
187                                 self.bbox(offset, y + (height + o) * 1, ew, height, RGB(0,0,0), bbw, bbh);
188                                 self.bbox(offset, y + (height + o) * 2, ew, height, RGB(0,0,0), bbw, bbh);
189
190                         for i in range(8):
191                                 height = yres / 3;
192                                 eh = height / 8;
193                                 offset = yres/6 + eh * i;
194                                 x = xres * 2 / 3;
195                                 width = yres / 6;
196
197                                 c.fill(x, offset, width, eh, self.basic_colors[i])
198                                 if i == 0:
199                                         self.bbox(x, offset, width, eh, RGB(0,0,0), bbw, bbh)
200
201                 c.writeText(xres / 10, yres / 6 - 40, xres * 3 / 5, 40, RGB(128,0,0), RGB(255,255,255), gFont("Regular", 40), 
202                         ("Color"))
203                 c.writeText(xres / 10, yres / 6, xres / 2, yres / 6, RGB(0,0,0), RGB(255,255,255), gFont("Regular", 20),
204                         _("Adjust the color settings so that all the color shades are distinguishable, but appear as saturated as possible. "
205                                 "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."),
206                                 RT_WRAP)
207
208                 c.flush()
209
210         def testpic_filter(self):
211                 c = self["Canvas"]
212
213                 xres, yres = 720, 576
214
215                 c.fill(0, 0, xres, yres, RGB(64, 64, 64))
216
217                 width = xres - xres/5
218                 offset = xres/10
219                 yb = yres * 2 / 3
220                 height = yres / 20
221                 o = yres / 60
222                 border = xres / 60
223
224                 g1 = 255
225                 g2 = 128
226
227                 c.fill(offset - border, yb - border, border * 2 + width, border * 2 + (height * 3 + o * 2), RGB(g1, g1, g1))
228
229                 for x in xrange(0, width, 2):
230                         c.fill(offset + x, yb, 1, height, RGB(g2,g2,g2))
231
232                 for x in xrange(0, width, 4):
233                         c.fill(offset + x, yb + (o + height), 2, height, RGB(g2,g2,g2))
234
235                 for x in xrange(0, width, 8):
236                         c.fill(offset + x, yb + (o + height) * 2, 4, height, RGB(g2,g2,g2))
237
238                 c.flush()
239
240         def testpic_gamma(self):
241                 self.next = None
242
243                 c = self["Canvas"]
244
245                 xres, yres = 720, 576
246
247                 c.fill(0, 0, xres, yres, RGB(0, 0, 0))
248
249                 width = xres - xres/5
250                 offset_x = xres/10
251
252                 height = yres - yres/5
253                 offset_y = yres/10
254
255                 for y in xrange(0, height, 4):
256                         c.fill(offset_x, offset_y + y, width/2, 2, RGB(255,255,255))
257
258                 l = 0
259                 fnt = gFont("Regular", height / 14)
260                 import math
261                 for i in xrange(1, 15):
262                         y = i * height / 14
263                         h = y - l
264                         gamma = 0.6 + i * 0.2
265                         col = int(math.pow(.5, 1.0/gamma) * 256.0)
266                         c.fill(offset_x + width/2, offset_y + l, width/2, h, RGB(col,col,col))
267
268                         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)
269                         l = y
270
271                 c.flush()
272
273         def testpic_fubk(self):
274                 self.next = None
275
276                 # TODO:
277                 # this test currently only works for 4:3 aspect.
278                 # also it's hardcoded to 720,576
279                 c = self["Canvas"]
280
281                 xres, yres = 720, 576
282
283                 c.fill(0, 0, xres, yres, RGB(128, 128, 128))
284
285                 for x in xrange(6, xres, 44):
286                         c.fill(x, 0, 3, yres, RGB(255,255,255))
287
288                 for y in xrange(34, yres, 44):
289                         c.fill(0, y, xres, 3, RGB(255,255,255))
290
291                 for i in range(8):
292                         c.fill(140+i*55, 80, 55, 80, self.basic_colors[i])
293                         g = i * 255 / 7
294                         c.fill(140+i*55, 160, 55, 80, RGB(g,g,g))
295
296                 x = 0
297                 phase = 0
298
299                 while x < 440:
300                         freq = (440 - x) / 44 + 1
301                         if phase:
302                                 col = RGB(255,255,255)
303                         else:
304                                 col = RGB(0,0,0)
305                         c.fill(140+x, 320, freq, 160, col)
306                         x += freq
307                         phase = not phase
308
309                 c.flush()
310