Beispiel #1
0
func (prc *RotateProcessor) getFilter(rot float32, params imageserver.Params) (gift.Filter, error) {
	switch rot {
	case 90:
		return gift.Rotate90(), nil
	case 180:
		return gift.Rotate180(), nil
	case 270:
		return gift.Rotate270(), nil
	}
	bkg, err := prc.getBackground(params)
	if err != nil {
		return nil, err
	}
	interp, err := prc.getInterpolation(params)
	if err != nil {
		return nil, err
	}
	return gift.Rotate(rot, bkg, interp), nil
}
func crop(img_name string, crop_map map[string][]string, current_dir string) {
	idx := strings.Index(img_name, ".jpg")
	if idx == -1 {
		panic("no *.jpg in " + img_name)
	}

	c := crop_map[img_name[:idx]]
	fmt.Println(img_name + " : " + strings.Join(c, ", "))

	f_path := current_dir + "/" + img_name

	f, _ := os.Open(f_path)
	defer f.Close()

	img, err := jpeg.Decode(f)
	if err != nil {
		return
	}

	/**********
	 *  Crop
	 **********/

	x0, _ := strconv.Atoi(c[0])
	y0, _ := strconv.Atoi(c[1])
	x1, _ := strconv.Atoi(c[2])
	y1, _ := strconv.Atoi(c[3])

	g := gift.New(
		gift.Crop(image.Rect(x0, y0, x1, y1)),
	)

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

	crop_f_path := current_dir + "/crop_" + img_name
	out, _ := os.Create(crop_f_path)
	defer out.Close()

	jpeg.Encode(out, crop, nil)

	/**********
	 *  Flip
	 **********/

	g = gift.New(
		gift.FlipHorizontal(),
	)

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

	flip_f_path := current_dir + "/flip_" + img_name
	out, _ = os.Create(flip_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)

	/***********
	 *  Rotate
	 ***********/

	g = gift.New(
		gift.Rotate90(),
	)

	dst = image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	rotate_f_path := current_dir + "/rotate90_" + img_name
	out, _ = os.Create(rotate_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)

	g = gift.New(
		gift.Rotate180(),
	)

	dst = image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	rotate_f_path = current_dir + "/rotate180_" + img_name
	out, _ = os.Create(rotate_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)

	g = gift.New(
		gift.Rotate270(),
	)

	dst = image.NewRGBA(g.Bounds(crop.Bounds()))
	g.Draw(dst, crop)

	rotate_f_path = current_dir + "/rotate270_" + img_name
	out, _ = os.Create(rotate_f_path)
	defer out.Close()

	jpeg.Encode(out, dst, nil)
}