Exemplo n.º 1
0
Arquivo: main.go Projeto: stanim/karta
func kartaPNGHandler(c *context, r *http.Request, w http.ResponseWriter) error {
	if r.Method != "GET" {
		return errors.New("no such handler")
	}

	seed := getInt(r, "s", 3, 0, math.MaxUint32)
	width := getInt(r, "w", 512, 10, 5120)
	height := getInt(r, "h", 512, 10, 5120)
	count := getInt(r, "c", 2048, 3, 10000)
	iterations := getInt(r, "i", 1, 0, 16)

	// Seed the random number generator
	rand.Seed(int64(seed))

	// Create a new karta
	k := karta.New(width, height, count, iterations)

	if k.Generate() == nil {
		w.Header().Add("Content-Type", "image/png")

		err := png.Encode(w, k.Image)
		if err != nil {
			return err
		}

		c.logf("Served PNG count=%v width=%v height=%v iterations=%v seed=%v",
			count, width, height, iterations, seed)
	} else {
		return errors.New("could not generate image")
	}

	return nil
}
Exemplo n.º 2
0
func kartaImageProvider(name string, width, height int) image.Image {
	if width == 0 {
		width = 512
	}

	if height == 0 {
		height = 512
	}

	count := 512
	iterations := 1

	// Create a new karta
	k := karta.New(width, height, count, iterations)

	if k.Generate() == nil {
		return k.Image
	}

	return image.NewRGBA(image.Rect(0, 0, width, height))
}