+static void j2k_read_siz(opj_j2k_t *j2k) {
+ int len, i;
+
+ opj_cio_t *cio = j2k->cio;
+ opj_image_t *image = j2k->image;
+ opj_cp_t *cp = j2k->cp;
+
+ len = cio_read(cio, 2); /* Lsiz */
+ cio_read(cio, 2); /* Rsiz (capabilities) */
+ image->x1 = cio_read(cio, 4); /* Xsiz */
+ image->y1 = cio_read(cio, 4); /* Ysiz */
+ image->x0 = cio_read(cio, 4); /* X0siz */
+ image->y0 = cio_read(cio, 4); /* Y0siz */
+ cp->tdx = cio_read(cio, 4); /* XTsiz */
+ cp->tdy = cio_read(cio, 4); /* YTsiz */
+ cp->tx0 = cio_read(cio, 4); /* XT0siz */
+ cp->ty0 = cio_read(cio, 4); /* YT0siz */
+
+ image->numcomps = cio_read(cio, 2); /* Csiz */
+
+#ifdef USE_JPWL
+ if (j2k->cp->correct) {
+ /* if JPWL is on, we check whether TX errors have damaged
+ too much the SIZ parameters */
+ if (!(image->x1 * image->y1)) {
+ opj_event_msg(j2k->cinfo, EVT_ERROR,
+ "JPWL: bad image size (%d x %d)\n",
+ image->x1, image->y1);
+ if (!JPWL_ASSUME || JPWL_ASSUME) {
+ opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return;
+ }
+ }
+ if (image->numcomps != ((len - 38) / 3)) {
+ opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
+ image->numcomps, ((len - 38) / 3));
+ if (!JPWL_ASSUME) {
+ opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return;
+ }
+ /* we try to correct */
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
+ if (image->numcomps < ((len - 38) / 3)) {
+ len = 38 + 3 * image->numcomps;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
+ len);
+ } else {
+ image->numcomps = ((len - 38) / 3);
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
+ image->numcomps);
+ }
+ }
+
+ /* update components number in the jpwl_exp_comps filed */
+ cp->exp_comps = image->numcomps;
+ }
+#endif /* USE_JPWL */
+
+ image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
+ for (i = 0; i < image->numcomps; i++) {
+ int tmp, w, h;
+ tmp = cio_read(cio, 1); /* Ssiz_i */
+ image->comps[i].prec = (tmp & 0x7f) + 1;
+ image->comps[i].sgnd = tmp >> 7;
+ image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
+ image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
+
+#ifdef USE_JPWL
+ if (j2k->cp->correct) {
+ /* if JPWL is on, we check whether TX errors have damaged
+ too much the SIZ parameters, again */
+ if (!(image->comps[i].dx * image->comps[i].dy)) {
+ opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
+ i, i, image->comps[i].dx, image->comps[i].dy);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return;
+ }
+ /* we try to correct */
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
+ if (!image->comps[i].dx) {
+ image->comps[i].dx = 1;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
+ i, image->comps[i].dx);
+ }
+ if (!image->comps[i].dy) {
+ image->comps[i].dy = 1;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
+ i, image->comps[i].dy);
+ }
+ }
+
+ }
+#endif /* USE_JPWL */
+
+ /* TODO: unused ? */
+ w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
+ h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
+
+ image->comps[i].resno_decoded = 0; /* number of resolution decoded */
+ image->comps[i].factor = cp->reduce; /* reducing factor per component */
+ }
+
+ cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
+ cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
+
+#ifdef USE_JPWL
+ if (j2k->cp->correct) {
+ /* if JPWL is on, we check whether TX errors have damaged
+ too much the SIZ parameters */
+ if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
+ opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: bad number of tiles (%d x %d)\n",
+ cp->tw, cp->th);
+ if (!JPWL_ASSUME) {
+ opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return;
+ }
+ /* we try to correct */
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
+ if (cp->tw < 1) {
+ cp->tw= 1;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
+ cp->tw);
+ }
+ if (cp->tw > cp->max_tiles) {
+ cp->tw= 1;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
+ "- setting %d tiles in x => HYPOTHESIS!!!\n",
+ cp->max_tiles, cp->tw);
+ }
+ if (cp->th < 1) {
+ cp->th= 1;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
+ cp->th);
+ }
+ if (cp->th > cp->max_tiles) {
+ cp->th= 1;
+ opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
+ "- setting %d tiles in y => HYPOTHESIS!!!\n",
+ cp->max_tiles, cp->th);
+ }
+ }
+ }
+#endif /* USE_JPWL */
+
+ cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
+ cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
+ cp->tileno_size = 0;
+
+#ifdef USE_JPWL
+ if (j2k->cp->correct) {
+ if (!cp->tcps) {
+ opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
+ "JPWL: could not alloc tcps field of cp\n");
+ if (!JPWL_ASSUME || JPWL_ASSUME) {
+ opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
+ return;
+ }
+ }
+ }
+#endif /* USE_JPWL */