renamed "openjpeg3d" in "openjp3d" (final step)
[openjpeg.git] / libopenjp3d / cio.c
1 /*\r
2  * Copyright (c) 2001-2003, David Janssens\r
3  * Copyright (c) 2002-2003, Yannick Verschueren\r
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
5  * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
7  * All rights reserved.\r
8  *\r
9  * Redistribution and use in source and binary forms, with or without\r
10  * modification, are permitted provided that the following conditions\r
11  * are met:\r
12  * 1. Redistributions of source code must retain the above copyright\r
13  *    notice, this list of conditions and the following disclaimer.\r
14  * 2. Redistributions in binary form must reproduce the above copyright\r
15  *    notice, this list of conditions and the following disclaimer in the\r
16  *    documentation and/or other materials provided with the distribution.\r
17  *\r
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28  * POSSIBILITY OF SUCH DAMAGE.\r
29  */\r
30 \r
31 #include "opj_includes.h"\r
32 \r
33 /* ----------------------------------------------------------------------- */\r
34 \r
35 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {\r
36         opj_cp_t *cp = NULL;\r
37         opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));\r
38         if(!cio) return NULL;\r
39         cio->cinfo = cinfo;\r
40         if(buffer && length) {\r
41                 /* wrap a user buffer containing the encoded image */\r
42                 cio->openmode = OPJ_STREAM_READ;\r
43                 cio->buffer = buffer;\r
44                 cio->length = length;\r
45         }\r
46         else if(!buffer && !length && cinfo) {\r
47                 /* allocate a buffer for the encoded image */\r
48                 cio->openmode = OPJ_STREAM_WRITE;\r
49                 switch(cinfo->codec_format) {\r
50                         case CODEC_J3D:\r
51                         case CODEC_J2K:\r
52                                 cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp;\r
53                                 break;\r
54                         default:\r
55                                 opj_free(cio);\r
56                                 return NULL;\r
57                 }\r
58                 cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4;\r
59                 cio->buffer = (unsigned char *)opj_malloc(cio->length);\r
60                 if(!cio->buffer) {\r
61                         opj_free(cio);\r
62                         return NULL;\r
63                 }\r
64         }\r
65         else {\r
66                 opj_free(cio);\r
67                 return NULL;\r
68         }\r
69 \r
70         /* Initialize byte IO */\r
71         cio->start = cio->buffer;\r
72         cio->end = cio->buffer + cio->length;\r
73         cio->bp = cio->buffer;\r
74 \r
75         return cio;\r
76 }\r
77 \r
78 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {\r
79         if(cio) {\r
80                 if(cio->openmode == OPJ_STREAM_WRITE) {\r
81                         /* destroy the allocated buffer */\r
82                         opj_free(cio->buffer);\r
83                 }\r
84                 /* destroy the cio */\r
85                 opj_free(cio);\r
86         }\r
87 }\r
88 \r
89 \r
90 /* ----------------------------------------------------------------------- */\r
91 \r
92 /*\r
93  * Get position in byte stream.\r
94  */\r
95 int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {\r
96         return cio->bp - cio->start;\r
97 }\r
98 \r
99 /*\r
100  * Set position in byte stream.\r
101  *\r
102  * pos : position, in number of bytes, from the beginning of the stream\r
103  */\r
104 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {\r
105         cio->bp = cio->start + pos;\r
106 }\r
107 \r
108 /*\r
109  * Number of bytes left before the end of the stream.\r
110  */\r
111 int cio_numbytesleft(opj_cio_t *cio) {\r
112         return cio->end - cio->bp;\r
113 }\r
114 \r
115 /*\r
116  * Get pointer to the current position in the stream.\r
117  */\r
118 unsigned char *cio_getbp(opj_cio_t *cio) {\r
119         return cio->bp;\r
120 }\r
121 \r
122 /*\r
123  * Write a byte.\r
124  */\r
125 bool cio_byteout(opj_cio_t *cio, unsigned char v) {\r
126         if (cio->bp >= cio->end) {\r
127                 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");\r
128                 return false;\r
129         }\r
130         *cio->bp++ = v;\r
131         return true;\r
132 }\r
133 \r
134 /*\r
135  * Read a byte.\r
136  */\r
137 unsigned char cio_bytein(opj_cio_t *cio) {\r
138         if (cio->bp >= cio->end) {\r
139                 opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");\r
140                 return 0;\r
141         }\r
142         return *cio->bp++;\r
143 }\r
144 \r
145 /*\r
146  * Write some bytes.\r
147  *\r
148  * v : value to write\r
149  * n : number of bytes to write\r
150  */\r
151 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {\r
152         int i;\r
153         for (i = n - 1; i >= 0; i--) {\r
154                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )\r
155                         return 0;\r
156         }\r
157         return n;\r
158 }\r
159 \r
160 /*\r
161  * Read some bytes.\r
162  *\r
163  * n : number of bytes to read\r
164  *\r
165  * return : value of the n bytes read\r
166  */\r
167 unsigned int cio_read(opj_cio_t *cio, int n) {\r
168         int i;\r
169         unsigned int v;\r
170         v = 0;\r
171         for (i = n - 1; i >= 0; i--) {\r
172                 v += cio_bytein(cio) << (i << 3);\r
173         }\r
174         return v;\r
175 }\r
176 \r
177 /* \r
178  * Skip some bytes.\r
179  *\r
180  * n : number of bytes to skip\r
181  */\r
182 void cio_skip(opj_cio_t *cio, int n) {\r
183         cio->bp += n;\r
184 }\r
185 \r
186 /*\r
187  * Write some bytes.\r
188  *\r
189  * v : value to write\r
190  * n : number of bytes to write\r
191  */\r
192 int cio_write_int(opj_cio_t *cio, int v, int n) {\r
193         int i;\r
194         for (i = n - 1; i >= 0; i--) {\r
195                 if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) )\r
196                         return 0;\r
197         }\r
198         return n;\r
199 }\r
200 \r
201 /*\r
202  * Read some bytes.\r
203  *\r
204  * n : number of bytes to read\r
205  *\r
206  * return : value of the n bytes read\r
207  */\r
208 int cio_read_int(opj_cio_t *cio, int n) {\r
209         int i;\r
210         int v;\r
211         v = 0;\r
212         for (i = n - 1; i >= 0; i--) {\r
213                 v += cio_bytein(cio) << (i << 3);\r
214         }\r
215         return v;\r
216 }\r
217 \r