[trunk] updated copyright and added copyright notice required by ISO, in each file...
[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, unsigned char *buffer, int length) {
41         opj_cp_t *cp = NULL;
42         opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
43         if(!cio) return NULL;
44         cio->cinfo = cinfo;
45         if(buffer && length) {
46                 /* wrap a user buffer containing the encoded image */
47                 cio->openmode = OPJ_STREAM_READ;
48                 cio->buffer = buffer;
49                 cio->length = length;
50         }
51         else if(!buffer && !length && cinfo) {
52                 /* allocate a buffer for the encoded image */
53                 cio->openmode = OPJ_STREAM_WRITE;
54                 switch(cinfo->codec_format) {
55                         case CODEC_J3D:
56                         case CODEC_J2K:
57                                 cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;
58                                 break;
59                         default:
60                                 opj_free(cio);
61                                 return NULL;
62                 }
63                 cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;
64                 cio->buffer = (unsigned char *)opj_malloc(cio->length);
65                 if(!cio->buffer) {
66                         opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
67                         opj_free(cio);
68                         return NULL;
69                 }
70         }
71         else {
72                 opj_free(cio);
73                 return NULL;
74         }
75
76         /* Initialize byte IO */
77         cio->start = cio->buffer;
78         cio->end = cio->buffer + cio->length;
79         cio->bp = cio->buffer;
80
81         return cio;
82 }
83
84 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
85         if(cio) {
86                 if(cio->openmode == OPJ_STREAM_WRITE) {
87                         /* destroy the allocated buffer */
88                         opj_free(cio->buffer);
89                 }
90                 /* destroy the cio */
91                 opj_free(cio);
92         }
93 }
94
95
96 /* ----------------------------------------------------------------------- */
97
98 /*
99  * Get position in byte stream.
100  */
101 int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
102         return cio->bp - cio->start;
103 }
104
105 /*
106  * Set position in byte stream.
107  *
108  * pos : position, in number of bytes, from the beginning of the stream
109  */
110 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
111         cio->bp = cio->start + pos;
112 }
113
114 /*
115  * Number of bytes left before the end of the stream.
116  */
117 int cio_numbytesleft(opj_cio_t *cio) {
118         return cio->end - cio->bp;
119 }
120
121 /*
122  * Get pointer to the current position in the stream.
123  */
124 unsigned char *cio_getbp(opj_cio_t *cio) {
125         return cio->bp;
126 }
127
128 /*
129  * Write a byte.
130  */
131 static bool cio_byteout(opj_cio_t *cio, unsigned char v) {
132         if (cio->bp >= cio->end) {
133                 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
134                 return false;
135         }
136         *cio->bp++ = v;
137         return true;
138 }
139
140 /*
141  * Read a byte.
142  */
143 static unsigned char cio_bytein(opj_cio_t *cio) {
144         if (cio->bp >= cio->end) {
145                 opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
146                 return 0;
147         }
148         return *cio->bp++;
149 }
150
151 /*
152  * Write some bytes.
153  *
154  * v : value to write
155  * n : number of bytes to write
156  */
157 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
158         int i;
159         for (i = n - 1; i >= 0; i--) {
160                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
161                         return 0;
162         }
163         return n;
164 }
165
166 /*
167  * Read some bytes.
168  *
169  * n : number of bytes to read
170  *
171  * return : value of the n bytes read
172  */
173 unsigned int cio_read(opj_cio_t *cio, int n) {
174         int i;
175         unsigned int v;
176         v = 0;
177         for (i = n - 1; i >= 0; i--) {
178                 v += cio_bytein(cio) << (i << 3);
179         }
180         return v;
181 }
182
183 /* 
184  * Skip some bytes.
185  *
186  * n : number of bytes to skip
187  */
188 void cio_skip(opj_cio_t *cio, int n) {
189         cio->bp += n;
190 }
191
192 /*
193  * Write some bytes.
194  *
195  * v : value to write
196  * n : number of bytes to write
197  */
198 int cio_write_int(opj_cio_t *cio, int v, int n) {
199         int i;
200         for (i = n - 1; i >= 0; i--) {
201                 if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )
202                         return 0;
203         }
204         return n;
205 }
206
207 /*
208  * Read some bytes.
209  *
210  * n : number of bytes to read
211  *
212  * return : value of the n bytes read
213  */
214 int cio_read_int(opj_cio_t *cio, int n) {
215         int i;
216         int v;
217         v = 0;
218         for (i = n - 1; i >= 0; i--) {
219                 v += cio_bytein(cio) << (i << 3);
220         }
221         return v;
222 }
223