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