func main() { var buf bytes.Buffer var width, height int var data []byte var err error // Load file data if data, err = ioutil.ReadFile("./testdata/1_webp_ll.webp"); err != nil { log.Fatal(err) } // GetInfo if width, height, _, err = webp.GetInfo(data); err != nil { log.Fatal(err) } fmt.Printf("width = %d, height = %d\n", width, height) // Decode webp m, err := webp.Decode(bytes.NewReader(data)) if err != nil { log.Fatal(err) } // Encode lossless webp if err = webp.Encode(&buf, m, &webp.Options{Lossless: true}); err != nil { log.Fatal(err) } if err = ioutil.WriteFile("output.webp", buf.Bytes(), 0666); err != nil { log.Fatal(err) } fmt.Printf("Save output.webp ok\n") }
func BenchmarkDecode_yellow_rose_lossy_with_alpha_chai2010_webp(b *testing.B) { data := tbLoadData(b, "yellow_rose.lossy-with-alpha.webp") b.ResetTimer() for i := 0; i < b.N; i++ { m, err := chai2010_webp.Decode(bytes.NewReader(data)) if err != nil { b.Fatal(err) } _ = m } }
func BenchmarkDecode_video_001_chai2010_webp(b *testing.B) { data := tbLoadData(b, "video-001.webp") b.ResetTimer() for i := 0; i < b.N; i++ { m, err := chai2010_webp.Decode(bytes.NewReader(data)) if err != nil { b.Fatal(err) } _ = m } }
func BenchmarkDecode_tux_lossless_chai2010_webp(b *testing.B) { data := tbLoadData(b, "tux.lossless.webp") b.ResetTimer() for i := 0; i < b.N; i++ { m, err := chai2010_webp.Decode(bytes.NewReader(data)) if err != nil { b.Fatal(err) } _ = m } }
func BenchmarkDecode_blue_purple_pink_lossy_chai2010_webp(b *testing.B) { data := tbLoadData(b, "blue-purple-pink.lossy.webp") b.ResetTimer() for i := 0; i < b.N; i++ { m, err := chai2010_webp.Decode(bytes.NewReader(data)) if err != nil { b.Fatal(err) } _ = m } }
func Post(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.Write(json.Message("ERROR", "Not supported Method")) return } f, _, err := r.FormFile("image") if err != nil { w.Write(json.Message("ERROR", "Can't Find Image")) return } defer f.Close() buf := bufio.NewReader(f) h := sha1.New() var result json.Result var ic iconf ic.machine = conf.Image.Machine if conf.InputType == "jpeg" { ic.image, _, err = image.Decode(io.TeeReader(buf, h)) } else { ic.image, err = webp.Decode(io.TeeReader(buf, h)) } ic.hash = fmt.Sprintf("%x", h.Sum(nil)) if err != nil { w.Write(json.Message("ERROR", "Unable to decode your image! Type="+conf.InputType+" error:"+err.Error())) return } setColor(&ic) for _, format := range conf.Image.Format { // jpeg, webp, ... for _, screen := range conf.Image.Screen { ic.format = format ic.ui = screen.Ui ic.density = screen.Density ic.width = screen.Width if ic.fid, err = imgToFile(&ic); err != nil { w.Write(json.Message("ERROR", "Unable to create a file")) return } fid := json.Fid{fmt.Sprintf("%s_%s", screen.Density, screen.Ui), ic.fid} result.Image = append(result.Image, fid) } } w.Write(json.Message("OK", &result)) }
func loadImgSpecFormat(file string, ft string) (image.Image, error) { file_origin, err := os.Open(file) if err != nil { return nil, Newcuserr(10000) } defer file_origin.Close() var img0 image.Image switch ft { case "gif": img0, err = gif.Decode(file_origin) case "png": img0, err = png.Decode(file_origin) case "webp": img0, err = webp.Decode(file_origin) default: img0, err = jpeg.Decode(file_origin) } if err != nil { return nil, Newcuserr(10001) } return img0, nil }