gographics/imagick — pull request #8 by justinfx (2013-12-30 12:13:33+00:00)
Title: Added MagickWand.GetImageFloats() (+tests) for returning float pixel data

I create this extra method on `MagickWand` because I had a need to retrieve a "packed" `[]float32` of `RGBRGB...` to supply to `OpenColorIO`'s [Processor::apply()](http://opencolorio.org/developers/api/OpenColorIO.html#Processor::apply__ImageDescRC) method.
It accepts float pixel information in order to apply color transformations: http://opencolorio.org/developers/api/OpenColorIO.html#packedimagedesc

After originally writing the function in Go, I saw that there was a lot of overhead in doing so many cgo calls in a loop. So I moved this routine down into C, in the imagick library. It about a 7x speed up. I structured it in a way to hopefully make it flexible

```
func BenchmarkFloatPixels(b *testing.B) {
wand := imagick.NewMagickWand()
defer wand.Destroy()

wand.ReadImage("logo:")
wand.ScaleImage(1024, 1024)

var (
rowNum int
inc int
pixel *imagick.PixelWand
pixels []float32
)

b.ResetTimer()

for i := 0; i < b.N; i++ {

inc = 0
cols := int(wand.GetImageWidth())
rows := int(wand.GetImageHeight())
pixels := make([]float32, cols*rows*3)

pixIter := wand.NewPixelIterator()
defer pixIter.Destroy()

for rowNum = 0; rowNum < rows; rowNum++ {
for _, pixel = range pixIter.GetNextIteratorRow() {
pixels[inc] = float32(pixel.GetRed())
pixels[inc+1] = float32(pixel.GetGreen())
pixels[inc+2] = float32(pixel.GetBlue())
inc += 3
}
}
}

b.StopTimer()
b.Logf("len(pixels) == %d", len(pixels))
}

func BenchmarkFloatPixelsC(b *testing.B) {
wand := imagick.NewMagickWand()
defer wand.Destroy()

wand.ReadImage("logo:")
wand.ScaleImage(1024, 1024)

var pixels []float32

b.ResetTimer()

for i := 0; i < b.N; i++ {
pixels = wand.GetImageFloats(imagick.FLOAT_FORMAT_RGB)
}

b.StopTimer()
b.Logf("len(pixels) == %d", len(pixels))
}
```

Results:
```
BenchmarkFloatPixels	2	640257007 ns/op
--- BENCH: BenchmarkFloatPixels
image_test.go:45: len(pixels) == 0
image_test.go:45: len(pixels) == 0
BenchmarkFloatPixelsC	20	91631297 ns/op
--- BENCH: BenchmarkFloatPixelsC
image_test.go:64: len(pixels) == 3145728
image_test.go:64: len(pixels) == 3145728
ok	github.com/justinfx/opencolorigo	4.426s
```