1 #include <lib/gdi/gpixmap.h>
2 #include <lib/gdi/region.h>
9 gLookup::gLookup(int size, const gPalette &pal, const gRGB &start, const gRGB &end)
12 build(size, pal, start, end);
15 void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRGB &end)
26 lookup=new gColor[size];
28 for (int i=0; i<size; i++)
33 int rdiff=-start.r+end.r;
34 int gdiff=-start.g+end.g;
35 int bdiff=-start.b+end.b;
36 int adiff=-start.a+end.a;
37 rdiff*=i; rdiff/=(size-1);
38 gdiff*=i; gdiff/=(size-1);
39 bdiff*=i; bdiff/=(size-1);
40 adiff*=i; adiff/=(size-1);
47 lookup[i]=pal.findColor(col);
55 gSurfaceSystem::gSurfaceSystem(eSize size, int _bpp)
69 case 24: // never use 24bit mode
79 data=malloc(x*y*bypp);
82 gSurfaceSystem::~gSurfaceSystem()
88 gPixmap *gPixmap::lock()
94 void gPixmap::unlock()
96 contentlock.unlock(1);
99 void gPixmap::fill(const gRegion ®ion, const gColor &color)
102 for (i=0; i<region.rects.size(); ++i)
104 const eRect &area = region.rects[i];
105 if ((area.height()<=0) || (area.width()<=0))
107 if (surface->bpp == 8)
109 for (int y=area.top(); y<area.bottom(); y++)
110 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
111 } else if (surface->bpp == 32)
112 for (int y=area.top(); y<area.bottom(); y++)
114 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
118 if (surface->clut.data && color < surface->clut.colors)
119 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
127 eWarning("couldn't fill %d bpp", surface->bpp);
131 void gPixmap::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag)
133 for (unsigned int i=0; i<clip.rects.size(); ++i)
135 eRect area=eRect(pos, src.size());
137 area&=eRect(ePoint(0, 0), size());
138 if ((area.width()<0) || (area.height()<0))
142 srcarea.moveBy(-pos.x(), -pos.y());
144 if ((surface->bpp == 8) && (src.surface->bpp==8))
146 __u8 *srcptr=(__u8*)src.surface->data;
147 __u8 *dstptr=(__u8*)surface->data;
149 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
150 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
151 for (int y=0; y<area.height(); y++)
153 if (flag & blitAlphaTest)
155 // no real alphatest yet
156 int width=area.width();
157 unsigned char *src=(unsigned char*)srcptr;
158 unsigned char *dst=(unsigned char*)dstptr;
159 // use duff's device here!
170 memcpy(dstptr, srcptr, area.width()*surface->bypp);
171 srcptr+=src.surface->stride;
172 dstptr+=surface->stride;
174 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
176 __u8 *srcptr=(__u8*)src.surface->data;
177 __u8 *dstptr=(__u8*)surface->data; // !!
180 for (int i=0; i<256; ++i)
182 if (src.surface->clut.data && (i<src.surface->clut.colors))
183 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);
189 srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
190 dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
191 for (int y=0; y<area.height(); y++)
193 if (flag & blitAlphaTest)
195 // no real alphatest yet
196 int width=area.width();
197 unsigned char *src=(unsigned char*)srcptr;
198 __u32 *dst=(__u32*)dstptr;
199 // use duff's device here!
211 int width=area.width();
212 unsigned char *src=(unsigned char*)srcptr;
213 __u32 *dst=(__u32*)dstptr;
217 srcptr+=src.surface->stride;
218 dstptr+=surface->stride;
221 eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
225 void gPixmap::mergePalette(const gPixmap &target)
227 eDebug("merge palette! %p %p", surface, target.surface);
228 if ((!surface->clut.colors) || (!target.surface->clut.colors))
231 gColor *lookup=new gColor[surface->clut.colors];
233 for (int i=0; i<surface->clut.colors; i++)
234 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
236 delete [] surface->clut.data;
237 surface->clut.colors=target.surface->clut.colors;
238 surface->clut.data=new gRGB[surface->clut.colors];
239 memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
241 __u8 *dstptr=(__u8*)surface->data;
243 for (int ay=0; ay<surface->y; ay++)
245 for (int ax=0; ax<surface->x; ax++)
246 dstptr[ax]=lookup[dstptr[ax]];
247 dstptr+=surface->stride;
254 static inline int sgn(int a)
264 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
268 int stride = surface->stride;
270 if (clip.rects.empty())
274 if (surface->bpp == 8)
276 srf8 = (__u8*)surface->data;
277 } else if (surface->bpp == 32)
279 srf32 = (__u32*)surface->data;
281 if (surface->clut.data && color < surface->clut.colors)
282 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
288 int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
289 int dx, dy, x, y, s1, s2, e, temp, swap, i;
309 /* i don't like this clipping loop, but the only */
310 /* other choice i see is to calculate the intersections */
311 /* before iterating through the pixels. */
313 /* one could optimize this because of the ordering */
319 /* if last pixel was invisble, first check bounding box */
322 /* check if we just got into the bbox again */
323 if (clip.extends.contains(x, y))
327 } else if (!clip.rects[a].contains(x, y))
332 if (a == clip.rects.size())
339 } while (!clip.rects[a].contains(x, y));
344 srf8[y * stride + x] = color;
346 srf32[y * stride/4 + x] = col;
362 gColor gPalette::findColor(const gRGB &rgb) const
364 int difference=1<<30, best_choice=0;
365 for (int t=0; t<colors; t++)
368 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
372 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
376 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
380 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
398 gPixmap::gPixmap(gSurface *surface): surface(surface)
402 gPixmap::gPixmap(eSize size, int bpp)
404 surface = new gSurfaceSystem(size, bpp);