]> Creatis software - gdcm.git/blob - src/gdcmRLE.cxx
BUG: doh, remove empty file
[gdcm.git] / src / gdcmRLE.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRLE.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/06 22:31:31 $
7   Version:   $Revision: 1.24 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include <stdio.h>
20 #include "gdcmFile.h"
21 #include <ctype.h>     // For isprint()
22
23 #define str2num(str, typeNum) *((typeNum *)(str))
24
25 //-----------------------------------------------------------------------------
26 /**
27  * \ingroup   gdcmFile
28  * \brief     Reads a 'Run Length Encoded' Dicom encapsulated file
29  * @param     fp already open File Pointer
30  * @param     image_buffer destination Address (in caller's memory space) 
31  *            at which the pixel data should be copied 
32  * @return    Boolean 
33  */
34 bool gdcmFile::gdcm_read_RLE_file (FILE* fp,void* image_buffer) {
35    char * im = (char *)image_buffer;
36    long uncompressedSegmentSize = Header->GetXSize() * Header->GetYSize();
37    
38
39    // Loop on the frame[s]
40    for( gdcmRLEFramesInfo::RLEFrameList::iterator
41         it  = Header->RLEInfo.Frames.begin();
42         it != Header->RLEInfo.Frames.end();
43       ++it )
44    {
45        std::cout << "...new frame...\n ";
46       // Loop on the fragments
47       for( unsigned int k = 1; k <= (*it)->NumberFragments; k++ )
48       {
49          fseek( fp, (*it)->Offset[k] ,SEEK_SET);  
50          gdcm_read_RLE_fragment( &im, (*it)->Length[k],
51                                  uncompressedSegmentSize, fp );
52       }
53    }
54
55    if (Header->GetBitsAllocated()==16) { // try to deal with RLE 16 Bits
56    
57       im = (char *)image_buffer;
58          //  need to make 16 Bits Pixels from Low Byte and Hight Byte 'Planes'
59
60       int l = Header->GetXSize()*Header->GetYSize();
61       int nbFrames = Header->GetZSize();
62
63       char * newDest = new char[l*nbFrames*2];
64       char *x  = newDest;
65       char * a = (char *)image_buffer;
66       char * b = a + l;
67
68       for (int i=0;i<nbFrames;i++) {
69          for (int j=0;j<l; j++) {
70             *(x++) = *(a++);
71             *(x++) = *(b++);
72          }
73       }
74       memmove(image_buffer,newDest,ImageDataSize);
75       delete[] newDest;
76    }
77       
78    return true;
79 }
80
81
82 // ----------------------------------------------------------------------------
83 // RLE LossLess Fragment
84 int gdcmFile::gdcm_read_RLE_fragment(char** areaToRead, long lengthToDecode, 
85                                      long uncompressedSegmentSize, FILE* fp) {
86    (void)lengthToDecode; //FIXME
87    int count;
88    long numberOfOutputBytes=0;
89    char n, car;
90
91    while( numberOfOutputBytes < uncompressedSegmentSize )
92    {
93       fread(&n,sizeof(char),1,fp);
94       count=n;
95       if (count >= 0 && count <= 127) {
96          fread(*areaToRead,(count+1)*sizeof(char),1,fp);
97          *areaToRead+=count+1;
98          numberOfOutputBytes+=count+1;
99       } else {
100          if (count <= -1 && count >= -127) {
101             fread(&car,sizeof(char),1,fp);
102             for(int i=0; i<-count+1; i++) {
103                (*areaToRead)[i]=car;  
104             }
105             *areaToRead+=(-count+1);
106             numberOfOutputBytes+=(-count+1); 
107          }
108       } 
109       // if count = 128 output nothing (See : PS 3.5-2003 Page 86)
110    } 
111    return 1;
112 }
113
114 // ----------------------------------------------------------------------------