+ /* Left */
+ if (p_start_x < 0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Left position of the decoded area (region_x0=%d) should be >= 0.\n",
+ p_start_x);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_start_x > l_image->x1) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
+ p_start_x, l_image->x1);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_start_x < l_image->x0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
+ p_start_x, l_image->x0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
+ p_image->x0 = l_image->x0;
+ } else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x -
+ l_cp->tx0) / l_cp->tdx;
+ p_image->x0 = (OPJ_UINT32)p_start_x;
+ }
+
+ /* Up */
+ if (p_start_y < 0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Up position of the decoded area (region_y0=%d) should be >= 0.\n",
+ p_start_y);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_start_y > l_image->y1) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
+ p_start_y, l_image->y1);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_start_y < l_image->y0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
+ p_start_y, l_image->y0);
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
+ p_image->y0 = l_image->y0;
+ } else {
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y -
+ l_cp->ty0) / l_cp->tdy;
+ p_image->y0 = (OPJ_UINT32)p_start_y;
+ }
+
+ /* Right */
+ if (p_end_x <= 0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Right position of the decoded area (region_x1=%d) should be > 0.\n",
+ p_end_x);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_end_x < l_image->x0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
+ p_end_x, l_image->x0);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_end_x > l_image->x1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
+ p_end_x, l_image->x1);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
+ p_image->x1 = l_image->x1;
+ } else {
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(
+ p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);
+ p_image->x1 = (OPJ_UINT32)p_end_x;
+ }
+
+ /* Bottom */
+ if (p_end_y <= 0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Bottom position of the decoded area (region_y1=%d) should be > 0.\n",
+ p_end_y);
+ return OPJ_FALSE;
+ } else if ((OPJ_UINT32)p_end_y < l_image->y0) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
+ p_end_y, l_image->y0);
+ return OPJ_FALSE;
+ }
+ if ((OPJ_UINT32)p_end_y > l_image->y1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
+ p_end_y, l_image->y1);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
+ p_image->y1 = l_image->y1;
+ } else {
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(
+ p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);
+ p_image->y1 = (OPJ_UINT32)p_end_y;
+ }
+ /* ----- */
+
+ p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
+
+ ret = opj_j2k_update_image_dimensions(p_image, p_manager);
+
+ if (ret) {
+ opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n",
+ p_image->x0, p_image->y0, p_image->x1, p_image->y1);
+ }
+
+ return ret;