Adds terminal unit resizing
Improves image rendering by allowing resizing based on terminal cell size, providing more control over image dimensions. This is enabled by a new `units` parameter where 0 defaults to the previous pixel sizing and 1 enables terminal unit sizing. Removes unused dependency.
This commit is contained in:
parent
7bde699fc2
commit
e3f9693cf6
16
cmd/main.go
16
cmd/main.go
@ -1,21 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"whspbrd/pkg/render_image"
|
||||||
"whspbrd/pkg/cell_size"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//render_image.RenderImage("kogami-pf-edit.jpg", 0, 3, 750, 0)
|
render_image.RenderImage("kogami-pf-edit.jpg", 0, 3, 20, 0, 1)
|
||||||
|
|
||||||
w, h, err := cell_size.GetTerminalCellSizePixels()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error getting terminal cell size:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
width, height := cell_size.GetConsoleSize()
|
|
||||||
|
|
||||||
fmt.Println("Terminal cell size in pixels:", w, "x", h)
|
|
||||||
fmt.Println("Console size in characters:", width, "x", height)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
18
examples/getTerminalSize.go
Normal file
18
examples/getTerminalSize.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"whspbrd/pkg/cell_size"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
w, h, err := cell_size.GetTerminalCellSizePixels()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting terminal cell size:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
width, height := cell_size.GetConsoleSize()
|
||||||
|
|
||||||
|
fmt.Println("Terminal cell size in pixels:", w, "x", h)
|
||||||
|
fmt.Println("Console size in characters:", width, "x", height)
|
||||||
|
}
|
||||||
9
go.mod
9
go.mod
@ -3,20 +3,11 @@ module whspbrd
|
|||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/getlantern/systray v1.2.2
|
|
||||||
github.com/jroimartin/gocui v0.5.0
|
github.com/jroimartin/gocui v0.5.0
|
||||||
golang.org/x/sys v0.30.0
|
golang.org/x/sys v0.30.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect
|
|
||||||
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 // indirect
|
|
||||||
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 // indirect
|
|
||||||
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 // indirect
|
|
||||||
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 // indirect
|
|
||||||
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f // indirect
|
|
||||||
github.com/go-stack/stack v1.8.0 // indirect
|
|
||||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||||
github.com/nsf/termbox-go v1.1.1 // indirect
|
github.com/nsf/termbox-go v1.1.1 // indirect
|
||||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
31
go.sum
31
go.sum
@ -1,39 +1,8 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 h1:NRUJuo3v3WGC/g5YiyF790gut6oQr5f3FBI88Wv0dx4=
|
|
||||||
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
|
|
||||||
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 h1:6uJ+sZ/e03gkbqZ0kUG6mfKoqDb4XMAzMIwlajq19So=
|
|
||||||
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7/go.mod h1:l+xpFBrCtDLpK9qNjxs+cHU6+BAdlBaxHqikB6Lku3A=
|
|
||||||
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 h1:guBYzEaLz0Vfc/jv0czrr2z7qyzTOGC9hiQ0VC+hKjk=
|
|
||||||
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7/go.mod h1:zx/1xUUeYPy3Pcmet8OSXLbF47l+3y6hIPpyLWoR9oc=
|
|
||||||
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 h1:micT5vkcr9tOVk1FiH8SWKID8ultN44Z+yzd2y/Vyb0=
|
|
||||||
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7/go.mod h1:dD3CgOrwlzca8ed61CsZouQS5h5jIzkK9ZWrTcf0s+o=
|
|
||||||
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 h1:XYzSdCbkzOC0FDNrgJqGRo8PCMFOBFL9py72DRs7bmc=
|
|
||||||
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55/go.mod h1:6mmzY2kW1TOOrVy+r41Za2MxXM+hhqTtY3oBKd2AgFA=
|
|
||||||
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f h1:wrYrQttPS8FHIRSlsrcuKazukx/xqO/PpLZzZXsF+EA=
|
|
||||||
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f/go.mod h1:D5ao98qkA6pxftxoqzibIBBrLSUli+kYnJqrgBf9cIA=
|
|
||||||
github.com/getlantern/systray v1.2.2 h1:dCEHtfmvkJG7HZ8lS/sLklTH4RKUcIsKrAD9sThoEBE=
|
|
||||||
github.com/getlantern/systray v1.2.2/go.mod h1:pXFOI1wwqwYXEhLPm9ZGjS2u/vVELeIgNMY5HvhHhcE=
|
|
||||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
|
||||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
|
||||||
github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
|
github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
|
||||||
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
|
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
|
||||||
github.com/lxn/walk v0.0.0-20210112085537-c389da54e794/go.mod h1:E23UucZGqpuUANJooIbHWCufXvOcT6E7Stq81gU+CSQ=
|
|
||||||
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
|
|
||||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||||
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
|
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
|
||||||
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
|
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
|
||||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
|
|
||||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
|
||||||
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
gopkg.in/Knetic/govaluate.v3 v3.0.0/go.mod h1:csKLBORsPbafmSCGTEh3U7Ozmsuq8ZSIlKk1bcqph0E=
|
|
||||||
|
|||||||
@ -83,7 +83,7 @@ func updateChatView(v *gocui.View) {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
// Print image directly to terminal (stdout)
|
// Print image directly to terminal (stdout)
|
||||||
render_image.RenderImage("kogami-rounded.png", i*3+2, 23, 50, 50)
|
render_image.RenderImage("kogami-rounded.png", i*3+2, 23, 50, 50, 0)
|
||||||
//err = kittyimg.Fprintln(os.Stdout, img)
|
//err = kittyimg.Fprintln(os.Stdout, img)
|
||||||
//if err != nil {
|
//if err != nil {
|
||||||
// log.Println("Error rendering image:", err)
|
// log.Println("Error rendering image:", err)
|
||||||
|
|||||||
@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
|
_ "image/jpeg"
|
||||||
|
_ "image/png"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"whspbrd/pkg/resize_image"
|
"whspbrd/pkg/resize_image"
|
||||||
@ -30,7 +32,7 @@ func encodeImageToBase64RGBA(rgba *image.RGBA) string {
|
|||||||
return base64.StdEncoding.EncodeToString(rgba.Pix)
|
return base64.StdEncoding.EncodeToString(rgba.Pix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderImage(filepath string, row int, col int, width_ int, height_ int) {
|
func RenderImage(filepath string, row int, col int, width_ int, height_ int, units int) {
|
||||||
img, err := LoadImage(filepath)
|
img, err := LoadImage(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error loading image: %v\n", err)
|
fmt.Printf("Error loading image: %v\n", err)
|
||||||
@ -38,7 +40,11 @@ func RenderImage(filepath string, row int, col int, width_ int, height_ int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rgba := convertToRGBA(img)
|
rgba := convertToRGBA(img)
|
||||||
|
if units == 1 {
|
||||||
|
rgba, _ = resize_image.ResizeInTerminal(*rgba, width_, height_)
|
||||||
|
} else {
|
||||||
rgba, _ = resize_image.Resize(*rgba, width_, height_)
|
rgba, _ = resize_image.Resize(*rgba, width_, height_)
|
||||||
|
}
|
||||||
encoded := encodeImageToBase64RGBA(rgba)
|
encoded := encodeImageToBase64RGBA(rgba)
|
||||||
|
|
||||||
width := rgba.Rect.Dx()
|
width := rgba.Rect.Dx()
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package resize_image
|
|||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"whspbrd/pkg/cell_size"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Resize(img image.RGBA, width int, height int) (*image.RGBA, error) {
|
func Resize(img image.RGBA, width int, height int) (*image.RGBA, error) {
|
||||||
@ -34,7 +35,34 @@ func Resize(img image.RGBA, width int, height int) (*image.RGBA, error) {
|
|||||||
return newImg, nil
|
return newImg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ResizeInTerminal(img image.RGBA, columns int, rows int) (*image.RGBA, error) {
|
||||||
|
if columns <= 0 && rows <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
width := convertColumnsToPixels(columns)
|
||||||
|
height := convertRowsToPixels(rows)
|
||||||
|
|
||||||
|
return Resize(img, width, height)
|
||||||
|
}
|
||||||
|
|
||||||
func getPixel(img image.RGBA, x int, y int) (uint8, uint8, uint8, uint8) {
|
func getPixel(img image.RGBA, x int, y int) (uint8, uint8, uint8, uint8) {
|
||||||
index := img.PixOffset(x, y)
|
index := img.PixOffset(x, y)
|
||||||
return img.Pix[index], img.Pix[index+1], img.Pix[index+2], img.Pix[index+3]
|
return img.Pix[index], img.Pix[index+1], img.Pix[index+2], img.Pix[index+3]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertColumnsToPixels(columns int) int {
|
||||||
|
w, _, err := cell_size.GetTerminalCellSizePixels()
|
||||||
|
if err != nil {
|
||||||
|
w = 8
|
||||||
|
}
|
||||||
|
return columns * w
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertRowsToPixels(rows int) int {
|
||||||
|
_, h, err := cell_size.GetTerminalCellSizePixels()
|
||||||
|
if err != nil {
|
||||||
|
h = 16
|
||||||
|
}
|
||||||
|
return rows * h
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user