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))
194 flag &= ~ blitAlphaBlend;
196 if ((surface->bpp == 8) && (src.surface->bpp==8))
198 __u8 *srcptr=(__u8*)src.surface->data;
199 __u8 *dstptr=(__u8*)surface->data;
201 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
202 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
203 for (int y=0; y<area.height(); y++)
205 if (flag & blitAlphaTest)
207 // no real alphatest yet
208 int width=area.width();
209 unsigned char *src=(unsigned char*)srcptr;
210 unsigned char *dst=(unsigned char*)dstptr;
211 // use duff's device here!
222 memcpy(dstptr, srcptr, area.width()*surface->bypp);
223 srcptr+=src.surface->stride;
224 dstptr+=surface->stride;
226 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
228 __u32 *srcptr=(__u32*)src.surface->data;
229 __u32 *dstptr=(__u32*)surface->data;
231 srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
232 dstptr+=area.left()+area.top()*surface->stride/4;
233 for (int y=0; y<area.height(); y++)
235 if (flag & blitAlphaTest)
237 int width=area.width();
238 unsigned long *src=(unsigned long*)srcptr;
239 unsigned long *dst=(unsigned long*)dstptr;
243 if (!((*src)&0xFF000000))
250 } else if (flag & blitAlphaBlend)
252 // uh oh. this is only until hardware accel is working.
254 int width=area.width();
256 unsigned char *src=(unsigned char*)srcptr;
257 unsigned char *dst=(unsigned char*)dstptr;
259 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
262 unsigned char sa = src[3];
263 unsigned char sr = src[2];
264 unsigned char sg = src[1];
265 unsigned char sb = src[0];
267 unsigned char da = dst[3];
268 unsigned char dr = dst[2];
269 unsigned char dg = dst[1];
270 unsigned char db = dst[0];
272 dst[3] = BLEND(0xFF, da, sa);
273 dst[2] = BLEND(sr, dr, sa);
274 dst[1] = BLEND(sg, dg, sa);
275 dst[0] = BLEND(sb, db, sa);
280 memcpy(dstptr, srcptr, area.width()*surface->bypp);
281 srcptr+=src.surface->stride/4;
282 dstptr+=surface->stride/4;
284 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
286 __u8 *srcptr=(__u8*)src.surface->data;
287 __u8 *dstptr=(__u8*)surface->data; // !!
290 for (int i=0; i<256; ++i)
292 if (src.surface->clut.data && (i<src.surface->clut.colors))
293 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);
299 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
300 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
301 for (int y=0; y<area.height(); y++)
303 if (flag & blitAlphaTest)
305 // no real alphatest yet
306 int width=area.width();
307 unsigned char *src=(unsigned char*)srcptr;
308 __u32 *dst=(__u32*)dstptr;
309 // use duff's device here!
321 int width=area.width();
322 unsigned char *src=(unsigned char*)srcptr;
323 __u32 *dst=(__u32*)dstptr;
327 srcptr+=src.surface->stride;
328 dstptr+=surface->stride;
331 eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
335 void gPixmap::mergePalette(const gPixmap &target)
337 if ((!surface->clut.colors) || (!target.surface->clut.colors))
340 gColor *lookup=new gColor[surface->clut.colors];
342 for (int i=0; i<surface->clut.colors; i++)
343 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
345 delete [] surface->clut.data;
346 surface->clut.colors=target.surface->clut.colors;
347 surface->clut.data=new gRGB[surface->clut.colors];
348 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
350 __u8 *dstptr=(__u8*)surface->data;
352 for (int ay=0; ay<surface->y; ay++)
354 for (int ax=0; ax<surface->x; ax++)
355 dstptr[ax]=lookup[dstptr[ax]];
356 dstptr+=surface->stride;
362 static inline int sgn(int a)
372 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
376 int stride = surface->stride;
378 if (clip.rects.empty())
382 if (surface->bpp == 8)
384 srf8 = (__u8*)surface->data;
385 } else if (surface->bpp == 32)
387 srf32 = (__u32*)surface->data;
389 if (surface->clut.data && color < surface->clut.colors)
390 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
396 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
397 int dx, dy, x, y, s1, s2, e, temp, swap, i;
417 /* i don't like this clipping loop, but the only */
418 /* other choice i see is to calculate the intersections */
419 /* before iterating through the pixels. */
421 /* one could optimize this because of the ordering */
427 /* if last pixel was invisble, first check bounding box */
430 /* check if we just got into the bbox again */
431 if (clip.extends.contains(x, y))
435 } else if (!clip.rects[a].contains(x, y))
440 if (a == clip.rects.size())
447 } while (!clip.rects[a].contains(x, y));
452 srf8[y * stride + x] = color;
454 srf32[y * stride/4 + x] = col;
470 gColor gPalette::findColor(const gRGB &rgb) const
474 return (rgb.r + rgb.g + rgb.b) / 3;
476 int difference=1<<30, best_choice=0;
477 for (int t=0; t<colors; t++)
480 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
484 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
488 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
492 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
510 gPixmap::gPixmap(gSurface *surface): surface(surface)
514 gPixmap::gPixmap(eSize size, int bpp, int accel)
516 surface = new gSurface(size, bpp, accel);