1
/*************************
2
* libdicom by Tony Voet *
3
*************************/
5
* $Id: transform.c,v 1.2 2003/09/03 21:35:53 enlf Exp $
12
static U16 dicom_clut(const CLUT *,U16);
18
int dicom_alloc(SINGLE *single)
21
U16 magic=0x1234,*data,*d;
24
dicom_log(DEBUG,"dicom_alloc()");
28
dicom_log(ERROR,"No image given");
33
dicom_log(WARNING,"Large BitsAllocated");
35
length=single->frames*single->w*single->h*single->samples;
37
data=malloc(length*2);
40
dicom_log(ERROR,"Out of memory");
44
high=single->alloc-single->high-1;
46
low=single->high+1-bit;
50
dicom_bit(single->data);
52
if ( *((U8*)&magic)==0x12 )
53
if (single->alloc==12)
54
for (l=length; l; l-=2)
56
*d++=mdc_dicom_12_unpack(1);
57
*d++=mdc_dicom_12_unpack(2);
60
for (l=length; l; l--)
63
*d++=dicom_32_read(bit);
67
if (single->alloc==16)
68
for (l=length; l; l--)
71
*d++=dicom_16_read(bit);
74
else if (single->alloc==12)
75
for (l=length; l; l-=2)
77
*d++=mdc_dicom_12_unpack(1);
78
*d++=mdc_dicom_12_unpack(2);
81
for (l=length; l; l--)
84
*d++=dicom_8_read(bit);
88
eNlfSafeFree(single->data);
92
single->high=single->bit-1;
101
int dicom_sign(SINGLE *single)
107
dicom_log(DEBUG,"dicom_sign()");
111
dicom_log(ERROR,"No image given");
118
if (single->alloc!=16)
120
dicom_log(ERROR,"BitsAllocated != 16");
124
if (single->high!=single->bit-1)
125
dicom_log(WARNING,"Wrong HighBit");
127
edge=1<<(single->bit-1);
129
length=single->frames*single->w*single->h*single->samples;
132
for (l=length; l; l--,d++)
138
switch(single->photometric)
143
if (single->clut[i].threshold.u16<edge)
144
single->clut[i].threshold.u16+=edge;
146
single->clut[i].threshold.u16-=edge;
149
if (!single->clut[i].data.u16)
150
dicom_log(ERROR,"Missing CLUT");
153
edge=1<<(single->clut[i].bit-1);
154
d=single->clut[i].data.u16;
156
for (l=single->clut[i].size; l; l--,d++)
177
int dicom_planar(SINGLE *single)
181
U16 *frame_s,*frame_d,*s,*d;
183
dicom_log(DEBUG,"dicom_planar()");
187
dicom_log(ERROR,"No image given");
191
if (single->samples<=1)
197
if (single->alloc!=16)
199
dicom_log(ERROR,"BitsAllocated != 16");
203
length=single->w*single->h;
205
frame_d=malloc(length*single->samples*2);
208
dicom_log(ERROR,"Out of memory");
212
for (i=0; i<single->frames; i++)
214
frame_s=(U16*)single->data+i*length*single->samples;
217
for (j=0; j<single->samples; j++)
221
for (l=length; l; l--)
228
memcpy(frame_s,frame_d,length*single->samples*2);
231
eNlfSafeFree(frame_d);
242
int dicom_shift(SINGLE *single)
248
dicom_log(DEBUG,"dicom_shift()");
252
dicom_log(ERROR,"No image given");
256
if (single->photometric==MONOCHROME1 || single->photometric==MONOCHROME2)
259
if (single->alloc!=16)
261
dicom_log(ERROR,"BitsAllocated != 16");
265
switch(single->photometric)
268
shift=15-single->high;
273
length=single->frames*single->w*single->h*single->samples;
276
for (l=length; l; l--)
283
shift=15-single->high;
287
length=single->frames*single->w*single->h;
290
for (l=length; l; l--)
304
shift=16-single->clut[i].bit;
309
d=single->clut[i].data.u16;
311
for (l=single->clut[i].size; l; l--)
314
single->clut[i].bit=16;
325
IMAGE *dicom_transform(SINGLE *single,int parametric)
333
dicom_log(DEBUG,"dicom_transform()");
337
dicom_log(ERROR,"No image given");
341
if (dicom_alloc(single))
344
switch(single->photometric)
348
/* keep original values, either negative and/or quantified */
351
/* make positive, colored files */
352
if (dicom_sign(single))
356
if (dicom_planar(single))
359
if (dicom_shift(single))
362
memset(&image,0,sizeof(IMAGE));
364
image.frames=single->frames;
368
switch(single->photometric)
373
image.data.gray=single->data;
381
if (single->photometric==MONOCHROME1)
382
dicom_invert(&image);
388
if (!single->clut[0].data.u16 ||
389
!single->clut[1].data.u16 ||
390
!single->clut[2].data.u16)
392
dicom_log(ERROR,"Missing CLUT");
402
image.data.rgb=malloc((unsigned)(image.frames*image.w*image.h)*3U);
405
dicom_log(ERROR,"Out of memory");
409
length=image.frames*image.w*image.h;
411
s=(U16*)single->data;
414
switch(single->photometric)
417
for (l=length; l; l--)
419
*d++=dicom_clut(single->clut, *s)>>8;
420
*d++=dicom_clut(single->clut+1,*s)>>8;
421
*d++=dicom_clut(single->clut+2,*s)>>8;
427
for (l=length*3; l; l--)
432
for (l=length; l; l--)
434
dicom_hsv(s[0],s[1],s[2],d);
441
for (l=length; l; l--)
444
*d++=dicom_clut(single->clut, *s)>>8;
445
*d++=dicom_clut(single->clut+1,*s)>>8;
446
*d++=dicom_clut(single->clut+2,*s)>>8;
459
for (l=length; l; l--)
461
*d++=(0xFFFF-*s++)>>8;
462
*d++=(0xFFFF-*s++)>>8;
463
*d++=(0xFFFF-*s++)>>8;
479
static U16 dicom_clut(const CLUT *clut,U16 i)
481
if (i<=clut->threshold.u16)
482
return clut->data.u16[0];
484
i-=clut->threshold.u16;
487
return clut->data.u16[clut->size-1];
489
return clut->data.u16[i];