2 //-----------------------------------------------------------------------------
3 #include "gdcmCommon.h"
6 #define str2num(str, typeNum) *((typeNum *)(str))
8 //-----------------------------------------------------------------------------
11 * \brief Parse pixel data from disk and *prints* the result
12 * \ For multi-fragment Jpeg/Rle files checking purpose *only*
13 * \ Allows to 'see' if the file *does* conform
14 * \ (some of them do not)
15 * \ with Dicom Part 3, Annex A (PS 3.5-2003, page 58, page 85)
18 bool gdcmFile::ParsePixelData(void) {
19 // DO NOT remove the printf s.
20 // The ONLY purpose of this method is to PRINT the content
23 if ( !(fp=Header->OpenFile()))
26 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 ) {
31 if ( !Header->IsDicomV3() ||
32 Header->IsImplicitVRLittleEndianTransferSyntax() ||
33 Header->IsExplicitVRLittleEndianTransferSyntax() ||
34 Header->IsExplicitVRBigEndianTransferSyntax() ||
35 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
37 printf ("gdcmFile::ParsePixelData : non JPEG/RLE File\n");
42 std::string str_nb=Header->GetEntryByNumber(0x0028,0x0100);
43 if (str_nb == GDCM_UNFOUND ) {
46 nb = atoi(str_nb.c_str() );
49 //int nBytes= nb/8; //FIXME
51 //int taille = Header->GetXSize() * Header->GetYSize() * Header->GetSamplesPerPixel();
53 printf ("Checking the Dicom-encapsulated Jpeg/RLE Pixels\n");
55 guint16 ItemTagGr,ItemTagEl;
58 //char * destination = NULL;
60 // -------------------- for Parsing : Position on begining of Jpeg/RLE Pixels
62 if( !Header->IsRLELossLessTransferSyntax()) {
66 fread(&ItemTagGr,2,1,fp); //Reading (fffe):Basic Offset Table Item Tag Gr
67 fread(&ItemTagEl,2,1,fp); //Reading (e000):Basic Offset Table Item Tag El
68 if(Header->GetSwapCode()) {
69 ItemTagGr=Header->SwapShort(ItemTagGr);
70 ItemTagEl=Header->SwapShort(ItemTagEl);
72 printf ("at %x : ItemTag (should be fffe,e000): %04x,%04x\n",
73 (unsigned)ftellRes,ItemTagGr,ItemTagEl );
76 if(Header->GetSwapCode())
77 ln=Header->SwapLong(ln); // Basic Offset Table Item Length
78 printf("at %x : Basic Offset Table Item Length (\?\?) %d x(%08x)\n",
79 (unsigned)ftellRes,ln,ln);
81 // What is it used for ??
82 char * BasicOffsetTableItemValue= (char *)malloc(ln+1);
83 fread(BasicOffsetTableItemValue,ln,1,fp);
85 for (int i=0;i<ln;i+=4){
86 a=str2num(&BasicOffsetTableItemValue[i],guint32);
87 printf(" x(%08x) %d\n",a,a);
92 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
93 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
94 if(Header->GetSwapCode()) {
95 ItemTagGr=Header->SwapShort(ItemTagGr);
96 ItemTagEl=Header->SwapShort(ItemTagEl);
98 printf ("at %x : ItemTag (should be fffe,e000 or e0dd): %04x,%04x\n",
99 (unsigned)ftellRes,ItemTagGr,ItemTagEl );
101 while ( ( ItemTagGr==0xfffe) && (ItemTagEl!=0xe0dd) ) { // Parse fragments
105 if(Header->GetSwapCode())
106 ln=Header->SwapLong(ln); // length
107 printf(" at %x : fragment length %d x(%08x)\n",
108 (unsigned)ftellRes, ln,ln);
110 // destination += taille * nBytes; // location in user's memory
111 //printf (" Destination will be x(%x) = %d \n",
112 // destination,destination );
114 // ------------------------
115 fseek(fp,ln,SEEK_CUR); // skipping (not reading) fragment pixels
116 // ------------------------
119 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
120 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
121 if(Header->GetSwapCode()) {
122 ItemTagGr=Header->SwapShort(ItemTagGr);
123 ItemTagEl=Header->SwapShort(ItemTagEl);
125 printf ("at %x : ItemTag (should be fffe,e000 or e0dd): %04x,%04x\n",
126 (unsigned)ftellRes,ItemTagGr,ItemTagEl );
132 long RleSegmentLength[15],fragmentLength;
133 guint32 nbRleSegments;
134 guint32 RleSegmentOffsetTable[15];
136 // Basic Offset Table with Item Value
138 fread(&ItemTagGr,2,1,fp); //Reading (fffe):Basic Offset Table Item Tag Gr
139 fread(&ItemTagEl,2,1,fp); //Reading (e000):Basic Offset Table Item Tag El
140 if(Header->GetSwapCode()) {
141 ItemTagGr=Header->SwapShort(ItemTagGr);
142 ItemTagEl=Header->SwapShort(ItemTagEl);
144 printf ("at %x : ItemTag (should be fffe,e000): %04x,%04x\n",
145 (unsigned)ftellRes,ItemTagGr,ItemTagEl );
149 if(Header->GetSwapCode())
150 ln=Header->SwapLong(ln); // Basic Offset Table Item Length
151 printf("at %x : Basic Offset Table Item Length (\?\?) %d x(%08x)\n",
152 (unsigned)ftellRes,ln,ln);
154 // What is it used for ??
155 char * BasicOffsetTableItemValue= (char *)malloc(ln+1);
156 fread(BasicOffsetTableItemValue,ln,1,fp);
158 for (int i=0;i<ln;i+=4){
159 a=str2num(&BasicOffsetTableItemValue[i],guint32);
160 printf(" x(%08x) %d\n",a,a);
165 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
166 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
167 if(Header->GetSwapCode()) {
168 ItemTagGr=Header->SwapShort(ItemTagGr);
169 ItemTagEl=Header->SwapShort(ItemTagEl);
171 printf ("at %x : ItemTag (should be fffe,e000 or e0dd): %04x,%04x\n",
172 (unsigned)ftellRes,ItemTagGr,ItemTagEl );
174 // while 'Sequence Delimiter Item' (fffe,e0dd) not found
175 while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
176 // Parse fragments of the current Fragment (Frame)
178 fread(&fragmentLength,4,1,fp);
179 if(Header->GetSwapCode())
180 fragmentLength=Header->SwapLong(fragmentLength); // length
181 printf(" at %x : 'fragment' length %d x(%08x)\n",
182 (unsigned)ftellRes, (unsigned)fragmentLength,(unsigned)fragmentLength);
184 //------------------ scanning (not reading) fragment pixels
186 fread(&nbRleSegments,4,1,fp); // Reading : Number of RLE Segments
187 if(Header->GetSwapCode())
188 nbRleSegments=Header->SwapLong(nbRleSegments);
189 printf(" Nb of RLE Segments : %d\n",nbRleSegments);
191 for(int k=1; k<=15; k++) { // Reading RLE Segments Offset Table
193 fread(&RleSegmentOffsetTable[k],4,1,fp);
194 if(Header->GetSwapCode())
195 RleSegmentOffsetTable[k]=Header->SwapLong(RleSegmentOffsetTable[k]);
196 printf(" at : %x Offset Segment %d : %d (%x)\n",
197 (unsigned)ftellRes,k,RleSegmentOffsetTable[k],
198 RleSegmentOffsetTable[k]);
201 if (nbRleSegments>1) { // skipping (not reading) RLE Segments
202 for(unsigned int k=1; k<=nbRleSegments-1; k++) {
203 RleSegmentLength[k]= RleSegmentOffsetTable[k+1]
204 - RleSegmentOffsetTable[k];
206 printf (" Segment %d : Length = %d x(%x) Start at %x\n",
207 k,(unsigned)RleSegmentLength[k],(unsigned)RleSegmentLength[k], (unsigned)ftellRes);
208 fseek(fp,RleSegmentLength[k],SEEK_CUR);
211 RleSegmentLength[nbRleSegments]= fragmentLength
212 - RleSegmentOffsetTable[nbRleSegments];
214 printf (" Segment %d : Length = %d x(%x) Start at %x\n",
215 nbRleSegments,(unsigned)RleSegmentLength[nbRleSegments],
216 (unsigned)RleSegmentLength[nbRleSegments],(unsigned)ftellRes);
218 fseek(fp,RleSegmentLength[nbRleSegments],SEEK_CUR);
220 // ------------------ end of scanning fragment pixels
223 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
224 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
225 if(Header->GetSwapCode()) {
226 ItemTagGr=Header->SwapShort(ItemTagGr);
227 ItemTagEl=Header->SwapShort(ItemTagEl);
229 printf ("at %x : ItemTag (should be fffe,e000 or e0dd): %04x,%04x\n",
230 (unsigned)ftellRes,ItemTagGr,ItemTagEl );
236 //-----------------------------------------------------------------------------