| 1 |
// ___ _____ ____ |
| 2 |
// / _ \/ _/ |/_/ /____ ______ _ |
| 3 |
// / ___// /_> </ __/ -_) __/ ' \ |
| 4 |
// /_/ /___/_/|_|\__/\__/_/ /_/_/_/ |
| 5 |
// |
| 6 |
// Copyright 2017 Eliuk Blau |
| 7 |
// |
| 8 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 9 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 11 |
|
| 12 |
package ansimage |
| 13 |
|
| 14 |
import ( |
| 15 |
"errors" |
| 16 |
"image" |
| 17 |
"image/color" |
| 18 |
"image/draw" |
| 19 |
_ "image/gif" // initialize decoder |
| 20 |
_ "image/jpeg" // initialize decoder |
| 21 |
_ "image/png" // initialize decoder |
| 22 |
"io" |
| 23 |
"os" |
| 24 |
|
| 25 |
"github.com/disintegration/imaging" |
| 26 |
_ "golang.org/x/image/bmp" // initialize decoder |
| 27 |
_ "golang.org/x/image/tiff" // initialize decoder |
| 28 |
_ "golang.org/x/image/webp" // initialize decoder |
| 29 |
|
| •30 |
"github.com/gdamore/tcell/v2" |
| 31 |
|
| 32 |
"maunium.net/go/gomuks/debug" |
| 33 |
"maunium.net/go/gomuks/ui/messages/tstring" |
| 34 |
) |
| 35 |
|
| 36 |
var ( |
| 37 |
// ErrHeightNonMoT happens when ANSImage height is not a Multiple of Two value. |
| 38 |
ErrHeightNonMoT = errors.New("ANSImage: height must be a Multiple of Two value") |
| 39 |
|
| 40 |
// ErrInvalidBoundsMoT happens when ANSImage height or width are invalid values (Multiple of Two). |
| 41 |
ErrInvalidBoundsMoT = errors.New("ANSImage: height or width must be >=2") |
| 42 |
|
| 43 |
// ErrOutOfBounds happens when ANSI-pixel coordinates are out of ANSImage bounds. |
| 44 |
ErrOutOfBounds = errors.New("ANSImage: out of bounds") |
| 45 |
) |
| 46 |
|
| 47 |
// ANSIpixel represents a pixel of an ANSImage. |
| 48 |
type ANSIpixel struct { |
| 49 |
Brightness uint8 |
| 50 |
R, G, B uint8 |
| 51 |
upper bool |
| 52 |
source *ANSImage |
| 53 |
} |
| 54 |
|
| 55 |
// ANSImage represents an image encoded in ANSI escape codes. |
| 56 |
type ANSImage struct { |
| 57 |
h, w int |
| 58 |
maxprocs int |
| 59 |
bgR uint8 |
| 60 |
bgG uint8 |
| 61 |
bgB uint8 |
| 62 |
pixmap [][]*ANSIpixel |
| 63 |
} |
| 64 |
|
| 65 |
func (ai *ANSImage) Pixmap() [][]*ANSIpixel { |
| 66 |
return ai.pixmap |
| 67 |
} |
| 68 |
|
| 69 |
// Height gets total rows of ANSImage. |
| 70 |
func (ai *ANSImage) Height() int { |
| 71 |
return ai.h |
| 72 |
} |
| 73 |
|
| 74 |
// Width gets total columns of ANSImage. |
| 75 |
func (ai *ANSImage) Width() int { |
| 76 |
return ai.w |
| 77 |
} |
| 78 |
|
| 79 |
// SetMaxProcs sets the maximum number of parallel goroutines to render the ANSImage |
| 80 |
// (user should manually sets `runtime.GOMAXPROCS(max)` before to this change takes effect). |
| 81 |
func (ai *ANSImage) SetMaxProcs(max int) { |
| 82 |
ai.maxprocs = max |
| 83 |
} |
| 84 |
|
| 85 |
// GetMaxProcs gets the maximum number of parallels goroutines to render the ANSImage. |
| 86 |
func (ai *ANSImage) GetMaxProcs() int { |
| 87 |
return ai.maxprocs |
| 88 |
} |
| 89 |
|
| 90 |
// SetAt sets ANSI-pixel color (RBG) and brightness in coordinates (y,x). |
| 91 |
func (ai *ANSImage) SetAt(y, x int, r, g, b, brightness uint8) error { |
| 92 |
if y >= 0 && y < ai.h && x >= 0 && x < ai.w { |
| 93 |
ai.pixmap[y][x].R = r |
| 94 |
ai.pixmap[y][x].G = g |
| 95 |
ai.pixmap[y][x].B = b |
| 96 |
ai.pixmap[y][x].Brightness = brightness |
| 97 |
ai.pixmap[y][x].upper = y%2 == 0 |
| 98 |
return nil |
| 99 |
} |
| 100 |
return ErrOutOfBounds |
| 101 |
} |
| 102 |
|
| 103 |
// GetAt gets ANSI-pixel in coordinates (y,x). |
| 104 |
func (ai *ANSImage) GetAt(y, x int) (*ANSIpixel, error) { |
| 105 |
if y >= 0 && y < ai.h && x >= 0 && x < ai.w { |
| 106 |
return &ANSIpixel{ |
| 107 |
R: ai.pixmap[y][x].R, |
| 108 |
G: ai.pixmap[y][x].G, |
| 109 |
B: ai.pixmap[y][x].B, |
| 110 |
Brightness: ai.pixmap[y][x].Brightness, |
| 111 |
upper: ai.pixmap[y][x].upper, |
| 112 |
source: ai.pixmap[y][x].source, |
| 113 |
}, |
| 114 |
nil |
| 115 |
} |
| 116 |
return nil, ErrOutOfBounds |
| 117 |
} |
| 118 |
|
| 119 |
// Render returns the ANSI-compatible string form of ANSImage. |
| 120 |
// (Nice info for ANSI True Colour - https://gist.github.com/XVilka/8346728) |
| 121 |
func (ai *ANSImage) Render() []tstring.TString { |
| 122 |
type renderData struct { |
| 123 |
row int |
| 124 |
render tstring.TString |
| 125 |
} |
| 126 |
|
| 127 |
rows := make([]tstring.TString, ai.h/2) |
| 128 |
for y := 0; y < ai.h; y += ai.maxprocs { |
| 129 |
ch := make(chan renderData, ai.maxprocs) |
| 130 |
for n, row := 0, y; (n <= ai.maxprocs) && (2*row+1 < ai.h); n, row = n+1, y+n { |
| 131 |
go func(row, y int) { |
| 132 |
defer func() { |
| 133 |
err := recover() |
| 134 |
if err != nil { |
| 135 |
debug.Print("Panic rendering ANSImage:", err) |
| 136 |
ch <- renderData{row: row, render: tstring.NewColorTString("ERROR", tcell.ColorRed)} |
| 137 |
} |
| 138 |
}() |
| 139 |
str := make(tstring.TString, ai.w) |
| 140 |
for x := 0; x < ai.w; x++ { |
| 141 |
topPixel := ai.pixmap[y][x] |
| 142 |
topColor := tcell.NewRGBColor(int32(topPixel.R), int32(topPixel.G), int32(topPixel.B)) |
| 143 |
|
| 144 |
bottomPixel := ai.pixmap[y+1][x] |
| 145 |
bottomColor := tcell.NewRGBColor(int32(bottomPixel.R), int32(bottomPixel.G), int32(bottomPixel.B)) |
| 146 |
|
| 147 |
str[x] = tstring.Cell{ |
| 148 |
Char: 'â–„', |
| 149 |
Style: tcell.StyleDefault.Background(topColor).Foreground(bottomColor), |
| 150 |
} |
| 151 |
} |
| 152 |
ch <- renderData{row: row, render: str} |
| 153 |
}(row, 2*row) |
| 154 |
} |
| 155 |
for n, row := 0, y; (n <= ai.maxprocs) && (2*row+1 < ai.h); n, row = n+1, y+n { |
| 156 |
data := <-ch |
| 157 |
rows[data.row] = data.render |
| 158 |
} |
| 159 |
} |
| 160 |
return rows |
| 161 |
} |
| 162 |
|
| 163 |
// New creates a new empty ANSImage ready to draw on it. |
| 164 |
func New(h, w int, bg color.Color) (*ANSImage, error) { |
| 165 |
if h%2 != 0 { |
| 166 |
return nil, ErrHeightNonMoT |
| 167 |
} |
| 168 |
|
| 169 |
if h < 2 || w < 2 { |
| 170 |
return nil, ErrInvalidBoundsMoT |
| 171 |
} |
| 172 |
|
| 173 |
r, g, b, _ := bg.RGBA() |
| 174 |
ansimage := &ANSImage{ |
| 175 |
h: h, w: w, |
| 176 |
maxprocs: 1, |
| 177 |
bgR: uint8(r), |
| 178 |
bgG: uint8(g), |
| 179 |
bgB: uint8(b), |
| 180 |
pixmap: nil, |
| 181 |
} |
| 182 |
|
| 183 |
ansimage.pixmap = func() [][]*ANSIpixel { |
| 184 |
v := make([][]*ANSIpixel, h) |
| 185 |
for y := 0; y < h; y++ { |
| 186 |
v[y] = make([]*ANSIpixel, w) |
| 187 |
for x := 0; x < w; x++ { |
| 188 |
v[y][x] = &ANSIpixel{ |
| 189 |
R: 0, |
| 190 |
G: 0, |
| 191 |
B: 0, |
| 192 |
Brightness: 0, |
| 193 |
source: ansimage, |
| 194 |
upper: y%2 == 0, |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
return v |
| 199 |
}() |
| 200 |
|
| 201 |
return ansimage, nil |
| 202 |
} |
| 203 |
|
| 204 |
// NewFromReader creates a new ANSImage from an io.Reader. |
| 205 |
// Background color is used to fill when image has transparency or dithering mode is enabled |
| 206 |
// Dithering mode is used to specify the way that ANSImage render ANSI-pixels (char/block elements). |
| 207 |
func NewFromReader(reader io.Reader, bg color.Color) (*ANSImage, error) { |
| 208 |
img, _, err := image.Decode(reader) |
| 209 |
if err != nil { |
| 210 |
return nil, err |
| 211 |
} |
| 212 |
|
| 213 |
return createANSImage(img, bg) |
| 214 |
} |
| 215 |
|
| 216 |
// NewScaledFromReader creates a new scaled ANSImage from an io.Reader. |
| 217 |
// Background color is used to fill when image has transparency or dithering mode is enabled |
| 218 |
// Dithering mode is used to specify the way that ANSImage render ANSI-pixels (char/block elements). |
| 219 |
func NewScaledFromReader(reader io.Reader, y, x int, bg color.Color) (*ANSImage, error) { |
| 220 |
img, _, err := image.Decode(reader) |
| 221 |
if err != nil { |
| 222 |
return nil, err |
| 223 |
} |
| 224 |
|
| 225 |
img = imaging.Resize(img, x, y, imaging.Lanczos) |
| 226 |
|
| 227 |
return createANSImage(img, bg) |
| 228 |
} |
| 229 |
|
| 230 |
// NewFromFile creates a new ANSImage from a file. |
| 231 |
// Background color is used to fill when image has transparency or dithering mode is enabled |
| 232 |
// Dithering mode is used to specify the way that ANSImage render ANSI-pixels (char/block elements). |
| 233 |
func NewFromFile(name string, bg color.Color) (*ANSImage, error) { |
| 234 |
reader, err := os.Open(name) |
| 235 |
if err != nil { |
| 236 |
return nil, err |
| 237 |
} |
| 238 |
defer reader.Close() |
| 239 |
return NewFromReader(reader, bg) |
| 240 |
} |
| 241 |
|
| 242 |
// NewScaledFromFile creates a new scaled ANSImage from a file. |
| 243 |
// Background color is used to fill when image has transparency or dithering mode is enabled |
| 244 |
// Dithering mode is used to specify the way that ANSImage render ANSI-pixels (char/block elements). |
| 245 |
func NewScaledFromFile(name string, y, x int, bg color.Color) (*ANSImage, error) { |
| 246 |
reader, err := os.Open(name) |
| 247 |
if err != nil { |
| 248 |
return nil, err |
| 249 |
} |
| 250 |
defer reader.Close() |
| 251 |
return NewScaledFromReader(reader, y, x, bg) |
| 252 |
} |
| 253 |
|
| 254 |
// createANSImage loads data from an image and returns an ANSImage. |
| 255 |
// Background color is used to fill when image has transparency or dithering mode is enabled |
| 256 |
// Dithering mode is used to specify the way that ANSImage render ANSI-pixels (char/block elements). |
| 257 |
func createANSImage(img image.Image, bg color.Color) (*ANSImage, error) { |
| 258 |
var rgbaOut *image.RGBA |
| 259 |
bounds := img.Bounds() |
| 260 |
|
| 261 |
// do compositing only if background color has no transparency (thank you @disq for the idea!) |
| 262 |
// (info - http://stackoverflow.com/questions/36595687/transparent-pixel-color-go-lang-image) |
| 263 |
if _, _, _, a := bg.RGBA(); a >= 0xffff { |
| 264 |
rgbaOut = image.NewRGBA(bounds) |
| 265 |
draw.Draw(rgbaOut, bounds, image.NewUniform(bg), image.ZP, draw.Src) |
| 266 |
draw.Draw(rgbaOut, bounds, img, image.ZP, draw.Over) |
| 267 |
} else { |
| 268 |
if v, ok := img.(*image.RGBA); ok { |
| 269 |
rgbaOut = v |
| 270 |
} else { |
| 271 |
rgbaOut = image.NewRGBA(bounds) |
| 272 |
draw.Draw(rgbaOut, bounds, img, image.ZP, draw.Src) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
yMin, xMin := bounds.Min.Y, bounds.Min.X |
| 277 |
yMax, xMax := bounds.Max.Y, bounds.Max.X |
| 278 |
|
| 279 |
// always sets an even number of ANSIPixel rows... |
| 280 |
yMax = yMax - yMax%2 // one for upper pixel and another for lower pixel --> without dithering |
| 281 |
|
| 282 |
ansimage, err := New(yMax, xMax, bg) |
| 283 |
if err != nil { |
| 284 |
return nil, err |
| 285 |
} |
| 286 |
|
| 287 |
for y := yMin; y < yMax; y++ { |
| 288 |
for x := xMin; x < xMax; x++ { |
| 289 |
v := rgbaOut.RGBAAt(x, y) |
| 290 |
if err := ansimage.SetAt(y, x, v.R, v.G, v.B, 0); err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return ansimage, nil |
| 297 |
} |