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,
41 unsigned char *buffer, int length)
44 opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
49 if (buffer && length) {
50 /* wrap a user buffer containing the encoded image */
51 cio->openmode = OPJ_STREAM_READ;
54 } else if (!buffer && !length && cinfo) {
55 /* allocate a buffer for the encoded image */
56 cio->openmode = OPJ_STREAM_WRITE;
57 switch (cinfo->codec_format) {
60 cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
66 cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;
67 cio->buffer = (unsigned char *)opj_malloc(cio->length);
69 opj_event_msg(cio->cinfo, EVT_ERROR,
70 "Error allocating memory for compressed bitstream\n");
79 /* Initialize byte IO */
80 cio->start = cio->buffer;
81 cio->end = cio->buffer + cio->length;
82 cio->bp = cio->buffer;
87 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio)
90 if (cio->openmode == OPJ_STREAM_WRITE) {
91 /* destroy the allocated buffer */
92 opj_free(cio->buffer);
100 /* ----------------------------------------------------------------------- */
103 * Get position in byte stream.
105 int OPJ_CALLCONV cio_tell(opj_cio_t *cio)
107 return cio->bp - cio->start;
111 * Set position in byte stream.
113 * pos : position, in number of bytes, from the beginning of the stream
115 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos)
117 cio->bp = cio->start + pos;
121 * Number of bytes left before the end of the stream.
123 int cio_numbytesleft(opj_cio_t *cio)
125 return cio->end - cio->bp;
129 * Get pointer to the current position in the stream.
131 unsigned char *cio_getbp(opj_cio_t *cio)
139 static bool cio_byteout(opj_cio_t *cio, unsigned char v)
141 if (cio->bp >= cio->end) {
142 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
152 static unsigned char cio_bytein(opj_cio_t *cio)
154 if (cio->bp >= cio->end) {
155 opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
165 * n : number of bytes to write
167 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n)
170 for (i = n - 1; i >= 0; i--) {
171 if (!cio_byteout(cio, (unsigned char)((v >> (i << 3)) & 0xff))) {
181 * n : number of bytes to read
183 * return : value of the n bytes read
185 unsigned int cio_read(opj_cio_t *cio, int n)
190 for (i = n - 1; i >= 0; i--) {
191 v += cio_bytein(cio) << (i << 3);
199 * n : number of bytes to skip
201 void cio_skip(opj_cio_t *cio, int n)
210 * n : number of bytes to write
212 int cio_write_int(opj_cio_t *cio, int v, int n)
215 for (i = n - 1; i >= 0; i--) {
216 if (!cio_byteout(cio, (char)((v >> (i << 3)) & 0xff))) {
226 * n : number of bytes to read
228 * return : value of the n bytes read
230 int cio_read_int(opj_cio_t *cio, int n)
235 for (i = n - 1; i >= 0; i--) {
236 v += cio_bytein(cio) << (i << 3);