diff options
author | Tulir Asokan <tulir@maunium.net> | 2019-06-16 15:21:01 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2019-06-16 15:21:01 +0300 |
commit | 242d3a00d984810b7f8466c11c2629bbf73d28c9 (patch) | |
tree | b89d766b04a741a50a90713acfa34ca2bce47ab9 /ui | |
parent | b019266062e3f886a40500a290c7a1a751d59f88 (diff) | |
parent | 3ebfb87da26a46ecfa3863e478d9272ab12260b4 (diff) |
Merge remote-tracking branch 'jrswab/imageResize'
Diffstat (limited to 'ui')
-rw-r--r-- | ui/messages/imagemessage.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go index 44d70e9..399c941 100644 --- a/ui/messages/imagemessage.go +++ b/ui/messages/imagemessage.go @@ -19,6 +19,7 @@ package messages import ( "bytes" "fmt" + "image" "image/color" "maunium.net/go/mautrix" @@ -103,7 +104,8 @@ func (msg *ImageMessage) Path() string { // 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 -// 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, uiMsg *UIMessage) { if width < 2 { return @@ -114,14 +116,23 @@ func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int 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 { msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)} debug.Print("Failed to display image:", err) return } - msg.buffer = image.Render() + msg.buffer = ansImage.Render() } func (msg *ImageMessage) Height() int { |