Memo‎ > ‎

Use libSDF to read SDF format on Windows

posted Mar 20, 2015, 9:46 AM by Teng-Yok Lee   [ updated Mar 20, 2015, 9:47 AM ]
This is my memo to read the data for IEEE SciVis 2015 Contest. The file is in SDF format. There is a C/C++ library libSDF to open the files, but some instructions are unclear, especially for the porting on Windows. Also, I cannot find examples about its usage so I write one:

Prerequisites

To build libSDF for Visual Studio 2010, I use cygwin and mingw x64. The procedure to install them can be seen here:

http://www.recheliu.org/memo/suggestionstoavoidarpackcompilationerrors

Build libSDF for Windows x64 platform

  • Edit SDFfuncs.c: Remove the preprocessor USE_ALLOCA.
  • Edit utils.c: Change the function MPMU_Fopen() to open the file as binary (otherwise not all bytes can be read):
MPMYFile *MPMY_Fopen(const char *path, int mpmy_flags)
{
    MPMYFile *fp;
    int iomode = MPMY_SINGL;
    char mode[8] = {[0] = 'r'};

    Msgf("Fopen %s\n", path);
    if (mpmy_flags & MPMY_RDONLY) mode[0] = 'r'; /* MPMY_RDONLY is 0 since O_RDONLY is (stupidly) 0 */
    if (mpmy_flags & MPMY_WRONLY) mode[0] = 'w';
    if (mpmy_flags & MPMY_APPEND) mode[0] = 'a';
    // TEST-ADD-BEGIN
    switch(mode[0]) {
    case 'r':
        strcpy(mode, "r+b");
        break;
    case 'w':
        strcpy(mode, "w+b");
        break;
    case 'a':
        strcpy(mode, "a+b");
        break;
    }
    // TEST-ADD-END
    ...

  • Edit Makefile: Change CC from gcc to the one for mingw.
CC=/usr/bin/x86_64-w64-mingw32-gcc.exe
  • Use cygwin to build the library.
$ make libSDF.a
$ /usr/bin/x86_64-w64-mingw32-dllwrap.exe --export-all-symbols *.o -lm --output-def libSDF_x64.def -o libSDF_x64.dll

  • Use Visual Studio (64-bit) Command Prompt to build the .dll

lib.exe /machine:X64 /def:libSDF_x64.def

My quick example to read the array "x"

    #define LOG_VAR(x) cout<<x<<endl;

    char* szSdfFilepath = "F:/data/viscontest/scivis2015/ds14_scivis_0128_e4_dt04_0.0200";
    LOG_VAR(SDFissdf(szSdfFilepath));

    SDF *sdf = SDFopen(szSdfFilepath, "");
    SDFdebug(1); // Put it 0 to disable debug information.

    int64_t uNrOfRecs = SDFnrecs("x", sdf);
    LOG_VAR(uNrOfRecs);
    vector<float> vfData(uNrOfRecs);
    SDFrdvecs(sdf, "x", uNrOfRecs, vfData.data(), 0, NULL);
    SDFclose(sdf);

    for(size_t i = 0; i < uNrOfRecs; i++)
    {
        if( 0.0f != vfData[i] )
        {
            LOG_VAR(vfData[i]);
        }
    }


Comments