Beispiel #1
0
// Transforms image (resize, rotate, flip, brightness, contrast)
func (c *Convertor) TransformImage(img image.Image) image.Image {
	var i image.Image = img

	if c.Opts.Width > 0 || c.Opts.Height > 0 {
		if c.Opts.Fit {
			i = imaging.Fit(i, c.Opts.Width, c.Opts.Height, filters[c.Opts.Filter])
		} else {
			i = imaging.Resize(i, c.Opts.Width, c.Opts.Height, filters[c.Opts.Filter])
		}
	}

	if c.Opts.Rotate > 0 {
		switch c.Opts.Rotate {
		case 90:
			i = imaging.Rotate90(i)
		case 180:
			i = imaging.Rotate180(i)
		case 270:
			i = imaging.Rotate270(i)
		}
	}

	if c.Opts.Flip != "none" {
		switch c.Opts.Flip {
		case "horizontal":
			i = imaging.FlipH(i)
		case "vertical":
			i = imaging.FlipV(i)
		}
	}

	if c.Opts.Brightness != 0 {
		i = imaging.AdjustBrightness(i, c.Opts.Brightness)
	}

	if c.Opts.Contrast != 0 {
		i = imaging.AdjustContrast(i, c.Opts.Contrast)
	}

	return i
}
Beispiel #2
0
func setupScene(gameEngine engine.Engine, shader *renderer.Shader) {
	camera := gameEngine.Camera()

	transparentNode := renderer.NewNode()
	gameEngine.AddSpatialTransparent(transparentNode)
	transparentNode.RendererParams = &renderer.RendererParams{
		DepthTest:    true,
		Unlit:        true,
		Transparency: renderer.EMISSIVE,
	}

	// Sky cubemap
	skyImg, err := assets.ImportImage("TestAssets/cloudSky.jpg")
	if err == nil {
		skyImg = imaging.AdjustBrightness(skyImg, -30)
		skyImg = imaging.AdjustContrast(skyImg, 30)
		geom := renderer.CreateSkyBox()
		geom.Transform(mgl32.Scale3D(10000, 10000, 10000))
		skyNode := renderer.NewNode()
		skyNode.SetOrientation(mgl32.QuatRotate(1.57, mgl32.Vec3{0, 1, 0}))
		skyNode.Material = renderer.NewMaterial(renderer.NewTexture("diffuseMap", skyImg, false))
		skyNode.RendererParams = renderer.NewRendererParams()
		skyNode.RendererParams.CullBackface = false
		skyNode.RendererParams.Unlit = true
		skyNode.Add(geom)
		gameEngine.AddSpatial(skyNode)
		// create an environmentMap using the skybox texture
		envCubeMap := renderer.NewCubemap("environmentMap", skyImg, true)
		gameEngine.DefaultCubeMap(envCubeMap)
	}

	// load scene objs
	objs := []string{
		"TestAssets/wellScene/floor.obj",
		"TestAssets/wellScene/frame1.obj",
		"TestAssets/wellScene/frame2.obj",
		"TestAssets/wellScene/well.obj",
		"TestAssets/wellScene/torches.obj",
	}
	for _, objFile := range objs {
		if geom, mat, err := assets.ImportObjCached(objFile); err == nil {
			sceneNode := renderer.NewNode()
			sceneNode.Add(geom)
			sceneNode.Material = mat
			sceneNode.RendererParams = renderer.NewRendererParams()
			sceneNode.RendererParams.CullBackface = false
			gameEngine.AddSpatial(sceneNode)
		}
	}

	for i := 0; i < 2; i++ {
		torchLocation := mgl32.Vec3{0.86, 1.76, 1.05}
		if i == 1 {
			torchLocation = mgl32.Vec3{0.86, 1.76, -1.05}
		}

		fire := fireParticles()
		spark := sparkParticles()
		torchParticles := effects.NewParticleGroup(camera, fire, spark)
		torchParticles.SetTranslation(torchLocation)
		transparentNode.Add(torchParticles)
		gameEngine.AddUpdatable(torchParticles)

		light := renderer.NewLight(renderer.POINT)
		light.SetTranslation(torchLocation.Add(mgl32.Vec3{0, 0.05, 0}))
		gameEngine.AddLight(light)

		var x float64
		gameEngine.AddUpdatable(engine.UpdatableFunc(func(dt float64) {
			x += dt
			mag := float32(math.Abs(0.6*math.Sin(3*x)+0.3*math.Sin(4*x)+0.15*math.Sin(7*x)+0.1*math.Sin(15*x))) + 0.5
			mag *= 0.05
			light.Color = [3]float32{1 * mag, 0.6 * mag, 0.4 * mag}
		}))
	}
}
Beispiel #3
0
func unsplash(broker *tg.Broker, update tg.APIMessage) {
	if isCommand(update, "unsplash") {
		text := ""
		user := update.User

		if update.ReplyTo != nil {
			if update.ReplyTo.Text == nil {
				broker.SendTextMessage(update.Chat, "Non c'e' niente di 'ispiratore' in questo..", &update.MessageID)
				return
			}
			text = *(update.ReplyTo.Text)
			user = update.ReplyTo.User
		} else {
			if strings.Index(*(update.Text), " ") > 0 {
				text = strings.TrimSpace(strings.SplitN(*(update.Text), " ", 2)[1])
			}
		}

		author := user.FirstName
		if user.LastName != "" {
			author = user.FirstName + " " + user.LastName
		}
		author += " (" + user.Username + ")"

		if strings.TrimSpace(text) == "" {
			broker.SendTextMessage(update.Chat, "Non c'e' niente di 'ispiratore' in questo..", &update.MessageID)
			return
		}

		resp, err := http.Get(unsplashUrl)
		if err != nil {
			log.Println("[unsplash] HTTP request failed: %s\n", err.Error())
			broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
			return
		}
		defer resp.Body.Close()

		img, _, err := image.Decode(resp.Body)
		if err != nil {
			log.Println("[unsplash] Image decode error: %s\n", err.Error())
			broker.SendTextMessage(update.Chat, "<b>ERRORE</b>: Non riesco a leggere l'immagine", &update.MessageID)
			return
		}

		// Darken image
		img = imaging.AdjustBrightness(imaging.AdjustGamma(imaging.AdjustSigmoid(img, 0.5, -6.0), 0.8), -20)

		// Create target image
		bounds := img.Bounds()
		iwidth := float64(bounds.Size().X)
		iheight := float64(bounds.Size().Y)

		timg := image.NewRGBA(bounds)
		gc := draw2dimg.NewGraphicContext(timg)
		gc.SetFontData(quoteFontData)
		gc.DrawImage(img)
		gc.SetStrokeColor(image.Black)
		gc.SetFillColor(image.White)

		text = strings.ToUpper(strings.TrimSpace(text))
		gc.Restore()
		gc.Save()

		// Detect appropriate font size
		scale := iheight / iwidth * (iwidth / 10) * 0.8
		gc.SetFontSize(scale)
		gc.SetLineWidth(scale / 15)

		// Get NEW bounds
		left, top, right, bottom := gc.GetStringBounds(text)

		width := right - left
		texts := []string{text}
		if width*1.2 > iwidth {
			// Split text
			texts = splitCenter(text)

			// Get longest line
			longer := float64(0)
			longid := 0
			widths := make([]float64, len(texts))
			for id := range texts {
				tleft, _, tright, _ := gc.GetStringBounds(texts[id])
				widths[id] = tright - tleft
				if width > longer {
					longer = widths[id]
					longid = id
				}
			}

			// Still too big? Decrease font size again
			iter := 0
			for width*1.2 > iwidth && iter < 10 {
				log.Println("Warning, resizing!")
				scale *= (0.9 - 0.05*float64(iter))
				gc.SetFontSize(scale)
				left, top, right, bottom = gc.GetStringBounds(texts[longid])
				width = right - left
				iter++
			}
		}

		texts = append(texts, author)
		height := bottom - top + 20
		margin := float64(height / 50)
		txtheight := (height + margin) * float64(len(texts))

		gc.Save()
		for id, txt := range texts {
			gc.Save()
			left, _, right, _ = gc.GetStringBounds(txt)
			width = right - left

			x := (iwidth - width) / 2
			y := (iheight-txtheight)/2 + (height+margin*2)*float64(id+1)
			if id == len(texts)-1 {
				gc.SetFontSize(scale * 0.7)
				left, _, right, _ = gc.GetStringBounds(txt)
				width = right - left
				x = (iwidth - width) / 1.5
				y = (iheight-txtheight)/2 + (height+margin)*float64(id+1) + margin*6
			}

			gc.Translate(x, y)
			gc.StrokeString(txt)
			gc.FillString(txt)
			gc.Restore()
		}

		buf := new(bytes.Buffer)
		err = jpeg.Encode(buf, timg, &(jpeg.Options{Quality: 80}))
		if err != nil {
			log.Println("[unsplash] Image encode error: %s\n", err.Error())
			broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
			return
		}
		broker.SendPhoto(update.Chat, buf.Bytes(), "quote.jpg", "", &update.MessageID)
	}
}