1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2004/08/26 15:29:53 $
7 Version: $Revision: 1.121 $
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 writing a new DICOMV3 part10 compliant
30 * file (see SetFileName, SetDcmTag and Write)
31 * Opens (in read only and when possible) an existing file and checks
32 * for DICOM compliance. Returns NULL on failure.
33 * \note the in-memory representation of all available tags found in
34 * the DICOM header is post-poned to first header information access.
35 * This avoid a double parsing of public part of the header when
36 * one sets an a posteriori shadow dictionary (efficiency can be
37 * seen as a side effect).
38 * @param header file to be opened for reading datas
41 gdcmFile::gdcmFile(gdcmHeader *header)
45 PixelRead = -1; // no ImageData read yet.
47 if (Header->IsReadable())
49 SetPixelDataSizeFromHeader();
55 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
56 * file (see SetFileName, SetDcmTag and Write)
57 * Opens (in read only and when possible) an existing file and checks
58 * for DICOM compliance. Returns NULL on failure.
59 * \note the in-memory representation of all available tags found in
60 * the DICOM header is post-poned to first header information access.
61 * This avoid a double parsing of public part of the header when
62 * one sets an a posteriori shadow dictionary (efficiency can be
63 * seen as a side effect).
64 * @param filename file to be opened for parsing
66 gdcmFile::gdcmFile(std::string const & filename )
68 Header = new gdcmHeader( filename );
70 PixelRead = -1; // no ImageData read yet.
72 if ( Header->IsReadable() )
74 SetPixelDataSizeFromHeader();
80 * \brief canonical destructor
81 * \note If the gdcmHeader is created by the gdcmFile, it is destroyed
93 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
101 * \brief computes the length (in bytes) to ALLOCATE to receive the
102 * image(s) pixels (multiframes taken into account)
103 * \warning : it is NOT the group 7FE0 length
104 * (no interest for compressed images).
105 * @return length to allocate
107 void gdcmFile::SetPixelDataSizeFromHeader()
109 // see PS 3.3-2003 : C.7.6.3.2.1
119 // YBR_FULL_422 (no LUT, no Palette)
125 // ex : gdcm-US-ALOKA-16.dcm
126 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
127 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
128 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
130 // ex : OT-PAL-8-face.dcm
131 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
132 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
133 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
135 // Number of "Bits Allocated"
137 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
139 if ( str_nb == GDCM_UNFOUND )
145 nb = atoi( str_nb.c_str() );
151 ImageDataSize = ImageDataSizeRaw = Header->GetXSize() * Header->GetYSize()
152 * Header->GetZSize() * (nb/8) * Header->GetSamplesPerPixel();
153 std::string str_PhotometricInterpretation =
154 Header->GetEntryByNumber(0x0028,0x0004);
156 // if ( str_PhotometricInterpretation == "PALETTE COLOR " )
157 // pb when undealt Segmented Palette Color
159 if ( Header->HasLUT() )
167 * \brief Returns the size (in bytes) of required memory to hold
168 * the pixel data represented in this file.
169 * @return The size of pixel data in bytes.
171 size_t gdcmFile::GetImageDataSize()
173 return ImageDataSize;
178 * \brief Returns the size (in bytes) of required memory to hold
179 * the pixel data represented in this file, when user DOESN'T want
180 * to get RGB pixels image when it's stored as a PALETTE COLOR image
181 * -the (vtk) user is supposed to know how deal with LUTs-
182 * \warning to be used with GetImagePixelsRaw()
183 * @return The size of pixel data in bytes.
185 size_t gdcmFile::GetImageDataSizeRaw()
187 return ImageDataSizeRaw;
192 * \brief Allocates necessary memory, copies the pixel data
193 * (image[s]/volume[s]) to newly allocated zone.
194 * Transforms YBR pixels into RGB pixels if any
195 * Transforms 3 planes R, G, B into a single RGB Plane
196 * Transforms single Grey plane + 3 Palettes into a RGB Plane
197 * @return Pointer to newly allocated pixel data.
198 * NULL if alloc fails
200 void *gdcmFile::GetImageData()
203 // I need to deallocate PixelData before doing any allocation:
204 PixelData = new uint8_t[ImageDataSize];
207 GetImageDataIntoVector(PixelData, ImageDataSize);
208 GetHeader()->SetEntryVoidAreaByNumber( PixelData,
209 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
211 PixelRead = 0; // no PixelRaw
218 * \brief Copies at most MaxSize bytes of pixel data to caller's
220 * \warning This function was designed to avoid people that want to build
221 * a volume from an image stack to need first to get the image pixels
222 * and then move them to the volume area.
223 * It's absolutely useless for any VTK user since vtk chooses
224 * to invert the lines of an image, that is the last line comes first
225 * (for some axis related reasons?). Hence he will have
226 * to load the image line by line, starting from the end.
227 * VTK users have to call GetImageData
229 * @param destination Address (in caller's memory space) at which the
230 * pixel data should be copied
231 * @param maxSize Maximum number of bytes to be copied. When MaxSize
232 * is not sufficient to hold the pixel data the copy is not
233 * executed (i.e. no partial copy).
234 * @return On success, the number of bytes actually copied. Zero on
235 * failure e.g. MaxSize is lower than necessary.
237 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t maxSize)
239 //size_t l = GetImageDataIntoVectorRaw (destination, maxSize);
240 GetImageDataIntoVectorRaw (destination, maxSize);
241 PixelRead = 0 ; // no PixelRaw
242 if ( !Header->HasLUT() )
244 return ImageDataSize;
247 // from Lut R + Lut G + Lut B
248 uint8_t *newDest = new uint8_t[ImageDataSize];
249 uint8_t *a = (uint8_t *)destination;
250 uint8_t *lutRGBA = Header->GetLUTRGBA();
255 //int l = ImageDataSizeRaw; //loss of precision
256 // move Gray pixels to temp area
257 memmove(newDest, destination, ImageDataSizeRaw);
258 for (size_t i=0; i<ImageDataSizeRaw; ++i)
268 // now, it's an RGB image
269 // Lets's write it in the Header
271 // CreateOrReplaceIfExist ?
273 std::string spp = "3"; // Samples Per Pixel
274 Header->SetEntryByNumber(spp,0x0028,0x0002);
275 std::string rgb = "RGB "; // Photometric Interpretation
276 Header->SetEntryByNumber(rgb,0x0028,0x0004);
277 std::string planConfig = "0"; // Planar Configuration
278 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
281 else //why is there a 'else' when an allocation failed ?
283 // need to make RGB Pixels (?)
284 // from grey Pixels (?!)
285 // and Gray Lut (!?!)
286 // or Segmented xxx Palette Color Lookup Table Data and so on
288 // Oops! I get one (gdcm-US-ALOKA-16.dcm)
289 // No idea how to manage such an image
290 // It seems that *no Dicom Viewer* has any idea :-(
291 // Segmented xxx Palette Color are *more* than 65535 long ?!?
293 std::string rgb = "MONOCHROME1 "; // Photometric Interpretation
294 Header->SetEntryByNumber(rgb,0x0028,0x0004);
297 /// \todo Drop Palette Color out of the Header?
298 return ImageDataSize;
303 * \brief Allocates necessary memory, copies the pixel data
304 * (image[s]/volume[s]) to newly allocated zone.
305 * Transforms YBR pixels into RGB pixels if any
306 * Transforms 3 planes R, G, B into a single RGB Plane
307 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
308 * @return Pointer to newly allocated pixel data.
309 * \ NULL if alloc fails
311 void * gdcmFile::GetImageDataRaw ()
313 size_t imgDataSize = ImageDataSize;
314 if ( Header->HasLUT() )
316 /// \todo Let gdcmHeader user a chance to get the right value
317 imgDataSize = ImageDataSizeRaw;
321 // I need to deallocate PixelData before doing any allocation:
322 PixelData = new uint8_t[imgDataSize];
325 GetImageDataIntoVectorRaw(PixelData, ImageDataSize);
326 GetHeader()->SetEntryVoidAreaByNumber(PixelData,
327 GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
329 PixelRead = 1; // PixelRaw
336 * \brief Copies at most MaxSize bytes of pixel data to caller's
338 * \warning This function was designed to avoid people that want to build
339 * a volume from an image stack to need first to get the image pixels
340 * and then move them to the volume area.
341 * It's absolutely useless for any VTK user since vtk chooses
342 * to invert the lines of an image, that is the last line comes first
343 * (for some axis related reasons?). Hence he will have
344 * to load the image line by line, starting from the end.
345 * VTK users hace to call GetImageData
346 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
347 * into a single RGB Pixels Plane
348 * the (VTK) user will manage the palettes
350 * @param destination Address (in caller's memory space) at which the
351 * pixel data should be copied
352 * @param maxSize Maximum number of bytes to be copied. When MaxSize
353 * is not sufficient to hold the pixel data the copy is not
354 * executed (i.e. no partial copy).
355 * @return On success, the number of bytes actually copied. Zero on
356 * failure e.g. MaxSize is lower than necessary.
358 size_t gdcmFile::GetImageDataIntoVectorRaw (void *destination, size_t maxSize)
360 int nb, nbu, highBit, sign;
361 PixelRead = 1 ; // PixelRaw
363 if ( ImageDataSize > maxSize )
365 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
366 "than caller's expected MaxSize");
370 ReadPixelData( destination );
372 // Number of Bits Allocated for storing a Pixel
373 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
374 if ( str_nb == GDCM_UNFOUND )
380 nb = atoi( str_nb.c_str() );
382 // From reading SetPixelDataSizeFromHeader, it seems 12 should be treated
383 // separately, correct ?
386 // Number of Bits actually used
387 std::string str_nbu = Header->GetEntryByNumber(0x0028,0x0101);
388 if ( str_nbu == GDCM_UNFOUND )
394 nbu = atoi( str_nbu.c_str() );
398 std::string str_highBit = Header->GetEntryByNumber(0x0028,0x0102);
399 if ( str_highBit == GDCM_UNFOUND )
405 highBit = atoi( str_highBit.c_str() );
411 std::string str_sign = Header->GetEntryByNumber(0x0028,0x0103);
412 if ( str_sign == GDCM_UNFOUND )
414 sign = 0; // default is unsigned
418 sign = atoi( str_sign.c_str() );
421 // re arange bytes inside the integer (processor endianity)
424 SwapZone(destination, Header->GetSwapCode(), ImageDataSize, nb);
427 // to avoid pb with some xmedcon breakers images
428 if ( nb == 16 && nbu < nb && sign == 0)
430 int l = (int)(ImageDataSize / (nb/8));
431 uint16_t *deb = (uint16_t *)destination;
432 for(int i = 0; i<l; i++)
442 // re arange bits inside the bytes
445 int l = (int)(ImageDataSize / (nb/8));
448 uint16_t mask = 0xffff;
449 mask = mask >> (nb-nbu);
450 uint16_t *deb = (uint16_t *)destination;
451 for(int i = 0; i<l; i++)
453 *deb = (*deb >> (nbu - highBit - 1)) & mask;
459 uint32_t mask = 0xffffffff;
460 mask = mask >> (nb - nbu);
461 uint32_t *deb = (uint32_t *)destination;
462 for(int i = 0; i<l; i++)
464 *deb = (*deb >> (nbu - highBit - 1)) & mask;
470 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: weird image");
474 // DO NOT remove this code commented out.
475 // Nobody knows what's expecting you ...
476 // Just to 'see' what was actually read on disk :-(
479 // f2 = fopen("SpuriousFile.RAW","wb");
480 // fwrite(destination,ImageDataSize,1,f2);
483 // Deal with the color
484 // -------------------
486 std::string str_PhotometricInterpretation =
487 Header->GetEntryByNumber(0x0028,0x0004);
489 if ( str_PhotometricInterpretation == "MONOCHROME1 "
490 || str_PhotometricInterpretation == "MONOCHROME2 " )
492 return ImageDataSize;
495 // Planar configuration = 0 : Pixels are already RGB
496 // Planar configuration = 1 : 3 planes : R, G, B
497 // Planar configuration = 2 : 1 gray Plane + 3 LUT
499 // Well ... supposed to be !
500 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
501 // PhotometricInterpretation=PALETTE COLOR
502 // and heuristic has to be found :-(
504 int planConf = Header->GetPlanarConfiguration(); // 0028,0006
506 // Whatever Planar Configuration is,
507 // "PALETTE COLOR " implies that we deal with the palette.
508 if ( str_PhotometricInterpretation == "PALETTE COLOR ")
516 // Pixels are already RGB
519 if (str_PhotometricInterpretation == "YBR_FULL")
521 // Warning : YBR_FULL_422 acts as RGB
522 // : we need to make RGB Pixels from Planes Y,cB,cR
524 // to see the tricks about YBR_FULL, YBR_FULL_422,
525 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
526 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
527 // and be *very* affraid
529 int l = Header->GetXSize() * Header->GetYSize();
530 int nbFrames = Header->GetZSize();
532 uint8_t* newDest = new uint8_t[ImageDataSize];
533 uint8_t* x = newDest;
534 uint8_t* a = (uint8_t*)destination;
539 /// \todo : Replace by the 'well known' integer computation
541 /// see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
542 /// for code optimisation
544 for (int i = 0; i < nbFrames; i++)
546 for (int j = 0; j < l; j++)
548 R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
549 G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
550 B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
552 if (R < 0.0) R = 0.0;
553 if (G < 0.0) G = 0.0;
554 if (B < 0.0) B = 0.0;
555 if (R > 255.0) R = 255.0;
556 if (G > 255.0) G = 255.0;
557 if (B > 255.0) B = 255.0;
565 memmove(destination, newDest, ImageDataSize);
570 // need to make RGB Pixels from R,G,B Planes
571 // (all the Frames at a time)
573 int l = Header->GetXSize() * Header->GetYSize() * Header->GetZSize();
575 uint8_t *newDest = new uint8_t[ImageDataSize];
576 uint8_t *x = newDest;
577 uint8_t *a = (uint8_t *)destination;
581 for (int j = 0; j < l; j++)
587 memmove(destination, newDest, ImageDataSize);
592 // Palettes were found
593 // Let the user deal with them !
594 return ImageDataSize;
596 // now, it's an RGB image
597 // Lets's write it in the Header
599 // CreateOrReplaceIfExist ?
601 std::string spp = "3"; // Samples Per Pixel
602 std::string rgb = "RGB "; // Photometric Interpretation
603 std::string planConfig = "0"; // Planar Configuration
605 Header->SetEntryByNumber(spp,0x0028,0x0002);
606 Header->SetEntryByNumber(rgb,0x0028,0x0004);
607 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
609 /// \todo Drop Palette Color out of the Header?
610 return ImageDataSize;
615 * \brief performs a shalow copy (not a deep copy) of the user given
617 * 'image' Pixels are presented as C-like 2D arrays : line per line.
618 * 'volume'Pixels are presented as C-like 3D arrays : lane per plane
619 * \warning user is kindly requested NOT TO 'free' the Pixel area
620 * @param inData user supplied pixel area
621 * @param expectedSize total image size, in Bytes
625 bool gdcmFile::SetImageData(void *inData, size_t expectedSize)
627 Header->SetImageDataSize( expectedSize );
629 ImageDataSize = expectedSize;
637 * \brief Writes on disk A SINGLE Dicom file
638 * NO test is performed on processor "Endiannity".
639 * It's up to the user to call his Reader properly
640 * @param fileName name of the file to be created
641 * (any already existing file is over written)
642 * @return false if write fails
645 bool gdcmFile::WriteRawData(std::string const & fileName)
647 FILE *fp1 = fopen(fileName.c_str(), "wb");
650 printf("Fail to open (write) file [%s] \n", fileName.c_str());
653 fwrite (PixelData, ImageDataSize, 1, fp1);
661 * \brief Writes on disk A SINGLE Dicom file,
662 * using the Implicit Value Representation convention
663 * NO test is performed on processor "Endiannity".
664 * @param fileName name of the file to be created
665 * (any already existing file is overwritten)
666 * @return false if write fails
669 bool gdcmFile::WriteDcmImplVR (std::string const & fileName)
671 return WriteBase(fileName, gdcmImplicitVR);
676 * \brief Writes on disk A SINGLE Dicom file,
677 * using the Explicit Value Representation convention
678 * NO test is performed on processor "Endiannity". * @param fileName name of the file to be created
679 * (any already existing file is overwritten)
680 * @return false if write fails
683 bool gdcmFile::WriteDcmExplVR (std::string const & fileName)
685 return WriteBase(fileName, gdcmExplicitVR);
690 * \brief Writes on disk A SINGLE Dicom file,
691 * using the ACR-NEMA convention
692 * NO test is performed on processor "Endiannity".
693 * (a l'attention des logiciels cliniques
694 * qui ne prennent en entrée QUE des images ACR ...
695 * \warning if a DICOM_V3 header is supplied,
696 * groups < 0x0008 and shadow groups are ignored
697 * \warning NO TEST is performed on processor "Endiannity".
698 * @param fileName name of the file to be created
699 * (any already existing file is overwritten)
700 * @return false if write fails
703 bool gdcmFile::WriteAcr (std::string const & fileName)
705 return WriteBase(fileName, gdcmACR);
708 //-----------------------------------------------------------------------------
712 * \brief NOT a end user inteded function
713 * (used by WriteDcmExplVR, WriteDcmImplVR, WriteAcr, etc)
714 * @param fileName name of the file to be created
715 * (any already existing file is overwritten)
716 * @param type file type (ExplicitVR, ImplicitVR, ...)
717 * @return false if write fails
719 bool gdcmFile::WriteBase (std::string const & fileName, FileType type)
721 if ( PixelRead == -1 && type != gdcmExplicitVR)
726 FILE *fp1 = fopen(fileName.c_str(), "wb");
729 printf("Failed to open (write) File [%s] \n", fileName.c_str());
733 if ( type == gdcmImplicitVR || type == gdcmExplicitVR )
735 // writing Dicom File Preamble
736 uint8_t* filePreamble = new uint8_t[128];
737 memset(filePreamble, 0, 128);
738 fwrite(filePreamble, 128, 1, fp1);
739 fwrite("DICM", 4, 1, fp1);
741 delete[] filePreamble;
744 // --------------------------------------------------------------
745 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
747 // if recognition code tells us we dealt with a LibIDO image
748 // we reproduce on disk the switch between lineNumber and columnNumber
749 // just before writting ...
751 /// \todo the best trick would be *change* the recognition code
752 /// but pb expected if user deals with, e.g. COMPLEX images
754 std::string rows, columns;
755 if ( Header->GetFileType() == gdcmACR_LIBIDO)
757 rows = Header->GetEntryByNumber(0x0028, 0x0010);
758 columns = Header->GetEntryByNumber(0x0028, 0x0011);
760 Header->SetEntryByNumber(columns, 0x0028, 0x0010);
761 Header->SetEntryByNumber(rows , 0x0028, 0x0011);
763 // ----------------- End of Special Patch ----------------
765 uint16_t grPixel = Header->GetGrPixel();
766 uint16_t numPixel = Header->GetNumPixel();;
768 gdcmDocEntry* PixelElement =
769 GetHeader()->GetDocEntryByNumber(grPixel, numPixel);
771 if ( PixelRead == 1 )
773 // we read pixel 'as is' (no tranformation LUT -> RGB)
774 PixelElement->SetLength( ImageDataSizeRaw );
776 else if ( PixelRead == 0 )
778 // we tranformed GrayLevel pixels + LUT into RGB Pixel
779 PixelElement->SetLength( ImageDataSize );
782 Header->Write(fp1, type);
784 // --------------------------------------------------------------
785 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
787 // ...and we restore the Header to be Dicom Compliant again
788 // just after writting
790 if ( Header->GetFileType() == gdcmACR_LIBIDO )
792 Header->SetEntryByNumber(rows , 0x0028, 0x0010);
793 Header->SetEntryByNumber(columns, 0x0028, 0x0011);
795 // ----------------- End of Special Patch ----------------
797 // fwrite(PixelData, ImageDataSize, 1, fp1); // should be useless, now
803 //-----------------------------------------------------------------------------
807 * \brief Swap the bytes, according to swap code.
808 * \warning not end user intended
809 * @param im area to deal with
810 * @param swap swap code
811 * @param lgr Area Length
812 * @param nb Pixels Bit number
814 void gdcmFile::SwapZone(void *im, int swap, int lgr, int nb)
820 uint16_t* im16 = (uint16_t*)im;
831 for(i=0; i < lgr/2; i++)
833 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
837 std::cout << "SWAP value (16 bits) not allowed :i" << swap <<
844 uint16_t fort, faible;
845 uint32_t* im32 = (uint32_t*)im;
852 for(i = 0; i < lgr/4; i++)
854 faible = im32[i] & 0x0000ffff; // 4321
855 fort = im32[i] >> 16;
856 fort = ( fort >> 8 ) | ( fort << 8 );
857 faible = ( faible >> 8 ) | ( faible << 8);
859 im32[i] = ( s32 << 16 ) | fort;
863 for(i = 0; i < lgr/4; i++)
865 faible = im32[i] & 0x0000ffff; // 2143
866 fort = im32[i] >> 16;
867 fort = ( fort >> 8 ) | ( fort << 8 );
868 faible = ( faible >> 8) | ( faible << 8);
870 im32[i] = ( s32 << 16 ) | faible;
874 for(i = 0; i < lgr/4; i++)
876 faible = im32[i] & 0x0000ffff; // 3412
877 fort = im32[i] >> 16;
879 im32[i] = ( s32 << 16 ) | fort;
883 std::cout << "SWAP value (32 bits) not allowed : " << swap <<
891 * \brief Read pixel data from disk (optionaly decompressing) into the
892 * caller specified memory location.
893 * @param destination where the pixel data should be stored.
896 bool gdcmFile::ReadPixelData(void *destination)
898 FILE *fp = Header->OpenFile();
904 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 )
910 // ---------------------- Compacted File (12 Bits Per Pixel)
911 // unpack 12 Bits pixels into 16 Bits pixels
912 // 2 pixels 12bit = [0xABCDEF]
913 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
915 if ( Header->GetBitsAllocated() == 12 )
917 int nbPixels = Header->GetXSize() * Header->GetYSize();
920 uint16_t* pdestination = (uint16_t*)destination;
921 for(int p = 0; p < nbPixels; p += 2 )
927 //Two steps is necessary to please VC++
928 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
930 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
933 // Troubles expected on Big-Endian processors ?
940 // ---------------------- Uncompressed File
941 if ( !Header->IsDicomV3() ||
942 Header->IsImplicitVRLittleEndianTransferSyntax() ||
943 Header->IsExplicitVRLittleEndianTransferSyntax() ||
944 Header->IsExplicitVRBigEndianTransferSyntax() ||
945 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() )
947 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
959 // ---------------------- Run Length Encoding
960 if ( Header->IsRLELossLessTransferSyntax() )
962 bool res = gdcm_read_RLE_file (fp,destination);
967 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
969 std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
970 if ( str_nb == GDCM_UNFOUND )
976 nb = atoi( str_nb.c_str() );
979 nb = 16; // ?? 12 should be ACR-NEMA only
984 int taille = Header->GetXSize() * Header->GetYSize()
985 * Header->GetSamplesPerPixel();
986 long fragmentBegining; // for ftell, fseek
988 bool jpg2000 = Header->IsJPEG2000();
989 bool jpgLossless = Header->IsJPEGLossless();
992 uint16_t ItemTagGr, ItemTagEl;
995 // Position on begining of Jpeg Pixels
997 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
998 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
999 if(Header->GetSwapCode())
1001 ItemTagGr = Header->SwapShort(ItemTagGr);
1002 ItemTagEl = Header->SwapShort(ItemTagEl);
1006 if( Header->GetSwapCode() )
1008 ln = Header->SwapLong( ln ); // Basic Offset Table Item length
1013 // What is it used for ?!?
1014 uint8_t* BasicOffsetTableItemValue = new uint8_t[ln+1];
1015 fread(BasicOffsetTableItemValue,ln,1,fp);
1016 //delete[] BasicOffsetTableItemValue;
1019 // first Fragment initialisation
1020 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1021 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1022 if( Header->GetSwapCode() )
1024 ItemTagGr = Header->SwapShort( ItemTagGr );
1025 ItemTagEl = Header->SwapShort( ItemTagEl );
1028 // parsing fragments until Sequence Delim. Tag found
1029 while ( ItemTagGr == 0xfffe && ItemTagEl != 0xe0dd )
1031 // --- for each Fragment
1033 if( Header->GetSwapCode() )
1035 ln = Header->SwapLong(ln); // Fragment Item length
1037 fragmentBegining = ftell( fp );
1041 // JPEG 2000 : call to ???
1042 res = gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
1043 // ------------------------------------- endif (JPEG2000)
1045 else if (jpgLossless)
1047 // JPEG LossLess : call to xmedcom Lossless JPEG
1048 // Reading Fragment pixels
1049 JPEGLosslessDecodeImage (fp, (uint16_t*)destination,
1050 Header->GetPixelSize() * 8 * Header->GetSamplesPerPixel(), ln);
1051 res = 1; // in order not to break the loop
1053 } // ------------------------------------- endif (JPEGLossless)
1056 // JPEG Lossy : call to IJG 6b
1057 if ( Header->GetBitsStored() == 8)
1059 // Reading Fragment pixels
1060 res = gdcm_read_JPEG_file (fp,destination);
1064 // Reading Fragment pixels
1065 res = gdcm_read_JPEG_file12 (fp,destination);
1067 // ------------------------------------- endif (JPEGLossy)
1075 // location in user's memory
1076 // for next fragment (if any)
1077 destination = (uint8_t*)destination + taille * nBytes;
1079 fseek(fp,fragmentBegining, SEEK_SET); // To be sure we start
1080 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
1082 ItemTagGr = ItemTagEl = 0;
1083 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
1084 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
1085 if( Header->GetSwapCode() )
1087 ItemTagGr = Header->SwapShort( ItemTagGr );
1088 ItemTagEl = Header->SwapShort( ItemTagEl );
1091 // endWhile parsing fragments until Sequence Delim. Tag found
1093 Header->CloseFile();
1096 //-----------------------------------------------------------------------------