Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to read ILD files from plain data #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions liblzr/liblzr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ class ILDA;
//Will return NULL on failure
LIBLZR_EXPORT ILDA* ilda_open(const char* filename, const char* mode);

// Read ILDA from plain data
//Will return NULL on failure
LIBLZR_EXPORT ILDA* ilda_open(const char* data, size_t size);

//closes the ILDA file, and releases the parsing context
LIBLZR_EXPORT int ilda_close(ILDA* f);

Expand Down
90 changes: 77 additions & 13 deletions liblzr/src/ilda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ ILDA* ilda_open(const char* filename, const char* mode)
return ilda;
}

ILDA* ilda_open(const char* data, size_t size)
{
ILDA* ilda = new ILDA();
ilda->read = true;
ilda->data = data;
ilda->data_size = size;

if(!data || size == 0)
{
perror("Invalid data options");
delete ilda;
return NULL;
}

if(init_frame_counts(ilda) != ILDA_CONTINUE)
{
perror("Failed to initialize frame counts");
delete ilda;
return NULL;
}

return ilda;
}

int ilda_close(ILDA* ilda)
{
Expand All @@ -69,7 +92,9 @@ int ilda_close(ILDA* ilda)
if(!ilda->read)
status = write_finish(ilda);

fclose(ilda->f);
if(ilda->f)
fclose(ilda->f);

delete ilda;

return ERROR_TO_LZR(status);
Expand Down Expand Up @@ -120,6 +145,10 @@ const char* ilda_error(ILDA* ilda)

ILDA::ILDA()
: f(nullptr)
, read(true)
, data(nullptr)
, data_size(0)
, data_index(0)
, error("")
{
//wipe the projector data (color and frame arrays)
Expand Down Expand Up @@ -179,18 +208,36 @@ int read_header(ILDA* ilda)
static_assert(sizeof(ilda_header) == 32, "ILDA header is not 32 bytes!");

//read one ILDA header
size_t r = fread((void*) &(ilda->h),
1,
sizeof(ilda_header),
ilda->f);
if(ilda->f)
{
size_t r = fread((void*) &(ilda->h),
1,
sizeof(ilda_header),
ilda->f);

//did we capture a full header?
if(r != sizeof(ilda_header))
{
if(r > 0)
ilda->error = "Encountered incomplete ILDA section header";

//did we capture a full header?
if(r != sizeof(ilda_header))
return ILDA_ERROR;
}
}
else if(ilda->data)
{
if(r > 0)
// check if data is available for header
if(ilda->data_index + sizeof(ilda_header) >= ilda->data_size)
{
ilda->error = "Encountered incomplete ILDA section header";
return ILDA_ERROR;
}

return ILDA_ERROR;
memcpy((void*) &(ilda->h),
(const void*) &(ilda->data[ilda->data_index]),
sizeof(ilda_header));

ilda->data_index += sizeof(ilda_header);
}

//is it prefixed with "ILDA"?
Expand Down Expand Up @@ -232,7 +279,17 @@ int skip_to_next_section(ILDA* ilda)

skip_bytes *= ilda->h.number_of_records;

if(fseek(ilda->f, skip_bytes, SEEK_CUR) != 0)
bool is_skip_error = false;
if(ilda->f)
{
is_skip_error = fseek(ilda->f, skip_bytes, SEEK_CUR) != 0;
}
else if(ilda->data)
{
ilda->data_index += skip_bytes;
is_skip_error = (ilda->data_index >= ilda->data_size);
}
if(is_skip_error)
{
ilda->error = "Failed to skip to next section";
return ILDA_ERROR;
Expand All @@ -244,10 +301,17 @@ int skip_to_next_section(ILDA* ilda)
int seek_to_start(ILDA* ilda)
{
//seek to the start of the file
if(fseek(ilda->f, 0, SEEK_SET) != 0)
if(ilda->f)
{
ilda->error = "Failed to seek to start of file";
return ILDA_ERROR;
if(fseek(ilda->f, 0, SEEK_SET) != 0)
{
ilda->error = "Failed to seek to start of file";
return ILDA_ERROR;
}
}
else if(ilda->data)
{
ilda->data_index = 0;
}

return ILDA_CONTINUE;
Expand Down
5 changes: 5 additions & 0 deletions liblzr/src/ilda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ class ILDA

FILE* f; // the current file
bool read; // if false, we're in write mode

const char* data; // data
size_t data_size; // data size
size_t data_index; // data current index

const char* error; // error string

ILDA_Projector* current_projector();
Expand Down
27 changes: 22 additions & 5 deletions liblzr/src/ilda_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ typedef int (*point_reader)(ILDA* ilda, Point& p);
//reads a single record, and error checks the size
static int read_record(ILDA* ilda, void* buffer, size_t buffer_size)
{
size_t r = fread(buffer, 1, buffer_size, ilda->f);
if(ilda->f)
{
size_t r = fread(buffer, 1, buffer_size, ilda->f);

//make sure we got everything...
if(r != buffer_size)
//make sure we got everything...
if(r != buffer_size)
{
ilda->error = "Encountered incomplete record";
return ILDA_ERROR;
}
}
else if(ilda->data)
{
ilda->error = "Encountered incomplete record";
return ILDA_ERROR;
// check if data is available for record
if(ilda->data_index + buffer_size >= ilda->data_size)
{
ilda->error = "Encountered incomplete record";
return ILDA_ERROR;
}

memcpy(buffer,
(const void*) &(ilda->data[ilda->data_index]),
buffer_size);
ilda->data_index += buffer_size;
}

return ILDA_CONTINUE;
Expand Down