ascimage

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: BSD-3-Clause Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ascimage

ascimage turns images into terminal cells: a Unicode quadrant glyph and two colors fitted to each cell's four samples, or one background color a cell. It writes them as .aimg files, which hold every cell in true color and in the xterm-256 palette at once (format), or as ANSI text; prints them fitted to the terminal; and browses folders of images and views them full screen, converting rasters live at the size shown. It reads PNG, JPEG and GIF with Go's own decoders. It began as vif's wall-image tool, and vif's image walls are made from its files.

Install

Go as go.mod names it, cgo off:

  • go install github.com/lixenwraith/ascimage/cmd/ascimage@latest
  • or make release, a stripped bin/ascimage, then make install: the binary, the licence and these documents under PREFIX (/usr/local, or ~/.local for a user who cannot write /usr/local/bin); make uninstall takes them away.

Commands

ascimage [view flags] [dir | image]
ascimage <command> [flags] [args]
  • view [dir | image]: browse a folder, the working one when none is named, or view an image full screen with its folder behind it. It is what runs with no command, or with only flags, a folder or an image (PNG, JPEG, GIF, .aimg, .aimgz). -z fit or a percentage of 1:1 starts an image's zoom; -no-status starts without the status bar.
  • convert <input> <output>, convert <input>... <dir>: the output's extension names what is written: .aimg plain, .aimgz gzipped, any other name (- for stdout) ANSI text in -c's depth. Several inputs, or a folder as the output, land in it under their own names in the form -to names (aimg, aimgz, ans). -w is the width in cells (80); -ax and -ay an anchor the file carries for its user. An .aimg input keeps its cells: it writes as ANSI, or as an .aimg with new anchors or the other form.
  • print <input>...: ANSI text as large as fits the terminal, or -w cells wide; out of a terminal, 80. Several inputs print each under its name.
  • info <input>...: a raster's format and pixels from its header alone; an .aimg's form, cells, mode and anchor, read whole, so one info describes is one that opens; and the file's size.
  • help [command]: the usage, or a command's flags.

The flags they share:

  • -m quadrant|bg: a quadrant glyph and two colors a cell, or one background color a cell.
  • -c auto|true|256|16: the color depth; auto reads TERM and COLORTERM, and in view the terminal itself, NO_COLOR included, which a named depth overrides.
  • -aspect: a cell's width over its height on the display (0.5).

Flags come before paths. Every input is read by its bytes, whatever its name.

ascimage                                      # browse the working folder
ascimage photo.jpg                            # view it, its folder behind it
ascimage convert -w 140 photo.jpg wall.aimg
ascimage convert -w 140 photo.jpg wall.aimgz  # the same, gzipped
ascimage convert -c 256 wall.aimgz wall.ans
ascimage convert -to aimgz -w 60 a.png b.jpg thumbs/
ascimage print -w 60 photo.jpg | less -R
ascimage info wall.aimgz

The browser and the view

The browser lists a folder's subfolders and the images it opens, with a preview of the one under the cursor converted at the preview's size; Enter views it full screen. The layout follows the room it has: list and preview side by side from 64 columns, the preview under the list in a tall narrow pane, one of the two at a time in a small one. The view shows an image at fit or on a ladder of zooms from 5% to 800% of 1:1, converting only the cells on screen, and decodes the images either side of it ahead, so stepping through a folder does not wait. ? lists the keys of either.

  • Browser: j k move, l or Enter opens, h or Backspace goes up, gg G first and last, ^U ^D and PgUp PgDn by pages, / filters names (Esc clears), Space marks, a marks all, u clears, s sorts by name, size or time, . shows hidden files, ~ goes home, Tab swaps list and preview when only one fits. The wheel moves; a click selects and a second opens.
  • View: h j k l and the arrows pan, by 10 with H J K L or Shift; ^U ^D, PgUp PgDn by half and whole screens; g G top and bottom, Home End the left and right edges; + - zoom, 0 1:1, f fit; n Space ] the next image, p Backspace [ the previous; Esc back to the folder. The wheel pans, Ctrl and the wheel zoom about the pointer, a drag pans, the side buttons step.
  • Both: m quadrant or background cells, c color depth (true, 256, 16), b the backdrop under transparent cells (panel, black, white, checker), w writes the marked images or the one in hand (the view's at the width shown) as convert does, ^L redraws, q quits.

The library

The package ascimage, at the module root, has no state:

  • ConvertImage(img, width, mode, colorMode, aspect): the cells CalculateOutputSize gives at a width, in one color mode, as a *ConvertedImage; a cell with Rune 0 is transparent.
  • ConvertRect(dst, img, gridW, gridH, rect, mode, colorMode): the cells of one window of such a grid, written into dst and the same cell for cell as ConvertImage's, so a viewer converts only what it shows.
  • ConvertImageDual(img, width, mode, aspect): a *DualModeImage, each cell in true color and 256 colors, with transparency; ToConvertedImage(colorMode) picks one, 16 colors as the ANSI 16 nearest the true colors.
  • WriteDualMode(w, img) writes a plain .aimg, ReadDualMode(r) reads either form by its bytes, SaveDualMode(path, img) gzips when the path ends in .aimgz, LoadDualMode(path), Gzipped(head).
  • WriteANSI(w, img): SGR-colored lines, each cell as its attributes say.
  • FitQuadrant(pixels): the glyph and two colors that show four colors with the least squared error. DefaultAspect, MaxSide, MaxCells, Extension, ExtensionGzip, QuadrantChars.
package main

import (
	"image"
	_ "image/jpeg"
	"log"
	"os"

	"github.com/lixenwraith/ascimage"
	"github.com/lixenwraith/terminal"
)

func main() {
	f, err := os.Open("photo.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	d := ascimage.ConvertImageDual(img, 80, ascimage.ModeQuadrant, ascimage.DefaultAspect)
	if err := ascimage.SaveDualMode("photo.aimgz", d); err != nil {
		log.Fatal(err)
	}
	if err := ascimage.WriteANSI(os.Stdout, d.ToConvertedImage(terminal.ColorModeTrueColor)); err != nil {
		log.Fatal(err)
	}
}

Invariants

  • The package imports the standard library, lixenwraith/color and lixenwraith/terminal alone; the command adds the terminal module's tui and inline.
  • A file's form is read from its bytes, never its name.
  • A reader checks every limit a header names before it allocates the grid.
  • ConvertRect's cells are ConvertImage's at the same grid, and convert, the TUI's write and print go through the same conversion.

Gotchas

  • Each sample is the source pixel under its centre, so a photo shrunk many times aliases; doc/todo.md holds the filter that would cure it.
  • A cell whose four samples are all transparent is transparent; a partly transparent one fits its transparent samples as black.
  • A GIF shows its first frame.
  • The 256-color cells of an .aimg are the indices it stores, which a reader on 256 colors uses as they are.

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

View Source
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

View Source
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

View Source
const DefaultAspect = 0.5

DefaultAspect is a terminal cell's width over its height

Variables

View Source
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

func CalculateOutputSize(srcW, srcH, targetWidth int, aspect float64) (outW, outH int)

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

func FitQuadrant(pixels [4]lcolor.RGB) (rune, lcolor.RGB, lcolor.RGB)

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

func Gzipped

func Gzipped(head []byte) bool

Gzipped reports whether bytes that open a file are gzip's, an .aimgz's

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

type ConvertedImage struct {
	Cells  []terminal.Cell
	Width  int
	Height int
}

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

String returns human-readable mode name

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL