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 = malloc(y * stride);
114 gSurface::~gSurface()
119 gAccel::getInstance()->accelFree(data_phys);
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::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag)
180 for (unsigned int i=0; i<clip.rects.size(); ++i)
182 eRect area=eRect(pos, src.size());
184 area&=eRect(ePoint(0, 0), size());
186 if ((area.width()<0) || (area.height()<0))
190 srcarea.moveBy(-pos.x(), -pos.y());
192 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
193 if (!gAccel::getInstance()->blit(surface, src.surface, area.topLeft(), srcarea, flag))
195 flag &= ~ blitAlphaBlend;
197 if ((surface->bpp == 8) && (src.surface->bpp==8))
199 __u8 *srcptr=(__u8*)src.surface->data;
200 __u8 *dstptr=(__u8*)surface->data;
202 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
203 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
204 for (int y=0; y<area.height(); y++)
206 if (flag & blitAlphaTest)
208 // no real alphatest yet
209 int width=area.width();
210 unsigned char *src=(unsigned char*)srcptr;
211 unsigned char *dst=(unsigned char*)dstptr;
212 // use duff's device here!
223 memcpy(dstptr, srcptr, area.width()*surface->bypp);
224 srcptr+=src.surface->stride;
225 dstptr+=surface->stride;
227 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
229 __u32 *srcptr=(__u32*)src.surface->data;
230 __u32 *dstptr=(__u32*)surface->data;
232 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
233 dstptr+=area.left()+area.top()*surface->stride/4;
234 for (int y=0; y<area.height(); y++)
236 if (flag & blitAlphaTest)
238 int width=area.width();
239 unsigned long *src=(unsigned long*)srcptr;
240 unsigned long *dst=(unsigned long*)dstptr;
244 if (!((*src)&0xFF000000))
251 } else if (flag & blitAlphaBlend)
253 // uh oh. this is only until hardware accel is working.
255 int width=area.width();
257 unsigned char *src=(unsigned char*)srcptr;
258 unsigned char *dst=(unsigned char*)dstptr;
260 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
263 unsigned char sa = src[3];
264 unsigned char sr = src[2];
265 unsigned char sg = src[1];
266 unsigned char sb = src[0];
268 unsigned char da = dst[3];
269 unsigned char dr = dst[2];
270 unsigned char dg = dst[1];
271 unsigned char db = dst[0];
273 dst[3] = BLEND(0xFF, da, sa);
274 dst[2] = BLEND(sr, dr, sa);
275 dst[1] = BLEND(sg, dg, sa);
276 dst[0] = BLEND(sb, db, sa);
281 memcpy(dstptr, srcptr, area.width()*surface->bypp);
282 srcptr+=src.surface->stride/4;
283 dstptr+=surface->stride/4;
285 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
287 __u8 *srcptr=(__u8*)src.surface->data;
288 __u8 *dstptr=(__u8*)surface->data; // !!
291 for (int i=0; i<256; ++i)
293 if (src.surface->clut.data && (i<src.surface->clut.colors))
294 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);
300 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
301 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
302 for (int y=0; y<area.height(); y++)
304 if (flag & blitAlphaTest)
306 // no real alphatest yet
307 int width=area.width();
308 unsigned char *src=(unsigned char*)srcptr;
309 __u32 *dst=(__u32*)dstptr;
310 // use duff's device here!
322 int width=area.width();
323 unsigned char *src=(unsigned char*)srcptr;
324 __u32 *dst=(__u32*)dstptr;
328 srcptr+=src.surface->stride;
329 dstptr+=surface->stride;
332 eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
336 void gPixmap::mergePalette(const gPixmap &target)
338 if ((!surface->clut.colors) || (!target.surface->clut.colors))
341 gColor *lookup=new gColor[surface->clut.colors];
343 for (int i=0; i<surface->clut.colors; i++)
344 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
346 delete [] surface->clut.data;
347 surface->clut.colors=target.surface->clut.colors;
348 surface->clut.data=new gRGB[surface->clut.colors];
349 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
351 __u8 *dstptr=(__u8*)surface->data;
353 for (int ay=0; ay<surface->y; ay++)
355 for (int ax=0; ax<surface->x; ax++)
356 dstptr[ax]=lookup[dstptr[ax]];
357 dstptr+=surface->stride;
363 static inline int sgn(int a)
373 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
377 int stride = surface->stride;
379 if (clip.rects.empty())
383 if (surface->bpp == 8)
385 srf8 = (__u8*)surface->data;
386 } else if (surface->bpp == 32)
388 srf32 = (__u32*)surface->data;
390 if (surface->clut.data && color < surface->clut.colors)
391 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
397 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
398 int dx, dy, x, y, s1, s2, e, temp, swap, i;
418 /* i don't like this clipping loop, but the only */
419 /* other choice i see is to calculate the intersections */
420 /* before iterating through the pixels. */
422 /* one could optimize this because of the ordering */
428 /* if last pixel was invisble, first check bounding box */
431 /* check if we just got into the bbox again */
432 if (clip.extends.contains(x, y))
436 } else if (!clip.rects[a].contains(x, y))
441 if (a == clip.rects.size())
448 } while (!clip.rects[a].contains(x, y));
453 srf8[y * stride + x] = color;
455 srf32[y * stride/4 + x] = col;
471 gColor gPalette::findColor(const gRGB &rgb) const
475 return (rgb.r + rgb.g + rgb.b) / 3;
477 int difference=1<<30, best_choice=0;
478 for (int t=0; t<colors; t++)
481 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
485 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
489 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
493 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
511 gPixmap::gPixmap(gSurface *surface): surface(surface)
515 gPixmap::gPixmap(eSize size, int bpp, int accel)
517 surface = new gSurface(size, bpp, accel);