Exemple #1
0
// WriteTo writes the image data to wr.
func (im *Image) WriteTo(wr io.Writer) (int, error) {
	written := 0
	for r := 1; r < im.rows; r += 2 {
		var before *Block
		for c := 1; c < im.cols; c++ {
			x := int(im.ratio * float64(c))
			yt := int(im.ratio * float64(r-1))
			yb := int(im.ratio * float64(r))

			b := &Block{
				Top:    ansirgb.Convert(im.im.At(x, yt)),
				Bottom: ansirgb.Convert(im.im.At(x, yb)),
			}

			if before != nil {
				b.nocolor = b.equals(before)
			}
			before = b

			n, err := io.WriteString(wr, b.String())
			written += n
			if err != nil {
				return written, err
			}
		}
		n, err := io.WriteString(wr, newLine())
		written += n
		if err != nil {
			return written, err
		}
	}
	return written, nil
}
Exemple #2
0
func getAnsiStream(byteChan chan []byte) {
	conn, err := net.Dial("tcp", "ws.revisit.link:80")
	if err != nil {
		panic(err)
	}

	h := make(http.Header)
	u, _ := url.Parse("http://ws.revisit.link:80/message")
	ws, _, err := websocket.NewClient(conn, u, h, 1024, 1024)
	if err != nil {
		log.Println(err)
	}

	for {
		_, p, err := ws.ReadMessage()
		if err != nil {
			log.Println(err)
			continue
		}

		id := gorevisit.ImageData{
			Data: string(p),
		}

		msg := &gorevisit.RevisitMsg{
			Content: id,
		}

		reader := msg.Content.ByteReader()
		img, _, err := image.Decode(reader)
		if err != nil {
			log.Println(err)
		}

		imgWidth := img.Bounds().Dx()
		imgHeight := img.Bounds().Dy()
		if imgWidth < width {
			width = imgWidth
		}
		ratio := float64(imgWidth) / float64(width)
		rows := int(float64(imgHeight) / ratio)
		for i := 1; i < rows; i += 2 {
			fmt.Println("")
		}
		cursorUp(rows / 2)

		var buf bytes.Buffer

		for i := 1; i < rows; i += 2 {
			for j := 0; j < width; j++ {
				x := int(ratio * float64(j))
				yTop := int(ratio * float64(i-1))
				yBottom := int(ratio * float64(i))
				top := ansirgb.Convert(img.At(x, yTop))
				bottom := ansirgb.Convert(img.At(x, yBottom))
				b := &block{}
				b.bottom = bottom
				if top.Code != bottom.Code {
					b.top = top
				}
				fmt.Fprintf(&buf, "%s", b)
			}
			reset()
			fmt.Fprintf(&buf, "\n")
		}
		content := buf.Bytes()
		byteChan <- content
	}
}