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 static void blit_8i_to_32(__u32 *dst, __u8 *src, __u32 *pal, int width)
216 static void blit_8i_to_32_at(__u32 *dst, __u8 *src, __u32 *pal, int width)
220 if (!(pal[*src]&0x80000000))
229 /* WARNING, this function is not endian safe! */
230 static void blit_8i_to_32_ab(__u32 *dst, __u8 *src, __u32 *pal, int width)
234 #define BLEND(x, y, a) (y + (((x-y) * a)>>8))
235 __u32 srccol = pal[*src++];
237 unsigned char sb = srccol & 0xFF;
238 unsigned char sg = (srccol >> 8) & 0xFF;
239 unsigned char sr = (srccol >> 16) & 0xFF;
240 unsigned char sa = (srccol >> 24) & 0xFF;
242 unsigned char db = dstcol & 0xFF;
243 unsigned char dg = (dstcol >> 8) & 0xFF;
244 unsigned char dr = (dstcol >> 16) & 0xFF;
245 unsigned char da = (dstcol >> 24) & 0xFF;
247 da = BLEND(0xFF, da, sa) & 0xFF;
248 dr = BLEND(sr, dr, sa) & 0xFF;
249 dg = BLEND(sg, dg, sa) & 0xFF;
250 db = BLEND(sb, db, sa) & 0xFF;
253 *dst++ = db | (dg << 8) | (dr << 16) | (da << 24);
258 void gPixmap::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag)
260 for (unsigned int i=0; i<clip.rects.size(); ++i)
262 eRect area=eRect(pos, src.size());
264 area&=eRect(ePoint(0, 0), size());
266 if ((area.width()<0) || (area.height()<0))
270 srcarea.moveBy(-pos.x(), -pos.y());
272 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
273 if (!gAccel::getInstance()->blit(surface, src.surface, area.topLeft(), srcarea, flag))
276 if ((surface->bpp == 8) && (src.surface->bpp==8))
278 __u8 *srcptr=(__u8*)src.surface->data;
279 __u8 *dstptr=(__u8*)surface->data;
281 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
282 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
283 for (int y=0; y<area.height(); y++)
285 if (flag & (blitAlphaTest|blitAlphaBlend))
287 // no real alphatest yet
288 int width=area.width();
289 unsigned char *src=(unsigned char*)srcptr;
290 unsigned char *dst=(unsigned char*)dstptr;
291 // use duff's device here!
302 memcpy(dstptr, srcptr, area.width()*surface->bypp);
303 srcptr+=src.surface->stride;
304 dstptr+=surface->stride;
306 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
308 __u32 *srcptr=(__u32*)src.surface->data;
309 __u32 *dstptr=(__u32*)surface->data;
311 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
312 dstptr+=area.left()+area.top()*surface->stride/4;
313 for (int y=0; y<area.height(); y++)
315 if (flag & blitAlphaTest)
317 int width=area.width();
318 unsigned long *src=(unsigned long*)srcptr;
319 unsigned long *dst=(unsigned long*)dstptr;
323 if (!((*src)&0xFF000000))
330 } else if (flag & blitAlphaBlend)
332 // uh oh. this is only until hardware accel is working.
334 int width=area.width();
336 unsigned char *src=(unsigned char*)srcptr;
337 unsigned char *dst=(unsigned char*)dstptr;
339 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
342 unsigned char sa = src[3];
343 unsigned char sr = src[2];
344 unsigned char sg = src[1];
345 unsigned char sb = src[0];
347 unsigned char da = dst[3];
348 unsigned char dr = dst[2];
349 unsigned char dg = dst[1];
350 unsigned char db = dst[0];
352 dst[3] = BLEND(0xFF, da, sa);
353 dst[2] = BLEND(sr, dr, sa);
354 dst[1] = BLEND(sg, dg, sa);
355 dst[0] = BLEND(sb, db, sa);
361 memcpy(dstptr, srcptr, area.width()*surface->bypp);
362 srcptr+=src.surface->stride/4;
363 dstptr+=surface->stride/4;
365 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
367 __u8 *srcptr=(__u8*)src.surface->data;
368 __u8 *dstptr=(__u8*)surface->data; // !!
371 for (int i=0; i<256; ++i)
373 if (src.surface->clut.data && (i<src.surface->clut.colors))
374 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);
380 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
381 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
382 for (int y=0; y<area.height(); y++)
384 int width=area.width();
385 unsigned char *psrc=(unsigned char*)srcptr;
386 __u32 *dst=(__u32*)dstptr;
387 if (flag & blitAlphaTest)
388 blit_8i_to_32_at(dst, psrc, pal, width);
389 else if (flag & blitAlphaBlend)
390 blit_8i_to_32_ab(dst, psrc, pal, width);
392 blit_8i_to_32(dst, psrc, pal, width);
393 srcptr+=src.surface->stride;
394 dstptr+=surface->stride;
397 eWarning("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
401 void gPixmap::mergePalette(const gPixmap &target)
403 if ((!surface->clut.colors) || (!target.surface->clut.colors))
406 gColor *lookup=new gColor[surface->clut.colors];
408 for (int i=0; i<surface->clut.colors; i++)
409 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
411 delete [] surface->clut.data;
412 surface->clut.colors=target.surface->clut.colors;
413 surface->clut.data=new gRGB[surface->clut.colors];
414 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
416 __u8 *dstptr=(__u8*)surface->data;
418 for (int ay=0; ay<surface->y; ay++)
420 for (int ax=0; ax<surface->x; ax++)
421 dstptr[ax]=lookup[dstptr[ax]];
422 dstptr+=surface->stride;
428 static inline int sgn(int a)
438 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
442 int stride = surface->stride;
444 if (clip.rects.empty())
448 if (surface->bpp == 8)
450 srf8 = (__u8*)surface->data;
451 } else if (surface->bpp == 32)
453 srf32 = (__u32*)surface->data;
455 if (surface->clut.data && color < surface->clut.colors)
456 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
462 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
463 int dx, dy, x, y, s1, s2, e, temp, swap, i;
483 /* i don't like this clipping loop, but the only */
484 /* other choice i see is to calculate the intersections */
485 /* before iterating through the pixels. */
487 /* one could optimize this because of the ordering */
493 /* if last pixel was invisble, first check bounding box */
496 /* check if we just got into the bbox again */
497 if (clip.extends.contains(x, y))
501 } else if (!clip.rects[a].contains(x, y))
506 if ((unsigned int)a == clip.rects.size())
513 } while (!clip.rects[a].contains(x, y));
518 srf8[y * stride + x] = color;
520 srf32[y * stride/4 + x] = col;
536 gColor gPalette::findColor(const gRGB &rgb) const
540 return (rgb.r + rgb.g + rgb.b) / 3;
542 int difference=1<<30, best_choice=0;
543 for (int t=0; t<colors; t++)
546 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
550 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
554 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
558 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
574 if (must_delete_surface)
578 gPixmap::gPixmap(gSurface *surface)
579 :surface(surface), must_delete_surface(false)
583 gPixmap::gPixmap(eSize size, int bpp, int accel)
584 :must_delete_surface(true)
586 surface = new gSurface(size, bpp, accel);