main.go - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       main.go (1676B)
       ---
            1 /* image to oldskool block characters */
            2 package main
            3 
            4 import (
            5         "fmt"
            6         "image/jpeg"
            7         "os"
            8 )
            9 
           10 func round(f float64) int64 {
           11         f += 0.5
           12         f *= 10
           13         return int64(f) / 10
           14 }
           15 
           16 func main() {
           17         file, err := os.Open("test.jpg")
           18         if err != nil {
           19                 panic(err)
           20         }
           21         defer file.Close()
           22 
           23         img, err := jpeg.Decode(file)
           24         if err != nil {
           25                 panic(err)
           26         }
           27 
           28         b := img.Bounds()
           29         w := b.Max.X
           30         h := b.Max.Y
           31 
           32         fmt.Printf(`<!DOCTYPE html>
           33         <html lang="nl" dir="ltr">
           34         <head>
           35                 <meta charset="utf-8" />
           36                 <style type="text/css">
           37                         * { line-height: 8px; font-size: 14px; padding: 0; margin: 0; vertical-align: top; }
           38                 </style>
           39         </head>
           40         <body>
           41         <pre>`)
           42 
           43         // http://www.utf8-chartable.de/unicode-utf8-table.pl?start=9600&number=128
           44         //                 " " empty (0x20)
           45         // ASCII code 176 = ░ ( Graphic character, low density dotted )
           46         // ASCII code 177 = ▒ ( Graphic character, medium density dotted )
           47         // ASCII code 178 = ▓ ( Graphic character, high density dotted )
           48         // ASCII code 219 = █ ( Block, graphic character )
           49         chars := []int{32, 0x2591, 0x2592, 0x2593, 0x2588}
           50         l := uint32(len(chars)) - 1
           51         for y := 0; y < h; y++ {
           52                 for x := 0; x < w; x++ {
           53                         c := img.At(x, y)
           54                         r, g, b, _ := c.RGBA()
           55 
           56                         avg := float64(r+g+b) / 3.0
           57                         v := avg / float64(0xffff)
           58                         v *= float64(l)
           59                         vi := uint32(round(v))
           60 
           61                         fmt.Printf("%c", chars[l-vi])
           62 
           63                         //fmt.Printf("&#%d;", chars[l-vi])
           64 
           65                         //fmt.Printf("<span style=\"color: rgb(%d, %d, %d)\">&#%d;</span>",
           66                         //        r / 255, g / 255, b / 255, chars[l-vi])
           67 
           68                         //fmt.Printf("<span style=\"background-color: rgb(%d, %d, %d)\">&#%d;</span>",
           69                         //        r / 255, g / 255, b / 255, ' ') // truncate
           70                 }
           71                 fmt.Printf("\n")
           72         }
           73         fmt.Printf(`</pre></body></html>`)
           74 }