func (g zoomedImageGetter) Get(ctx groupcache.Context, key string, dest groupcache.Sink) error { bfz := &BuildingFloorZoom{} if err := json.Unmarshal([]byte(key), bfz); err != nil { return err } floor := g.s.GetBuildingFloor(bfz.Building, bfz.Floor) var err error floor.ImageOnce.Do(func() { var img, origImg draw.Image floor.ImageWG.Add(1) defer floor.ImageWG.Done() origImg, err = floor.LoadImage() if err != nil { return } img = origImg if floor.Rotation != 0 { rotatedWidth, rotatedHeight := newImageDimentions(origImg, floor.Rotation) img = image.NewNRGBA64(image.Rect(0, 0, rotatedWidth, rotatedHeight)) if err = graphics.Rotate(img, origImg, &graphics.RotateOptions{Angle: floor.Rotation}); err != nil { return } } floor.RotatedImage = img }) floor.ImageWG.Wait() if err != nil { return err } img := floor.RotatedImage coords := ctx.(*models.Coords) pixelsPerLongitude := TileSize / coords.DLng() pixelsPerLatitude := TileSize / coords.DLat() newWidth := floor.Coords.DLng() * pixelsPerLongitude newHeight := floor.Coords.DLat() * pixelsPerLatitude log.Printf("Generating resized image %f %f", newWidth, newHeight) resizedImg := resize.Resize(uint(newWidth), uint(newHeight), img, resize.NearestNeighbor) var buf bytes.Buffer if err := tileEncoder.Encode(&buf, resizedImg); err != nil { return err } return dest.SetBytes(buf.Bytes()) }
func main() { flag.Parse() c, err := colorful.Hex(*bg) if err != nil { log.Fatal(err) } f, err := os.Open(flag.Arg(0)) if err != nil { log.Fatal(err) } src, _, err := image.Decode(f) if err != nil { log.Fatal(err) } f.Close() g := &gif.GIF{ Image: []*image.Paletted{}, Delay: []int{}, LoopCount: 0, } var base float64 = (math.Pi * 2) * 10 * (*speed) / 360 if *reverse { base *= -1 } limit := int(360 / 10 / (*speed)) q := median.Quantizer(256) p := q.Quantize(make(color.Palette, 0, 256), src) for i := 0; i < limit; i++ { dst := image.NewPaletted(src.Bounds(), p) draw.Draw(dst, src.Bounds(), &image.Uniform{c}, image.ZP, draw.Src) err = graphics.Rotate(dst, src, &graphics.RotateOptions{Angle: base * float64(i)}) if err != nil { log.Fatal(err) } if *zoom { w, h := float64(src.Bounds().Dx()), float64(src.Bounds().Dy()) tmp := image.NewPaletted(src.Bounds(), p) draw.Draw(tmp, src.Bounds(), &image.Uniform{c}, image.ZP, draw.Src) z := float64(0.5 + float64(i)/30.0) graphics.I. Scale(z, z). Translate((w-w*z)/2, (h-h*z)/2). Transform(tmp, dst, interp.Bilinear) dst = tmp } g.Image = append(g.Image, dst) g.Delay = append(g.Delay, *delay) } out, err := os.Create(*output) if err != nil { log.Fatal(err) } defer out.Close() err = gif.EncodeAll(out, g) if err != nil { log.Fatal(err) } }