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, unsigned char *buffer, int length) {
44 opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
47 if(buffer && length) {
48 /* wrap a user buffer containing the encoded image */
49 cio->openmode = OPJ_STREAM_READ;
53 else if(!buffer && !length && cinfo) {
54 /* allocate a buffer for the encoded image */
55 cio->openmode = OPJ_STREAM_WRITE;
56 switch(cinfo->codec_format) {
58 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
61 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
67 cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
68 cio->buffer = (unsigned char *)opj_malloc(cio->length);
70 opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
80 /* Initialize byte IO */
81 cio->start = cio->buffer;
82 cio->end = cio->buffer + cio->length;
83 cio->bp = cio->buffer;
88 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) {
106 return cio->bp - cio->start;
110 * Set position in byte stream.
112 * pos : position, in number of bytes, from the beginning of the stream
114 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
115 cio->bp = cio->start + pos;
119 * Number of bytes left before the end of the stream.
121 int cio_numbytesleft(opj_cio_t *cio) {
122 return cio->end - cio->bp;
126 * Get pointer to the current position in the stream.
128 unsigned char *cio_getbp(opj_cio_t *cio) {
135 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
136 if (cio->bp >= cio->end) {
137 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
147 unsigned char cio_bytein(opj_cio_t *cio) {
148 if (cio->bp >= cio->end) {
149 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);
159 * n : number of bytes to write
161 unsigned int OPJ_CALLCONV cio_write(opj_cio_t *cio, unsigned int64 v, int n) {
163 for (i = n - 1; i >= 0; i--) {
164 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
173 * n : number of bytes to read
175 * return : value of the n bytes read
177 unsigned int OPJ_CALLCONV cio_read(opj_cio_t *cio, int n) {
181 for (i = n - 1; i >= 0; i--) {
182 v += cio_bytein(cio) << (i << 3);
190 * n : number of bytes to skip
192 void OPJ_CALLCONV cio_skip(opj_cio_t *cio, int n) {