~ubuntu-branches/ubuntu/warty/xmedcon/warty

« back to all changes in this revision

Viewing changes to libs/dicom/bit.c

  • Committer: Bazaar Package Importer
  • Author(s): Roland Marcus Rutschmann
  • Date: 2004-06-07 09:00:14 UTC
  • Revision ID: james.westby@ubuntu.com-20040607090014-t39n52qc9zjqqqkh
Tags: upstream-0.9.6
ImportĀ upstreamĀ versionĀ 0.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************
 
2
 * libdicom by Tony Voet *
 
3
 *************************/
 
4
/*
 
5
 * $Id: bit.c,v 1.1 2002/10/14 22:03:47 enlf Exp $
 
6
 */
 
7
 
 
8
#include "dicom.h"
 
9
 
 
10
union
 
11
{
 
12
  U32   *u32;
 
13
  U16   *u16;
 
14
  U8    *u8;
 
15
}
 
16
source;
 
17
 
 
18
U32     cache32;
 
19
U16     cache16;
 
20
U8      cache8;
 
21
 
 
22
int left;
 
23
 
 
24
/*******
 
25
 * bit *
 
26
 *******/
 
27
 
 
28
void dicom_bit(void *data)
 
29
{
 
30
  dicom_log(DEBUG,"dicom_bit()");
 
31
 
 
32
  source.u32=data;
 
33
  left=0;
 
34
}
 
35
 
 
36
/**********
 
37
 * 8 skip *
 
38
 **********/
 
39
 
 
40
void dicom_8_skip(int bit)
 
41
{
 
42
  if (!bit)
 
43
    return;
 
44
 
 
45
  if (bit<left)
 
46
  {
 
47
    cache8<<=bit;
 
48
    left-=bit;
 
49
  }
 
50
  else
 
51
  {
 
52
    bit-=left;
 
53
 
 
54
    cache8=*source.u8++;
 
55
    left=8;
 
56
 
 
57
    if (bit)
 
58
      dicom_8_skip(bit);
 
59
  }
 
60
}
 
61
 
 
62
/***********
 
63
 * 16 skip *
 
64
 ***********/
 
65
 
 
66
void dicom_16_skip(int bit)
 
67
{
 
68
  if (!bit)
 
69
    return;
 
70
 
 
71
  if (bit<left)
 
72
  {
 
73
    cache16<<=bit;
 
74
    left-=bit;
 
75
  }
 
76
  else
 
77
  {
 
78
    bit-=left;
 
79
 
 
80
    cache16=*source.u16++;
 
81
    left=16;
 
82
 
 
83
    if (bit)
 
84
      dicom_16_skip(bit);
 
85
  }
 
86
}
 
87
 
 
88
/***********
 
89
 * 32 skip *
 
90
 ***********/
 
91
 
 
92
void dicom_32_skip(int bit)
 
93
{
 
94
  if (!bit)
 
95
    return;
 
96
 
 
97
  if (bit<left)
 
98
  {
 
99
    cache32<<=bit;
 
100
    left-=bit;
 
101
  }
 
102
  else
 
103
  {
 
104
    bit-=left;
 
105
 
 
106
    cache32=*source.u32++;
 
107
    left=32;
 
108
 
 
109
    if (bit)
 
110
      dicom_32_skip(bit);
 
111
  }
 
112
}
 
113
 
 
114
/**********
 
115
 * 8 read *
 
116
 **********/
 
117
 
 
118
U32 dicom_8_read(int bit)
 
119
{
 
120
  U32 result;
 
121
 
 
122
  if (!bit)
 
123
    return 0;
 
124
 
 
125
  if (bit<left)
 
126
  {
 
127
    result=cache8>>(8-bit);
 
128
    cache8<<=bit;
 
129
    left-=bit;
 
130
  }
 
131
  else
 
132
  {
 
133
    result=cache8>>(8-left);
 
134
    bit-=left;
 
135
 
 
136
    cache8=*source.u8++;
 
137
    left=8;
 
138
 
 
139
    if (!bit)
 
140
      return result;
 
141
 
 
142
    result<<=bit;
 
143
    result|=dicom_8_read(bit);
 
144
  }
 
145
 
 
146
  return result;
 
147
}
 
148
 
 
149
/***********
 
150
 * 16 read *
 
151
 ***********/
 
152
 
 
153
U32 dicom_16_read(int bit)
 
154
{
 
155
  U32 result;
 
156
 
 
157
  if (!bit)
 
158
    return 0;
 
159
 
 
160
  if (bit<left)
 
161
  {
 
162
    result=cache16>>(16-bit);
 
163
    cache16<<=bit;
 
164
    left-=bit;
 
165
  }
 
166
  else
 
167
  {
 
168
    result=cache16>>(16-left);
 
169
    bit-=left;
 
170
 
 
171
    cache16=*source.u16++;
 
172
    left=16;
 
173
 
 
174
    if (!bit)
 
175
      return result;
 
176
 
 
177
    result<<=bit;
 
178
    result|=dicom_16_read(bit);
 
179
  }
 
180
 
 
181
  return result;
 
182
}
 
183
 
 
184
/***********
 
185
 * 32 read *
 
186
 ***********/
 
187
 
 
188
U32 dicom_32_read(int bit)
 
189
{
 
190
  U32 result;
 
191
 
 
192
  if (!bit)
 
193
    return 0;
 
194
 
 
195
  if (bit<left)
 
196
  {
 
197
    result=cache32>>(32-bit);
 
198
    cache32<<=bit;
 
199
    left-=bit;
 
200
  }
 
201
  else
 
202
  {
 
203
    result=cache32>>(32-left);
 
204
    bit-=left;
 
205
 
 
206
    cache32=*source.u32++;
 
207
    left=32;
 
208
 
 
209
    if (!bit)
 
210
      return result;
 
211
 
 
212
    result<<=bit;
 
213
    result|=dicom_32_read(bit);
 
214
  }
 
215
 
 
216
  return result;
 
217
}
 
218
 
 
219
/* eNlf: BEGIN - support for 12bit unpacking */
 
220
/*************
 
221
 * 12 unpack *
 
222
 *************/
 
223
 
 
224
/* 2 pix 12bit =     [0xABCDEF]      */
 
225
/* 2 pix 16bit = [0x0ABD] + [0x0FCE] */
 
226
U16 mdc_dicom_12_unpack(int pix)
 
227
{
 
228
  U16 result;
 
229
  U8  b0, b1, b2;
 
230
  switch (pix) {
 
231
    case 1: /* ABD-part (1st pix) */
 
232
        b0 = *source.u8++;
 
233
        b1 = *source.u8;
 
234
        result = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
 
235
                      /* A */          /* B */            /* D */
 
236
        break;
 
237
    case 2: /* FCE-part (2nd pix) */
 
238
        b1 = *source.u8++;
 
239
        b2 = *source.u8++;
 
240
        result = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
 
241
                      /* F */          /* C */            /* E */
 
242
        break;
 
243
    default:
 
244
        result = 0;
 
245
  }
 
246
 
 
247
  return result;
 
248
}
 
249
/* eNlf: END   - support for 12 bits unpacking */