Skip to content

Instantly share code, notes, and snippets.

@lintianzhi
Last active August 29, 2015 14:21
Show Gist options
  • Save lintianzhi/71fa927710ff8e7dd4fe to your computer and use it in GitHub Desktop.
Save lintianzhi/71fa927710ff8e7dd4fe to your computer and use it in GitHub Desktop.
gaussian blur by ufop
package main
import (
"encoding/json"
"image"
"log"
"net/http"
"strconv"
"strings"
"image/jpeg"
_ "image/png"
"code.google.com/p/graphics-go/graphics"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/uop", handle)
log.Println("start blur service at", ":9100")
log.Fatal(http.ListenAndServe(":9100", mux))
}
type ufopArg struct {
Cmd string `json:"cmd"`
Source struct {
URL string `json:"url"`
MimeType string `json:"mimetype"`
Fsize int `json:"fsize"`
Bucket string `json:"bucket"`
Key string `json:"key"`
} `json:"src"`
}
func handle(w http.ResponseWriter, req *http.Request) {
var arg ufopArg
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&arg)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
opt, err := parseCmd(arg.Cmd)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
resp, err := http.Get(arg.Source.URL)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer resp.Body.Close()
m, _, err := image.Decode(resp.Body)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
dst := image.NewRGBA(m.Bounds())
err = graphics.Blur(dst, m, &graphics.BlurOptions{StdDev: opt.Deviation})
w.Header().Set("Content-Type", "image/jpeg")
w.WriteHeader(200)
jpeg.Encode(w, dst, &jpeg.Options{85})
}
type cmdArg struct {
Deviation float64
}
func parseCmd(cmd string) (opt cmdArg, err error) {
l := strings.Split(cmd, "/")
for i, v := range l {
if v == "deviation" && i+1 < len(l) {
opt.Deviation, err = strconv.ParseFloat(l[i+1], 64)
return
}
}
opt.Deviation = 0.5
return
}
/* ufop.yaml
image: ubuntu
build_script:
- cp $RESOURCE/* .
run: ./blur
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment