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
101 if (gAccel::getInstance())
102 eDebug("accel memory: %d", gAccel::getInstance()->accelAlloc(data, data_phys, y * stride));
104 eDebug("no accel available");
111 data = new unsigned char [y * stride];
116 gSurface::~gSurface()
121 gAccel::getInstance()->accelFree(data_phys);
123 delete [] (unsigned char*)data;
129 gPixmap *gPixmap::lock()
135 void gPixmap::unlock()
137 contentlock.unlock(1);
140 void gPixmap::fill(const gRegion ®ion, const gColor &color)
143 for (i=0; i<region.rects.size(); ++i)
145 const eRect &area = region.rects[i];
146 if ((area.height()<=0) || (area.width()<=0))
149 if (surface->bpp == 8)
151 for (int y=area.top(); y<area.bottom(); y++)
152 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
153 } else if (surface->bpp == 32)
157 if (surface->clut.data && color < surface->clut.colors)
158 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
164 if (surface->data_phys && gAccel::getInstance())
165 if (!gAccel::getInstance()->fill(surface, area, col))
168 for (int y=area.top(); y<area.bottom(); y++)
170 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
176 eWarning("couldn't fill %d bpp", surface->bpp);
180 void gPixmap::fill(const gRegion ®ion, const gRGB &color)
183 for (i=0; i<region.rects.size(); ++i)
185 const eRect &area = region.rects[i];
186 if ((area.height()<=0) || (area.width()<=0))
189 if (surface->bpp == 32)
196 if (surface->data_phys && gAccel::getInstance())
197 if (!gAccel::getInstance()->fill(surface, area, col))
200 for (int y=area.top(); y<area.bottom(); y++)
202 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
208 eWarning("couldn't rgbfill %d bpp", surface->bpp);
212 static void blit_8i_to_32(__u32 *dst, __u8 *src, __u32 *pal, int width)
218 static void blit_8i_to_32_at(__u32 *dst, __u8 *src, __u32 *pal, int width)
222 if (!(pal[*src]&0x80000000))
231 /* WARNING, this function is not endian safe! */
232 static void blit_8i_to_32_ab(__u32 *dst, __u8 *src, __u32 *pal, int width)
236 #define BLEND(x, y, a) (y + (((x-y) * a)>>8))
237 __u32 srccol = pal[*src++];
239 unsigned char sb = srccol & 0xFF;
240 unsigned char sg = (srccol >> 8) & 0xFF;
241 unsigned char sr = (srccol >> 16) & 0xFF;
242 unsigned char sa = (srccol >> 24) & 0xFF;
244 unsigned char db = dstcol & 0xFF;
245 unsigned char dg = (dstcol >> 8) & 0xFF;
246 unsigned char dr = (dstcol >> 16) & 0xFF;
247 unsigned char da = (dstcol >> 24) & 0xFF;
249 da = BLEND(0xFF, da, sa) & 0xFF;
250 dr = BLEND(sr, dr, sa) & 0xFF;
251 dg = BLEND(sg, dg, sa) & 0xFF;
252 db = BLEND(sb, db, sa) & 0xFF;
255 *dst++ = db | (dg << 8) | (dr << 16) | (da << 24);
261 void gPixmap::blit(const gPixmap &src, const eRect &_pos, const gRegion &clip, int flag)
263 // eDebug("blit: -> %d.%d %d:%d -> %d.%d %d:%d, flags=%d",
264 // _pos.x(), _pos.y(), _pos.width(), _pos.height(),
265 // clip.extends.x(), clip.extends.y(), clip.extends.width(), clip.extends.height(),
269 // eDebug("source size: %d %d", src.size().width(), src.size().height());
271 if (!(flag & blitScale)) /* pos' size is valid only when scaling */
272 pos = eRect(pos.topLeft(), src.size());
273 else if (pos.size() == src.size()) /* no scaling required */
276 int scale_x = FIX, scale_y = FIX;
278 if (flag & blitScale)
280 ASSERT(src.size().width());
281 ASSERT(src.size().height());
282 scale_x = pos.size().width() * FIX / src.size().width();
283 scale_y = pos.size().height() * FIX / src.size().height();
286 // eDebug("SCALE %x %x", scale_x, scale_y);
288 for (unsigned int i=0; i<clip.rects.size(); ++i)
290 // eDebug("clip rect: %d %d %d %d", clip.rects[i].x(), clip.rects[i].y(), clip.rects[i].width(), clip.rects[i].height());
291 eRect area = pos; /* pos is the virtual (pre-clipping) area on the dest, which can be larger/smaller than src if scaling is enabled */
293 area&=eRect(ePoint(0, 0), size());
295 if ((area.width()<0) || (area.height()<0))
298 eRect srcarea = area;
299 srcarea.moveBy(-pos.x(), -pos.y());
301 // eDebug("srcarea before scale: %d %d %d %d",
302 // srcarea.x(), srcarea.y(), srcarea.width(), srcarea.height());
304 if (flag & blitScale)
305 srcarea = eRect(srcarea.x() * FIX / scale_x, srcarea.y() * FIX / scale_y, srcarea.width() * FIX / scale_x, srcarea.height() * FIX / scale_y);
307 // eDebug("srcarea after scale: %d %d %d %d",
308 // srcarea.x(), srcarea.y(), srcarea.width(), srcarea.height());
310 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
311 if (!gAccel::getInstance()->blit(surface, src.surface, area, srcarea, flag))
314 if (flag & blitScale)
316 eWarning("unimplemented: scale on non-accel surfaces");
320 if ((surface->bpp == 8) && (src.surface->bpp==8))
322 __u8 *srcptr=(__u8*)src.surface->data;
323 __u8 *dstptr=(__u8*)surface->data;
325 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
326 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
327 for (int y=0; y<area.height(); y++)
329 if (flag & (blitAlphaTest|blitAlphaBlend))
331 // no real alphatest yet
332 int width=area.width();
333 unsigned char *src=(unsigned char*)srcptr;
334 unsigned char *dst=(unsigned char*)dstptr;
335 // use duff's device here!
346 memcpy(dstptr, srcptr, area.width()*surface->bypp);
347 srcptr+=src.surface->stride;
348 dstptr+=surface->stride;
350 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
352 __u32 *srcptr=(__u32*)src.surface->data;
353 __u32 *dstptr=(__u32*)surface->data;
355 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
356 dstptr+=area.left()+area.top()*surface->stride/4;
357 for (int y=0; y<area.height(); y++)
359 if (flag & blitAlphaTest)
361 int width=area.width();
362 unsigned long *src=(unsigned long*)srcptr;
363 unsigned long *dst=(unsigned long*)dstptr;
367 if (!((*src)&0xFF000000))
374 } else if (flag & blitAlphaBlend)
376 // uh oh. this is only until hardware accel is working.
378 int width=area.width();
380 unsigned char *src=(unsigned char*)srcptr;
381 unsigned char *dst=(unsigned char*)dstptr;
383 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
386 unsigned char sa = src[3];
387 unsigned char sr = src[2];
388 unsigned char sg = src[1];
389 unsigned char sb = src[0];
391 unsigned char da = dst[3];
392 unsigned char dr = dst[2];
393 unsigned char dg = dst[1];
394 unsigned char db = dst[0];
396 dst[3] = BLEND(0xFF, da, sa);
397 dst[2] = BLEND(sr, dr, sa);
398 dst[1] = BLEND(sg, dg, sa);
399 dst[0] = BLEND(sb, db, sa);
405 memcpy(dstptr, srcptr, area.width()*surface->bypp);
406 srcptr+=src.surface->stride/4;
407 dstptr+=surface->stride/4;
409 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
411 __u8 *srcptr=(__u8*)src.surface->data;
412 __u8 *dstptr=(__u8*)surface->data; // !!
415 for (int i=0; i<256; ++i)
417 if (src.surface->clut.data && (i<src.surface->clut.colors))
418 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);
424 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
425 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
426 for (int y=0; y<area.height(); y++)
428 int width=area.width();
429 unsigned char *psrc=(unsigned char*)srcptr;
430 __u32 *dst=(__u32*)dstptr;
431 if (flag & blitAlphaTest)
432 blit_8i_to_32_at(dst, psrc, pal, width);
433 else if (flag & blitAlphaBlend)
434 blit_8i_to_32_ab(dst, psrc, pal, width);
436 blit_8i_to_32(dst, psrc, pal, width);
437 srcptr+=src.surface->stride;
438 dstptr+=surface->stride;
441 eWarning("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
447 void gPixmap::mergePalette(const gPixmap &target)
449 if ((!surface->clut.colors) || (!target.surface->clut.colors))
452 gColor *lookup=new gColor[surface->clut.colors];
454 for (int i=0; i<surface->clut.colors; i++)
455 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
457 delete [] surface->clut.data;
458 surface->clut.colors=target.surface->clut.colors;
459 surface->clut.data=new gRGB[surface->clut.colors];
460 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
462 __u8 *dstptr=(__u8*)surface->data;
464 for (int ay=0; ay<surface->y; ay++)
466 for (int ax=0; ax<surface->x; ax++)
467 dstptr[ax]=lookup[dstptr[ax]];
468 dstptr+=surface->stride;
474 static inline int sgn(int a)
484 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
488 int stride = surface->stride;
490 if (clip.rects.empty())
494 if (surface->bpp == 8)
496 srf8 = (__u8*)surface->data;
497 } else if (surface->bpp == 32)
499 srf32 = (__u32*)surface->data;
501 if (surface->clut.data && color < surface->clut.colors)
502 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
508 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
509 int dx, dy, x, y, s1, s2, e, temp, swap, i;
529 /* i don't like this clipping loop, but the only */
530 /* other choice i see is to calculate the intersections */
531 /* before iterating through the pixels. */
533 /* one could optimize this because of the ordering */
539 /* if last pixel was invisble, first check bounding box */
542 /* check if we just got into the bbox again */
543 if (clip.extends.contains(x, y))
547 } else if (!clip.rects[a].contains(x, y))
552 if ((unsigned int)a == clip.rects.size())
559 } while (!clip.rects[a].contains(x, y));
564 srf8[y * stride + x] = color;
566 srf32[y * stride/4 + x] = col;
582 gColor gPalette::findColor(const gRGB &rgb) const
586 return (rgb.r + rgb.g + rgb.b) / 3;
588 int difference=1<<30, best_choice=0;
589 for (int t=0; t<colors; t++)
592 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
596 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
600 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
604 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
620 if (must_delete_surface)
624 gPixmap::gPixmap(gSurface *surface)
625 :surface(surface), must_delete_surface(false)
629 gPixmap::gPixmap(eSize size, int bpp, int accel)
630 :must_delete_surface(true)
632 surface = new gSurface(size, bpp, accel);