9fans.net/go@v0.0.5/draw/cloadimage.go (about)

     1  package draw
     2  
     3  import "fmt"
     4  
     5  // Cload replaces the specified rectangle in image i with the compressed data,
     6  // returning the number of bytes copied from data.
     7  // It is an error if data does not contain pixels for the entire rectangle.
     8  //
     9  // See the package documentation for details about the compressed data format.
    10  // Each call to Cload must pass data starting at the beginning of a compressed
    11  // data block, specifically the y coordinate and data length for the block.
    12  func (dst *Image) Cload(r Rectangle, data []byte) (int, error) {
    13  	dst.Display.mu.Lock()
    14  	defer dst.Display.mu.Unlock()
    15  	i := dst
    16  	if !r.In(i.R) {
    17  		return 0, fmt.Errorf("cloadimage: bad rectangle")
    18  	}
    19  
    20  	miny := r.Min.Y
    21  	m := 0
    22  	ncblock := compblocksize(r, i.Depth)
    23  	for miny != r.Max.Y {
    24  		maxy := atoi(data[0*12:])
    25  		nb := atoi(data[1*12:])
    26  		if maxy <= miny || r.Max.Y < maxy {
    27  			return 0, fmt.Errorf("creadimage: bad maxy %d", maxy)
    28  		}
    29  		data = data[2*12:]
    30  		m += 2 * 12
    31  		if nb <= 0 || ncblock < nb || nb > len(data) {
    32  			return 0, fmt.Errorf("creadimage: bad count %d", nb)
    33  		}
    34  		// TODO: error check?
    35  		a := i.Display.bufimage(21 + nb)
    36  		a[0] = 'Y'
    37  		bplong(a[1:], i.id)
    38  		bplong(a[5:], uint32(r.Min.Y))
    39  		bplong(a[9:], uint32(miny))
    40  		bplong(a[13:], uint32(r.Max.Y))
    41  		bplong(a[17:], uint32(maxy))
    42  		copy(a[21:], data)
    43  		miny = maxy
    44  		data = data[nb:]
    45  		m += nb
    46  	}
    47  	return m, nil
    48  }