Example #1
0
func (img *Image) createSDLSurface(f *sdl.PixelFormat) (*sdl.Surface, error) {
	pitch := uint32(f.BytesPerPixel) * img.w

	s := sdl.CreateRGBSurfaceFrom(img.pixels,
		int(img.w), int(img.h), int(f.BitsPerPixel), int(pitch),
		f.Rmask, f.Gmask, f.Bmask, f.Amask,
	)

	if s == nil {
		return nil, errors.New(sdl.GetError())
	}

	s.Pixels = &img.pixels[0] // Avoid garbage collection

	return s, nil
}
Example #2
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}
	defer sdl.Quit()

	image0 := sdl.Load(*file)
	if image0 == nil {
		panic(sdl.GetError())
	}

	factorx := float32(1)
	factory := float32(1)

	dstW := uint32(float32(image0.W) * factorx)
	dstH := uint32(float32(image0.H) * factory)

	screen := sdl.SetVideoMode(int(dstW), int(dstH), 32, sdl.DOUBLEBUF)

	if screen == nil {
		panic(sdl.GetError())
	}

	image := sdl.DisplayFormat(image0)
	format := image.Format

	err := initAndPrepCL()
	check(err)

	order := cl.RGBA
	elemSize := 4

	src, err := c.NewImage2D(cl.MEM_READ_ONLY|cl.MEM_USE_HOST_PTR, order, cl.UNSIGNED_INT8,
		uint32(image.W), uint32(image.H), uint32(image.Pitch), image.Pixels)
	check(err)

	dst, err := c.NewImage2D(cl.MEM_WRITE_ONLY, order, cl.UNSIGNED_INT8,
		dstW, dstH, 0, nil)
	check(err)

	angle := float32(0)
	for running := true; running; angle += 0.001 {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch ev := e.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyboardEvent:
				if ev.Keysym.Sym == sdl.K_ESCAPE {
					running = false
					break
				}
			}
		}

		//pixels, err := imageCall(dstW, dstH, "image_recscale", src, dst, 1/factorx, 1/factory)

		//pixels, err := imageCall(dstW, dstH, "image_rotate", src, dst, angle)
		m := mul(S(1.2, 1.2), R(angle))
		off := []float32{float32(dstW / 2), float32(dstH / 2)}
		pixels, err := imageCall(dstW, dstH, "image_affine2", src, dst,
			[]float32(m[0:2]), []float32(m[2:4]),
			off, off)
		check(err)

		news := sdl.CreateRGBSurfaceFrom(pixels,
			int(dstW), int(dstH), int(elemSize*8), int(elemSize)*int(dstW),
			format.Rmask, format.Gmask, format.Bmask, format.Amask,
		)

		if news == nil {
			log.Fatal(sdl.GetError())
		}

		screen.FillRect(nil, 0)
		screen.Blit(&sdl.Rect{0, 0, 0, 0}, news, nil)
		screen.Flip()
		sdl.Delay(25)
	}
}