added cmake files to the project
[openjpeg.git] / libopenjpeg / 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, Herv� 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_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_J2K:
51                                 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
52                                 break;
53                         case CODEC_JP2:
54                                 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
55                                 break;
56                         default:
57                                 opj_free(cio);
58                                 return NULL;
59                 }
60                 cio->length = cp->tdx * cp->tdy * cp->tw * cp->th * 4;
61                 cio->buffer = (unsigned char *)opj_malloc(cio->length);
62                 if(!cio->buffer) {
63                         opj_free(cio);
64                         return NULL;
65                 }
66         }
67         else {
68                 opj_free(cio);
69                 return NULL;
70         }
71
72         /* Initialize byte IO */
73         cio->start = cio->buffer;
74         cio->end = cio->buffer + cio->length;
75         cio->bp = cio->buffer;
76
77         return cio;
78 }
79
80 void opj_cio_close(opj_cio_t *cio) {
81         if(cio) {
82                 if(cio->openmode == OPJ_STREAM_WRITE) {
83                         /* destroy the allocated buffer */
84                         opj_free(cio->buffer);
85                 }
86                 /* destroy the cio */
87                 opj_free(cio);
88         }
89 }
90
91
92 /* ----------------------------------------------------------------------- */
93
94 /*
95  * Get position in byte stream.
96  */
97 int cio_tell(opj_cio_t *cio) {
98         return cio->bp - cio->start;
99 }
100
101 /*
102  * Set position in byte stream.
103  *
104  * pos : position, in number of bytes, from the beginning of the stream
105  */
106 void cio_seek(opj_cio_t *cio, int pos) {
107         cio->bp = cio->start + pos;
108 }
109
110 /*
111  * Number of bytes left before the end of the stream.
112  */
113 int cio_numbytesleft(opj_cio_t *cio) {
114         return cio->end - cio->bp;
115 }
116
117 /*
118  * Get pointer to the current position in the stream.
119  */
120 unsigned char *cio_getbp(opj_cio_t *cio) {
121         return cio->bp;
122 }
123
124 /*
125  * Write a byte.
126  */
127 bool cio_byteout(opj_cio_t *cio, unsigned char v) {
128         if (cio->bp >= cio->end) {
129                 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
130                 return false;
131         }
132         *cio->bp++ = v;
133         return true;
134 }
135
136 /*
137  * Read a byte.
138  */
139 unsigned char cio_bytein(opj_cio_t *cio) {
140         if (cio->bp >= cio->end) {
141                 opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n");
142                 return 0;
143         }
144         return *cio->bp++;
145 }
146
147 /*
148  * Write some bytes.
149  *
150  * v : value to write
151  * n : number of bytes to write
152  */
153 unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
154         int i;
155         for (i = n - 1; i >= 0; i--) {
156                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
157                         return 0;
158         }
159         return n;
160 }
161
162 /*
163  * Read some bytes.
164  *
165  * n : number of bytes to read
166  *
167  * return : value of the n bytes read
168  */
169 unsigned int cio_read(opj_cio_t *cio, int n) {
170         int i;
171         unsigned int v;
172         v = 0;
173         for (i = n - 1; i >= 0; i--) {
174                 v += cio_bytein(cio) << (i << 3);
175         }
176         return v;
177 }
178
179 /* 
180  * Skip some bytes.
181  *
182  * n : number of bytes to skip
183  */
184 void cio_skip(opj_cio_t *cio, int n) {
185         cio->bp += n;
186 }
187
188
189