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; }
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; }
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; }
/* * 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 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 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_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 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 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; jp2_read_boxhdr(cinfo, cio, &box); do { if(JP2_JP2C != box.type) { cio_skip(cio, box.length - 8); jp2_read_boxhdr(cinfo, cio, &box); } } while(JP2_JP2C != box.type); *j2k_codestream_offset = cio_tell(cio); *j2k_codestream_length = box.length - 8; return true; }
bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; int skip_len; opj_common_ptr cinfo = jp2->cinfo; jp2_read_boxhdr(cinfo, cio, &box); do { if (JP2_JP2H != box.type) { if (box.type == JP2_JP2C) { opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n"); return false; } cio_skip(cio, box.length - 8); jp2_read_boxhdr(cinfo, cio, &box); } } while(JP2_JP2H != box.type); if (!jp2_read_ihdr(jp2, cio)) return false; if (jp2->bpc == 255) { if (!jp2_read_bpcc(jp2, cio)) return false; } if (!jp2_read_colr(jp2, cio)) return false; skip_len = box.init_pos + box.length - cio_tell(cio); if (skip_len < 0) { opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n"); return false; } cio_skip(cio, box.init_pos + box.length - cio_tell(cio)); return true; }
static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; int skip_len; opj_common_ptr cinfo = jp2->cinfo; jp2_read_boxhdr(cinfo, cio, &box); do { if (JP2_COLR != box.type) { cio_skip(cio, box.length - 8); jp2_read_boxhdr(cinfo, cio, &box); } } while(JP2_COLR != box.type); 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 { /* skip PROFILE */ skip_len = box.init_pos + box.length - cio_tell(cio); if (skip_len < 0) { opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n"); return false; } cio_skip(cio, box.init_pos + box.length - cio_tell(cio)); } if (cio_tell(cio) - box.init_pos != box.length) { opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n"); return false; } return true; }
int jp2_read_bpcc(jp2_struct_t * jp2_struct) { unsigned int i; jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_BPCC != box.type) { fprintf(stderr, "Error: Expected BPCC Marker\n"); return 1; } for (i = 0; i < jp2_struct->numcomps; i++) jp2_struct->comps[i].bpcc = cio_read(1); if (cio_tell() - box.init_pos != box.length) { fprintf(stderr, "Error with BPCC Box\n"); return 1; } return 0; }
/* * Read the JP box * * JPEG 2000 signature * * return 1 if error else 0 */ int jp2_read_jp() { jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_JP != box.type) { fprintf(stderr, "Error: Expected JP Marker\n"); return 1; } if (0x0d0a870a != cio_read(4)) { fprintf(stderr, "Error with JP Marker\n"); return 1; } if (cio_tell() - box.init_pos != box.length) { fprintf(stderr, "Error with JP Box size\n"); return 1; } return 0; }
int jp2_read_jp2c(unsigned char *src, int len, jp2_struct_t * jp2_struct, j2k_cp_t * cp) { jp2_box_t box; jp2_read_boxhdr(&box); if (JP2_JP2C != box.type) { fprintf(stderr, "Error: Expected JP2C Marker\n"); return 1; } src += cio_tell(); 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; }
static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) { opj_jp2_box_t box; opj_common_ptr cinfo = jp2->cinfo; jp2_read_boxhdr(cinfo, cio, &box); if (JP2_JP != box.type) { opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n"); return false; } if (0x0d0a870a != cio_read(cio, 4)) { opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n"); return false; } if (cio_tell(cio) - box.init_pos != box.length) { opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n"); return false; } return true; }
opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color) { opj_jp2_box_t box; int jp2h_end; opj_common_ptr cinfo = jp2->cinfo; if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; do { if (JP2_JP2H != box.type) { if (box.type == JP2_JP2C) { opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n"); return OPJ_FALSE; } if (box.length <= 8) return OPJ_FALSE; cio_skip(cio, box.length - 8); if(cio->bp >= cio->end) return OPJ_FALSE; if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; } } while(JP2_JP2H != box.type); if (!jp2_read_ihdr(jp2, cio)) return OPJ_FALSE; jp2h_end = box.init_pos + box.length; if (jp2->bpc == 255) { if (!jp2_read_bpcc(jp2, cio)) return OPJ_FALSE; } if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; while(cio_tell(cio) < jp2h_end) { if(box.type == JP2_COLR) { if( !jp2_read_colr(jp2, cio, &box, color)) { if (box.length <= 8) return OPJ_FALSE; cio_seek(cio, box.init_pos + 8); cio_skip(cio, box.length - 8); } if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; continue; } if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef) { if( !jp2_read_cdef(jp2, cio, &box, color)) { if (box.length <= 8) return OPJ_FALSE; cio_seek(cio, box.init_pos + 8); cio_skip(cio, box.length - 8); } if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; continue; } if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef) { if( !jp2_read_pclr(jp2, cio, &box, color)) { if (box.length <= 8) return OPJ_FALSE; cio_seek(cio, box.init_pos + 8); cio_skip(cio, box.length - 8); } if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; continue; } if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef) { if( !jp2_read_cmap(jp2, cio, &box, color)) { if (box.length <= 8) return OPJ_FALSE; cio_seek(cio, box.init_pos + 8); cio_skip(cio, box.length - 8); } if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; continue; } if (box.length <= 8) return OPJ_FALSE; cio_seek(cio, box.init_pos + 8); cio_skip(cio, box.length - 8); if( jp2_read_boxhdr(cinfo, cio, &box) == OPJ_FALSE ) return OPJ_FALSE; }/* while(cio_tell(cio) < box_end) */ cio_seek(cio, jp2h_end); /* Part 1, I.5.3.3 : 'must contain at least one' */ return (color->jp2_has_colr == 1); }/* jp2_read_jp2h() */