Exemple #1
0
func NewMemPImageFrom(m image.Image) *MemPImage {
	if p, ok := m.(*MemPImage); ok {
		return p.Clone()
	}
	if p, ok := AsMemPImage(m); ok {
		return p.Clone()
	}

	switch m := m.(type) {
	case *image.Gray:
		b := m.Bounds()
		p := NewMemPImage(b, 1, reflect.Uint8)

		for y := b.Min.Y; y < b.Max.Y; y++ {
			off0 := m.PixOffset(0, y)
			off1 := p.PixOffset(0, y)
			copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
			off0 += m.Stride
			off1 += p.XStride
		}
		return p

	case *image.Gray16:
		b := m.Bounds()
		p := NewMemPImage(b, 1, reflect.Uint16)

		for y := b.Min.Y; y < b.Max.Y; y++ {
			off0 := m.PixOffset(0, y)
			off1 := p.PixOffset(0, y)
			copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
			off0 += m.Stride
			off1 += p.XStride
		}
		if isLittleEndian {
			p.XPix.SwapEndian(p.XDataType)
		}
		return p

	case *image.RGBA:
		b := m.Bounds()
		p := NewMemPImage(b, 4, reflect.Uint8)

		for y := b.Min.Y; y < b.Max.Y; y++ {
			off0 := m.PixOffset(0, y)
			off1 := p.PixOffset(0, y)
			copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
			off0 += m.Stride
			off1 += p.XStride
		}
		return p

	case *image.RGBA64:
		b := m.Bounds()
		p := NewMemPImage(b, 4, reflect.Uint16)

		for y := b.Min.Y; y < b.Max.Y; y++ {
			off0 := m.PixOffset(0, y)
			off1 := p.PixOffset(0, y)
			copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
			off0 += m.Stride
			off1 += p.XStride
		}
		if isLittleEndian {
			p.XPix.SwapEndian(p.XDataType)
		}
		return p

	case *image.YCbCr:
		b := m.Bounds()
		p := NewMemPImage(b, 4, reflect.Uint8)
		for y := b.Min.Y; y < b.Max.Y; y++ {
			for x := b.Min.X; x < b.Max.X; x++ {
				R, G, B, A := m.At(x, y).RGBA()

				i := p.PixOffset(x, y)
				p.XPix[i+0] = uint8(R >> 8)
				p.XPix[i+1] = uint8(G >> 8)
				p.XPix[i+2] = uint8(B >> 8)
				p.XPix[i+3] = uint8(A >> 8)
			}
		}
		return p

	default:
		b := m.Bounds()
		p := NewMemPImage(b, 4, reflect.Uint16)
		for y := b.Min.Y; y < b.Max.Y; y++ {
			for x := b.Min.X; x < b.Max.X; x++ {
				R, G, B, A := m.At(x, y).RGBA()

				i := p.PixOffset(x, y)
				p.XPix[i+0] = uint8(R >> 8)
				p.XPix[i+1] = uint8(R)
				p.XPix[i+2] = uint8(G >> 8)
				p.XPix[i+3] = uint8(G)
				p.XPix[i+4] = uint8(B >> 8)
				p.XPix[i+5] = uint8(B)
				p.XPix[i+6] = uint8(A >> 8)
				p.XPix[i+7] = uint8(A)
			}
		}
		return p
	}
}