Skip to content

Commit

Permalink
Modified support for comments. The library supports both text and bin…
Browse files Browse the repository at this point in the history
…ary comments while kdu_compress supports text only. Multiple comments are supported.
  • Loading branch information
aous72 committed Jun 5, 2023
1 parent 3fe0fce commit bfa556c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 64 deletions.
8 changes: 3 additions & 5 deletions src/apps/ojph_compress/ojph_compress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,14 +928,12 @@ int main(int argc, char * argv[]) {
"Please supply a proper input filename with a proper three-letter "
"extension\n");

// add the command-line specified COM marker

ojph::comment_exchange com_ex;
if (com_string)
codestream.set_user_com(com_string);

com_ex.set_string(com_string);
ojph::j2c_outfile j2c_file;
j2c_file.open(output_filename);
codestream.write_headers(&j2c_file);
codestream.write_headers(&j2c_file, &com_ex, com_string ? 1 : 0);

ojph::ui32 next_comp;
ojph::line_buf* cur_line = codestream.exchange(NULL, next_comp);
Expand Down
12 changes: 4 additions & 8 deletions src/core/codestream/ojph_codestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ namespace ojph {
return param_qcd(&state->qcd);
}

////////////////////////////////////////////////////////////////////////////
void codestream::set_user_com(const char *s)
{
state->set_user_com(s);
}

////////////////////////////////////////////////////////////////////////////
void codestream::set_planar(bool planar)
{
Expand Down Expand Up @@ -127,9 +121,11 @@ namespace ojph {
}

////////////////////////////////////////////////////////////////////////////
void codestream::write_headers(outfile_base *file)
void codestream::write_headers(outfile_base *file,
const comment_exchange* comments,
ui32 num_comments)
{
state->write_headers(file);
state->write_headers(file, comments, num_comments);
}

////////////////////////////////////////////////////////////////////////////
Expand Down
67 changes: 22 additions & 45 deletions src/core/codestream/ojph_codestream_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ namespace ojph {
profile = OJPH_PN_UNDEFINED;
tilepart_div = OJPH_TILEPART_NODIVSIONS;
need_tlm = false;
user_com = NULL;

cur_comp = 0;
cur_line = 0;
Expand All @@ -93,8 +92,6 @@ namespace ojph {
////////////////////////////////////////////////////////////////////////////
codestream::~codestream()
{
if (user_com)
free(user_com);
if (qcc_store != qcc)
delete[] qcc;
if (allocator)
Expand Down Expand Up @@ -552,7 +549,9 @@ namespace ojph {
}

//////////////////////////////////////////////////////////////////////////
void codestream::write_headers(outfile_base *file)
void codestream::write_headers(outfile_base *file,
const comment_exchange* comments,
ui32 num_comments)
{
//finalize
siz.check_validity();
Expand Down Expand Up @@ -653,19 +652,25 @@ namespace ojph {
if (file->write(buf, len) != len)
OJPH_ERROR(0x00030027, "Error writing to file");

t = swap_byte(JP2K_MARKER::COM);
if (file->write(&t, 2) != 2)
OJPH_ERROR(0x00030028, "Error writing to file");
len = strlen(user_com);
t = swap_byte((ui16)(len - 2));
if (file->write(&t, 2) != 2)
OJPH_ERROR(0x00030029, "Error writing to file");
//1 for General use (IS 8859-15:1999 (Latin) values)
t = swap_byte((ui16)(1));
if (file->write(&t, 2) != 2)
OJPH_ERROR(0x0003002A, "Error writing to file");
if (file->write(user_com, len) != len)
OJPH_ERROR(0x0003002B, "Error writing to file");
for (ui32 i = 0; i < num_comments; ++i)
{
char* data = comments[i].data;
ui32 Rcom = comments[i].Rcom;
ui32 len = comments[i].len;

t = swap_byte(JP2K_MARKER::COM);
if (file->write(&t, 2) != 2)
OJPH_ERROR(0x00030028, "Error writing to file");
t = swap_byte((ui16)(len + 4));
if (file->write(&t, 2) != 2)
OJPH_ERROR(0x00030029, "Error writing to file");
//1 for General use (IS 8859-15:1999 (Latin) values)
t = swap_byte(Rcom);
if (file->write(&t, 2) != 2)
OJPH_ERROR(0x0003002A, "Error writing to file");
if (file->write(data, len) != len)
OJPH_ERROR(0x0003002B, "Error writing to file");
}
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1034,34 +1039,6 @@ namespace ojph {
}
}

//////////////////////////////////////////////////////////////////////////
void codestream::set_user_com(const char *s)
{
if (user_com) {
free(user_com);
user_com = NULL;
}

if (!s)
return;

size_t src_sz = strlen(s);

if (src_sz > 65530) {
OJPH_ERROR(0x000300D0, "COM marker string length must be smaller than 65531");
return;
}

user_com = (char*) malloc(src_sz + 1);

if (!user_com) {
OJPH_ERROR(0x000300D1, "cannot allocate memory for COM marker");
return;
}

strncpy (user_com, s, src_sz + 1);
}

//////////////////////////////////////////////////////////////////////////
void codestream::set_planar(int planar)
{
Expand Down
5 changes: 2 additions & 3 deletions src/core/codestream/ojph_codestream_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ namespace ojph {
outfile_base* get_file() { return outfile; }

line_buf* exchange(line_buf* line, ui32& next_component);
void write_headers(outfile_base *file);
void write_headers(outfile_base *file, const comment_exchange* comments,
ui32 num_comments);
void enable_resilience();
bool is_resilient() { return resilient; }
void read_headers(infile_base *file);
void restrict_input_resolution(ui32 skipped_res_for_data,
ui32 skipped_res_for_recon);
void read();
void set_user_com(const char *s);
void set_planar(int planar);
void set_profile(const char *s);
void set_tilepart_divisions(ui32 value);
Expand Down Expand Up @@ -150,7 +150,6 @@ namespace ojph {
int profile;
ui32 tilepart_div; // tilepart division value
bool need_tlm; // true if tlm markers are needed
char* user_com;

private:
param_siz siz;
Expand Down
31 changes: 31 additions & 0 deletions src/core/codestream/ojph_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,37 @@ namespace ojph {
state->set_delta(delta);
}

////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
void comment_exchange::set_string(char* str)
{
size_t t = strlen(str);
if (len > 65531)
OJPH_ERROR(0x000500C1,
"COM marker string length cannot be larger than 65531");
this->data = str;
this->len = (ui16)t;
this->Rcom = 1;
}

//////////////////////////////////////////////////////////////////////////
void comment_exchange::set_data(char* data, ui16 len)
{
if (len > 65531)
OJPH_ERROR(0x000500C2,
"COM marker string length cannot be larger than 65531");
this->data = data;
this->len = len;
this->Rcom = 0;
}

//////////////////////////////////////////////////////////////////////////
//
//
Expand Down
7 changes: 4 additions & 3 deletions src/core/common/ojph_codestream.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace ojph {
class param_siz;
class param_cod;
class param_qcd;
class comment_exchange;
class mem_fixed_allocator;
struct point;
struct line_buf;
Expand All @@ -71,8 +72,6 @@ namespace ojph {
OJPH_EXPORT
~codestream();

OJPH_EXPORT
void set_user_com(const char *s);
OJPH_EXPORT
void set_planar(bool planar);
OJPH_EXPORT
Expand All @@ -83,7 +82,9 @@ namespace ojph {
void request_tlm_marker(bool needed);

OJPH_EXPORT
void write_headers(outfile_base *file);
void write_headers(outfile_base *file,
const comment_exchange* comments = NULL,
ui32 num_comments = 0);
OJPH_EXPORT
line_buf* exchange(line_buf* line, ui32& next_component);
OJPH_EXPORT
Expand Down
18 changes: 18 additions & 0 deletions src/core/common/ojph_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace ojph {
struct param_qcd;
struct param_qcc;
struct param_cap;
class codestream;
}

////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -168,6 +169,23 @@ namespace ojph {
local::param_qcd* state;
};

////////////////////////////////////////////////////////////////////////////
class comment_exchange
{
friend class local::codestream;
public:
comment_exchange() : data(NULL), len(0), Rcom(0) {}
OJPH_EXPORT
void set_string(char* str);
OJPH_EXPORT
void set_data(char* data, ui16 len);

private:
char* data;
ui16 len;
ui16 Rcom;
};

////////////////////////////////////////////////////////////////////////////
//class param_qcc
//{
Expand Down

0 comments on commit bfa556c

Please sign in to comment.