In pi.c, removed the Recursive function pi_check_next_level() and modified the code.
[openjpeg.git] / libopenjpeg / cio.c
index eadf71b2119851adb4815f1c0ffb7518287b2c29..2ac262a1f6b54013165218e2bd1a6e2d6d53f36b 100644 (file)
@@ -58,9 +58,10 @@ opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer
                                opj_free(cio);
                                return NULL;
                }
-               cio->length = cp->tdx * cp->tdy * cp->tw * cp->th * 4;
+               cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
                cio->buffer = (unsigned char *)opj_malloc(cio->length);
                if(!cio->buffer) {
+                       opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
                        opj_free(cio);
                        return NULL;
                }
@@ -139,7 +140,7 @@ bool cio_byteout(opj_cio_t *cio, unsigned char v) {
  */
 unsigned char cio_bytein(opj_cio_t *cio) {
        if (cio->bp >= cio->end) {
-               opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
+               opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
                return 0;
        }
        return *cio->bp++;