Exemple #1
0
func doit(fn string) (string, error) {
	file, err := os.Open(fn)

	if err != nil {
		return "", err
	}

	defer file.Close()

	img, _, err := image.Decode(file)
	if err != nil {
		return "", err
	}

	b := img.Bounds()
	var g *gift.GIFT

	if b.Max.X-b.Min.X >= resizeThreshold || b.Max.Y-b.Min.Y >= resizeThreshold {
		g = gift.New(
			gift.Resize(resizeSize, resizeSize, gift.LanczosResampling),
			gift.GaussianBlur(sigma))
	} else {
		g = gift.New(gift.GaussianBlur(sigma))
	}

	dst := image.NewRGBA(g.Bounds(img.Bounds()))
	g.Draw(dst, img)
	img = dst

	fn = path.Base(fn)
	ext := path.Ext(fn)
	fn = fmt.Sprintf("./%s.blur%s", fn[:len(fn)-len(ext)], ext)

	w, _ := os.Create(fn)
	defer w.Close()
	if err = jpeg.Encode(w, dst, nil); err != nil {
		return "", err
	}

	return fn, nil
}
Exemple #2
0
func ApplyFilter(src image.Image, g *gift.GIFT) image.Image {
	dst := image.NewRGBA(g.Bounds(src.Bounds()))
	g.Draw(dst, src)
	return dst
}
Exemple #3
0
	)

	BeforeEach(func() {
		thumbnailFilter = ThumbnailFilter()
		mediumFilter = MediumFilter()
		srcImg = image.NewGray(image.Rect(0, 0, 10, 20))
		wb = new(bytes.Buffer)
		jpegFilename = "testdata/cat.jpg"
		pngFilename = "testdata/cat.png"
		gifFilename = "testdata/cat.gif"
	})

	Describe("Apply filter", func() {
		Context("when filter applied successfully", func() {
			It("should it's bound be", func() {
				Expect(ApplyFilter(srcImg, thumbnailFilter).Bounds()).To(Equal(thumbnailFilter.Bounds(srcImg.Bounds())))
			})
		})
	})
	Describe("Handle jpeg", func() {
		BeforeEach(func() {
			file, err = os.Open(jpegFilename)
		})
		It("should not error", func() {
			Expect(err).NotTo(HaveOccurred())
		})

		Context("when jpeg parsed with filter successfully", func() {

			BeforeEach(func() {
				err = ParseJpeg(wb, file, mediumFilter)