2 //-----------------------------------------------------------------------------
5 #include "jpeg/ljpg/jpegless.h"
7 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT;
9 //-----------------------------------------------------------------------------
10 // Constructor / Destructor
13 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
14 * file (see SetFileName, SetDcmTag and Write)
15 * Opens (in read only and when possible) an existing file and checks
16 * for DICOM compliance. Returns NULL on failure.
17 * \Note the in-memory representation of all available tags found in
18 * the DICOM header is post-poned to first header information access.
19 * This avoid a double parsing of public part of the header when
20 * one sets an a posteriori shadow dictionary (efficiency can be
21 * seen as a side effect).
23 * @param header file to be opened for reading datas
28 gdcmFile::gdcmFile(gdcmHeader *header) {
31 PixelRead=-1; // no ImageData read yet.
33 if (Header->IsReadable())
34 SetPixelDataSizeFromHeader();
39 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
40 * file (see SetFileName, SetDcmTag and Write)
41 * Opens (in read only and when possible) an existing file and checks
42 * for DICOM compliance. Returns NULL on failure.
43 * \Note the in-memory representation of all available tags found in
44 * the DICOM header is post-poned to first header information access.
45 * This avoid a double parsing of public part of the header when
46 * one sets an a posteriori shadow dictionary (efficiency can be
47 * seen as a side effect).
48 * @param filename file to be opened for parsing
51 gdcmFile::gdcmFile(std::string & filename) {
52 Header=new gdcmHeader(filename.c_str());
54 PixelRead=-1; // no ImageData read yet.
56 if (Header->IsReadable())
57 SetPixelDataSizeFromHeader();
60 gdcmFile::gdcmFile(const char * filename) {
61 Header=new gdcmHeader(filename);
63 PixelRead=-1; // no ImageData read yet.
65 if (Header->IsReadable())
66 SetPixelDataSizeFromHeader();
71 * \brief canonical destructor
72 * \Note If the gdcmHeader is created by the gdcmFile, it is destroyed
75 gdcmFile::~gdcmFile(void) {
81 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
91 gdcmHeader *gdcmFile::GetHeader(void) {
97 * \brief computes the length (in bytes) to ALLOCATE to receive the
98 * image(s) pixels (multiframes taken into account)
99 * \warning : it is NOT the group 7FE0 length
100 * (no interest for compressed images).
101 * @return length to allocate
103 void gdcmFile::SetPixelDataSizeFromHeader(void) {
104 // see PS 3.3-2003 : C.7.6.3.2.1
114 // YBR_FULL_422 (no LUT, no Palette)
120 // ex : gdcm-US-ALOKA-16.dcm
121 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
122 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
123 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
125 // ex : OT-PAL-8-face.dcm
126 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
127 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
128 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
132 str_nb=Header->GetEntryByNumber(0x0028,0x0100);
133 if (str_nb == GDCM_UNFOUND ) {
136 nb = atoi(str_nb.c_str() );
137 if (nb == 12) nb =16;
139 lgrTotale = lgrTotaleRaw = Header->GetXSize() * Header->GetYSize()
140 * Header->GetZSize() * (nb/8)* Header->GetSamplesPerPixel();
141 std::string str_PhotometricInterpretation =
142 Header->GetEntryByNumber(0x0028,0x0004);
144 /*if ( str_PhotometricInterpretation == "PALETTE COLOR " )*/
145 // pb when undealt Segmented Palette Color
147 if (Header->HasLUT()) {
154 * \brief Returns the size (in bytes) of required memory to hold
155 * the pixel data represented in this file.
156 * @return The size of pixel data in bytes.
158 size_t gdcmFile::GetImageDataSize(void) {
164 * \brief Returns the size (in bytes) of required memory to hold
165 * \ the pixel data represented in this file, when user DOESN'T want
166 * \ to get RGB pixels image when it's stored as a PALETTE COLOR image
167 * \ - the (vtk) user is supposed to know how deal with LUTs -
168 * \warning to be used with GetImagePixelsRaw()
169 * @return The size of pixel data in bytes.
171 size_t gdcmFile::GetImageDataSizeRaw(void) {
172 return (lgrTotaleRaw);
177 * \brief Allocates necessary memory, copies the pixel data
178 * (image[s]/volume[s]) to newly allocated zone.
179 * Transforms YBR pixels into RGB pixels if any
180 * Transforms 3 planes R, G, B into a single RGB Plane
181 * Transforms single Grey plane + 3 Palettes into a RGB Plane
182 * @return Pointer to newly allocated pixel data.
183 * \ NULL if alloc fails
185 void * gdcmFile::GetImageData (void) {
186 PixelData = (void *) malloc(lgrTotale);
188 GetImageDataIntoVector(PixelData, lgrTotale);
189 PixelRead=0; // no PixelRaw
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);
216 PixelRead=0 ; // no PixelRaw
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->SetEntryByNumber(spp,0x0028,0x0002);
243 std::string rgb= "RGB "; // Photometric Interpretation
244 Header->SetEntryByNumber(rgb,0x0028,0x0004);
245 std::string planConfig = "0"; // Planar Configuration
246 Header->SetEntryByNumber(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 // Oops! I get one (gdcm-US-ALOKA-16.dcm)
255 // No idea how to manage such an image
256 // It seems that *no Dicom Viewer* has any idea :-(
257 // Segmented xxx Palette Color are *more* than 65535 long ?!?
259 std::string rgb= "MONOCHROME1 "; // Photometric Interpretation
260 Header->SetEntryByNumber(rgb,0x0028,0x0004);
262 // TODO : Drop Palette Color out of the Header?
268 * \brief Allocates necessary memory, copies the pixel data
269 * (image[s]/volume[s]) to newly allocated zone.
270 * Transforms YBR pixels into RGB pixels if any
271 * Transforms 3 planes R, G, B into a single RGB Plane
272 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
273 * @return Pointer to newly allocated pixel data.
274 * \ NULL if alloc fails
276 void * gdcmFile::GetImageDataRaw (void) {
277 if (Header->HasLUT())
278 lgrTotale /= 3; // TODO Let gdcmHeadar user a chance
279 // to get the right value
280 // Create a member lgrTotaleRaw ???
281 PixelData = (void *) malloc(lgrTotale);
283 GetImageDataIntoVectorRaw(PixelData, lgrTotale);
284 PixelRead=1; // PixelRaw
290 * \brief Copies at most MaxSize bytes of pixel data to caller's
292 * \warning This function was designed to avoid people that want to build
293 * a volume from an image stack to need first to get the image pixels
294 * and then move them to the volume area.
295 * It's absolutely useless for any VTK user since vtk chooses
296 * to invert the lines of an image, that is the last line comes first
297 * (for some axis related reasons?). Hence he will have
298 * to load the image line by line, starting from the end.
299 * VTK users hace to call GetImageData
300 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
301 * into a single RGB Pixels Plane
302 * the (VTK) user will manage the palettes
304 * @param destination Address (in caller's memory space) at which the
305 * pixel data should be copied
306 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
307 * is not sufficient to hold the pixel data the copy is not
308 * executed (i.e. no partial copy).
309 * @return On success, the number of bytes actually copied. Zero on
310 * failure e.g. MaxSize is lower than necessary.
312 size_t gdcmFile::GetImageDataIntoVectorRaw (void* destination, size_t MaxSize) {
314 int nb, nbu, highBit, signe;
315 std::string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
316 PixelRead=1 ; // PixelRaw
318 if ( lgrTotale > MaxSize ) {
319 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
320 "than caller's expected MaxSize");
324 (void)ReadPixelData(destination);
326 // Number of Bits Allocated for storing a Pixel
327 str_nb = Header->GetEntryByNumber(0x0028,0x0100);
328 if (str_nb == GDCM_UNFOUND ) {
331 nb = atoi(str_nb.c_str() );
333 // Number of Bits actually used
334 str_nbu=Header->GetEntryByNumber(0x0028,0x0101);
335 if (str_nbu == GDCM_UNFOUND ) {
338 nbu = atoi(str_nbu.c_str() );
341 str_highBit=Header->GetEntryByNumber(0x0028,0x0102);
342 if (str_highBit == GDCM_UNFOUND ) {
345 highBit = atoi(str_highBit.c_str() );
350 str_signe=Header->GetEntryByNumber(0x0028,0x0103);
351 if (str_signe == GDCM_UNFOUND ) {
352 signe = 0; // default is unsigned
354 signe = atoi(str_signe.c_str() );
357 // re arange bytes inside the integer (processor endianity)
359 SwapZone(destination, Header->GetSwapCode(), lgrTotale, nb);
361 // to avoid pb with some xmedcon breakers images
362 if (nb==16 && nbu<nb && signe==0) {
363 int l = (int)lgrTotale / (nb/8);
364 guint16 *deb = (guint16 *)destination;
365 for(int i = 0; i<l; i++) {
372 // re arange bits inside the bytes
374 int l = (int)lgrTotale / (nb/8);
376 guint16 mask = 0xffff;
377 mask = mask >> (nb-nbu);
378 guint16 *deb = (guint16 *)destination;
379 for(int i = 0; i<l; i++) {
380 *deb = (*deb >> (nbu-highBit-1)) & mask;
383 } else if (nb == 32 ) {
384 guint32 mask = 0xffffffff;
385 mask = mask >> (nb-nbu);
386 guint32 *deb = (guint32 *)destination;
387 for(int i = 0; i<l; i++) {
388 *deb = (*deb >> (nbu-highBit-1)) & mask;
392 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
396 // DO NOT remove this code commented out.
397 // Nobody knows what's expecting you ...
398 // Just to 'see' what was actually read on disk :-(
401 // f2 = fopen("SpuriousFile.RAW","wb");
402 // fwrite(destination,lgrTotale,1,f2);
405 // Deal with the color
406 // -------------------
408 std::string str_PhotometricInterpretation =
409 Header->GetEntryByNumber(0x0028,0x0004);
411 if ( (str_PhotometricInterpretation == "MONOCHROME1 ")
412 || (str_PhotometricInterpretation == "MONOCHROME2 ") ) {
416 // Planar configuration = 0 : Pixels are already RGB
417 // Planar configuration = 1 : 3 planes : R, G, B
418 // Planar configuration = 2 : 1 gray Plane + 3 LUT
420 // Well ... supposed to be !
421 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
422 // PhotometricInterpretation=PALETTE COLOR
423 // and heuristic has to be found :-(
425 int planConf=Header->GetPlanarConfiguration(); // 0028,0006
427 // Whatever Planar Configuration is,
428 // "PALETTE COLOR " implies that we deal with the palette.
429 if (str_PhotometricInterpretation == "PALETTE COLOR ")
434 // Pixels are already RGB
440 if (str_PhotometricInterpretation == "YBR_FULL") {
442 // Warning : YBR_FULL_422 acts as RGB
443 // : we need to make RGB Pixels from Planes Y,cB,cR
445 // to see the tricks about YBR_FULL, YBR_FULL_422,
446 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
447 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
448 // and be *very* affraid
450 int l = Header->GetXSize()*Header->GetYSize();
451 int nbFrames = Header->GetZSize();
453 unsigned char * newDest = (unsigned char*) malloc(lgrTotale);
454 unsigned char *x = newDest;
455 unsigned char * a = (unsigned char *)destination;
456 unsigned char * b = a + l;
457 unsigned char * c = b + l;
460 // TODO : Replace by the 'well known'
461 // integer computation counterpart
462 // see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
463 // for code optimisation
465 for (int i=0;i<nbFrames;i++) {
466 for (int j=0;j<l; j++) {
467 R= 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
468 G= 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
469 B= 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
474 if (R>255.0) R=255.0;
475 if (G>255.0) G=255.0;
476 if (B>255.0) B=255.0;
478 *(x++) = (unsigned char)R;
479 *(x++) = (unsigned char)G;
480 *(x++) = (unsigned char)B;
484 memmove(destination,newDest,lgrTotale);
489 // need to make RGB Pixels from R,G,B Planes
490 // (all the Frames at a time)
492 int l = Header->GetXSize()*Header->GetYSize()*Header->GetZSize();
494 char * newDest = (char*) malloc(lgrTotale);
496 char * a = (char *)destination;
500 for (int j=0;j<l; j++) {
505 memmove(destination,newDest,lgrTotale);
511 // Palettes were found
512 // Let the user deal with them !
515 // now, it's an RGB image
516 // Lets's write it in the Header
518 // CreateOrReplaceIfExist ?
520 std::string spp = "3"; // Samples Per Pixel
521 Header->SetEntryByNumber(spp,0x0028,0x0002);
522 std::string rgb="RGB "; // Photometric Interpretation
523 Header->SetEntryByNumber(rgb,0x0028,0x0004);
525 std::string planConfig = "0"; // Planar Configuration
526 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
528 // TODO : Drop Palette Color out of the Header?
535 * \warning doit-etre etre publique ?
536 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
539 * @param ExpectedSize
543 bool gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
544 Header->SetImageDataSize(ExpectedSize);
546 lgrTotale = ExpectedSize;
552 * \brief Writes on disk A SINGLE Dicom file
553 * NO test is performed on processor "Endiannity".
554 * Ca sera à l'utilisateur d'appeler son Reader correctement
555 * (Equivalent a IdImaWriteRawFile)
557 * @param fileName name of the file to be created
558 * (any already existing file is over written)
559 * @return false if write fails
562 bool gdcmFile::WriteRawData (std::string fileName) {
564 fp1 = fopen(fileName.c_str(),"wb");
566 printf("Fail to open (write) file [%s] \n",fileName.c_str());
569 fwrite (PixelData,lgrTotale, 1, fp1);
576 * \brief Writes on disk A SINGLE Dicom file
577 * NO test is performed on processor "Endiannity".
579 * @param fileName name of the file to be created
580 * (any already existing file is overwritten)
581 * @return false if write fails
584 bool gdcmFile::WriteDcmImplVR (std::string fileName) {
585 return WriteBase(fileName, ImplicitVR);
591 * @param fileName name of the file to be created
592 * (any already existing file is overwritten)
593 * @return false if write fails
596 bool gdcmFile::WriteDcmImplVR (const char* fileName) {
597 return WriteDcmImplVR (std::string (fileName));
603 * @param fileName name of the file to be created
604 * (any already existing file is overwritten)
605 * @return false if write fails
608 bool gdcmFile::WriteDcmExplVR (std::string fileName) {
609 return WriteBase(fileName, ExplicitVR);
614 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
615 * (a l'attention des logiciels cliniques
616 * qui ne prennent en entrée QUE des images ACR ...
617 * \warning if a DICOM_V3 header is supplied,
618 * groups < 0x0008 and shadow groups are ignored)
619 * \warning NO TEST is performed on processor "Endiannity".
620 * Ca fonctionnera correctement (?) sur processeur Intel
621 * (Equivalent a IdDcmWrite)
623 * @param fileName name of the file to be created
624 * (any already existing file is overwritten)
625 * @return false if write fails
628 bool gdcmFile::WriteAcr (std::string fileName) {
629 return WriteBase(fileName, ACR);
632 //-----------------------------------------------------------------------------
637 * @param fileName name of the file to be created
638 * (any already existing file is overwritten)
639 * @param type file type (ExplicitVR, ImplicitVR, ...)
640 * @return false if write fails
642 bool gdcmFile::WriteBase (std::string fileName, FileType type) {
646 if (PixelRead==-1 && type != DICOMDIR) {
647 std::cout << "U never Read the pixels; U cannot write the file"
652 fp1 = fopen(fileName.c_str(),"wb");
654 printf("Failed to open (write) File [%s] \n",fileName.c_str());
658 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
660 // writing Dicom File Preamble
661 filePreamble=(char*)calloc(128,1);
662 fwrite(filePreamble,128,1,fp1);
663 fwrite("DICM",4,1,fp1);
667 // --------------------------------------------------------------
668 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
670 // if recognition code tells us we dealt with a LibIDO image
671 // we reproduce on disk the switch between lineNumber and columnNumber
672 // just before writting ...
674 // TODO : the best trick would be *change* the recognition code
675 // but pb expected if user deals with, e.g. COMPLEX images
677 std::string rows, columns;
678 if ( Header->GetFileType() == ACR_LIBIDO){
679 rows = Header->GetEntryByNumber(0x0028, 0x0010);
680 columns = Header->GetEntryByNumber(0x0028, 0x0011);
681 Header->SetEntryByNumber(columns, 0x0028, 0x0010);
682 Header->SetEntryByNumber(rows , 0x0028, 0x0011);
684 // ----------------- End of Special Patch ----------------
686 // TODO : get the grPixel, numPixel values (for some ACR-NEMA images only)
687 guint16 grPixel =0x7fe0;
688 guint16 numPixel=0x0010;
690 // Update Pixel Data Length
691 // the *last* of the 7fe0,0010, if many.
693 TagKey key = gdcmDictEntry::TranslateToKey(grPixel, numPixel);
694 TagHeaderEntryHT::iterator p2;
695 gdcmHeaderEntry * PixelElement;
697 IterHT it= Header->GetEntry().equal_range(key); // get a pair of iterators first-last synonym
699 if (Header->GetEntry().count(key) == 1) // only the first is significant
700 p2=it.first; // iterator on the first (unique) synonym
702 p2=it.second;// iterator on the last synonym
704 PixelElement=p2->second; // H Table target column (2-nd col)
705 PixelElement->SetPrintLevel(2);
706 PixelElement->Print();
709 PixelElement->SetLength(lgrTotaleRaw);
710 else if (PixelRead==0)
711 PixelElement->SetLength(lgrTotale);
713 PixelElement->SetPrintLevel(2);
714 PixelElement->Print();
716 Header->Write(fp1, type);
718 // --------------------------------------------------------------
719 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
721 // ...and we restore the Header to be Dicom Compliant again
722 // just after writting
724 if (Header->GetFileType() == ACR_LIBIDO){
725 Header->SetEntryByNumber(rows , 0x0028, 0x0010);
726 Header->SetEntryByNumber(columns, 0x0028, 0x0011);
728 // ----------------- End of Special Patch ----------------
730 fwrite(PixelData, lgrTotale, 1, fp1);
735 //-----------------------------------------------------------------------------
739 * \brief Swap the bytes, according to swap code.
740 * \warning not end user intended
741 * @param im area to deal with
742 * @param swap swap code
743 * @param lgr Area Length
744 * @param nb Pixels Bit number
746 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
764 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
765 | ((((unsigned short int*)im)[i])<<8);
769 printf("SWAP value (16 bits) not allowed : %d\n", swap);
780 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
781 fort =((unsigned long int*)im)[i]>>16;
782 fort= (fort>>8) | (fort<<8);
783 faible=(faible>>8) | (faible<<8);
785 ((unsigned long int*)im)[i]=(s32<<16)|fort;
791 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
792 fort=((unsigned long int*)im)[i]>>16;
793 fort= (fort>>8) | (fort<<8);
794 faible=(faible>>8) | (faible<<8);
796 ((unsigned long int*)im)[i]=(s32<<16)|faible;
802 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
803 fort=((unsigned long int*)im)[i]>>16;
805 ((unsigned long int*)im)[i]=(s32<<16)|fort;
810 printf("SWAP value (32 bits) not allowed : %d\n", swap);
817 * \brief Read pixel data from disk (optionaly decompressing) into the
818 * caller specified memory location.
819 * @param destination where the pixel data should be stored.
822 bool gdcmFile::ReadPixelData(void* destination) {
826 if ( !(fp=Header->OpenFile()))
828 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 ) {
832 // ---------------------- Compacted File (12 Bits Per Pixel)
833 /* unpack 12 Bits pixels into 16 Bits pixels */
834 /* 2 pixels 12bit = [0xABCDEF] */
835 /* 2 pixels 16bit = [0x0ABD] + [0x0FCE] */
837 if (Header->GetBitsAllocated()==12) {
838 int nbPixels = Header->GetXSize() * Header->GetYSize();
839 unsigned char b0, b1, b2;
841 unsigned short int* pdestination = (unsigned short int*)destination;
842 for(int p=0;p<nbPixels;p+=2) {
846 //Two steps is necessary to please VC++
847 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
848 /* A */ /* B */ /* D */
849 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
850 /* F */ /* C */ /* E */
852 // Troubles expected on Big-Endian processors ?
859 // ---------------------- Uncompressed File
860 if ( !Header->IsDicomV3() ||
861 Header->IsImplicitVRLittleEndianTransferSyntax() ||
862 Header->IsExplicitVRLittleEndianTransferSyntax() ||
863 Header->IsExplicitVRBigEndianTransferSyntax() ||
864 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
866 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
867 if ( ItemRead != 1 ) {
876 // ---------------------- Run Length Encoding
877 if (Header->IsRLELossLessTransferSyntax()) {
878 bool res = (bool)gdcm_read_RLE_file (fp,destination);
883 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
885 std::string str_nb=Header->GetEntryByNumber(0x0028,0x0100);
886 if (str_nb == GDCM_UNFOUND ) {
889 nb = atoi(str_nb.c_str() );
890 if (nb == 12) nb =16; // ?? 12 should be ACR-NEMA only ?
895 int taille = Header->GetXSize() * Header->GetYSize()
896 * Header->GetSamplesPerPixel();
897 long fragmentBegining; // for ftell, fseek
899 bool jpg2000 = Header->IsJPEG2000();
900 bool jpgLossless = Header->IsJPEGLossless();
903 guint16 ItemTagGr,ItemTagEl;
906 // Position on begining of Jpeg Pixels
908 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
909 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
910 if(Header->GetSwapCode()) {
911 ItemTagGr=Header->SwapShort(ItemTagGr);
912 ItemTagEl=Header->SwapShort(ItemTagEl);
915 if(Header->GetSwapCode())
916 ln=Header->SwapLong(ln); // Basic Offset Table Item length
919 // What is it used for ?!?
920 char *BasicOffsetTableItemValue = (char *)malloc(ln+1);
921 fread(BasicOffsetTableItemValue,ln,1,fp);
924 // first Fragment initialisation
925 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
926 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
927 if(Header->GetSwapCode()) {
928 ItemTagGr=Header->SwapShort(ItemTagGr);
929 ItemTagEl=Header->SwapShort(ItemTagEl);
932 // parsing fragments until Sequence Delim. Tag found
933 while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
934 // --- for each Fragment
937 if(Header->GetSwapCode())
938 ln=Header->SwapLong(ln); // Fragment Item length
940 fragmentBegining=ftell(fp);
942 if (jpg2000) { // JPEG 2000 : call to ???
944 res = (bool)gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
946 } // ------------------------------------- endif (JPEG2000)
948 else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
950 JPEGLosslessDecodeImage (fp, // Reading Fragment pixels
951 (unsigned short *)destination,
952 Header->GetPixelSize()*8* Header->GetSamplesPerPixel(),
954 res=1; // in order not to break the loop
956 } // ------------------------------------- endif (JPEGLossless)
958 else { // JPEG Lossy : call to IJG 6b
960 if (Header->GetBitsStored() == 8) {
961 res = (bool)gdcm_read_JPEG_file (fp,destination); // Reading Fragment pixels
963 res = (bool)gdcm_read_JPEG_file12 (fp,destination);// Reading Fragment pixels
965 } // ------------------------------------- endif (JPEGLossy)
969 destination = (char *)destination + taille * nBytes; // location in user's memory
970 // for next fragment (if any)
972 fseek(fp,fragmentBegining,SEEK_SET); // To be sure we start
973 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
975 ItemTagGr = ItemTagEl =0;
976 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
977 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
978 if(Header->GetSwapCode()) {
979 ItemTagGr=Header->SwapShort(ItemTagGr);
980 ItemTagEl=Header->SwapShort(ItemTagEl);
983 } // endWhile parsing fragments until Sequence Delim. Tag found
988 //-----------------------------------------------------------------------------