Skip to content

Commit

Permalink
added FileStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce committed Aug 27, 2024
1 parent a54b376 commit c48459c
Show file tree
Hide file tree
Showing 6 changed files with 646 additions and 0 deletions.
77 changes: 77 additions & 0 deletions persistence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef _OPENCV3_OBJDETECT_H_
#define _OPENCV3_OBJDETECT_H_

#include <stdbool.h>

#ifdef __cplusplus
#include <opencv2/opencv.hpp>
extern "C" {
#endif

#include "core.h"

#ifdef __cplusplus
typedef cv::FileStorage* FileStorage;
typedef cv::FileNode* FileNode;
#else
typedef void* FileStorage;
typedef void* FileNode;
#endif

// FileStorage
FileStorage FileStorage_Create(void);
FileStorage FileStorage_CreateWithParams(const char* filename, int flags, const char* encoding);
void FileStorage_Close(FileStorage fs);

const char *FileStorage_ElName(FileStorage fs);
int FileStorage_State(FileStorage fs);

void FileStorage_EndWriteStruct(FileStorage fs);
int FileStorage_GetFormat(FileStorage fs);
bool FileStorage_IsOpened(FileStorage fs);
bool FileStorage_Open(FileStorage fs, const char* filename, int flags, const char* encoding);
void FileStorage_Release(FileStorage fs);
const char* FileStorage_ReleaseAndGetString(FileStorage fs);
void FileStorage_StartWriteStruct(FileStorage fs, const char* name, int flags, const char* typeName);
void FileStorage_WriteMat(FileStorage fs, const char* name, Mat val);
void FileStorage_WriteString(FileStorage fs, const char* name, const char* val);
void FileStorage_WriteStringArray(FileStorage fs, const char* name, const char** val, size_t len);
void FileStorage_WriteDouble(FileStorage fs, const char* name, double val);
void FileStorage_WriteInt(FileStorage fs, const char* name, int val);
void FileStorage_WriteComment(FileStorage fs, const char* comment, bool append);
void FileStorage_WriteRaw(FileStorage fs, const char* fmt, const void* vec, size_t len);

FileNode FileStorage_GetFirstTopLevelNode(FileStorage fs);
FileNode FileStorage_GetNode(FileStorage fs, const char* nodename);
FileNode FileStorage_Root(FileStorage fs, int streamidx);

bool FileNode_Empty(FileNode fn);
bool FileNode_IsInt(FileNode fn);
bool FileNode_IsMap(FileNode fn);
bool FileNode_IsNamed(FileNode fn);
bool FileNode_IsNone(FileNode fn);
bool FileNode_IsReal(FileNode fn);
bool FileNode_IsSeq(FileNode fn);
bool FileNode_IsString(FileNode fn);
char** FileNode_Keys(FileNode fn);
size_t FileNode_KeysCount(FileNode fn);
void FileNode_KeysFree(char** keys, size_t len);
Mat FileNode_Mat(FileNode fn);
const char* FileNode_Name(FileNode fn);
float FileNode_Float(FileNode fn);
const char* FileNode_String(FileNode fn);
FileNode FileNode_Get(FileNode fn, int i); //FileNode operator[] (int i) const
FileNode FileNode_GetByName(FileNode fn, const char* nodename); //FileNode operator[] (const char *nodename) const
size_t FileNode_RawSize(FileNode fn);
void FileNode_ReadRaw(FileNode fn, const char* fmt, void *vec, size_t len);
void FileNode_SetValue(FileNode fn, int type, const void *value, int len);
size_t FileNode_Size(FileNode fn);
int FileNode_Type(FileNode fn);

void FileNode_Close(FileNode fn);

#ifdef __cplusplus
}
#endif

#endif //_OPENCV3_OBJDETECT_H_
113 changes: 113 additions & 0 deletions persistence_filenode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <string.h>
#include "persistence.h"

bool FileNode_Empty(FileNode fn) {
return fn->empty();
}

bool FileNode_IsInt(FileNode fn){
return fn->isInt();
}

bool FileNode_IsMap(FileNode fn){
return fn->isMap();
}

bool FileNode_IsNamed(FileNode fn) {
return fn->isNamed();
}

bool FileNode_IsNone(FileNode fn){
return fn->isNone();
}

bool FileNode_IsReal(FileNode fn){
return fn->isReal();
}

bool FileNode_IsSeq(FileNode fn) {
return fn->isSeq();
}

bool FileNode_IsString(FileNode fn) {
return fn->isString();
}

char** FileNode_Keys(FileNode fn) {

std::vector<cv::String> keys = fn->keys();

char** c_keys = new char*[keys.size()];

for (int i = 0; i < keys.size(); i++) {
char *c_key = new char[keys[i].length()+1];
strcpy(c_key, keys[i].c_str());
c_keys[i] = c_key;
}

return c_keys;
}

size_t FileNode_KeysCount(FileNode fn) {
return fn->keys().size();
}


void FileNode_KeysFree(char** keys, size_t len) {
for(int i = 0; i < len; i++) {
delete keys[i];
}
delete keys;
}

Mat FileNode_Mat(FileNode fn) {
return new cv::Mat(fn->mat());
}

const char* FileNode_Name(FileNode fn) {
char* str = new char[fn->name().length()+1];
strcpy(str, fn->name().c_str());
return str;
}

float FileNode_Float(FileNode fn) {
return (float)fn->real();
}

const char* FileNode_String(FileNode fn) {
char* str = new char[fn->string().length()+1];
strcpy(str, fn->string().c_str());
return str;
}

FileNode FileNode_Get(FileNode fn, int i) {
return new cv::FileNode((*fn)[i]);
}

FileNode FileNode_GetByName(FileNode fn, const char* nodename) {
return new cv::FileNode((*fn)[nodename]);
}

size_t FileNode_RawSize(FileNode fn) {
return fn->rawSize();
}

void FileNode_ReadRaw(FileNode fn, const char* fmt, void *vec, size_t len) {
fn->readRaw(fmt, vec, len);
}

void FileNode_SetValue(FileNode fn, int type, const void *value, int len) {
fn->setValue(type, value, len);
}

size_t FileNode_Size(FileNode fn) {
return fn->size();
}

int FileNode_Type(FileNode fn) {
return fn->type();
}

void FileNode_Close(FileNode fn){
delete fn;
}
84 changes: 84 additions & 0 deletions persistence_filenode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gocv

/*
#include <stdlib.h>
#include "persistence.h"
*/
import "C"
import "unsafe"

type FileNodeType int

const (
FileNodeTypeNone FileNodeType = 0
FileNodeTypeInt FileNodeType = 1
FileNodeTypeReal FileNodeType = 2
FileNodeTypeFloat FileNodeType = FileNodeTypeReal
FileNodeTypeStr FileNodeType = 3
FileNodeTypeString FileNodeType = FileNodeTypeStr
FileNodeTypeSeq FileNodeType = 4
FileNodeTypeMap FileNodeType = 5
FileNodeTypeTypeMask FileNodeType = 7
FileNodeTypeFlow FileNodeType = 8
FileNodeTypeUniform FileNodeType = 8
FileNodeTypeEmpty FileNodeType = 16
FileNodeTypeNamed FileNodeType = 32
)

// FileNode is a wrapper for the OpenCV FileNode class
//
// Ref: https://docs.opencv.org/4.x/de/dd9/classcv_1_1FileNode.html
type FileNode struct {
p C.FileNode
}

func (fn *FileNode) Empty() bool {
return bool(C.FileNode_Empty(fn.p))
}

func (fn *FileNode) IsInt() bool {
return bool(C.FileNode_IsInt(fn.p))
}

func (fn *FileNode) IsMap() bool {
return bool(C.FileNode_IsMap(fn.p))
}

func (fn *FileNode) IsNamed() bool {
return bool(C.FileNode_IsNamed(fn.p))
}

func (fn *FileNode) IsNone() bool {
return bool(C.FileNode_IsNone(fn.p))
}

func (fn *FileNode) IsReal() bool {
return bool(C.FileNode_IsReal(fn.p))
}

func (fn *FileNode) IsSeq() bool {
return bool(C.FileNode_IsSeq(fn.p))
}

func (fn *FileNode) IsString() bool {
return bool(C.FileNode_IsString(fn.p))
}

func (fn *FileNode) Keys() []string {

c_keys_count := C.FileNode_KeysCount(fn.p)
c_keys := C.FileNode_Keys(fn.p)
defer C.FileNode_KeysFree(c_keys, c_keys_count)

keys := make([]string, int(c_keys_count))
c_keys_slice := unsafe.Slice(c_keys, c_keys_count)

for i := 0; i < int(c_keys_count); i++ {
keys[i] = C.GoString(c_keys_slice[i])
}
return keys
}

func (fn *FileNode) Close() {
C.FileNode_Close(fn.p)
}
113 changes: 113 additions & 0 deletions persistence_filestorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <string.h>
#include "persistence.h"


FileStorage FileStorage_Create(void) {
return new cv::FileStorage();
}

FileStorage FileStorage_CreateWithParams(const char* filename, int flags, const char* encoding) {
return new cv::FileStorage(filename, flags, encoding);
}

const char *FileStorage_ElName(FileStorage fs) {
char* str = new char[fs->elname.length()+1];
strcpy(str, fs->elname.c_str());
return str;
}
int FileStorage_State(FileStorage fs) {
return fs->state;
}

void FileStorage_Close(FileStorage fs) {
fs->release();
delete fs;
}

void FileStorage_EndWriteStruct(FileStorage fs) {
fs->endWriteStruct();
}

int FileStorage_GetFormat(FileStorage fs){
return fs->getFormat();
}

bool FileStorage_IsOpened(FileStorage fs) {
return fs->isOpened();
}

bool FileStorage_Open(FileStorage fs, const char* filename, int flags, const char* encoding) {
return fs->open(filename, flags, encoding);
}

void FileStorage_Release(FileStorage fs) {
fs->release();
delete fs;
}

const char* FileStorage_ReleaseAndGetString(FileStorage fs) {
cv::String s = fs->releaseAndGetString();

char* str = new char[s.length()+1];
strcpy(str, s.c_str());
return str;
}

void FileStorage_StartWriteStruct(FileStorage fs, const char* name, int flags, const char* typeName){
fs->startWriteStruct(name, flags, typeName);
}

void FileStorage_WriteMat(FileStorage fs, const char* name, Mat val){
fs->write(name, *val);
}

void FileStorage_WriteString(FileStorage fs, const char* name, const char* val) {
fs->write(name, val);
}

void FileStorage_WriteStringArray(FileStorage fs, const char* name, const char** val, size_t len) {
std::vector<cv::String> vals;

for(int i = 0; i < len; i++) {
vals.push_back(val[i]);
}

fs->write(name, vals);
}

void FileStorage_WriteDouble(FileStorage fs, const char* name, double val){
fs->write(name, val);
}

void FileStorage_WriteInt(FileStorage fs, const char* name, int val){
fs->write(name, val);
}

void FileStorage_WriteComment(FileStorage fs, const char* comment, bool append){
fs->writeComment(comment, append);
}
void FileStorage_WriteRaw(FileStorage fs, const char* fmt, const void* vec, size_t len){
fs->writeRaw(fmt, vec, len);
}

FileNode FileStorage_GetFirstTopLevelNode(FileStorage fs) {
cv::FileNode node = fs->getFirstTopLevelNode();

FileNode fn = new cv::FileNode(node);
return fn;
}

FileNode FileStorage_GetNode(FileStorage fs, const char* nodename) {

cv::FileNode node = (*fs)[nodename];

FileNode fn = new cv::FileNode(node);
return fn;
}

FileNode FileStorage_Root(FileStorage fs, int streamidx) {
cv::FileNode node = fs->root(streamidx);

FileNode fn = new cv::FileNode(node);
return fn;
}
Loading

0 comments on commit c48459c

Please sign in to comment.