COMP: Fix compilation on MINGW
[openjpeg.git] / libopenjpeg / cio.c
index bc5adc6076594c7e9dba6f6c4e949202e2e12c4f..c9c59bc243a2f758b3a67e04d1f77c86478080f8 100644 (file)
@@ -1,5 +1,9 @@
 /*
- * Copyright (c) 2001-2002, David Janssens
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herv� Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  * 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 "opj_includes.h"
 
-static unsigned char *cio_start, *cio_end, *cio_bp;
+/* ----------------------------------------------------------------------- */
 
-extern jmp_buf j2k_error;
+opj_cio_t* 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 = cp->tdx * cp->tdy * cp->tw * cp->th * 4;
+               cio->buffer = (unsigned char *)opj_malloc(cio->length);
+               if(!cio->buffer) {
+                       opj_free(cio);
+                       return NULL;
+               }
+       }
+       else {
+               opj_free(cio);
+               return NULL;
+       }
 
-/* <summary> */
-/* Number of bytes written. */
-/* </summary> */
-int cio_numbytes()
-{
-       return cio_bp - cio_start;
-}
+       /* Initialize byte IO */
+       cio->start = cio->buffer;
+       cio->end = cio->buffer + cio->length;
+       cio->bp = cio->buffer;
 
-/* <summary> */
-/* Get position in byte stream. */
-/* </summary> */
-int cio_tell()
-{
-       return cio_bp - cio_start;
+       return cio;
 }
 
-/* <summary> */
-/* Set position in byte stream. */
-/* </summary> */
-void cio_seek(int pos)
-{
-       cio_bp = cio_start + pos;
+void 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);
+       }
 }
 
-/* <summary> */
-/* Number of bytes left before the end of the stream. */
-/* </summary> */
-int cio_numbytesleft()
-{
-       return cio_end - cio_bp;
+
+/* ----------------------------------------------------------------------- */
+
+/*
+ * Get position in byte stream.
+ */
+int cio_tell(opj_cio_t *cio) {
+       return cio->bp - cio->start;
 }
 
-/* <summary> */
-/* Get pointer to the current position in the stream. */
-/* </summary> */
-unsigned char *cio_getbp()
-{
-       return cio_bp;
+/*
+ * Set position in byte stream.
+ *
+ * pos : position, in number of bytes, from the beginning of the stream
+ */
+void cio_seek(opj_cio_t *cio, int pos) {
+       cio->bp = cio->start + pos;
 }
 
-/* <summary> */
-/* Initialize byte IO. */
-/* </summary> */
-void cio_init(unsigned char *bp, int len)
-{
-       cio_start = bp;
-       cio_end = bp + len;
-       cio_bp = bp;
+/*
+ * Number of bytes left before the end of the stream.
+ */
+int cio_numbytesleft(opj_cio_t *cio) {
+       return cio->end - cio->bp;
 }
 
-/* <summary> */
-/* Write a byte. */
-/* </summary> */
-void cio_byteout(unsigned char v)
-{
-       if (cio_bp >= cio_end)
-               longjmp(j2k_error, 1);
-       *cio_bp++ = v;
+/*
+ * Get pointer to the current position in the stream.
+ */
+unsigned char *cio_getbp(opj_cio_t *cio) {
+       return cio->bp;
+}
 
+/*
+ * Write a byte.
+ */
+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 false;
+       }
+       *cio->bp++ = v;
+       return true;
 }
 
-/* <summary> */
-/* Read a byte. */
-/* </summary> */
-unsigned char cio_bytein()
-{
-       if (cio_bp >= cio_end)
-               longjmp(j2k_error, 1);
-       return *cio_bp++;
+/*
+ * Read a byte.
+ */
+unsigned char cio_bytein(opj_cio_t *cio) {
+       if (cio->bp >= cio->end) {
+               opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
+               return 0;
+       }
+       return *cio->bp++;
 }
 
-/* <summary> */
-/* Write a byte. */
-/* </summary> */
-void cio_write(unsigned int v, int n)
-{
+/*
+ * Write some bytes.
+ *
+ * v : value to write
+ * n : number of bytes to write
+ */
+unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
        int i;
        for (i = n - 1; i >= 0; i--) {
-               cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));
+               if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
+                       return 0;
        }
+       return n;
 }
 
-/* <summary> */
-/* Read some bytes. */
-/* </summary> */
-unsigned int cio_read(int n)
-{
+/*
+ * Read some bytes.
+ *
+ * n : number of bytes to read
+ *
+ * return : value of the n bytes read
+ */
+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 += cio_bytein() << (i << 3);
+               v += cio_bytein(cio) << (i << 3);
        }
        return v;
 }
 
-/* <summary> */
-/* Write some bytes. */
-/* </summary> */
-void cio_skip(int n)
-{
-       cio_bp += n;
+/* 
+ * Skip some bytes.
+ *
+ * n : number of bytes to skip
+ */
+void cio_skip(opj_cio_t *cio, int n) {
+       cio->bp += n;
 }
+
+
+