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