electricboogaloo/image.go

96 lines
2.1 KiB
Go

package main
import (
"bytes"
"fmt"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"os"
"strconv"
"golang.org/x/image/draw"
)
func retrieveArt(path string) ([]byte, error) {
//streaming PC is primarily flacs, so flacs first, then mp3s, then check
//for covers in folder, finally just use a default image if none of the
//above exist.
switch {
case hasFlacTrackArt(path):
return getFlacArt(path)
case hasMp3TrackArt(path):
return getMP3Art(path)
case hasCoverArtFile(path):
return getCoverArtFile(path)
default:
return getDefaultArt()
}
}
func writeArtFile(orig []byte) (string, error) {
imageSize := 700
pathOut := "/run/user/1000/albumcover.jpg"
img := make([]byte, 0)
imgBuffIn, imgBuffOut := bytes.NewBuffer(orig), bytes.NewBuffer(img)
imgOrig, _, err := image.Decode(imgBuffIn)
if err != nil {
return "", err
}
f, err := os.Create(pathOut)
if err != nil {
return "", err
}
defer f.Close()
imgOut := image.NewRGBA(image.Rect(0, 0, imageSize, imageSize))
draw.ApproxBiLinear.Scale(imgOut, imgOut.Rect, imgOrig, imgOrig.Bounds(), draw.Over, nil)
err = jpeg.Encode(imgBuffOut, imgOut, nil)
if err != nil {
return "", err
}
_, err = f.Write(imgBuffOut.Bytes())
if err != nil {
return "", err
}
return pathOut, nil
}
func processBGColor(path string) (float64, error) {
imageSize := 1
f, err := os.ReadFile(path)
if err != nil {
return 0, err
}
imgBuff := bytes.NewBuffer(f)
imgOrig, _, err := image.Decode(imgBuff)
if err != nil {
return 0, err
}
img := image.NewRGBA(image.Rect(0, 0, imageSize, imageSize))
draw.ApproxBiLinear.Scale(img, img.Rect, imgOrig, imgOrig.Bounds(), draw.Over, nil)
r, g, b, _ := img.At(0, 0).RGBA()
// convert from 32 bit color back into 8 bit color
// this is to undo x |= x << 8 each value goes through
// trust me it works bro. bro it works.
r &= r >> 8
g &= g >> 8
b &= b >> 8
//a &= a >> 8
// 25 becomes 0x19, 36 is close to 10% of 256
fmt.Printf("0x%02x %02x %02x %02x\n", 25, r, g, b)
// it wants it abgr for some reason
rgba := fmt.Sprintf("0x%02x%02x%02x%02x", 25, b, g, r)
rgbaInt, err := strconv.ParseInt(rgba, 0, 64)
return float64(rgbaInt), err
}