diff --git a/imageUtils.go b/imageUtils.go new file mode 100644 index 0000000..dac07b0 --- /dev/null +++ b/imageUtils.go @@ -0,0 +1,61 @@ +package main + +import ( + "image" + "os" + + "github.com/dhowden/tag" + "github.com/nfnt/resize" +) + +/* + Gets the Image Path from the uri to the string passed + if embedded image is found the path to that Image is returned else + path to default image is sent. +*/ +func getAlbumArt(uri string) string { + var path string = "default.jpg" + f, err := os.Open(uri) + if err != nil { + panic(err) + } + m, err := tag.ReadFrom(f) + if err != nil { + panic(err) + } + albumCover := m.Picture() + if albumCover != nil { + b, err := os.Create("hello.jpg") + if err != nil { + panic(err) + } + defer b.Close() + b.Write(albumCover.Data) + path = "hello.jpg" + b.Close() + } + f.Close() + return path +} + +func getImg(uri string) (image.Image, error) { + + f, err := os.Open(uri) + if err != nil { + return nil, err + } + defer f.Close() + + img, _, err := image.Decode(f) + if err != nil { + return nil, err + } + + img = resize.Thumbnail( + uint(IMG_W*22), uint(IMG_H*15), + img, + resize.Bilinear, + ) + + return img, nil +} diff --git a/render.go b/render.go new file mode 100644 index 0000000..88d115d --- /dev/null +++ b/render.go @@ -0,0 +1,64 @@ +package main + +import ( + "gitlab.com/diamondburned/ueberzug-go" +) + +/* + Renderer is just a channel on which we will send the Path to the song whose + Image is to be Rendered. This channel is passed to the openImage which in turn is called + by the Start() function as a go routine. +*/ +type Renderer struct { + c chan string +} + +/* + Returns a new Renderer with a string channel +*/ +func newRenderer() *Renderer { + c := make(chan string) + return &Renderer{ + c: c, + } +} + +/* + Send Image Path to Renderer +*/ +func (self *Renderer) Send(path string) { + self.c <- path +} + +/* + + Go Routine that will Be Called and will listen on the channel c + for changes and on getting a string over the channel will open the Image and + keep listening again. This will keep the image blocked ( i.e no need to use time.Sleep() etc. ) + and saves resources too. + +*/ +func openImage(path string, c chan string) { + var im *ueberzug.Image + if path != "stop" { + img2, _ := getImg(getAlbumArt(path)) + im, _ = ueberzug.NewImage(img2, (IMG_X*10)+15, (IMG_Y*22)+10) + } + d := <-c + if im != nil { + im.Clear() + } + if d != "stop" { + openImage(d, c) + } else { + openImage("stop", c) + } +} + +/* + Initialises the Renderer and calls the go routine openImage and passes the channel + as argument. +*/ +func (self *Renderer) Start(path string) { + go openImage(path, self.c) +}