示例#1
0
文件: hello.go 项目: kamoljan/webp
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")
}
示例#2
0
func imgToFile(ic *iconf) (string, error) {
	img := resize.Resize(ic.width, 0, ic.image, resize.NearestNeighbor)
	ic.height = uint(img.Bounds().Size().Y)
	ic.fid = fmt.Sprintf("%s-%s-%s-%s-%s-%s-%d-%d", ic.machine, ic.format, ic.density, ic.ui, ic.hash, ic.color, ic.width, ic.height)
	dir := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", conf.SushiobrolStore, ic.machine, ic.format, ic.density, ic.ui, ic.hash[0:2], ic.hash[2:4])
	ic.path = fmt.Sprintf("%s/%s", dir, ic.fid)
	out, err := os.Create(ic.path)
	if err != nil {
		log.Println(err)
		return "", err
	}
	defer out.Close()
	if conf.InputType == "jpeg" {
		err = jpeg.Encode(out, img, nil) // write image to file
		if err != nil {
			fmt.Println("ERROR: Unable to Encode into webp")
			return "", err
		}
	} else {
		if webp.Encode(out, img, &webp.Options{conf.Lossless, conf.Quality}); err != nil {
			fmt.Println("ERROR: Unable to Encode into webp")
			return "", err
		}
	}
	return ic.fid, err
}
示例#3
0
// Resize and crop an image
func ProcessFile(inputFile string, outputFile string, width int, height int, quality int) {

	if inputFile == "" || outputFile == "" {
		return
	}

	imagefile, err := os.Open(inputFile)
	// TODO: implement a saner way to handle
	// open errors
	if err != nil {
		fmt.Printf("Open error \n" + inputFile)
		log.Println(err) // Don't use log.Fatal to exit
		return
	}

	// Decode the image.
	m, _, err := image.Decode(imagefile)
	imagefile.Close()

	if err != nil {
		fmt.Printf("Decode error \n")
		log.Println(err)
		return
	}

	b := m.Bounds()

	// All images are converted to the NRGBA type
	rgbaImage := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
	draw.Draw(rgbaImage, rgbaImage.Bounds(), m, b.Min, draw.Src)

	// Perform an optimal resize with 4 iterations
	m2 := optimal.OptimalResize(rgbaImage, width, height, 4)

	fo, err := os.Create(outputFile)

	if err != nil {
		fmt.Printf("create file error \n")
		fo.Close()
		return
	}

	writer := bufio.NewWriter(fo)

	switch filepath.Ext(outputFile) {
	case ".png":
		png.Encode(writer, m2)
		break
	case ".webp":
		webp.Encode(writer, m2, &webp.Options{Lossless: false, Quality: float32(quality)})
		break
	default: //default to jpg
		jpeg.Encode(writer, m2, &jpeg.Options{Quality: quality})
		break
	}
	writer.Flush()
	fo.Close()
}
示例#4
0
文件: handler.go 项目: aimin/ykthumb
//to webp
func towebp(file string, to string, sft string) error {
	img0, err := loadImgSpecFormat(file, sft)
	if err != nil {
		return err
	}
	buf := new(bytes.Buffer)
	err = webp.Encode(buf, img0, &webp.Options{Quality: 75})
	if err = ioutil.WriteFile(to, buf.Bytes(), 0666); err != nil {
		return err
	}
	return nil
}
示例#5
0
func BenchmarkRGBA_webp_q75(b *testing.B) {
	m := tToRGBA(tLoadImage("../testdata/lena512color.png"))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := webp.Encode(ioutil.Discard, m, &webp.Options{
			Lossless: false,
			Quality:  75,
		})
		if err != nil {
			b.Fatal(err)
		}
	}
}
示例#6
0
func BenchmarkGray_webp_lossless(b *testing.B) {
	m := tToGray(tLoadImage("../testdata/lena512color.png"))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := webp.Encode(ioutil.Discard, m, &webp.Options{
			Lossless: true,
			Quality:  0,
		})
		if err != nil {
			b.Fatal(err)
		}
	}
}
示例#7
0
func EncodeWebp(ibuf []byte, wquality float32) ([]byte, error) {
	var data bytes.Buffer
	var obuf []byte
	src := bytes.NewBuffer(ibuf)

	img, _, err := image.Decode(src)

	if err == nil {
		if webp.Encode(&data, img, &webp.Options{false, wquality}); err == nil {
			obuf = data.Bytes()
		}
	}

	return obuf, err
}
func main() {
	i, o := os.Args[1], os.Args[2]
	f, err := os.Open(i)
	if err != nil {
		log.Fatal("Error unable to open a file: ", err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal("Error unable to decode a file: ", err)
	}
	w, err := os.Create(o)
	if err != nil {
		log.Fatal("Error unable to create a file: ", err)
	}
	defer w.Close()
	err = webp.Encode(w, img, &webp.Options{true, 90})
	if err != nil {
		log.Fatal("Error unable to encode a file: ", err)
	}
}