1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2004/09/13 07:49:36 $
7 Version: $Revision: 1.125 $
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
42 gdcmFile::gdcmFile(gdcmHeader *header)
51 * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
52 * file (gdcmHeader only deals with the ... header)
53 * Opens (in read only and when possible) an existing file and checks
54 * for DICOM compliance. Returns NULL on failure.
55 * It will be up to the user to load the pixels into memory
56 * (see GetImageData, GetImageDataRaw)
57 * \note the in-memory representation of all available tags found in
58 * the DICOM header is post-poned to first header information access.
59 * This avoid a double parsing of public part of the header when
60 * one sets an a posteriori shadow dictionary (efficiency can be
61 * seen as a side effect).
62 * @param filename file to be opened for parsing
64 gdcmFile::gdcmFile(std::string const & filename )
66 Header = new gdcmHeader( filename );
73 * \brief Sets some initial for the Constructor
75 void gdcmFile::SetInitialValues()
77 PixelRead = -1; // no ImageData read yet.
78 LastAllocatedPixelDataLength = 0;
83 InitialPlanConfig = "";
84 InitialBitsAllocated = "";
86 InitialRedLUTDescr = 0;
87 InitialGreenLUTDescr = 0;
88 InitialBlueLUTDescr = 0;
89 InitialRedLUTData = 0;
90 InitialGreenLUTData = 0;
91 InitialBlueLUTData = 0;
93 if ( Header->IsReadable() )
95 SetPixelDataSizeFromHeader();
97 // the following values *may* be modified
98 // by gdcmFile::GetImageDataIntoVectorRaw
99 // we save their initial value.
100 InitialSpp = Header->GetEntryByNumber(0x0028,0x0002);
101 InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004);
102 InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006);
103 InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100);
105 // the following entries *may* be removed from the H table
106 // (NOT deleted ...) by gdcmFile::GetImageDataIntoVectorRaw
107 // we keep a pointer on them.
108 InitialRedLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1101);
109 InitialGreenLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1102);
110 InitialBlueLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1103);
111 InitialRedLUTData = Header->GetDocEntryByNumber(0x0028,0x1201);
112 InitialGreenLUTData = Header->GetDocEntryByNumber(0x0028,0x1202);
113 InitialBlueLUTData = Header->GetDocEntryByNumber(0x0028,0x1203);
119 * \brief canonical destructor
120 * \note If the gdcmHeader was created by the gdcmFile constructor,
121 * it is destroyed by the gdcmFile
123 gdcmFile::~gdcmFile()
131 // InitialLutDescriptors and InitialLutData
132 // will have to be deleted if the don't belong any longer
133 // to the Header H table ...
136 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
144 * \brief computes the length (in bytes) we must ALLOCATE to receive the
145 * image(s) pixels (multiframes taken into account)
146 * \warning : it is NOT the group 7FE0 length
147 * (no interest for compressed images).
148 * \warning : not end user intended ?
150 void gdcmFile::SetPixelDataSizeFromHeader()
152 // see PS 3.3-2003 : C.7.6.3.2.1
162 // YBR_FULL_422 (no LUT, no Palette)
168 // ex : gdcm-US-ALOKA-16.dcm
169 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
170 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
171 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
173 // ex : OT-PAL-8-face.dcm
174 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
175 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
176 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
178 // Number of "Bits Allocated"
180 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
182 if ( str_nb == GDCM_UNFOUND )
188 nb = atoi( str_nb.c_str() );
195 ImageDataSizeRaw = Header->GetXSize() * Header->GetYSize()
196 * Header->GetZSize() * (nb/8) * Header->GetSamplesPerPixel();
197 std::string str_PhotometricInterpretation =
198 Header->GetEntryByNumber(0x0028,0x0004);
200 // if ( str_PhotometricInterpretation == "PALETTE COLOR " ),
201 // pb when undealt Segmented Palette Color
203 if ( Header->HasLUT() )
211 * \brief - Allocates necessary memory,
212 * - Reads the pixels from disk (uncompress if necessary),
213 * - Transforms YBR pixels, if any, into RGB pixels
214 * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
215 * - Transforms single Grey plane + 3 Palettes into a RGB Plane
216 * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
217 * @return Pointer to newly allocated pixel data.
218 * NULL if alloc fails
220 void *gdcmFile::GetImageData()
223 // I need to deallocate Pixel_Data before doing any allocation:
226 if ( LastAllocatedPixelDataLength != ImageDataSize )
229 Pixel_Data = new uint8_t[ImageDataSize];
233 LastAllocatedPixelDataLength = ImageDataSize;
235 // we load the pixels (and transform grey level + LUT into RGB)
236 GetImageDataIntoVector(Pixel_Data, ImageDataSize);
238 // We say the value *is* loaded.
239 GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
240 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
242 // Will be 7fe0, 0010 in standard case
243 GetHeader()->SetEntryVoidAreaByNumber( Pixel_Data,
244 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
246 PixelRead = 0; // no PixelRaw
254 * Read the pixels from disk (uncompress if necessary),
255 * Transforms YBR pixels, if any, into RGB pixels
256 * Transforms 3 planes R, G, B, if any, into a single RGB Plane
257 * Transforms single Grey plane + 3 Palettes into a RGB Plane
258 * Copies at most MaxSize bytes of pixel data to caller allocated
260 * \warning This function allows people that want to build a volume
261 * from an image stack *not to* have, first to get the image pixels,
262 * and then move them to the volume area.
263 * It's absolutely useless for any VTK user since vtk chooses
264 * to invert the lines of an image, that is the last line comes first
265 * (for some axis related reasons?). Hence he will have
266 * to load the image line by line, starting from the end.
267 * VTK users have to call GetImageData
269 * @param destination Address (in caller's memory space) at which the
270 * pixel data should be copied
271 * @param maxSize Maximum number of bytes to be copied. When MaxSize
272 * is not sufficient to hold the pixel data the copy is not
273 * executed (i.e. no partial copy).
274 * @return On success, the number of bytes actually copied. Zero on
275 * failure e.g. MaxSize is lower than necessary.
277 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t maxSize)
279 GetImageDataIntoVectorRaw (destination, maxSize);
280 PixelRead = 0 ; // =0 : no ImageDataRaw
281 if ( !Header->HasLUT() )
283 return ImageDataSize;
286 // from Lut R + Lut G + Lut B
287 uint8_t *newDest = new uint8_t[ImageDataSize];
288 uint8_t *a = (uint8_t *)destination;
289 uint8_t *lutRGBA = Header->GetLUTRGBA();
294 // move Gray pixels to temp area
295 memmove(newDest, destination, ImageDataSizeRaw);
296 for (size_t i=0; i<ImageDataSizeRaw; ++i)
306 // now, it's an RGB image
307 // Lets's write it in the Header
309 // FIXME : Better use CreateOrReplaceIfExist ?
311 std::string spp = "3"; // Samples Per Pixel
312 Header->SetEntryByNumber(spp,0x0028,0x0002);
313 std::string rgb = "RGB "; // Photometric Interpretation
314 Header->SetEntryByNumber(rgb,0x0028,0x0004);
315 std::string planConfig = "0"; // Planar Configuration
316 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
319 else // GetLUTRGBA() failed
321 // (gdcm-US-ALOKA-16.dcm), contains Segmented xxx Palette Color
322 // that are *more* than 65535 long ?!?
323 // No idea how to manage such an image !
324 // Need to make RGB Pixels (?) from grey Pixels (?!) and Gray Lut (!?!)
325 // It seems that *no Dicom Viewer* has any idea :-(
327 std::string photomInterp = "MONOCHROME1 "; // Photometric Interpretation
328 Header->SetEntryByNumber(photomInterp,0x0028,0x0004);
331 /// \todo Drop Palette Color out of the Header?
332 return ImageDataSize;
337 * \brief Allocates necessary memory,
338 * Transforms YBR pixels (if any) into RGB pixels
339 * Transforms 3 planes R, G, B (if any) into a single RGB Plane
340 * Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
341 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
342 * @return Pointer to newly allocated pixel data.
343 * \ NULL if alloc fails
345 void * gdcmFile::GetImageDataRaw ()
348 if ( Header->HasLUT() )
349 /// \todo Let gdcmHeader user a chance to get the right value
350 imgDataSize = ImageDataSizeRaw;
352 imgDataSize = ImageDataSize;
355 // I need to deallocate Pixel_Data before doing any allocation:
358 if ( LastAllocatedPixelDataLength != imgDataSize )
361 Pixel_Data = new uint8_t[imgDataSize];
365 LastAllocatedPixelDataLength = imgDataSize;
367 // we load the pixels ( grey level or RGB, but NO transformation)
368 GetImageDataIntoVectorRaw(Pixel_Data, imgDataSize);
370 // We say the value *is* loaded.
371 GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
372 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
374 // will be 7fe0, 0010 in standard cases
375 GetHeader()->SetEntryVoidAreaByNumber(Pixel_Data,
376 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
378 PixelRead = 1; // PixelRaw
385 * \brief Copies at most MaxSize bytes of pixel data to caller's
387 * \warning This function was designed to avoid people that want to build
388 * a volume from an image stack to need first to get the image pixels
389 * and then move them to the volume area.
390 * It's absolutely useless for any VTK user since vtk chooses
391 * to invert the lines of an image, that is the last line comes first
392 * (for some axis related reasons?). Hence he will have
393 * to load the image line by line, starting from the end.
394 * VTK users hace to call GetImageData
395 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
396 * into a single RGB Pixels Plane
397 * the (VTK) user will manage the palettes
399 * @param destination Address (in caller's memory space) at which the
400 * pixel data should be copied
401 * @param maxSize Maximum number of bytes to be copied. When MaxSize
402 * is not sufficient to hold the pixel data the copy is not
403 * executed (i.e. no partial copy).
404 * @return On success, the number of bytes actually copied. Zero on
405 * failure e.g. MaxSize is lower than necessary.
407 size_t gdcmFile::GetImageDataIntoVectorRaw (void *destination, size_t maxSize)
409 int nb, nbu, highBit, sign;
411 // we save the initial values of the following
412 // in order to be able to restore the header in a disk-consistent state
413 // (if user asks twice to get the pixels from disk)
415 if ( PixelRead == -1 ) // File was never "read" before
417 InitialSpp = Header->GetEntryByNumber(0x0028,0x0002);
418 InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004);
419 InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006);
420 InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100);
422 else // File was already "read", the following *may* have been modified
423 // we restore them to be in a disk-consistent state
425 // FIXME : What happened with the LUTs ?
426 Header->SetEntryByNumber(InitialSpp,0x0028,0x0002);
427 Header->SetEntryByNumber(InitialPhotInt,0x0028,0x0004);
428 Header->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006);
429 Header->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100);
432 PixelRead = 1 ; // PixelRaw
434 if ( ImageDataSize > maxSize )
436 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
437 "than caller's expected MaxSize");
441 ReadPixelData( destination );
443 // Number of Bits Allocated for storing a Pixel
444 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
445 if ( str_nb == GDCM_UNFOUND )
451 nb = atoi( str_nb.c_str() );
453 // From reading SetPixelDataSizeFromHeader, it seems 12 should be treated
454 // separately, correct ?
457 // Number of Bits actually used
458 std::string str_nbu = Header->GetEntryByNumber(0x0028,0x0101);
459 if ( str_nbu == GDCM_UNFOUND )
465 nbu = atoi( str_nbu.c_str() );
469 std::string str_highBit = Header->GetEntryByNumber(0x0028,0x0102);
470 if ( str_highBit == GDCM_UNFOUND )
476 highBit = atoi( str_highBit.c_str() );
482 std::string str_sign = Header->GetEntryByNumber(0x0028,0x0103);
483 if ( str_sign == GDCM_UNFOUND )
485 sign = 0; // default is unsigned
489 sign = atoi( str_sign.c_str() );
492 // re arange bytes inside the integer (processor endianity)
495 SwapZone(destination, Header->GetSwapCode(), ImageDataSize, nb);
498 // to avoid pb with some xmedcon breakers images
499 if ( nb == 16 && nbu < nb && sign == 0)
501 int l = (int)(ImageDataSize / (nb/8));
502 uint16_t *deb = (uint16_t *)destination;
503 for(int i = 0; i<l; i++)
513 // re arange bits inside the bytes
516 int l = (int)(ImageDataSize / (nb/8));
519 uint16_t mask = 0xffff;
520 mask = mask >> (nb-nbu);
521 uint16_t *deb = (uint16_t *)destination;
522 for(int i = 0; i<l; i++)
524 *deb = (*deb >> (nbu - highBit - 1)) & mask;
530 uint32_t mask = 0xffffffff;
531 mask = mask >> (nb - nbu);
532 uint32_t *deb = (uint32_t *)destination;
533 for(int i = 0; i<l; i++)
535 *deb = (*deb >> (nbu - highBit - 1)) & mask;
541 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: weird image");
545 // DO NOT remove this code commented out.
546 // Nobody knows what's expecting you ...
547 // Just to 'see' what was actually read on disk :-(
550 // f2 = fopen("SpuriousFile.RAW","wb");
551 // fwrite(destination,ImageDataSize,1,f2);
554 // Deal with the color
555 // -------------------
557 std::string str_PhotometricInterpretation =
558 Header->GetEntryByNumber(0x0028,0x0004);
560 if ( str_PhotometricInterpretation == "MONOCHROME1 "
561 || str_PhotometricInterpretation == "MONOCHROME2 " )
563 return ImageDataSize;
566 // Planar configuration = 0 : Pixels are already RGB
567 // Planar configuration = 1 : 3 planes : R, G, B
568 // Planar configuration = 2 : 1 gray Plane + 3 LUT
570 // Well ... supposed to be !
571 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
572 // PhotometricInterpretation=PALETTE COLOR
573 // and heuristic has to be found :-(
575 int planConf = Header->GetPlanarConfiguration(); // 0028,0006
577 // Whatever Planar Configuration is,
578 // "PALETTE COLOR " implies that we deal with the palette.
579 if ( str_PhotometricInterpretation == "PALETTE COLOR ")
587 // Pixels are already RGB
590 if (str_PhotometricInterpretation == "YBR_FULL")
592 // Warning : YBR_FULL_422 acts as RGB
593 // : we need to make RGB Pixels from Planes Y,cB,cR
595 // to see the tricks about YBR_FULL, YBR_FULL_422,
596 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
597 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
598 // and be *very* affraid
600 int l = Header->GetXSize() * Header->GetYSize();
601 int nbFrames = Header->GetZSize();
603 uint8_t* newDest = new uint8_t[ImageDataSize];
604 uint8_t* x = newDest;
605 uint8_t* a = (uint8_t*)destination;
610 /// \todo : Replace by the 'well known' integer computation
612 /// see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
613 /// for code optimisation
615 for (int i = 0; i < nbFrames; i++)
617 for (int j = 0; j < l; j++)
619 R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
620 G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
621 B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
623 if (R < 0.0) R = 0.0;
624 if (G < 0.0) G = 0.0;
625 if (B < 0.0) B = 0.0;
626 if (R > 255.0) R = 255.0;
627 if (G > 255.0) G = 255.0;
628 if (B > 255.0) B = 255.0;
636 memmove(destination, newDest, ImageDataSize);
641 // need to make RGB Pixels from R,G,B Planes
642 // (all the Frames at a time)
644 int l = Header->GetXSize() * Header->GetYSize() * Header->GetZSize();
646 uint8_t *newDest = new uint8_t[ImageDataSize];
647 uint8_t *x = newDest;
648 uint8_t *a = (uint8_t *)destination;
652 for (int j = 0; j < l; j++)
658 memmove(destination, newDest, ImageDataSize);
663 // Palettes were found
664 // Let the user deal with them !
665 return ImageDataSize;
667 // now, it's an RGB image
668 // Lets's write it in the Header
670 // CreateOrReplaceIfExist ?
672 std::string spp = "3"; // Samples Per Pixel
673 std::string photInt = "RGB "; // Photometric Interpretation
674 std::string planConfig = "0"; // Planar Configuration
678 Header->SetEntryByNumber(spp,0x0028,0x0002);
679 Header->SetEntryByNumber(photInt,0x0028,0x0004);
680 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
682 /// \todo Drop Palette Color out of the Header?
683 return ImageDataSize;
688 * \brief performs a shalow copy (not a deep copy) of the user given
690 * 'image' Pixels are presented as C-like 2D arrays : line per line.
691 * 'volume'Pixels are presented as C-like 3D arrays : plane per plane
692 * \warning user is kindly requested NOT TO 'free' the Pixel area
693 * @param inData user supplied pixel area
694 * @param expectedSize total image size, in Bytes
698 bool gdcmFile::SetImageData(void *inData, size_t expectedSize)
700 Header->SetImageDataSize( expectedSize );
701 // FIXME : if already allocated, memory leak !
703 ImageDataSize = ImageDataSizeRaw = expectedSize;
705 // FIXME : 7fe0, 0010 IS NOT set ...
711 * \brief Writes on disk A SINGLE Dicom file
712 * NO test is performed on processor "Endiannity".
713 * It's up to the user to call his Reader properly
714 * @param fileName name of the file to be created
715 * (any already existing file is over written)
716 * @return false if write fails
719 bool gdcmFile::WriteRawData(std::string const & fileName)
721 FILE *fp1 = fopen(fileName.c_str(), "wb");
724 printf("Fail to open (write) file [%s] \n", fileName.c_str());
727 fwrite (Pixel_Data, ImageDataSize, 1, fp1);
735 * \brief Writes on disk A SINGLE Dicom file,
736 * using the Implicit Value Representation convention
737 * NO test is performed on processor "Endiannity".
738 * @param fileName name of the file to be created
739 * (any already existing file is overwritten)
740 * @return false if write fails
743 bool gdcmFile::WriteDcmImplVR (std::string const & fileName)
745 return WriteBase(fileName, gdcmImplicitVR);
750 * \brief Writes on disk A SINGLE Dicom file,
751 * using the Explicit Value Representation convention
752 * NO test is performed on processor "Endiannity". * @param fileName name of the file to be created
753 * (any already existing file is overwritten)
754 * @return false if write fails
757 bool gdcmFile::WriteDcmExplVR (std::string const & fileName)
759 return WriteBase(fileName, gdcmExplicitVR);
764 * \brief Writes on disk A SINGLE Dicom file,
765 * using the ACR-NEMA convention
766 * NO test is performed on processor "Endiannity".
767 * (a l'attention des logiciels cliniques
768 * qui ne prennent en entrée QUE des images ACR ...
769 * \warning if a DICOM_V3 header is supplied,
770 * groups < 0x0008 and shadow groups are ignored
771 * \warning NO TEST is performed on processor "Endiannity".
772 * @param fileName name of the file to be created
773 * (any already existing file is overwritten)
774 * @return false if write fails
777 bool gdcmFile::WriteAcr (std::string const & fileName)
779 return WriteBase(fileName, gdcmACR);
782 //-----------------------------------------------------------------------------
786 * \brief NOT a end user inteded function
787 * (used by WriteDcmExplVR, WriteDcmImplVR, WriteAcr, etc)
788 * @param fileName name of the file to be created
789 * (any already existing file is overwritten)
790 * @param type file type (ExplicitVR, ImplicitVR, ...)
791 * @return false if write fails
793 bool gdcmFile::WriteBase (std::string const & fileName, FileType type)
795 if ( PixelRead == -1 && type != gdcmExplicitVR)
800 FILE *fp1 = fopen(fileName.c_str(), "wb");
803 printf("Failed to open (write) File [%s] \n", fileName.c_str());
807 if ( type == gdcmImplicitVR || type == gdcmExplicitVR )
809 // writing Dicom File Preamble
810 uint8_t* filePreamble = new uint8_t[128];
811 memset(filePreamble, 0, 128);
812 fwrite(filePreamble, 128, 1, fp1);
813 fwrite("DICM", 4, 1, fp1);
815 delete[] filePreamble;
818 // --------------------------------------------------------------
819 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
821 // if recognition code tells us we dealt with a LibIDO image
822 // we reproduce on disk the switch between lineNumber and columnNumber
823 // just before writting ...
825 /// \todo the best trick would be *change* the recognition code
826 /// but pb expected if user deals with, e.g. COMPLEX images
828 std::string rows, columns;
829 if ( Header->GetFileType() == gdcmACR_LIBIDO)
831 rows = Header->GetEntryByNumber(0x0028, 0x0010);
832 columns = Header->GetEntryByNumber(0x0028, 0x0011);
834 Header->SetEntryByNumber(columns, 0x0028, 0x0010);
835 Header->SetEntryByNumber(rows , 0x0028, 0x0011);
837 // ----------------- End of Special Patch ----------------
839 uint16_t grPixel = Header->GetGrPixel();
840 uint16_t numPixel = Header->GetNumPixel();;
842 gdcmDocEntry* PixelElement =
843 GetHeader()->GetDocEntryByNumber(grPixel, numPixel);
845 if ( PixelRead == 1 )
847 // we read pixel 'as is' (no tranformation LUT -> RGB)
848 PixelElement->SetLength( ImageDataSizeRaw );
850 else if ( PixelRead == 0 )
852 // we tranformed GrayLevel pixels + LUT into RGB Pixel
853 PixelElement->SetLength( ImageDataSize );
856 Header->Write(fp1, type);
858 // --------------------------------------------------------------
859 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
861 // ...and we restore the Header to be Dicom Compliant again
862 // just after writting
864 if ( Header->GetFileType() == gdcmACR_LIBIDO )
866 Header->SetEntryByNumber(rows , 0x0028, 0x0010);
867 Header->SetEntryByNumber(columns, 0x0028, 0x0011);
869 // ----------------- End of Special Patch ----------------
871 // fwrite(Pixel_Data, ImageDataSize, 1, fp1); // should be useless, now
877 //-----------------------------------------------------------------------------
881 * \brief Swap the bytes, according to swap code.
882 * \warning not end user intended
883 * @param im area to deal with
884 * @param swap swap code
885 * @param lgr Area Length
886 * @param nb Pixels Bit number
888 void gdcmFile::SwapZone(void *im, int swap, int lgr, int nb)
894 uint16_t* im16 = (uint16_t*)im;
905 for(i=0; i < lgr/2; i++)
907 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
911 std::cout << "SWAP value (16 bits) not allowed :i" << swap <<
918 uint16_t fort, faible;
919 uint32_t* im32 = (uint32_t*)im;
926 for(i = 0; i < lgr/4; i++)
928 faible = im32[i] & 0x0000ffff; // 4321
929 fort = im32[i] >> 16;
930 fort = ( fort >> 8 ) | ( fort << 8 );
931 faible = ( faible >> 8 ) | ( faible << 8);
933 im32[i] = ( s32 << 16 ) | fort;
937 for(i = 0; i < lgr/4; i++)
939 faible = im32[i] & 0x0000ffff; // 2143
940 fort = im32[i] >> 16;
941 fort = ( fort >> 8 ) | ( fort << 8 );
942 faible = ( faible >> 8) | ( faible << 8);
944 im32[i] = ( s32 << 16 ) | faible;
948 for(i = 0; i < lgr/4; i++)
950 faible = im32[i] & 0x0000ffff; // 3412
951 fort = im32[i] >> 16;
953 im32[i] = ( s32 << 16 ) | fort;
957 std::cout << "SWAP value (32 bits) not allowed : " << swap <<
965 * \brief Read pixel data from disk (optionaly decompressing) into the
966 * caller specified memory location.
967 * @param destination where the pixel data should be stored.
970 bool gdcmFile::ReadPixelData(void *destination)
972 FILE *fp = Header->OpenFile();
978 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 )
984 // ---------------------- Compacted File (12 Bits Per Pixel)
985 // unpack 12 Bits pixels into 16 Bits pixels
986 // 2 pixels 12bit = [0xABCDEF]
987 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
989 if ( Header->GetBitsAllocated() == 12 )
991 int nbPixels = Header->GetXSize() * Header->GetYSize();
994 uint16_t* pdestination = (uint16_t*)destination;
995 for(int p = 0; p < nbPixels; p += 2 )
1001 //Two steps is necessary to please VC++
1002 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
1004 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
1007 // Troubles expected on Big-Endian processors ?
1010 Header->CloseFile();
1014 // ---------------------- Uncompressed File
1015 if ( !Header->IsDicomV3() ||
1016 Header->IsImplicitVRLittleEndianTransferSyntax() ||
1017 Header->IsExplicitVRLittleEndianTransferSyntax() ||
1018 Header->IsExplicitVRBigEndianTransferSyntax() ||
1019 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() )
1021 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
1022 Header->CloseFile();
1023 if ( ItemRead != 1 )
1033 // ---------------------- Run Length Encoding
1034 if ( Header->IsRLELossLessTransferSyntax() )
1036 bool res = gdcm_read_RLE_file (fp,destination);
1037 Header->CloseFile();
1041 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
1043 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
1044 if ( str_nb == GDCM_UNFOUND )
1050 nb = atoi( str_nb.c_str() );
1053 nb = 16; // ?? 12 should be ACR-NEMA only
1058 int taille = Header->GetXSize() * Header->GetYSize()
1059 * Header->GetSamplesPerPixel();
1060 long fragmentBegining; // for ftell, fseek
1062 bool jpg2000 = Header->IsJPEG2000();
1063 bool jpgLossless = Header->IsJPEGLossless();
1066 uint16_t ItemTagGr, ItemTagEl;
1069 // Position on begining of Jpeg Pixels
1071 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1072 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1073 if(Header->GetSwapCode())
1075 ItemTagGr = Header->SwapShort(ItemTagGr);
1076 ItemTagEl = Header->SwapShort(ItemTagEl);
1080 if( Header->GetSwapCode() )
1082 ln = Header->SwapLong( ln ); // Basic Offset Table Item length
1087 // What is it used for ?!?
1088 uint8_t* BasicOffsetTableItemValue = new uint8_t[ln+1];
1089 fread(BasicOffsetTableItemValue,ln,1,fp);
1090 //delete[] BasicOffsetTableItemValue;
1093 // first Fragment initialisation
1094 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1095 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1096 if( Header->GetSwapCode() )
1098 ItemTagGr = Header->SwapShort( ItemTagGr );
1099 ItemTagEl = Header->SwapShort( ItemTagEl );
1102 // parsing fragments until Sequence Delim. Tag found
1103 while ( ItemTagGr == 0xfffe && ItemTagEl != 0xe0dd )
1105 // --- for each Fragment
1107 if( Header->GetSwapCode() )
1109 ln = Header->SwapLong(ln); // Fragment Item length
1111 fragmentBegining = ftell( fp );
1115 // JPEG 2000 : call to ???
1116 res = gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
1117 // ------------------------------------- endif (JPEG2000)
1119 else if (jpgLossless)
1121 // JPEG LossLess : call to xmedcom Lossless JPEG
1122 // Reading Fragment pixels
1123 JPEGLosslessDecodeImage (fp, (uint16_t*)destination,
1124 Header->GetPixelSize() * 8 * Header->GetSamplesPerPixel(), ln);
1125 res = 1; // in order not to break the loop
1127 } // ------------------------------------- endif (JPEGLossless)
1130 // JPEG Lossy : call to IJG 6b
1131 if ( Header->GetBitsStored() == 8)
1133 // Reading Fragment pixels
1134 res = gdcm_read_JPEG_file (fp,destination);
1138 // Reading Fragment pixels
1139 res = gdcm_read_JPEG_file12 (fp,destination);
1141 // ------------------------------------- endif (JPEGLossy)
1149 // location in user's memory
1150 // for next fragment (if any)
1151 destination = (uint8_t*)destination + taille * nBytes;
1153 fseek(fp,fragmentBegining, SEEK_SET); // To be sure we start
1154 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
1156 ItemTagGr = ItemTagEl = 0;
1157 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1158 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1159 if( Header->GetSwapCode() )
1161 ItemTagGr = Header->SwapShort( ItemTagGr );
1162 ItemTagEl = Header->SwapShort( ItemTagEl );
1165 // endWhile parsing fragments until Sequence Delim. Tag found
1167 Header->CloseFile();
1170 //-----------------------------------------------------------------------------