2 //-----------------------------------------------------------------------------
5 #include "jpeg/ljpg/jpegless.h"
7 //-----------------------------------------------------------------------------
8 // Constructor / Destructor
11 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
12 * file (see SetFileName, SetDcmTag and Write)
13 * Opens (in read only and when possible) an existing file and checks
14 * for DICOM compliance. Returns NULL on failure.
15 * \Note the in-memory representation of all available tags found in
16 * the DICOM header is post-poned to first header information access.
17 * This avoid a double parsing of public part of the header when
18 * one sets an a posteriori shadow dictionary (efficiency can be
19 * seen as a side effect).
21 * @param header file to be opened for reading datas
26 gdcmFile::gdcmFile(gdcmHeader *header) {
30 if (Header->IsReadable())
31 SetPixelDataSizeFromHeader();
36 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
37 * file (see SetFileName, SetDcmTag and Write)
38 * Opens (in read only and when possible) an existing file and checks
39 * for DICOM compliance. Returns NULL on failure.
40 * \Note the in-memory representation of all available tags found in
41 * the DICOM header is post-poned to first header information access.
42 * This avoid a double parsing of public part of the header when
43 * one sets an a posteriori shadow dictionary (efficiency can be
44 * seen as a side effect).
45 * @param filename file to be opened for parsing
48 gdcmFile::gdcmFile(std::string & filename) {
49 Header=new gdcmHeader(filename.c_str());
52 if (Header->IsReadable())
53 SetPixelDataSizeFromHeader();
56 gdcmFile::gdcmFile(const char * filename) {
57 Header=new gdcmHeader(filename);
60 if (Header->IsReadable())
61 SetPixelDataSizeFromHeader();
66 * \brief canonical destructor
67 * \Note If the gdcmHeader is created by the gdcmFile, it is destroyed
70 gdcmFile::~gdcmFile(void) {
76 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
86 gdcmHeader *gdcmFile::GetHeader(void) {
92 * \brief computes the length (in bytes) to ALLOCATE to receive the
93 * image(s) pixels (multiframes taken into account)
94 * \warning : it is NOT the group 7FE0 length
95 * (no interest for compressed images).
96 * @return length to allocate
98 void gdcmFile::SetPixelDataSizeFromHeader(void) {
99 // see PS 3.3-2003 : C.7.6.3.2.1
109 // YBR_FULL_422 (no LUT, no Palette)
115 // ex : gdcm-US-ALOKA-16.dcm
116 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
117 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
118 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
120 // ex : OT-PAL-8-face.dcm
121 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
122 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
123 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
127 str_nb=Header->GetEntryByNumber(0x0028,0x0100);
128 if (str_nb == GDCM_UNFOUND ) {
131 nb = atoi(str_nb.c_str() );
132 if (nb == 12) nb =16;
134 lgrTotale = lgrTotaleRaw = Header->GetXSize() * Header->GetYSize()
135 * Header->GetZSize() * (nb/8)* Header->GetSamplesPerPixel();
136 std::string str_PhotometricInterpretation =
137 Header->GetEntryByNumber(0x0028,0x0004);
139 /*if ( str_PhotometricInterpretation == "PALETTE COLOR " )*/
140 // pb when undealt Segmented Palette Color
142 if (Header->HasLUT()) {
149 * \brief Returns the size (in bytes) of required memory to hold
150 * the pixel data represented in this file.
151 * @return The size of pixel data in bytes.
153 size_t gdcmFile::GetImageDataSize(void) {
159 * \brief Returns the size (in bytes) of required memory to hold
160 * \ the pixel data represented in this file, when user DOESN'T want
161 * \ to get RGB pixels image when it's stored as a PALETTE COLOR image
162 * \ - the (vtk) user is supposed to know how deal with LUTs -
163 * \warning to be used with GetImagePixelsRaw()
164 * @return The size of pixel data in bytes.
166 size_t gdcmFile::GetImageDataSizeRaw(void) {
167 return (lgrTotaleRaw);
172 * \brief Allocates necessary memory, copies the pixel data
173 * (image[s]/volume[s]) to newly allocated zone.
174 * Transforms YBR pixels into RGB pixels if any
175 * Transforms 3 planes R, G, B into a single RGB Plane
176 * Transforms single Grey plane + 3 Palettes into a RGB Plane
177 * @return Pointer to newly allocated pixel data.
178 * \ NULL if alloc fails
180 void * gdcmFile::GetImageData (void) {
181 PixelData = (void *) malloc(lgrTotale);
183 GetImageDataIntoVector(PixelData, lgrTotale);
189 * \brief Copies at most MaxSize bytes of pixel data to caller's
191 * \warning This function was designed to avoid people that want to build
192 * a volume from an image stack to need first to get the image pixels
193 * and then move them to the volume area.
194 * It's absolutely useless for any VTK user since vtk chooses
195 * to invert the lines of an image, that is the last line comes first
196 * (for some axis related reasons?). Hence he will have
197 * to load the image line by line, starting from the end.
198 * VTK users have to call GetImageData
200 * @param destination Address (in caller's memory space) at which the
201 * pixel data should be copied
202 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
203 * is not sufficient to hold the pixel data the copy is not
204 * executed (i.e. no partial copy).
205 * @return On success, the number of bytes actually copied. Zero on
206 * failure e.g. MaxSize is lower than necessary.
208 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
209 size_t l = GetImageDataIntoVectorRaw (destination, MaxSize);
211 if (!Header->HasLUT())
214 // from Lut R + Lut G + Lut B
215 unsigned char * newDest = (unsigned char *)malloc(lgrTotale);
216 unsigned char * a = (unsigned char *)destination;
217 unsigned char * lutRGBA = Header->GetLUTRGBA();
219 int l = lgrTotaleRaw;
220 memmove(newDest, destination, l);// move Gray pixels to temp area
222 for (int i=0;i<l; i++) { // Build RGB Pixels
230 // now, it's an RGB image
231 // Lets's write it in the Header
233 // CreateOrReplaceIfExist ?
235 std::string spp = "3"; // Samples Per Pixel
236 Header->SetEntryByNumber(spp,0x0028,0x0002);
237 std::string rgb= "RGB "; // Photometric Interpretation
238 Header->SetEntryByNumber(rgb,0x0028,0x0004);
239 std::string planConfig = "0"; // Planar Configuration
240 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
243 // need to make RGB Pixels (?)
244 // from grey Pixels (?!)
245 // and Gray Lut (!?!)
246 // or Segmented xxx Palette Color Lookup Table Data and so on
248 // Oops! I get one (gdcm-US-ALOKA-16.dcm)
249 // No idea how to manage such an image
250 // It seems that *no Dicom Viewer* has any idea :-(
251 // Segmented xxx Palette Color are *more* than 65535 long ?!?
253 std::string rgb= "MONOCHROME1 "; // Photometric Interpretation
254 Header->SetEntryByNumber(rgb,0x0028,0x0004);
256 // TODO : Drop Palette Color out of the Header?
262 * \brief Allocates necessary memory, copies the pixel data
263 * (image[s]/volume[s]) to newly allocated zone.
264 * Transforms YBR pixels into RGB pixels if any
265 * Transforms 3 planes R, G, B into a single RGB Plane
266 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
267 * @return Pointer to newly allocated pixel data.
268 * \ NULL if alloc fails
270 void * gdcmFile::GetImageDataRaw (void) {
271 if (Header->HasLUT())
272 lgrTotale /= 3; // TODO Let gdcmHeadar user a chance
273 // to get the right value
274 // Create a member lgrTotaleRaw ???
275 PixelData = (void *) malloc(lgrTotale);
277 GetImageDataIntoVectorRaw(PixelData, lgrTotale);
283 * \brief Copies at most MaxSize bytes of pixel data to caller's
285 * \warning This function was designed to avoid people that want to build
286 * a volume from an image stack to need first to get the image pixels
287 * and then move them to the volume area.
288 * It's absolutely useless for any VTK user since vtk chooses
289 * to invert the lines of an image, that is the last line comes first
290 * (for some axis related reasons?). Hence he will have
291 * to load the image line by line, starting from the end.
292 * VTK users hace to call GetImageData
293 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
294 * into a single RGB Pixels Plane
295 * the (VTK) user will manage the palettes
297 * @param destination Address (in caller's memory space) at which the
298 * pixel data should be copied
299 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
300 * is not sufficient to hold the pixel data the copy is not
301 * executed (i.e. no partial copy).
302 * @return On success, the number of bytes actually copied. Zero on
303 * failure e.g. MaxSize is lower than necessary.
305 size_t gdcmFile::GetImageDataIntoVectorRaw (void* destination, size_t MaxSize) {
307 int nb, nbu, highBit, signe;
308 std::string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
310 if ( lgrTotale > MaxSize ) {
311 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
312 "than caller's expected MaxSize");
316 (void)ReadPixelData(destination);
318 // Number of Bits Allocated for storing a Pixel
319 str_nb = Header->GetEntryByNumber(0x0028,0x0100);
320 if (str_nb == GDCM_UNFOUND ) {
323 nb = atoi(str_nb.c_str() );
325 // Number of Bits actually used
326 str_nbu=Header->GetEntryByNumber(0x0028,0x0101);
327 if (str_nbu == GDCM_UNFOUND ) {
330 nbu = atoi(str_nbu.c_str() );
333 str_highBit=Header->GetEntryByNumber(0x0028,0x0102);
334 if (str_highBit == GDCM_UNFOUND ) {
337 highBit = atoi(str_highBit.c_str() );
342 str_signe=Header->GetEntryByNumber(0x0028,0x0103);
343 if (str_signe == GDCM_UNFOUND ) {
344 signe = 0; // default is unsigned
346 signe = atoi(str_signe.c_str() );
349 // re arange bytes inside the integer (processor endianity)
351 SwapZone(destination, Header->GetSwapCode(), lgrTotale, nb);
353 // to avoid pb with some xmedcon breakers images
354 if (nb==16 && nbu<nb && signe==0) {
355 int l = (int)lgrTotale / (nb/8);
356 guint16 *deb = (guint16 *)destination;
357 for(int i = 0; i<l; i++) {
364 // re arange bits inside the bytes
366 int l = (int)lgrTotale / (nb/8);
368 guint16 mask = 0xffff;
369 mask = mask >> (nb-nbu);
370 guint16 *deb = (guint16 *)destination;
371 for(int i = 0; i<l; i++) {
372 *deb = (*deb >> (nbu-highBit-1)) & mask;
375 } else if (nb == 32 ) {
376 guint32 mask = 0xffffffff;
377 mask = mask >> (nb-nbu);
378 guint32 *deb = (guint32 *)destination;
379 for(int i = 0; i<l; i++) {
380 *deb = (*deb >> (nbu-highBit-1)) & mask;
384 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
388 // DO NOT remove this code commented out.
389 // Nobody knows what's expecting you ...
390 // Just to 'see' what was actually read on disk :-(
393 // f2 = fopen("SpuriousFile.RAW","wb");
394 // fwrite(destination,lgrTotale,1,f2);
397 // Deal with the color
398 // -------------------
400 std::string str_PhotometricInterpretation =
401 Header->GetEntryByNumber(0x0028,0x0004);
403 if ( (str_PhotometricInterpretation == "MONOCHROME1 ")
404 || (str_PhotometricInterpretation == "MONOCHROME2 ") ) {
408 // Planar configuration = 0 : Pixels are already RGB
409 // Planar configuration = 1 : 3 planes : R, G, B
410 // Planar configuration = 2 : 1 gray Plane + 3 LUT
412 // Well ... supposed to be !
413 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
414 // PhotometricInterpretation=PALETTE COLOR
415 // and heuristic has to be found :-(
417 int planConf=Header->GetPlanarConfiguration(); // 0028,0006
419 // Whatever Planar Configuration is,
420 // "PALETTE COLOR " implies that we deal with the palette.
421 if (str_PhotometricInterpretation == "PALETTE COLOR ")
426 // Pixels are already RGB
432 if (str_PhotometricInterpretation == "YBR_FULL") {
434 // Warning : YBR_FULL_422 acts as RGB
435 // : we need to make RGB Pixels from Planes Y,cB,cR
437 // to see the tricks about YBR_FULL, YBR_FULL_422,
438 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
439 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
440 // and be *very* affraid
442 int l = Header->GetXSize()*Header->GetYSize();
443 int nbFrames = Header->GetZSize();
445 unsigned char * newDest = (unsigned char*) malloc(lgrTotale);
446 unsigned char *x = newDest;
447 unsigned char * a = (unsigned char *)destination;
448 unsigned char * b = a + l;
449 unsigned char * c = b + l;
452 // TODO : Replace by the 'well known'
453 // integer computation counterpart
454 // see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
455 // for code optimisation
457 for (int i=0;i<nbFrames;i++) {
458 for (int j=0;j<l; j++) {
459 R= 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
460 G= 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
461 B= 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
466 if (R>255.0) R=255.0;
467 if (G>255.0) G=255.0;
468 if (B>255.0) B=255.0;
470 *(x++) = (unsigned char)R;
471 *(x++) = (unsigned char)G;
472 *(x++) = (unsigned char)B;
476 memmove(destination,newDest,lgrTotale);
481 // need to make RGB Pixels from R,G,B Planes
482 // (all the Frames at a time)
484 int l = Header->GetXSize()*Header->GetYSize()*Header->GetZSize();
486 char * newDest = (char*) malloc(lgrTotale);
488 char * a = (char *)destination;
492 for (int j=0;j<l; j++) {
497 memmove(destination,newDest,lgrTotale);
503 // Palettes were found
504 // Let the user deal with them !
507 // now, it's an RGB image
508 // Lets's write it in the Header
510 // CreateOrReplaceIfExist ?
512 std::string spp = "3"; // Samples Per Pixel
513 Header->SetEntryByNumber(spp,0x0028,0x0002);
514 std::string rgb="RGB "; // Photometric Interpretation
515 Header->SetEntryByNumber(rgb,0x0028,0x0004);
517 std::string planConfig = "0"; // Planar Configuration
518 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
520 // TODO : Drop Palette Color out of the Header?
527 * \warning doit-etre etre publique ?
528 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
531 * @param ExpectedSize
535 bool gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
536 Header->SetImageDataSize(ExpectedSize);
538 lgrTotale = ExpectedSize;
544 * \brief Ecrit sur disque les pixels d'UNE image
545 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
546 * Ca sera à l'utilisateur d'appeler son Reader correctement
547 * (Equivalent a IdImaWriteRawFile)
549 * @param fileName name of the file to be created
550 * (any already existing file is over written)
551 * @return false if write fails
554 bool gdcmFile::WriteRawData (std::string fileName) {
556 fp1 = fopen(fileName.c_str(),"wb");
558 printf("Echec ouverture (ecriture) Fichier [%s] \n",fileName.c_str());
561 fwrite (PixelData,lgrTotale, 1, fp1);
568 * \brief Ecrit sur disque UNE image Dicom
569 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
570 * Ca fonctionnera correctement (?) sur processeur Intel
571 * (Equivalent a IdDcmWrite)
573 * @param fileName name of the file to be created
574 * (any already existing file is overwritten)
575 * @return false if write fails
578 bool gdcmFile::WriteDcmImplVR (std::string fileName) {
579 return WriteBase(fileName, ImplicitVR);
585 * @param fileName name of the file to be created
586 * (any already existing file is overwritten)
587 * @return false if write fails
590 bool gdcmFile::WriteDcmImplVR (const char* fileName) {
591 return WriteDcmImplVR (std::string (fileName));
597 * @param fileName name of the file to be created
598 * (any already existing file is overwritten)
599 * @return false if write fails
602 bool gdcmFile::WriteDcmExplVR (std::string fileName) {
603 return WriteBase(fileName, ExplicitVR);
608 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
609 * (a l'attention des logiciels cliniques
610 * qui ne prennent en entrée QUE des images ACR ...
611 * \warning if a DICOM_V3 header is supplied,
612 * groups < 0x0008 and shadow groups are ignored)
613 * \warning NO TEST is performed on processor "Endiannity".
614 * Ca fonctionnera correctement (?) sur processeur Intel
615 * (Equivalent a IdDcmWrite)
617 * @param fileName name of the file to be created
618 * (any already existing file is overwritten)
619 * @return false if write fails
622 bool gdcmFile::WriteAcr (std::string fileName) {
623 return WriteBase(fileName, ACR);
626 //-----------------------------------------------------------------------------
631 * @param fileName name of the file to be created
632 * (any already existing file is overwritten)
633 * @param type file type (ExplicitVR, ImplicitVR, ...)
634 * @return false if write fails
636 bool gdcmFile::WriteBase (std::string fileName, FileType type) {
639 fp1 = fopen(fileName.c_str(),"wb");
641 printf("Echec ouverture (ecriture) Fichier [%s] \n",fileName.c_str());
645 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
647 // writing Dicom File Preamble
648 filePreamble=(char*)calloc(128,1);
649 fwrite(filePreamble,128,1,fp1);
650 fwrite("DICM",4,1,fp1);
653 // --------------------------------------------------------------
654 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
656 // if recognition code tells us we dealt with a LibIDO image
657 // we reproduce on disk the switch between lineNumber and columnNumber
658 // just before writting ...
660 std::string rows, columns;
661 if ( Header->GetFileType() == ACR_LIBIDO){
662 rows = Header->GetEntryByNumber(0x0028, 0x0010);
663 columns = Header->GetEntryByNumber(0x0028, 0x0011);
664 Header->SetEntryByNumber(columns, 0x0028, 0x0010);
665 Header->SetEntryByNumber(rows , 0x0028, 0x0011);
667 // ----------------- End of Special Patch ----------------
669 Header->Write(fp1, type);
671 // --------------------------------------------------------------
672 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
674 // ...and we restore the Header to be Dicom Compliant again
675 // just after writting
677 if (Header->GetFileType() == ACR_LIBIDO){
678 Header->SetEntryByNumber(rows , 0x0028, 0x0010);
679 Header->SetEntryByNumber(columns, 0x0028, 0x0011);
681 // ----------------- End of Special Patch ----------------
683 fwrite(PixelData, lgrTotale, 1, fp1);
688 //-----------------------------------------------------------------------------
692 * \brief Swap the bytes, according to swap code.
693 * \warning not end user intended
694 * @param im area to deal with
695 * @param swap swap code
696 * @param lgr Area Length
697 * @param nb Pixels Bit number
699 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
717 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
718 | ((((unsigned short int*)im)[i])<<8);
722 printf("SWAP value (16 bits) not allowed : %d\n", swap);
733 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
734 fort =((unsigned long int*)im)[i]>>16;
735 fort= (fort>>8) | (fort<<8);
736 faible=(faible>>8) | (faible<<8);
738 ((unsigned long int*)im)[i]=(s32<<16)|fort;
744 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
745 fort=((unsigned long int*)im)[i]>>16;
746 fort= (fort>>8) | (fort<<8);
747 faible=(faible>>8) | (faible<<8);
749 ((unsigned long int*)im)[i]=(s32<<16)|faible;
755 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
756 fort=((unsigned long int*)im)[i]>>16;
758 ((unsigned long int*)im)[i]=(s32<<16)|fort;
763 printf("SWAP value (32 bits) not allowed : %d\n", swap);
770 * \brief Read pixel data from disk (optionaly decompressing) into the
771 * caller specified memory location.
772 * @param destination where the pixel data should be stored.
775 bool gdcmFile::ReadPixelData(void* destination) {
779 if ( !(fp=Header->OpenFile()))
781 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 ) {
785 // ---------------------- Compacted File (12 Bits Per Pixel)
786 /* unpack 12 Bits pixels into 16 Bits pixels */
787 /* 2 pixels 12bit = [0xABCDEF] */
788 /* 2 pixels 16bit = [0x0ABD] + [0x0FCE] */
790 if (Header->GetBitsAllocated()==12) {
791 int nbPixels = Header->GetXSize() * Header->GetYSize();
792 unsigned char b0, b1, b2;
794 unsigned short int* pdestination = (unsigned short int*)destination;
795 for(int p=0;p<nbPixels;p+=2) {
799 //Two steps is necessary to please VC++
800 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
801 /* A */ /* B */ /* D */
802 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
803 /* F */ /* C */ /* E */
805 // Troubles expected on Big-Endian processors ?
812 // ---------------------- Uncompressed File
813 if ( !Header->IsDicomV3() ||
814 Header->IsImplicitVRLittleEndianTransferSyntax() ||
815 Header->IsExplicitVRLittleEndianTransferSyntax() ||
816 Header->IsExplicitVRBigEndianTransferSyntax() ||
817 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
819 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
820 if ( ItemRead != 1 ) {
829 // ---------------------- Run Length Encoding
830 if (Header->IsRLELossLessTransferSyntax()) {
831 bool res = (bool)gdcm_read_RLE_file (fp,destination);
836 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
838 std::string str_nb=Header->GetEntryByNumber(0x0028,0x0100);
839 if (str_nb == GDCM_UNFOUND ) {
842 nb = atoi(str_nb.c_str() );
843 if (nb == 12) nb =16; // ?? 12 should be ACR-NEMA only ?
848 int taille = Header->GetXSize() * Header->GetYSize()
849 * Header->GetSamplesPerPixel();
850 long fragmentBegining; // for ftell, fseek
852 bool jpg2000 = Header->IsJPEG2000();
853 bool jpgLossless = Header->IsJPEGLossless();
856 guint16 ItemTagGr,ItemTagEl;
859 // Position on begining of Jpeg Pixels
861 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
862 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
863 if(Header->GetSwapCode()) {
864 ItemTagGr=Header->SwapShort(ItemTagGr);
865 ItemTagEl=Header->SwapShort(ItemTagEl);
868 if(Header->GetSwapCode())
869 ln=Header->SwapLong(ln); // Basic Offset Table Item length
872 // What is it used for ?!?
873 char *BasicOffsetTableItemValue = (char *)malloc(ln+1);
874 fread(BasicOffsetTableItemValue,ln,1,fp);
877 // first Fragment initialisation
878 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
879 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
880 if(Header->GetSwapCode()) {
881 ItemTagGr=Header->SwapShort(ItemTagGr);
882 ItemTagEl=Header->SwapShort(ItemTagEl);
885 // parsing fragments until Sequence Delim. Tag found
886 while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
887 // --- for each Fragment
890 if(Header->GetSwapCode())
891 ln=Header->SwapLong(ln); // Fragment Item length
893 fragmentBegining=ftell(fp);
895 if (jpg2000) { // JPEG 2000 : call to ???
897 res = (bool)gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
899 } // ------------------------------------- endif (JPEG2000)
901 else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
903 JPEGLosslessDecodeImage (fp, // Reading Fragment pixels
904 (unsigned short *)destination,
905 Header->GetPixelSize()*8* Header->GetSamplesPerPixel(),
907 res=1; // in order not to break the loop
909 } // ------------------------------------- endif (JPEGLossless)
911 else { // JPEG Lossy : call to IJG 6b
913 if (Header->GetBitsStored() == 8) {
914 res = (bool)gdcm_read_JPEG_file (fp,destination); // Reading Fragment pixels
916 res = (bool)gdcm_read_JPEG_file12 (fp,destination);// Reading Fragment pixels
918 } // ------------------------------------- endif (JPEGLossy)
922 destination = (char *)destination + taille * nBytes; // location in user's memory
923 // for next fragment (if any)
925 fseek(fp,fragmentBegining,SEEK_SET); // To be sure we start
926 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
928 ItemTagGr = ItemTagEl =0;
929 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
930 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
931 if(Header->GetSwapCode()) {
932 ItemTagGr=Header->SwapShort(ItemTagGr);
933 ItemTagEl=Header->SwapShort(ItemTagEl);
936 } // endWhile parsing fragments until Sequence Delim. Tag found
941 //-----------------------------------------------------------------------------