Documentation
¶
Overview ¶
Package ascimage converts raster images into terminal cells, a Unicode quadrant glyph fitted to each cell's four samples or one background color per sample, and reads and writes them as one file holding true color and xterm-256 cells at once.
Index ¶
- Constants
- Variables
- func CalculateOutputSize(srcW, srcH, targetWidth int, aspect float64) (outW, outH int)
- func ConvertRect(dst []terminal.Cell, img image.Image, gridW, gridH int, r image.Rectangle, ...) []terminal.Cell
- func FitQuadrant(pixels [4]lcolor.RGB) (rune, lcolor.RGB, lcolor.RGB)
- func Gzipped(head []byte) bool
- func SaveDualMode(path string, img *DualModeImage) error
- func WriteANSI(w io.Writer, img *ConvertedImage) error
- func WriteDualMode(w io.Writer, img *DualModeImage) error
- type ConvertedImage
- type DualCell
- type DualModeImage
- type RenderMode
Constants ¶
const ( Extension = ".aimg" ExtensionGzip = ".aimgz" )
Extension names a plain .aimg and ExtensionGzip one gzipped whole, the forms doc/format.md specifies; a reader tells them apart by their bytes
const ( MaxSide = 1 << 14 MaxCells = 1 << 22 )
MaxSide and MaxCells bound a grid: ReadDualMode refuses a header past them before it allocates, and WriteDualMode never writes a file it would refuse
const DefaultAspect = 0.5
DefaultAspect is a terminal cell's width over its height
Variables ¶
var QuadrantChars = [16]rune{
' ',
'▘',
'▝',
'▀',
'▖',
'▌',
'▞',
'▛',
'▗',
'▚',
'▐',
'▜',
'▄',
'▙',
'▟',
'█',
}
QuadrantChars maps 4-bit patterns to Unicode quadrant characters Bit order: 0=UL, 1=UR, 2=LL, 3=LR (1 = foreground)
Functions ¶
func CalculateOutputSize ¶
CalculateOutputSize is the cell grid an image fills at a width, at least one row
func ConvertRect ¶
func ConvertRect(dst []terminal.Cell, img image.Image, gridW, gridH int, r image.Rectangle, mode RenderMode, colorMode terminal.ColorMode) []terminal.Cell
ConvertRect converts the cells r covers of img's grid of gridW by gridH cells into dst, row by row, growing it as it must: the cells ConvertImage gives at that size, so a viewer converts only what it shows. A cell of r outside the grid is transparent.
func FitQuadrant ¶
FitQuadrant returns the quadrant glyph and its two colors that show a cell's four quadrants, UL UR LL LR, with the least squared error, each color the mean of its side. A split and its complement fit alike and the first of equals wins, so only the eight leaving the lower right to the background are tried.
func SaveDualMode ¶
func SaveDualMode(path string, img *DualModeImage) error
SaveDualMode writes img to path, gzipped at gzip's default level when the path ends in ExtensionGzip; a failed write removes the file it began
func WriteANSI ¶
func WriteANSI(w io.Writer, img *ConvertedImage) error
WriteANSI writes img as lines of SGR-colored text, each cell's colors as its attributes say (true color, or the palette, its first 16 as the ANSI codes), a transparent cell in the terminal's own colors, every line reset at its end
func WriteDualMode ¶
func WriteDualMode(w io.Writer, img *DualModeImage) error
WriteDualMode writes img as a plain .aimg, the text header and then its cells
Types ¶
type ConvertedImage ¶
ConvertedImage holds an image's cells in one color mode; Rune 0 marks a transparent cell, which shows what lies under the image
func ConvertImage ¶
func ConvertImage(img image.Image, targetWidth int, mode RenderMode, colorMode terminal.ColorMode, aspect float64) *ConvertedImage
ConvertImage converts an image to the cells CalculateOutputSize gives at a width and aspect, in one color mode
type DualCell ¶
type DualCell struct {
Rune rune
TrueFg lcolor.RGB
TrueBg lcolor.RGB
Palette256Fg uint8
Palette256Bg uint8
Transparent bool
}
DualCell stores both color mode representations for one cell
type DualModeImage ¶
type DualModeImage struct {
Width int
Height int
RenderMode RenderMode
AnchorX int
AnchorY int
Cells []DualCell
}
DualModeImage holds both TrueColor and 256-color representations
func ConvertImageDual ¶
func ConvertImageDual(img image.Image, targetWidth int, mode RenderMode, aspect float64) *DualModeImage
ConvertImageDual converts an image to the cells CalculateOutputSize gives at a width and aspect, in both color modes in one pass
func LoadDualMode ¶
func LoadDualMode(path string) (*DualModeImage, error)
LoadDualMode reads the .aimg at path, plain or gzipped
func ReadDualMode ¶
func ReadDualMode(r io.Reader) (*DualModeImage, error)
ReadDualMode reads an .aimg, plain or gzipped, as its bytes say whatever its name, refusing all that version 1 does not define: another magic or version, a header past its limits or missing a key, cells that hold no rune or that are fewer or more than it names, a gzip stream that fails
func (*DualModeImage) ToConvertedImage ¶
func (d *DualModeImage) ToConvertedImage(colorMode terminal.ColorMode) *ConvertedImage
ToConvertedImage extracts single-mode ConvertedImage from dual representation
type RenderMode ¶
type RenderMode uint8
RenderMode determines the rendering approach
const ( ModeBackgroundOnly RenderMode = iota ModeQuadrant )
func (RenderMode) String ¶
func (m RenderMode) String() string
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
ascimage
command
Command ascimage turns images into terminal cells: it writes them as .aimg files or ANSI text, prints and describes them, and browses and views them full screen, converting rasters live at the size shown.
|
Command ascimage turns images into terminal cells: it writes them as .aimg files or ANSI text, prints and describes them, and browses and views them full screen, converting rasters live at the size shown. |