Reformat whole codebase with astyle.options (#128)
[openjpeg.git] / src / lib / openjp3d / 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) 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.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
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.
22  *
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.
34  */
35
36 #include "opj_includes.h"
37
38 /* ----------------------------------------------------------------------- */
39
40 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo,
41                                      unsigned char *buffer, int length)
42 {
43     opj_cp_t *cp = NULL;
44     opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
45     if (!cio) {
46         return NULL;
47     }
48     cio->cinfo = cinfo;
49     if (buffer && length) {
50         /* wrap a user buffer containing the encoded image */
51         cio->openmode = OPJ_STREAM_READ;
52         cio->buffer = buffer;
53         cio->length = length;
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) {
58         case CODEC_J3D:
59         case CODEC_J2K:
60             cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
61             break;
62         default:
63             opj_free(cio);
64             return NULL;
65         }
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);
68         if (!cio->buffer) {
69             opj_event_msg(cio->cinfo, EVT_ERROR,
70                           "Error allocating memory for compressed bitstream\n");
71             opj_free(cio);
72             return NULL;
73         }
74     } else {
75         opj_free(cio);
76         return NULL;
77     }
78
79     /* Initialize byte IO */
80     cio->start = cio->buffer;
81     cio->end = cio->buffer + cio->length;
82     cio->bp = cio->buffer;
83
84     return cio;
85 }
86
87 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio)
88 {
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 {
107     return cio->bp - cio->start;
108 }
109
110 /*
111  * Set position in byte stream.
112  *
113  * pos : position, in number of bytes, from the beginning of the stream
114  */
115 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos)
116 {
117     cio->bp = cio->start + pos;
118 }
119
120 /*
121  * Number of bytes left before the end of the stream.
122  */
123 int cio_numbytesleft(opj_cio_t *cio)
124 {
125     return cio->end - cio->bp;
126 }
127
128 /*
129  * Get pointer to the current position in the stream.
130  */
131 unsigned char *cio_getbp(opj_cio_t *cio)
132 {
133     return cio->bp;
134 }
135
136 /*
137  * Write a byte.
138  */
139 static bool cio_byteout(opj_cio_t *cio, unsigned char v)
140 {
141     if (cio->bp >= cio->end) {
142         opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
143         return false;
144     }
145     *cio->bp++ = v;
146     return true;
147 }
148
149 /*
150  * Read a byte.
151  */
152 static unsigned char cio_bytein(opj_cio_t *cio)
153 {
154     if (cio->bp >= cio->end) {
155         opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
156         return 0;
157     }
158     return *cio->bp++;
159 }
160
161 /*
162  * Write some bytes.
163  *
164  * v : value to write
165  * n : number of bytes to write
166  */
167 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n)
168 {
169     int i;
170     for (i = n - 1; i >= 0; i--) {
171         if (!cio_byteout(cio, (unsigned char)((v >> (i << 3)) & 0xff))) {
172             return 0;
173         }
174     }
175     return n;
176 }
177
178 /*
179  * Read some bytes.
180  *
181  * n : number of bytes to read
182  *
183  * return : value of the n bytes read
184  */
185 unsigned int cio_read(opj_cio_t *cio, int n)
186 {
187     int i;
188     unsigned int v;
189     v = 0;
190     for (i = n - 1; i >= 0; i--) {
191         v += cio_bytein(cio) << (i << 3);
192     }
193     return v;
194 }
195
196 /*
197  * Skip some bytes.
198  *
199  * n : number of bytes to skip
200  */
201 void cio_skip(opj_cio_t *cio, int n)
202 {
203     cio->bp += n;
204 }
205
206 /*
207  * Write some bytes.
208  *
209  * v : value to write
210  * n : number of bytes to write
211  */
212 int cio_write_int(opj_cio_t *cio, int v, int n)
213 {
214     int i;
215     for (i = n - 1; i >= 0; i--) {
216         if (!cio_byteout(cio, (char)((v >> (i << 3)) & 0xff))) {
217             return 0;
218         }
219     }
220     return n;
221 }
222
223 /*
224  * Read some bytes.
225  *
226  * n : number of bytes to read
227  *
228  * return : value of the n bytes read
229  */
230 int cio_read_int(opj_cio_t *cio, int n)
231 {
232     int i;
233     int v;
234     v = 0;
235     for (i = n - 1; i >= 0; i--) {
236         v += cio_bytein(cio) << (i << 3);
237     }
238     return v;
239 }
240