1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2004/09/01 16:23:59 $
7 Version: $Revision: 1.122 $
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.htm for details.
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.
17 =========================================================================*/
20 #include "gdcmDebug.h"
21 #include "jpeg/ljpg/jpegless.h"
23 typedef std::pair<TagDocEntryHT::iterator,TagDocEntryHT::iterator> IterHT;
25 //-------------------------------------------------------------------------
26 // Constructor / Destructor
29 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
30 * file (gdcmHeader only deals with the ... header)
31 * Opens (in read only and when possible) an existing file and checks
32 * for DICOM compliance. Returns NULL on failure.
33 * It will be up to the user to load the pixels into memory
34 * (see GetImageData, GetImageDataRaw)
35 * \note the in-memory representation of all available tags found in
36 * the DICOM header is post-poned to first header information access.
37 * This avoid a double parsing of public part of the header when
38 * user sets an a posteriori shadow dictionary (efficiency can be
39 * seen as a side effect).
40 * @param header already built gdcmHeader
43 gdcmFile::gdcmFile(gdcmHeader *header)
48 PixelRead = -1; // no ImageData read yet.
49 LastAllocatedPixelDataLength = 0;
54 InitialPlanConfig = "";
55 InitialBitsAllocated = "";
57 if (Header->IsReadable())
59 SetPixelDataSizeFromHeader();
65 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
66 * file (gdcmHeader only deals with the ... header)
67 * Opens (in read only and when possible) an existing file and checks
68 * for DICOM compliance. Returns NULL on failure.
69 * It will be up to the user to load the pixels into memory
70 * (see GetImageData, GetImageDataRaw)
71 * \note the in-memory representation of all available tags found in
72 * the DICOM header is post-poned to first header information access.
73 * This avoid a double parsing of public part of the header when
74 * one sets an a posteriori shadow dictionary (efficiency can be
75 * seen as a side effect).
76 * @param filename file to be opened for parsing
78 gdcmFile::gdcmFile(std::string const & filename )
80 Header = new gdcmHeader( filename );
83 PixelRead = -1; // no ImageData read yet.
84 LastAllocatedPixelDataLength = 0;
89 InitialPlanConfig = "";
90 InitialBitsAllocated = "";
92 if ( Header->IsReadable() )
94 SetPixelDataSizeFromHeader();
100 * \brief canonical destructor
101 * \note If the gdcmHeader was created by the gdcmFile constructor,
102 * it is destroyed by the gdcmFile
104 gdcmFile::~gdcmFile()
113 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
121 * \brief computes the length (in bytes) we must ALLOCATE to receive the
122 * image(s) pixels (multiframes taken into account)
123 * \warning : it is NOT the group 7FE0 length
124 * (no interest for compressed images).
126 void gdcmFile::SetPixelDataSizeFromHeader()
128 // see PS 3.3-2003 : C.7.6.3.2.1
138 // YBR_FULL_422 (no LUT, no Palette)
144 // ex : gdcm-US-ALOKA-16.dcm
145 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
146 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
147 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
149 // ex : OT-PAL-8-face.dcm
150 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
151 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
152 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
154 // Number of "Bits Allocated"
156 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
158 if ( str_nb == GDCM_UNFOUND )
164 nb = atoi( str_nb.c_str() );
170 ImageDataSize = ImageDataSizeRaw = Header->GetXSize() * Header->GetYSize()
171 * Header->GetZSize() * (nb/8) * Header->GetSamplesPerPixel();
172 std::string str_PhotometricInterpretation =
173 Header->GetEntryByNumber(0x0028,0x0004);
175 // if ( str_PhotometricInterpretation == "PALETTE COLOR " ),
176 // pb when undealt Segmented Palette Color
178 if ( Header->HasLUT() )
187 * \brief Returns the size (in bytes) of required memory to hold
188 * the pixel data represented in this file, if user DOESN'T want
189 * to get RGB pixels image when it's stored as a PALETTE COLOR image
190 * -the (vtk) user is supposed to know how to deal with LUTs-
191 * \warning to be used with GetImagePixelsRaw()
192 * @return The size of pixel data in bytes.
194 size_t gdcmFile::GetImageDataSizeRaw()
196 return ImageDataSizeRaw;
201 * \brief - Allocates necessary memory,
202 * - Reads the pixels from disk (uncompress if necessary),
203 * - Transforms YBR pixels, if any, into RGB pixels
204 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
205 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
206 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
207 * @return Pointer to newly allocated pixel data.
208 * NULL if alloc fails
210 void *gdcmFile::GetImageData()
213 // I need to deallocate PixelData before doing any allocation:
216 if ( LastAllocatedPixelDataLength != ImageDataSize)
218 PixelData = new uint8_t[ImageDataSize];
221 LastAllocatedPixelDataLength = ImageDataSize;
223 GetImageDataIntoVector(PixelData, ImageDataSize);
224 // Will be 7fe0, 0010 in standard case
225 GetHeader()->SetEntryVoidAreaByNumber( PixelData,
226 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
228 PixelRead = 0; // no PixelRaw
236 * Read the pixels from disk (uncompress if necessary),
237 * Transforms YBR pixels, if any, into RGB pixels
238 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
239 * Transforms single Grey plane + 3 Palettes into a RGB Plane
240 * Copies at most MaxSize bytes of pixel data to caller allocated
242 * \warning This function allows people that want to build a volume
243 * from an image stack *not to* have, first to get the image pixels,
244 * and then move them to the volume area.
245 * It's absolutely useless for any VTK user since vtk chooses
246 * to invert the lines of an image, that is the last line comes first
247 * (for some axis related reasons?). Hence he will have
248 * to load the image line by line, starting from the end.
249 * VTK users have to call GetImageData
251 * @param destination Address (in caller's memory space) at which the
252 * pixel data should be copied
253 * @param maxSize Maximum number of bytes to be copied. When MaxSize
254 * is not sufficient to hold the pixel data the copy is not
255 * executed (i.e. no partial copy).
256 * @return On success, the number of bytes actually copied. Zero on
257 * failure e.g. MaxSize is lower than necessary.
259 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t maxSize)
261 GetImageDataIntoVectorRaw (destination, maxSize);
262 PixelRead = 0 ; // =0 : no ImageDataRaw
263 if ( !Header->HasLUT() )
265 return ImageDataSize;
268 // from Lut R + Lut G + Lut B
269 uint8_t *newDest = new uint8_t[ImageDataSize];
270 uint8_t *a = (uint8_t *)destination;
271 uint8_t *lutRGBA = Header->GetLUTRGBA();
276 // move Gray pixels to temp area
277 memmove(newDest, destination, ImageDataSizeRaw);
278 for (size_t i=0; i<ImageDataSizeRaw; ++i)
288 // now, it's an RGB image
289 // Lets's write it in the Header
291 // FIXME : Better use CreateOrReplaceIfExist ?
293 std::string spp = "3"; // Samples Per Pixel
294 Header->SetEntryByNumber(spp,0x0028,0x0002);
295 std::string rgb = "RGB "; // Photometric Interpretation
296 Header->SetEntryByNumber(rgb,0x0028,0x0004);
297 std::string planConfig = "0"; // Planar Configuration
298 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
301 else // GetLUTRGBA() failed
303 // (gdcm-US-ALOKA-16.dcm), contains Segmented xxx Palette Color
304 // that are *more* than 65535 long ?!?
305 // No idea how to manage such an image !
306 // Need to make RGB Pixels (?) from grey Pixels (?!) and Gray Lut (!?!)
307 // It seems that *no Dicom Viewer* has any idea :-(
309 std::string photomInterp = "MONOCHROME1 "; // Photometric Interpretation
310 Header->SetEntryByNumber(photomInterp,0x0028,0x0004);
313 /// \todo Drop Palette Color out of the Header?
314 return ImageDataSize;
319 * \brief Allocates necessary memory,
320 * Transforms YBR pixels (if any) into RGB pixels
321 * Transforms 3 planes R, G, B (if any) into a single RGB Plane
322 * Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
323 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
324 * @return Pointer to newly allocated pixel data.
325 * \ NULL if alloc fails
327 void * gdcmFile::GetImageDataRaw ()
330 if ( Header->HasLUT() )
331 /// \todo Let gdcmHeader user a chance to get the right value
332 imgDataSize = ImageDataSizeRaw;
334 imgDataSize = ImageDataSize;
337 // I need to deallocate PixelData before doing any allocation:
340 if ( LastAllocatedPixelDataLength != imgDataSize)
342 PixelData = new uint8_t[imgDataSize];
345 LastAllocatedPixelDataLength = imgDataSize;
347 GetImageDataIntoVectorRaw(PixelData, imgDataSize);
348 // will be 7fe0, 0010 in standard cases
349 GetHeader()->SetEntryVoidAreaByNumber(PixelData,
350 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
352 PixelRead = 1; // PixelRaw
359 * \brief Copies at most MaxSize bytes of pixel data to caller's
361 * \warning This function was designed to avoid people that want to build
362 * a volume from an image stack to need first to get the image pixels
363 * and then move them to the volume area.
364 * It's absolutely useless for any VTK user since vtk chooses
365 * to invert the lines of an image, that is the last line comes first
366 * (for some axis related reasons?). Hence he will have
367 * to load the image line by line, starting from the end.
368 * VTK users hace to call GetImageData
369 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
370 * into a single RGB Pixels Plane
371 * the (VTK) user will manage the palettes
373 * @param destination Address (in caller's memory space) at which the
374 * pixel data should be copied
375 * @param maxSize Maximum number of bytes to be copied. When MaxSize
376 * is not sufficient to hold the pixel data the copy is not
377 * executed (i.e. no partial copy).
378 * @return On success, the number of bytes actually copied. Zero on
379 * failure e.g. MaxSize is lower than necessary.
381 size_t gdcmFile::GetImageDataIntoVectorRaw (void *destination, size_t maxSize)
383 int nb, nbu, highBit, sign;
385 // we save the initial values of the following
386 // in order to be able to restore the header in a disk-consistent state
387 // (if user asks twice to get the pixels from disk)
389 if ( PixelRead == -1 ) // File was never "read" before
391 InitialSpp = Header->GetEntryByNumber(0x0028,0x0002);
392 InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004);
393 InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006);
394 InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100);
396 else // File was already "read", the following *may* have been modified
397 // we restore them to be in a disk-consistent state
399 // FIXME : What happened with the LUTs ?
400 Header->SetEntryByNumber(InitialSpp,0x0028,0x0002);
401 Header->SetEntryByNumber(InitialPhotInt,0x0028,0x0004);
402 Header->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006);
403 Header->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100);
406 PixelRead = 1 ; // PixelRaw
408 if ( ImageDataSize > maxSize )
410 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
411 "than caller's expected MaxSize");
415 ReadPixelData( destination );
417 // Number of Bits Allocated for storing a Pixel
418 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
419 if ( str_nb == GDCM_UNFOUND )
425 nb = atoi( str_nb.c_str() );
427 // From reading SetPixelDataSizeFromHeader, it seems 12 should be treated
428 // separately, correct ?
431 // Number of Bits actually used
432 std::string str_nbu = Header->GetEntryByNumber(0x0028,0x0101);
433 if ( str_nbu == GDCM_UNFOUND )
439 nbu = atoi( str_nbu.c_str() );
443 std::string str_highBit = Header->GetEntryByNumber(0x0028,0x0102);
444 if ( str_highBit == GDCM_UNFOUND )
450 highBit = atoi( str_highBit.c_str() );
456 std::string str_sign = Header->GetEntryByNumber(0x0028,0x0103);
457 if ( str_sign == GDCM_UNFOUND )
459 sign = 0; // default is unsigned
463 sign = atoi( str_sign.c_str() );
466 // re arange bytes inside the integer (processor endianity)
469 SwapZone(destination, Header->GetSwapCode(), ImageDataSize, nb);
472 // to avoid pb with some xmedcon breakers images
473 if ( nb == 16 && nbu < nb && sign == 0)
475 int l = (int)(ImageDataSize / (nb/8));
476 uint16_t *deb = (uint16_t *)destination;
477 for(int i = 0; i<l; i++)
487 // re arange bits inside the bytes
490 int l = (int)(ImageDataSize / (nb/8));
493 uint16_t mask = 0xffff;
494 mask = mask >> (nb-nbu);
495 uint16_t *deb = (uint16_t *)destination;
496 for(int i = 0; i<l; i++)
498 *deb = (*deb >> (nbu - highBit - 1)) & mask;
504 uint32_t mask = 0xffffffff;
505 mask = mask >> (nb - nbu);
506 uint32_t *deb = (uint32_t *)destination;
507 for(int i = 0; i<l; i++)
509 *deb = (*deb >> (nbu - highBit - 1)) & mask;
515 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: weird image");
519 // DO NOT remove this code commented out.
520 // Nobody knows what's expecting you ...
521 // Just to 'see' what was actually read on disk :-(
524 // f2 = fopen("SpuriousFile.RAW","wb");
525 // fwrite(destination,ImageDataSize,1,f2);
528 // Deal with the color
529 // -------------------
531 std::string str_PhotometricInterpretation =
532 Header->GetEntryByNumber(0x0028,0x0004);
534 if ( str_PhotometricInterpretation == "MONOCHROME1 "
535 || str_PhotometricInterpretation == "MONOCHROME2 " )
537 return ImageDataSize;
540 // Planar configuration = 0 : Pixels are already RGB
541 // Planar configuration = 1 : 3 planes : R, G, B
542 // Planar configuration = 2 : 1 gray Plane + 3 LUT
544 // Well ... supposed to be !
545 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
546 // PhotometricInterpretation=PALETTE COLOR
547 // and heuristic has to be found :-(
549 int planConf = Header->GetPlanarConfiguration(); // 0028,0006
551 // Whatever Planar Configuration is,
552 // "PALETTE COLOR " implies that we deal with the palette.
553 if ( str_PhotometricInterpretation == "PALETTE COLOR ")
561 // Pixels are already RGB
564 if (str_PhotometricInterpretation == "YBR_FULL")
566 // Warning : YBR_FULL_422 acts as RGB
567 // : we need to make RGB Pixels from Planes Y,cB,cR
569 // to see the tricks about YBR_FULL, YBR_FULL_422,
570 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
571 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
572 // and be *very* affraid
574 int l = Header->GetXSize() * Header->GetYSize();
575 int nbFrames = Header->GetZSize();
577 uint8_t* newDest = new uint8_t[ImageDataSize];
578 uint8_t* x = newDest;
579 uint8_t* a = (uint8_t*)destination;
584 /// \todo : Replace by the 'well known' integer computation
586 /// see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
587 /// for code optimisation
589 for (int i = 0; i < nbFrames; i++)
591 for (int j = 0; j < l; j++)
593 R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
594 G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
595 B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
597 if (R < 0.0) R = 0.0;
598 if (G < 0.0) G = 0.0;
599 if (B < 0.0) B = 0.0;
600 if (R > 255.0) R = 255.0;
601 if (G > 255.0) G = 255.0;
602 if (B > 255.0) B = 255.0;
610 memmove(destination, newDest, ImageDataSize);
615 // need to make RGB Pixels from R,G,B Planes
616 // (all the Frames at a time)
618 int l = Header->GetXSize() * Header->GetYSize() * Header->GetZSize();
620 uint8_t *newDest = new uint8_t[ImageDataSize];
621 uint8_t *x = newDest;
622 uint8_t *a = (uint8_t *)destination;
626 for (int j = 0; j < l; j++)
632 memmove(destination, newDest, ImageDataSize);
637 // Palettes were found
638 // Let the user deal with them !
639 return ImageDataSize;
641 // now, it's an RGB image
642 // Lets's write it in the Header
644 // CreateOrReplaceIfExist ?
646 std::string spp = "3"; // Samples Per Pixel
647 std::string photInt = "RGB "; // Photometric Interpretation
648 std::string planConfig = "0"; // Planar Configuration
652 Header->SetEntryByNumber(spp,0x0028,0x0002);
653 Header->SetEntryByNumber(photInt,0x0028,0x0004);
654 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
656 /// \todo Drop Palette Color out of the Header?
657 return ImageDataSize;
662 * \brief performs a shalow copy (not a deep copy) of the user given
664 * 'image' Pixels are presented as C-like 2D arrays : line per line.
665 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
666 * \warning user is kindly requested NOT TO 'free' the Pixel area
667 * @param inData user supplied pixel area
668 * @param expectedSize total image size, in Bytes
672 bool gdcmFile::SetImageData(void *inData, size_t expectedSize)
674 Header->SetImageDataSize( expectedSize );
675 // FIXME : if already allocated, memory leak !
677 ImageDataSize = ImageDataSizeRaw = expectedSize;
679 // FIXME : 7fe0, 0010 IS NOT set ...
685 * \brief Writes on disk A SINGLE Dicom file
686 * NO test is performed on processor "Endiannity".
687 * It's up to the user to call his Reader properly
688 * @param fileName name of the file to be created
689 * (any already existing file is over written)
690 * @return false if write fails
693 bool gdcmFile::WriteRawData(std::string const & fileName)
695 FILE *fp1 = fopen(fileName.c_str(), "wb");
698 printf("Fail to open (write) file [%s] \n", fileName.c_str());
701 fwrite (PixelData, ImageDataSize, 1, fp1);
709 * \brief Writes on disk A SINGLE Dicom file,
710 * using the Implicit Value Representation convention
711 * NO test is performed on processor "Endiannity".
712 * @param fileName name of the file to be created
713 * (any already existing file is overwritten)
714 * @return false if write fails
717 bool gdcmFile::WriteDcmImplVR (std::string const & fileName)
719 return WriteBase(fileName, gdcmImplicitVR);
724 * \brief Writes on disk A SINGLE Dicom file,
725 * using the Explicit Value Representation convention
726 * NO test is performed on processor "Endiannity". * @param fileName name of the file to be created
727 * (any already existing file is overwritten)
728 * @return false if write fails
731 bool gdcmFile::WriteDcmExplVR (std::string const & fileName)
733 return WriteBase(fileName, gdcmExplicitVR);
738 * \brief Writes on disk A SINGLE Dicom file,
739 * using the ACR-NEMA convention
740 * NO test is performed on processor "Endiannity".
741 * (a l'attention des logiciels cliniques
742 * qui ne prennent en entrée QUE des images ACR ...
743 * \warning if a DICOM_V3 header is supplied,
744 * groups < 0x0008 and shadow groups are ignored
745 * \warning NO TEST is performed on processor "Endiannity".
746 * @param fileName name of the file to be created
747 * (any already existing file is overwritten)
748 * @return false if write fails
751 bool gdcmFile::WriteAcr (std::string const & fileName)
753 return WriteBase(fileName, gdcmACR);
756 //-----------------------------------------------------------------------------
760 * \brief NOT a end user inteded function
761 * (used by WriteDcmExplVR, WriteDcmImplVR, WriteAcr, etc)
762 * @param fileName name of the file to be created
763 * (any already existing file is overwritten)
764 * @param type file type (ExplicitVR, ImplicitVR, ...)
765 * @return false if write fails
767 bool gdcmFile::WriteBase (std::string const & fileName, FileType type)
769 if ( PixelRead == -1 && type != gdcmExplicitVR)
774 FILE *fp1 = fopen(fileName.c_str(), "wb");
777 printf("Failed to open (write) File [%s] \n", fileName.c_str());
781 if ( type == gdcmImplicitVR || type == gdcmExplicitVR )
783 // writing Dicom File Preamble
784 uint8_t* filePreamble = new uint8_t[128];
785 memset(filePreamble, 0, 128);
786 fwrite(filePreamble, 128, 1, fp1);
787 fwrite("DICM", 4, 1, fp1);
789 delete[] filePreamble;
792 // --------------------------------------------------------------
793 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
795 // if recognition code tells us we dealt with a LibIDO image
796 // we reproduce on disk the switch between lineNumber and columnNumber
797 // just before writting ...
799 /// \todo the best trick would be *change* the recognition code
800 /// but pb expected if user deals with, e.g. COMPLEX images
802 std::string rows, columns;
803 if ( Header->GetFileType() == gdcmACR_LIBIDO)
805 rows = Header->GetEntryByNumber(0x0028, 0x0010);
806 columns = Header->GetEntryByNumber(0x0028, 0x0011);
808 Header->SetEntryByNumber(columns, 0x0028, 0x0010);
809 Header->SetEntryByNumber(rows , 0x0028, 0x0011);
811 // ----------------- End of Special Patch ----------------
813 uint16_t grPixel = Header->GetGrPixel();
814 uint16_t numPixel = Header->GetNumPixel();;
816 gdcmDocEntry* PixelElement =
817 GetHeader()->GetDocEntryByNumber(grPixel, numPixel);
819 if ( PixelRead == 1 )
821 // we read pixel 'as is' (no tranformation LUT -> RGB)
822 PixelElement->SetLength( ImageDataSizeRaw );
824 else if ( PixelRead == 0 )
826 // we tranformed GrayLevel pixels + LUT into RGB Pixel
827 PixelElement->SetLength( ImageDataSize );
830 Header->Write(fp1, type);
832 // --------------------------------------------------------------
833 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
835 // ...and we restore the Header to be Dicom Compliant again
836 // just after writting
838 if ( Header->GetFileType() == gdcmACR_LIBIDO )
840 Header->SetEntryByNumber(rows , 0x0028, 0x0010);
841 Header->SetEntryByNumber(columns, 0x0028, 0x0011);
843 // ----------------- End of Special Patch ----------------
845 // fwrite(PixelData, ImageDataSize, 1, fp1); // should be useless, now
851 //-----------------------------------------------------------------------------
855 * \brief Swap the bytes, according to swap code.
856 * \warning not end user intended
857 * @param im area to deal with
858 * @param swap swap code
859 * @param lgr Area Length
860 * @param nb Pixels Bit number
862 void gdcmFile::SwapZone(void *im, int swap, int lgr, int nb)
868 uint16_t* im16 = (uint16_t*)im;
879 for(i=0; i < lgr/2; i++)
881 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
885 std::cout << "SWAP value (16 bits) not allowed :i" << swap <<
892 uint16_t fort, faible;
893 uint32_t* im32 = (uint32_t*)im;
900 for(i = 0; i < lgr/4; i++)
902 faible = im32[i] & 0x0000ffff; // 4321
903 fort = im32[i] >> 16;
904 fort = ( fort >> 8 ) | ( fort << 8 );
905 faible = ( faible >> 8 ) | ( faible << 8);
907 im32[i] = ( s32 << 16 ) | fort;
911 for(i = 0; i < lgr/4; i++)
913 faible = im32[i] & 0x0000ffff; // 2143
914 fort = im32[i] >> 16;
915 fort = ( fort >> 8 ) | ( fort << 8 );
916 faible = ( faible >> 8) | ( faible << 8);
918 im32[i] = ( s32 << 16 ) | faible;
922 for(i = 0; i < lgr/4; i++)
924 faible = im32[i] & 0x0000ffff; // 3412
925 fort = im32[i] >> 16;
927 im32[i] = ( s32 << 16 ) | fort;
931 std::cout << "SWAP value (32 bits) not allowed : " << swap <<
939 * \brief Read pixel data from disk (optionaly decompressing) into the
940 * caller specified memory location.
941 * @param destination where the pixel data should be stored.
944 bool gdcmFile::ReadPixelData(void *destination)
946 FILE *fp = Header->OpenFile();
952 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 )
958 // ---------------------- Compacted File (12 Bits Per Pixel)
959 // unpack 12 Bits pixels into 16 Bits pixels
960 // 2 pixels 12bit = [0xABCDEF]
961 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
963 if ( Header->GetBitsAllocated() == 12 )
965 int nbPixels = Header->GetXSize() * Header->GetYSize();
968 uint16_t* pdestination = (uint16_t*)destination;
969 for(int p = 0; p < nbPixels; p += 2 )
975 //Two steps is necessary to please VC++
976 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
978 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
981 // Troubles expected on Big-Endian processors ?
988 // ---------------------- Uncompressed File
989 if ( !Header->IsDicomV3() ||
990 Header->IsImplicitVRLittleEndianTransferSyntax() ||
991 Header->IsExplicitVRLittleEndianTransferSyntax() ||
992 Header->IsExplicitVRBigEndianTransferSyntax() ||
993 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() )
995 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
1007 // ---------------------- Run Length Encoding
1008 if ( Header->IsRLELossLessTransferSyntax() )
1010 bool res = gdcm_read_RLE_file (fp,destination);
1011 Header->CloseFile();
1015 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
1017 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
1018 if ( str_nb == GDCM_UNFOUND )
1024 nb = atoi( str_nb.c_str() );
1027 nb = 16; // ?? 12 should be ACR-NEMA only
1032 int taille = Header->GetXSize() * Header->GetYSize()
1033 * Header->GetSamplesPerPixel();
1034 long fragmentBegining; // for ftell, fseek
1036 bool jpg2000 = Header->IsJPEG2000();
1037 bool jpgLossless = Header->IsJPEGLossless();
1040 uint16_t ItemTagGr, ItemTagEl;
1043 // Position on begining of Jpeg Pixels
1045 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1046 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1047 if(Header->GetSwapCode())
1049 ItemTagGr = Header->SwapShort(ItemTagGr);
1050 ItemTagEl = Header->SwapShort(ItemTagEl);
1054 if( Header->GetSwapCode() )
1056 ln = Header->SwapLong( ln ); // Basic Offset Table Item length
1061 // What is it used for ?!?
1062 uint8_t* BasicOffsetTableItemValue = new uint8_t[ln+1];
1063 fread(BasicOffsetTableItemValue,ln,1,fp);
1064 //delete[] BasicOffsetTableItemValue;
1067 // first Fragment initialisation
1068 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1069 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1070 if( Header->GetSwapCode() )
1072 ItemTagGr = Header->SwapShort( ItemTagGr );
1073 ItemTagEl = Header->SwapShort( ItemTagEl );
1076 // parsing fragments until Sequence Delim. Tag found
1077 while ( ItemTagGr == 0xfffe && ItemTagEl != 0xe0dd )
1079 // --- for each Fragment
1081 if( Header->GetSwapCode() )
1083 ln = Header->SwapLong(ln); // Fragment Item length
1085 fragmentBegining = ftell( fp );
1089 // JPEG 2000 : call to ???
1090 res = gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
1091 // ------------------------------------- endif (JPEG2000)
1093 else if (jpgLossless)
1095 // JPEG LossLess : call to xmedcom Lossless JPEG
1096 // Reading Fragment pixels
1097 JPEGLosslessDecodeImage (fp, (uint16_t*)destination,
1098 Header->GetPixelSize() * 8 * Header->GetSamplesPerPixel(), ln);
1099 res = 1; // in order not to break the loop
1101 } // ------------------------------------- endif (JPEGLossless)
1104 // JPEG Lossy : call to IJG 6b
1105 if ( Header->GetBitsStored() == 8)
1107 // Reading Fragment pixels
1108 res = gdcm_read_JPEG_file (fp,destination);
1112 // Reading Fragment pixels
1113 res = gdcm_read_JPEG_file12 (fp,destination);
1115 // ------------------------------------- endif (JPEGLossy)
1123 // location in user's memory
1124 // for next fragment (if any)
1125 destination = (uint8_t*)destination + taille * nBytes;
1127 fseek(fp,fragmentBegining, SEEK_SET); // To be sure we start
1128 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
1130 ItemTagGr = ItemTagEl = 0;
1131 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1132 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1133 if( Header->GetSwapCode() )
1135 ItemTagGr = Header->SwapShort( ItemTagGr );
1136 ItemTagEl = Header->SwapShort( ItemTagEl );
1139 // endWhile parsing fragments until Sequence Delim. Tag found
1141 Header->CloseFile();
1144 //-----------------------------------------------------------------------------