Cleanup image widget some more
This commit is contained in:
parent
0819f87750
commit
0eb6d61079
@ -27,20 +27,20 @@ func main() {
|
|||||||
for _, arg := range os.Args[1:] {
|
for _, arg := range os.Args[1:] {
|
||||||
resp, err := http.Get(arg)
|
resp, err := http.Get(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatalf("failed to fetch image: %v", err)
|
||||||
}
|
}
|
||||||
im, _, err := image.Decode(resp.Body)
|
image, _, err := image.Decode(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatalf("failed to decode fetched image: %v", err)
|
||||||
}
|
}
|
||||||
images = append(images, im)
|
images = append(images, image)
|
||||||
}
|
}
|
||||||
if len(images) == 0 {
|
if len(images) == 0 {
|
||||||
im, _, err := image.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(gopher)))
|
image, _, err := image.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(GOPHER_IMAGE)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatalf("failed to decode gopher image: %v", err)
|
||||||
}
|
}
|
||||||
images = append(images, im)
|
images = append(images, image)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ui.Init(); err != nil {
|
if err := ui.Init(); err != nil {
|
||||||
@ -49,7 +49,7 @@ func main() {
|
|||||||
defer ui.Close()
|
defer ui.Close()
|
||||||
|
|
||||||
img := widgets.NewImage(nil)
|
img := widgets.NewImage(nil)
|
||||||
img.SetRect(0, 0, 50, 50)
|
img.SetRect(0, 0, 100, 50)
|
||||||
index := 0
|
index := 0
|
||||||
render := func() {
|
render := func() {
|
||||||
img.Image = images[index]
|
img.Image = images[index]
|
||||||
@ -70,26 +70,21 @@ func main() {
|
|||||||
switch e.ID {
|
switch e.ID {
|
||||||
case "q", "<C-c>":
|
case "q", "<C-c>":
|
||||||
return
|
return
|
||||||
case "<Left>":
|
case "<Left>", "h":
|
||||||
index = (index + len(images) - 1) % len(images)
|
index = (index + len(images) - 1) % len(images)
|
||||||
render()
|
case "<Right>", "l":
|
||||||
case "<Right>":
|
|
||||||
index = (index + 1) % len(images)
|
index = (index + 1) % len(images)
|
||||||
render()
|
case "<Up>", "k":
|
||||||
case "<Up>":
|
|
||||||
img.MonochromeThreshold++
|
img.MonochromeThreshold++
|
||||||
render()
|
case "<Down>", "j":
|
||||||
case "<Down>":
|
|
||||||
img.MonochromeThreshold--
|
img.MonochromeThreshold--
|
||||||
render()
|
|
||||||
case "<Enter>":
|
case "<Enter>":
|
||||||
img.Monochrome = !img.Monochrome
|
img.Monochrome = !img.Monochrome
|
||||||
render()
|
case "<Tab>":
|
||||||
case "\\":
|
|
||||||
img.MonochromeInvert = !img.MonochromeInvert
|
img.MonochromeInvert = !img.MonochromeInvert
|
||||||
render()
|
|
||||||
}
|
}
|
||||||
|
render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const gopher = `iVBORw0KGgoAAAANSUhEUgAAAEsAAAA8CAAAAAALAhhPAAAFfUlEQVRYw62XeWwUVRzHf2+OPbo9d7tsWyiyaZti6eWGAhISoIGKECEKCAiJJkYTiUgTMYSIosYYBBIUIxoSPIINEBDi2VhwkQrVsj1ESgu9doHWdrul7ba73WNm3vOPtsseM9MdwvvrzTs+8/t95ze/33sI5BqiabU6m9En8oNjduLnAEDLUsQXFF8tQ5oxK3vmnNmDSMtrncks9Hhtt/qeWZapHb1ha3UqYSWVl2ZmpWgaXMXGohQAvmeop3bjTRtv6SgaK/Pb9/bFzUrYslbFAmHPp+3WhAYdr+7GN/YnpN46Opv55VDsJkoEpMrY/vO2BIYQ6LLvm0ThY3MzDzzeSJeeWNyTkgnIE5ePKsvKlcg/0T9QMzXalwXMlj54z4c0rh/mzEfr+FgWEz2w6uk8dkzFAgcARAgNp1ZYef8bH2AgvuStbc2/i6CiWGj98y2tw2l4FAXKkQBIf+exyRnteY83LfEwDQAYCoK+P6bxkZm/0966LxcAAILHB56kgD95PPxltuYcMtFTWw/F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jZ+0hQcPEgId/Fn/pl9crKH02+5o2b9T/eMx7pKoskYgAAAABJRU5ErkJggg==`
|
||||||
|
@ -48,10 +48,30 @@ func (self *Image) Draw(buf *Buffer) {
|
|||||||
}
|
}
|
||||||
for bx := 0; bx < bufWidth; bx++ {
|
for bx := 0; bx < bufWidth; bx++ {
|
||||||
for by := 0; by < bufHeight; by++ {
|
for by := 0; by < bufHeight; by++ {
|
||||||
ul := self.colorAverage(2*bx*imageWidth/bufWidth/2, (2*bx+1)*imageWidth/bufWidth/2, 2*by*imageHeight/bufHeight/2, (2*by+1)*imageHeight/bufHeight/2)
|
ul := self.colorAverage(
|
||||||
ur := self.colorAverage((2*bx+1)*imageWidth/bufWidth/2, (2*bx+2)*imageWidth/bufWidth/2, 2*by*imageHeight/bufHeight/2, (2*by+1)*imageHeight/bufHeight/2)
|
2*bx*imageWidth/bufWidth/2,
|
||||||
ll := self.colorAverage(2*bx*imageWidth/bufWidth/2, (2*bx+1)*imageWidth/bufWidth/2, (2*by+1)*imageHeight/bufHeight/2, (2*by+2)*imageHeight/bufHeight/2)
|
(2*bx+1)*imageWidth/bufWidth/2,
|
||||||
lr := self.colorAverage((2*bx+1)*imageWidth/bufWidth/2, (2*bx+2)*imageWidth/bufWidth/2, (2*by+1)*imageHeight/bufHeight/2, (2*by+2)*imageHeight/bufHeight/2)
|
2*by*imageHeight/bufHeight/2,
|
||||||
|
(2*by+1)*imageHeight/bufHeight/2,
|
||||||
|
)
|
||||||
|
ur := self.colorAverage(
|
||||||
|
(2*bx+1)*imageWidth/bufWidth/2,
|
||||||
|
(2*bx+2)*imageWidth/bufWidth/2,
|
||||||
|
2*by*imageHeight/bufHeight/2,
|
||||||
|
(2*by+1)*imageHeight/bufHeight/2,
|
||||||
|
)
|
||||||
|
ll := self.colorAverage(
|
||||||
|
2*bx*imageWidth/bufWidth/2,
|
||||||
|
(2*bx+1)*imageWidth/bufWidth/2,
|
||||||
|
(2*by+1)*imageHeight/bufHeight/2,
|
||||||
|
(2*by+2)*imageHeight/bufHeight/2,
|
||||||
|
)
|
||||||
|
lr := self.colorAverage(
|
||||||
|
(2*bx+1)*imageWidth/bufWidth/2,
|
||||||
|
(2*bx+2)*imageWidth/bufWidth/2,
|
||||||
|
(2*by+1)*imageHeight/bufHeight/2,
|
||||||
|
(2*by+2)*imageHeight/bufHeight/2,
|
||||||
|
)
|
||||||
buf.SetCell(
|
buf.SetCell(
|
||||||
NewCell(blocksChar(ul, ur, ll, lr, self.MonochromeThreshold, self.MonochromeInvert)),
|
NewCell(blocksChar(ul, ur, ll, lr, self.MonochromeThreshold, self.MonochromeInvert)),
|
||||||
image.Pt(self.Inner.Min.X+bx, self.Inner.Min.Y+by),
|
image.Pt(self.Inner.Min.X+bx, self.Inner.Min.Y+by),
|
||||||
@ -74,13 +94,7 @@ func (self *Image) Draw(buf *Buffer) {
|
|||||||
(by+1)*imageHeight/bufHeight,
|
(by+1)*imageHeight/bufHeight,
|
||||||
)
|
)
|
||||||
buf.SetCell(
|
buf.SetCell(
|
||||||
Cell{
|
NewCell(c.ch(), NewStyle(c.fgColor(), ColorBlack)),
|
||||||
Rune: c.ch(),
|
|
||||||
Style: NewStyle(
|
|
||||||
c.fgColor(),
|
|
||||||
ColorBlack,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
image.Pt(self.Inner.Min.X+bx, self.Inner.Min.Y+by),
|
image.Pt(self.Inner.Min.X+bx, self.Inner.Min.Y+by),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -92,7 +106,12 @@ func (self *Image) colorAverage(x0, x1, y0, y1 int) colorAverager {
|
|||||||
var c colorAverager
|
var c colorAverager
|
||||||
for x := x0; x < x1; x++ {
|
for x := x0; x < x1; x++ {
|
||||||
for y := y0; y < y1; y++ {
|
for y := y0; y < y1; y++ {
|
||||||
c = c.add(self.Image.At(x+self.Image.Bounds().Min.X, y+self.Image.Bounds().Min.Y))
|
c = c.add(
|
||||||
|
self.Image.At(
|
||||||
|
x+self.Image.Bounds().Min.X,
|
||||||
|
y+self.Image.Bounds().Min.Y,
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
|
Loading…
Reference in New Issue
Block a user