add missing files, add ability to specify table_id mask and table_id ext mask
[enigma2.git] / lib / gdi / gpixmap.cpp
1 #include <lib/gdi/gpixmap.h>
2 #include <lib/gdi/region.h>
3
4 gLookup::gLookup()
5         :size(0), lookup(0)
6 {
7 }
8
9 gLookup::gLookup(int size, const gPalette &pal, const gRGB &start, const gRGB &end)
10         :size(0), lookup(0)
11 {
12         build(size, pal, start, end);
13 }
14
15 void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRGB &end)
16 {
17         if (lookup)
18         {
19                 delete [] lookup;
20                 lookup=0;
21                 size=0;
22         }
23         size=_size;
24         if (!size)
25                 return;
26         lookup=new gColor[size];
27         
28         for (int i=0; i<size; i++)
29         {
30                 gRGB col;
31                 if (i)
32                 {
33                         int rdiff=-start.r+end.r;
34                         int gdiff=-start.g+end.g;
35                         int bdiff=-start.b+end.b;
36                         int adiff=-start.a+end.a;
37                         rdiff*=i; rdiff/=(size-1);
38                         gdiff*=i; gdiff/=(size-1);
39                         bdiff*=i; bdiff/=(size-1);
40                         adiff*=i; adiff/=(size-1);
41                         col.r=start.r+rdiff;
42                         col.g=start.g+gdiff;
43                         col.b=start.b+bdiff;
44                         col.a=start.a+adiff;
45                 } else
46                         col=start;
47                 lookup[i]=pal.findColor(col);
48         }
49 }
50
51 gSurface::~gSurface()
52 {
53 }
54
55 gSurfaceSystem::gSurfaceSystem(eSize size, int _bpp)
56 {
57         x=size.width();
58         y=size.height();
59         bpp=_bpp;
60         switch (bpp)
61         {
62         case 8:
63                 bypp=1;
64                 break;
65         case 15:
66         case 16:
67                 bypp=2;
68                 break;
69         case 24:                // never use 24bit mode
70         case 32:
71                 bypp=4;
72                 break;
73         default:
74                 bypp=(bpp+7)/8;
75         }
76         stride=x*bypp;
77         clut.colors=0;
78         clut.data=0;
79         data=malloc(x*y*bypp);
80 }
81
82 gSurfaceSystem::~gSurfaceSystem()
83 {
84         free(data);
85         delete[] clut.data;
86 }
87
88 gPixmap *gPixmap::lock()
89 {
90         contentlock.lock(1);
91         return this;
92 }
93
94 void gPixmap::unlock()
95 {
96         contentlock.unlock(1);
97 }
98
99 void gPixmap::fill(const gRegion &region, const gColor &color)
100 {
101         unsigned int i;
102         for (i=0; i<region.rects.size(); ++i)
103         {
104                 const eRect &area = region.rects[i];
105                 if ((area.height()<=0) || (area.width()<=0))
106                         continue;
107                 if (surface->bpp == 8)
108                 {
109                         for (int y=area.top(); y<area.bottom(); y++)
110                                 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
111                 } else if (surface->bpp == 32)
112                         for (int y=area.top(); y<area.bottom(); y++)
113                         {
114                                 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
115                                 int x=area.width();
116                                 __u32 col;
117
118                                 if (surface->clut.data && color < surface->clut.colors)
119                                         col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
120                                 else
121                                         col=0x10101*color;
122                                 col^=0xFF000000;                        
123                                 while (x--)
124                                         *dst++=col;
125                         }
126                 else
127                         eWarning("couldn't fill %d bpp", surface->bpp);
128         }
129 }
130
131 void gPixmap::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag)
132 {
133         for (unsigned int i=0; i<clip.rects.size(); ++i)
134         {
135                 eRect area=eRect(pos, src.size());
136                 area&=clip.rects[i];
137                 area&=eRect(ePoint(0, 0), size());
138                 if ((area.width()<0) || (area.height()<0))
139                         continue;
140
141                 eRect srcarea=area;
142                 srcarea.moveBy(-pos.x(), -pos.y());
143                 
144                 if ((surface->bpp == 8) && (src.surface->bpp==8))
145                 {
146                         __u8 *srcptr=(__u8*)src.surface->data;
147                         __u8 *dstptr=(__u8*)surface->data;
148         
149                         srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
150                         dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
151                         for (int y=0; y<area.height(); y++)
152                         {
153                                 if (flag & blitAlphaTest)
154                                 {
155                       // no real alphatest yet
156                                         int width=area.width();
157                                         unsigned char *src=(unsigned char*)srcptr;
158                                         unsigned char *dst=(unsigned char*)dstptr;
159                                                 // use duff's device here!
160                                         while (width--)
161                                         {
162                                                 if (!*src)
163                                                 {
164                                                         src++;
165                                                         dst++;
166                                                 } else
167                                                         *dst++=*src++;
168                                         }
169                                 } else
170                                         memcpy(dstptr, srcptr, area.width()*surface->bypp);
171                                 srcptr+=src.surface->stride;
172                                 dstptr+=surface->stride;
173                         }
174                 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
175                 {       
176                         __u8 *srcptr=(__u8*)src.surface->data;
177                         __u8 *dstptr=(__u8*)surface->data; // !!
178                         __u32 pal[256];
179
180                         for (int i=0; i<256; ++i)
181                         {
182                                 if (src.surface->clut.data && (i<src.surface->clut.colors))
183                                         pal[i]=(src.surface->clut.data[i].a<<24)|(src.surface->clut.data[i].r<<16)|(src.surface->clut.data[i].g<<8)|(src.surface->clut.data[i].b);
184                                 else
185                                         pal[i]=0x010101*i;
186                                 pal[i]^=0xFF000000;
187                         }
188         
189                         srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
190                         dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
191                         for (int y=0; y<area.height(); y++)
192                         {
193                                 if (flag & blitAlphaTest)
194                                 {
195                       // no real alphatest yet
196                                         int width=area.width();
197                                         unsigned char *src=(unsigned char*)srcptr;
198                                         __u32 *dst=(__u32*)dstptr;
199                                                 // use duff's device here!
200                                         while (width--)
201                                         {
202                                                 if (!*src)
203                                                 {
204                                                         src++;
205                                                         dst++;
206                                                 } else
207                                                         *dst++=pal[*src++];
208                                         }
209                                 } else
210                                 {
211                                         int width=area.width();
212                                         unsigned char *src=(unsigned char*)srcptr;
213                                         __u32 *dst=(__u32*)dstptr;
214                                         while (width--)
215                                                 *dst++=pal[*src++];
216                                 }
217                                 srcptr+=src.surface->stride;
218                                 dstptr+=surface->stride;
219                         }
220                 } else
221                         eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
222         }
223 }
224
225 void gPixmap::mergePalette(const gPixmap &target)
226 {
227         eDebug("merge palette! %p %p", surface, target.surface);
228         if ((!surface->clut.colors) || (!target.surface->clut.colors))
229                 return;
230
231         gColor *lookup=new gColor[surface->clut.colors];
232
233         for (int i=0; i<surface->clut.colors; i++)
234                 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
235         
236         delete [] surface->clut.data;
237         surface->clut.colors=target.surface->clut.colors;
238         surface->clut.data=new gRGB[surface->clut.colors];
239         memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
240
241         __u8 *dstptr=(__u8*)surface->data;
242
243         for (int ay=0; ay<surface->y; ay++)
244         {
245                 for (int ax=0; ax<surface->x; ax++)
246                         dstptr[ax]=lookup[dstptr[ax]];
247                 dstptr+=surface->stride;
248         }
249         
250         delete [] lookup;
251 }
252
253 static inline int sgn(int a)
254 {
255         if (a < 0)
256                 return -1;
257         else if (!a)
258                 return 0;
259         else
260                 return 1;
261 }
262
263 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
264 {
265         __u8 *srf8 = 0;
266         __u32 *srf32 = 0; 
267         int stride = surface->stride;
268         
269         if (clip.rects.empty())
270                 return;
271                 
272         __u32 col;
273         if (surface->bpp == 8)
274         {
275                 srf8 = (__u8*)surface->data;
276         } else if (surface->bpp == 32)
277         {
278                 srf32 = (__u32*)surface->data;
279                 
280                 if (surface->clut.data && color < surface->clut.colors)
281                         col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
282                 else
283                         col=0x10101*color;
284                 col^=0xFF000000;                        
285         }
286         
287         int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
288         int dx, dy, x, y, s1, s2, e, temp, swap, i;
289         dy=abs(yb-ya);
290         dx=abs(xb-xa);
291         s1=sgn(xb-xa);
292         s2=sgn(yb-ya);
293         x=xa;
294         y=ya;
295         if (dy>dx)
296         {
297                 temp=dx;
298                 dx=dy;
299                 dy=temp;
300                 swap=1;
301         } else
302                 swap=0;
303         e = 2*dy-dx;
304         
305         int lasthit = 0;
306         for(i=1; i<=dx; i++)
307         {
308                                 /* i don't like this clipping loop, but the only */
309                                 /* other choice i see is to calculate the intersections */
310                                 /* before iterating through the pixels. */
311                                 
312                                 /* one could optimize this because of the ordering */
313                                 /* of the bands. */
314                                 
315                 lasthit = 0;
316                 int a = lasthit;
317                 
318                         /* if last pixel was invisble, first check bounding box */
319                 if (a == -1)
320                 {
321                                 /* check if we just got into the bbox again */
322                         if (clip.extends.contains(x, y))
323                                 lasthit = a = 0;
324                         else
325                                 goto fail;
326                 } else if (!clip.rects[a].contains(x, y))
327                 {
328                         do
329                         {
330                                 ++a;
331                                 if (a == clip.rects.size())
332                                         a = 0;
333                                 if (a == lasthit)
334                                 {
335                                         goto fail;
336                                         lasthit = -1;
337                                 }
338                         } while (!clip.rects[a].contains(x, y));
339                         lasthit = a;
340                 }
341                 
342                 if (srf8)
343                         srf8[y * stride + x] = color;
344                 if (srf32)
345                         srf32[y * stride/4 + x] = col;
346 fail:
347                 while (e>=0)
348                 {
349                         if (swap==1) x+=s1;
350                         else y+=s2;
351                         e-=2*dx;
352                 }
353     if (swap==1)
354         y+=s2;
355                 else
356                         x+=s1;
357                 e+=2*dy;
358         }
359 }
360
361 gColor gPalette::findColor(const gRGB &rgb) const
362 {
363         int difference=1<<30, best_choice=0;
364         for (int t=0; t<colors; t++)
365         {
366                 int ttd;
367                 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
368                 ttd=td;
369                 if (ttd>=difference)
370                         continue;
371                 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
372                 ttd+=td;
373                 if (ttd>=difference)
374                         continue;
375                 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
376                 ttd+=td;
377                 if (ttd>=difference)
378                         continue;
379                 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
380                 ttd+=td;
381                 if (ttd>=difference)
382                         continue;
383                 if (!ttd)
384                         return t;
385                 difference=ttd;
386                 best_choice=t;
387         }
388         return best_choice;
389 }
390
391 DEFINE_REF(gPixmap);
392
393 gPixmap::~gPixmap()
394 {
395 }
396
397 gPixmap::gPixmap(gSurface *surface): surface(surface)
398 {
399 }
400
401 gPixmap::gPixmap(eSize size, int bpp)
402 {
403         surface = new gSurfaceSystem(size, bpp);
404 }
405