~dricottone/image2ascii

ref: 1bc839449862f554fce7811d83c871c2665fd1f1 image2ascii/convert/convert_test.go -rw-r--r-- 4.6 KiB
1bc83944 — qeesung Fxied the fetched wrong image width and height issue 6 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package convert

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"reflect"
	"testing"
)

// TestOpenImageFile test open different type image file
func TestOpenImageFile(t *testing.T) {
	assertions := assert.New(t)
	jpgFilename := "testdata/jpg_sample_image.jpg"
	openedImage, err := OpenImageFile(jpgFilename)
	assertions.True(err == nil, "jpg image format should be supported")
	assertions.True(openedImage != nil, "opened jpg file should not be nil")

	pngFilename := "testdata/png_sample_image.png"
	openedImage, err = OpenImageFile(pngFilename)
	assertions.True(err == nil, "png image format should be supported")
	assertions.True(openedImage != nil, "opened jpg file should not be nil")

	notSupported := "testdata/not_supported_sample_image"
	openedImage, err = OpenImageFile(notSupported)
	assertions.True(err != nil, "should not open unsupported image")
	assertions.True(openedImage == nil, "not supported image should be nil")
}

// TestOpenNotExistsFile test open a not exists file
func TestOpenNotExistsFile(t *testing.T) {
	assertions := assert.New(t)
	_, err := OpenImageFile("not exists")
	assertions.True(err != nil)
}

// TestImage2ASCIIMatrix test convert a image to ascii matrix
func TestImage2ASCIIMatrix(t *testing.T) {
	imageTests := []struct {
		imageFilename string
		asciiMatrix   []string
	}{
		{"testdata/3x3_black.png", []string{
			" ", " ", " ", "\n",
			" ", " ", " ", "\n",
			" ", " ", " ", "\n"}},
		{"testdata/3x3_white.png", []string{
			"@", "@", "@", "\n",
			"@", "@", "@", "\n",
			"@", "@", "@", "\n"}},
		{"testdata/8x3_multi_colors.png", []string{
			"L", "0", "L", "t", "0", "t", "G", "0", "\n",
			"i", "L", "t", "1", "L", "1", "f", "L", "\n",
			"i", "f", "i", "i", ";", "L", ";", "t", "\n",
		}},
	}

	for _, tt := range imageTests {
		t.Run(tt.imageFilename, func(t *testing.T) {
			convertOptions := DefaultOptions
			convertOptions.FitScreen = false
			convertOptions.Colored = false

			matrix := ImageFile2ASCIIMatrix(tt.imageFilename, &convertOptions)
			if !reflect.DeepEqual(matrix, tt.asciiMatrix) {
				t.Errorf("image %s convert expected to %+v, but get %+v",
					tt.imageFilename, tt.asciiMatrix, matrix)
			}
		})
	}
}

func TestImageFile2ASCIIString(t *testing.T) {
	imageTests := []struct {
		imageFilename string
		asciiString   string
	}{
		{"testdata/3x3_black.png", "   \n   \n   \n"},
		{"testdata/3x3_white.png", "@@@\n@@@\n@@@\n"},
		{"testdata/8x3_multi_colors.png", "L0Lt0tG0\niLt1L1fL\nifii;L;t\n"},
	}

	for _, tt := range imageTests {
		t.Run(tt.imageFilename, func(t *testing.T) {
			convertOptions := DefaultOptions
			convertOptions.FitScreen = false
			convertOptions.Colored = false

			charString := ImageFile2ASCIIString(tt.imageFilename, &convertOptions)
			if charString != tt.asciiString {
				t.Errorf("image %s convert expected to %+v, but get %+v",
					tt.imageFilename, tt.asciiString, charString)
			}
		})
	}
}

func TestImage2ReversedASCIIString(t *testing.T) {
	imageTests := []struct {
		imageFilename string
		asciiString   string
	}{
		{"testdata/3x3_white.png", "   \n   \n   \n"},
		{"testdata/3x3_black.png", "@@@\n@@@\n@@@\n"},
	}

	for _, tt := range imageTests {
		t.Run(tt.imageFilename, func(t *testing.T) {
			convertOptions := DefaultOptions
			convertOptions.FitScreen = false
			convertOptions.Colored = false
			convertOptions.Reversed = true

			charString := ImageFile2ASCIIString(tt.imageFilename, &convertOptions)
			if charString != tt.asciiString {
				t.Errorf("image %s convert expected to %+v, but get %+v",
					tt.imageFilename, tt.asciiString, charString)
			}
		})
	}
}

// BenchmarkBigImage2ASCIIMatrix benchmark convert big image to ascii
func BenchmarkBigImage2ASCIIMatrix(b *testing.B) {
	convertOptions := DefaultOptions
	convertOptions.FitScreen = false
	convertOptions.Colored = false
	convertOptions.ExpectedWidth = 200
	convertOptions.ExpectedHeight = 200

	for i := 0; i < b.N; i++ {
		_ = ImageFile2ASCIIMatrix("testdata/cat_2000x1500.jpg", &convertOptions)
	}
}

// BenchmarkSmallImage2ASCIIMatrix benchmark convert small image to ascii
func BenchmarkSmallImage2ASCIIMatrix(b *testing.B) {
	convertOptions := DefaultOptions
	convertOptions.FitScreen = false
	convertOptions.Colored = false
	convertOptions.ExpectedWidth = 200
	convertOptions.ExpectedHeight = 200

	for i := 0; i < b.N; i++ {
		_ = ImageFile2ASCIIMatrix("testdata/husky_200x200.jpg", &convertOptions)
	}
}

// ExampleImage2ASCIIMatrix is example
func ExampleImage2ASCISString() {
	imageFilename := "testdata/3x3_white.png"
	convertOptions := DefaultOptions
	convertOptions.FitScreen = false
	convertOptions.Colored = false
	asciiString := ImageFile2ASCIIString(imageFilename, &convertOptions)
	fmt.Println(asciiString)
	/* Output:
@@@
@@@
@@@
	 */
}