[1.5] Import portion of patch from issue 297
[openjpeg.git] / libopenjpeg / cio.c
index f6d1c875526d2c89d96f7c79cf1a52c89c8a4b1a..97cccea6dee55491cf96e82c9ab5cce1e95b84cc 100644 (file)
@@ -1,5 +1,10 @@
 /*
- * Copyright (c) 2001-2002, David Janssens
+ * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2007, Professor Benoit Macq
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "cio.h"
-#include <setjmp.h>
-#include <memory.h>
+#include "opj_includes.h"
+#include <assert.h>
+
+/* ----------------------------------------------------------------------- */
+
+opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
+       opj_cp_t *cp = NULL;
+       opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
+       if(!cio) return NULL;
+       cio->cinfo = cinfo;
+       if(buffer && length) {
+               /* wrap a user buffer containing the encoded image */
+               cio->openmode = OPJ_STREAM_READ;
+               cio->buffer = buffer;
+               cio->length = length;
+       }
+       else if(!buffer && !length && cinfo) {
+               /* allocate a buffer for the encoded image */
+               cio->openmode = OPJ_STREAM_WRITE;
+               switch(cinfo->codec_format) {
+                       case CODEC_J2K:
+                               cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
+                               break;
+                       case CODEC_JP2:
+                               cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
+                               break;
+                       default:
+                               opj_free(cio);
+                               return NULL;
+               }
+               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;
+               }
+       }
+       else {
+               opj_free(cio);
+               return NULL;
+       }
+
+       /* Initialize byte IO */
+       cio->start = cio->buffer;
+       cio->end = cio->buffer + cio->length;
+       cio->bp = cio->buffer;
+
+       return cio;
+}
 
-static unsigned char *cio_start;       /* pointer to the start of the stream */
-static unsigned char *cio_end; /* pointer to the end of the stream */
-static unsigned char *cio_bp;  /* pointer to the present position */
+void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
+       if(cio) {
+               if(cio->openmode == OPJ_STREAM_WRITE) {
+                       /* destroy the allocated buffer */
+                       opj_free(cio->buffer);
+               }
+               /* destroy the cio */
+               opj_free(cio);
+       }
+}
 
-extern jmp_buf j2k_error;
 
-/* 
- * Number of bytes written.
- */
-int cio_numbytes()
-{
-  return cio_bp - cio_start;
-}
+/* ----------------------------------------------------------------------- */
 
 /*
  * Get position in byte stream.
  */
-int cio_tell()
-{
-  return cio_bp - cio_start;
+int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
+       return cio->bp - cio->start;
 }
 
 /*
@@ -55,59 +106,48 @@ int cio_tell()
  *
  * pos : position, in number of bytes, from the beginning of the stream
  */
-void cio_seek(int pos)
-{
-  cio_bp = cio_start + pos;
+void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
+  assert((cio->start + pos) <= cio->end);
+       cio->bp = cio->start + pos;
 }
 
 /*
  * Number of bytes left before the end of the stream.
  */
-int cio_numbytesleft()
-{
-  return cio_end - cio_bp;
+int cio_numbytesleft(opj_cio_t *cio) {
+  assert((cio->end - cio->bp) >= 0);
+       return cio->end - cio->bp;
 }
 
 /*
  * Get pointer to the current position in the stream.
  */
-unsigned char *cio_getbp()
-{
-  return cio_bp;
-}
-
-/* 
- * Initialize byte IO
- *
- * bp  : destination/source stream
- * len : length of the stream
- */
-void cio_init(unsigned char *bp, int len)
-{
-  cio_start = bp;
-  cio_end = bp + len;
-  cio_bp = bp;
+unsigned char *cio_getbp(opj_cio_t *cio) {
+       return cio->bp;
 }
 
 /*
  * Write a byte.
  */
-void cio_byteout(unsigned char v)
-{
-  if (cio_bp >= cio_end)
-    longjmp(j2k_error, 1);
-  *cio_bp++ = v;
-
+opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
+       if (cio->bp >= cio->end) {
+               opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
+               return OPJ_FALSE;
+       }
+       *cio->bp++ = v;
+       return OPJ_TRUE;
 }
 
 /*
  * Read a byte.
  */
-unsigned char cio_bytein()
-{
-  if (cio_bp >= cio_end)
-    longjmp(j2k_error, 1);
-  return *cio_bp++;
+unsigned char cio_bytein(opj_cio_t *cio) {
+  assert(cio->bp >= cio->start);
+       if (cio->bp >= cio->end) {
+               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++;
 }
 
 /*
@@ -116,12 +156,13 @@ unsigned char cio_bytein()
  * v : value to write
  * n : number of bytes to write
  */
-void cio_write(unsigned int v, int n)
-{
-  int i;
-  for (i = n - 1; i >= 0; i--) {
-    cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));
-  }
+unsigned int cio_write(opj_cio_t *cio, unsigned int64 v, int n) {
+       int i;
+       for (i = n - 1; i >= 0; i--) {
+               if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
+                       return 0;
+       }
+       return n;
 }
 
 /*
@@ -131,15 +172,14 @@ void cio_write(unsigned int v, int n)
  *
  * return : value of the n bytes read
  */
-unsigned int cio_read(int n)
-{
-  int i;
-  unsigned int v;
-  v = 0;
-  for (i = n - 1; i >= 0; i--) {
-    v += cio_bytein() << (i << 3);
-  }
-  return v;
+unsigned int cio_read(opj_cio_t *cio, int n) {
+       int i;
+       unsigned int v;
+       v = 0;
+       for (i = n - 1; i >= 0; i--) {
+               v += (unsigned int)cio_bytein(cio) << (i << 3);
+       }
+       return v;
 }
 
 /* 
@@ -147,33 +187,13 @@ unsigned int cio_read(int n)
  *
  * n : number of bytes to skip
  */
-void cio_skip(int n)
-{
-  cio_bp += n;
+void cio_skip(opj_cio_t *cio, int n) {
+  assert((cio->bp + n) >= cio->bp);
+  if (((cio->bp + n) < cio->start) || ((cio->bp + n) > cio->end)) {
+    assert(0);
+  }
+       cio->bp += n;
 }
 
-/* 
- * Read n bytes, copy to buffer
- *
- * n : number of bytes to transfer
- */
-void cio_read_to_buf(unsigned char* src_buf, int n)/* Glenn adds */
-{
-  if (cio_bp + n > cio_end)
-    longjmp(j2k_error, 1);
-  memcpy(cio_bp, src_buf, n);
-  cio_bp += n;
-}
 
-/* 
- * Write n bytes, copy from buffer
- *
- * n : number of bytes to transfer
- */
-void cio_write_from_buf(unsigned char* dest_buf, int n)/* Glenn adds */
-{
-  if (cio_bp + n > cio_end)
-    longjmp(j2k_error, 1);
-  memcpy(dest_buf, cio_bp, n);
-  cio_bp += n;
-}
\ No newline at end of file
+