2023-07-19 22:00:00 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
hsv "git.snrd.eu/sunred/humanshader/validator"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := os.Args[1:]
|
2023-07-19 23:00:00 +02:00
|
|
|
|
|
|
|
// TODO: Better argument parsing
|
|
|
|
if len(args) == 1 || len(args) == 3 || len(args) == 5 {
|
|
|
|
fmt.Printf("Writing PPM image to %s.\n", args[0])
|
|
|
|
if len(args) == 3 || len(args) == 5 {
|
|
|
|
width, _ := strconv.Atoi(args[1])
|
|
|
|
height, _ := strconv.Atoi(args[2])
|
|
|
|
if len(args) == 5 {
|
|
|
|
startx, _ := strconv.Atoi(args[3])
|
|
|
|
starty, _ := strconv.Atoi(args[4])
|
|
|
|
writePPM(args[0], width, height, startx, starty)
|
|
|
|
} else {
|
|
|
|
writePPM(args[0], width, height, 0, 0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writePPM(args[0], 71, 40, 0, 0)
|
|
|
|
}
|
|
|
|
fmt.Println("Done")
|
|
|
|
} else if len(args) == 2 {
|
|
|
|
pxX, _ := strconv.Atoi(args[0])
|
|
|
|
pxY, _ := strconv.Atoi(args[1])
|
|
|
|
printPixel(pxX, pxY)
|
2023-07-19 22:00:00 +02:00
|
|
|
} else {
|
|
|
|
input := bufio.NewScanner(os.Stdin)
|
2023-07-19 23:00:00 +02:00
|
|
|
fmt.Print("Pixel x coordinate: ")
|
2023-07-19 22:00:00 +02:00
|
|
|
input.Scan()
|
2023-07-19 23:00:00 +02:00
|
|
|
pxX, _ := strconv.Atoi(input.Text())
|
2023-07-19 22:00:00 +02:00
|
|
|
fmt.Print("Pixel y coordinate: ")
|
|
|
|
input.Scan()
|
2023-07-19 23:00:00 +02:00
|
|
|
pxY, _ := strconv.Atoi(input.Text())
|
|
|
|
fmt.Println()
|
|
|
|
printPixel(pxX, pxY)
|
2023-07-19 22:00:00 +02:00
|
|
|
}
|
2023-07-19 23:00:00 +02:00
|
|
|
}
|
2023-07-19 22:00:00 +02:00
|
|
|
|
2023-07-19 23:00:00 +02:00
|
|
|
func printPixel(x int, y int) {
|
2023-07-19 22:00:00 +02:00
|
|
|
val := hsv.NewValidator(true)
|
2023-07-19 23:00:00 +02:00
|
|
|
val.Calculate(x, y)
|
2023-07-19 22:00:00 +02:00
|
|
|
r, g, b := val.GetColor()
|
2023-07-19 23:00:00 +02:00
|
|
|
fmt.Printf("\nR: %d\nG: %d\nB: %d\nHEX: #%02X%02X%02X\n", r, g, b, r, g, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePPM(path string, width int, height int, startx int, starty int) {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
bw := bufio.NewWriter(f)
|
|
|
|
bw.WriteString("P3\n")
|
|
|
|
bw.WriteString(fmt.Sprintf("%d %d\n", width, height))
|
|
|
|
bw.WriteString("255\n")
|
|
|
|
for y := startx; y < startx+height; y++ {
|
|
|
|
for x := starty; x < starty+width; x++ {
|
|
|
|
v := hsv.NewValidator(false)
|
|
|
|
v.Calculate(x, y)
|
|
|
|
r, g, b := v.GetColor()
|
|
|
|
bw.WriteString(fmt.Sprintf("%d %d %d ", r, g, b))
|
|
|
|
}
|
|
|
|
bw.WriteString("\n")
|
|
|
|
}
|
|
|
|
bw.Flush()
|
2023-07-19 22:00:00 +02:00
|
|
|
}
|