#include "paint.h" static uchar *tmp; static int tmpsz; static int alloc(int sz) { if(sz > tmpsz) { if(tmp != nil) free(tmp); tmpsz = 0; if((tmp = malloc(sz)) == nil) return -1; tmpsz = sz; } return 0; } int syncimg(Image *img, Memimage *mem, Rectangle r) { int n; if(alloc(Dy(r) * Dx(r) * chantodepth(mem->chan)) < 0) return -1; if((n = unloadmemimage(mem, r, tmp, tmpsz)) < 0) { werrstr("unload"); return -1; } if(loadimage(img, r, tmp, n) < 0) { werrstr("load"); return -1; } return 0; } int magsyncimg(Image *img, Memimage *mem, Rectangle imgr, Point pos, int mag) { int nch, xfract, y, x, ch; uchar *dst, *src; nch = chantodepth(mem->chan) / 8; if(alloc(Dy(imgr) * Dx(imgr) * nch) < 0) return -1; xfract = (pos.x + imgr.min.x) % mag; for(y = 0; y < Dy(imgr); y++) { dst = tmp + y*Dx(imgr)*nch; src = byteaddr(mem, divpt(addpt(addpt(imgr.min, pos), Pt(0, y)), mag)); for(x = 0; x < Dx(imgr); x++) { for(ch = 0; ch < nch; ch++) dst[x*nch + ch] = src[((x + xfract)/mag)*nch + ch]; } } if(loadimage(img, imgr, tmp, Dy(imgr)*Dx(imgr)*nch) < 0) { werrstr("load"); return -1; } return 0; } int synccanvasmem(Canvas *c, Rectangle memr) { Rectangle imgr; /* the area of the Image which will be overwritten */ imgr = rectsubpt(scalerect(memr, c->mag), c->pos); if(rectclip(&imgr, c->img->r) == 0) return 0; return synccanvasimg(c, imgr); } int synccanvasimg(Canvas *c, Rectangle imgr) { return magsyncimg(c->img, c->s->mem, imgr, c->pos, c->mag); }