From ea02edb8d9c87c5c62535bbc59d84bbafead03a2 Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Sun, 4 Nov 2018 00:01:45 +0800 Subject: [PATCH] Refactor rename the PixelASCII to CharPixel --- ascii/ascii.go | 12 ++++++------ convert/convert.go | 18 +++++++++--------- convert/convert_test.go | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ascii/ascii.go b/ascii/ascii.go index 7011738..7d6c9f5 100644 --- a/ascii/ascii.go +++ b/ascii/ascii.go @@ -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), diff --git a/convert/convert.go b/convert/convert.go index b1d05a1..31fe3ff 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -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 diff --git a/convert/convert_test.go b/convert/convert_test.go index b855caa..9951caa 100644 --- a/convert/convert_test.go +++ b/convert/convert_test.go @@ -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) -- 2.45.2