feat/api #46

Open
Gregor Weiss wants to merge 56 commits from feat/api into main
4 changed files with 49 additions and 113 deletions
Showing only changes of commit 0673dafb64 - Show all commits

View file

@ -92,7 +92,7 @@
\************************************************************************************************/
uint64 bytes_used (bitstream const *const stream);
//==========|==========================|======================|======|======|=====================
bitstream* init_stream (uchar *const memory,
bitstream* init_bitstream (uchar *const memory,
uint32 const size,
char const instr);
//==========|==========================|======================|======|======|=====================

View file

@ -107,8 +107,6 @@ bytes_used(bitstream const *const stream)
}
/*----------------------------------------------------------------------------------------------------------*\
! FUNCTION NAME: bitstream* bwc_init_stream(uchar* memory, uint32 size, char instr) !
! -------------- !
! !
! DESCRIPTION: !
! ------------ !
@ -145,7 +143,7 @@ bytes_used(bitstream const *const stream)
! !
\*----------------------------------------------------------------------------------------------------------*/
bitstream*
init_stream(uchar* memory, uint32 size, char instr)
init_bitstream(uchar* memory, uint32 size, char instr)
{
/*-----------------------*\
! DEFINE STRUCTS: !
@ -155,6 +153,7 @@ init_stream(uchar* memory, uint32 size, char instr)
/*-----------------------*\
! DEFINE ASSERTIONS: !
\*-----------------------*/
assert(memory);
assert(instr == 'c' || instr == 'd');
/*--------------------------------------------------------*\
@ -168,39 +167,11 @@ init_stream(uchar* memory, uint32 size, char instr)
return NULL;
}
// TODO: Here is a problem. Memory might be allocated here or coming from outside.
// Violates RAII. Can lead to problem in terminate_stream.
/*--------------------------------------------------------*\
! Evaluate if a valid memory handle has been passed by the !
! function caller. !
\*--------------------------------------------------------*/
if(!memory)
{
/*--------------------------------------------------------*\
! If no valid memory handle has been passed, allocate a !
! memory block with the specifiec stream size. !
\*--------------------------------------------------------*/
stream->memory = calloc(size, sizeof(uchar));
if(!stream->memory)
{
// memory allocation error
fprintf(stderr, MEMERROR);
return NULL;
}
}
else
{
/*--------------------------------------------------------*\
! If a valid memory handle has been passed for decoding, !
! save the memory handle in the bwc stream structure. !
\*--------------------------------------------------------*/
stream->memory = memory;
}
/*--------------------------------------------------------*\
! Initialize the byte buffer counter, stream size and size !
! increment for the current stream. !
\*--------------------------------------------------------*/
stream->memory = memory;
stream->t = (instr == 'c') ? 8 : 0;
stream->Lmax = size;
stream->size_incr = (uint64)(size / 2);
@ -249,11 +220,6 @@ init_stream(uchar* memory, uint32 size, char instr)
void
emit_chunck(bitstream *const stream, const uchar* chunck, const uint64 size)
{
/*-----------------------*\
! DEFINE INT VARIABLES: !
\*-----------------------*/
uint64 Lreq;
/*-----------------------*\
! DEFINE ASSERTIONS: !
\*-----------------------*/
@ -261,54 +227,30 @@ emit_chunck(bitstream *const stream, const uchar* chunck, const uint64 size)
assert(chunck);
/*--------------------------------------------------------*\
! Evaluate the memory block size if the current chunck of !
! data is written to the stream. !
! Check if an error was encountered in a previous writing !
! operation !
\*--------------------------------------------------------*/
Lreq = (bytes_used(stream) + size);
/*--------------------------------------------------------*\
! Check if the enough memory has been allocated for the !
! stream to store the additional data chunck. !
\*--------------------------------------------------------*/
if(Lreq > stream->Lmax)
if(!stream->error)
{
/*--------------------------------------------------------*\
! If the stream is not large enough, check if this is due !
! to an error encountered in a previous writing operation !
! Check if the enough memory has been allocated for the !
! stream to store the additional symbol. !
\*--------------------------------------------------------*/
if(!stream->error)
if((bytes_used(stream) + size) > stream->Lmax)
{
/*--------------------------------------------------------*\
! If the error flag is not set, increase the stream size !
! until it is large enough to store the additional data !
! chunck. !
\*--------------------------------------------------------*/
while(Lreq > stream->Lmax)
{
stream->Lmax += stream->size_incr + size;
stream->size_incr = (uint64)(stream->Lmax / 2);
}
/*--------------------------------------------------------*\
! Reallocate the stream data block. !
\*--------------------------------------------------------*/
stream->memory = realloc(stream->memory, stream->Lmax);
if(!stream->memory)
{
// memory allocation error
stream->error |= 1;
stream->Lmax = 0;
return;
}
}
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
// memory allocation error
stream->error |= 1;
stream->Lmax = 0;
return;
}
}
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
return;
}
/*--------------------------------------------------------*\
! Copy the additional data to the stream memory block. !
@ -372,44 +314,30 @@ emit_symbol(bitstream *const stream, const uint64 symbol, const uint8 size)
assert(stream);
/*--------------------------------------------------------*\
! Check if the enough memory has been allocated for the !
! stream to store the additional symbol. !
! Check if an error was encountered in a previous writing !
! operation !
\*--------------------------------------------------------*/
if((bytes_used(stream) + size) > stream->Lmax)
if(!stream->error)
{
/*--------------------------------------------------------*\
! If the stream is not large enough, check if this is due !
! to an error encountered in a previous writing operation !
! Check if the enough memory has been allocated for the !
! stream to store the additional symbol. !
\*--------------------------------------------------------*/
if(!stream->error)
if((bytes_used(stream) + size) > stream->Lmax)
{
/*--------------------------------------------------------*\
! If the error flag is not set, increment the stream size !
! to store the additional symbol. !
\*--------------------------------------------------------*/
stream->Lmax += stream->size_incr;
stream->size_incr = (uint64)(stream->Lmax / 2);
/*--------------------------------------------------------*\
! Reallocate the stream data block. !
\*--------------------------------------------------------*/
stream->memory = realloc(stream->memory, stream->Lmax);
if(!stream->memory)
{
// memory allocation error
stream->error |= 1;
stream->Lmax = 0;
return;
}
}
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
// memory allocation error
stream->error |= 1;
stream->Lmax = 0;
return;
}
}
else
{
/*--------------------------------------------------------*\
! Exit to function caller if error flag has been set. !
\*--------------------------------------------------------*/
return;
}
/*--------------------------------------------------------*\
! Copy the additional symbol to the stream memory block !

View file

@ -1395,7 +1395,7 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
! Initialize the final codestream and emit the header !
! bytes. !
\*--------------------------------------------------------*/
stream = init_stream(data->out, control->codestreamSize, 'c');
stream = init_bitstream(data->out, control->codestreamSize, 'c');
codestream_write_header(stream, field, data);
/*--------------------------------------------------------*\
@ -1414,7 +1414,7 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
assemble_tile(field, tile, stream);
}
emit_symbol(stream, EOC, 2);
emit_symbol(&(*stream), EOC, 2);
packed_stream = calloc(1, sizeof(bwc_span));
if(!packed_stream)
@ -1484,7 +1484,7 @@ parse_codestream(bwc_codec *const codec, bwc_stream *const data, uint8 const lay
! Initialize a bitstream used to parse the packed code- !
! stream. !
\*--------------------------------------------------------*/
stream = init_stream(data->inp, 10, 'd');
stream = init_bitstream(data->inp, 10, 'd');
/*--------------------------------------------------------*\
! Parse the main header and set up the codec structure for !

View file

@ -450,6 +450,7 @@ create_packet(bwc_codec *const field, bwc_tile *const tile,
int16 *cp_contr;
int16 delta_z;
int8 m;
uchar *memory;
/*-----------------------*\
! DEFINE STRUCTS: !
@ -482,7 +483,14 @@ create_packet(bwc_codec *const field, bwc_tile *const tile,
! Initialize the stream that is used to assemble the pack- !
! et header. !
\*--------------------------------------------------------*/
header = init_stream(NULL, PACKET_HEADER_SIZE, 'c');
memory = calloc(PACKET_HEADER_SIZE, sizeof(uchar));
if(!memory)
{
// memory allocation error
fprintf(stderr, MEMERROR);
return -1;
}
header = init_bitstream(memory, PACKET_HEADER_SIZE, 'c');
if(!header)
{
// memory allocation error
@ -1392,7 +1400,7 @@ parse_packet(bwc_codec *const field, bwc_tile *const tile,
! Initialize the stream that is used to parse the packet !
! codestream. !
\*--------------------------------------------------------*/
header = init_stream(packet->header.memory, body_size, 'd');
header = init_bitstream(packet->header.memory, body_size, 'd');
if(!header)
{
// memory allocation error