package shmparser
|
|
/*
|
#cgo LDFLAGS: -ldl
|
#include <stdlib.h>
|
#include "shmparser.h"
|
*/
|
import "C"
|
import (
|
"strings"
|
"time"
|
"unsafe"
|
)
|
|
func str2byte(s string) []byte {
|
x := (*[2]uintptr)(unsafe.Pointer(&s))
|
h := [3]uintptr{x[0], x[1], x[1]}
|
return *(*[]byte)(unsafe.Pointer(&h))
|
}
|
|
func byte2str(d []byte) string {
|
return *(*string)(unsafe.Pointer(&d))
|
}
|
|
// Image2shm fill image to shm
|
func Image2shm(shm []byte, cid, cname string, data []byte, w, h int, fid int64) {
|
timestamp := strings.Replace(time.Now().Format("2006-01-02 15:04:05.000"), ".", ":", -1)
|
|
dataptr := (*C.uchar)(unsafe.Pointer(&data[0]))
|
cidbyte := str2byte(cid)
|
cidptr := (*C.char)(unsafe.Pointer(&cidbyte[0]))
|
cnamebyte := str2byte(cname)
|
cnameptr := (*C.char)(unsafe.Pointer(&cnamebyte[0]))
|
|
tsbyte := str2byte(timestamp)
|
tsptr := (*C.char)(unsafe.Pointer(&tsbyte[0]))
|
|
cimg := C.make_image_ref(C.ulong(fid),
|
dataptr, C.uint(len(data)), C.uint(w), C.uint(h),
|
tsptr, C.uint(len(timestamp)),
|
cidptr, C.uint(len(cid)),
|
cnameptr, C.uint(len(cname)),
|
)
|
|
shmptr := unsafe.Pointer(&shm[0])
|
C.goimage2shm(shmptr, unsafe.Pointer(&cimg))
|
}
|
|
// ImageShm Image struct in Shm
|
type ImageShm struct {
|
Data []byte
|
Width int
|
Height int
|
Timestamp string
|
Id uint64
|
Cid string
|
Cname string
|
}
|
|
func Shm2Image(shm []byte) ImageShm {
|
cimg := C.shm2image(unsafe.Pointer(&shm[0]))
|
|
var goimg ImageShm
|
const maxLen = 0xffffffffffff
|
|
var data, ts, cid, cname unsafe.Pointer
|
var sd, id C.ulong
|
var sts, scid, scname, w, h C.uint
|
|
p := unsafe.Pointer(cimg)
|
|
C.goimageinfo(p, &data, &sd, &ts, &sts, &cid, &scid, &cname, &scname, &id, &w, &h)
|
goimg.Data = (*[maxLen]byte)(data)[:uint64(sd):uint64(sd)]
|
|
tmp := (*[maxLen]byte)(ts)[:int(sts):int(sts)]
|
goimg.Timestamp = byte2str(tmp)
|
|
tmp = (*[maxLen]byte)(cid)[:int(scid):int(scid)]
|
goimg.Cid = byte2str(tmp)
|
|
tmp = (*[maxLen]byte)(cname)[:int(scname):int(scname)]
|
goimg.Cname = byte2str(tmp)
|
|
goimg.Width = int(w)
|
goimg.Height = int(h)
|
goimg.Id = uint64(id)
|
|
C.free_stimg(cimg)
|
|
return goimg
|
}
|
|
// Rect struct
|
type Rect struct {
|
Left int
|
Top int
|
Right int
|
Bottom int
|
}
|
|
// Target analysis Target
|
type Target struct {
|
Id uint64
|
Type string
|
Confidence int
|
Rc Rect
|
Feat []byte
|
Attr []byte
|
}
|
|
// Resdk result of sdk
|
type Resdk struct {
|
Type string
|
Id string
|
Name string
|
Timestamp string
|
Targets []Target
|
}
|
|
// Strule struct of rule message
|
type Strule struct {
|
Handletrack string
|
Datatype string
|
Sdks []Resdk
|
}
|
|
// Rule2Shm rule message init to shm
|
func Rule2Shm(shm []byte, handletrack string) {
|
shmptr := unsafe.Pointer(&shm[0])
|
ht := str2byte(handletrack)
|
htptr := (*C.char)(unsafe.Pointer(&ht[0]))
|
|
C.rule2shm(shmptr, htptr, C.uint(len(handletrack)))
|
}
|
|
// AddResSdk2RuleInShm add sdk message to rule in shm
|
func AddResSdk2RuleInShm(shm []byte, sdk Resdk) {
|
|
ctgts := C.gotargetsnew(C.uint(len(sdk.Targets)))
|
for k, v := range sdk.Targets {
|
tpbyte := str2byte(v.Type)
|
tptr := (*C.char)(unsafe.Pointer(&tpbyte[0]))
|
featptr := (*C.uchar)(unsafe.Pointer(&v.Feat[0]))
|
attrptr := (*C.uchar)(unsafe.Pointer(&v.Attr[0]))
|
|
C.gotargetsadd(ctgts, C.int(k), C.ulong(v.Id), tptr, C.uint(len(v.Type)),
|
C.int(v.Confidence),
|
C.int(v.Rc.Left), C.int(v.Rc.Top), C.int(v.Rc.Right), C.int(v.Rc.Bottom),
|
featptr, C.uint(len(v.Feat)), attrptr, C.uint(len(v.Attr)))
|
}
|
|
shmptr := unsafe.Pointer(&shm[0])
|
ts := str2byte(sdk.Timestamp)
|
tsptr := (*C.char)(unsafe.Pointer(&ts[0]))
|
|
tp := str2byte(sdk.Type)
|
tpstr := (*C.char)(unsafe.Pointer(&tp[0]))
|
|
C.goruleaddsdk(shmptr, ctgts, C.uint(len(sdk.Targets)),
|
tpstr, C.uint(len(sdk.Type)),
|
tsptr, C.uint(len(sdk.Timestamp)))
|
|
C.gotargetsdel(ctgts)
|
}
|
|
// Shm2Rule get Rule from shm
|
func Shm2Rule(shm []byte) Strule {
|
|
var rule Strule
|
|
crule := C.shm2rule(unsafe.Pointer(&shm[0]))
|
|
const maxLen = 0xffffffffffff
|
|
var data unsafe.Pointer
|
var s32 C.uint
|
|
p := unsafe.Pointer(crule)
|
C.gorulehandletrack(p, &data, &s32)
|
tmp := (*[maxLen]byte)(data)[:int(s32):int(s32)]
|
rule.Handletrack = byte2str(tmp)
|
|
C.goruledatatype(p, &data, &s32)
|
tmp = (*[maxLen]byte)(data)[:int(s32):int(s32)]
|
rule.Datatype = byte2str(tmp)
|
|
////////////////////////////////////////////////
|
|
C.free_strule(crule)
|
|
return rule
|
}
|