54 lines · 1.2 KB · Go
RawDownload
package draw9 import (	"fmt"	"image") // Load accepts image(6) data and loads it into the Image dst using the draw(3) 'y' message.func (dst *Image) Load(r image.Rectangle, data []byte) (int, error) {	dst.Display.mu.Lock()	defer dst.Display.mu.Unlock()	return dst.load(r, data)} func (dst *Image) load(r image.Rectangle, data []byte) (int, error) {	i := dst	chunk := i.Display.bufsize - 64	if !r.In(i.R) {		return 0, fmt.Errorf("loadimage: bad rectangle")	}	bpl := BytesPerLine(r, i.Depth)	n := bpl * r.Dy()	if n > len(data) {		return 0, fmt.Errorf("loadimage: insufficient data")	}	ndata := 0	for r.Max.Y > r.Min.Y {		dy := r.Dy()		if dy*bpl > chunk {			dy = chunk / bpl		}		if dy <= 0 {			return 0, fmt.Errorf("loadimage: image too wide for buffer")		}		n := dy * bpl		a := i.Display.bufimage(21 + n)		a[0] = 'y'		bplong(a[1:], uint32(i.ID))		bplong(a[5:], uint32(r.Min.X))		bplong(a[9:], uint32(r.Min.Y))		bplong(a[13:], uint32(r.Max.X))		bplong(a[17:], uint32(r.Min.Y+dy))		copy(a[21:], data)		ndata += n		data = data[n:]		r.Min.Y += dy	}	/*		if err := i.Display.flush(false); err != nil {			return ndata, err		}	*/	return ndata, nil}