Ejemplo n.º 1
0
func findBufferEdges(c *cl.Context, dev *cl.Device, queue *cl.CommandQueue, src image.Image) (image.Image, *cl.Event) {
	width := src.Bounds().Dx()
	height := src.Bounds().Dy()

	srcBuf, err := createBufferFromImage(c, src)
	checkFatalError(err)
	size := width * height * 4 * 4
	dstBuf, err := c.CreateEmptyBuffer(cl.MemWriteOnly, size)
	checkFatalError(err)

	program := buildProgram(c, dev, "edgedetect.cl")

	kernel, err := program.CreateKernel("bufferEdgeDetect")
	checkFatalError(err)

	err = kernel.SetArg(0, srcBuf)
	checkFatalError(err)

	err = kernel.SetArg(1, dstBuf)
	checkFatalError(err)

	err = kernel.SetArg(2, int32(width))
	checkFatalError(err)

	const edgeSize = 1

	kev, err := queue.EnqueueNDRangeKernel(kernel, []int{edgeSize, edgeSize}, []int{width - 2*edgeSize, height - 2*edgeSize}, nil, nil)
	checkFatalError(err)

	dst := image.NewRGBA(src.Bounds())

	_, err = queue.EnqueueReadBufferUint8(dstBuf, true, 0, dst.Pix, nil)
	checkFatalError(err)

	return dst, kev
}