Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This example demonstrates decoding a JPEG image and examining its pixels.
- package main
- import (
- // "encoding/base64"
- "fmt"
- "image"
- // "image/color"
- "log"
- "os"
- // "strings"
- // Package image/jpeg is not used explicitly in the code below,
- // but is imported for its initialization side-effect, which allows
- // image.Decode to understand JPEG formatted images. Uncomment these
- // two lines to also understand GIF and PNG images:
- // _ "image/gif"
- _ "image/jpeg"
- _ "image/png"
- )
- type rep struct {
- R, G, B, A uint32
- }
- var cMap = map[rep]int{
- rep{0, 0, 0, 65535}: 3, // black
- rep{65535, 65535, 65535, 65535}: 0, // white
- rep{65535, 0, 0, 65535}: 5, // red
- }
- func main() {
- // Decode the JPEG data. If reading from file, create a reader with
- //
- reader, err := os.Open(os.Args[1])
- if err != nil {
- log.Fatal(err)
- }
- defer reader.Close()
- m, _, err := image.Decode(reader)
- if err != nil {
- log.Fatal(err)
- }
- bounds := m.Bounds()
- fmt.Print("[")
- for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
- fmt.Print("[")
- for x := bounds.Min.X; x < bounds.Max.X; x++ {
- r, g, b, a := m.At(x, y).RGBA()
- if a == 0 {
- fmt.Print("-1,")
- continue
- }
- c := rep{r, g, b, a}
- found := false
- for col, i := range cMap {
- // fmt.Println("%x %x", col, c)
- // return
- if col == c {
- fmt.Printf("%d,", i)
- found = true
- break
- }
- }
- if found {
- continue
- }
- fmt.Printf("(%d, %d, %d, %d @ %d,%d) ", r, g, b, a, x, y)
- }
- fmt.Print("],\n")
- }
- fmt.Print("];\n")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement