-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (62 loc) · 1.97 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"image/color"
_ "image/jpeg"
_ "image/png"
"log"
)
func main() {
// Supported input formats : jpg, jpeg, png
//Get the file name
// Define the flag for color printing
useColor := flag.Bool("color", false, "Print images in color")
flag.Parse()
// Get the remaining non-flag arguments (positional arguments)
args := flag.Args()
if len(args) < 1 {
log.Fatal("Please provide an image file name as the argument.")
}
fileName := args[0]
// Load the image data (pixels of image type)
imageData, err := loadImage(fileName)
if err != nil {
log.Fatal(err)
}
//Number of pixels on the X and Y axis
bound:= imageData.Bounds()
//Creating a new Image type with reduced width and heigth to fit the terminal
widthFactor, heigthFactor, _ := getNewBounds(imageData) //ImageSet is the third return
// fmt.Printf("Dimensions X:= %d and Y:= %d\n wf := %d hf:= %d\n",bound.Dx(),bound.Dy(), widthFactor, heigthFactor)
//Printing in color
if *useColor{
//Printing each pixel of the new image while converting each oldpixel to grayscale and mapping a suitable ascii character according to luminosity of the pixel
for y := bound.Min.Y; y < bound.Max.Y; y+=heigthFactor{
for x := bound.Min.X; x < bound.Max.X; x+=widthFactor{
oldPixel := imageData.At(x, y)
pixel := color.GrayModel.Convert(oldPixel)
// imageSet.Set(x, y, pixel)
// newPixel := imageSet.At(x, y)
r, g, b := getRGB(pixel)
cr,cg,cb:=getRGB(oldPixel)
intensity := getIntensity(r,g,b)
asciiColorPrinter(intensity,cr,cg,cb)
}
fmt.Print("\n")
}
}else{
for y := bound.Min.Y; y < bound.Max.Y; y+=heigthFactor{
for x := bound.Min.X; x < bound.Max.X; x+=widthFactor{
oldPixel := imageData.At(x, y)
pixel := color.GrayModel.Convert(oldPixel)
// imageSet.Set(x, y, pixel)
// newPixel := imageSet.At(x, y)
r, g, b := getRGB(pixel)
intensity := getIntensity(r,g,b)
asciiPrinter(intensity)
}
fmt.Print("\n")
}
}
}