Esempio n. 1
0
func createBufferFromImage(c *cl.Context, src image.Image) (*cl.MemObject, error) {
	switch m := src.(type) {
	case *image.RGBA:
		return c.CreateBuffer(cl.MemUseHostPtr|cl.MemReadOnly, m.Pix)
	case *image.Gray:
		return c.CreateBuffer(cl.MemUseHostPtr|cl.MemReadOnly, m.Pix)
	}
	return nil, fmt.Errorf("image type not supported yet")
}
Esempio n. 2
0
func buildProgram(c *cl.Context, dev *cl.Device, filename string) *cl.Program {

	data, err := ioutil.ReadFile(filename)
	checkFatalError(err)

	program, err := c.CreateProgramWithSource([]string{string(data)})
	checkFatalError(err)

	err = program.BuildProgram([]*cl.Device{dev}, "")
	if err != nil {
		fmt.Println(err)
		panic("Build Error")
	}

	return program
}
Esempio n. 3
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
}
Esempio n. 4
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
}