Merge pull request #647 from stweil/master
[openjpeg.git] / src / lib / openmj2 / cio.c
1 /*
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.
6  *
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.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
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.
24  *
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.
36  */
37
38 #include "opj_includes.h"
39
40 /* ----------------------------------------------------------------------- */
41
42 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
43         opj_cp_t *cp = NULL;
44         opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
45         if(!cio) return NULL;
46         cio->cinfo = cinfo;
47         if(buffer && length) {
48                 /* wrap a user buffer containing the encoded image */
49                 cio->openmode = OPJ_STREAM_READ;
50                 cio->buffer = buffer;
51                 cio->length = length;
52         }
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) {
57                         case CODEC_J2K:
58                                 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
59                                 break;
60                         case CODEC_JP2:
61                                 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
62                                 break;
63                         default:
64                                 opj_free(cio);
65                                 return NULL;
66                 }
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);
69                 if(!cio->buffer) {
70                         opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
71                         opj_free(cio);
72                         return NULL;
73                 }
74         }
75         else {
76                 opj_free(cio);
77                 return NULL;
78         }
79
80         /* Initialize byte IO */
81         cio->start = cio->buffer;
82         cio->end = cio->buffer + cio->length;
83         cio->bp = cio->buffer;
84
85         return cio;
86 }
87
88 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
89         if(cio) {
90                 if(cio->openmode == OPJ_STREAM_WRITE) {
91                         /* destroy the allocated buffer */
92                         opj_free(cio->buffer);
93                 }
94                 /* destroy the cio */
95                 opj_free(cio);
96         }
97 }
98
99
100 /* ----------------------------------------------------------------------- */
101
102 /*
103  * Get position in byte stream.
104  */
105 int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
106         return cio->bp - cio->start;
107 }
108
109 /*
110  * Set position in byte stream.
111  *
112  * pos : position, in number of bytes, from the beginning of the stream
113  */
114 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
115         cio->bp = cio->start + pos;
116 }
117
118 /*
119  * Number of bytes left before the end of the stream.
120  */
121 int cio_numbytesleft(opj_cio_t *cio) {
122         return cio->end - cio->bp;
123 }
124
125 /*
126  * Get pointer to the current position in the stream.
127  */
128 unsigned char *cio_getbp(opj_cio_t *cio) {
129         return cio->bp;
130 }
131
132 /*
133  * Write a byte.
134  */
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");
138                 return OPJ_FALSE;
139         }
140         *cio->bp++ = v;
141         return OPJ_TRUE;
142 }
143
144 /*
145  * Read a byte.
146  */
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);
150                 return 0;
151         }
152         return *cio->bp++;
153 }
154
155 /*
156  * Write some bytes.
157  *
158  * v : value to write
159  * n : number of bytes to write
160  */
161 unsigned int OPJ_CALLCONV cio_write(opj_cio_t *cio, unsigned int64 v, int n) {
162         int i;
163         for (i = n - 1; i >= 0; i--) {
164                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
165                         return 0;
166         }
167         return n;
168 }
169
170 /*
171  * Read some bytes.
172  *
173  * n : number of bytes to read
174  *
175  * return : value of the n bytes read
176  */
177 unsigned int OPJ_CALLCONV cio_read(opj_cio_t *cio, int n) {
178         int i;
179         unsigned int v;
180         v = 0;
181         for (i = n - 1; i >= 0; i--) {
182                 v += cio_bytein(cio) << (i << 3);
183         }
184         return v;
185 }
186
187 /* 
188  * Skip some bytes.
189  *
190  * n : number of bytes to skip
191  */
192 void OPJ_CALLCONV cio_skip(opj_cio_t *cio, int n) {
193         cio->bp += n;
194 }
195
196
197