/*
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
- * Copyright (c) 2002-2007, Professor Benoit Macq
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, 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) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
+ * Copyright (c) 2012, CS Systemes d'Information, France
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
/* ----------------------------------------------------------------------- */
-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;
-}
/* ----------------------------------------------------------------------- */
void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
{
- const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes;
+ const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value)+sizeof(OPJ_UINT32)-p_nb_bytes;
assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
*p_value = 0;
- memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes);
+ memcpy(l_data_ptr+sizeof(OPJ_UINT32)-p_nb_bytes,p_buffer,p_nb_bytes);
}
void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
}
}
-opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,opj_bool l_is_input)
+opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,OPJ_BOOL l_is_input)
{
opj_stream_private_t * l_stream = 00;
- l_stream = (opj_stream_private_t*) opj_malloc(sizeof(opj_stream_private_t));
+ l_stream = (opj_stream_private_t*) opj_calloc(1,sizeof(opj_stream_private_t));
if (! l_stream) {
return 00;
}
- memset(l_stream,0,sizeof(opj_stream_private_t));
l_stream->m_buffer_size = p_buffer_size;
l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_buffer_size);
if (! l_stream->m_stored_data) {
return (opj_stream_t *) l_stream;
}
-opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool l_is_input)
+opj_stream_t* OPJ_CALLCONV opj_stream_default_create(OPJ_BOOL l_is_input)
{
- return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input);
+ return opj_stream_create(OPJ_J2K_STREAM_CHUNK_SIZE,l_is_input);
}
void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream)
opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
if (l_stream) {
+ if (l_stream->m_free_user_data_fn) {
+ l_stream->m_free_user_data_fn(l_stream->m_user_data);
+ }
opj_free(l_stream->m_stored_data);
l_stream->m_stored_data = 00;
opj_free(l_stream);
l_stream->m_skip_fn = p_function;
}
-void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
+void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data, opj_stream_free_user_data_fn p_function)
{
opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+ if (!l_stream)
+ return;
l_stream->m_user_data = p_data;
+ l_stream->m_free_user_data_fn = p_function;
}
void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
{
opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
+ if (!l_stream)
+ return;
l_stream->m_user_data_length = data_length;
}
p_stream->m_current_data = p_stream->m_stored_data;
}
- while(1){
+ for (;;) {
/* we should read less than a chunk -> read a chunk */
if (p_size < p_stream->m_buffer_size) {
/* we should do an actual read on the media */
if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
/* end of stream */
- opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
p_stream->m_bytes_in_buffer = 0;
p_stream->m_status |= opj_stream_e_end;
if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
/* end of stream */
- opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
p_stream->m_bytes_in_buffer = 0;
p_stream->m_status |= opj_stream_e_end;
return (OPJ_SIZE_T)-1;
}
- while(1) {
+ for (;;) {
l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
/* we have more memory than required */
}
-opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
+OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
{
/* the number of bytes written on the media. */
OPJ_SIZE_T l_current_write_nb_bytes = 0;
if (l_current_write_nb_bytes == (OPJ_SIZE_T)-1) {
p_stream->m_status |= opj_stream_e_error;
- opj_event_msg_v2(p_event_mgr, EVT_INFO, "Error on writing stream!\n");
+ opj_event_msg(p_event_mgr, EVT_INFO, "Error on writing stream!\n");
return OPJ_FALSE;
}
/* we should do an actual skip on the media */
l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
if (l_current_skip_nb_bytes == (OPJ_OFF_T) -1) {
- opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
p_stream->m_status |= opj_stream_e_end;
p_stream->m_byte_offset += l_skip_nb_bytes;
OPJ_OFF_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
{
- opj_bool l_is_written = 0;
+ OPJ_BOOL l_is_written = 0;
OPJ_OFF_T l_current_skip_nb_bytes = 0;
OPJ_OFF_T l_skip_nb_bytes = 0;
if (! l_is_written) {
p_stream->m_status |= opj_stream_e_error;
p_stream->m_bytes_in_buffer = 0;
- p_stream->m_current_data = p_stream->m_current_data;
return (OPJ_OFF_T) -1;
}
/* then skip */
l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
if (l_current_skip_nb_bytes == (OPJ_OFF_T)-1) {
- opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream error!\n");
+ opj_event_msg(p_event_mgr, EVT_INFO, "Stream error!\n");
p_stream->m_status |= opj_stream_e_error;
p_stream->m_byte_offset += l_skip_nb_bytes;
return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
}
-opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
{
OPJ_ARG_NOT_USED(p_event_mgr);
p_stream->m_current_data = p_stream->m_stored_data;
return OPJ_TRUE;
}
-opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
+OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
{
if (! opj_stream_flush(p_stream,p_event_mgr)) {
p_stream->m_status |= opj_stream_e_error;
return OPJ_TRUE;
}
-opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
+OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
{
assert(p_size >= 0);
return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
}
-opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
+OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream)
{
return p_stream->m_seek_fn != opj_stream_default_seek;
}
return (OPJ_OFF_T) -1;
}
-opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
+OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
{
OPJ_ARG_NOT_USED(p_nb_bytes);
OPJ_ARG_NOT_USED(p_user_data);