Modifications to increase modularity of jp2 coding/decoding

This commit is contained in:
Francois-Olivier Devaux 2004-11-26 16:33:18 +00:00
parent 1bdb07cefe
commit 0ddef9b2db
4 changed files with 86 additions and 60 deletions

View File

@ -318,7 +318,8 @@ int main(int argc, char **argv)
int ir = 0; int ir = 0;
int res_spec = 0; /* For various precinct sizes specification */ int res_spec = 0; /* For various precinct sizes specification */
char sep; char sep;
char *outbuf; char *j2k_codestream;
char *jp2_codestream;
FILE *f; FILE *f;
@ -848,10 +849,11 @@ int main(int argc, char **argv)
return 1; return 1;
} }
} else { } else {
outbuf = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2); /* Allocate memory for all tiles */ j2k_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
cio_init(outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2); /* Allocate memory for all tiles */
cio_init(j2k_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len = len =
j2k_encode(&img, &cp, outbuf, j2k_encode(&img, &cp, j2k_codestream,
cp.tdx * cp.tdy * cp.tw * cp.th * 2, index); cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
if (len == 0) { if (len == 0) {
fprintf(stderr, "failed to encode image\n"); fprintf(stderr, "failed to encode image\n");
@ -862,8 +864,8 @@ int main(int argc, char **argv)
fprintf(stderr, "failed to open %s for writing\n", outfile); fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1; return 1;
} }
fwrite(outbuf, 1, len, f); fwrite(j2k_codestream, 1, len, f);
free(outbuf); free(j2k_codestream);
fclose(f); fclose(f);
} }
} else { /* JP2 format output */ } else { /* JP2 format output */
@ -873,30 +875,40 @@ int main(int argc, char **argv)
jp2_struct->image = &img; jp2_struct->image = &img;
/* Initialising the standard JP2 box content */ /* Initialising the standard JP2 box content */
/* If you wish to modify those boxes, you have to modify the jp2_struct content */ /* If you wish to modify those boxes, you have to modify
the jp2_struct content */
if (jp2_init_stdjp2(jp2_struct, &img)) { if (jp2_init_stdjp2(jp2_struct, &img)) {
fprintf(stderr, "Error with jp2 initialization"); fprintf(stderr, "Error with jp2 initialization");
return 1; return 1;
}; };
if (cp.intermed_file == 1) { if (cp.intermed_file == 1) {
/*For the moment, JP2 format does not use intermediary files for each tile */ /*For the moment, JP2 format does not use intermediary
files for each tile */
cp.intermed_file = 0; cp.intermed_file = 0;
} }
outbuf = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2); j2k_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
cio_init(outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2); jp2_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len = jp2_encode(jp2_struct, &cp, outbuf, index);
cio_init(j2k_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len = j2k_encode(&img, &cp, j2k_codestream,
cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
if (len == 0) { if (len == 0) {
fprintf(stderr, "failed to encode image\n"); fprintf(stderr, "failed to encode image\n");
return 1; return 1;
} }
jp2_struct->j2k_codestream_len = len;
cio_init(jp2_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len = jp2_wrap_j2k(jp2_struct, j2k_codestream, jp2_codestream);
f = fopen(outfile, "wb"); f = fopen(outfile, "wb");
if (!f) { if (!f) {
fprintf(stderr, "failed to open %s for writing\n", outfile); fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1; return 1;
} }
fwrite(outbuf, 1, len, f); fwrite(jp2_codestream, 1, len, f);
free(outbuf); free(jp2_codestream);
free(j2k_codestream);
fclose(f); fclose(f);
} }

View File

@ -25,12 +25,20 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
//MEMORY LEAK //MEMORY LEAK
#ifdef _DEBUG #ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC #define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first #include <stdlib.h> // Must be included first
#include <crtdbg.h> #include <crtdbg.h>
#endif #endif
//MEM //MEM
@ -147,6 +155,7 @@ int main(int argc, char **argv)
src_name--; src_name--;
S1 = *src_name; S1 = *src_name;
/* J2K format */ /* J2K format */
if ((S1 == 'j' && S2 == '2' && S3 == 'k') if ((S1 == 'j' && S2 == '2' && S3 == 'k')
|| (S1 == 'J' && S2 == '2' && S3 == 'K') || (S1 == 'j' && S2 == '2' || (S1 == 'J' && S2 == '2' && S3 == 'K') || (S1 == 'j' && S2 == '2'
@ -165,7 +174,12 @@ int main(int argc, char **argv)
jp2_struct->image = &img; jp2_struct->image = &img;
if (jp2_decode(src, len, jp2_struct, &cp)) { if (jp2_read_struct(src, jp2_struct, len)) {
fprintf(stderr, "j2k_to_image: failed to decode jp2 structure!\n");
return 1;
}
if (!j2k_decode(src + jp2_struct->j2k_codestream_offset, jp2_struct->j2k_codestream_len, &img, &cp)) {
fprintf(stderr, "j2k_to_image: failed to decode image!\n"); fprintf(stderr, "j2k_to_image: failed to decode image!\n");
return 1; return 1;
} }
@ -561,12 +575,20 @@ int main(int argc, char **argv)
break; break;
} }
j2k_dec_release(); j2k_dec_release();
//MEMORY LEAK //MEMORY LEAK
#ifdef _DEBUG #ifdef _DEBUG
_CrtDumpMemoryLeaks(); _CrtDumpMemoryLeaks();
#endif #endif
//MEM //MEM
return 0; return 0;

View File

@ -65,6 +65,11 @@ int jp2_read_boxhdr(jp2_box_t * box)
return 1; return 1;
}; };
box->length = cio_read(4); box->length = cio_read(4);
if (box->length == 0)
box->length = cio_numbytesleft() + 12;
}
else if (box->length == 0) {
box->length = cio_numbytesleft() + 8;
} }
return 0; return 0;
} }
@ -424,28 +429,27 @@ int jp2_read_ftyp(jp2_struct_t * jp2_struct)
return 0; return 0;
} }
int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer, int jp2_write_jp2c(jp2_struct_t * jp2_struct, char *j2k_codestream)
char *index)
{ {
int len;
jp2_box_t box; jp2_box_t box;
box.init_pos = cio_tell(); box.init_pos = cio_tell();
cio_skip(4); cio_skip(4);
cio_write(JP2_JP2C, 4); // JP2C cio_write(JP2_JP2C, 4); // JP2C
len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * cp->th * cp->tw * 2, index); jp2_struct->j2k_codestream_offset = cio_tell();
memcpy(cio_getbp(),j2k_codestream, jp2_struct->j2k_codestream_len);
box.length = cio_tell() - box.init_pos; box.length = 8 + jp2_struct->j2k_codestream_len;
cio_seek(box.init_pos); cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */ cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length); cio_seek(box.init_pos + box.length);
return box.length; return box.length;
} }
int jp2_read_jp2c(unsigned char *src, int len, jp2_struct_t * jp2_struct, int jp2_read_jp2c(unsigned char *src, jp2_struct_t * jp2_struct)
j2k_cp_t * cp)
{ {
jp2_box_t box; jp2_box_t box;
@ -455,12 +459,8 @@ int jp2_read_jp2c(unsigned char *src, int len, jp2_struct_t * jp2_struct,
return 1; return 1;
} }
src += cio_tell(); jp2_struct->j2k_codestream_offset = cio_tell();
jp2_struct->j2k_codestream_len = box.length - 8;
if (j2k_decode(src, len, jp2_struct->image, cp) == 0) {
fprintf(stderr, "JP2F box: failed to decode J2K bitstream image!\n");
return 1;
}
return 0; return 0;
} }
@ -508,8 +508,8 @@ int jp2_read_jp()
} }
int jp2_decode(unsigned char *src, int len, jp2_struct_t * jp2_struct,
j2k_cp_t * cp) int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
{ {
cio_init(src, len); cio_init(src, len);
@ -519,20 +519,17 @@ int jp2_decode(unsigned char *src, int len, jp2_struct_t * jp2_struct,
return 1; return 1;
if (jp2_read_jp2h(jp2_struct)) if (jp2_read_jp2h(jp2_struct))
return 1; return 1;
if (jp2_read_jp2c(src, len, jp2_struct, cp)) if (jp2_read_jp2c(src, jp2_struct))
return 1; return 1;
return 0; return 0;
} }
int jp2_encode(jp2_struct_t * jp2_struct, j2k_cp_t * cp, char *output, int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, char *output)
char *index)
{ {
int len;
jp2_write_jp(); jp2_write_jp();
jp2_write_ftyp(jp2_struct); jp2_write_ftyp(jp2_struct);
jp2_write_jp2h(jp2_struct); jp2_write_jp2h(jp2_struct);
len = jp2_write_jp2c(jp2_struct->image, cp, output, index); jp2_write_jp2c(jp2_struct, j2k_codestream);
return cio_tell(); return cio_tell();
} }

View File

@ -53,6 +53,8 @@ typedef struct {
unsigned int *cl; unsigned int *cl;
jp2_comps_t *comps; jp2_comps_t *comps;
j2k_image_t *image; j2k_image_t *image;
unsigned int j2k_codestream_offset;
unsigned int j2k_codestream_len;
} jp2_struct_t; } jp2_struct_t;
typedef struct { typedef struct {
@ -69,17 +71,13 @@ typedef struct {
*/ */
int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img); int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img);
/* int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer, /* int jp2_write_jp2c(char *j2k_codestream, int j2k_len);
* char *index);
* *
* Write the jp2c codestream box * Write the jp2c codestream box
* img: the j2k_image that will be compressed * jp2_struct: the jp2 structure you are working with
* jp2_buffer: the buffer that will recieve the compressed data * j2k_codestream: the j2k codestream to include in jp2 file
* index: the name of the index file
*/ */
int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer, int jp2_write_jp2c(jp2_struct_t * jp2_struct, char *j2k_codestream);
char *index);
/* int jp2_write_jp2h(jp2_struct_t * jp2_struct); /* int jp2_write_jp2h(jp2_struct_t * jp2_struct);
* *
@ -95,28 +93,25 @@ void jp2_write_jp2h(jp2_struct_t * jp2_struct);
*/ */
int jp2_read_jp2h(jp2_struct_t * jp2_struct); int jp2_read_jp2h(jp2_struct_t * jp2_struct);
/* int jp2_encode(jp2_struct_t * jp2_struct, j2k_cp_t * cp, char *output, /* int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
* char *index); int j2k_len, char *output)
* *
* Encode a JP2 file * Wrap a J2K codestream in a JP2 file
* jp2_buffer: the buffer containing the pointer to the image to encode * jp2_struct: the jp2 structure used to create jp2 boxes
* cp: coding parameters of the image * j2k_codestream: the j2k codestream to include in jp2 file
* outbuf: pointer to memory where compressed data will be written * output: pointer to jp2 codestream that will be created
* index: the name of the index file
*/ */
int jp2_encode(jp2_struct_t * jp2_struct, j2k_cp_t * cp, char *output, int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
char *index); char *output);
/* int jp2_decode(unsigned char *src, int len, jp2_struct_t * jp2_struct,
* j2k_cp_t * cp); /* int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct);
* *
* Decode a JP2 file * Decode the structure of a JP2 file
* src: pointer to memory where compressed data is stored * src: pointer to memory where compressed data is stored
* len: length of src buffer
* jp2_struct: the jp2 structure that will be created * jp2_struct: the jp2 structure that will be created
* cp: coding parameters of the image * len: length of jp2 codestream
*/ */
int jp2_decode(unsigned char *src, int len, jp2_struct_t * jp2_struct, int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len);
j2k_cp_t * cp);
#endif #endif