Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os"
- "strconv"
- "image"
- "image/png"
- "image/color"
- "encoding/hex"
- )
- var palette = []string{"#ffffff","#cdcdcd","#888888","#555555","#222222","#000000","#ffa7d1","#e50000",
- "#800000","#ffddca","#f6b389","#e59500","#a06a42","#604028","#e5d900","#94e044",
- "#02be01","#005f00","#00d3dd","#0083c7","#0000ea","#cf6ee4","#ff00ff","#820080"}
- func decodeBoard(boardName string, imageName string, height int, width int) {
- boardFile, err := os.Open(boardName)
- if err != nil {
- fmt.Println("Unable to open board.")
- return
- }
- // Read board data into array
- boardData := make([]byte, height*width)
- defer boardFile.Close()
- boardFile.Read(boardData)
- // Convert palette
- paletteData := []color.Color{}
- for _, code := range palette {
- r, _ := hex.DecodeString(code[1:3])
- g, _ := hex.DecodeString(code[3:5])
- b, _ := hex.DecodeString(code[5:7])
- paletteData = append(paletteData, color.RGBA{uint8(r[0]),uint8(g[0]),uint8(b[0]),255})
- }
- // Import board data to image
- imageData := image.NewPaletted(image.Rect(0, 0, height, width), paletteData)
- copy(imageData.Pix, boardData)
- // Write to image
- imageFile, err := os.Create(imageName)
- if err != nil {
- fmt.Println("Unable to open image.")
- return
- }
- defer imageFile.Close()
- png.Encode(imageFile, imageData)
- fmt.Println("Saved board to "+imageName)
- }
- func encodeBoard(imageName string, boardName string) {
- // Import image
- imageFile, err := os.Open(imageName)
- if err != nil {
- fmt.Println("Unable to open image.")
- return
- }
- defer imageFile.Close()
- imageData, err := png.Decode(imageFile)
- if err != nil {
- fmt.Println("Unable to decode image.")
- return
- }
- // Convert palette
- paletteData := []color.Color{}
- for _, code := range palette {
- r, _ := hex.DecodeString(code[1:3])
- g, _ := hex.DecodeString(code[3:5])
- b, _ := hex.DecodeString(code[5:7])
- paletteData = append(paletteData, color.RGBA{r[0],g[0],b[0],255})
- }
- // Write data to board
- boardFile, err := os.Create(boardName)
- if err != nil {
- fmt.Println("Unable to open image.")
- return
- }
- defer boardFile.Close()
- boardFile.Write(imageData.(*image.Paletted).Pix)
- fmt.Println("Saved board to "+boardName)
- }
- func main() {
- // Print Message
- fmt.Println("Pxls Canvas Tool\nby flewkey\n--------\nUsage:\ncanvas board.dat board.png height width\ncanvas board.png board.dat\n--------")
- // Check arguments
- if len(os.Args) < 3 {
- fmt.Println("File names not specified.")
- return
- }
- decode := false
- if os.Args[2][len(os.Args[2])-4:] == ".png" {
- decode = true
- }
- if (len(os.Args) < 5 && decode) {
- fmt.Println("Height and width not specified.")
- return
- }
- if decode {
- height, err := strconv.Atoi(os.Args[3])
- width, err := strconv.Atoi(os.Args[4])
- if err != nil {
- fmt.Println("Height and width must be integers.")
- return
- }
- decodeBoard(os.Args[1], os.Args[2], height, width)
- } else {
- encodeBoard(os.Args[1], os.Args[2])
- }
- }
Add Comment
Please, Sign In to add comment