2003-11-27 11:10:17 +01:00
|
|
|
/*
|
2007-01-15 10:55:40 +01:00
|
|
|
* Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
|
|
|
|
* Copyright (c) 2002-2007, Professor Benoit Macq
|
2005-12-02 14:34:15 +01:00
|
|
|
* Copyright (c) 2001-2003, David Janssens
|
|
|
|
* Copyright (c) 2002-2003, Yannick Verschueren
|
2007-01-15 10:55:40 +01:00
|
|
|
* Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
|
|
|
|
* Copyright (c) 2005, Herve Drolon, FreeImage Team
|
2003-11-27 11:10:17 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
#include "opj_includes.h"
|
2003-11-27 11:10:17 +01:00
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
/* ----------------------------------------------------------------------- */
|
2003-11-27 11:10:17 +01:00
|
|
|
|
2006-01-31 22:26:11 +01:00
|
|
|
opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
|
2005-12-08 10:38:47 +01:00
|
|
|
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;
|
|
|
|
}
|
2007-09-07 17:01:55 +02:00
|
|
|
cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
|
2005-12-08 10:38:47 +01:00
|
|
|
cio->buffer = (unsigned char *)opj_malloc(cio->length);
|
|
|
|
if(!cio->buffer) {
|
2007-09-07 17:01:55 +02:00
|
|
|
opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
|
2005-12-08 10:38:47 +01:00
|
|
|
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;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2006-01-31 22:26:11 +01:00
|
|
|
void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
|
2005-12-08 10:38:47 +01:00
|
|
|
if(cio) {
|
|
|
|
if(cio->openmode == OPJ_STREAM_WRITE) {
|
|
|
|
/* destroy the allocated buffer */
|
|
|
|
opj_free(cio->buffer);
|
|
|
|
}
|
|
|
|
/* destroy the cio */
|
|
|
|
opj_free(cio);
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Get position in byte stream.
|
|
|
|
*/
|
2006-01-31 22:26:11 +01:00
|
|
|
int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
|
2005-12-08 10:38:47 +01:00
|
|
|
return cio->bp - cio->start;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Set position in byte stream.
|
|
|
|
*
|
|
|
|
* pos : position, in number of bytes, from the beginning of the stream
|
|
|
|
*/
|
2006-01-31 22:26:11 +01:00
|
|
|
void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
|
2005-12-08 10:38:47 +01:00
|
|
|
cio->bp = cio->start + pos;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Number of bytes left before the end of the stream.
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
int cio_numbytesleft(opj_cio_t *cio) {
|
2005-12-08 10:38:47 +01:00
|
|
|
return cio->end - cio->bp;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Get pointer to the current position in the stream.
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
unsigned char *cio_getbp(opj_cio_t *cio) {
|
2005-12-08 10:38:47 +01:00
|
|
|
return cio->bp;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Write a byte.
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
bool cio_byteout(opj_cio_t *cio, unsigned char v) {
|
2005-12-08 10:38:47 +01:00
|
|
|
if (cio->bp >= cio->end) {
|
|
|
|
opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*cio->bp++ = v;
|
|
|
|
return true;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Read a byte.
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
unsigned char cio_bytein(opj_cio_t *cio) {
|
2005-12-08 10:38:47 +01:00
|
|
|
if (cio->bp >= cio->end) {
|
2007-08-30 11:51:20 +02:00
|
|
|
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);
|
2005-12-08 10:38:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return *cio->bp++;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Write some bytes.
|
|
|
|
*
|
|
|
|
* v : value to write
|
|
|
|
* n : number of bytes to write
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
|
2005-12-08 10:38:47 +01:00
|
|
|
int i;
|
|
|
|
for (i = n - 1; i >= 0; i--) {
|
|
|
|
if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return n;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Read some bytes.
|
|
|
|
*
|
|
|
|
* n : number of bytes to read
|
|
|
|
*
|
|
|
|
* return : value of the n bytes read
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
unsigned int cio_read(opj_cio_t *cio, int n) {
|
2005-12-08 10:38:47 +01:00
|
|
|
int i;
|
|
|
|
unsigned int v;
|
|
|
|
v = 0;
|
|
|
|
for (i = n - 1; i >= 0; i--) {
|
|
|
|
v += cio_bytein(cio) << (i << 3);
|
|
|
|
}
|
|
|
|
return v;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
|
|
|
|
2004-02-13 10:47:40 +01:00
|
|
|
/*
|
|
|
|
* Skip some bytes.
|
|
|
|
*
|
|
|
|
* n : number of bytes to skip
|
|
|
|
*/
|
2005-12-02 14:34:15 +01:00
|
|
|
void cio_skip(opj_cio_t *cio, int n) {
|
2005-12-08 10:38:47 +01:00
|
|
|
cio->bp += n;
|
2003-11-27 11:10:17 +01:00
|
|
|
}
|
2005-05-23 17:26:29 +02:00
|
|
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|