~dricottone/image2ascii

49547468910f360ba23ca6cb7da66e8ebecf9b7e — qeesung 6 years ago 79e00ff
Rewrite the go docs
4 files changed, 16 insertions(+), 14 deletions(-)

M ascii/ascii.go
M ascii/option.go
M convert/convert.go
M convert/resize.go
M ascii/ascii.go => ascii/ascii.go +3 -2
@@ 1,4 1,4 @@
// The ascii package can convert a image pixel to a raw char
// Package ascii can convert a image pixel to a raw char
// base on it's RGBA value, in another word, input a image pixel
// output a raw char ascii.
package ascii


@@ 10,7 10,7 @@ import (
	"reflect"
)

// Convert a pixel to a ASCII char string
// ConvertPixelToASCII converts a pixel to a ASCII char string
func ConvertPixelToASCII(pixel color.Color, options *Options) string {
	defaultOptions := NewOptions()
	defaultOptions.mergeOptions(options)


@@ 50,6 50,7 @@ func intensity(r, g, b, a uint64) uint64 {
	return (r + g + b) * a / 255
}

// decorateWithColor decorate the raw char with the color base on r,g,b value
func decorateWithColor(r, g, b uint64, rawChar byte) string {
	coloredChar := rgbterm.FgString(string([]byte{rawChar}), uint8(r), uint8(g), uint8(b))
	return coloredChar

M ascii/option.go => ascii/option.go +4 -4
@@ 1,25 1,25 @@
package ascii

// Convert options
// Options convert pixel to raw char
type Options struct {
	Pixels  []byte
	Reverse bool
	Colored bool
}

// Default options
// DefaultOptions that contains the default pixels
var DefaultOptions = Options{
	Pixels:  []byte(" .,:;i1tfLCG08@"),
	Reverse: false,
	Colored: true,
}

// Create a new options
// NewOptions create a new convert option
func NewOptions() Options {
	return DefaultOptions
}

// Merge options
// mergeOptions merge two options
func (options *Options) mergeOptions(newOptions *Options) {
	options.Pixels = append([]byte{}, newOptions.Pixels...)
	options.Reverse = newOptions.Reverse

M convert/convert.go => convert/convert.go +5 -5
@@ 10,15 10,16 @@ import (
	"os"
)

// Options to convert the image to ASCII
type Options struct {
	Ratio          float64
	ExpectedWidth  int
	ExpectedHeight int
	FitScreen      bool
	Colored bool
	Colored        bool
}

// Convert a image to ascii matrix
// Image2ASCIIMatrix converts a image to ASCII matrix
func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string {
	// Resize the convert first
	newImage := ScaleImage(image, imageConvertOptions)


@@ 40,8 41,7 @@ func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string
	return rawCharValues
}

// Convert a image to ascii matrix, then concat the matrix value
// to a long string for easy display
// Image2ASCIIString converts a image to ascii matrix, and the join the matrix to a string
func Image2ASCIIString(image image.Image, options *Options) string {
	convertedPixelASCII := Image2ASCIIMatrix(image, options)
	var buffer bytes.Buffer


@@ 52,7 52,7 @@ func Image2ASCIIString(image image.Image, options *Options) string {
	return buffer.String()
}

// Convert a image file to ascii string
// ImageFile2ASCIIString converts a image file to ascii string
func ImageFile2ASCIIString(imageFilename string, option *Options) string {
	f, err := os.Open(imageFilename)
	if err != nil {

M convert/resize.go => convert/resize.go +4 -3
@@ 10,7 10,7 @@ import (
	"runtime"
)

// Resize the convert to expected size base on the ratio
// ScaleImage resize the convert to expected size base on the convert options
func ScaleImage(image image.Image, options *Options) (newImage image.Image) {
	sz := image.Bounds()
	ratio := options.Ratio


@@ 46,7 46,7 @@ func ScaleImage(image image.Image, options *Options) (newImage image.Image) {
	return
}

// Get the terminal char width on different system
// charWidth get the terminal char width on different system
func charWidth() float64 {
	if isWindows() {
		return 0.714


@@ 54,11 54,12 @@ func charWidth() float64 {
	return 0.5
}

// Check if current system is windows
// isWindows check if current system is windows
func isWindows() bool {
	return runtime.GOOS == "windows"
}

// getFitScreenSize get the current terminal screen size
func getFitScreenSize() (newWidth, newHeight uint) {
	if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
		log.Fatal("Can not detect the terminal, please disable the '-s fitScreen' option")