1 #include <lib/gdi/gpixmap.h>
2 #include <lib/gdi/region.h>
3 #include <lib/gdi/accel.h>
10 gLookup::gLookup(int size, const gPalette &pal, const gRGB &start, const gRGB &end)
13 build(size, pal, start, end);
16 void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRGB &end)
27 lookup=new gColor[size];
29 for (int i=0; i<size; i++)
34 int rdiff=-start.r+end.r;
35 int gdiff=-start.g+end.g;
36 int bdiff=-start.b+end.b;
37 int adiff=-start.a+end.a;
38 rdiff*=i; rdiff/=(size-1);
39 gdiff*=i; gdiff/=(size-1);
40 bdiff*=i; bdiff/=(size-1);
41 adiff*=i; adiff/=(size-1);
48 lookup[i]=pal.findColor(col);
66 gSurface::gSurface(eSize size, int _bpp, int accel)
81 case 24: // never use 24bit mode
99 if (gAccel::getInstance())
100 eDebug("accel memory: %d", gAccel::getInstance()->accelAlloc(data, data_phys, y * stride));
102 eDebug("no accel available");
109 data = new unsigned char [y * stride];
114 gSurface::~gSurface()
119 gAccel::getInstance()->accelFree(data_phys);
121 delete [] (unsigned char*)data;
127 gPixmap *gPixmap::lock()
133 void gPixmap::unlock()
135 contentlock.unlock(1);
138 void gPixmap::fill(const gRegion ®ion, const gColor &color)
141 for (i=0; i<region.rects.size(); ++i)
143 const eRect &area = region.rects[i];
144 if ((area.height()<=0) || (area.width()<=0))
147 if (surface->bpp == 8)
149 for (int y=area.top(); y<area.bottom(); y++)
150 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
151 } else if (surface->bpp == 32)
155 if (surface->clut.data && color < surface->clut.colors)
156 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
162 if (surface->data_phys && gAccel::getInstance())
163 if (!gAccel::getInstance()->fill(surface, area, col))
166 for (int y=area.top(); y<area.bottom(); y++)
168 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
174 eWarning("couldn't fill %d bpp", surface->bpp);
178 void gPixmap::fill(const gRegion ®ion, const gRGB &color)
181 for (i=0; i<region.rects.size(); ++i)
183 const eRect &area = region.rects[i];
184 if ((area.height()<=0) || (area.width()<=0))
187 if (surface->bpp == 32)
194 if (surface->data_phys && gAccel::getInstance())
195 if (!gAccel::getInstance()->fill(surface, area, col))
198 for (int y=area.top(); y<area.bottom(); y++)
200 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
206 eWarning("couldn't rgbfill %d bpp", surface->bpp);
210 void gPixmap::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag)
212 for (unsigned int i=0; i<clip.rects.size(); ++i)
214 eRect area=eRect(pos, src.size());
216 area&=eRect(ePoint(0, 0), size());
218 if ((area.width()<0) || (area.height()<0))
222 srcarea.moveBy(-pos.x(), -pos.y());
224 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
225 if (!gAccel::getInstance()->blit(surface, src.surface, area.topLeft(), srcarea, flag))
227 flag &= ~ blitAlphaBlend;
229 if ((surface->bpp == 8) && (src.surface->bpp==8))
231 __u8 *srcptr=(__u8*)src.surface->data;
232 __u8 *dstptr=(__u8*)surface->data;
234 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
235 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
236 for (int y=0; y<area.height(); y++)
238 if (flag & blitAlphaTest)
240 // no real alphatest yet
241 int width=area.width();
242 unsigned char *src=(unsigned char*)srcptr;
243 unsigned char *dst=(unsigned char*)dstptr;
244 // use duff's device here!
255 memcpy(dstptr, srcptr, area.width()*surface->bypp);
256 srcptr+=src.surface->stride;
257 dstptr+=surface->stride;
259 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
261 __u32 *srcptr=(__u32*)src.surface->data;
262 __u32 *dstptr=(__u32*)surface->data;
264 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
265 dstptr+=area.left()+area.top()*surface->stride/4;
266 for (int y=0; y<area.height(); y++)
268 if (flag & blitAlphaTest)
270 int width=area.width();
271 unsigned long *src=(unsigned long*)srcptr;
272 unsigned long *dst=(unsigned long*)dstptr;
276 if (!((*src)&0xFF000000))
283 } else if (flag & blitAlphaBlend)
285 // uh oh. this is only until hardware accel is working.
287 int width=area.width();
289 unsigned char *src=(unsigned char*)srcptr;
290 unsigned char *dst=(unsigned char*)dstptr;
292 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
295 unsigned char sa = src[3];
296 unsigned char sr = src[2];
297 unsigned char sg = src[1];
298 unsigned char sb = src[0];
300 unsigned char da = dst[3];
301 unsigned char dr = dst[2];
302 unsigned char dg = dst[1];
303 unsigned char db = dst[0];
305 dst[3] = BLEND(0xFF, da, sa);
306 dst[2] = BLEND(sr, dr, sa);
307 dst[1] = BLEND(sg, dg, sa);
308 dst[0] = BLEND(sb, db, sa);
313 memcpy(dstptr, srcptr, area.width()*surface->bypp);
314 srcptr+=src.surface->stride/4;
315 dstptr+=surface->stride/4;
317 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
319 __u8 *srcptr=(__u8*)src.surface->data;
320 __u8 *dstptr=(__u8*)surface->data; // !!
323 for (int i=0; i<256; ++i)
325 if (src.surface->clut.data && (i<src.surface->clut.colors))
326 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);
332 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
333 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
334 for (int y=0; y<area.height(); y++)
336 if (flag & blitAlphaTest)
338 // no real alphatest yet
339 int width=area.width();
340 unsigned char *src=(unsigned char*)srcptr;
341 __u32 *dst=(__u32*)dstptr;
342 // use duff's device here!
345 if (!(pal[*src]&0x80000000))
354 int width=area.width();
355 unsigned char *src=(unsigned char*)srcptr;
356 __u32 *dst=(__u32*)dstptr;
360 srcptr+=src.surface->stride;
361 dstptr+=surface->stride;
364 eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
368 void gPixmap::mergePalette(const gPixmap &target)
370 if ((!surface->clut.colors) || (!target.surface->clut.colors))
373 gColor *lookup=new gColor[surface->clut.colors];
375 for (int i=0; i<surface->clut.colors; i++)
376 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
378 delete [] surface->clut.data;
379 surface->clut.colors=target.surface->clut.colors;
380 surface->clut.data=new gRGB[surface->clut.colors];
381 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
383 __u8 *dstptr=(__u8*)surface->data;
385 for (int ay=0; ay<surface->y; ay++)
387 for (int ax=0; ax<surface->x; ax++)
388 dstptr[ax]=lookup[dstptr[ax]];
389 dstptr+=surface->stride;
395 static inline int sgn(int a)
405 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
409 int stride = surface->stride;
411 if (clip.rects.empty())
415 if (surface->bpp == 8)
417 srf8 = (__u8*)surface->data;
418 } else if (surface->bpp == 32)
420 srf32 = (__u32*)surface->data;
422 if (surface->clut.data && color < surface->clut.colors)
423 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
429 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
430 int dx, dy, x, y, s1, s2, e, temp, swap, i;
450 /* i don't like this clipping loop, but the only */
451 /* other choice i see is to calculate the intersections */
452 /* before iterating through the pixels. */
454 /* one could optimize this because of the ordering */
460 /* if last pixel was invisble, first check bounding box */
463 /* check if we just got into the bbox again */
464 if (clip.extends.contains(x, y))
468 } else if (!clip.rects[a].contains(x, y))
473 if ((unsigned int)a == clip.rects.size())
480 } while (!clip.rects[a].contains(x, y));
485 srf8[y * stride + x] = color;
487 srf32[y * stride/4 + x] = col;
503 gColor gPalette::findColor(const gRGB &rgb) const
507 return (rgb.r + rgb.g + rgb.b) / 3;
509 int difference=1<<30, best_choice=0;
510 for (int t=0; t<colors; t++)
513 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
517 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
521 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
525 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
541 if (must_delete_surface)
545 gPixmap::gPixmap(gSurface *surface)
546 :surface(surface), must_delete_surface(false)
550 gPixmap::gPixmap(eSize size, int bpp, int accel)
551 :must_delete_surface(true)
553 surface = new gSurface(size, bpp, accel);