|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "image" |
| 7 | + "image/color" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "sync/atomic" |
| 12 | + |
| 13 | + tea "github.com/charmbracelet/bubbletea/v2" |
| 14 | + "github.com/charmbracelet/x/ansi" |
| 15 | + "github.com/charmbracelet/x/ansi/kitty" |
| 16 | + "github.com/charmbracelet/x/input" |
| 17 | +) |
| 18 | + |
| 19 | +// number is a global number used to generate unique image numbers. |
| 20 | +var number int32 |
| 21 | + |
| 22 | +// nextImageNumber returns the next unique image number. |
| 23 | +func nextNumber() int32 { |
| 24 | + return atomic.AddInt32(&number, 1) |
| 25 | +} |
| 26 | + |
| 27 | +// Protocol is the terminal graphics protocol used to render the image. |
| 28 | +type Protocol byte |
| 29 | + |
| 30 | +// Graphic protocol constants. |
| 31 | +const ( |
| 32 | + HalfBlocks Protocol = iota + 1 |
| 33 | + Sixel |
| 34 | + ITerm2 |
| 35 | + Kitty |
| 36 | +) |
| 37 | + |
| 38 | +// Model represents a terminal graphics image. |
| 39 | +type Model struct { |
| 40 | + // The protocol used |
| 41 | + Protocol Protocol |
| 42 | + // The area covering the image in cells |
| 43 | + area image.Rectangle |
| 44 | + // The image data (exclusive with file) |
| 45 | + m image.Image |
| 46 | + // The image file path (exclusive with m) |
| 47 | + file string |
| 48 | + |
| 49 | + // The image options |
| 50 | + opts kitty.Options |
| 51 | + |
| 52 | + // The image unique id. A non-zero indicates the image was transmitted successfully. |
| 53 | + id int |
| 54 | + // The image number |
| 55 | + num int |
| 56 | + |
| 57 | + // Whether the image was transmitted |
| 58 | + didTransmit bool |
| 59 | + |
| 60 | + // The terminal width and height |
| 61 | + w, h int |
| 62 | +} |
| 63 | + |
| 64 | +func newModel(area image.Rectangle) (m Model) { |
| 65 | + // We always use virtual placement for images |
| 66 | + m.opts.VirtualPlacement = true |
| 67 | + // Always chunk the image |
| 68 | + m.opts.Chunk = true |
| 69 | + // Transmit and put/display the image |
| 70 | + m.opts.Action = kitty.TransmitAndPut |
| 71 | + |
| 72 | + num := int(nextNumber()) |
| 73 | + m.num = num |
| 74 | + m.opts.Number = num |
| 75 | + |
| 76 | + m.SetArea(area) |
| 77 | + |
| 78 | + return |
| 79 | +} |
| 80 | + |
| 81 | +// NewLocal creates a new image model from a local file. |
| 82 | +func NewLocal(file string, area image.Rectangle) (m Model, err error) { |
| 83 | + m = newModel(area) |
| 84 | + m.file = file |
| 85 | + m.area = area |
| 86 | + // TODO: Fix me! This currently doesn't work with PNG |
| 87 | + // ext := filepath.Ext(file) |
| 88 | + // if strings.Contains(ext, "png") { |
| 89 | + // // We're done here, there's no need to decode the image. |
| 90 | + // m.opts.Format = kitty.PNG |
| 91 | + // m.opts.File = file |
| 92 | + // return |
| 93 | + // } |
| 94 | + |
| 95 | + f, err := os.Open(file) |
| 96 | + if err != nil { |
| 97 | + return m, fmt.Errorf("could not open image file: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + defer f.Close() //nolint:errcheck |
| 101 | + |
| 102 | + im, mtyp, err := image.Decode(f) |
| 103 | + if err != nil { |
| 104 | + return m, fmt.Errorf("could not decode image: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + m.m = im |
| 108 | + |
| 109 | + // Use a temporary file to store the image data |
| 110 | + m.opts.Transmission = kitty.TempFile |
| 111 | + |
| 112 | + // TODO: Enable compression |
| 113 | + // m.opts.Compression = kitty.Zlib |
| 114 | + |
| 115 | + // Set the image size |
| 116 | + bounds := im.Bounds() |
| 117 | + m.opts.ImageWidth = bounds.Dx() |
| 118 | + m.opts.ImageHeight = bounds.Dy() |
| 119 | + |
| 120 | + // Optimize for JPEG images and alpha transparency |
| 121 | + switch mtyp { |
| 122 | + case "jpeg": |
| 123 | + m.opts.Format = kitty.RGB |
| 124 | + default: |
| 125 | + m.opts.Format = kitty.RGBA |
| 126 | + } |
| 127 | + |
| 128 | + return |
| 129 | +} |
| 130 | + |
| 131 | +// New creates a new image model given an image and an area in cells |
| 132 | +func New(im image.Image, area image.Rectangle) Model { |
| 133 | + m := newModel(area) |
| 134 | + m.opts.Transmission = kitty.Direct |
| 135 | + // m.opts.Compression = kitty.Zlib |
| 136 | + m.opts.Format = kitty.RGBA |
| 137 | + m.m = im |
| 138 | + // Set the image size |
| 139 | + bounds := im.Bounds() |
| 140 | + m.opts.ImageWidth = bounds.Dx() |
| 141 | + m.opts.ImageHeight = bounds.Dy() |
| 142 | + m.SetArea(area) |
| 143 | + return m |
| 144 | +} |
| 145 | + |
| 146 | +// ID returns the image id unique with respect to the terminal. |
| 147 | +func (m Model) ID() int { |
| 148 | + return m.id |
| 149 | +} |
| 150 | + |
| 151 | +// Number returns the image number unique with respect to the library. |
| 152 | +func (m Model) Number() int { |
| 153 | + return m.num |
| 154 | +} |
| 155 | + |
| 156 | +// SetArea sets the image area in cells. |
| 157 | +func (m *Model) SetArea(area image.Rectangle) { |
| 158 | + m.area = area |
| 159 | + m.opts.Columns = m.area.Dx() |
| 160 | + m.opts.Rows = m.area.Dy() |
| 161 | + m.didTransmit = false |
| 162 | +} |
| 163 | + |
| 164 | +// Area returns the image area in cells. |
| 165 | +func (m Model) Area() image.Rectangle { |
| 166 | + return m.area |
| 167 | +} |
| 168 | + |
| 169 | +// transmit is a command that transmits the image to the terminal. |
| 170 | +func (m *Model) transmit() tea.Msg { |
| 171 | + var seq bytes.Buffer |
| 172 | + if err := ansi.WriteKittyGraphics(&seq, m.m, &m.opts); err != nil { |
| 173 | + // TODO: Error handling |
| 174 | + return nil |
| 175 | + } |
| 176 | + |
| 177 | + m.didTransmit = true |
| 178 | + return tea.RawMsg{Msg: seq.String()} |
| 179 | +} |
| 180 | + |
| 181 | +// Init initializes the image model. |
| 182 | +func (m Model) Init() (tea.Model, tea.Cmd) { |
| 183 | + return m, tea.Batch( |
| 184 | + // TODO: Query support |
| 185 | + ) |
| 186 | +} |
| 187 | + |
| 188 | +// Update updates the image model. |
| 189 | +func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 190 | + var cmds []tea.Cmd |
| 191 | + switch msg := msg.(type) { |
| 192 | + case tea.KeyMsg: |
| 193 | + m.didTransmit = false |
| 194 | + case tea.WindowSizeMsg: |
| 195 | + m.w, m.h = msg.Width, msg.Height |
| 196 | + case input.KittyGraphicsEvent: |
| 197 | + if msg.Options.Number == m.num && |
| 198 | + msg.Options.ID > 0 && |
| 199 | + bytes.Equal(msg.Payload, []byte("OK")) { |
| 200 | + // Store the actual image id |
| 201 | + m.id = msg.Options.ID |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if !m.didTransmit { |
| 206 | + cmds = append(cmds, m.transmit) |
| 207 | + m.didTransmit = true |
| 208 | + } |
| 209 | + |
| 210 | + return m, tea.Batch(cmds...) |
| 211 | +} |
| 212 | + |
| 213 | +// View returns a string representation to render the image. |
| 214 | +func (m Model) View() string { |
| 215 | + if m.id == 0 { |
| 216 | + // TODO: Maybe use a spinner? |
| 217 | + return "Loading image..." |
| 218 | + } |
| 219 | + |
| 220 | + log.Printf("area: %v", m.area) |
| 221 | + |
| 222 | + // Build Kitty graphics unicode place holders |
| 223 | + var fgSeq string |
| 224 | + var extra int |
| 225 | + var r, g, b int |
| 226 | + extra, r, g, b = m.id>>24&0xff, m.id>>16&0xff, m.id>>8&0xff, m.id&0xff |
| 227 | + |
| 228 | + if r == 0 && g == 0 { |
| 229 | + fgSeq = ansi.Style{}.ForegroundColor(ansi.ExtendedColor(b)).String() //nolint:gosec |
| 230 | + } else { |
| 231 | + fgSeq = ansi.Style{}.ForegroundColor(color.RGBA{ |
| 232 | + R: uint8(r), //nolint:gosec |
| 233 | + G: uint8(g), //nolint:gosec |
| 234 | + B: uint8(b), //nolint:gosec |
| 235 | + A: 0xff, |
| 236 | + }).String() |
| 237 | + } |
| 238 | + |
| 239 | + var s strings.Builder |
| 240 | + width := min(m.area.Dx(), m.w) |
| 241 | + height := min(m.area.Dy(), m.h) |
| 242 | + s.WriteString(ansi.ResetStyle) |
| 243 | + |
| 244 | + for y := 0; y < height; y++ { |
| 245 | + // As an optimization, we only write the fg color sequence id, and |
| 246 | + // column-row data once on the first cell. The terminal will handle |
| 247 | + // the rest. |
| 248 | + s.WriteString(fgSeq) |
| 249 | + s.WriteRune(kitty.Placeholder) |
| 250 | + s.WriteRune(kitty.Diacritic(y)) |
| 251 | + s.WriteRune(kitty.Diacritic(0)) |
| 252 | + if extra > 0 { |
| 253 | + s.WriteRune(kitty.Diacritic(extra)) |
| 254 | + } |
| 255 | + |
| 256 | + for x := 1; x < width; x++ { |
| 257 | + s.WriteRune(kitty.Placeholder) |
| 258 | + } |
| 259 | + |
| 260 | + s.WriteString(ansi.ResetStyle) |
| 261 | + if y != m.area.Dy()-1 { |
| 262 | + s.WriteByte('\n') |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + return s.String() |
| 267 | +} |
| 268 | + |
| 269 | +// Rect returns a rectangle from the given x, y, width, and height. |
| 270 | +func Rect(x, y, w, h int) image.Rectangle { |
| 271 | + return image.Rect(x, y, x+w, y+h) |
| 272 | +} |
| 273 | + |
| 274 | +func min(a, b int) int { |
| 275 | + if a < b { |
| 276 | + return a |
| 277 | + } |
| 278 | + return b |
| 279 | +} |
0 commit comments