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(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_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; }
int write_thix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio) { 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) 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; }
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); }
size_t write_phix( size_t coff, opj_codestream_info_t cstr_info, opj_bool EPHused, size_t j2klen, opj_cio_t *cio) { size_t len, lenp=0; int compno, i; opj_jp2_box_t *box; 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_PHIX, 4); /* PHIX */ write_manf( i, cstr_info.numcomps, box, cio); for( compno=0; compno<cstr_info.numcomps; compno++){ box[compno].length = write_phixfaix( 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_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 opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_box_t *box, opj_jp2_color_t *color) { int skip_len; opj_common_ptr cinfo; /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour * Specification boxes after the first.' */ if(color->jp2_has_colr) return OPJ_FALSE; cinfo = jp2->cinfo; jp2->meth = cio_read(cio, 1); /* METH */ jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */ jp2->approx = cio_read(cio, 1); /* APPROX */ if (jp2->meth == 1) { jp2->enumcs = cio_read(cio, 4); /* EnumCS */ } else if (jp2->meth == 2) { /* skip PROFILE */ skip_len = box->init_pos + box->length - cio_tell(cio); if (skip_len < 0) { opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n"); return OPJ_FALSE; } if(skip_len > 0) { unsigned char *start; start = cio_getbp(cio); color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len); color->icc_profile_len = skip_len; cio_skip(cio, box->init_pos + box->length - cio_tell(cio)); memcpy(color->icc_profile_buf, start, skip_len); } } if (cio_tell(cio) - box->init_pos != box->length) { opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n"); return OPJ_FALSE; } color->jp2_has_colr = 1; return OPJ_TRUE; }/* jp2_read_colr() */
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 j2k_read_coc() { int len, compno; j2k_tcp_t *tcp; info_tile_t *tile; info_marker_t *tmp; tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; len = cio_read(2); /* <INDEX> [MHIX BOX] */ if (j2k_state == J2K_STATE_MH) { if (!img.marker_mul.num_COC) img.marker_mul.COC = (info_marker_t*)malloc(img.marker_mul.CzCOC * sizeof(info_marker_t)); if (img.marker_mul.num_COC >= img.marker_mul.CzCOC) { tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzCOC) * sizeof(info_marker_t)); memcpy(tmp,img.marker_mul.COC,img.marker_mul.CzCOC); img.marker_mul.CzCOC += INCREMENT; free(img.marker_mul.COC); img.marker_mul.COC = tmp; } img.marker_mul.COC[img.marker_mul.num_COC].type = J2K_MS_COC; img.marker_mul.COC[img.marker_mul.num_COC].start_pos = cio_tell()-2; img.marker_mul.COC[img.marker_mul.num_COC].len = len; img.marker_mul.num_COC++; } else { tile = &img.tile[j2k_curtileno]; if (!tile->marker_mul.num_COC) tile->marker_mul.COC = (info_marker_t*)malloc(tile->marker_mul.CzCOC * sizeof(info_marker_t)); if (tile->marker_mul.num_COC >= tile->marker_mul.CzCOC) { tmp = (info_marker_t*)malloc((INCREMENT + tile->marker_mul.CzCOC) * sizeof(info_marker_t)); memcpy(tmp,tile->marker_mul.COC,tile->marker_mul.CzCOC); tile->marker_mul.CzCOC += INCREMENT; free(tile->marker_mul.COC); tile->marker_mul.COC = tmp; } tile->marker_mul.COC[tile->marker_mul.num_COC].type = J2K_MS_COC; tile->marker_mul.COC[tile->marker_mul.num_COC].start_pos = cio_tell() - 2; tile->marker_mul.COC[tile->marker_mul.num_COC].len = len; tile->marker_mul.num_COC++; } /* </INDEX> [MHIX BOX] */ compno =cio_read(j2k_img->numcomps <= 256 ? 1 : 2); tcp->tccps[compno].csty = cio_read(1); j2k_read_cox(compno); }
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); }
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); }
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; }
int jp2_read_colr(jp2_struct_t * jp2_struct) { jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_COLR != box.type) { fprintf(stderr, "Error: Expected COLR Marker\n"); return 1; } jp2_struct->meth = cio_read(1); // METH jp2_struct->precedence = cio_read(1); // PRECEDENCE jp2_struct->approx = cio_read(1); // APPROX if (jp2_struct->meth == 1) jp2_struct->enumcs = cio_read(4); // EnumCS else cio_read(1); // PROFILE if (cio_tell() - box.init_pos != box.length) { fprintf(stderr, "Error with BPCC Box\n"); return 1; } return 0; }
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); }
/* * Read the IHDR box * * Image Header box * */ int jp2_read_ihdr(jp2_struct_t * jp2_struct) { jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_IHDR != box.type) { fprintf(stderr, "Error: Expected IHDR Marker\n"); return 1; } jp2_struct->h = cio_read(4); // HEIGHT jp2_struct->w = cio_read(4); // WIDTH jp2_struct->numcomps = cio_read(2); // NC jp2_struct->bpc = cio_read(1); // BPC jp2_struct->C = cio_read(1); // C jp2_struct->UnkC = cio_read(1); // UnkC jp2_struct->IPR = cio_read(1); // IPR if (cio_tell() - box.init_pos != box.length) { fprintf(stderr, "Error with IHDR Box\n"); return 1; } return 0; }
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); }
static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) { int i; opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) { opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n"); return OPJ_FALSE; } if (JP2_FTYP != box.type) { opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n"); return OPJ_FALSE; } jp2->brand = cio_read(cio, 4); /* BR */ jp2->minversion = cio_read(cio, 4); /* MinV */ jp2->numcl = (box.length - 16) / 4; jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int)); for (i = 0; i < (int)jp2->numcl; i++) { jp2->cl[i] = cio_read(cio, 4); /* CLi */ } if (cio_tell(cio) - box.init_pos != box.length) { opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n"); return OPJ_FALSE; } return OPJ_TRUE; }
static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) { unsigned int i; opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) { opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n"); return OPJ_FALSE; } if (JP2_BPCC != box.type) { opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n"); return OPJ_FALSE; } for (i = 0; i < jp2->numcomps; i++) { jp2->comps[i].bpcc = cio_read(cio, 1); } if (cio_tell(cio) - box.init_pos != box.length) { opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n"); return OPJ_FALSE; } return OPJ_TRUE; }
static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; if(jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) { opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n"); return OPJ_FALSE; } if (JP2_IHDR != box.type) { opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n"); return OPJ_FALSE; } jp2->h = cio_read(cio, 4); /* HEIGHT */ jp2->w = cio_read(cio, 4); /* WIDTH */ jp2->numcomps = cio_read(cio, 2); /* NC */ jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t)); jp2->bpc = cio_read(cio, 1); /* BPC */ jp2->C = cio_read(cio, 1); /* C */ jp2->UnkC = cio_read(cio, 1); /* UnkC */ jp2->IPR = cio_read(cio, 1); /* IPR */ if (cio_tell(cio) - box.init_pos != box.length) { opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n"); return OPJ_FALSE; } return OPJ_TRUE; }
/* * Read the JP2H box * * JP2 Header box * */ int jp2_read_jp2h(jp2_struct_t * jp2_struct) { jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_JP2H != box.type) { fprintf(stderr, "Error: Expected JP2H Marker\n"); return 1; } if (jp2_read_ihdr(jp2_struct)) return 1; if (jp2_struct->bpc == 255) if (jp2_read_bpcc(jp2_struct)) return 1; if (jp2_read_colr(jp2_struct)) return 1; if (cio_tell() - box.init_pos != box.length) { fprintf(stderr, "Error with JP2H Box\n"); return 1; } return 0; }
static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) { opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) { opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n"); return OPJ_FALSE; } do { if(JP2_JP2C != box.type) { /* cf. http://code.google.com/p/openjpeg/issues/detail?id=155 */ if (box.length < 8) return OPJ_FALSE; cio_skip(cio, box.length - 8); if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; } } while(JP2_JP2C != box.type); *j2k_codestream_offset = cio_tell(cio); /* cf. http://code.google.com/p/openjpeg/issues/detail?id=155 */ if (box.length < 8) return OPJ_FALSE; *j2k_codestream_length = box.length - 8; return OPJ_TRUE; }
static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; if (jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE) { opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n"); return OPJ_FALSE; } if (JP2_JP != box.type) { opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n"); return OPJ_FALSE; } if (0x0d0a870a != cio_read(cio, 4)) { opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n"); return OPJ_FALSE; } if (cio_tell(cio) - box.init_pos != box.length) { opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n"); return OPJ_FALSE; } return OPJ_TRUE; }
/* * Read the FTYP box * * File type box * */ int jp2_read_ftyp(jp2_struct_t * jp2_struct) { int i; jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_FTYP != box.type) { fprintf(stderr, "Error: Excpected FTYP Marker\n"); return 1; } jp2_struct->brand = cio_read(4); /* BR */ jp2_struct->minversion = cio_read(4); /* MinV */ jp2_struct->numcl = (box.length - 16) / 4; jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int)); for (i = jp2_struct->numcl; i > 0; i--) jp2_struct->cl[i] = cio_read(4); /* CLi */ if (cio_tell() - box.init_pos != box.length) { fprintf(stderr, "Error with FTYP Box\n"); return 1; } return 0; }
static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) { box->init_pos = cio_tell(cio); box->length = cio_read(cio, 4); box->type = cio_read(cio, 4); if (box->length == 1) { if (cio_read(cio, 4) != 0) { opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n"); return OPJ_FALSE; } box->length = cio_read(cio, 4); if (box->length == 0) { box->length = cio_numbytesleft(cio) + 12; } } else if (box->length == 0) { box->length = cio_numbytesleft(cio) + 8; } if (box->length < 0) { opj_event_msg(cinfo, EVT_ERROR, "Integer overflow in box->length\n"); return OPJ_FALSE; /* TODO: actually check jp2_read_boxhdr's return value */ } return OPJ_TRUE; }
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; }
static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) { opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; if (jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE) { opj_event_msg(cinfo, EVT_ERROR, "Failed to read boxhdr\n"); return OPJ_FALSE; } do { if (JP2_JP2C != box.type) { if (box.length <= 8) { return OPJ_FALSE; } cio_skip(cio, box.length - 8); if (jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE) { return OPJ_FALSE; } } } while (JP2_JP2C != box.type); *j2k_codestream_offset = cio_tell(cio); if (box.length <= 8) { return OPJ_FALSE; } *j2k_codestream_length = box.length - 8; return OPJ_TRUE; }