size_t write_tilemhix( size_t coff, opj_codestream_info_t cstr_info, int tileno, opj_cio_t *cio) { int i; opj_tile_info_t tile; opj_tp_info_t tp; size_t len, lenp; opj_marker_info_t *marker; lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_MHIX, 4); /* MHIX */ tile = cstr_info.tile[tileno]; tp = tile.tp[0]; cio_write( cio, tp.tp_end_header-tp.tp_start_pos+1, 8); /* TLEN */ marker = cstr_info.tile[tileno].marker; for( i=0; i<cstr_info.tile[tileno].marknum; i++){ /* Marker restricted to 1 apparition */ cio_write( cio, marker[i].type, 2); cio_write( cio, 0, 2); cio_write( cio, marker[i].pos-coff, 8); cio_write( cio, marker[i].len, 2); } /* free( marker);*/ len = cio_tell( cio) - lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); return len; }
void j2k_write_siz(void) { int i; int lenp, len; //fprintf(stderr, "%.8x: SIZ\n", cio_tell()); cio_write(J2K_MS_SIZ, 2); lenp=cio_tell(); cio_skip(2); cio_write(0, 2); cio_write(j2k_img->x1, 4); cio_write(j2k_img->y1, 4); cio_write(j2k_img->x0, 4); cio_write(j2k_img->y0, 4); cio_write(j2k_cp->tdx, 4); cio_write(j2k_cp->tdy, 4); cio_write(j2k_cp->tx0, 4); cio_write(j2k_cp->ty0, 4); cio_write(j2k_img->numcomps, 2); for (i=0; i<j2k_img->numcomps; i++) { cio_write(j2k_img->comps[i].prec-1+(j2k_img->comps[i].sgnd<<7), 1); cio_write(j2k_img->comps[i].dx, 1); cio_write(j2k_img->comps[i].dy, 1); } len=cio_tell()-lenp; cio_seek(lenp); cio_write(len, 2); cio_seek(lenp+len); }
int write_tpix_v2( int coff, opj_codestream_info_t cstr_info, int j2klen, opj_stream_private_t *cio, opj_event_mgr_t * p_manager ) { OPJ_BYTE l_data_header [4]; int len, lenp; #if 0 lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_TPIX, 4); /* TPIX */ #else lenp = opj_stream_tell(cio); opj_stream_skip(cio, 4, p_manager); opj_write_bytes(l_data_header,JPIP_TPIX,4); /* TPIX */ opj_stream_write_data(cio,l_data_header,4,p_manager); #endif write_tpixfaix_v2( coff, 0, cstr_info, j2klen, cio,p_manager); #if 0 len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); #else len = opj_stream_tell(cio)-lenp; opj_stream_skip(cio, lenp, p_manager); opj_write_bytes(l_data_header,len,4);/* L */ opj_stream_write_data(cio,l_data_header,4,p_manager); opj_stream_seek(cio, lenp+len,p_manager); #endif return len; }
static int write_fidx_v2( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio, opj_event_mgr_t * p_manager ) { OPJ_BYTE l_data_header [4]; int len, lenp; #if 0 lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_FIDX, 4); /* IPTR */ #else lenp = opj_stream_tell(cio); opj_stream_skip(cio, 4, p_manager); opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */ opj_stream_write_data(cio,l_data_header,4,p_manager); #endif write_prxy_v2( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager); #if 0 len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); #else len = opj_stream_tell(cio)-lenp; opj_stream_skip(cio, lenp, p_manager); opj_write_bytes(l_data_header,len,4);/* L */ opj_stream_write_data(cio,l_data_header,4,p_manager); opj_stream_seek(cio, lenp+len,p_manager); #endif return len; }
void j2k_write_siz() { int i; int lenp, len; cio_write(J2K_MS_SIZ, 2); /* SIZ */ lenp = cio_tell(); cio_skip(2); cio_write(0, 2); /* Rsiz (capabilities) */ cio_write(j2k_img->x1, 4); /* Xsiz */ cio_write(j2k_img->y1, 4); /* Ysiz */ cio_write(j2k_img->x0, 4); /* X0siz */ cio_write(j2k_img->y0, 4); /* Y0siz */ cio_write(j2k_cp->tdx, 4); /* XTsiz */ cio_write(j2k_cp->tdy, 4); /* YTsiz */ cio_write(j2k_cp->tx0, 4); /* XT0siz */ cio_write(j2k_cp->ty0, 4); /* YT0siz */ cio_write(j2k_img->numcomps, 2); /* Csiz */ for (i = 0; i < j2k_img->numcomps; i++) { cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */ cio_write(j2k_img->comps[i].dx, 1); /* XRsiz_i */ cio_write(j2k_img->comps[i].dy, 1); /* YRsiz_i */ } len = cio_tell() - lenp; cio_seek(lenp); cio_write(len, 2); /* Lsiz */ cio_seek(lenp + len); }
int write_ppix( int coff, opj_codestream_info_t cstr_info, opj_bool EPHused, int j2klen, opj_cio_t *cio) { int len, lenp, compno, i; opj_jp2_box_t *box; /* printf("cstr_info.packno %d\n", cstr_info.packno); //NMAX? */ lenp = -1; box = (opj_jp2_box_t *)opj_calloc( cstr_info.numcomps, sizeof(opj_jp2_box_t)); for (i=0; i<2; i++) { if (i) cio_seek( cio, lenp); lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_PPIX, 4); /* PPIX */ write_manf( i, cstr_info.numcomps, box, cio); for (compno=0; compno<cstr_info.numcomps; compno++) { box[compno].length = write_ppixfaix( coff, compno, cstr_info, EPHused, j2klen, cio); box[compno].type = JPIP_FAIX; } len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); } opj_free(box); return len; }
static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { unsigned int j2k_codestream_offset, j2k_codestream_length; opj_jp2_box_t box; opj_j2k_t *j2k = jp2->j2k; box.init_pos = cio_tell(cio); cio_skip(cio, 4); cio_write(cio, JP2_JP2C, 4); /* JP2C */ /* J2K encoding */ j2k_codestream_offset = cio_tell(cio); if(!j2k_encode(j2k, cio, image, cstr_info)) { opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n"); return 0; } j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset; jp2->j2k_codestream_offset = j2k_codestream_offset; jp2->j2k_codestream_length = j2k_codestream_length; box.length = 8 + jp2->j2k_codestream_length; cio_seek(cio, box.init_pos); cio_write(cio, box.length, 4); /* L */ cio_seek(cio, box.init_pos + box.length); return box.length; }
int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio) { int i; int len, lenp; lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_MHIX, 4); /* MHIX */ cio_write( cio, cstr_info.main_head_end-cstr_info.main_head_start+1, 8); /* TLEN */ for(i = 1; i < cstr_info.marknum; i++){ /* Marker restricted to 1 apparition, skip SOC marker */ cio_write( cio, cstr_info.marker[i].type, 2); cio_write( cio, 0, 2); cio_write( cio, cstr_info.marker[i].pos-coff, 8); cio_write( cio, cstr_info.marker[i].len, 2); } len = cio_tell( cio) - lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); return len; }
size_t write_thix( size_t coff, opj_codestream_info_t cstr_info, opj_cio_t *cio) { size_t len, lenp; int i; int tileno; opj_jp2_box_t *box; lenp = 0; box = (opj_jp2_box_t *)opj_calloc( cstr_info.tw*cstr_info.th, sizeof(opj_jp2_box_t)); for ( i = 0; i < 2 ; i++ ){ if (i) cio_seek( cio, lenp); lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_THIX, 4); /* THIX */ write_manf( i, cstr_info.tw*cstr_info.th, box, cio); for (tileno = 0; tileno < cstr_info.tw*cstr_info.th; tileno++){ box[tileno].length = write_tilemhix( coff, cstr_info, tileno, cio); box[tileno].type = JPIP_MHIX; } len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); } opj_free(box); return len; }
int write_cidx( int offset, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t cstr_info, int j2klen) { int len, i, lenp; opj_jp2_box_t *box; int num_box = 0; opj_bool EPHused; (void)image; /* unused ? */ lenp = -1; box = (opj_jp2_box_t *)opj_calloc( 32, sizeof(opj_jp2_box_t)); for (i=0;i<2;i++){ if(i) cio_seek( cio, lenp); lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_CIDX, 4); /* CIDX */ write_cptr( offset, cstr_info.codestream_size, cio); write_manf( i, num_box, box, cio); num_box = 0; box[num_box].length = write_mainmhix( offset, cstr_info, cio); box[num_box].type = JPIP_MHIX; num_box++; box[num_box].length = write_tpix( offset, cstr_info, j2klen, cio); box[num_box].type = JPIP_TPIX; num_box++; box[num_box].length = write_thix( offset, cstr_info, cio); box[num_box].type = JPIP_THIX; num_box++; EPHused = check_EPHuse( offset, cstr_info.marker, cstr_info.marknum, cio); box[num_box].length = write_ppix( offset, cstr_info, EPHused, j2klen, cio); box[num_box].type = JPIP_PPIX; num_box++; box[num_box].length = write_phix( offset, cstr_info, EPHused, j2klen, cio); box[num_box].type = JPIP_PHIX; num_box++; len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); } opj_free( box); return len; }
int write_thix_v2( int coff, opj_codestream_info_t cstr_info, opj_stream_private_t *cio, opj_event_mgr_t * p_manager ) { OPJ_BYTE l_data_header [4]; int len, lenp, i; int tileno; opj_jp2_box_t *box; lenp = 0; box = (opj_jp2_box_t *)opj_calloc( cstr_info.tw*cstr_info.th, sizeof(opj_jp2_box_t)); for ( i = 0; i < 2 ; i++ ){ if (i) #if 0 cio_seek( cio, lenp); #else opj_stream_seek( cio, lenp, p_manager); #endif #if 0 lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_THIX, 4); /* THIX */ #else lenp = opj_stream_tell(cio); opj_stream_skip(cio, 4, p_manager); /* L [at the end] */ opj_write_bytes(l_data_header,JPIP_THIX,4); /* THIX */ opj_stream_write_data(cio,l_data_header,4,p_manager); #endif write_manf_v2( i, cstr_info.tw*cstr_info.th, box, cio); for (tileno = 0; tileno < cstr_info.tw*cstr_info.th; tileno++){ box[tileno].length = write_tilemhix_v2( coff, cstr_info, tileno, cio); box[tileno].type = JPIP_MHIX; } #if 0 len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); #else len = opj_stream_tell(cio)-lenp; opj_stream_seek(cio, lenp, p_manager); opj_write_bytes(l_data_header,len,4); /* L */ opj_stream_write_data(cio,l_data_header,4,p_manager); opj_stream_seek( cio, lenp+len,p_manager); #endif } opj_free(box); return len; }
static void jp2_write_jp(opj_cio_t *cio) { opj_jp2_box_t box; box.init_pos = cio_tell(cio); cio_skip(cio, 4); cio_write(cio, JP2_JP, 4); /* JP2 signature */ cio_write(cio, 0x0d0a870a, 4); box.length = cio_tell(cio) - box.init_pos; cio_seek(cio, box.init_pos); cio_write(cio, box.length, 4); /* L */ cio_seek(cio, box.init_pos + box.length); }
void jp2_write_jp() { jp2_box_t box; box.init_pos = cio_tell(); cio_skip(4); cio_write(JP2_JP, 4); // JP cio_write(0x0d0a870a, 4); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); cio_write(box.length, 4); /* L */ cio_seek(box.init_pos + box.length); }
static void jp2_write_res(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; box.init_pos = cio_tell(cio); cio_skip(cio, 4); cio_write(cio, JP2_RES, 4); /* RES */ jp2_write_resc(jp2, cio); box.length = cio_tell(cio) - box.init_pos; cio_seek(cio, box.init_pos); cio_write(cio, box.length, 4); /* L */ cio_seek(cio, box.init_pos + box.length); }
opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx; pos_jp2c = pos_iptr = -1; /* remove a warning */ /* JP2 encoding */ /* JPEG 2000 Signature box */ jp2_write_jp(cio); /* File Type box */ jp2_write_ftyp(jp2, cio); /* JP2 Header box */ jp2_write_jp2h(jp2, cio); #if 0 if( jp2->jpip_on){ pos_iptr = cio_tell( cio); cio_skip( cio, 24); /* IPTR further ! */ pos_jp2c = cio_tell( cio); } #endif /* J2K encoding */ if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){ opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n"); return OPJ_FALSE; } #if 0 if( jp2->jpip_on){ pos_cidx = cio_tell( cio); len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8); pos_fidx = cio_tell( cio); len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio); end_pos = cio_tell( cio); cio_seek( cio, pos_iptr); write_iptr( pos_fidx, len_fidx, cio); cio_seek( cio, end_pos); } #endif return OPJ_TRUE; }
void j2k_write_coc(int compno) { j2k_tcp_t *tcp; int lenp, len; //fprintf(stderr, "%.8x: COC\n", cio_tell()); cio_write(J2K_MS_COC, 2); lenp=cio_tell(); cio_skip(2); tcp=&j2k_cp->tcps[j2k_curtileno]; cio_write(compno, j2k_img->numcomps<=256?1:2); cio_write(tcp->tccps[compno].csty, 1); j2k_write_cox(compno); len=cio_tell()-lenp; cio_seek(lenp); cio_write(len, 2); cio_seek(lenp+len); }
static void write_iptr( int offset, int length, opj_cio_t *cio) { int len, lenp; lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_IPTR, 4); /* IPTR */ cio_write( cio, offset, 8); cio_write( cio, length, 8); len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); }
void write_cptr(int coff, int clen, opj_cio_t *cio) { int len, lenp; lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_CPTR, 4); /* T */ cio_write( cio, 0, 2); /* DR A PRECISER !! */ cio_write( cio, 0, 2); /* CONT */ cio_write( cio, coff, 8); /* COFF A PRECISER !! */ cio_write( cio, clen, 8); /* CLEN */ len = cio_tell( cio) - lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); }
int write_tpix( int coff, opj_codestream_info_t cstr_info, int j2klen, opj_cio_t *cio) { int len, lenp; lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_TPIX, 4); /* TPIX */ write_tpixfaix( coff, 0, cstr_info, j2klen, cio); len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); return len; }
void j2k_write_coc(int compno) { j2k_tcp_t *tcp; int lenp, len; cio_write(J2K_MS_COC, 2); /* COC */ lenp = cio_tell(); cio_skip(2); tcp = &j2k_cp->tcps[j2k_curtileno]; cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */ cio_write(tcp->tccps[compno].csty, 1); /* Scoc */ j2k_write_cox(compno); len = cio_tell() - lenp; cio_seek(lenp); cio_write(len, 2); /* Lcoc */ cio_seek(lenp + len); }
void jp2_write_bpcc(jp2_struct_t * jp2_struct) { unsigned int i; jp2_box_t box; box.init_pos = cio_tell(); cio_skip(4); cio_write(JP2_BPCC, 4); // BPCC for (i = 0; i < jp2_struct->numcomps; i++) cio_write(jp2_struct->comps[i].bpcc, 1); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); cio_write(box.length, 4); /* L */ cio_seek(box.init_pos + box.length); }
static void ns_jp2_write_xml(opj_cio_t *cio, unsigned char * xml) { unsigned int i; opj_jp2_box_t box; size_t len; box.init_pos = cio_tell(cio); cio_skip(cio, 4); cio_write(cio, JP2_XML, 4); /* box type is 'xml ' */ len = strlen((const char *)xml); for (i = 0; i < len; i++) { cio_write(cio, xml[i], 1); } box.length = cio_tell(cio) - box.init_pos; cio_seek(cio, box.init_pos); cio_write(cio, box.length, 4); cio_seek(cio, box.init_pos + box.length); }
static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio) { int len, lenp; lenp = cio_tell( cio); cio_skip( cio, 4); /* L [at the end] */ cio_write( cio, JPIP_FIDX, 4); /* IPTR */ write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio); len = cio_tell( cio)-lenp; cio_seek( cio, lenp); cio_write( cio, len, 4); /* L */ cio_seek( cio, lenp+len); return len; }
static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) { unsigned int i; opj_jp2_box_t box; box.init_pos = cio_tell(cio); cio_skip(cio, 4); cio_write(cio, JP2_BPCC, 4); /* BPCC */ for (i = 0; i < jp2->numcomps; i++) { cio_write(cio, jp2->comps[i].bpcc, 1); } box.length = cio_tell(cio) - box.init_pos; cio_seek(cio, box.init_pos); cio_write(cio, box.length, 4); /* L */ cio_seek(cio, box.init_pos + box.length); }
void j2k_write_com(void) { unsigned int i; int lenp, len; char str[256]; sprintf(str, "Creator: J2000 codec"); //fprintf(stderr, "%.8x: COM\n", cio_tell()); cio_write(J2K_MS_COM, 2); lenp=cio_tell(); cio_skip(2); cio_write(0, 2); for (i=0; i<strlen(str); i++) { cio_write(str[i], 1); } len=cio_tell()-lenp; cio_seek(lenp); cio_write(len, 2); cio_seek(lenp+len); }
int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer, char *index) { int len; jp2_box_t box; box.init_pos = cio_tell(); cio_skip(4); cio_write(JP2_JP2C, 4); // JP2C len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * cp->th * cp->tw * 2, index); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); cio_write(box.length, 4); /* L */ cio_seek(box.init_pos + box.length); return box.length; }
void j2k_write_cod(void) { j2k_tcp_t *tcp; int lenp, len; //fprintf(stderr, "%.8x: COD\n", cio_tell()); cio_write(J2K_MS_COD, 2); lenp=cio_tell(); cio_skip(2); tcp=&j2k_cp->tcps[j2k_curtileno]; cio_write(tcp->csty, 1); cio_write(tcp->prg, 1); cio_write(tcp->numlayers, 2); cio_write(tcp->mct, 1); j2k_write_cox(0); len=cio_tell()-lenp; cio_seek(lenp); cio_write(len, 2); cio_seek(lenp+len); }
/* * Write the JP2H box * * JP2 Header box * */ void jp2_write_jp2h(jp2_struct_t * jp2_struct) { jp2_box_t box; box.init_pos = cio_tell(); cio_skip(4);; cio_write(JP2_JP2H, 4); /* JP2H */ jp2_write_ihdr(jp2_struct); if (jp2_struct->bpc == 255) jp2_write_bpcc(jp2_struct); jp2_write_colr(jp2_struct); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); cio_write(box.length, 4); /* L */ cio_seek(box.init_pos + box.length); }
void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; box.init_pos = cio_tell(cio); cio_skip(cio, 4); cio_write(cio, JP2_JP2H, 4); /* JP2H */ jp2_write_ihdr(jp2, cio); if (jp2->bpc == 255) { jp2_write_bpcc(jp2, cio); } jp2_write_colr(jp2, cio); box.length = cio_tell(cio) - box.init_pos; cio_seek(cio, box.init_pos); cio_write(cio, box.length, 4); /* L */ cio_seek(cio, box.init_pos + box.length); }
void j2k_write_com() { unsigned int i; int lenp, len; char str[256]; sprintf(str, "%s", j2k_cp->comment); cio_write(J2K_MS_COM, 2); lenp = cio_tell(); cio_skip(2); cio_write(0, 2); for (i = 0; i < strlen(str); i++) { cio_write(str[i], 1); } len = cio_tell() - lenp; cio_seek(lenp); cio_write(len, 2); cio_seek(lenp + len); }