2 * Copyright (c) 2001-2003, David Janssens
\r
3 * Copyright (c) 2002-2003, Yannick Verschueren
\r
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
\r
5 * Copyright (c) 2005, Herve Drolon, FreeImage Team
\r
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
\r
7 * All rights reserved.
\r
9 * Redistribution and use in source and binary forms, with or without
\r
10 * modification, are permitted provided that the following conditions
\r
12 * 1. Redistributions of source code must retain the above copyright
\r
13 * notice, this list of conditions and the following disclaimer.
\r
14 * 2. Redistributions in binary form must reproduce the above copyright
\r
15 * notice, this list of conditions and the following disclaimer in the
\r
16 * documentation and/or other materials provided with the distribution.
\r
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
28 * POSSIBILITY OF SUCH DAMAGE.
\r
31 #include "opj_includes.h"
\r
33 /* ----------------------------------------------------------------------- */
\r
35 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
\r
36 opj_cp_t *cp = NULL;
\r
37 opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
\r
38 if(!cio) return NULL;
\r
40 if(buffer && length) {
\r
41 /* wrap a user buffer containing the encoded image */
\r
42 cio->openmode = OPJ_STREAM_READ;
\r
43 cio->buffer = buffer;
\r
44 cio->length = length;
\r
46 else if(!buffer && !length && cinfo) {
\r
47 /* allocate a buffer for the encoded image */
\r
48 cio->openmode = OPJ_STREAM_WRITE;
\r
49 switch(cinfo->codec_format) {
\r
52 cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
\r
58 cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;
\r
59 cio->buffer = (unsigned char *)opj_malloc(cio->length);
\r
70 /* Initialize byte IO */
\r
71 cio->start = cio->buffer;
\r
72 cio->end = cio->buffer + cio->length;
\r
73 cio->bp = cio->buffer;
\r
78 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
\r
80 if(cio->openmode == OPJ_STREAM_WRITE) {
\r
81 /* destroy the allocated buffer */
\r
82 opj_free(cio->buffer);
\r
84 /* destroy the cio */
\r
90 /* ----------------------------------------------------------------------- */
\r
93 * Get position in byte stream.
\r
95 int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
\r
96 return cio->bp - cio->start;
\r
100 * Set position in byte stream.
\r
102 * pos : position, in number of bytes, from the beginning of the stream
\r
104 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
\r
105 cio->bp = cio->start + pos;
\r
109 * Number of bytes left before the end of the stream.
\r
111 int cio_numbytesleft(opj_cio_t *cio) {
\r
112 return cio->end - cio->bp;
\r
116 * Get pointer to the current position in the stream.
\r
118 unsigned char *cio_getbp(opj_cio_t *cio) {
\r
125 bool cio_byteout(opj_cio_t *cio, unsigned char v) {
\r
126 if (cio->bp >= cio->end) {
\r
127 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
\r
137 unsigned char cio_bytein(opj_cio_t *cio) {
\r
138 if (cio->bp >= cio->end) {
\r
139 opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
\r
146 * Write some bytes.
\r
148 * v : value to write
\r
149 * n : number of bytes to write
\r
151 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
\r
153 for (i = n - 1; i >= 0; i--) {
\r
154 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
\r
163 * n : number of bytes to read
\r
165 * return : value of the n bytes read
\r
167 unsigned int cio_read(opj_cio_t *cio, int n) {
\r
171 for (i = n - 1; i >= 0; i--) {
\r
172 v += cio_bytein(cio) << (i << 3);
\r
180 * n : number of bytes to skip
\r
182 void cio_skip(opj_cio_t *cio, int n) {
\r
187 * Write some bytes.
\r
189 * v : value to write
\r
190 * n : number of bytes to write
\r
192 int cio_write_int(opj_cio_t *cio, int v, int n) {
\r
194 for (i = n - 1; i >= 0; i--) {
\r
195 if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )
\r
204 * n : number of bytes to read
\r
206 * return : value of the n bytes read
\r
208 int cio_read_int(opj_cio_t *cio, int n) {
\r
212 for (i = n - 1; i >= 0; i--) {
\r
213 v += cio_bytein(cio) << (i << 3);
\r