Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8436 | maxcodehac | 1 | /* |
2 | * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium |
||
3 | * Copyright (c) 2002-2007, Professor Benoit Macq |
||
4 | * Copyright (c) 2001-2003, David Janssens |
||
5 | * Copyright (c) 2002-2003, Yannick Verschueren |
||
6 | * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe |
||
7 | * Copyright (c) 2005, Herve Drolon, FreeImage Team |
||
8 | * All rights reserved. |
||
9 | * |
||
10 | * Redistribution and use in source and binary forms, with or without |
||
11 | * modification, are permitted provided that the following conditions |
||
12 | * are met: |
||
13 | * 1. Redistributions of source code must retain the above copyright |
||
14 | * notice, this list of conditions and the following disclaimer. |
||
15 | * 2. Redistributions in binary form must reproduce the above copyright |
||
16 | * notice, this list of conditions and the following disclaimer in the |
||
17 | * documentation and/or other materials provided with the distribution. |
||
18 | * |
||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' |
||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
29 | * POSSIBILITY OF SUCH DAMAGE. |
||
30 | */ |
||
31 | |||
32 | #include "opj_includes.h" |
||
33 | |||
34 | /** @defgroup BIO BIO - Individual bit input-output stream */ |
||
35 | /*@{*/ |
||
36 | |||
37 | /** @name Local static functions */ |
||
38 | /*@{*/ |
||
39 | |||
40 | /** |
||
41 | Write a bit |
||
42 | @param bio BIO handle |
||
43 | @param b Bit to write (0 or 1) |
||
44 | */ |
||
45 | static void bio_putbit(opj_bio_t *bio, int b); |
||
46 | /** |
||
47 | Read a bit |
||
48 | @param bio BIO handle |
||
49 | @return Returns the read bit |
||
50 | */ |
||
51 | static int bio_getbit(opj_bio_t *bio); |
||
52 | /** |
||
53 | Write a byte |
||
54 | @param bio BIO handle |
||
55 | @return Returns 0 if successful, returns 1 otherwise |
||
56 | */ |
||
57 | static int bio_byteout(opj_bio_t *bio); |
||
58 | /** |
||
59 | Read a byte |
||
60 | @param bio BIO handle |
||
61 | @return Returns 0 if successful, returns 1 otherwise |
||
62 | */ |
||
63 | static int bio_bytein(opj_bio_t *bio); |
||
64 | |||
65 | /*@}*/ |
||
66 | |||
67 | /*@}*/ |
||
68 | |||
69 | /* |
||
70 | ========================================================== |
||
71 | local functions |
||
72 | ========================================================== |
||
73 | */ |
||
74 | |||
75 | static int bio_byteout(opj_bio_t *bio) { |
||
76 | bio->buf = (bio->buf << 8) & 0xffff; |
||
77 | bio->ct = bio->buf == 0xff00 ? 7 : 8; |
||
78 | if (bio->bp >= bio->end) { |
||
79 | return 1; |
||
80 | } |
||
81 | *bio->bp++ = bio->buf >> 8; |
||
82 | return 0; |
||
83 | } |
||
84 | |||
85 | static int bio_bytein(opj_bio_t *bio) { |
||
86 | bio->buf = (bio->buf << 8) & 0xffff; |
||
87 | bio->ct = bio->buf == 0xff00 ? 7 : 8; |
||
88 | if (bio->bp >= bio->end) { |
||
89 | return 1; |
||
90 | } |
||
91 | bio->buf |= *bio->bp++; |
||
92 | return 0; |
||
93 | } |
||
94 | |||
95 | static void bio_putbit(opj_bio_t *bio, int b) { |
||
96 | if (bio->ct == 0) { |
||
97 | bio_byteout(bio); |
||
98 | } |
||
99 | bio->ct--; |
||
100 | bio->buf |= b << bio->ct; |
||
101 | } |
||
102 | |||
103 | static int bio_getbit(opj_bio_t *bio) { |
||
104 | if (bio->ct == 0) { |
||
105 | bio_bytein(bio); |
||
106 | } |
||
107 | bio->ct--; |
||
108 | return (bio->buf >> bio->ct) & 1; |
||
109 | } |
||
110 | |||
111 | /* |
||
112 | ========================================================== |
||
113 | Bit Input/Output interface |
||
114 | ========================================================== |
||
115 | */ |
||
116 | |||
117 | opj_bio_t* bio_create(void) { |
||
118 | opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t)); |
||
119 | return bio; |
||
120 | } |
||
121 | |||
122 | void bio_destroy(opj_bio_t *bio) { |
||
123 | if(bio) { |
||
124 | opj_free(bio); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | int bio_numbytes(opj_bio_t *bio) { |
||
129 | return (bio->bp - bio->start); |
||
130 | } |
||
131 | |||
132 | void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) { |
||
133 | bio->start = bp; |
||
134 | bio->end = bp + len; |
||
135 | bio->bp = bp; |
||
136 | bio->buf = 0; |
||
137 | bio->ct = 8; |
||
138 | } |
||
139 | |||
140 | void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) { |
||
141 | bio->start = bp; |
||
142 | bio->end = bp + len; |
||
143 | bio->bp = bp; |
||
144 | bio->buf = 0; |
||
145 | bio->ct = 0; |
||
146 | } |
||
147 | |||
148 | void bio_write(opj_bio_t *bio, int v, int n) { |
||
149 | int i; |
||
150 | for (i = n - 1; i >= 0; i--) { |
||
151 | bio_putbit(bio, (v >> i) & 1); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | int bio_read(opj_bio_t *bio, int n) { |
||
156 | int i, v; |
||
157 | v = 0; |
||
158 | for (i = n - 1; i >= 0; i--) { |
||
159 | v += bio_getbit(bio) << i; |
||
160 | } |
||
161 | return v; |
||
162 | } |
||
163 | |||
164 | int bio_flush(opj_bio_t *bio) { |
||
165 | bio->ct = 0; |
||
166 | if (bio_byteout(bio)) { |
||
167 | return 1; |
||
168 | } |
||
169 | if (bio->ct == 7) { |
||
170 | bio->ct = 0; |
||
171 | if (bio_byteout(bio)) { |
||
172 | return 1; |
||
173 | } |
||
174 | } |
||
175 | return 0; |
||
176 | } |
||
177 | |||
178 | int bio_inalign(opj_bio_t *bio) { |
||
179 | bio->ct = 0; |
||
180 | if ((bio->buf & 0xff) == 0xff) { |
||
181 | if (bio_bytein(bio)) { |
||
182 | return 1; |
||
183 | } |
||
184 | bio->ct = 0; |
||
185 | } |
||
186 | return 0; |
||
187 | }><>><>><>><> |