| 4453 | |
| 4454 | |
| 4455 | static void |
| 4456 | icvWriteGraph( CvFileStorage* fs, const char* name, |
| 4457 | const void* struct_ptr, CvAttrList attr ) |
| 4458 | { |
| 4459 | int* flag_buf = 0; |
| 4460 | char* write_buf = 0; |
| 4461 | const CvGraph* graph = (const CvGraph*)struct_ptr; |
| 4462 | CvSeqReader reader; |
| 4463 | char buf[128]; |
| 4464 | int i, k, vtx_count, edge_count; |
| 4465 | char vtx_dt_buf[128], *vtx_dt; |
| 4466 | char edge_dt_buf[128], *edge_dt; |
| 4467 | int write_buf_size; |
| 4468 | |
| 4469 | assert( CV_IS_GRAPH(graph) ); |
| 4470 | vtx_count = cvGraphGetVtxCount( graph ); |
| 4471 | edge_count = cvGraphGetEdgeCount( graph ); |
| 4472 | flag_buf = (int*)cvAlloc( vtx_count*sizeof(flag_buf[0])); |
| 4473 | |
| 4474 | // count vertices |
| 4475 | cvStartReadSeq( (CvSeq*)graph, &reader ); |
| 4476 | for( i = 0, k = 0; i < graph->total; i++ ) |
| 4477 | { |
| 4478 | if( CV_IS_SET_ELEM( reader.ptr )) |
| 4479 | { |
| 4480 | CvGraphVtx* vtx = (CvGraphVtx*)reader.ptr; |
| 4481 | flag_buf[k] = vtx->flags; |
| 4482 | vtx->flags = k++; |
| 4483 | } |
| 4484 | CV_NEXT_SEQ_ELEM( graph->elem_size, reader ); |
| 4485 | } |
| 4486 | |
| 4487 | // write header |
| 4488 | cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_GRAPH ); |
| 4489 | |
| 4490 | cvWriteString(fs, "flags", CV_IS_GRAPH_ORIENTED(graph) ? "oriented" : "", 1); |
| 4491 | |
| 4492 | cvWriteInt( fs, "vertex_count", vtx_count ); |
| 4493 | vtx_dt = icvGetFormat( (CvSeq*)graph, "vertex_dt", |
| 4494 | &attr, sizeof(CvGraphVtx), vtx_dt_buf ); |
| 4495 | if( vtx_dt ) |
| 4496 | cvWriteString( fs, "vertex_dt", vtx_dt, 0 ); |
| 4497 | |
| 4498 | cvWriteInt( fs, "edge_count", edge_count ); |
| 4499 | edge_dt = icvGetFormat( (CvSeq*)graph->edges, "edge_dt", |
| 4500 | &attr, sizeof(CvGraphEdge), buf ); |
| 4501 | sprintf( edge_dt_buf, "2if%s", edge_dt ? edge_dt : "" ); |
| 4502 | edge_dt = edge_dt_buf; |
| 4503 | cvWriteString( fs, "edge_dt", edge_dt, 0 ); |
| 4504 | |
| 4505 | icvWriteHeaderData( fs, (CvSeq*)graph, &attr, sizeof(CvGraph) ); |
| 4506 | |
| 4507 | write_buf_size = MAX( 3*graph->elem_size, 1 << 16 ); |
| 4508 | write_buf_size = MAX( 3*graph->edges->elem_size, write_buf_size ); |
| 4509 | write_buf = (char*)cvAlloc( write_buf_size ); |
| 4510 | |
| 4511 | // as vertices and edges are written in similar way, |
| 4512 | // do it as a parametrized 2-iteration loop |
nothing calls this directly
no test coverage detected