+ OPJ_ARG_NOT_USED(box);
+ OPJ_ARG_NOT_USED(jp2);
+
+/* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
+ * inside a JP2 Header box.'
+*/
+ if(color->jp2_cdef) return OPJ_FALSE;
+
+ if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
+
+ info = (opj_jp2_cdef_info_t*)
+ opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
+
+ color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
+ color->jp2_cdef->info = info;
+ color->jp2_cdef->n = n;
+
+ for(i = 0; i < n; ++i)
+ {
+ info[i].cn = (unsigned short)cio_read(cio, 2);
+ info[i].typ = (unsigned short)cio_read(cio, 2);
+ info[i].asoc = (unsigned short)cio_read(cio, 2);
+
+ }
+ return OPJ_TRUE;
+}/* jp2_read_cdef() */
+
+/**
+ * Reads the Component Definition box.
+ *
+ * @param p_cdef_header_data pointer to actual data (already read from file)
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_cdef_header_size pointer that will hold the size of the color header
+ * @param p_manager the user event manager.
+ *
+ * @return true if the cdef header is valid, false else.
+*/
+static opj_bool jp2_read_cdef_v2( opj_jp2_v2_t * jp2,
+ unsigned char * p_cdef_header_data,
+ OPJ_UINT32 p_cdef_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ opj_jp2_cdef_info_t *cdef_info;
+ unsigned short i;
+ OPJ_UINT32 l_value;
+
+ // preconditions
+ assert(jp2 != 00);
+ assert(p_cdef_header_data != 00);
+ assert(p_manager != 00);
+
+ /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
+ * inside a JP2 Header box.'*/
+ if(jp2->color.jp2_cdef) return OPJ_FALSE;
+
+ opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
+ p_cdef_header_data+= 2;
+
+ if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
+ return OPJ_FALSE;
+ }
+
+ cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
+
+ jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
+ jp2->color.jp2_cdef->info = cdef_info;
+ jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
+
+ for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
+ opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
+ p_cdef_header_data +=2;
+ cdef_info[i].cn = (OPJ_UINT16) l_value;
+
+ opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
+ p_cdef_header_data +=2;
+ cdef_info[i].typ = (OPJ_UINT16) l_value;
+
+ opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
+ p_cdef_header_data +=2;
+ cdef_info[i].asoc = (OPJ_UINT16) l_value;
+ }
+
+ return OPJ_TRUE;
+}
+
+
+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() */
+
+/**
+ * Reads the Colour Specification box.
+ *
+ * @param p_colr_header_data pointer to actual data (already read from file)
+ * @param jp2 the jpeg2000 file codec.
+ * @param p_colr_header_size pointer that will hold the size of the color header
+ * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false.
+ * @param p_manager the user event manager.
+ *
+ * @return true if the bpc header is valid, fale else.
+*/
+static opj_bool jp2_read_colr_v2( opj_jp2_v2_t * jp2,
+ unsigned char * p_colr_header_data,
+ OPJ_UINT32 p_colr_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ OPJ_UINT32 l_value;
+
+ // preconditions
+ assert(jp2 != 00);
+ assert(p_colr_header_data != 00);
+ assert(p_manager != 00);
+
+ if (p_colr_header_size < 3) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
+ return OPJ_FALSE;
+ }
+
+ /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
+ * Specification boxes after the first.'
+ */
+ if(jp2->color.jp2_has_colr) {
+ opj_event_msg_v2(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
+ p_colr_header_data += p_colr_header_size;
+ return OPJ_TRUE;
+ }
+
+ opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
+ ++p_colr_header_data;
+
+ opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
+ ++p_colr_header_data;
+
+ opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
+ ++p_colr_header_data;
+
+ if (jp2->meth == 1) {
+ if (p_colr_header_size != 7) {
+ opj_event_msg_v2(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
+ return OPJ_FALSE;
+ }
+
+ opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
+ }
+ else if (jp2->meth == 2) {
+ // ICC profile
+ int it_icc_value = 0;
+ int icc_len = p_colr_header_size - 3;
+
+ jp2->color.icc_profile_len = icc_len;
+ jp2->color.icc_profile_buf = (unsigned char*) opj_malloc(icc_len);
+
+ memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(unsigned char));
+
+ for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
+ {
+ opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
+ ++p_colr_header_data;
+ jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
+ }
+
+ }
+ else // TODO MSD
+ opj_event_msg_v2(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), so we will skip the fields following the approx field.\n", jp2->meth);
+
+ jp2->color.jp2_has_colr = 1;
+
+ return OPJ_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;
+
+ 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 OPJ_FALSE;
+ }
+ cio_skip(cio, box.length - 8);
+
+ if(cio->bp >= cio->end) return OPJ_FALSE;
+
+ jp2_read_boxhdr(cinfo, cio, &box);
+ }
+ } 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;
+ }
+ jp2_read_boxhdr(cinfo, cio, &box);
+
+ while(cio_tell(cio) < jp2h_end)
+ {
+ if(box.type == JP2_COLR)
+ {
+ if( !jp2_read_colr(jp2, cio, &box, color))
+ {
+ cio_seek(cio, box.init_pos + 8);
+ cio_skip(cio, box.length - 8);
+ }
+ jp2_read_boxhdr(cinfo, cio, &box);
+ continue;
+ }
+ if(box.type == JP2_CDEF)
+ {
+ if( !jp2_read_cdef(jp2, cio, &box, color))
+ {
+ cio_seek(cio, box.init_pos + 8);
+ cio_skip(cio, box.length - 8);
+ }
+ jp2_read_boxhdr(cinfo, cio, &box);
+ continue;
+ }
+ if(box.type == JP2_PCLR)
+ {
+ if( !jp2_read_pclr(jp2, cio, &box, color))
+ {
+ cio_seek(cio, box.init_pos + 8);
+ cio_skip(cio, box.length - 8);
+ }
+ jp2_read_boxhdr(cinfo, cio, &box);
+ continue;
+ }
+ if(box.type == JP2_CMAP)
+ {
+ if( !jp2_read_cmap(jp2, cio, &box, color))
+ {
+ cio_seek(cio, box.init_pos + 8);
+ cio_skip(cio, box.length - 8);
+ }
+ jp2_read_boxhdr(cinfo, cio, &box);
+ continue;
+ }
+ cio_seek(cio, box.init_pos + 8);
+ cio_skip(cio, box.length - 8);
+ jp2_read_boxhdr(cinfo, cio, &box);
+
+ }/* 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() */
+
+opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
+ opj_codestream_info_t *cstr_info)
+{
+ opj_common_ptr cinfo;
+ opj_image_t *image = NULL;
+ opj_jp2_color_t color;
+
+ if(!jp2 || !cio)
+ {
+ return NULL;
+ }
+ memset(&color, 0, sizeof(opj_jp2_color_t));
+ cinfo = jp2->cinfo;
+
+/* JP2 decoding */
+ if(!jp2_read_struct(jp2, cio, &color))
+ {
+ free_color_data(&color);
+ opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
+ return NULL;
+ }
+
+/* J2K decoding */
+ image = j2k_decode(jp2->j2k, cio, cstr_info);
+
+ if(!image)
+ {
+ free_color_data(&color);
+ opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
+ return NULL;
+ }
+
+/* Set Image Color Space */
+ if (jp2->enumcs == 16)
+ image->color_space = CLRSPC_SRGB;
+ else if (jp2->enumcs == 17)
+ image->color_space = CLRSPC_GRAY;
+ else if (jp2->enumcs == 18)
+ image->color_space = CLRSPC_SYCC;
+ else
+ image->color_space = CLRSPC_UNKNOWN;
+
+ if(color.jp2_cdef)
+ {
+ jp2_apply_cdef(image, &color);
+ }
+ if(color.jp2_pclr)
+ {
+/* Part 1, I.5.3.4: Either both or none : */
+ if( !color.jp2_pclr->cmap)
+ jp2_free_pclr(&color);
+ else
+ jp2_apply_pclr(&color, image);
+ }
+ if(color.icc_profile_buf)
+ {
+ image->icc_profile_buf = color.icc_profile_buf;
+ color.icc_profile_buf = NULL;
+ image->icc_profile_len = color.icc_profile_len;
+ }
+ return image;
+
+}/* opj_jp2_decode() */
+
+
+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);
+}
+
+static void jp2_write_ftyp(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_FTYP, 4); /* FTYP */
+
+ cio_write(cio, jp2->brand, 4); /* BR */
+ cio_write(cio, jp2->minversion, 4); /* MinV */
+
+ for (i = 0; i < jp2->numcl; i++) {
+ cio_write(cio, jp2->cl[i], 4); /* CL */
+ }
+
+ 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);
+}
+
+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;
+
+ jp2_read_boxhdr(cinfo, cio, &box);
+
+ 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 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 */