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