Exemplo n.º 1
0
// average convert the sums to averages and returns the result.
func average(sum []uint64, w, h int, n uint64) tipimage.Image {
	ret := tipimage.NewRGBA(tipimage.Rect(0, 0, w, h))
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			index := 4 * (y*w + x)
			ret.SetRGBA(x, y, color.RGBA{
				uint8(sum[index+0] / n),
				uint8(sum[index+1] / n),
				uint8(sum[index+2] / n),
				uint8(sum[index+3] / n),
			})
		}
	}
	return ret
}
Exemplo n.º 2
0
// Resample returns a resampled copy of the tipimage slice r of m.
// The returned tipimage has width w and height h.
func Resample(m tipimage.Image, r tipimage.Rectangle, w, h int) tipimage.Image {
	if w < 0 || h < 0 {
		return nil
	}
	if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
		return tipimage.NewRGBA64(tipimage.Rect(0, 0, w, h))
	}
	curw, curh := r.Dx(), r.Dy()
	img := tipimage.NewRGBA(tipimage.Rect(0, 0, w, h))
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			// Get a source pixel.
			subx := x * curw / w
			suby := y * curh / h
			r32, g32, b32, a32 := m.At(subx, suby).RGBA()
			r := uint8(r32 >> 8)
			g := uint8(g32 >> 8)
			b := uint8(b32 >> 8)
			a := uint8(a32 >> 8)
			img.SetRGBA(x, y, color.RGBA{r, g, b, a})
		}
	}
	return img
}