Ejemplo n.º 1
0
func SizeofImage(m image.Image) int {
	if m, ok := m.(SizeofImager); ok {
		return m.SizeofImage()
	}
	if m, ok := AsMemPImage(m); ok {
		return int(unsafe.Sizeof(*m)) + len(m.XPix)
	}

	b := m.Bounds()
	switch m := m.(type) {
	case *image.Alpha:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*1
	case *image.Alpha16:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*2
	case *image.Gray:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*1
	case *image.Gray16:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*2
	case *image.NRGBA:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*4
	case *image.NRGBA64:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*8
	case *image.RGBA:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*4
	case *image.RGBA64:
		return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*8
	case *image.Uniform:
		return int(unsafe.Sizeof(*m))
	case *image.YCbCr:
		return int(unsafe.Sizeof(*m)) + len(m.Y) + len(m.Cb) + len(m.Cr)
	}

	// return same as RGBA64 size
	return int(unsafe.Sizeof((*image.RGBA64)(nil))) + b.Dx()*b.Dy()*8
}