import of enigma2
[enigma2.git] / lib / gdi / gfbdc.cpp
1 #include <lib/gdi/gfbdc.h>
2
3 #include <lib/base/init.h>
4 #include <lib/base/init_num.h>
5 #include <lib/base/econfig.h>
6
7 gFBDC *gFBDC::instance;
8
9 gFBDC::gFBDC()
10 {
11         instance=this;
12         fb=new fbClass;
13
14         if (!fb->Available())
15                 eFatal("no framebuffer available");
16
17         fb->SetMode(720, 576, 8);
18         for (int y=0; y<576; y++)                                                                                                                                                // make whole screen transparent
19                 memset(fb->lfb+y*fb->Stride(), 0x00, fb->Stride());
20
21         pixmap=new gPixmap();
22         pixmap->x=720;
23         pixmap->y=576;
24         pixmap->bpp=8;
25         pixmap->bypp=1;
26         pixmap->stride=fb->Stride();
27         pixmap->data=fb->lfb;
28         
29         pixmap->clut.colors=256;
30         pixmap->clut.data=new gRGB[pixmap->clut.colors];
31         memset(pixmap->clut.data, 0, sizeof(*pixmap->clut.data)*pixmap->clut.colors);
32         reloadSettings();
33 }
34
35 gFBDC::~gFBDC()
36 {
37         delete pixmap;
38         delete fb;
39         instance=0;
40 }
41
42 void gFBDC::calcRamp()
43 {
44 #if 0
45         float fgamma=gamma ? gamma : 1;
46         fgamma/=10.0;
47         fgamma=1/log(fgamma);
48         for (int i=0; i<256; i++)
49         {
50                 float raw=i/255.0; // IIH, float.
51                 float corr=pow(raw, fgamma) * 256.0;
52
53                 int d=corr * (float)(256-brightness) / 256 + brightness;
54                 if (d < 0)
55                         d=0;
56                 if (d > 255)
57                         d=255;
58                 ramp[i]=d;
59                 
60                 rampalpha[i]=i*alpha/256;
61         }
62 #endif
63         for (int i=0; i<256; i++)
64         {
65                 int d;
66                 d=i;
67                 d=(d-128)*(gamma+64)/(128+64)+128;
68                 d+=brightness-128; // brightness correction
69                 if (d<0)
70                         d=0;
71                 if (d>255)
72                         d=255;
73                 ramp[i]=d;
74
75 /*              if ( eDVB::getInstance()->getmID == 1 )
76                         rampalpha[i]=i*alpha/65535;
77                 else*/
78                         rampalpha[i]=i*alpha/256;
79         }
80
81         rampalpha[255]=255; // transparent BLEIBT bitte so.
82 }
83
84 void gFBDC::setPalette()
85 {
86         if (!pixmap->clut.data)
87                 return;
88         
89         for (int i=0; i<256; ++i)
90         {
91                 fb->CMAP()->red[i]=ramp[pixmap->clut.data[i].r]<<8;
92                 fb->CMAP()->green[i]=ramp[pixmap->clut.data[i].g]<<8;
93                 fb->CMAP()->blue[i]=ramp[pixmap->clut.data[i].b]<<8;
94                 fb->CMAP()->transp[i]=rampalpha[pixmap->clut.data[i].a]<<8;
95                 if (!fb->CMAP()->red[i])
96                         fb->CMAP()->red[i]=0x100;
97         }
98         fb->PutCMAP();
99 }
100
101 void gFBDC::exec(gOpcode *o)
102 {
103         switch (o->opcode)
104         {
105         case gOpcode::setPalette:
106         {
107                 gPixmapDC::exec(o);
108                 setPalette();
109                 break;
110         }
111         default:
112                 gPixmapDC::exec(o);
113                 break;
114         }
115 }
116
117 gFBDC *gFBDC::getInstance()
118 {
119         return instance;
120 }
121
122 void gFBDC::setAlpha(int a)
123 {
124         alpha=a;
125
126         calcRamp();
127         setPalette();
128 }
129
130 void gFBDC::setBrightness(int b)
131 {
132         brightness=b;
133
134         calcRamp();
135         setPalette();
136 }
137
138 void gFBDC::setGamma(int g)
139 {
140         gamma=g;
141
142         calcRamp();
143         setPalette();
144 }
145
146 void gFBDC::saveSettings()
147 {
148         eConfig::getInstance()->setKey("/ezap/osd/alpha", alpha);
149         eConfig::getInstance()->setKey("/ezap/osd/gamma", gamma);
150         eConfig::getInstance()->setKey("/ezap/osd/brightness", brightness);
151 }
152
153 void gFBDC::reloadSettings()
154 {
155         if (eConfig::getInstance()->getKey("/ezap/osd/alpha", alpha))
156                 alpha=255;
157         if (eConfig::getInstance()->getKey("/ezap/osd/gamma", gamma))
158                 gamma=128;
159         if (eConfig::getInstance()->getKey("/ezap/osd/brightness", brightness))
160                 brightness=128;
161
162         calcRamp();
163         setPalette();
164 }
165
166 eAutoInitP0<gFBDC> init_gFBDC(eAutoInitNumbers::graphic+1, "GFBDC");