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).
22 * @param header file to be opened for reading datas
26 gdcmFile::gdcmFile(gdcmHeader *header) {
29 PixelRead=-1; // no ImageData read yet.
31 if (Header->IsReadable())
32 SetPixelDataSizeFromHeader();
37 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
38 * file (see SetFileName, SetDcmTag and Write)
39 * Opens (in read only and when possible) an existing file and checks
40 * for DICOM compliance. Returns NULL on failure.
41 * \note the in-memory representation of all available tags found in
42 * the DICOM header is post-poned to first header information access.
43 * This avoid a double parsing of public part of the header when
44 * one sets an a posteriori shadow dictionary (efficiency can be
45 * seen as a side effect).
46 * @param filename file to be opened for parsing
48 gdcmFile::gdcmFile(std::string & filename) {
49 Header=new gdcmHeader(filename.c_str());
51 PixelRead=-1; // no ImageData read yet.
53 if (Header->IsReadable())
54 SetPixelDataSizeFromHeader();
59 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
60 * file (see SetFileName, SetDcmTag and Write)
61 * Opens (in read only and when possible) an existing file and checks
62 * for DICOM compliance. Returns NULL on failure.
63 * \note the in-memory representation of all available tags found in
64 * the DICOM header is post-poned to first header information access.
65 * This avoid a double parsing of public part of the header when
66 * one sets an a posteriori shadow dictionary (efficiency can be
67 * seen as a side effect).
68 * @param filename file to be opened for parsing
70 gdcmFile::gdcmFile(const char * filename) {
71 Header=new gdcmHeader(filename);
73 PixelRead=-1; // no ImageData read yet.
75 if (Header->IsReadable())
76 SetPixelDataSizeFromHeader();
81 * \brief canonical destructor
82 * \Note If the gdcmHeader is created by the gdcmFile, it is destroyed
85 gdcmFile::~gdcmFile(void) {
91 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
98 * \brief returns the gdcmHeader *Header
101 gdcmHeader *gdcmFile::GetHeader(void) {
107 * \brief computes the length (in bytes) to ALLOCATE to receive the
108 * image(s) pixels (multiframes taken into account)
109 * \warning : it is NOT the group 7FE0 length
110 * (no interest for compressed images).
111 * @return length to allocate
113 void gdcmFile::SetPixelDataSizeFromHeader(void) {
114 // see PS 3.3-2003 : C.7.6.3.2.1
124 // YBR_FULL_422 (no LUT, no Palette)
130 // ex : gdcm-US-ALOKA-16.dcm
131 // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
132 // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
133 // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
135 // ex : OT-PAL-8-face.dcm
136 // 0028|1201 [US] [Red Palette Color Lookup Table Data]
137 // 0028|1202 [US] [Green Palette Color Lookup Table Data]
138 // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
142 str_nb=Header->GetEntryByNumber(0x0028,0x0100);
143 if (str_nb == GDCM_UNFOUND ) {
146 nb = atoi(str_nb.c_str() );
147 if (nb == 12) nb =16;
149 lgrTotale = lgrTotaleRaw = Header->GetXSize() * Header->GetYSize()
150 * Header->GetZSize() * (nb/8)* Header->GetSamplesPerPixel();
151 std::string str_PhotometricInterpretation =
152 Header->GetEntryByNumber(0x0028,0x0004);
154 /*if ( str_PhotometricInterpretation == "PALETTE COLOR " )*/
155 // pb when undealt Segmented Palette Color
157 if (Header->HasLUT()) {
164 * \brief Returns the size (in bytes) of required memory to hold
165 * the pixel data represented in this file.
166 * @return The size of pixel data in bytes.
168 size_t gdcmFile::GetImageDataSize(void) {
174 * \brief Returns the size (in bytes) of required memory to hold
175 * the pixel data represented in this file, when user DOESN'T want
176 * to get RGB pixels image when it's stored as a PALETTE COLOR image
177 * -the (vtk) user is supposed to know how deal with LUTs-
178 * \warning to be used with GetImagePixelsRaw()
179 * @return The size of pixel data in bytes.
181 size_t gdcmFile::GetImageDataSizeRaw(void) {
182 return (lgrTotaleRaw);
187 * \brief Allocates necessary memory, copies the pixel data
188 * (image[s]/volume[s]) to newly allocated zone.
189 * Transforms YBR pixels into RGB pixels if any
190 * Transforms 3 planes R, G, B into a single RGB Plane
191 * Transforms single Grey plane + 3 Palettes into a RGB Plane
192 * @return Pointer to newly allocated pixel data.
193 * NULL if alloc fails
195 void * gdcmFile::GetImageData (void) {
196 PixelData = (void *) malloc(lgrTotale);
198 GetImageDataIntoVector(PixelData, lgrTotale);
200 PixelRead=0; // no PixelRaw
206 * \brief Copies at most MaxSize bytes of pixel data to caller's
208 * \warning This function was designed to avoid people that want to build
209 * a volume from an image stack to need first to get the image pixels
210 * and then move them to the volume area.
211 * It's absolutely useless for any VTK user since vtk chooses
212 * to invert the lines of an image, that is the last line comes first
213 * (for some axis related reasons?). Hence he will have
214 * to load the image line by line, starting from the end.
215 * VTK users have to call GetImageData
217 * @param destination Address (in caller's memory space) at which the
218 * pixel data should be copied
219 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
220 * is not sufficient to hold the pixel data the copy is not
221 * executed (i.e. no partial copy).
222 * @return On success, the number of bytes actually copied. Zero on
223 * failure e.g. MaxSize is lower than necessary.
225 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
226 size_t l = GetImageDataIntoVectorRaw (destination, MaxSize);
227 PixelRead=0 ; // no PixelRaw
228 if (!Header->HasLUT())
231 // from Lut R + Lut G + Lut B
232 unsigned char * newDest = (unsigned char *)malloc(lgrTotale);
233 unsigned char * a = (unsigned char *)destination;
234 unsigned char * lutRGBA = Header->GetLUTRGBA();
236 int l = lgrTotaleRaw;
237 memmove(newDest, destination, l);// move Gray pixels to temp area
239 for (int i=0;i<l; i++) { // Build RGB Pixels
247 // now, it's an RGB image
248 // Lets's write it in the Header
250 // CreateOrReplaceIfExist ?
252 std::string spp = "3"; // Samples Per Pixel
253 Header->SetEntryByNumber(spp,0x0028,0x0002);
254 std::string rgb= "RGB "; // Photometric Interpretation
255 Header->SetEntryByNumber(rgb,0x0028,0x0004);
256 std::string planConfig = "0"; // Planar Configuration
257 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
260 // need to make RGB Pixels (?)
261 // from grey Pixels (?!)
262 // and Gray Lut (!?!)
263 // or Segmented xxx Palette Color Lookup Table Data and so on
265 // Oops! I get one (gdcm-US-ALOKA-16.dcm)
266 // No idea how to manage such an image
267 // It seems that *no Dicom Viewer* has any idea :-(
268 // Segmented xxx Palette Color are *more* than 65535 long ?!?
270 std::string rgb= "MONOCHROME1 "; // Photometric Interpretation
271 Header->SetEntryByNumber(rgb,0x0028,0x0004);
273 // TODO : Drop Palette Color out of the Header?
279 * \brief Allocates necessary memory, copies the pixel data
280 * (image[s]/volume[s]) to newly allocated zone.
281 * Transforms YBR pixels into RGB pixels if any
282 * Transforms 3 planes R, G, B into a single RGB Plane
283 * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
284 * @return Pointer to newly allocated pixel data.
285 * \ NULL if alloc fails
287 void * gdcmFile::GetImageDataRaw (void) {
288 if (Header->HasLUT())
289 lgrTotale /= 3; // TODO Let gdcmHeadar user a chance
290 // to get the right value
291 // Create a member lgrTotaleRaw ???
292 PixelData = (void *) malloc(lgrTotale);
294 GetImageDataIntoVectorRaw(PixelData, lgrTotale);
295 PixelRead=1; // PixelRaw
301 * \brief Copies at most MaxSize bytes of pixel data to caller's
303 * \warning This function was designed to avoid people that want to build
304 * a volume from an image stack to need first to get the image pixels
305 * and then move them to the volume area.
306 * It's absolutely useless for any VTK user since vtk chooses
307 * to invert the lines of an image, that is the last line comes first
308 * (for some axis related reasons?). Hence he will have
309 * to load the image line by line, starting from the end.
310 * VTK users hace to call GetImageData
311 * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
312 * into a single RGB Pixels Plane
313 * the (VTK) user will manage the palettes
315 * @param destination Address (in caller's memory space) at which the
316 * pixel data should be copied
317 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
318 * is not sufficient to hold the pixel data the copy is not
319 * executed (i.e. no partial copy).
320 * @return On success, the number of bytes actually copied. Zero on
321 * failure e.g. MaxSize is lower than necessary.
323 size_t gdcmFile::GetImageDataIntoVectorRaw (void* destination, size_t MaxSize) {
325 int nb, nbu, highBit, signe;
326 std::string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
327 PixelRead=1 ; // PixelRaw
329 if ( lgrTotale > MaxSize ) {
330 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
331 "than caller's expected MaxSize");
335 (void)ReadPixelData(destination);
337 // Number of Bits Allocated for storing a Pixel
338 str_nb = Header->GetEntryByNumber(0x0028,0x0100);
339 if (str_nb == GDCM_UNFOUND ) {
342 nb = atoi(str_nb.c_str() );
344 // Number of Bits actually used
345 str_nbu=Header->GetEntryByNumber(0x0028,0x0101);
346 if (str_nbu == GDCM_UNFOUND ) {
349 nbu = atoi(str_nbu.c_str() );
352 str_highBit=Header->GetEntryByNumber(0x0028,0x0102);
353 if (str_highBit == GDCM_UNFOUND ) {
356 highBit = atoi(str_highBit.c_str() );
361 str_signe=Header->GetEntryByNumber(0x0028,0x0103);
362 if (str_signe == GDCM_UNFOUND ) {
363 signe = 0; // default is unsigned
365 signe = atoi(str_signe.c_str() );
368 // re arange bytes inside the integer (processor endianity)
370 SwapZone(destination, Header->GetSwapCode(), lgrTotale, nb);
372 // to avoid pb with some xmedcon breakers images
373 if (nb==16 && nbu<nb && signe==0) {
374 int l = (int)lgrTotale / (nb/8);
375 guint16 *deb = (guint16 *)destination;
376 for(int i = 0; i<l; i++) {
382 // re arange bits inside the bytes
384 int l = (int)lgrTotale / (nb/8);
386 guint16 mask = 0xffff;
387 mask = mask >> (nb-nbu);
388 guint16 *deb = (guint16 *)destination;
389 for(int i = 0; i<l; i++) {
390 *deb = (*deb >> (nbu-highBit-1)) & mask;
393 } else if (nb == 32 ) {
394 guint32 mask = 0xffffffff;
395 mask = mask >> (nb-nbu);
396 guint32 *deb = (guint32 *)destination;
397 for(int i = 0; i<l; i++) {
398 *deb = (*deb >> (nbu-highBit-1)) & mask;
402 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
406 // DO NOT remove this commented out code .
407 // Nobody knows what's expecting you ...
408 // Just to 'see' what was actually read on disk :-(
411 // f2 = fopen("SpuriousFile.RAW","wb");
412 // fwrite(destination,lgrTotale,1,f2);
415 // Deal with the color
416 // -------------------
418 std::string str_PhotometricInterpretation =
419 Header->GetEntryByNumber(0x0028,0x0004);
421 if ( (str_PhotometricInterpretation == "MONOCHROME1 ")
422 || (str_PhotometricInterpretation == "MONOCHROME2 ") ) {
426 // Planar configuration = 0 : Pixels are already RGB
427 // Planar configuration = 1 : 3 planes : R, G, B
428 // Planar configuration = 2 : 1 gray Plane + 3 LUT
430 // Well ... supposed to be !
431 // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
432 // PhotometricInterpretation=PALETTE COLOR
433 // and heuristic has to be found :-(
435 int planConf=Header->GetPlanarConfiguration(); // 0028,0006
437 // Whatever Planar Configuration is,
438 // "PALETTE COLOR " implies that we deal with the palette.
439 if (str_PhotometricInterpretation == "PALETTE COLOR ")
444 // Pixels are already RGB
450 if (str_PhotometricInterpretation == "YBR_FULL") {
452 // Warning : YBR_FULL_422 acts as RGB
453 // : we need to make RGB Pixels from Planes Y,cB,cR
455 // to see the tricks about YBR_FULL, YBR_FULL_422,
456 // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
457 // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
458 // and be *very* affraid
460 int l = Header->GetXSize()*Header->GetYSize();
461 int nbFrames = Header->GetZSize();
463 unsigned char * newDest = (unsigned char*) malloc(lgrTotale);
464 unsigned char *x = newDest;
465 unsigned char * a = (unsigned char *)destination;
466 unsigned char * b = a + l;
467 unsigned char * c = b + l;
470 // TODO : Replace by the 'well known'
471 // integer computation counterpart
472 // see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
473 // for code optimisation
475 for (int i=0;i<nbFrames;i++) {
476 for (int j=0;j<l; j++) {
477 R= 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
478 G= 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
479 B= 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
484 if (R>255.0) R=255.0;
485 if (G>255.0) G=255.0;
486 if (B>255.0) B=255.0;
488 *(x++) = (unsigned char)R;
489 *(x++) = (unsigned char)G;
490 *(x++) = (unsigned char)B;
494 memmove(destination,newDest,lgrTotale);
499 // need to make RGB Pixels from R,G,B Planes
500 // (all the Frames at a time)
502 int l = Header->GetXSize()*Header->GetYSize()*Header->GetZSize();
504 char * newDest = (char*) malloc(lgrTotale);
506 char * a = (char *)destination;
510 for (int j=0;j<l; j++) {
515 memmove(destination,newDest,lgrTotale);
521 // Palettes were found
522 // Let the user deal with them !
525 // now, it's an RGB image
526 // Lets's write it in the Header
528 // CreateOrReplaceIfExist ?
530 std::string spp = "3"; // Samples Per Pixel
531 Header->SetEntryByNumber(spp,0x0028,0x0002);
532 std::string rgb="RGB "; // Photometric Interpretation
533 Header->SetEntryByNumber(rgb,0x0028,0x0004);
535 std::string planConfig = "0"; // Planar Configuration
536 Header->SetEntryByNumber(planConfig,0x0028,0x0006);
538 // TODO : Drop Palette Color out of the Header?
544 * \brief performs a shadow copy (not a deep copy) of the user given
546 * 'image' Pixels are presented as C-like 2D arrays : line per line.
547 * 'volume'Pixels are presented as C-like 3D arrays : lane per plane
548 * \warning user is kindly requested NOT TO 'free' the Pixel area
549 * @param inData user supplied pixel area
550 * @param ExpectedSize total image size, in Bytes
554 bool gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
555 Header->SetImageDataSize(ExpectedSize);
557 lgrTotale = ExpectedSize;
563 * \brief Writes on disk A SINGLE Dicom file
564 * NO test is performed on processor "Endiannity".
565 * It's up to the user to call his Reader properly
566 * @param fileName name of the file to be created
567 * (any already existing file is over written)
568 * @return false if write fails
571 bool gdcmFile::WriteRawData (std::string fileName) {
573 fp1 = fopen(fileName.c_str(),"wb");
575 printf("Fail to open (write) file [%s] \n",fileName.c_str());
578 fwrite (PixelData,lgrTotale, 1, fp1);
585 * \brief Writes on disk A SINGLE Dicom file,
586 * using the Implicit Value Representation convention
587 * NO test is performed on processor "Endiannity".
588 * @param fileName name of the file to be created
589 * (any already existing file is overwritten)
590 * @return false if write fails
593 bool gdcmFile::WriteDcmImplVR (std::string fileName) {
594 return WriteBase(fileName, ImplicitVR);
599 * \brief Writes on disk A SINGLE Dicom file,
600 * using the Implicit Value Representation convention
601 * NO test is performed on processor "Endiannity". * @param fileName name of the file to be created
602 * (any already existing file is overwritten)
603 * @return false if write fails
606 bool gdcmFile::WriteDcmImplVR (const char* fileName) {
607 return WriteDcmImplVR (std::string (fileName));
612 * \brief Writes on disk A SINGLE Dicom file,
613 * using the Explicit Value Representation convention
614 * NO test is performed on processor "Endiannity". * @param fileName name of the file to be created
615 * (any already existing file is overwritten)
616 * @return false if write fails
619 bool gdcmFile::WriteDcmExplVR (std::string fileName) {
620 return WriteBase(fileName, ExplicitVR);
625 * \brief Writes on disk A SINGLE Dicom file,
626 * using the ACR-NEMA convention
627 * NO test is performed on processor "Endiannity".
628 * (a l'attention des logiciels cliniques
629 * qui ne prennent en entrée QUE des images ACR ...
630 * \warning if a DICOM_V3 header is supplied,
631 * groups < 0x0008 and shadow groups are ignored
632 * \warning NO TEST is performed on processor "Endiannity".
633 * @param fileName name of the file to be created
634 * (any already existing file is overwritten)
635 * @return false if write fails
638 bool gdcmFile::WriteAcr (std::string fileName) {
639 return WriteBase(fileName, ACR);
642 //-----------------------------------------------------------------------------
646 * \brief NOT a end user inteded function
647 * (used by WriteDcmExplVR, WriteDcmImplVR, WriteAcr, etc)
648 * @param fileName name of the file to be created
649 * (any already existing file is overwritten)
650 * @param type file type (ExplicitVR, ImplicitVR, DICOMDIR, ...)
651 * @return false if write fails
653 bool gdcmFile::WriteBase (std::string fileName, FileType type) {
657 if (PixelRead==-1 && type != DICOMDIR) {
658 /* std::cout << "U never Read the pixels; U cannot write the file"
663 fp1 = fopen(fileName.c_str(),"wb");
665 printf("Failed to open (write) File [%s] \n",fileName.c_str());
669 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
671 // writing Dicom File Preamble
672 filePreamble=(char*)calloc(128,1);
673 fwrite(filePreamble,128,1,fp1);
674 fwrite("DICM",4,1,fp1);
678 // --------------------------------------------------------------
679 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
681 // if recognition code tells us we dealt with a LibIDO image
682 // we reproduce on disk the switch between lineNumber and columnNumber
683 // just before writting ...
685 // TODO : the best trick would be *change* the recognition code
686 // but pb expected if user deals with, e.g. COMPLEX images
688 std::string rows, columns;
689 if ( Header->GetFileType() == ACR_LIBIDO){
690 rows = Header->GetEntryByNumber(0x0028, 0x0010);
691 columns = Header->GetEntryByNumber(0x0028, 0x0011);
692 Header->SetEntryByNumber(columns, 0x0028, 0x0010);
693 Header->SetEntryByNumber(rows , 0x0028, 0x0011);
695 // ----------------- End of Special Patch ----------------
697 // TODO : get the grPixel, numPixel values (for some ACR-NEMA images only)
699 guint16 grPixel =Header->GetGrPixel();
700 guint16 numPixel=Header->GetNumPixel();;
702 // Update Pixel Data Length
703 // the *last* of the (GrPixel, NumPixel), if many.
705 TagKey key = gdcmDictEntry::TranslateToKey(grPixel, numPixel);
706 TagHeaderEntryHT::iterator p2;
707 gdcmHeaderEntry * PixelElement;
709 IterHT it= Header->GetEntry().equal_range(key); // get a pair of iterators first-last synonym
711 if (Header->GetEntry().count(key) == 1) // only the first is significant
712 p2=it.first; // iterator on the first (unique) synonym
714 p2=it.second;// iterator on the last synonym
716 PixelElement=p2->second; // H Table target column (2-nd col)
717 // PixelElement->SetPrintLevel(2);
718 // PixelElement->Print();
721 PixelElement->SetLength(lgrTotaleRaw);
722 else if (PixelRead==0)
723 PixelElement->SetLength(lgrTotale);
725 //PixelElement->SetPrintLevel(2);
726 //PixelElement->Print();
728 Header->Write(fp1, type);
730 // --------------------------------------------------------------
731 // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
733 // ...and we restore the Header to be Dicom Compliant again
734 // just after writting
736 if (Header->GetFileType() == ACR_LIBIDO){
737 Header->SetEntryByNumber(rows , 0x0028, 0x0010);
738 Header->SetEntryByNumber(columns, 0x0028, 0x0011);
740 // ----------------- End of Special Patch ----------------
742 fwrite(PixelData, lgrTotale, 1, fp1);
747 //-----------------------------------------------------------------------------
751 * \brief Swap the bytes, according to swap code.
752 * \warning not end user intended
753 * @param im area to deal with
754 * @param swap swap code
755 * @param lgr Area Length
756 * @param nb Pixels Bit number
758 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
775 for(i=0;i<lgr/2;i++) {
776 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
777 | ((((unsigned short int*)im)[i])<<8);
782 printf("SWAP value (16 bits) not allowed : %d\n", swap);
792 for(i=0;i<lgr/4;i++) {
793 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
794 fort =((unsigned long int*)im)[i]>>16;
795 fort= (fort>>8) | (fort<<8);
796 faible=(faible>>8) | (faible<<8);
798 ((unsigned long int*)im)[i]=(s32<<16)|fort;
803 for(i=0;i<lgr/4;i++) {
804 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
805 fort=((unsigned long int*)im)[i]>>16;
806 fort= (fort>>8) | (fort<<8);
807 faible=(faible>>8) | (faible<<8);
809 ((unsigned long int*)im)[i]=(s32<<16)|faible;
814 for(i=0;i<lgr/4;i++) {
815 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
816 fort=((unsigned long int*)im)[i]>>16;
818 ((unsigned long int*)im)[i]=(s32<<16)|fort;
823 printf("SWAP value (32 bits) not allowed : %d\n", swap);
830 * \brief Read pixel data from disk (optionaly decompressing) into the
831 * caller specified memory location.
832 * @param destination where the pixel data should be stored.
835 bool gdcmFile::ReadPixelData(void* destination) {
839 if ( !(fp=Header->OpenFile()))
841 if ( fseek(fp, Header->GetPixelOffset(), SEEK_SET) == -1 ) {
845 // ---------------------- Compacted File (12 Bits Per Pixel)
846 /* unpack 12 Bits pixels into 16 Bits pixels */
847 /* 2 pixels 12bit = [0xABCDEF] */
848 /* 2 pixels 16bit = [0x0ABD] + [0x0FCE] */
850 if (Header->GetBitsAllocated()==12) {
851 int nbPixels = Header->GetXSize() * Header->GetYSize();
852 unsigned char b0, b1, b2;
854 unsigned short int* pdestination = (unsigned short int*)destination;
855 for(int p=0;p<nbPixels;p+=2) {
859 //Two steps is necessary to please VC++
860 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
861 /* A */ /* B */ /* D */
862 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
863 /* F */ /* C */ /* E */
865 // Troubles expected on Big-Endian processors ?
872 // ---------------------- Uncompressed File
873 if ( !Header->IsDicomV3() ||
874 Header->IsImplicitVRLittleEndianTransferSyntax() ||
875 Header->IsExplicitVRLittleEndianTransferSyntax() ||
876 Header->IsExplicitVRBigEndianTransferSyntax() ||
877 Header->IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
879 size_t ItemRead = fread(destination, Header->GetPixelAreaLength(), 1, fp);
880 if ( ItemRead != 1 ) {
889 // ---------------------- Run Length Encoding
890 if (Header->IsRLELossLessTransferSyntax()) {
891 bool res = (bool)gdcm_read_RLE_file (fp,destination);
896 // --------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
898 std::string str_nb=Header->GetEntryByNumber(0x0028,0x0100);
899 if (str_nb == GDCM_UNFOUND ) {
902 nb = atoi(str_nb.c_str() );
903 if (nb == 12) nb =16; // ?? 12 should be ACR-NEMA only ?
908 int taille = Header->GetXSize() * Header->GetYSize()
909 * Header->GetSamplesPerPixel();
910 long fragmentBegining; // for ftell, fseek
912 bool jpg2000 = Header->IsJPEG2000();
913 bool jpgLossless = Header->IsJPEGLossless();
916 guint16 ItemTagGr,ItemTagEl;
919 // Position on begining of Jpeg Pixels
921 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
922 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
923 if(Header->GetSwapCode()) {
924 ItemTagGr=Header->SwapShort(ItemTagGr);
925 ItemTagEl=Header->SwapShort(ItemTagEl);
928 if(Header->GetSwapCode())
929 ln=Header->SwapLong(ln); // Basic Offset Table Item length
932 // What is it used for ?!?
933 char *BasicOffsetTableItemValue = (char *)malloc(ln+1);
934 fread(BasicOffsetTableItemValue,ln,1,fp);
937 // first Fragment initialisation
938 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
939 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
940 if(Header->GetSwapCode()) {
941 ItemTagGr=Header->SwapShort(ItemTagGr);
942 ItemTagEl=Header->SwapShort(ItemTagEl);
945 // parsing fragments until Sequence Delim. Tag found
946 while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
947 // --- for each Fragment
950 if(Header->GetSwapCode())
951 ln=Header->SwapLong(ln); // Fragment Item length
953 fragmentBegining=ftell(fp);
955 if (jpg2000) { // JPEG 2000 : call to ???
957 res = (bool)gdcm_read_JPEG2000_file (fp,destination); // Not Yet written
959 } // ------------------------------------- endif (JPEG2000)
961 else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
963 JPEGLosslessDecodeImage (fp, // Reading Fragment pixels
964 (unsigned short *)destination,
965 Header->GetPixelSize()*8* Header->GetSamplesPerPixel(),
967 res=1; // in order not to break the loop
969 } // ------------------------------------- endif (JPEGLossless)
971 else { // JPEG Lossy : call to IJG 6b
973 if (Header->GetBitsStored() == 8) {
974 res = (bool)gdcm_read_JPEG_file (fp,destination); // Reading Fragment pixels
976 res = (bool)gdcm_read_JPEG_file12 (fp,destination);// Reading Fragment pixels
978 } // ------------------------------------- endif (JPEGLossy)
982 destination = (char *)destination + taille * nBytes; // location in user's memory
983 // for next fragment (if any)
985 fseek(fp,fragmentBegining,SEEK_SET); // To be sure we start
986 fseek(fp,ln,SEEK_CUR); // at the begining of next fragment
988 ItemTagGr = ItemTagEl =0;
989 fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
990 fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
991 if(Header->GetSwapCode()) {
992 ItemTagGr=Header->SwapShort(ItemTagGr);
993 ItemTagEl=Header->SwapShort(ItemTagEl);
996 } // endWhile parsing fragments until Sequence Delim. Tag found
1001 //-----------------------------------------------------------------------------