3 #include <lib/gdi/gpixmap.h>
4 #include <lib/gdi/region.h>
5 #include <lib/gdi/accel.h>
9 #error "no BYTE_ORDER defined!"
17 gLookup::gLookup(int size, const gPalette &pal, const gRGB &start, const gRGB &end)
20 build(size, pal, start, end);
23 void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRGB &end)
34 lookup=new gColor[size];
36 for (int i=0; i<size; i++)
41 int rdiff=-start.r+end.r;
42 int gdiff=-start.g+end.g;
43 int bdiff=-start.b+end.b;
44 int adiff=-start.a+end.a;
45 rdiff*=i; rdiff/=(size-1);
46 gdiff*=i; gdiff/=(size-1);
47 bdiff*=i; bdiff/=(size-1);
48 adiff*=i; adiff/=(size-1);
55 lookup[i]=pal.findColor(col);
73 gSurface::gSurface(eSize size, int _bpp, int accel)
88 case 24: // never use 24bit mode
110 if (gAccel::getInstance())
111 eDebug("accel memory: %d", gAccel::getInstance()->accelAlloc(data, data_phys, y * stride + pal_size));
113 eDebug("no accel available");
120 data = new unsigned char [y * stride];
125 gSurface::~gSurface()
130 gAccel::getInstance()->accelFree(data_phys);
132 delete [] (unsigned char*)data;
138 gPixmap *gPixmap::lock()
144 void gPixmap::unlock()
146 contentlock.unlock(1);
149 void gPixmap::fill(const gRegion ®ion, const gColor &color)
152 for (i=0; i<region.rects.size(); ++i)
154 const eRect &area = region.rects[i];
158 if (surface->bpp == 8)
160 for (int y=area.top(); y<area.bottom(); y++)
161 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
162 } else if (surface->bpp == 16)
166 if (surface->clut.data && color < surface->clut.colors)
167 icol=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
170 #if BYTE_ORDER == LITTLE_ENDIAN
171 __u16 col = bswap_16(((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19);
173 __u16 col = ((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19;
175 for (int y=area.top(); y<area.bottom(); y++)
177 __u16 *dst=(__u16*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
182 } else if (surface->bpp == 32)
186 if (surface->clut.data && color < surface->clut.colors)
187 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
193 if (surface->data_phys && gAccel::getInstance())
194 if (!gAccel::getInstance()->fill(surface, area, col))
197 for (int y=area.top(); y<area.bottom(); y++)
199 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
205 eWarning("couldn't fill %d bpp", surface->bpp);
209 void gPixmap::fill(const gRegion ®ion, const gRGB &color)
212 for (i=0; i<region.rects.size(); ++i)
214 const eRect &area = region.rects[i];
218 if (surface->bpp == 32)
225 if (surface->data_phys && gAccel::getInstance())
226 if (!gAccel::getInstance()->fill(surface, area, col))
229 for (int y=area.top(); y<area.bottom(); y++)
231 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
236 } else if (surface->bpp == 16)
238 __u32 icol = color.argb();
239 #if BYTE_ORDER == LITTLE_ENDIAN
240 __u16 col = bswap_16(((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19);
242 __u16 col = ((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19;
244 for (int y=area.top(); y<area.bottom(); y++)
246 __u16 *dst=(__u16*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
252 eWarning("couldn't rgbfill %d bpp", surface->bpp);
256 static inline void blit_8i_to_32(__u32 *dst, __u8 *src, __u32 *pal, int width)
262 static inline void blit_8i_to_32_at(__u32 *dst, __u8 *src, __u32 *pal, int width)
266 if (!(pal[*src]&0x80000000))
275 static inline void blit_8i_to_16(__u16 *dst, __u8 *src, __u32 *pal, int width)
278 *dst++=pal[*src++] & 0xFFFF;
281 static inline void blit_8i_to_16_at(__u16 *dst, __u8 *src, __u32 *pal, int width)
285 if (!(pal[*src]&0x80000000))
290 *dst++=pal[*src++] & 0xFFFF;
294 /* WARNING, this function is not endian safe! */
295 static void blit_8i_to_32_ab(__u32 *dst, __u8 *src, __u32 *pal, int width)
299 #define BLEND(x, y, a) (y + (((x-y) * a)>>8))
300 __u32 srccol = pal[*src++];
302 unsigned char sb = srccol & 0xFF;
303 unsigned char sg = (srccol >> 8) & 0xFF;
304 unsigned char sr = (srccol >> 16) & 0xFF;
305 unsigned char sa = (srccol >> 24) & 0xFF;
307 unsigned char db = dstcol & 0xFF;
308 unsigned char dg = (dstcol >> 8) & 0xFF;
309 unsigned char dr = (dstcol >> 16) & 0xFF;
310 unsigned char da = (dstcol >> 24) & 0xFF;
312 da = BLEND(0xFF, da, sa) & 0xFF;
313 dr = BLEND(sr, dr, sa) & 0xFF;
314 dg = BLEND(sg, dg, sa) & 0xFF;
315 db = BLEND(sb, db, sa) & 0xFF;
318 *dst++ = db | (dg << 8) | (dr << 16) | (da << 24);
324 void gPixmap::blit(const gPixmap &src, const eRect &_pos, const gRegion &clip, int flag)
326 // eDebug("blit: -> %d.%d %d:%d -> %d.%d %d:%d, flags=%d",
327 // _pos.x(), _pos.y(), _pos.width(), _pos.height(),
328 // clip.extends.x(), clip.extends.y(), clip.extends.width(), clip.extends.height(),
332 // eDebug("source size: %d %d", src.size().width(), src.size().height());
334 if (!(flag & blitScale)) /* pos' size is valid only when scaling */
335 pos = eRect(pos.topLeft(), src.size());
336 else if (pos.size() == src.size()) /* no scaling required */
339 int scale_x = FIX, scale_y = FIX;
341 if (flag & blitScale)
343 ASSERT(src.size().width());
344 ASSERT(src.size().height());
345 scale_x = pos.size().width() * FIX / src.size().width();
346 scale_y = pos.size().height() * FIX / src.size().height();
349 // eDebug("SCALE %x %x", scale_x, scale_y);
351 for (unsigned int i=0; i<clip.rects.size(); ++i)
353 // eDebug("clip rect: %d %d %d %d", clip.rects[i].x(), clip.rects[i].y(), clip.rects[i].width(), clip.rects[i].height());
354 eRect area = pos; /* pos is the virtual (pre-clipping) area on the dest, which can be larger/smaller than src if scaling is enabled */
356 area&=eRect(ePoint(0, 0), size());
361 eRect srcarea = area;
362 srcarea.moveBy(-pos.x(), -pos.y());
364 // eDebug("srcarea before scale: %d %d %d %d",
365 // srcarea.x(), srcarea.y(), srcarea.width(), srcarea.height());
367 if (flag & blitScale)
368 srcarea = eRect(srcarea.x() * FIX / scale_x, srcarea.y() * FIX / scale_y, srcarea.width() * FIX / scale_x, srcarea.height() * FIX / scale_y);
370 // eDebug("srcarea after scale: %d %d %d %d",
371 // srcarea.x(), srcarea.y(), srcarea.width(), srcarea.height());
373 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
374 if (!gAccel::getInstance()->blit(surface, src.surface, area, srcarea, flag))
377 if (flag & blitScale)
379 eWarning("unimplemented: scale on non-accel surfaces");
383 if ((surface->bpp == 8) && (src.surface->bpp==8))
385 __u8 *srcptr=(__u8*)src.surface->data;
386 __u8 *dstptr=(__u8*)surface->data;
388 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
389 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
390 for (int y=0; y<area.height(); y++)
392 if (flag & (blitAlphaTest|blitAlphaBlend))
394 // no real alphatest yet
395 int width=area.width();
396 unsigned char *src=(unsigned char*)srcptr;
397 unsigned char *dst=(unsigned char*)dstptr;
398 // use duff's device here!
409 memcpy(dstptr, srcptr, area.width()*surface->bypp);
410 srcptr+=src.surface->stride;
411 dstptr+=surface->stride;
413 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
415 __u32 *srcptr=(__u32*)src.surface->data;
416 __u32 *dstptr=(__u32*)surface->data;
418 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
419 dstptr+=area.left()+area.top()*surface->stride/4;
420 for (int y=0; y<area.height(); y++)
422 if (flag & blitAlphaTest)
424 int width=area.width();
425 unsigned long *src=(unsigned long*)srcptr;
426 unsigned long *dst=(unsigned long*)dstptr;
430 if (!((*src)&0xFF000000))
437 } else if (flag & blitAlphaBlend)
439 // uh oh. this is only until hardware accel is working.
441 int width=area.width();
443 unsigned char *src=(unsigned char*)srcptr;
444 unsigned char *dst=(unsigned char*)dstptr;
446 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
449 unsigned char sa = src[3];
450 unsigned char sr = src[2];
451 unsigned char sg = src[1];
452 unsigned char sb = src[0];
454 unsigned char da = dst[3];
455 unsigned char dr = dst[2];
456 unsigned char dg = dst[1];
457 unsigned char db = dst[0];
459 dst[3] = BLEND(0xFF, da, sa);
460 dst[2] = BLEND(sr, dr, sa);
461 dst[1] = BLEND(sg, dg, sa);
462 dst[0] = BLEND(sb, db, sa);
468 memcpy(dstptr, srcptr, area.width()*surface->bypp);
469 srcptr+=src.surface->stride/4;
470 dstptr+=surface->stride/4;
472 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
474 __u8 *srcptr=(__u8*)src.surface->data;
475 __u8 *dstptr=(__u8*)surface->data; // !!
478 for (int i=0; i<256; ++i)
480 if (src.surface->clut.data && (i<src.surface->clut.colors))
481 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);
487 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
488 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
489 for (int y=0; y<area.height(); y++)
491 int width=area.width();
492 unsigned char *psrc=(unsigned char*)srcptr;
493 __u32 *dst=(__u32*)dstptr;
494 if (flag & blitAlphaTest)
495 blit_8i_to_32_at(dst, psrc, pal, width);
496 else if (flag & blitAlphaBlend)
497 blit_8i_to_32_ab(dst, psrc, pal, width);
499 blit_8i_to_32(dst, psrc, pal, width);
500 srcptr+=src.surface->stride;
501 dstptr+=surface->stride;
503 } else if ((surface->bpp == 16) && (src.surface->bpp==8))
505 __u8 *srcptr=(__u8*)src.surface->data;
506 __u8 *dstptr=(__u8*)surface->data; // !!
509 for (int i=0; i<256; ++i)
512 if (src.surface->clut.data && (i<src.surface->clut.colors))
513 icol=(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);
516 #if BYTE_ORDER == LITTLE_ENDIAN
517 pal[i] = bswap_16(((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19);
519 pal[i] = ((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19;
524 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
525 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
527 if (flag & blitAlphaBlend)
528 eWarning("ignore unsupported 8bpp -> 16bpp alphablend!");
530 for (int y=0; y<area.height(); y++)
532 int width=area.width();
533 unsigned char *psrc=(unsigned char*)srcptr;
534 __u16 *dst=(__u16*)dstptr;
535 if (flag & blitAlphaTest)
536 blit_8i_to_16_at(dst, psrc, pal, width);
538 blit_8i_to_16(dst, psrc, pal, width);
539 srcptr+=src.surface->stride;
540 dstptr+=surface->stride;
542 } else if ((surface->bpp == 16) && (src.surface->bpp==32))
544 __u8 *srcptr=(__u8*)src.surface->data;
545 __u8 *dstptr=(__u8*)surface->data;
547 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride;
548 dstptr+=area.left()+area.top()*surface->stride;
550 if (flag & blitAlphaBlend)
551 eWarning("ignore unsupported 32bpp -> 16bpp alphablend!");
553 for (int y=0; y<area.height(); y++)
555 int width=area.width();
556 __u32 *srcp=(__u32*)srcptr;
557 __u16 *dstp=(__u16*)dstptr;
559 if (flag & blitAlphaTest)
563 if (!((*srcp)&0xFF000000))
569 __u32 icol = *srcp++;
570 #if BYTE_ORDER == LITTLE_ENDIAN
571 *dstp++ = bswap_16(((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19);
573 *dstp++ = ((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19;
581 __u32 icol = *srcp++;
582 #if BYTE_ORDER == LITTLE_ENDIAN
583 *dstp++ = bswap_16(((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19);
585 *dstp++ = ((icol & 0xFF) >> 3) << 11 | ((icol & 0xFF00) >> 10) << 5 | (icol & 0xFF0000) >> 19;
589 srcptr+=src.surface->stride;
590 dstptr+=surface->stride;
593 eWarning("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
599 void gPixmap::mergePalette(const gPixmap &target)
601 if ((!surface->clut.colors) || (!target.surface->clut.colors))
604 gColor *lookup=new gColor[surface->clut.colors];
606 for (int i=0; i<surface->clut.colors; i++)
607 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
609 delete [] surface->clut.data;
610 surface->clut.colors=target.surface->clut.colors;
611 surface->clut.data=new gRGB[surface->clut.colors];
612 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
614 __u8 *dstptr=(__u8*)surface->data;
616 for (int ay=0; ay<surface->y; ay++)
618 for (int ax=0; ax<surface->x; ax++)
619 dstptr[ax]=lookup[dstptr[ax]];
620 dstptr+=surface->stride;
626 static inline int sgn(int a)
636 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
641 int stride = surface->stride;
643 if (clip.rects.empty())
648 if (surface->bpp == 8)
649 srf8 = (__u8*)surface->data;
652 srf32 = (__u32*)surface->data;
653 if (surface->clut.data && color < surface->clut.colors)
654 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
660 if (surface->bpp == 16)
661 #if BYTE_ORDER == LITTLE_ENDIAN
662 col16=bswap_16(((col & 0xFF) >> 3) << 11 | ((col & 0xFF00) >> 10) << 5 | (col & 0xFF0000) >> 19);
664 col16=((col & 0xFF) >> 3) << 11 | ((col & 0xFF00) >> 10) << 5 | (col & 0xFF0000) >> 19;
667 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
668 int dx, dy, x, y, s1, s2, e, temp, swap, i;
688 /* i don't like this clipping loop, but the only */
689 /* other choice i see is to calculate the intersections */
690 /* before iterating through the pixels. */
692 /* one could optimize this because of the ordering */
698 /* if last pixel was invisble, first check bounding box */
701 /* check if we just got into the bbox again */
702 if (clip.extends.contains(x, y))
706 } else if (!clip.rects[a].contains(x, y))
711 if ((unsigned int)a == clip.rects.size())
718 } while (!clip.rects[a].contains(x, y));
723 srf8[y * stride + x] = color;
725 srf16[y * stride/2 + x] = col16;
727 srf32[y * stride/4 + x] = col;
746 gColor gPalette::findColor(const gRGB &rgb) const
750 return (rgb.r + rgb.g + rgb.b) / 3;
752 int difference=1<<30, best_choice=0;
753 for (int t=0; t<colors; t++)
756 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
760 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
764 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
768 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
784 if (must_delete_surface)
788 gPixmap::gPixmap(gSurface *surface)
789 :surface(surface), must_delete_surface(false)
793 gPixmap::gPixmap(eSize size, int bpp, int accel)
794 :must_delete_surface(true)
796 surface = new gSurface(size, bpp, accel);