~dricottone/image2ascii

ea02edb8d9c87c5c62535bbc59d84bbafead03a2 — qeesung 6 years ago fe3f9eb
Refactor rename the PixelASCII to CharPixel
3 files changed, 17 insertions(+), 17 deletions(-)

M ascii/ascii.go
M convert/convert.go
M convert/convert_test.go
M ascii/ascii.go => ascii/ascii.go +6 -6
@@ 10,8 10,8 @@ import (
	"reflect"
)

// PixelASCII is converted pixel ascii
type PixelASCII struct {
// CharPixel is converted pixel ascii
type CharPixel struct {
	Char byte
	R    uint8
	G    uint8


@@ 55,15 55,15 @@ func NewPixelConverter() PixelConverter {
// PixelConverter define the convert pixel operation
type PixelConverter interface {
	ConvertPixelToASCII(pixel color.Color, options *Options) string
	ConvertPixelToPixelASCII(pixel color.Color, options *Options) PixelASCII
	ConvertPixelToPixelASCII(pixel color.Color, options *Options) CharPixel
}

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

// ConvertPixelToPixelASCII convert a image pixel to PixelASCII
func (converter PixelASCIIConverter) ConvertPixelToPixelASCII(pixel color.Color, options *Options) PixelASCII {
// ConvertPixelToPixelASCII convert a image pixel to CharPixel
func (converter PixelASCIIConverter) ConvertPixelToPixelASCII(pixel color.Color, options *Options) CharPixel {
	convertOptions := NewOptions()
	convertOptions.mergeOptions(options)



@@ 80,7 80,7 @@ func (converter PixelASCIIConverter) ConvertPixelToPixelASCII(pixel color.Color,
	// Choose the char
	precision := float64(255 * 3 / (len(convertOptions.Pixels) - 1))
	rawChar := convertOptions.Pixels[converter.roundValue(float64(value)/precision)]
	return PixelASCII{
	return CharPixel{
		Char: rawChar,
		R:    uint8(r),
		G:    uint8(g),

M convert/convert.go => convert/convert.go +9 -9
@@ 50,8 50,8 @@ type Converter interface {
	Image2ASCIIString(image image.Image, options *Options) string
	ImageFile2ASCIIMatrix(imageFilename string, option *Options) []string
	ImageFile2ASCIIString(imageFilename string, option *Options) string
	Image2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.PixelASCII
	ImageFile2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.PixelASCII
	Image2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.CharPixel
	ImageFile2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.CharPixel
}

// ImageConverter implement the Convert interface, and responsible


@@ 61,15 61,15 @@ type ImageConverter struct {
	pixelConverter ascii.PixelConverter
}

// Image2PixelASCIIMatrix convert a image to a pixel ascii matrix
func (converter *ImageConverter) Image2PixelASCIIMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.PixelASCII {
// Image2CharPixelMatrix convert a image to a pixel ascii matrix
func (converter *ImageConverter) Image2CharPixelMatrix(image image.Image, imageConvertOptions *Options) [][]ascii.CharPixel {
	newImage := converter.resizeHandler.ScaleImage(image, imageConvertOptions)
	sz := newImage.Bounds()
	newWidth := sz.Max.X
	newHeight := sz.Max.Y
	pixelASCIIs := make([][]ascii.PixelASCII, 0, newHeight)
	pixelASCIIs := make([][]ascii.CharPixel, 0, newHeight)
	for i := 0; i < int(newHeight); i++ {
		line := make([]ascii.PixelASCII, 0, newWidth)
		line := make([]ascii.CharPixel, 0, newWidth)
		for j := 0; j < int(newWidth); j++ {
			pixel := color.NRGBAModel.Convert(newImage.At(j, i))
			// Convert the pixel to ascii char


@@ 84,13 84,13 @@ func (converter *ImageConverter) Image2PixelASCIIMatrix(image image.Image, image
	return pixelASCIIs
}

// ImageFile2PixelASCIIMatrix convert a image to a pixel ascii matrix
func (converter *ImageConverter) ImageFile2PixelASCIIMatrix(imageFilename string, imageConvertOptions *Options) [][]ascii.PixelASCII {
// ImageFile2CharPixelMatrix convert a image to a pixel ascii matrix
func (converter *ImageConverter) ImageFile2CharPixelMatrix(imageFilename string, imageConvertOptions *Options) [][]ascii.CharPixel {
	img, err := OpenImageFile(imageFilename)
	if err != nil {
		log.Fatal("open image failed : " + err.Error())
	}
	return converter.Image2PixelASCIIMatrix(img, imageConvertOptions)
	return converter.Image2CharPixelMatrix(img, imageConvertOptions)
}

// Image2ASCIIMatrix converts a image to ASCII matrix

M convert/convert_test.go => convert/convert_test.go +2 -2
@@ 96,7 96,7 @@ func TestImageFile2ASCIIString(t *testing.T) {
	}
}

func TestImageFile2PixelASCIIMatrix(t *testing.T) {
func TestImageFile2CharPixelMatrix(t *testing.T) {
	converter := NewImageConverter()
	imageTests := []struct {
		imageFilename string


@@ 114,7 114,7 @@ func TestImageFile2PixelASCIIMatrix(t *testing.T) {
			convertOptions.FitScreen = false
			convertOptions.Colored = false

			matrix := converter.ImageFile2PixelASCIIMatrix(tt.imageFilename, &convertOptions)
			matrix := converter.ImageFile2CharPixelMatrix(tt.imageFilename, &convertOptions)
			if len(matrix) != tt.height|| len(matrix[0]) != tt.width{
				t.Errorf("image %s convert expected to %+v, %+v, but get %+v",
					tt.imageFilename, tt.width, tt.height, matrix)