Advertisement
qaisjp

/r/place. generate pixel data from file

Apr 2nd, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.59 KB | None | 0 0
  1. // This example demonstrates decoding a JPEG image and examining its pixels.
  2. package main
  3.  
  4. import (
  5.     // "encoding/base64"
  6.     "fmt"
  7.     "image"
  8.     // "image/color"
  9.     "log"
  10.     "os"
  11.     // "strings"
  12.  
  13.     // Package image/jpeg is not used explicitly in the code below,
  14.     // but is imported for its initialization side-effect, which allows
  15.     // image.Decode to understand JPEG formatted images. Uncomment these
  16.     // two lines to also understand GIF and PNG images:
  17.     // _ "image/gif"
  18.     _ "image/jpeg"
  19.     _ "image/png"
  20. )
  21.  
  22. type rep struct {
  23.     R, G, B, A uint32
  24. }
  25.  
  26. var cMap = map[rep]int{
  27.     rep{0, 0, 0, 65535}:             3, // black
  28.     rep{65535, 65535, 65535, 65535}: 0, // white
  29.     rep{65535, 0, 0, 65535}:         5, // red
  30. }
  31.  
  32. func main() {
  33.     // Decode the JPEG data. If reading from file, create a reader with
  34.     //
  35.     reader, err := os.Open(os.Args[1])
  36.     if err != nil {
  37.         log.Fatal(err)
  38.     }
  39.     defer reader.Close()
  40.  
  41.     m, _, err := image.Decode(reader)
  42.     if err != nil {
  43.         log.Fatal(err)
  44.     }
  45.     bounds := m.Bounds()
  46.  
  47.     fmt.Print("[")
  48.     for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  49.         fmt.Print("[")
  50.         for x := bounds.Min.X; x < bounds.Max.X; x++ {
  51.             r, g, b, a := m.At(x, y).RGBA()
  52.  
  53.             if a == 0 {
  54.                 fmt.Print("-1,")
  55.                 continue
  56.             }
  57.  
  58.             c := rep{r, g, b, a}
  59.  
  60.             found := false
  61.             for col, i := range cMap {
  62.                 // fmt.Println("%x %x", col, c)
  63.                 // return
  64.                 if col == c {
  65.                     fmt.Printf("%d,", i)
  66.                     found = true
  67.                     break
  68.                 }
  69.             }
  70.  
  71.             if found {
  72.                 continue
  73.             }
  74.  
  75.             fmt.Printf("(%d, %d, %d, %d @ %d,%d) ", r, g, b, a, x, y)
  76.  
  77.         }
  78.         fmt.Print("],\n")
  79.     }
  80.     fmt.Print("];\n")
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement