/src/freeimage-svn/FreeImage/trunk/Source/LibOpenJPEG/bio.c
Line | Count | Source (jump to first uncovered line) |
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 opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b); |
46 | | /** |
47 | | Read a bit |
48 | | @param bio BIO handle |
49 | | @return Returns the read bit |
50 | | */ |
51 | | static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio); |
52 | | /** |
53 | | Write a byte |
54 | | @param bio BIO handle |
55 | | @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise |
56 | | */ |
57 | | static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio); |
58 | | /** |
59 | | Read a byte |
60 | | @param bio BIO handle |
61 | | @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise |
62 | | */ |
63 | | static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio); |
64 | | |
65 | | /*@}*/ |
66 | | |
67 | | /*@}*/ |
68 | | |
69 | | /* |
70 | | ========================================================== |
71 | | local functions |
72 | | ========================================================== |
73 | | */ |
74 | | |
75 | 0 | OPJ_BOOL opj_bio_byteout(opj_bio_t *bio) { |
76 | 0 | bio->buf = (bio->buf << 8) & 0xffff; |
77 | 0 | bio->ct = bio->buf == 0xff00 ? 7 : 8; |
78 | 0 | if (bio->bp >= bio->end) { |
79 | 0 | return OPJ_FALSE; |
80 | 0 | } |
81 | 0 | *bio->bp++ = (OPJ_BYTE)(bio->buf >> 8); |
82 | 0 | return OPJ_TRUE; |
83 | 0 | } |
84 | | |
85 | 0 | OPJ_BOOL opj_bio_bytein(opj_bio_t *bio) { |
86 | 0 | bio->buf = (bio->buf << 8) & 0xffff; |
87 | 0 | bio->ct = bio->buf == 0xff00 ? 7 : 8; |
88 | 0 | if (bio->bp >= bio->end) { |
89 | 0 | return OPJ_FALSE; |
90 | 0 | } |
91 | 0 | bio->buf |= *bio->bp++; |
92 | 0 | return OPJ_TRUE; |
93 | 0 | } |
94 | | |
95 | 0 | void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b) { |
96 | 0 | if (bio->ct == 0) { |
97 | 0 | opj_bio_byteout(bio); /* MSD: why not check the return value of this function ? */ |
98 | 0 | } |
99 | 0 | bio->ct--; |
100 | 0 | bio->buf |= b << bio->ct; |
101 | 0 | } |
102 | | |
103 | 0 | OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio) { |
104 | 0 | if (bio->ct == 0) { |
105 | 0 | opj_bio_bytein(bio); /* MSD: why not check the return value of this function ? */ |
106 | 0 | } |
107 | 0 | bio->ct--; |
108 | 0 | return (bio->buf >> bio->ct) & 1; |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | ========================================================== |
113 | | Bit Input/Output interface |
114 | | ========================================================== |
115 | | */ |
116 | | |
117 | 0 | opj_bio_t* opj_bio_create(void) { |
118 | 0 | opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t)); |
119 | 0 | return bio; |
120 | 0 | } |
121 | | |
122 | 0 | void opj_bio_destroy(opj_bio_t *bio) { |
123 | 0 | if(bio) { |
124 | 0 | opj_free(bio); |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | ptrdiff_t opj_bio_numbytes(opj_bio_t *bio) { |
129 | 0 | return (bio->bp - bio->start); |
130 | 0 | } |
131 | | |
132 | 0 | void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) { |
133 | 0 | bio->start = bp; |
134 | 0 | bio->end = bp + len; |
135 | 0 | bio->bp = bp; |
136 | 0 | bio->buf = 0; |
137 | 0 | bio->ct = 8; |
138 | 0 | } |
139 | | |
140 | 0 | void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) { |
141 | 0 | bio->start = bp; |
142 | 0 | bio->end = bp + len; |
143 | 0 | bio->bp = bp; |
144 | 0 | bio->buf = 0; |
145 | 0 | bio->ct = 0; |
146 | 0 | } |
147 | | |
148 | 0 | void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n) { |
149 | 0 | OPJ_UINT32 i; |
150 | 0 | for (i = n - 1; i < n; i--) { |
151 | 0 | opj_bio_putbit(bio, (v >> i) & 1); |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | 0 | OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n) { |
156 | 0 | OPJ_UINT32 i; |
157 | 0 | OPJ_UINT32 v; |
158 | 0 | v = 0; |
159 | 0 | for (i = n - 1; i < n; i--) { |
160 | 0 | v += opj_bio_getbit(bio) << i; |
161 | 0 | } |
162 | 0 | return v; |
163 | 0 | } |
164 | | |
165 | 0 | OPJ_BOOL opj_bio_flush(opj_bio_t *bio) { |
166 | 0 | bio->ct = 0; |
167 | 0 | if (! opj_bio_byteout(bio)) { |
168 | 0 | return OPJ_FALSE; |
169 | 0 | } |
170 | 0 | if (bio->ct == 7) { |
171 | 0 | bio->ct = 0; |
172 | 0 | if (! opj_bio_byteout(bio)) { |
173 | 0 | return OPJ_FALSE; |
174 | 0 | } |
175 | 0 | } |
176 | 0 | return OPJ_TRUE; |
177 | 0 | } |
178 | | |
179 | 0 | OPJ_BOOL opj_bio_inalign(opj_bio_t *bio) { |
180 | 0 | bio->ct = 0; |
181 | 0 | if ((bio->buf & 0xff) == 0xff) { |
182 | 0 | if (! opj_bio_bytein(bio)) { |
183 | 0 | return OPJ_FALSE; |
184 | 0 | } |
185 | 0 | bio->ct = 0; |
186 | 0 | } |
187 | 0 | return OPJ_TRUE; |
188 | 0 | } |