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());
185 if ((area.width()<0) || (area.height()<0))
189 srcarea.moveBy(-pos.x(), -pos.y());
191 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
192 if (!gAccel::getInstance()->blit(surface, src.surface, area.topLeft(), srcarea, flag))
195 if ((surface->bpp == 8) && (src.surface->bpp==8))
197 __u8 *srcptr=(__u8*)src.surface->data;
198 __u8 *dstptr=(__u8*)surface->data;
200 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
201 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
202 for (int y=0; y<area.height(); y++)
204 if (flag & blitAlphaTest)
206 // no real alphatest yet
207 int width=area.width();
208 unsigned char *src=(unsigned char*)srcptr;
209 unsigned char *dst=(unsigned char*)dstptr;
210 // use duff's device here!
221 memcpy(dstptr, srcptr, area.width()*surface->bypp);
222 srcptr+=src.surface->stride;
223 dstptr+=surface->stride;
225 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
227 __u32 *srcptr=(__u32*)src.surface->data;
228 __u32 *dstptr=(__u32*)surface->data;
230 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
231 dstptr+=area.left()+area.top()*surface->stride/4;
232 for (int y=0; y<area.height(); y++)
234 if (flag & blitAlphaTest)
236 int width=area.width();
237 unsigned long *src=(unsigned long*)srcptr;
238 unsigned long *dst=(unsigned long*)dstptr;
242 if (!((*src)&0xFF000000))
249 } else if (flag & blitAlphaBlend)
251 // uh oh. this is only until hardware accel is working.
253 int width=area.width();
255 unsigned char *src=(unsigned char*)srcptr;
256 unsigned char *dst=(unsigned char*)dstptr;
258 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
261 unsigned char sa = src[3];
262 unsigned char sr = src[2];
263 unsigned char sg = src[1];
264 unsigned char sb = src[0];
266 unsigned char da = dst[3];
267 unsigned char dr = dst[2];
268 unsigned char dg = dst[1];
269 unsigned char db = dst[0];
271 dst[3] = BLEND(0xFF, da, sa);
272 dst[2] = BLEND(sr, dr, sa);
273 dst[1] = BLEND(sg, dg, sa);
274 dst[0] = BLEND(sb, db, sa);
279 memcpy(dstptr, srcptr, area.width()*surface->bypp);
280 srcptr+=src.surface->stride/4;
281 dstptr+=surface->stride/4;
283 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
285 __u8 *srcptr=(__u8*)src.surface->data;
286 __u8 *dstptr=(__u8*)surface->data; // !!
289 for (int i=0; i<256; ++i)
291 if (src.surface->clut.data && (i<src.surface->clut.colors))
292 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);
298 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
299 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
300 for (int y=0; y<area.height(); y++)
302 if (flag & blitAlphaTest)
304 // no real alphatest yet
305 int width=area.width();
306 unsigned char *src=(unsigned char*)srcptr;
307 __u32 *dst=(__u32*)dstptr;
308 // use duff's device here!
320 int width=area.width();
321 unsigned char *src=(unsigned char*)srcptr;
322 __u32 *dst=(__u32*)dstptr;
326 srcptr+=src.surface->stride;
327 dstptr+=surface->stride;
330 eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
334 void gPixmap::mergePalette(const gPixmap &target)
336 if ((!surface->clut.colors) || (!target.surface->clut.colors))
339 gColor *lookup=new gColor[surface->clut.colors];
341 for (int i=0; i<surface->clut.colors; i++)
342 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
344 delete [] surface->clut.data;
345 surface->clut.colors=target.surface->clut.colors;
346 surface->clut.data=new gRGB[surface->clut.colors];
347 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
349 __u8 *dstptr=(__u8*)surface->data;
351 for (int ay=0; ay<surface->y; ay++)
353 for (int ax=0; ax<surface->x; ax++)
354 dstptr[ax]=lookup[dstptr[ax]];
355 dstptr+=surface->stride;
361 static inline int sgn(int a)
371 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
375 int stride = surface->stride;
377 if (clip.rects.empty())
381 if (surface->bpp == 8)
383 srf8 = (__u8*)surface->data;
384 } else if (surface->bpp == 32)
386 srf32 = (__u32*)surface->data;
388 if (surface->clut.data && color < surface->clut.colors)
389 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
395 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
396 int dx, dy, x, y, s1, s2, e, temp, swap, i;
416 /* i don't like this clipping loop, but the only */
417 /* other choice i see is to calculate the intersections */
418 /* before iterating through the pixels. */
420 /* one could optimize this because of the ordering */
426 /* if last pixel was invisble, first check bounding box */
429 /* check if we just got into the bbox again */
430 if (clip.extends.contains(x, y))
434 } else if (!clip.rects[a].contains(x, y))
439 if (a == clip.rects.size())
446 } while (!clip.rects[a].contains(x, y));
451 srf8[y * stride + x] = color;
453 srf32[y * stride/4 + x] = col;
469 gColor gPalette::findColor(const gRGB &rgb) const
471 int difference=1<<30, best_choice=0;
472 for (int t=0; t<colors; t++)
475 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
479 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
483 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
487 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
505 gPixmap::gPixmap(gSurface *surface): surface(surface)
509 gPixmap::gPixmap(eSize size, int bpp, int accel)
511 surface = new gSurface(size, bpp, accel);