render rgba image
This commit is contained in:
parent
de37cd71fa
commit
0e727c5a5b
@ -1,12 +1,10 @@
|
|||||||
package term_image
|
package term_image
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
"image/png"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,21 +18,14 @@ func LoadImage(filePath string) (image.Image, error) {
|
|||||||
return img, err
|
return img, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func EncodeImageToBase64RGBA(img image.Image) image.Image {
|
func ConvertToRGBA(img image.Image) *image.RGBA {
|
||||||
rgba := image.NewRGBA(img.Bounds())
|
rgba := image.NewRGBA(img.Bounds())
|
||||||
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
|
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
|
||||||
fmt.Print(rgba)
|
|
||||||
return rgba
|
return rgba
|
||||||
}
|
}
|
||||||
func EncodeImageToBase64(img image.Image) (string, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := png.Encode(&buf, img)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("Error encodeing image: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
encoded := base64.StdEncoding.EncodeToString(buf.Bytes())
|
func EncodeImageToBase64RGBA(rgba *image.RGBA) string {
|
||||||
return encoded, nil
|
return base64.StdEncoding.EncodeToString(rgba.Pix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderImage(filepath string, row int, col int) {
|
func RenderImage(filepath string, row int, col int) {
|
||||||
@ -44,26 +35,34 @@ func RenderImage(filepath string, row int, col int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
EncodeImageToBase64RGBA(img)
|
rgba := ConvertToRGBA(img)
|
||||||
encoded, err := EncodeImageToBase64(img)
|
encoded := EncodeImageToBase64RGBA(rgba)
|
||||||
|
|
||||||
|
width := rgba.Rect.Dx()
|
||||||
|
height := rgba.Rect.Dy()
|
||||||
|
|
||||||
fmt.Printf("\033[s\033[%d;%dH", row, col)
|
fmt.Printf("\033[s\033[%d;%dH", row, col)
|
||||||
|
|
||||||
chunk_size := 4096
|
chunk_size := 4096
|
||||||
pos := 0
|
pos := 0
|
||||||
|
first := true
|
||||||
for pos < len(encoded) {
|
for pos < len(encoded) {
|
||||||
fmt.Print("\033_G")
|
fmt.Print("\033_G")
|
||||||
if pos == 0 {
|
if first {
|
||||||
fmt.Print("q=2,a=T,f=100,")
|
fmt.Printf("q=2,a=T,f=32,s=%d,v=%d,", width, height)
|
||||||
|
first = false
|
||||||
}
|
}
|
||||||
remaining := len(encoded) - pos
|
remaining := len(encoded) - pos
|
||||||
chunk_len := remaining
|
chunk_len := remaining
|
||||||
if remaining > chunk_size {
|
if chunk_len > chunk_size {
|
||||||
chunk_len = chunk_size
|
chunk_len = chunk_size
|
||||||
}
|
}
|
||||||
if pos+chunk_len < len(encoded) {
|
if pos+chunk_len < len(encoded) {
|
||||||
fmt.Print("m=1")
|
fmt.Print("m=1;")
|
||||||
|
} else {
|
||||||
|
fmt.Print(";")
|
||||||
}
|
}
|
||||||
fmt.Printf(";%s", encoded[pos:][:chunk_len])
|
fmt.Print(encoded[pos : pos+chunk_len])
|
||||||
fmt.Print("\033\\")
|
fmt.Print("\033\\")
|
||||||
pos += chunk_len
|
pos += chunk_len
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user