Lines Matching refs:jp2
42 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
56 * @param jp2 the jpeg2000 file codec.
62 static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
70 * @param jp2 jpeg2000 file codec.
75 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
81 * @param jp2 jpeg2000 file codec.
86 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
93 * @param jp2 the jpeg2000 file codec.
99 static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
104 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
114 * @param jp2 jpeg2000 file codec.
119 static OPJ_BYTE * opj_jp2_write_cdef( opj_jp2_t *jp2,
125 * @param jp2 jpeg2000 file codec.
130 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
137 * @param jp2 the jpeg2000 file codec.
142 static OPJ_BOOL opj_jp2_write_ftyp( opj_jp2_t *jp2,
150 * @param jp2 the jpeg2000 file codec.
156 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
161 static OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
166 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
169 * @param jp2 the jpeg2000 file codec.
173 * @return true if the JP2 Header box was successfully recognized.
175 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
181 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
183 * @param jp2 the jpeg2000 file codec.
189 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
197 * @param jp2 the jpeg2000 file codec.
202 static OPJ_BOOL opj_jp2_write_jp2c( opj_jp2_t *jp2,
210 * @param jp2 the jpeg2000 file codec.
213 static OPJ_BOOL opj_jpip_write_iptr( opj_jp2_t *jp2,
220 * @param jp2 the jpeg2000 file codec.
223 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
230 * @param jp2 the jpeg2000 file codec.
233 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
242 * @param jp2 the jpeg2000 file codec.
248 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
257 * @param jp2 the jpeg2000 file codec.
262 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
278 * @param jp2 JP2 handle
285 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
293 * @param jp2 JP2 handle
301 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
310 * @param jp2 the jpeg2000 file codec.
316 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
329 static OPJ_BOOL opj_jp2_setup_end_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
335 static OPJ_BOOL opj_jp2_setup_end_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
340 * @param jp2 the jpeg2000 file header structure.
346 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
354 * @param jp2 the jpeg2000 file codec to execute the procedures on.
360 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
384 static OPJ_BOOL opj_jp2_setup_encoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
389 static OPJ_BOOL opj_jp2_setup_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
391 static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
452 jp2, opj_event_mgr_t * p_manager);
458 static OPJ_BOOL opj_jp2_setup_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager);
541 static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2,
548 assert(jp2 != 00);
556 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
558 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
560 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
564 opj_free(jp2->comps);
565 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps, sizeof(opj_jp2_comps_t));
566 if (jp2->comps == 0) {
571 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
574 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
578 if (jp2->C != 7){
579 opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
582 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
584 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
590 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
597 assert(jp2 != 00);
614 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
617 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
620 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
623 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
626 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
629 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
632 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
640 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
646 OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps;
650 assert(jp2 != 00);
666 for (i = 0; i < jp2->numcomps; ++i) {
667 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
676 static OPJ_BOOL opj_jp2_read_bpcc( opj_jp2_t *jp2,
686 assert(jp2 != 00);
690 if (jp2->bpc != 255 ){
691 opj_event_msg(p_manager, EVT_WARNING, "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",jp2->bpc);
695 if (p_bpc_header_size != jp2->numcomps) {
701 for (i = 0; i < jp2->numcomps; ++i) {
702 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
708 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, OPJ_UINT32 * p_nb_bytes_written)
717 assert(jp2 != 00);
719 assert(jp2->color.jp2_cdef != 00);
720 assert(jp2->color.jp2_cdef->info != 00);
721 assert(jp2->color.jp2_cdef->n > 0U);
723 l_cdef_size += 6U * jp2->color.jp2_cdef->n;
738 l_value = jp2->color.jp2_cdef->n;
742 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
743 l_value = jp2->color.jp2_cdef->info[i].cn;
746 l_value = jp2->color.jp2_cdef->info[i].typ;
749 l_value = jp2->color.jp2_cdef->info[i].asoc;
758 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
767 assert(jp2 != 00);
769 assert(jp2->meth == 1 || jp2->meth == 2);
771 switch (jp2->meth) {
776 assert(jp2->color.icc_profile_len); /* ICC profile */
777 l_colr_size += jp2->color.icc_profile_len;
796 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
799 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
802 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
805 if (jp2->meth == 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
806 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); } /* EnumCS */
808 if (jp2->meth == 2) { /* ICC profile */
810 for(i = 0; i < jp2->color.icc_profile_len; ++i) {
811 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
841 OPJ_UINT32 nr_channels = image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
956 /* file9.jp2 */
1067 static OPJ_BOOL opj_jp2_read_pclr( opj_jp2_t *jp2,
1083 assert(jp2 != 00);
1087 if(jp2->color.jp2_pclr)
1145 jp2->color.jp2_pclr = jp2_pclr;
1174 static OPJ_BOOL opj_jp2_read_cmap( opj_jp2_t * jp2,
1185 assert(jp2 != 00);
1191 if(jp2->color.jp2_pclr == NULL) {
1197 * inside a JP2 Header box' :
1199 if(jp2->color.jp2_pclr->cmap) {
1204 nr_channels = jp2->color.jp2_pclr->nr_channels;
1229 jp2->color.jp2_pclr->cmap = cmap;
1298 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
1309 assert(jp2 != 00);
1315 * inside a JP2 Header box.'*/
1316 if(jp2->color.jp2_cdef) return OPJ_FALSE;
1340 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1341 if(!jp2->color.jp2_cdef)
1346 jp2->color.jp2_cdef->info = cdef_info;
1347 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1349 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1366 static OPJ_BOOL opj_jp2_read_colr( opj_jp2_t *jp2,
1375 assert(jp2 != 00);
1384 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1387 if(jp2->color.jp2_has_colr) {
1388 opj_event_msg(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1393 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1396 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1399 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1402 if (jp2->meth == 1) {
1407 if ((p_colr_header_size > 7) && (jp2->enumcs != 14)) { /* handled below for CIELab) */
1412 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1416 if(jp2->enumcs == 14)/* CIELab */
1456 jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1457 jp2->color.icc_profile_len = 0;
1459 jp2->color.jp2_has_colr = 1;
1461 else if (jp2->meth == 2) {
1466 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1467 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1,(size_t)icc_len);
1468 if (!jp2->color.icc_profile_buf)
1470 jp2->color.icc_profile_len = 0;
1478 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1481 jp2->color.jp2_has_colr = 1;
1483 else if (jp2->meth > 2)
1486 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1488 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1493 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1502 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1503 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1507 if (!jp2->ignore_pclr_cmap_cdef){
1508 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1513 if (jp2->enumcs == 16)
1515 else if (jp2->enumcs == 17)
1517 else if (jp2->enumcs == 18)
1519 else if (jp2->enumcs == 24)
1521 else if (jp2->enumcs == 12)
1526 if(jp2->color.jp2_pclr) {
1528 if( !jp2->color.jp2_pclr->cmap)
1529 opj_jp2_free_pclr(&(jp2->color));
1531 opj_jp2_apply_pclr(p_image, &(jp2->color));
1535 if(jp2->color.jp2_cdef) {
1536 opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1539 if(jp2->color.icc_profile_buf) {
1540 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1541 p_image->icc_profile_len = jp2->color.icc_profile_len;
1542 jp2->color.icc_profile_buf = NULL;
1549 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1567 assert(jp2 != 00);
1572 if (jp2->bpc == 255) {
1584 if (jp2
1595 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1597 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1623 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1631 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1652 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1657 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
1663 assert(jp2 != 00);
1681 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1684 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1687 for (i = 0; i < jp2->numcl; i++) {
1688 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1702 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1710 assert(jp2 != 00);
1717 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1721 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1739 static OPJ_BOOL opj_jp2_write_jp( opj_jp2_t *jp2,
1748 assert(jp2 != 00);
1766 /* JP2 decoder interface */
1769 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1772 opj_j2k_setup_decoder(jp2->j2k, parameters);
1774 /* further JP2 initializations go here */
1775 jp2->color.jp2_has_colr = 0;
1776 jp2->comps = NULL;
1777 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1781 /* JP2 encoder interface */
1784 OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2,
1797 if(!jp2 || !parameters || !image)
1805 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1809 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ) == OPJ_FALSE) {
1813 /* setup the JP2 codec */
1818 jp2->brand = JP2_JP2; /* BR */
1819 jp2->minversion = 0; /* MinV */
1820 jp2->numcl = 1;
1821 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1822 if (!jp2->cl){
1823 jp2->cl = NULL;
1824 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1827 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1831 jp2->numcomps = image->numcomps; /* NC */
1832 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1833 if (!jp2->comps) {
1834 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP2 encoder\n");
1835 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1839 jp2->h = image->y1 - image->y0; /* HEIGHT */
1840 jp2->w = image->x1 - image->x0; /* WIDTH */
1844 jp2->bpc = depth_0 + (sign << 7);
1849 jp2->bpc = 255;
1851 jp2->C = 7; /* C : Always 7 */
1852 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1853 jp2->IPR = 0; /* IPR, no intellectual property */
1857 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1862 jp2->meth = 2;
1863 jp2->enumcs = 0;
1866 jp2->meth = 1;
1868 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1870 jp2->enumcs = 17; /* greyscale */
1872 jp2->enumcs = 18; /* YUV */
1886 switch (jp2->enumcs) {
1911 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1912 if(!jp2->color.jp2_cdef) {
1913 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n");
1916 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
1917 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
1918 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(image->numcomps * sizeof(opj_jp2_cdef_info_t));
1919 if (!jp2->color.jp2_cdef->info) {
1921 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup the JP2 encoder\n");
1924 jp2->color.jp2_cdef->n = (OPJ_UINT16) image->numcomps; /* cast is valid : image->numcomps [1,16384] */
1926 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1927 jp2->color.jp2_cdef->info[i].typ = 0U;
1928 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i+1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
1932 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1933 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
1934 jp2->color.jp2_cdef->info[i].asoc = 0U; /* Apply alpha channel to the whole image */
1937 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1938 jp2->color.jp2_cdef->info[i].typ = 65535U;
1939 jp2->color.jp2_cdef->info[i].asoc = 65535U;
1944 jp2->precedence = 0; /* PRECEDENCE */
1945 jp2->approx = 0; /* APPROX */
1947 jp2->jpip_on = parameters->jpip_on;
1952 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
1956 return opj_j2k_encode(jp2->j2k, stream, p_manager);
1959 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
1965 assert(jp2 != 00);
1970 if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
1975 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1979 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
1982 OPJ_BOOL opj_jp2_end_compress( opj_jp2_t *jp2,
1988 assert(jp2 != 00);
1993 if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
1997 if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
2002 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
2005 static OPJ_BOOL opj_jp2_setup_end_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2008 assert(jp2 != 00);
2012 if( jp2->jpip_on ) {
2013 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr, p_manager)) {
2018 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2023 if( jp2->jpip_on )
2025 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx, p_manager)) {
2028 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx, p_manager)) {
2036 static OPJ_BOOL opj_jp2_setup_end_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2039 assert(jp2 != 00);
2042 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2050 static OPJ_BOOL opj_jp2_default_validation ( opj_jp2_t * jp2,
2059 assert(jp2 != 00);
2067 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2070 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2074 l_is_valid &= (jp2->j2k != 00);
2077 l_is_valid &= (jp2->m_procedure_list != 00);
2080 l_is_valid &= (jp2->m_validation_list != 00);
2084 l_is_valid &= (jp2->numcl > 0);
2086 l_is_valid &= (jp2->h > 0);
2088 l_is_valid &= (jp2->w > 0);
2090 for (i = 0; i < jp2->numcomps; ++i) {
2091 l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) < 38U); /* 0 is valid, ignore sign for check */
2095 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2104 static OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
2119 assert(jp2 != 00);
2132 if (jp2->jp2_state & JP2_STATE_HEADER) {
2133 jp2
2162 if (jp2->jp2_state & JP2_STATE_HEADER) {
2167 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2200 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
2206 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2207 opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2211 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2212 opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format: second box must be file type box\n");
2216 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2234 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2240 static OPJ_BOOL opj_jp2_exec ( opj_jp2_t * jp2,
2247 OPJ_BOOL (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
2253 assert(jp2 != 00);
2258 l_procedure = (OPJ_BOOL (**) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2261 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
2270 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2277 assert(jp2 != 00);
2282 if (! opj_jp2_setup_encoding_validation (jp2, p_manager)) {
2287 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
2292 if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2297 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
2301 return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
2340 * @param jp2 the jpeg2000 file codec.
2346 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2357 assert(jp2 != 00);
2360 if (jp2->jp2_state != JP2_STATE_NONE) {
2378 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2387 * @param jp2 the jpeg2000 file codec.
2393 static OPJ_BOOL opj_jp2_read_ftyp( opj_jp2_t *jp2,
2403 assert(jp2 != 00);
2406 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2417 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
2420 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
2432 jp2->numcl = l_remaining_bytes >> 2;
2433 if (jp2->numcl) {
2434 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2435 if (jp2->cl == 00) {
2441 for (i = 0; i < jp2->numcl; ++i)
2443 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
2447 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2452 static OPJ_BOOL opj_jp2_skip_jp2c( opj_jp2_t *jp2,
2457 assert(jp2 != 00);
2461 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2470 static OPJ_BOOL opj_jpip_skip_iptr( opj_jp2_t *jp2,
2475 assert(jp2 != 00);
2479 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2489 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2492 * @param jp2 the jpeg2000 file codec.
2496 * @return true if the JP2 Header box was successfully recognized.
2498 static OPJ_BOOL opj_jp2_read_jp2h( opj_jp2_t *jp2,
2511 assert(jp2 != 00);
2515 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
2520 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2526 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
2531 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2540 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
2545 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2557 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2561 jp2->jp2_state |= JP2_STATE_HEADER;
2637 opj_jp2_t *jp2,
2643 assert(jp2 != 00);
2648 if (! opj_jp2_setup_decoding_validation (jp2, p_manager)) {
2653 if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2658 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2663 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2668 jp2->j2k,
2673 jp2, opj_event_mgr_t * p_manager)
2676 assert(jp2 != 00);
2679 if (! opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation, p_manager)) {
2687 static OPJ_BOOL opj_jp2_setup_decoding_validation (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2690 assert(jp2 != 00);
2698 static OPJ_BOOL opj_jp2_setup_header_writing (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2701 assert(jp2 != 00);
2704 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp, p_manager)) {
2707 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2710 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2713 if( jp2->jpip_on ) {
2714 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2718 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c,p_manager)) {
2727 static OPJ_BOOL opj_jp2_setup_header_reading (opj_jp2_t *jp2, opj_event_mgr_t * p_manager)
2730 assert(jp2 != 00);
2733 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2789 void opj_jp2_destroy(opj_jp2_t *jp2)
2791 if (jp2) {
2793 opj_j2k_destroy(jp2->j2k);
2794 jp2->j2k = 00;
2796 if (jp2->comps) {
2797 opj_free(jp2->comps);
2798 jp2->comps = 00;
2801 if (jp2->cl) {
2802 opj_free(jp2->cl);
2803 jp2->cl = 00;
2806 if (jp2->color.icc_profile_buf) {
2807 opj_free(jp2->color.icc_profile_buf);
2808 jp2->color.icc_profile_buf = 00;
2811 if (jp2->color.jp2_cdef) {
2812 if (jp2->color.jp2_cdef->info) {
2813 opj_free(jp2->color.jp2_cdef->info);
2814 jp2->color.jp2_cdef->info = NULL;
2817 opj_free(jp2->color.jp2_cdef);
2818 jp2->color.jp2_cdef = 00;
2821 if (jp2->color.jp2_pclr) {
2822 if (jp2->color.jp2_pclr->cmap) {
2823 opj_free(jp2->color.jp2_pclr->cmap);
2824 jp2->color.jp2_pclr->cmap = NULL;
2826 if (jp2->color.jp2_pclr->channel_sign) {
2827 opj_free(jp2->color.jp2_pclr->channel_sign);
2828 jp2->color.jp2_pclr->channel_sign = NULL;
2830 if (jp2->color.jp2_pclr->channel_size) {
2831 opj_free(jp2->color.jp2_pclr->channel_size);
2832 jp2->color.jp2_pclr->channel_size = NULL;
2834 if (jp2->color.jp2_pclr->entries) {
2835 opj_free(jp2->color.jp2_pclr->entries);
2836 jp2->color.jp2_pclr->entries = NULL;
2839 opj_free(jp2->color.jp2_pclr);
2840 jp2->color.jp2_pclr = 00;
2843 if (jp2->m_validation_list) {
2844 opj_procedure_list_destroy(jp2->m_validation_list);
2845 jp2->m_validation_list = 00;
2848 if (jp2->m_procedure_list) {
2849 opj_procedure_list_destroy(jp2->m_procedure_list);
2850 jp2->m_procedure_list = 00;
2853 opj_free(jp2);
2877 opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2880 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2925 /* JP2 encoder interface */
2930 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1,sizeof(opj_jp2_t));
2931 if (jp2) {
2935 jp2->j2k = opj_j2k_create_compress();
2938 jp2->j2k = opj_j2k_create_decompress();
2941 if (jp2->j2k == 00) {
2942 opj_jp2_destroy(jp2);
2947 jp2->color.icc_profile_buf = NULL;
2948 jp2->color.icc_profile_len = 0;
2949 jp2->color.jp2_cdef = NULL;
2950 jp2->color.jp2_pclr = NULL;
2951 jp2->color.jp2_has_colr = 0;
2954 jp2->m_validation_list = opj_procedure_list_create();
2955 if (! jp2->m_validation_list) {
2956 opj_jp2_destroy(jp2);
2961 jp2->m_procedure_list = opj_procedure_list_create();
2962 if (! jp2->m_procedure_list) {
2963 opj_jp2_destroy(jp2);
2968 return jp2;
3001 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3009 assert(jp2 != 00);
3025 if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
3043 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3051 assert(jp2 != 00);
3075 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3083 assert(jp2 != 00);