Пример #1
0
func findImageEdges(c *cl.Context, dev *cl.Device, queue *cl.CommandQueue, src image.Image) (image.Image, *cl.Event) {
	width := src.Bounds().Dx()
	height := src.Bounds().Dy()

	srcTex, err := c.CreateImage2DFromImage(cl.MemUseHostPtr|cl.MemReadOnly, src)
	checkFatalError(err)
	dstTex, err := c.CreateImageSimple(cl.MemWriteOnly, width, height, cl.ChannelOrderRGBA, cl.ChannelDataTypeUNormInt8, nil)
	checkFatalError(err)

	// program, err := c.NewProgramFromFile("optlens.cl")
	// checkFatalError(err)

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

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

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

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

	// err = kernel.SetArg(2, int32(2))
	// 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.EnqueueReadImage(dstTex, true, [3]int{0, 0, 0}, [3]int{width, height, 1}, 0, 0, dst.Pix, nil)
	checkFatalError(err)

	return dst, kev
}
Пример #2
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
}