gomonochromebitmap

package module
v0.1.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 7 Imported by: 5

README

gomonochromebitmap

Golang library for monochrome image operations

I developed this library for my personal "retrocomputing"/emulator project. This library is only for black-white images.

At the moment some basic functions are implemented in very naive way. I just need this library for getting things running I will provide more optimized versions after I complete my other project using this library.

##example Please check further examples from gomonochromebitmap_test.go

Documentation

Overview

Package providing clipping on line

This is more "modern" font solution supporting embed directive (avail since go 1.16)

Fonts are generated beforehand and loaded with embed directive.

Font data is like this - font width uint16 - font height uint16

Then repeating part - flags, now 0, 1=RLE encoded - first 8bit char code - number of chars first,first+1,first+2 - byte w*h*n bits of data. Alligned to byte on each char

Rendering supports rotation

Run length coding on large fonts Option to scale down font when rendering (embed largest font, scale down for smaller fonts)

Some fonts for gomonochromebitmap

Package provides functions for operating monochrome images

Index

Constants

View Source
const (
	ANSI_NO    = ""
	ANSI_RESET = "\033[0m"
)
View Source
const (
	FGANSI_BLACK  = "\033[30m"
	FGANSI_RED    = "\033[31m"
	FGANSI_GREEN  = "\033[32m"
	FGANSI_YELLOW = "\033[33m"
	FGANSI_BLUE   = "\033[34m"
	FGANSI_PURPLE = "\033[35m"
	FGANSI_CYAN   = "\033[36m"
	FGANSI_WHITE  = "\033[37m"

	FGANSI_BRIGHT_BLACK  = "\033[30;1m"
	FGANSI_BRIGHT_RED    = "\033[31;1m"
	FGANSI_BRIGHT_GREEN  = "\033[32;1m"
	FGANSI_BRIGHT_YELLOW = "\033[33;1m"
	FGANSI_BRIGHT_BLUE   = "\033[34;1m"
	FGANSI_BRIGHT_PURPLE = "\033[35;1m"
	FGANSI_BRIGHT_CYAN   = "\033[36;1m"
	FGANSI_BRIGHT_WHITE  = "\033[37;1m"
)
View Source
const (
	BGANSI_BLACK  = "\033[40m"
	BGANSI_RED    = "\033[41m"
	BGANSI_GREEN  = "\033[42m"
	BGANSI_YELLOW = "\033[43m"
	BGANSI_BLUE   = "\033[44m"
	BGANSI_PURPLE = "\033[45m"
	BGANSI_CYAN   = "\033[46m"
	BGANSI_WHITE  = "\033[47m"

	BGANSI_BRIGHT_BLACK  = "\033[40;1m"
	BGANSI_BRIGHT_RED    = "\033[41;1m"
	BGANSI_BRIGHT_GREEN  = "\033[42;1m"
	BGANSI_BRIGHT_YELLOW = "\033[43;1m"
	BGANSI_BRIGHT_BLUE   = "\033[44;1m"
	BGANSI_BRIGHT_PURPLE = "\033[45;1m"
	BGANSI_BRIGHT_CYAN   = "\033[46;1m"
	BGANSI_BRIGHT_WHITE  = "\033[47;1m"
)
View Source
const (
	INSIDE = 0 // 0000
	LEFT   = 1 // 0001
	RIGHT  = 2 // 0010
	BOTTOM = 4 // 0100
	TOP    = 8 // 1000
)

Define region codes for Cohen-Sutherland algorithm

View Source
const (
	CLEARDISPLAY = "\033c"
)

Variables

This section is empty.

Functions

func ClipLine

func ClipLine(a, b image.Point, area image.Rectangle) (*image.Point, *image.Point)

ClipLine clips a line segment from a to b inside the rectangle area

func CreatePlanarColorImage

func CreatePlanarColorImage(planes []MonoBitmap, palette []color.Color) (*image.RGBA, error)

Use each monochrome bitmap as bit in color palette index. https://en.wikipedia.org/wiki/Planar_(computer_graphics)

Types

type AnsiColorString

type AnsiColorString string

type BlockGraphics

type BlockGraphics struct {
	Clear       bool
	HaveBorder  bool
	BorderColor AnsiColorString
	TextColor   AnsiColorString
}

func (*BlockGraphics) ToFullBlockChars

func (p *BlockGraphics) ToFullBlockChars(bitmap *MonoBitmap) string

ToFullBlockChars creates console printable version of image ' ','█'

func (*BlockGraphics) ToHalfBlockChars

func (p *BlockGraphics) ToHalfBlockChars(bitmap *MonoBitmap) string

ToHalfBlockChars ' ', '▀', '▄', '█'

func (*BlockGraphics) ToQuadBlockChars

func (p *BlockGraphics) ToQuadBlockChars(bitmap *MonoBitmap) string

ToQuadBlockChars ' ', '▘', '▝', '▀','▖', '▌', '▞', '▛','▗', '▚', '▐', '▜','▄', '▙', '▟', '█'

type Font

type Font []byte

Must be just []byte in type definition so this works

func (*Font) GetHeader

func (p *Font) GetHeader() (FontFileHeader, error)

func (*Font) GetRune

func (p *Font) GetRune(c rune) ([]byte, error)

type FontFileBlockHeader

type FontFileBlockHeader struct {
	FirstRune     rune // rune is 32bit
	NumberOfCodes byte //
}

type FontFileHeader

type FontFileHeader struct {
	Width  uint16
	Height uint16
}

type MonoBitmap

type MonoBitmap struct {
	Pix []uint32 //using byte vs uint16 vs uint32 vs uint64...  32bit shoud suit well for raspi1/2
	W   int
	H   int
}

func NewMonoBitmap

func NewMonoBitmap(w int, h int, fill bool) MonoBitmap

NewMonoBitmap initializes empty bitmap fill is default value

func NewMonoBitmapFromImage

func NewMonoBitmapFromImage(img image.Image, area image.Rectangle, threshold byte, invert bool) MonoBitmap

NewMonoBitmapFromImage initializes bitmap from image. Color conversion: if any Red,Green or Blue value is over threshold then pixel is true

func (*MonoBitmap) Bounds

func (p *MonoBitmap) Bounds() image.Rectangle

Bounds returns W,H in Rect struct

func (*MonoBitmap) Circle

func (p *MonoBitmap) Circle(p0 image.Point, r int, value bool)

Modified from C++ source https://en.wikipedia.org/wiki/Midpoint_circle_algorithm

func (*MonoBitmap) CircleFill

func (p *MonoBitmap) CircleFill(p0 image.Point, r int, value bool)

Modified from C++ source https://en.wikipedia.org/wiki/Midpoint_circle_algorithm

func (*MonoBitmap) DrawBitmap

func (p *MonoBitmap) DrawBitmap(source MonoBitmap, sourceArea image.Rectangle, targetCorner image.Point, drawTrue bool, drawFalse bool, invert bool)

Draws source bitmap on bitmap drawTrue, draw when point value is true drawFalse, draw when point value is true

func (*MonoBitmap) Fill

func (p *MonoBitmap) Fill(area image.Rectangle, fillValue bool)

Fills rectangle area from map. Used for clearing image

func (*MonoBitmap) FlipH

func (p *MonoBitmap) FlipH()

func (*MonoBitmap) FlipV

func (p *MonoBitmap) FlipV()

Flip with axis in vertical

func (*MonoBitmap) GetDisplayImage

func (p *MonoBitmap) GetDisplayImage(trueColorUpper color.Color, trueColorDowner color.Color, upperRows int, falseColor color.Color, pixelW int, pixelH int, gapW int, gapH int) *image.RGBA

Generates image that is rendered like it was LCD. Space in between segments is transparent upper vs lower color allows to render two color LCD's (like cyan and yellow strip)

func (*MonoBitmap) GetFgBgImage

func (p *MonoBitmap) GetFgBgImage(fgPic image.Image, bgPic image.Image) (image.Image, error)

func (*MonoBitmap) GetImage

func (p *MonoBitmap) GetImage(trueColor color.Color, falseColor color.Color) *image.RGBA

BenchmarkGetBlankImage-16 750 1588057 ns/op 1925196 B/op 4 allocs/op GetImage Creates RGBA image from bitmap

func (*MonoBitmap) GetPix

func (p *MonoBitmap) GetPix(x int, y int) bool

Gets pixel. Returns false if out of range

func (*MonoBitmap) GetPixNoCheck

func (p *MonoBitmap) GetPixNoCheck(x int, y int) bool

func (*MonoBitmap) GetView

func (p *MonoBitmap) GetView(w int, h int, p0 image.Point, pxStep int, edges bool) MonoBitmap

Get view (size w,h) for display. Starting from corner p0. Result is centered. If p0 goes outside, function clamps view This is meant only for producing scrollable output picture for display. Better scaling functions elsewhere pxStep=0, autoscale, so bitmap will fit pxStep=1 is 1:1 pxStep=2 is 2:1 (50% scale) pxStep=3 is 3:1 (25% scale) pxStep is limited to point where whole bitmap is visible Returns: image, actual cornerpoint and zoom used. Useful if UI includes

func (*MonoBitmap) Hline

func (p *MonoBitmap) Hline(x0 int, x1 int, y int, value bool)

func (*MonoBitmap) Hline_

func (p *MonoBitmap) Hline_(x0 int, x1 int, y int, value bool)

func (*MonoBitmap) Invert

func (p *MonoBitmap) Invert(area image.Rectangle)

Inverts pixel values

func (*MonoBitmap) Line

func (p *MonoBitmap) Line(p0In image.Point, p1In image.Point, value bool)

Bresenham's line, copied from http://41j.com/blog/2012/09/bresenhams-line-drawing-algorithm-implemetations-in-go-and-c/

func (*MonoBitmap) Print

func (p *MonoBitmap) Print(text string, font MonoFont, lineSpacing int, gap int, area image.Rectangle, drawTrue bool, drawFalse bool, invert bool, wrap bool) image.Rectangle

Prints message on screen.Creates new lines on \n Returns rectangle where text was printed

func (*MonoBitmap) RLEdecode

func (p *MonoBitmap) RLEdecode(activeFirst bool, data []byte) error

RLEdecode decodes run length compressed bitmap data

func (*MonoBitmap) RLEencode

func (p *MonoBitmap) RLEencode(activeFirst bool) []byte

RLEencodes bitmap in runlength compressed format

func (*MonoBitmap) Rectangle

func (p *MonoBitmap) Rectangle(area image.Rectangle)

func (*MonoBitmap) Rotate90

func (p *MonoBitmap) Rotate90(turn90 int)

Rotates in 90 decree steps +1=90 clockwise -1=90 anticlockwise +2=180 clockwise etc...

func (*MonoBitmap) SetPix

func (p *MonoBitmap) SetPix(x int, y int, value bool)

TODO BUG: does not work if not div by 8

func (*MonoBitmap) SetPixNoCheck

func (p *MonoBitmap) SetPixNoCheck(x int, y int, value bool)

func (*MonoBitmap) Vline

func (p *MonoBitmap) Vline(x int, y0 int, y1 int, value bool)

type MonoFont

type MonoFont map[rune]MonoBitmap //All bitmaps have same size

func GetFont_11x16

func GetFont_11x16() MonoFont

func GetFont_3x6

func GetFont_3x6() MonoFont

func GetFont_4x5

func GetFont_4x5() MonoFont

func GetFont_5x7

func GetFont_5x7() MonoFont

func GetFont_6x10

func GetFont_6x10() MonoFont

func GetFont_8x12

func GetFont_8x12() MonoFont

func GetFont_8x8

func GetFont_8x8() MonoFont

func (*MonoFont) AreaEstimated

func (p *MonoFont) AreaEstimated(s string, maxWidth int, lineSpacing int, gap int) image.Rectangle

func (*MonoFont) GetWH

func (p *MonoFont) GetWH() (int, int)

type Typesetter

type Typesetter struct {
	DrawTrue  bool
	DrawFalse bool

	Gap int

	Typeface *Font
}

On grayscale

Directories

Path Synopsis
This sub library is for simulating gadgets on desktop Supports keys and monochrome display.
This sub library is for simulating gadgets on desktop Supports keys and monochrome display.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳