-
Notifications
You must be signed in to change notification settings - Fork 35
/
statistics_im.go
51 lines (45 loc) · 1.51 KB
/
statistics_im.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// +build !gm
package magick
// #include <magick/api.h>
import "C"
import (
"reflect"
"unsafe"
)
func (im *Image) statistics() (*Statistics, error) {
var ex C.ExceptionInfo
C.GetExceptionInfo(&ex)
defer C.DestroyExceptionInfo(&ex)
stats := C.GetImageChannelStatistics(im.image, &ex)
if stats != nil {
defer freeMagickMemory(unsafe.Pointer(stats))
}
if stats == nil || ex.severity != C.UndefinedException {
return nil, exError(&ex, "getting statistics")
}
return newStatistics(stats), nil
}
func newChannelStatistics(ch *C.ChannelStatistics) *ChannelStatistics {
return &ChannelStatistics{
Minimum: float64(ch.minima / C.QuantumRange),
Maximum: float64(ch.maxima / C.QuantumRange),
Mean: float64(ch.mean / C.QuantumRange),
StdDev: float64(ch.standard_deviation / C.QuantumRange),
Variance: float64(ch.variance / C.QuantumRange),
Kurtosis: float64(ch.kurtosis / C.QuantumRange),
Skewness: float64(ch.skewness / C.QuantumRange),
}
}
func newStatistics(stats *C.ChannelStatistics) *Statistics {
count := C.OpacityChannel + 1
var channels []C.ChannelStatistics
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&channels)))
sliceHeader.Cap = count
sliceHeader.Len = count
sliceHeader.Data = uintptr(unsafe.Pointer(stats))
red := newChannelStatistics(&channels[C.RedChannel])
green := newChannelStatistics(&channels[C.GreenChannel])
blue := newChannelStatistics(&channels[C.BlueChannel])
opacity := newChannelStatistics(&channels[C.OpacityChannel])
return &Statistics{red, green, blue, opacity}
}