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).
46 * @param filename file to be opened for parsing
50 gdcmFile::gdcmFile(std::string & filename) {
51 Header=new gdcmHeader(filename.c_str());
54 if (Header->IsReadable())
55 SetPixelDataSizeFromHeader();
58 gdcmFile::gdcmFile(const char * filename) {
59 Header=new gdcmHeader(filename);
62 if (Header->IsReadable())
63 SetPixelDataSizeFromHeader();
68 * \brief Destructor dedicated to writing a new DICOMV3 part10 compliant
69 * file (see SetFileName, SetDcmTag and Write)
70 * \Note If the gdcmHeader is created by the gdcmFile, it is destroyed
74 gdcmFile::~gdcmFile(void) {
80 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
90 gdcmHeader *gdcmFile::GetHeader(void) {
96 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
97 * pixels de l'image ou DES images dans le cas d'un multiframe
99 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
100 * (dans le cas d'images compressees, elle n'a pas de sens).
102 * @return longueur a allouer
104 void gdcmFile::SetPixelDataSizeFromHeader(void) {
105 // see PS 3.3-2003 : C.7.6.3.2.1
115 // YBR_FULL_422 (no LUT, no Palette)
121 // ex : gdcm-US-ALOKA-16.dcm
122 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
123 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
124 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
126 // ex : OT-PAL-8-face.dcm
127 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
128 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
129 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
133 str_nb=Header->GetPubEntryByNumber(0x0028,0x0100);
134 if (str_nb == GDCM_UNFOUND ) {
137 nb = atoi(str_nb.c_str() );
138 if (nb == 12) nb =16;
140 lgrTotale = lgrTotaleRaw = Header->GetXSize() * Header->GetYSize()
141 * Header->GetZSize() * (nb/8)* Header->GetSamplesPerPixel();
142 std::string str_PhotometricInterpretation =
143 Header->GetPubEntryByNumber(0x0028,0x0004);
145 /*if ( str_PhotometricInterpretation == "PALETTE COLOR " )*/
146 // pb when undealt Segmented Palette Color
148 if (Header->HasLUT()) {
155 * \brief Returns the size (in bytes) of required memory to hold
156 * the pixel data represented in this file.
157 * @return The size of pixel data in bytes.
159 size_t gdcmFile::GetImageDataSize(void) {
165 * \brief Returns the size (in bytes) of required memory to hold
166 * \ the pixel data represented in this file, when user DOESN'T want
167 * \ to get RGB pixels image when it's stored as a PALETTE COLOR image
168 * \ - the (vtk) user is supposed to know how deal with LUTs -
169 * \ warning to be used with GetImagePixelsRaw()
170 * @return The size of pixel data in bytes.
172 size_t gdcmFile::GetImageDataSizeRaw(void) {
173 return (lgrTotaleRaw);
178 * \brief Allocates necessary memory, copies the pixel data
179 * (image[s]/volume[s]) to newly allocated zone.
180 * Transforms YBR pixels into RGB pixels if any
181 Transforms 3 planes R, G, B into a single RGB Plane
182 Transforms single Grey plane + 3 Palettes into a RGB Plane
183 * @return Pointer to newly allocated pixel data.
184 * \ NULL if alloc fails
186 void * gdcmFile::GetImageData (void) {
187 PixelData = (void *) malloc(lgrTotale);
189 GetImageDataIntoVector(PixelData, lgrTotale);
195 * \brief Copies at most MaxSize bytes of pixel data to caller's
197 * \warning This function was designed to avoid people that want to build
198 * a volume from an image stack to need first to get the image pixels
199 * and then move them to the volume area.
200 * It's absolutely useless for any VTK user since vtk chooses
201 * to invert the lines of an image, that is the last line comes first
202 * (for some axis related reasons?). Hence he will have
203 * to load the image line by line, starting from the end.
204 * VTK users have to call GetImageData
206 * @param destination Address (in caller's memory space) at which the
207 * pixel data should be copied
208 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
209 * is not sufficient to hold the pixel data the copy is not
210 * executed (i.e. no partial copy).
211 * @return On success, the number of bytes actually copied. Zero on
212 * failure e.g. MaxSize is lower than necessary.
214 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
215 size_t l = GetImageDataIntoVectorRaw (destination, MaxSize);
217 if (!Header->HasLUT())
220 // from Lut R + Lut G + Lut B
221 unsigned char * newDest = (unsigned char *)malloc(lgrTotale);
222 unsigned char * a = (unsigned char *)destination;
223 unsigned char * lutRGBA = Header->GetLUTRGBA();
225 int l = lgrTotaleRaw;
226 memmove(newDest, destination, l);// move Gray pixels to temp area
228 for (int i=0;i<l; i++) { // Build RGB Pixels
236 // now, it's an RGB image
237 // Lets's write it in the Header
239 // CreateOrReplaceIfExist ?
241 std::string spp = "3"; // Samples Per Pixel
242 Header->SetPubEntryByNumber(spp,0x0028,0x0002);
243 std::string rgb= "RGB "; // Photometric Interpretation
244 Header->SetPubEntryByNumber(rgb,0x0028,0x0004);
245 std::string planConfig = "0"; // Planar Configuration
246 Header->SetPubEntryByNumber(planConfig,0x0028,0x0006);
249 // need to make RGB Pixels (?)
250 // from grey Pixels (?!)
251 // and Gray Lut (!?!)
252 // or Segmented xxx Palette Color Lookup Table Data and so on
254 // Well . I'll wait till I find such an image
256 // Oops! I get one (gdcm-US-ALOKA-16.dcm)
257 // No idea how to manage it
258 // It seems that *no Dicom Viewer* has any idea :-(
259 // Segmented xxx Palette Color are *more* than 65535 long ?!?
261 std::string rgb= "MONOCHROME1 "; // Photometric Interpretation
262 Header->SetPubEntryByNumber(rgb,0x0028,0x0004);
266 // TODO : Drop Palette Color out of the Header?
272 * \brief Allocates necessary memory, copies the pixel data
273 * (image[s]/volume[s]) to newly allocated zone.
274 * Transforms YBR pixels into RGB pixels if any
275 Transforms 3 planes R, G, B into a single RGB Plane
276 DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
277 * @return Pointer to newly allocated pixel data.
278 * \ NULL if alloc fails
280 void * gdcmFile::GetImageDataRaw (void) {
281 if (Header->HasLUT())
282 lgrTotale /= 3; // TODO Let gdcmHeadar user a chance
283 // to get the right value
284 // Create a member lgrTotaleRaw ???
285 PixelData = (void *) malloc(lgrTotale);
287 GetImageDataIntoVectorRaw(PixelData, lgrTotale);
293 * \brief Copies at most MaxSize bytes of pixel data to caller's
295 * \warning This function was designed to avoid people that want to build
296 * a volume from an image stack to need first to get the image pixels
297 * and then move them to the volume area.
298 * It's absolutely useless for any VTK user since vtk chooses
299 * to invert the lines of an image, that is the last line comes first
300 * (for some axis related reasons?). Hence he will have
301 * to load the image line by line, starting from the end.
302 * VTK users hace to call GetImageData
303 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
304 * into a single RGB Pixels Plane
305 * the (VTK) user will manage the palettes
307 * @param destination Address (in caller's memory space) at which the
308 * pixel data should be copied
309 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
310 * is not sufficient to hold the pixel data the copy is not
311 * executed (i.e. no partial copy).
312 * @return On success, the number of bytes actually copied. Zero on
313 * failure e.g. MaxSize is lower than necessary.
315 size_t gdcmFile::GetImageDataIntoVectorRaw (void* destination, size_t MaxSize) {
317 int nb, nbu, highBit, signe;
318 std::string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
320 if ( lgrTotale > MaxSize ) {
321 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
322 "than caller's expected MaxSize");
326 (void)ReadPixelData(destination);
328 // Nombre de Bits Alloues pour le stockage d'un Pixel
329 str_nb = Header->GetPubEntryByNumber(0x0028,0x0100);
330 if (str_nb == GDCM_UNFOUND ) {
333 nb = atoi(str_nb.c_str() );
336 // Nombre de Bits Utilises
337 str_nbu=Header->GetPubEntryByNumber(0x0028,0x0101);
338 if (str_nbu == GDCM_UNFOUND ) {
341 nbu = atoi(str_nbu.c_str() );
344 // Position du Bit de Poids Fort
345 str_highBit=Header->GetPubEntryByNumber(0x0028,0x0102);
346 if (str_highBit == GDCM_UNFOUND ) {
349 highBit = atoi(str_highBit.c_str() );
354 str_signe=Header->GetPubEntryByNumber(0x0028,0x0103);
355 if (str_signe == GDCM_UNFOUND ) {
356 signe = 0; // default is unsigned
358 signe = atoi(str_signe.c_str() );
361 // re arange bytes inside the integer
363 SwapZone(destination, Header->GetSwapCode(), lgrTotale, nb);
365 // to avoid pb with some xmedcon breakers images
366 if (nb==16 && nbu<nb && signe==0) {
367 int l = (int)lgrTotale / (nb/8);
368 guint16 *deb = (guint16 *)destination;
369 for(int i = 0; i<l; i++) {
376 // re arange bits inside the bytes
378 int l = (int)lgrTotale / (nb/8);
380 guint16 mask = 0xffff;
381 mask = mask >> (nb-nbu);
382 guint16 *deb = (guint16 *)destination;
383 for(int i = 0; i<l; i++) {
384 *deb = (*deb >> (nbu-highBit-1)) & mask;
387 } else if (nb == 32 ) {
388 guint32 mask = 0xffffffff;
389 mask = mask >> (nb-nbu);
390 guint32 *deb = (guint32 *)destination;
391 for(int i = 0; i<l; i++) {
392 *deb = (*deb >> (nbu-highBit-1)) & mask;
396 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
401 // Just to 'see' what was actually read on disk :-(
402 // Some troubles expected
404 // f2 = fopen("SpuriousFile.raw","wb");
405 // fwrite(destination,lgrTotale,1,f2);
408 // Deal with the color
409 // -------------------
411 std::string str_PhotometricInterpretation =
412 Header->GetPubEntryByNumber(0x0028,0x0004);
414 if ( (str_PhotometricInterpretation == "MONOCHROME1 ")
415 || (str_PhotometricInterpretation == "MONOCHROME2 ") ) {
419 // Planar configuration = 0 : Pixels are already RGB
420 // Planar configuration = 1 : 3 planes : R, G, B
421 // Planar configuration = 2 : 1 gray Plane + 3 LUT
423 // Well ... supposed to be !
424 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
425 // PhotometricInterpretation=PALETTE COLOR
426 // and heuristic has to be found :-(
428 int planConf=Header->GetPlanarConfiguration(); // 0028,0006
430 // Whatever Planar Configuration is,
431 // "PALETTE COLOR " implies that we deal with the palette.
432 if (str_PhotometricInterpretation == "PALETTE COLOR ")
437 // Pixels are already RGB
443 if (str_PhotometricInterpretation == "YBR_FULL") {
445 // Warning : YBR_FULL_422 acts as RGB
446 // : we need to make RGB Pixels from Planes Y,cB,cR
447 // see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
448 // for code optimisation
450 // to see the tricks about YBR_FULL, YBR_FULL_422,
451 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
452 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
453 // and be *very* affraid
455 int l = Header->GetXSize()*Header->GetYSize();
456 int nbFrames = Header->GetZSize();
458 unsigned char * newDest = (unsigned char*) malloc(lgrTotale);
459 unsigned char *x = newDest;
460 unsigned char * a = (unsigned char *)destination;
461 unsigned char * b = a + l;
462 unsigned char * c = b + l;
466 // TODO : Replace by the 'well known'
467 // integer computation counterpart
468 for (int i=0;i<nbFrames;i++) {
469 for (int j=0;j<l; j++) {
470 R= 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
471 G= 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
472 B= 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
477 if (R>255.0) R=255.0;
478 if (G>255.0) G=255.0;
479 if (B>255.0) B=255.0;
481 *(x++) = (unsigned char)R;
482 *(x++) = (unsigned char)G;
483 *(x++) = (unsigned char)B;
487 memmove(destination,newDest,lgrTotale);
492 // need to make RGB Pixels from R,G,B Planes
493 // (all the Frames at a time)
495 int l = Header->GetXSize()*Header->GetYSize()*Header->GetZSize();
497 char * newDest = (char*) malloc(lgrTotale);
499 char * a = (char *)destination;
503 for (int j=0;j<l; j++) {
509 memmove(destination,newDest,lgrTotale);
517 // Palettes were found
518 // Let the user deal with them !
521 // now, it's an RGB image
522 // Lets's write it in the Header
524 // CreateOrReplaceIfExist ?
526 std::string spp = "3"; // Samples Per Pixel
527 Header->SetPubEntryByNumber(spp,0x0028,0x0002);
528 std::string rgb="RGB "; // Photometric Interpretation
529 Header->SetPubEntryByNumber(rgb,0x0028,0x0004);
531 std::string planConfig = "0"; // Planar Configuration
532 Header->SetPubEntryByNumber(planConfig,0x0028,0x0006);
534 // TODO : Drop Palette Color out of the Header?
541 * \warning doit-etre etre publique ?
542 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
545 * @param ExpectedSize
547 * @return integer acts as a boolean
549 bool gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
550 Header->SetImageDataSize(ExpectedSize);
552 lgrTotale = ExpectedSize;
558 * \brief Ecrit sur disque les pixels d'UNE image
559 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
560 * Ca sera à l'utilisateur d'appeler son Reader correctement
561 * (Equivalent a IdImaWriteRawFile)
567 bool gdcmFile::WriteRawData (std::string fileName) {
569 fp1 = fopen(fileName.c_str(),"wb");
571 printf("Echec ouverture (ecriture) Fichier [%s] \n",fileName.c_str());
574 fwrite (PixelData,lgrTotale, 1, fp1);
581 * \brief Ecrit sur disque UNE image Dicom
582 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
583 * Ca fonctionnera correctement (?) sur processeur Intel
584 * (Equivalent a IdDcmWrite)
587 * @return int acts as a boolean
590 bool gdcmFile::WriteDcmImplVR (std::string fileName) {
591 return WriteBase(fileName, ImplicitVR);
598 * @return int acts as a boolean
601 bool gdcmFile::WriteDcmImplVR (const char* fileName) {
602 return WriteDcmImplVR (std::string (fileName));
609 * @return int acts as a boolean
612 bool gdcmFile::WriteDcmExplVR (std::string fileName) {
613 return WriteBase(fileName, ExplicitVR);
618 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
619 * (a l'attention des logiciels cliniques
620 * qui ne prennent en entrée QUE des images ACR ...
621 * \warning si un header DICOM est fourni en entree,
622 * les groupes < 0x0008 et les groupes impairs sont ignores)
623 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
624 * Ca fonctionnera correctement (?) sur processeur Intel
625 * (Equivalent a IdDcmWrite)
628 * @return int acts as a boolean
631 bool gdcmFile::WriteAcr (std::string fileName) {
632 return WriteBase(fileName, ACR);
635 //-----------------------------------------------------------------------------
643 * @return int acts as a boolean
645 bool gdcmFile::WriteBase (std::string FileName, FileType type) {
648 fp1 = fopen(FileName.c_str(),"wb");
650 printf("Echec ouverture (ecriture) Fichier [%s] \n",FileName.c_str());
654 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
656 // writing Dicom File Preamble
657 filePreamble=(char*)calloc(128,1);
658 fwrite(filePreamble,128,1,fp1);
659 fwrite("DICM",4,1,fp1);
662 // --------------------------------------------------------------
663 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
665 // if recognition code tells us we dealt with a LibIDO image
666 // we reproduce on disk the switch between lineNumber and columnNumber
667 // just before writting ...
669 std::string rows, columns;
670 if ( Header->GetFileType() == ACR_LIBIDO){
671 rows = Header->GetPubEntryByNumber(0x0028, 0x0010);
672 columns = Header->GetPubEntryByNumber(0x0028, 0x0011);
673 Header->SetPubEntryByNumber(columns, 0x0028, 0x0010);
674 Header->SetPubEntryByNumber(rows , 0x0028, 0x0011);
676 // ----------------- End of Special Patch ----------------
678 Header->Write(fp1, type);
680 // --------------------------------------------------------------
681 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
683 // ...and we restore the Header to be Dicom Compliant again
684 // just after writting
686 if (Header->GetFileType() == ACR_LIBIDO){
687 Header->SetPubEntryByNumber(rows , 0x0028, 0x0010);
688 Header->SetPubEntryByNumber(columns, 0x0028, 0x0011);
690 // ----------------- End of Special Patch ----------------
692 fwrite(PixelData, lgrTotale, 1, fp1);
697 //-----------------------------------------------------------------------------
701 * \brief Swap the bytes, according to swap code.
702 * \warning not end user intended
703 * @param im area to deal with
704 * @param swap swap code
705 * @param lgr Area Length
706 * @param nb Pixels Bit number
708 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
726 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
727 | ((((unsigned short int*)im)[i])<<8);
731 printf("valeur de SWAP (16 bits) not allowed : %d\n", swap);
742 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
743 fort =((unsigned long int*)im)[i]>>16;
744 fort= (fort>>8) | (fort<<8);
745 faible=(faible>>8) | (faible<<8);
747 ((unsigned long int*)im)[i]=(s32<<16)|fort;
753 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
754 fort=((unsigned long int*)im)[i]>>16;
755 fort= (fort>>8) | (fort<<8);
756 faible=(faible>>8) | (faible<<8);
758 ((unsigned long int*)im)[i]=(s32<<16)|faible;
764 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
765 fort=((unsigned long int*)im)[i]>>16;
767 ((unsigned long int*)im)[i]=(s32<<16)|fort;
772 printf(" SWAP value (32 bits) not allowed : %d\n", swap);
779 * \brief Read pixel data from disk (optionaly decompressing) into the
780 * caller specified memory location.
781 * @param destination where the pixel data should be stored.
784 bool gdcmFile::ReadPixelData(void* destination) {
788 if ( !(fp=Header->OpenFile()))
791 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 ) {
797 // ---------------------- Compacted File (12 Bits Per Pixel)
798 /* unpack 12 Bits pixels into 16 Bits pixels */
799 /* 2 pixels 12bit = [0xABCDEF] */
800 /* 2 pixels 16bit = [0x0ABD] + [0x0FCE] */
801 if (Header->GetBitsAllocated()==12) {
802 int nbPixels = Header->GetXSize() * Header->GetYSize();
803 unsigned char b0, b1, b2;
805 unsigned short int* pdestination = (unsigned short int*)destination;
806 for(int p=0;p<nbPixels;p+=2) {
810 //Two steps is necessary to please VC++
811 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
812 /* A */ /* B */ /* D */
813 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
814 /* F */ /* C */ /* E */
816 // Troubles expected on Big-Endian processors ?
821 // ---------------------- Uncompressed File
822 if ( !Header->IsDicomV3() ||
823 Header->IsImplicitVRLittleEndianTransferSyntax() ||
824 Header->IsExplicitVRLittleEndianTransferSyntax() ||
825 Header->IsExplicitVRBigEndianTransferSyntax() ||
826 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
828 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
830 if ( ItemRead != 1 ) {
839 // ---------------------- Run Length Encoding
840 if (Header->IsRLELossLessTransferSyntax()) {
841 bool res = (bool)gdcm_read_RLE_file (fp,destination);
845 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
847 std::string str_nb=Header->GetPubEntryByNumber(0x0028,0x0100);
848 if (str_nb == GDCM_UNFOUND ) {
851 nb = atoi(str_nb.c_str() );
852 if (nb == 12) nb =16; // ?? 12 should be ACR-NEMA only ?
857 int taille = Header->GetXSize() * Header->GetYSize()
858 * Header->GetSamplesPerPixel();
859 long fragmentBegining; // for ftell, fseek
861 bool jpg2000 = Header->IsJPEG2000();
862 bool jpgLossless = Header->IsJPEGLossless();
865 guint16 ItemTagGr,ItemTagEl;
868 // Position on begining of Jpeg Pixels
870 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
871 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
872 if(Header->GetSwapCode()) {
873 ItemTagGr=Header->SwapShort(ItemTagGr);
874 ItemTagEl=Header->SwapShort(ItemTagEl);
877 if(Header->GetSwapCode())
878 ln=Header->SwapLong(ln); // Basic Offset Table Item length
881 // What is it used for ?!?
882 char *BasicOffsetTableItemValue = (char *)malloc(ln+1);
883 fread(BasicOffsetTableItemValue,ln,1,fp);
886 // first Fragment initialisation
887 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
888 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
889 if(Header->GetSwapCode()) {
890 ItemTagGr=Header->SwapShort(ItemTagGr);
891 ItemTagEl=Header->SwapShort(ItemTagEl);
894 // parsing fragments until Sequence Delim. Tag found
895 while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
896 // --- for each Fragment
899 if(Header->GetSwapCode())
900 ln=Header->SwapLong(ln); // Fragment Item length
902 fragmentBegining=ftell(fp);
904 if (jpg2000) { // JPEG 2000 : call to ???
906 res = (bool)gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
908 } // ------------------------------------- endif (JPEG2000)
910 else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
912 JPEGLosslessDecodeImage (fp, // Reading Fragment pixels
913 (unsigned short *)destination,
914 Header->GetPixelSize()*8* Header->GetSamplesPerPixel(),
916 res=1; // in order not to break the loop
918 } // ------------------------------------- endif (JPEGLossless)
920 else { // JPEG Lossy : call to IJG 6b
922 if (Header->GetBitsStored() == 8) {
923 res = (bool)gdcm_read_JPEG_file (fp,destination); // Reading Fragment pixels
925 res = (bool)gdcm_read_JPEG_file12 (fp,destination);// Reading Fragment pixels
927 } // ------------------------------------- endif (JPEGLossy)
931 destination = (char *)destination + taille * nBytes; // location in user's memory
932 // for next fragment (if any)
934 fseek(fp,fragmentBegining,SEEK_SET); // To be sure we start
935 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
937 ItemTagGr = ItemTagEl =0;
938 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
939 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
940 if(Header->GetSwapCode()) {
941 ItemTagGr=Header->SwapShort(ItemTagGr);
942 ItemTagEl=Header->SwapShort(ItemTagEl);
945 } // endWhile parsing fragments until Sequence Delim. Tag found
950 //-----------------------------------------------------------------------------