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) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include "opj_includes.h"
40 /* ----------------------------------------------------------------------- */
42 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo,
43 unsigned char *buffer, int length)
46 opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
51 if (buffer && length) {
52 /* wrap a user buffer containing the encoded image */
53 cio->openmode = OPJ_STREAM_READ;
56 } else if (!buffer && !length && cinfo) {
57 /* allocate a buffer for the encoded image */
58 cio->openmode = OPJ_STREAM_WRITE;
59 switch (cinfo->codec_format) {
61 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
64 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
70 cio->length = (unsigned int)(0.1625 * cp->img_size +
71 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
72 cio->buffer = (unsigned char *)opj_malloc(cio->length);
74 opj_event_msg(cio->cinfo, EVT_ERROR,
75 "Error allocating memory for compressed bitstream\n");
84 /* Initialize byte IO */
85 cio->start = cio->buffer;
86 cio->end = cio->buffer + cio->length;
87 cio->bp = cio->buffer;
92 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio)
95 if (cio->openmode == OPJ_STREAM_WRITE) {
96 /* destroy the allocated buffer */
97 opj_free(cio->buffer);
105 /* ----------------------------------------------------------------------- */
108 * Get position in byte stream.
110 int OPJ_CALLCONV cio_tell(opj_cio_t *cio)
112 return cio->bp - cio->start;
116 * Set position in byte stream.
118 * pos : position, in number of bytes, from the beginning of the stream
120 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos)
122 cio->bp = cio->start + pos;
126 * Number of bytes left before the end of the stream.
128 int cio_numbytesleft(opj_cio_t *cio)
130 return cio->end - cio->bp;
134 * Get pointer to the current position in the stream.
136 unsigned char *cio_getbp(opj_cio_t *cio)
144 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v)
146 if (cio->bp >= cio->end) {
147 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
157 unsigned char cio_bytein(opj_cio_t *cio)
159 if (cio->bp >= cio->end) {
160 opj_event_msg(cio->cinfo, EVT_ERROR,
161 "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n",
162 cio->start, cio->bp, cio->end);
172 * n : number of bytes to write
174 unsigned int OPJ_CALLCONV cio_write(opj_cio_t *cio, unsigned int64 v, int n)
177 for (i = n - 1; i >= 0; i--) {
178 if (!cio_byteout(cio, (unsigned char)((v >> (i << 3)) & 0xff))) {
188 * n : number of bytes to read
190 * return : value of the n bytes read
192 unsigned int OPJ_CALLCONV cio_read(opj_cio_t *cio, int n)
197 for (i = n - 1; i >= 0; i--) {
198 v += cio_bytein(cio) << (i << 3);
206 * n : number of bytes to skip
208 void OPJ_CALLCONV cio_skip(opj_cio_t *cio, int n)