daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Dec 12th, 2018 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "os"
  6.     "strconv"
  7.     "image"
  8.     "image/png"
  9.     "image/color"
  10.     "encoding/hex"
  11. )
  12.  
  13. var palette = []string{"#ffffff","#cdcdcd","#888888","#555555","#222222","#000000","#ffa7d1","#e50000",
  14.     "#800000","#ffddca","#f6b389","#e59500","#a06a42","#604028","#e5d900","#94e044",
  15.     "#02be01","#005f00","#00d3dd","#0083c7","#0000ea","#cf6ee4","#ff00ff","#820080"}
  16.  
  17. func decodeBoard(boardName string, imageName string, height int, width int) {
  18.     boardFile, err := os.Open(boardName)
  19.     if err != nil {
  20.         fmt.Println("Unable to open board.")
  21.         return
  22.     }
  23.    
  24.     // Read board data into array
  25.     boardData := make([]byte, height*width)
  26.     defer boardFile.Close()
  27.     boardFile.Read(boardData)
  28.    
  29.     // Convert palette
  30.     paletteData := []color.Color{}
  31.     for _, code := range palette {
  32.         r, _ := hex.DecodeString(code[1:3])
  33.         g, _ := hex.DecodeString(code[3:5])
  34.         b, _ := hex.DecodeString(code[5:7])
  35.         paletteData = append(paletteData, color.RGBA{uint8(r[0]),uint8(g[0]),uint8(b[0]),255})
  36.     }
  37.    
  38.     // Import board data to image
  39.     imageData := image.NewPaletted(image.Rect(0, 0, height, width), paletteData)
  40.     copy(imageData.Pix, boardData)
  41.    
  42.     // Write to image
  43.     imageFile, err := os.Create(imageName)
  44.     if err != nil {
  45.         fmt.Println("Unable to open image.")
  46.         return
  47.     }
  48.     defer imageFile.Close()
  49.     png.Encode(imageFile, imageData)
  50.    
  51.     fmt.Println("Saved board to "+imageName)
  52. }
  53.  
  54. func encodeBoard(imageName string, boardName string) {
  55.     // Import image
  56.     imageFile, err := os.Open(imageName)
  57.     if err != nil {
  58.         fmt.Println("Unable to open image.")
  59.         return
  60.     }
  61.     defer imageFile.Close()
  62.     imageData, err := png.Decode(imageFile)
  63.     if err != nil {
  64.         fmt.Println("Unable to decode image.")
  65.         return
  66.     }
  67.    
  68.     // Convert palette
  69.     paletteData := []color.Color{}
  70.     for _, code := range palette {
  71.         r, _ := hex.DecodeString(code[1:3])
  72.         g, _ := hex.DecodeString(code[3:5])
  73.         b, _ := hex.DecodeString(code[5:7])
  74.         paletteData = append(paletteData, color.RGBA{r[0],g[0],b[0],255})
  75.     }
  76.    
  77.     // Write data to board
  78.     boardFile, err := os.Create(boardName)
  79.     if err != nil {
  80.         fmt.Println("Unable to open image.")
  81.         return
  82.     }
  83.     defer boardFile.Close()
  84.     boardFile.Write(imageData.(*image.Paletted).Pix)
  85.    
  86.     fmt.Println("Saved board to "+boardName)
  87.    
  88. }
  89.  
  90. func main() {
  91.     // Print Message
  92.     fmt.Println("Pxls Canvas Tool\nby flewkey\n--------\nUsage:\ncanvas board.dat board.png height width\ncanvas board.png board.dat\n--------")
  93.  
  94.     // Check arguments
  95.     if len(os.Args) < 3 {
  96.         fmt.Println("File names not specified.")
  97.         return
  98.     }
  99.     decode := false
  100.     if os.Args[2][len(os.Args[2])-4:] == ".png" {
  101.         decode = true
  102.     }
  103.     if (len(os.Args) < 5 && decode) {
  104.         fmt.Println("Height and width not specified.")
  105.         return
  106.     }
  107.     if decode {
  108.         height, err := strconv.Atoi(os.Args[3])
  109.         width, err := strconv.Atoi(os.Args[4])
  110.         if err != nil {
  111.             fmt.Println("Height and width must be integers.")
  112.             return
  113.         }
  114.         decodeBoard(os.Args[1], os.Args[2], height, width)
  115.     } else {
  116.         encodeBoard(os.Args[1], os.Args[2])
  117.     }
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top