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