~dricottone/image2ascii

ref: 6ee682ae1c41bfd64968d71334d72b52c8d29ca3 image2ascii/convert/convert.go -rw-r--r-- 5.2 KiB
6ee682ae — qeesung add parallel convert 6 years ago
                                                                                
c3affd9f qeesung
1021c573 qeesung
bc120092 qeesung
1021c573 qeesung
6ee682ae qeesung
fc681bf0 秦世成
a482eea7 qeesung
fc681bf0 秦世成
a482eea7 qeesung
bc120092 qeesung
1021c573 qeesung
6ee682ae qeesung
49547468 qeesung
1021c573 qeesung
c6c0ddbf qeesung
49547468 qeesung
1021c573 qeesung
fc681bf0 秦世成
e6d60b68 qeesung
a482eea7 qeesung
49547468 qeesung
c5de7795 qeesung
1021c573 qeesung
c5de7795 qeesung
1021c573 qeesung
6ee682ae qeesung
1021c573 qeesung
c5de7795 qeesung
1021c573 qeesung
6ee682ae qeesung
49547468 qeesung
1021c573 qeesung
bc120092 qeesung
6ee682ae qeesung
5faf6387 qeesung
6ee682ae qeesung
49547468 qeesung
bc120092 qeesung
a482eea7 qeesung
6ee682ae qeesung
a482eea7 qeesung
bc120092 qeesung
5faf6387 qeesung
bc120092 qeesung
a482eea7 qeesung
bc120092 qeesung
a482eea7 qeesung
bc120092 qeesung
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Package convert can convert a image to ascii string or matrix
package convert

import (
	"bytes"
	"github.com/qeesung/image2ascii/ascii"
	"image"
	"image/color"
	"runtime"
	// Support decode jpeg image
	_ "image/jpeg"
	// Support deocde the png image
	_ "image/png"
	"log"
	"os"
)

type Pixel struct {
	i              int
	j              int
	color          color.Color
	convertedChars string
}

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

// DefaultOptions for convert image
var DefaultOptions = Options{
	Ratio:          1,
	ExpectedWidth:  -1,
	ExpectedHeight: -1,
	FitScreen:      true,
	Colored:        true,
}

// Image2ASCIIMatrix converts a image to ASCII matrix
func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string {
	// Resize the convert first
	newImage := ScaleImage(image, imageConvertOptions)
	sz := newImage.Bounds()
	newWidth := sz.Max.X
	newHeight := sz.Max.Y
	rawCharValues := make([]string, 0, int(newWidth*newHeight+newHeight))
	for i := 0; i < int(newHeight); i++ {
		for j := 0; j < int(newWidth); j++ {
			pixel := color.NRGBAModel.Convert(newImage.At(j, i))
			// Convert the pixel to ascii char
			pixelConvertOptions := ascii.NewOptions()
			pixelConvertOptions.Colored = imageConvertOptions.Colored
			rawChar := ascii.ConvertPixelToASCII(pixel, &pixelConvertOptions)
			rawCharValues = append(rawCharValues, rawChar)
		}
		rawCharValues = append(rawCharValues, "\n")
	}
	return rawCharValues
}

func Image2ASCIIMatrixParallel(image image.Image, imageConvertOptions *Options) []string {
	// Resize the convert first
	newImage := ScaleImage(image, imageConvertOptions)
	sz := newImage.Bounds()
	newWidth := sz.Max.X
	newHeight := sz.Max.Y
	cap := int(newWidth*newHeight + newHeight)
	rawCharValues := make([]string, cap, cap)

	// set the new line string
	for i := 0; i < newHeight; i++ {
		newLineIndex := (i+1)*(newWidth+1) - 1
		rawCharValues[newLineIndex] = "\n"
	}

	pixelChannel := make(chan Pixel, newWidth)
	charChannel := make(chan Pixel, newWidth)
	defer close(pixelChannel)
	defer close(charChannel)

	// schedule the convert task
	go func() {
		for i := 0; i < int(newHeight); i++ {
			for j := 0; j < int(newWidth); j++ {
				pixelColor := color.NRGBAModel.Convert(newImage.At(j, i))
				pixelChannel <- Pixel{
					i:              i,
					j:              j,
					color:          pixelColor,
					convertedChars: "",
				}
			}
		}
	}()

	// convert the pixel
	pixelConvertOptions := ascii.NewOptions()
	pixelConvertOptions.Colored = imageConvertOptions.Colored
	coreCount := runtime.NumCPU()
	for i := 0; i < coreCount; i++ {
		go func() {
			for pixel := range pixelChannel {
				rawChar := ascii.ConvertPixelToASCII(pixel.color, &pixelConvertOptions)
				pixel.convertedChars = rawChar
				charChannel <- pixel
			}
		}()
	}

	// get the results
	for k := 0; k < int(newWidth)*int(newHeight); k++ {
		convertedPixel := <-charChannel
		i, j := convertedPixel.i, convertedPixel.j
		index := i*int(newWidth+1) + j
		rawCharValues[index] = convertedPixel.convertedChars
	}
	return rawCharValues
}

// 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

	for i := 0; i < len(convertedPixelASCII); i++ {
		buffer.WriteString(convertedPixelASCII[i])
	}
	return buffer.String()
}

func Image2ASCIIStringParallel(image image.Image, options *Options) string {
	convertedPixelASCII := Image2ASCIIMatrixParallel(image, options)
	var buffer bytes.Buffer

	for i := 0; i < len(convertedPixelASCII); i++ {
		buffer.WriteString(convertedPixelASCII[i])
	}
	return buffer.String()
}

// ImageFile2ASCIIMatrix converts a image file to ascii matrix
func ImageFile2ASCIIMatrix(imageFilename string, option *Options) []string {
	img, err := OpenImageFile(imageFilename)
	if err != nil {
		log.Fatal("open image failed : " + err.Error())
	}
	return Image2ASCIIMatrix(img, option)
}

// ImageFile2ASCIIMatrix converts a image file to ascii matrix
func ImageFile2ASCIIMatrixParallel(imageFilename string, option *Options) []string {
	img, err := OpenImageFile(imageFilename)
	if err != nil {
		log.Fatal("open image failed : " + err.Error())
	}
	return Image2ASCIIMatrixParallel(img, option)
}

// ImageFile2ASCIIString converts a image file to ascii string
func ImageFile2ASCIIString(imageFilename string, option *Options) string {
	img, err := OpenImageFile(imageFilename)
	if err != nil {
		log.Fatal("open image failed : " + err.Error())
	}
	return Image2ASCIIString(img, option)
}

// ImageFile2ASCIIString converts a image file to ascii string
func ImageFile2ASCIIStringParallel(imageFilename string, option *Options) string {
	img, err := OpenImageFile(imageFilename)
	if err != nil {
		log.Fatal("open image failed : " + err.Error())
	}
	return Image2ASCIIStringParallel(img, option)
}

// OpenImageFile open a image and return a image object
func OpenImageFile(imageFilename string) (image.Image, error) {
	f, err := os.Open(imageFilename)
	if err != nil {
		return nil, err
	}

	img, _, err := image.Decode(f)
	if err != nil {
		return nil, err
	}

	defer f.Close()
	return img, nil
}