/* ----------------------------------------------------------------------- */
-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 = (int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
- assert(cio->length >= 0);
- cio->buffer = (unsigned char *)opj_malloc((OPJ_SIZE_T)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;
-}
-
-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);
- }
-}
-
-/* ----------------------------------------------------------------------- */
-
-/*
- * Get position in byte stream.
- */
-OPJ_OFF_T cio_tell(opj_cio_t *cio) {
- return cio->bp - cio->start;
-}
-
-/*
- * 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;
-}
-
-/*
- * Number of bytes left before the end of the stream.
- */
-OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio) {
- const ptrdiff_t diff = cio->end - cio->bp;
- assert( diff >= 0 );
- return (OPJ_SIZE_T)diff;
-}
-
-/*
- * Get pointer to the current position in the stream.
- */
-unsigned char *cio_getbp(opj_cio_t *cio) {
- return cio->bp;
-}
-
-/*
- * Write a byte.
- */
-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(opj_cio_t *cio) {
- 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++;
-}
-
-/*
- * Write some bytes.
- *
- * v : value to write
- * n : number of bytes to write
- */
-unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n) {
- int i;
- for (i = n - 1; i >= 0; i--) {
- if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
- return 0;
- }
- assert( n >= 0 );
- return (unsigned 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 = 0;
- for (i = n - 1; i >= 0; i--) {
- const unsigned int c = cio_bytein(cio);
- v += c << (i << 3);
- }
- return v;
-}
-
-/*
- * Skip some bytes.
- *
- * n : number of bytes to skip
- */
-void cio_skip(opj_cio_t *cio, int n) {
- cio->bp += n;
-}
/* ----------------------------------------------------------------------- */
/** @name Exported functions (see also openjpeg.h) */
/*@{*/
/* ----------------------------------------------------------------------- */
-/**
-Number of bytes left before the end of the stream
-@param cio CIO handle
-@return Returns the number of bytes before the end of the stream
-*/
-OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio);
-/**
-Get pointer to the current position in the stream
-@param cio CIO handle
-@return Returns a pointer to the current position
-*/
-unsigned char *cio_getbp(opj_cio_t *cio);
-/**
-*/
-opj_bool cio_byteout(opj_cio_t *cio, unsigned char v);
-/**
-*/
-unsigned char cio_bytein(opj_cio_t *cio);
-/**
-Write some bytes
-@param cio CIO handle
-@param v Value to write
-@param n Number of bytes to write
-@return Returns the number of bytes written or 0 if an error occured
-*/
-DEPRECATED(unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n));
-/**
-Read some bytes
-@param cio CIO handle
-@param n Number of bytes to read
-@return Returns the value of the n bytes read
-*/
-DEPRECATED(unsigned int cio_read(opj_cio_t *cio, int n));
-/**
-Skip some bytes
-@param cio CIO handle
-@param n Number of bytes to skip
-*/
-DEPRECATED(void cio_skip(opj_cio_t *cio, int n));
/* ----------------------------------------------------------------------- */
/*@}*/