~dricottone/image2ascii

8ba26814b21f49a6e1df6ae9475697a5bcd75d07 — qeesung 6 years ago 6bbfcf8
fixed the lint issue
M ascii/ascii.go => ascii/ascii.go +3 -0
@@ 38,14 38,17 @@ func (options *Options) mergeOptions(newOptions *Options) {
	options.Colored = newOptions.Colored
}

// NewPixelConverter create a new pixel converter
func NewPixelConverter() PixelConverter {
	return PixelASCIIConverter{}
}

// PixelConverter define the convert pixel operation
type PixelConverter interface {
	ConvertPixelToASCII(pixel color.Color, options *Options) string
}

// PixelASCIIConverter responsible for pixel ascii conversion
type PixelASCIIConverter struct {
}


M ascii/ascii_test.go => ascii/ascii_test.go +1 -1
@@ 115,7 115,7 @@ func TestReverseSlice(t *testing.T) {
}

// ExampleConvertPixelToASCII is a example convert pixel to ascii char
func ExampleConvertPixelToASCII() {
func ExamplePixelASCIIConverter_ConvertPixelToASCII() {
	converter := NewPixelConverter()
	// Create the pixel
	r, g, b, a := uint8(255), uint8(255), uint8(255), uint8(255)

M convert/convert.go => convert/convert.go +4 -0
@@ 36,6 36,7 @@ var DefaultOptions = Options{
	StretchedScreen: false,
}

// NewImageConverter create a new image converter
func NewImageConverter() *ImageConverter {
	return &ImageConverter{
		resizeHandler:  NewResizeHandler(),


@@ 43,6 44,7 @@ func NewImageConverter() *ImageConverter {
	}
}

// Converter define the convert image basic operations
type Converter interface {
	Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string
	Image2ASCIIString(image image.Image, options *Options) string


@@ 50,6 52,8 @@ type Converter interface {
	ImageFile2ASCIIString(imageFilename string, option *Options) string
}

// ImageConverter implement the Convert interface, and responsible
// to image conversion
type ImageConverter struct {
	resizeHandler  ResizeHandler
	pixelConverter ascii.PixelConverter

M convert/convert_test.go => convert/convert_test.go +1 -1
@@ 151,7 151,7 @@ func BenchmarkSmallImage2ASCIIMatrix(b *testing.B) {
}

// ExampleImage2ASCIIMatrix is example
func ExampleImage2ASCISString() {
func ExampleImageConverter_ImageFile2ASCIIString() {
	converter := NewImageConverter()
	imageFilename := "testdata/3x3_white.png"
	convertOptions := DefaultOptions

M convert/resize.go => convert/resize.go +6 -0
@@ 7,16 7,20 @@ import (
	"log"
)

// NewResizeHandler create a new resize handler
func NewResizeHandler() ResizeHandler {
	return &ImageResizeHandler{
		terminal: terminal.NewTerminalAccessor(),
	}
}

// ResizeHandler define the operation to resize a image
type ResizeHandler interface {
	ScaleImage(image image.Image, options *Options) (newImage image.Image)
}

// ImageResizeHandler implement the ResizeHandler interface and
// responsible for image resizing
type ImageResizeHandler struct {
	terminal terminal.Terminal
}


@@ 119,10 123,12 @@ func (handler *ImageResizeHandler) CalcFitSize(width, height, toBeFitWidth, toBe
	return
}

// ScaleWidthByRatio scaled the width by ratio
func (handler *ImageResizeHandler) ScaleWidthByRatio(width float64, ratio float64) int {
	return int(width * ratio)
}

// ScaleHeightByRatio scaled the height by ratio
func (handler *ImageResizeHandler) ScaleHeightByRatio(height float64, ratio float64) int {
	return int(height * ratio * handler.terminal.CharWidth())
}

M convert/resize_test.go => convert/resize_test.go +1 -1
@@ 194,7 194,7 @@ func TestStretchTheTerminalScreenSize(t *testing.T) {
}

// ExampleScaleImage is scale image example
func ExampleScaleImage() {
func ExampleImageResizeHandler_ScaleImage() {
	handler := NewResizeHandler()
	imageFilePath := "testdata/cat_2000x1500.jpg"
	img, err := OpenImageFile(imageFilePath)

M terminal/terminal.go => terminal/terminal.go +6 -0
@@ 13,6 13,7 @@ const (
	charWidthOther   = 0.5
)

// NewTerminalAccessor create a new terminal accessor
func NewTerminalAccessor() Terminal {
	return Accessor{}
}


@@ 24,9 25,12 @@ type Terminal interface {
	IsWindows() bool
}

// Accessor implement the Terminal interface and
// fetch the terminal basic information
type Accessor struct {
}

// CharWidth get the terminal char width
func (accessor Accessor) CharWidth() float64 {
	if accessor.IsWindows() {
		return charWidthWindows


@@ 34,10 38,12 @@ func (accessor Accessor) CharWidth() float64 {
	return charWidthOther
}

// IsWindows check if current system is windows
func (accessor Accessor) IsWindows() bool {
	return runtime.GOOS == "windows"
}

// ScreenSize get the terminal screen size
func (accessor Accessor) ScreenSize() (newWidth, newHeight int, err error) {
	if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
		return 0, 0,