Skip to content

Commit

Permalink
Adding background support for non-svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Apr 8, 2024
1 parent ab6a7a0 commit 5def9c3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/kenshaw/iv
go 1.20

require (
github.com/kenshaw/colors v0.1.2
github.com/kenshaw/colors v0.1.4
github.com/kenshaw/rasterm v0.1.10
github.com/spf13/cobra v1.8.0
github.com/xo/resvg v0.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kenshaw/colors v0.1.2 h1:qy1V9XzxSbUkFW3WyeTf3+ma3604Qks/S9iWDi3cyqE=
github.com/kenshaw/colors v0.1.2/go.mod h1:m8LcSiaLgIxtzCHQqxReKkJPP5TfXUZZdQvFdCuQGyY=
github.com/kenshaw/colors v0.1.4 h1:2IbZLfJJlLuUyHN6/vgMK+1b4g+9GcD6u2pBx9GwviE=
github.com/kenshaw/colors v0.1.4/go.mod h1:m8LcSiaLgIxtzCHQqxReKkJPP5TfXUZZdQvFdCuQGyY=
github.com/kenshaw/rasterm v0.1.10 h1:cMCTpBHfqmftt/VqeT6B+9Td+mYi+ZtziN+XBdrTQfA=
github.com/kenshaw/rasterm v0.1.10/go.mod h1:kL4DCN+wOlQ4BPBCxA+itiVwiObRAj0Hkze7SbCyYaw=
github.com/kenshaw/snaker v0.2.0 h1:DPlxCtAv9mw1wSsvIN1khUAPJUIbFJUckMIDWSQ7TC8=
Expand Down
34 changes: 24 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"image"
"image/color"
"image/draw"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -111,7 +112,7 @@ func do(w io.Writer, bg color.Color, args []string) error {
}
files = append(files, v...)
}
return render(w, files)
return render(w, bg, files)
}

func open(name string) ([]string, error) {
Expand All @@ -138,36 +139,49 @@ func open(name string) ([]string, error) {

var extRE = regexp.MustCompile(`(?i)\.(jpe?g|gif|png|svg|bmp|bitmap|tiff?|hei[vc]|webp)$`)

func render(w io.Writer, files []string) error {
func render(w io.Writer, bg color.Color, files []string) error {
var c color.Color
if !colors.Is(bg, colors.Transparent) {
c = color.NRGBAModel.Convert(bg).(color.NRGBA)
}
for i := 0; i < len(files); i++ {
if err := renderFile(w, files[i]); err != nil {
if err := renderFile(w, c, files[i]); err != nil {
fmt.Fprintf(w, "error: unable to render arg %d: %v\n", i, err)
}
}
return nil
}

// doFile renders the specified file to w.
func renderFile(w io.Writer, file string) error {
func renderFile(w io.Writer, bg color.Color, file string) error {
fmt.Fprintln(w, file+":")
f, err := os.OpenFile(file, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("can't open %s: %w", file, err)
}
img, _, err := image.Decode(f)
img, typ, err := image.Decode(f)
if err != nil {
defer f.Close()
return fmt.Errorf("can't decode %s: %w", file, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("can't close %s: %w", file, err)
}
if typ != "svg" && bg != nil {
img = addBackground(bg.(color.NRGBA), img)
}
return rasterm.Encode(w, img)
}

/*
func init() {
_ "github.com/jcbritobr/pnm"
image.RegisterFormat("pbm", "P?", Decode, DecodeConfig)
// addBackground adds a background for the image.
func addBackground(bg color.NRGBA, fg image.Image) image.Image {
b := fg.Bounds()
img := image.NewNRGBA(b)
for i := 0; i < b.Dx(); i++ {
for j := 0; j < b.Dy(); j++ {
img.SetNRGBA(i, j, bg)
}
}
draw.Draw(img, b, fg, image.Point{}, draw.Over)
return img
}
*/

0 comments on commit 5def9c3

Please sign in to comment.