3 #include <lib/gdi/gpixmap.h>
4 #include <lib/gdi/region.h>
5 #include <lib/gdi/accel.h>
12 gLookup::gLookup(int size, const gPalette &pal, const gRGB &start, const gRGB &end)
15 build(size, pal, start, end);
18 void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRGB &end)
29 lookup=new gColor[size];
31 for (int i=0; i<size; i++)
36 int rdiff=-start.r+end.r;
37 int gdiff=-start.g+end.g;
38 int bdiff=-start.b+end.b;
39 int adiff=-start.a+end.a;
40 rdiff*=i; rdiff/=(size-1);
41 gdiff*=i; gdiff/=(size-1);
42 bdiff*=i; bdiff/=(size-1);
43 adiff*=i; adiff/=(size-1);
50 lookup[i]=pal.findColor(col);
68 gSurface::gSurface(eSize size, int _bpp, int accel)
83 case 24: // never use 24bit mode
105 if (gAccel::getInstance())
106 eDebug("accel memory: %d", gAccel::getInstance()->accelAlloc(data, data_phys, y * stride + pal_size));
108 eDebug("no accel available");
115 data = new unsigned char [y * stride];
120 gSurface::~gSurface()
125 gAccel::getInstance()->accelFree(data_phys);
127 delete [] (unsigned char*)data;
133 gPixmap *gPixmap::lock()
139 void gPixmap::unlock()
141 contentlock.unlock(1);
144 void gPixmap::fill(const gRegion ®ion, const gColor &color)
147 for (i=0; i<region.rects.size(); ++i)
149 const eRect &area = region.rects[i];
153 if (surface->bpp == 8)
155 for (int y=area.top(); y<area.bottom(); y++)
156 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
157 } else if (surface->bpp == 32)
161 if (surface->clut.data && color < surface->clut.colors)
162 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
168 if (surface->data_phys && gAccel::getInstance())
169 if (!gAccel::getInstance()->fill(surface, area, col))
172 for (int y=area.top(); y<area.bottom(); y++)
174 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
180 eWarning("couldn't fill %d bpp", surface->bpp);
184 void gPixmap::fill(const gRegion ®ion, const gRGB &color)
187 for (i=0; i<region.rects.size(); ++i)
189 const eRect &area = region.rects[i];
193 if (surface->bpp == 32)
200 if (surface->data_phys && gAccel::getInstance())
201 if (!gAccel::getInstance()->fill(surface, area, col))
204 for (int y=area.top(); y<area.bottom(); y++)
206 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
212 eWarning("couldn't rgbfill %d bpp", surface->bpp);
216 static void blit_8i_to_32(__u32 *dst, __u8 *src, __u32 *pal, int width)
222 static void blit_8i_to_32_at(__u32 *dst, __u8 *src, __u32 *pal, int width)
226 if (!(pal[*src]&0x80000000))
235 /* WARNING, this function is not endian safe! */
236 static void blit_8i_to_32_ab(__u32 *dst, __u8 *src, __u32 *pal, int width)
240 #define BLEND(x, y, a) (y + (((x-y) * a)>>8))
241 __u32 srccol = pal[*src++];
243 unsigned char sb = srccol & 0xFF;
244 unsigned char sg = (srccol >> 8) & 0xFF;
245 unsigned char sr = (srccol >> 16) & 0xFF;
246 unsigned char sa = (srccol >> 24) & 0xFF;
248 unsigned char db = dstcol & 0xFF;
249 unsigned char dg = (dstcol >> 8) & 0xFF;
250 unsigned char dr = (dstcol >> 16) & 0xFF;
251 unsigned char da = (dstcol >> 24) & 0xFF;
253 da = BLEND(0xFF, da, sa) & 0xFF;
254 dr = BLEND(sr, dr, sa) & 0xFF;
255 dg = BLEND(sg, dg, sa) & 0xFF;
256 db = BLEND(sb, db, sa) & 0xFF;
259 *dst++ = db | (dg << 8) | (dr << 16) | (da << 24);
265 void gPixmap::blit(const gPixmap &src, const eRect &_pos, const gRegion &clip, int flag)
267 // eDebug("blit: -> %d.%d %d:%d -> %d.%d %d:%d, flags=%d",
268 // _pos.x(), _pos.y(), _pos.width(), _pos.height(),
269 // clip.extends.x(), clip.extends.y(), clip.extends.width(), clip.extends.height(),
273 // eDebug("source size: %d %d", src.size().width(), src.size().height());
275 if (!(flag & blitScale)) /* pos' size is valid only when scaling */
276 pos = eRect(pos.topLeft(), src.size());
277 else if (pos.size() == src.size()) /* no scaling required */
280 int scale_x = FIX, scale_y = FIX;
282 if (flag & blitScale)
284 ASSERT(src.size().width());
285 ASSERT(src.size().height());
286 scale_x = pos.size().width() * FIX / src.size().width();
287 scale_y = pos.size().height() * FIX / src.size().height();
290 // eDebug("SCALE %x %x", scale_x, scale_y);
292 for (unsigned int i=0; i<clip.rects.size(); ++i)
294 // eDebug("clip rect: %d %d %d %d", clip.rects[i].x(), clip.rects[i].y(), clip.rects[i].width(), clip.rects[i].height());
295 eRect area = pos; /* pos is the virtual (pre-clipping) area on the dest, which can be larger/smaller than src if scaling is enabled */
297 area&=eRect(ePoint(0, 0), size());
302 eRect srcarea = area;
303 srcarea.moveBy(-pos.x(), -pos.y());
305 // eDebug("srcarea before scale: %d %d %d %d",
306 // srcarea.x(), srcarea.y(), srcarea.width(), srcarea.height());
308 if (flag & blitScale)
309 srcarea = eRect(srcarea.x() * FIX / scale_x, srcarea.y() * FIX / scale_y, srcarea.width() * FIX / scale_x, srcarea.height() * FIX / scale_y);
311 // eDebug("srcarea after scale: %d %d %d %d",
312 // srcarea.x(), srcarea.y(), srcarea.width(), srcarea.height());
314 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
315 if (!gAccel::getInstance()->blit(surface, src.surface, area, srcarea, flag))
318 if (flag & blitScale)
320 eWarning("unimplemented: scale on non-accel surfaces");
324 if ((surface->bpp == 8) && (src.surface->bpp==8))
326 __u8 *srcptr=(__u8*)src.surface->data;
327 __u8 *dstptr=(__u8*)surface->data;
329 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
330 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
331 for (int y=0; y<area.height(); y++)
333 if (flag & (blitAlphaTest|blitAlphaBlend))
335 // no real alphatest yet
336 int width=area.width();
337 unsigned char *src=(unsigned char*)srcptr;
338 unsigned char *dst=(unsigned char*)dstptr;
339 // use duff's device here!
350 memcpy(dstptr, srcptr, area.width()*surface->bypp);
351 srcptr+=src.surface->stride;
352 dstptr+=surface->stride;
354 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
356 __u32 *srcptr=(__u32*)src.surface->data;
357 __u32 *dstptr=(__u32*)surface->data;
359 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
360 dstptr+=area.left()+area.top()*surface->stride/4;
361 for (int y=0; y<area.height(); y++)
363 if (flag & blitAlphaTest)
365 int width=area.width();
366 unsigned long *src=(unsigned long*)srcptr;
367 unsigned long *dst=(unsigned long*)dstptr;
371 if (!((*src)&0xFF000000))
378 } else if (flag & blitAlphaBlend)
380 // uh oh. this is only until hardware accel is working.
382 int width=area.width();
384 unsigned char *src=(unsigned char*)srcptr;
385 unsigned char *dst=(unsigned char*)dstptr;
387 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
390 unsigned char sa = src[3];
391 unsigned char sr = src[2];
392 unsigned char sg = src[1];
393 unsigned char sb = src[0];
395 unsigned char da = dst[3];
396 unsigned char dr = dst[2];
397 unsigned char dg = dst[1];
398 unsigned char db = dst[0];
400 dst[3] = BLEND(0xFF, da, sa);
401 dst[2] = BLEND(sr, dr, sa);
402 dst[1] = BLEND(sg, dg, sa);
403 dst[0] = BLEND(sb, db, sa);
409 memcpy(dstptr, srcptr, area.width()*surface->bypp);
410 srcptr+=src.surface->stride/4;
411 dstptr+=surface->stride/4;
413 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
415 __u8 *srcptr=(__u8*)src.surface->data;
416 __u8 *dstptr=(__u8*)surface->data; // !!
419 for (int i=0; i<256; ++i)
421 if (src.surface->clut.data && (i<src.surface->clut.colors))
422 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);
428 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
429 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
430 for (int y=0; y<area.height(); y++)
432 int width=area.width();
433 unsigned char *psrc=(unsigned char*)srcptr;
434 __u32 *dst=(__u32*)dstptr;
435 if (flag & blitAlphaTest)
436 blit_8i_to_32_at(dst, psrc, pal, width);
437 else if (flag & blitAlphaBlend)
438 blit_8i_to_32_ab(dst, psrc, pal, width);
440 blit_8i_to_32(dst, psrc, pal, width);
441 srcptr+=src.surface->stride;
442 dstptr+=surface->stride;
445 eWarning("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
451 void gPixmap::mergePalette(const gPixmap &target)
453 if ((!surface->clut.colors) || (!target.surface->clut.colors))
456 gColor *lookup=new gColor[surface->clut.colors];
458 for (int i=0; i<surface->clut.colors; i++)
459 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
461 delete [] surface->clut.data;
462 surface->clut.colors=target.surface->clut.colors;
463 surface->clut.data=new gRGB[surface->clut.colors];
464 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
466 __u8 *dstptr=(__u8*)surface->data;
468 for (int ay=0; ay<surface->y; ay++)
470 for (int ax=0; ax<surface->x; ax++)
471 dstptr[ax]=lookup[dstptr[ax]];
472 dstptr+=surface->stride;
478 static inline int sgn(int a)
488 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
492 int stride = surface->stride;
494 if (clip.rects.empty())
498 if (surface->bpp == 8)
500 srf8 = (__u8*)surface->data;
501 } else if (surface->bpp == 32)
503 srf32 = (__u32*)surface->data;
505 if (surface->clut.data && color < surface->clut.colors)
506 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
512 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
513 int dx, dy, x, y, s1, s2, e, temp, swap, i;
533 /* i don't like this clipping loop, but the only */
534 /* other choice i see is to calculate the intersections */
535 /* before iterating through the pixels. */
537 /* one could optimize this because of the ordering */
543 /* if last pixel was invisble, first check bounding box */
546 /* check if we just got into the bbox again */
547 if (clip.extends.contains(x, y))
551 } else if (!clip.rects[a].contains(x, y))
556 if ((unsigned int)a == clip.rects.size())
563 } while (!clip.rects[a].contains(x, y));
568 srf8[y * stride + x] = color;
570 srf32[y * stride/4 + x] = col;
586 gColor gPalette::findColor(const gRGB &rgb) const
590 return (rgb.r + rgb.g + rgb.b) / 3;
592 int difference=1<<30, best_choice=0;
593 for (int t=0; t<colors; t++)
596 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
600 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
604 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
608 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
624 if (must_delete_surface)
628 gPixmap::gPixmap(gSurface *surface)
629 :surface(surface), must_delete_surface(false)
633 gPixmap::gPixmap(eSize size, int bpp, int accel)
634 :must_delete_surface(true)
636 surface = new gSurface(size, bpp, accel);