2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /* ----------------------------------------------------------------------- */
40 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
42 opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
45 if(buffer && length) {
46 /* wrap a user buffer containing the encoded image */
47 cio->openmode = OPJ_STREAM_READ;
51 else if(!buffer && !length && cinfo) {
52 /* allocate a buffer for the encoded image */
53 cio->openmode = OPJ_STREAM_WRITE;
54 switch(cinfo->codec_format) {
57 cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
63 cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;
64 cio->buffer = (unsigned char *)opj_malloc(cio->length);
66 opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
76 /* Initialize byte IO */
77 cio->start = cio->buffer;
78 cio->end = cio->buffer + cio->length;
79 cio->bp = cio->buffer;
84 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
86 if(cio->openmode == OPJ_STREAM_WRITE) {
87 /* destroy the allocated buffer */
88 opj_free(cio->buffer);
96 /* ----------------------------------------------------------------------- */
99 * Get position in byte stream.
101 int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
102 return cio->bp - cio->start;
106 * Set position in byte stream.
108 * pos : position, in number of bytes, from the beginning of the stream
110 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
111 cio->bp = cio->start + pos;
115 * Number of bytes left before the end of the stream.
117 int cio_numbytesleft(opj_cio_t *cio) {
118 return cio->end - cio->bp;
122 * Get pointer to the current position in the stream.
124 unsigned char *cio_getbp(opj_cio_t *cio) {
131 static bool cio_byteout(opj_cio_t *cio, unsigned char v) {
132 if (cio->bp >= cio->end) {
133 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
143 static unsigned char cio_bytein(opj_cio_t *cio) {
144 if (cio->bp >= cio->end) {
145 opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
155 * n : number of bytes to write
157 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
159 for (i = n - 1; i >= 0; i--) {
160 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
169 * n : number of bytes to read
171 * return : value of the n bytes read
173 unsigned int cio_read(opj_cio_t *cio, int n) {
177 for (i = n - 1; i >= 0; i--) {
178 v += cio_bytein(cio) << (i << 3);
186 * n : number of bytes to skip
188 void cio_skip(opj_cio_t *cio, int n) {
196 * n : number of bytes to write
198 int cio_write_int(opj_cio_t *cio, int v, int n) {
200 for (i = n - 1; i >= 0; i--) {
201 if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )
210 * n : number of bytes to read
212 * return : value of the n bytes read
214 int cio_read_int(opj_cio_t *cio, int n) {
218 for (i = n - 1; i >= 0; i--) {
219 v += cio_bytein(cio) << (i << 3);