- use "xadvance" instead of "glpyh->width" as bounding box width
[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 #if 0
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 #endif
252 }
253
254 static inline int sgn(int a)
255 {
256         if (a < 0)
257                 return -1;
258         else if (!a)
259                 return 0;
260         else
261                 return 1;
262 }
263
264 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
265 {
266         __u8 *srf8 = 0;
267         __u32 *srf32 = 0; 
268         int stride = surface->stride;
269         
270         if (clip.rects.empty())
271                 return;
272                 
273         __u32 col;
274         if (surface->bpp == 8)
275         {
276                 srf8 = (__u8*)surface->data;
277         } else if (surface->bpp == 32)
278         {
279                 srf32 = (__u32*)surface->data;
280                 
281                 if (surface->clut.data && color < surface->clut.colors)
282                         col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
283                 else
284                         col=0x10101*color;
285                 col^=0xFF000000;                        
286         }
287         
288         int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
289         int dx, dy, x, y, s1, s2, e, temp, swap, i;
290         dy=abs(yb-ya);
291         dx=abs(xb-xa);
292         s1=sgn(xb-xa);
293         s2=sgn(yb-ya);
294         x=xa;
295         y=ya;
296         if (dy>dx)
297         {
298                 temp=dx;
299                 dx=dy;
300                 dy=temp;
301                 swap=1;
302         } else
303                 swap=0;
304         e = 2*dy-dx;
305         
306         int lasthit = 0;
307         for(i=1; i<=dx; i++)
308         {
309                                 /* i don't like this clipping loop, but the only */
310                                 /* other choice i see is to calculate the intersections */
311                                 /* before iterating through the pixels. */
312                                 
313                                 /* one could optimize this because of the ordering */
314                                 /* of the bands. */
315                                 
316                 lasthit = 0;
317                 int a = lasthit;
318                 
319                         /* if last pixel was invisble, first check bounding box */
320                 if (a == -1)
321                 {
322                                 /* check if we just got into the bbox again */
323                         if (clip.extends.contains(x, y))
324                                 lasthit = a = 0;
325                         else
326                                 goto fail;
327                 } else if (!clip.rects[a].contains(x, y))
328                 {
329                         do
330                         {
331                                 ++a;
332                                 if (a == clip.rects.size())
333                                         a = 0;
334                                 if (a == lasthit)
335                                 {
336                                         goto fail;
337                                         lasthit = -1;
338                                 }
339                         } while (!clip.rects[a].contains(x, y));
340                         lasthit = a;
341                 }
342                 
343                 if (srf8)
344                         srf8[y * stride + x] = color;
345                 if (srf32)
346                         srf32[y * stride/4 + x] = col;
347 fail:
348                 while (e>=0)
349                 {
350                         if (swap==1) x+=s1;
351                         else y+=s2;
352                         e-=2*dx;
353                 }
354     if (swap==1)
355         y+=s2;
356                 else
357                         x+=s1;
358                 e+=2*dy;
359         }
360 }
361
362 gColor gPalette::findColor(const gRGB &rgb) const
363 {
364         int difference=1<<30, best_choice=0;
365         for (int t=0; t<colors; t++)
366         {
367                 int ttd;
368                 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
369                 ttd=td;
370                 if (ttd>=difference)
371                         continue;
372                 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
373                 ttd+=td;
374                 if (ttd>=difference)
375                         continue;
376                 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
377                 ttd+=td;
378                 if (ttd>=difference)
379                         continue;
380                 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
381                 ttd+=td;
382                 if (ttd>=difference)
383                         continue;
384                 if (!ttd)
385                         return t;
386                 difference=ttd;
387                 best_choice=t;
388         }
389         return best_choice;
390 }
391
392 DEFINE_REF(gPixmap);
393
394 gPixmap::~gPixmap()
395 {
396 }
397
398 gPixmap::gPixmap(gSurface *surface): surface(surface)
399 {
400 }
401
402 gPixmap::gPixmap(eSize size, int bpp)
403 {
404         surface = new gSurfaceSystem(size, bpp);
405 }
406