changed one of the 9-7 coefficients (value closer to the one in the std)
[openjpeg.git] / libopenjpeg / cio.c
1 /*
2  * Copyright (c) 2001-2002, David Janssens
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "cio.h"
28 #include <setjmp.h>
29 #include <memory.h>
30
31 static unsigned char *cio_start;        /* pointer to the start of the stream */
32 static unsigned char *cio_end;  /* pointer to the end of the stream */
33 static unsigned char *cio_bp;   /* pointer to the present position */
34
35 extern jmp_buf j2k_error;
36
37 /* 
38  * Number of bytes written.
39  */
40 int cio_numbytes()
41 {
42   return cio_bp - cio_start;
43 }
44
45 /*
46  * Get position in byte stream.
47  */
48 int cio_tell()
49 {
50   return cio_bp - cio_start;
51 }
52
53 /*
54  * Set position in byte stream.
55  *
56  * pos : position, in number of bytes, from the beginning of the stream
57  */
58 void cio_seek(int pos)
59 {
60   cio_bp = cio_start + pos;
61 }
62
63 /*
64  * Number of bytes left before the end of the stream.
65  */
66 int cio_numbytesleft()
67 {
68   return cio_end - cio_bp;
69 }
70
71 /*
72  * Get pointer to the current position in the stream.
73  */
74 unsigned char *cio_getbp()
75 {
76   return cio_bp;
77 }
78
79 /* 
80  * Initialize byte IO
81  *
82  * bp  : destination/source stream
83  * len : length of the stream
84  */
85 void cio_init(unsigned char *bp, int len)
86 {
87   cio_start = bp;
88   cio_end = bp + len;
89   cio_bp = bp;
90 }
91
92 /*
93  * Write a byte.
94  */
95 void cio_byteout(unsigned char v)
96 {
97   if (cio_bp >= cio_end)
98     longjmp(j2k_error, 1);
99   *cio_bp++ = v;
100
101 }
102
103 /*
104  * Read a byte.
105  */
106 unsigned char cio_bytein()
107 {
108   if (cio_bp >= cio_end)
109     longjmp(j2k_error, 1);
110   return *cio_bp++;
111 }
112
113 /*
114  * Write some bytes.
115  *
116  * v : value to write
117  * n : number of bytes to write
118  */
119 void cio_write(unsigned int v, int n)
120 {
121   int i;
122   for (i = n - 1; i >= 0; i--) {
123     cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));
124   }
125 }
126
127 /*
128  * Read some bytes.
129  *
130  * n : number of bytes to read
131  *
132  * return : value of the n bytes read
133  */
134 unsigned int cio_read(int n)
135 {
136   int i;
137   unsigned int v;
138   v = 0;
139   for (i = n - 1; i >= 0; i--) {
140     v += cio_bytein() << (i << 3);
141   }
142   return v;
143 }
144
145 /* 
146  * Skip some bytes.
147  *
148  * n : number of bytes to skip
149  */
150 void cio_skip(int n)
151 {
152   cio_bp += n;
153 }
154
155 /* 
156  * Read n bytes, copy to buffer
157  *
158  * n : number of bytes to transfer
159  */
160 void cio_read_to_buf(unsigned char* src_buf, int n)/* Glenn adds */
161 {
162   if (cio_bp + n > cio_end)
163     longjmp(j2k_error, 1);
164   memcpy(cio_bp, src_buf, n);
165   cio_bp += n;
166 }
167
168 /* 
169  * Write n bytes, copy from buffer
170  *
171  * n : number of bytes to transfer
172  */
173 void cio_write_from_buf(unsigned char* dest_buf, int n)/* Glenn adds */
174 {
175   if (cio_bp + n > cio_end)
176     longjmp(j2k_error, 1);
177   memcpy(dest_buf, cio_bp, n);
178   cio_bp += n;
179 }