Scale image message based on the user's text area.
Using the "image" package from the standard library the images loaded by gomuks now scale to one third the size of the text area. The image data contains both hight and width while the scaling uses only width the option to scale by hight is possible if passed into `CalculateBuffer()`. `ansimage.NewScaledFromReader()` now takes the new variable based off the size of the buffers' width. This may resolve issue #92
This commit is contained in:
parent
ae3bb9c7a3
commit
3ebfb87da2
@ -19,6 +19,7 @@ package messages
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
@ -100,11 +101,13 @@ func (msg *ImageMessage) Path() string {
|
|||||||
|
|
||||||
// CalculateBuffer generates the internal buffer for this message that consists
|
// CalculateBuffer generates the internal buffer for this message that consists
|
||||||
// of the text of this message split into lines at most as wide as the width
|
// of the text of this message split into lines at most as wide as the width
|
||||||
// parameter.
|
// parameter. If the message width is larger than the width of the buffer
|
||||||
|
// the message is scaled to one third the buffer width.
|
||||||
func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
|
func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
|
||||||
if width < 2 {
|
if width < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.CalculateReplyBuffer(prefs, width)
|
msg.CalculateReplyBuffer(prefs, width)
|
||||||
|
|
||||||
if prefs.BareMessageView || prefs.DisableImages {
|
if prefs.BareMessageView || prefs.DisableImages {
|
||||||
@ -112,12 +115,21 @@ func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
image, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), 0, width, color.Black)
|
img, _, err := image.DecodeConfig(bytes.NewReader(msg.data))
|
||||||
|
if err != nil {
|
||||||
|
debug.Print("Image could not be decoded:", err)
|
||||||
|
}
|
||||||
|
imgWidth := img.Width
|
||||||
|
if img.Width > width {
|
||||||
|
imgWidth = width / 3
|
||||||
|
}
|
||||||
|
|
||||||
|
ansImage, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.data), 0, imgWidth, color.Black)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)}
|
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)}
|
||||||
debug.Print("Failed to display image:", err)
|
debug.Print("Failed to display image:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.buffer = image.Render()
|
msg.buffer = ansImage.Render()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user