1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.115 2003/11/12 15:35:19 jpr Exp $
3 #include "gdcmHeader.h"
11 #include <netinet/in.h>
13 #include <cctype> // for isalpha
18 // Refer to gdcmHeader::CheckSwap()
19 #define HEADER_LENGTH_TO_READ 256
20 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
21 //#define _MaxSizeLoadElementValue_ 1024
22 #define _MaxSizeLoadElementValue_ 4096
27 void gdcmHeader::Initialise(void) {
28 dicom_vr = gdcmGlobal::GetVR();
29 dicom_ts = gdcmGlobal::GetTS();
30 Dicts = gdcmGlobal::GetDicts();
31 RefPubDict = Dicts->GetDefaultPubDict();
32 RefShaDict = (gdcmDict*)0;
39 * @param exception_on_error
41 gdcmHeader::gdcmHeader(const char *InFilename,
42 bool exception_on_error,
43 bool enable_sequences ) {
49 SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
50 filename = InFilename;
52 if ( !OpenFile(exception_on_error))
62 * @param exception_on_error
64 gdcmHeader::gdcmHeader(bool exception_on_error) {
65 SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
72 * @param exception_on_error
75 bool gdcmHeader::OpenFile(bool exception_on_error)
76 throw(gdcmFileError) {
77 fp=fopen(filename.c_str(),"rb");
78 if(exception_on_error) {
80 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
85 fread(&zero, (size_t)2, (size_t)1, fp);
87 //ACR -- or DICOM with no Preamble
88 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
91 fseek(fp, 126L, SEEK_CUR);
93 fread(dicm, (size_t)4, (size_t)1, fp);
94 if( memcmp(dicm, "DICM", 4) == 0 )
97 dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
100 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
106 * \ingroup gdcmHeader
108 * @return TRUE if the close was successfull
110 bool gdcmHeader::CloseFile(void) {
111 int closed = fclose(fp);
119 * \ingroup gdcmHeader
120 * \brief Canonical destructor.
122 gdcmHeader::~gdcmHeader (void) {
123 dicom_vr = (gdcmVR*)0;
124 Dicts = (gdcmDictSet*)0;
125 RefPubDict = (gdcmDict*)0;
126 RefShaDict = (gdcmDict*)0;
132 // ---> Warning : This fourth field is NOT part
133 // of the 'official' Dicom Dictionnary
134 // and should NOT be used.
135 // (Not defined for all the groups
136 // may be removed in a future release)
139 // META Meta Information
151 // NMI Nuclear Medicine
153 // BFS Basic Film Session
154 // BFB Basic Film Box
155 // BIB Basic Image Box
171 * \ingroup gdcmHeader
172 * \brief Discover what the swap code is (among little endian, big endian,
173 * bad little endian, bad big endian).
176 void gdcmHeader::CheckSwap()
178 // The only guaranted way of finding the swap code is to find a
179 // group tag since we know it's length has to be of four bytes i.e.
180 // 0x00000004. Finding the swap code in then straigthforward. Trouble
181 // occurs when we can't find such group...
183 guint32 x=4; // x : for ntohs
184 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
188 char deb[HEADER_LENGTH_TO_READ];
190 // First, compare HostByteOrder and NetworkByteOrder in order to
191 // determine if we shall need to swap bytes (i.e. the Endian type).
197 // The easiest case is the one of a DICOM header, since it possesses a
198 // file preamble where it suffice to look for the string "DICM".
199 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
202 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
203 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
204 // Next, determine the value representation (VR). Let's skip to the
205 // first element (0002, 0000) and check there if we find "UL"
206 // - or "OB" if the 1st one is (0002,0001) -,
207 // in which case we (almost) know it is explicit VR.
208 // WARNING: if it happens to be implicit VR then what we will read
209 // is the length of the group. If this ascii representation of this
210 // length happens to be "UL" then we shall believe it is explicit VR.
211 // FIXME: in order to fix the above warning, we could read the next
212 // element value (or a couple of elements values) in order to make
213 // sure we are not commiting a big mistake.
215 // * the 128 bytes of File Preamble (often padded with zeroes),
216 // * the 4 bytes of "DICM" string,
217 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
218 // i.e. a total of 136 bytes.
221 // Use gdcmHeader::dicom_vr to test all the possibilities
222 // instead of just checking for UL, OB and UI !?
223 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
224 (memcmp(entCur, "OB", (size_t)2) == 0) ||
225 (memcmp(entCur, "UI", (size_t)2) == 0) )
227 filetype = ExplicitVR;
228 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
229 "explicit Value Representation");
231 filetype = ImplicitVR;
232 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
233 "not an explicit Value Representation");
237 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
238 "HostByteOrder != NetworkByteOrder");
241 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
242 "HostByteOrder = NetworkByteOrder");
245 // Position the file position indicator at first tag (i.e.
246 // after the file preamble and the "DICM" string).
248 fseek (fp, 132L, SEEK_SET);
252 // Alas, this is not a DicomV3 file and whatever happens there is no file
253 // preamble. We can reset the file position indicator to where the data
254 // is (i.e. the beginning of the file).
255 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
258 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
259 // By clean we mean that the length of the first tag is written down.
260 // If this is the case and since the length of the first group HAS to be
261 // four (bytes), then determining the proper swap code is straightforward.
264 // We assume the array of char we are considering contains the binary
265 // representation of a 32 bits integer. Hence the following dirty
267 s = *((guint32 *)(entCur));
287 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
288 "ACR/NEMA unfound swap info (time to raise bets)");
291 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
292 // It is time for despaired wild guesses. So, let's assume this file
293 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
294 // not present. Then the only info we have is the net2host one.
304 * \ingroup gdcmHeader
307 void gdcmHeader::SwitchSwapToBigEndian(void) {
308 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
309 "Switching to BigEndian mode.");
327 * \ingroup gdcmHeader
328 * \brief Find the value representation of the current tag.
331 void gdcmHeader::FindVR( gdcmElValue *ElVal) {
332 if (filetype != ExplicitVR)
338 char msg[100]; // for sprintf. Sorry
340 long PositionOnEntry = ftell(fp);
341 // Warning: we believe this is explicit VR (Value Representation) because
342 // we used a heuristic that found "UL" in the first tag. Alas this
343 // doesn't guarantee that all the tags will be in explicit VR. In some
344 // cases (see e-film filtered files) one finds implicit VR tags mixed
345 // within an explicit VR file. Hence we make sure the present tag
346 // is in explicit VR and try to fix things if it happens not to be
348 bool RealExplicit = true;
350 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
352 vr = std::string(VR);
354 // Assume we are reading a falsely explicit VR file i.e. we reached
355 // a tag where we expect reading a VR but are in fact we read the
356 // first to bytes of the length. Then we will interogate (through find)
357 // the dicom_vr dictionary with oddities like "\004\0" which crashes
358 // both GCC and VC++ implementations of the STL map. Hence when the
359 // expected VR read happens to be non-ascii characters we consider
360 // we hit falsely explicit VR tag.
362 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
363 RealExplicit = false;
365 // CLEANME searching the dicom_vr at each occurence is expensive.
366 // PostPone this test in an optional integrity check at the end
367 // of parsing or only in debug mode.
368 if ( RealExplicit && !dicom_vr->Count(vr) )
371 if ( RealExplicit ) {
372 if ( ElVal->IsVrUnknown() ) {
373 // When not a dictionary entry, we can safely overwrite the vr.
377 if ( ElVal->GetVR() == vr ) {
378 // The vr we just read and the dictionary agree. Nothing to do.
381 // The vr present in the file and the dictionary disagree. We assume
382 // the file writer knew best and use the vr of the file. Since it would
383 // be unwise to overwrite the vr of a dictionary (since it would
384 // compromise it's next user), we need to clone the actual DictEntry
385 // and change the vr for the read one.
386 gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
391 ElVal->SetDictEntry(NewTag);
395 // We thought this was explicit VR, but we end up with an
396 // implicit VR tag. Let's backtrack.
398 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
399 ElVal->GetGroup(),ElVal->GetElement());
400 dbg.Verbose(1, "gdcmHeader::FindVR: ",msg);
402 fseek(fp, PositionOnEntry, SEEK_SET);
403 // When this element is known in the dictionary we shall use, e.g. for
404 // the semantics (see the usage of IsAnInteger), the vr proposed by the
405 // dictionary entry. Still we have to flag the element as implicit since
406 // we know now our assumption on expliciteness is not furfilled.
408 if ( ElVal->IsVrUnknown() )
409 ElVal->SetVR("Implicit");
410 ElVal->SetImplicitVr();
414 * \ingroup gdcmHeader
415 * \brief Determines if the Transfer Syntax was already encountered
416 * and if it corresponds to a ImplicitVRLittleEndian one.
418 * @return True when ImplicitVRLittleEndian found. False in all other cases.
420 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
421 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
424 LoadElementValueSafe(Element);
425 std::string Transfer = Element->GetValue();
426 if ( Transfer == "1.2.840.10008.1.2" )
432 * \ingroup gdcmHeader
433 * \brief Determines if the Transfer Syntax was already encountered
434 * and if it corresponds to a ExplicitVRLittleEndian one.
436 * @return True when ExplicitVRLittleEndian found. False in all other cases.
438 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
439 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
442 LoadElementValueSafe(Element);
443 std::string Transfer = Element->GetValue();
444 if ( Transfer == "1.2.840.10008.1.2.1" )
450 * \ingroup gdcmHeader
451 * \brief Determines if the Transfer Syntax was already encountered
452 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
454 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
456 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
457 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
460 LoadElementValueSafe(Element);
461 std::string Transfer = Element->GetValue();
462 if ( Transfer == "1.2.840.10008.1.2.1.99" )
468 * \ingroup gdcmHeader
469 * \brief Determines if the Transfer Syntax was already encountered
470 * and if it corresponds to a Explicit VR Big Endian one.
472 * @return True when big endian found. False in all other cases.
474 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
475 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
478 LoadElementValueSafe(Element);
479 std::string Transfer = Element->GetValue();
480 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
486 * \ingroup gdcmHeader
487 * \brief Determines if the Transfer Syntax was already encountered
488 * and if it corresponds to a JPEGBaseLineProcess1 one.
490 * @return True when JPEGBaseLineProcess1found. False in all other cases.
492 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
493 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
496 LoadElementValueSafe(Element);
497 std::string Transfer = Element->GetValue();
498 if ( Transfer == "1.2.840.10008.1.2.4.50" )
504 * \ingroup gdcmHeader
509 bool gdcmHeader::IsJPEGLossless(void) {
510 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
511 // faire qq chose d'intelligent a la place de ça
514 LoadElementValueSafe(Element);
515 const char * Transfert = Element->GetValue().c_str();
516 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
517 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
518 if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
525 * \ingroup gdcmHeader
526 * \brief Determines if the Transfer Syntax was already encountered
527 * and if it corresponds to a JPEGExtendedProcess2-4 one.
529 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
531 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
532 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
535 LoadElementValueSafe(Element);
536 std::string Transfer = Element->GetValue();
537 if ( Transfer == "1.2.840.10008.1.2.4.51" )
543 * \ingroup gdcmHeader
544 * \brief Determines if the Transfer Syntax was already encountered
545 * and if it corresponds to a JPEGExtendeProcess3-5 one.
547 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
549 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
550 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
553 LoadElementValueSafe(Element);
554 std::string Transfer = Element->GetValue();
555 if ( Transfer == "1.2.840.10008.1.2.4.52" )
561 * \ingroup gdcmHeader
562 * \brief Determines if the Transfer Syntax was already encountered
563 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
565 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
568 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
569 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
572 LoadElementValueSafe(Element);
573 std::string Transfer = Element->GetValue();
574 if ( Transfer == "1.2.840.10008.1.2.4.53" )
580 * \ingroup gdcmHeader
581 * \brief Determines if the Transfer Syntax was already encountered
582 * and if it corresponds to a RLE Lossless one.
584 * @return True when RLE Lossless found. False in all
587 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
588 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
591 LoadElementValueSafe(Element);
592 std::string Transfer = Element->GetValue();
593 if ( Transfer == "1.2.840.10008.1.2.5" )
599 * \ingroup gdcmHeader
600 * \brief Determines if the Transfer Syntax was already encountered
601 * and if it corresponds to a JPEG200 one.0
603 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
606 bool gdcmHeader::IsJPEG2000(void) {
607 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
610 LoadElementValueSafe(Element);
611 std::string Transfer = Element->GetValue();
612 if ( (Transfer == "1.2.840.10008.1.2.4.90")
613 || (Transfer == "1.2.840.10008.1.2.4.91") )
619 * \ingroup gdcmHeader
620 * \brief Predicate for dicom version 3 file.
621 * @return True when the file is a dicom version 3.
623 bool gdcmHeader::IsDicomV3(void) {
624 if ( (filetype == ExplicitVR)
625 || (filetype == ImplicitVR) )
631 * \ingroup gdcmHeader
632 * \brief When the length of an element value is obviously wrong (because
633 * the parser went Jabberwocky) one can hope improving things by
634 * applying this heuristic.
636 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
638 ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found
640 if ( FoundLength == 0xffffffff) {
644 // Sorry for the patch!
645 // XMedCom did the trick to read some nasty GE images ...
646 else if (FoundLength == 13) {
647 // The following 'if' will be removed when there is no more
648 // images on Creatis HDs with a 13 length for Manufacturer...
649 if ( (ElVal->GetGroup() != 0x0008) &&
650 ( (ElVal->GetElement() != 0x0070) || (ElVal->GetElement() != 0x0080) ) ) {
651 // end of remove area
653 ElVal->SetReadLength(10); // a bug is to be fixed
656 // to fix some garbage 'Leonardo' Siemens images
657 // May be commented out to avoid overhead
658 else if ( (ElVal->GetGroup() == 0x0009)
660 ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){
662 ElVal->SetReadLength(4); // a bug is to be fixed
666 // to try to 'go inside' SeQuences (with length), and not to skip them
667 else if ( ElVal->GetVR() == "SQ") {
668 if (enableSequences) // only if the user does want to !
672 // a SeQuence Element is beginning
673 // Let's forget it's length
674 // (we want to 'go inside')
675 else if(ElVal->GetGroup() == 0xfffe){
679 ElVal->SetUsableLength(FoundLength);
683 * \ingroup gdcmHeader
688 guint32 gdcmHeader::FindLengthOB(void) {
689 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
692 long PositionOnEntry = ftell(fp);
693 bool FoundSequenceDelimiter = false;
694 guint32 TotalLength = 0;
697 while ( ! FoundSequenceDelimiter) {
702 TotalLength += 4; // We even have to decount the group and element
704 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
705 char msg[100]; // for sprintf. Sorry
706 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
707 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
711 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
712 FoundSequenceDelimiter = true;
713 else if ( n != 0xe000 ){
714 char msg[100]; // for sprintf. Sorry
715 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
717 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
721 ItemLength = ReadInt32();
722 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
723 // the ItemLength with ReadInt32
724 SkipBytes(ItemLength);
726 fseek(fp, PositionOnEntry, SEEK_SET);
731 * \ingroup gdcmHeader
736 void gdcmHeader::FindLength (gdcmElValue * ElVal) {
737 guint16 element = ElVal->GetElement();
738 guint16 group = ElVal->GetGroup();
739 std::string vr = ElVal->GetVR();
741 if( (element == 0x0010) && (group == 0x7fe0) ) {
743 dbg.Verbose(2, "gdcmHeader::FindLength: ",
744 "we reached 7fe0 0010");
747 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
748 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
750 // The following reserved two bytes (see PS 3.5-2001, section
751 // 7.1.2 Data element structure with explicit vr p27) must be
752 // skipped before proceeding on reading the length on 4 bytes.
753 fseek(fp, 2L, SEEK_CUR);
755 guint32 length32 = ReadInt32();
757 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
758 ElVal->SetLength(FindLengthOB());
761 FixFoundLength(ElVal, length32);
765 // Length is encoded on 2 bytes.
766 length16 = ReadInt16();
768 // We can tell the current file is encoded in big endian (like
769 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
770 // and it's value is the one of the encoding of a big endian file.
771 // In order to deal with such big endian encoded files, we have
772 // (at least) two strategies:
773 // * when we load the "Transfer Syntax" tag with value of big endian
774 // encoding, we raise the proper flags. Then we wait for the end
775 // of the META group (0x0002) among which is "Transfer Syntax",
776 // before switching the swap code to big endian. We have to postpone
777 // the switching of the swap code since the META group is fully encoded
778 // in little endian, and big endian coding only starts at the next
779 // group. The corresponding code can be hard to analyse and adds
780 // many additional unnecessary tests for regular tags.
781 // * the second strategy consists in waiting for trouble, that shall
782 // appear when we find the first group with big endian encoding. This
783 // is easy to detect since the length of a "Group Length" tag (the
784 // ones with zero as element number) has to be of 4 (0x0004). When we
785 // encounter 1024 (0x0400) chances are the encoding changed and we
786 // found a group with big endian encoding.
787 // We shall use this second strategy. In order to make sure that we
788 // can interpret the presence of an apparently big endian encoded
789 // length of a "Group Length" without committing a big mistake, we
790 // add an additional check: we look in the already parsed elements
791 // for the presence of a "Transfer Syntax" whose value has to be "big
792 // endian encoding". When this is the case, chances are we have got our
793 // hands on a big endian encoded file: we switch the swap code to
794 // big endian and proceed...
795 if ( (element == 0x0000) && (length16 == 0x0400) ) {
796 if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
797 dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
802 SwitchSwapToBigEndian();
803 // Restore the unproperly loaded values i.e. the group, the element
804 // and the dictionary entry depending on them.
805 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
806 guint16 CorrectElem = SwapShort(ElVal->GetElement());
807 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
810 // This correct tag is not in the dictionary. Create a new one.
811 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
813 // FIXME this can create a memory leaks on the old entry that be
814 // left unreferenced.
815 ElVal->SetDictEntry(NewTag);
818 // Heuristic: well some files are really ill-formed.
819 if ( length16 == 0xffff) {
821 //dbg.Verbose(0, "gdcmHeader::FindLength",
822 // "Erroneous element length fixed.");
823 // Actually, length= 0xffff means that we deal with
824 // Unknown Sequence Length
827 FixFoundLength(ElVal, (guint32)length16);
831 // Either implicit VR or a non DICOM conformal (see not below) explicit
832 // VR that ommited the VR of (at least) this element. Farts happen.
833 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
834 // on Data elements "Implicit and Explicit VR Data Elements shall
835 // not coexist in a Data Set and Data Sets nested within it".]
836 // Length is on 4 bytes.
837 FixFoundLength(ElVal, ReadInt32());
842 * \ingroup gdcmHeader
843 * \brief Swaps back the bytes of 4-byte long integer accordingly to
845 * @return The properly swaped 32 bits integer.
847 guint32 gdcmHeader::SwapLong(guint32 a) {
852 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
853 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
857 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
861 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
864 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
871 * \ingroup gdcmHeader
872 * \brief Swaps the bytes so they agree with the processor order
873 * @return The properly swaped 16 bits integer.
875 guint16 gdcmHeader::SwapShort(guint16 a) {
876 if ( (sw==4321) || (sw==2143) )
877 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
882 * \ingroup gdcmHeader
887 void gdcmHeader::SkipBytes(guint32 NBytes) {
888 //FIXME don't dump the returned value
889 (void)fseek(fp, (long)NBytes, SEEK_CUR);
893 * \ingroup gdcmHeader
898 void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
899 SkipBytes(ElVal->GetLength());
903 * \ingroup gdcmHeader
908 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
911 if ((guint32)NewSize >= (guint32)0xffffffff) {
912 MaxSizeLoadElementValue = 0xffffffff;
915 MaxSizeLoadElementValue = NewSize;
919 * \ingroup gdcmHeader
920 * \brief Loads the element content if it's length is not bigger
921 * than the value specified with
922 * gdcmHeader::SetMaxSizeLoadElementValue()
924 void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
926 guint16 group = ElVal->GetGroup();
927 std::string vr= ElVal->GetVR();
928 guint32 length = ElVal->GetLength();
929 bool SkipLoad = false;
931 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
933 // FIXME Sequences not treated yet !
935 // Ne faudrait-il pas au contraire trouver immediatement
936 // une maniere 'propre' de traiter les sequences (vr = SQ)
937 // car commencer par les ignorer risque de conduire a qq chose
938 // qui pourrait ne pas etre generalisable
939 // Well, I'm expecting your code !!!
941 // the test was commented out to 'go inside' the SeQuences
942 // we don't any longer skip them !
947 // A SeQuence "contains" a set of Elements.
948 // (fffe e000) tells us an Element is beginning
949 // (fffe e00d) tells us an Element just ended
950 // (fffe e0dd) tells us the current SeQuence just ended
952 if( group == 0xfffe )
957 ElVal->SetValue("gdcm::Skipped");
961 // When the length is zero things are easy:
967 // The elements whose length is bigger than the specified upper bound
968 // are not loaded. Instead we leave a short notice of the offset of
969 // the element content and it's length.
970 if (length > MaxSizeLoadElementValue) {
971 std::ostringstream s;
972 s << "gdcm::NotLoaded.";
973 s << " Address:" << (long)ElVal->GetOffset();
974 s << " Length:" << ElVal->GetLength();
975 s << " x(" << std::hex << ElVal->GetLength() << ")";
976 ElVal->SetValue(s.str());
980 // When an integer is expected, read and convert the following two or
981 // four bytes properly i.e. as an integer as opposed to a string.
983 // Actually, elements with Value Multiplicity > 1
984 // contain a set of integers (not a single one)
985 // Any compacter code suggested (?)
987 if ( IsAnInteger(ElVal) ) {
989 std::ostringstream s;
991 if (vr == "US" || vr == "SS") {
993 NewInt = ReadInt16();
996 for (int i=1; i < nbInt; i++) {
998 NewInt = ReadInt16();
1003 } else if (vr == "UL" || vr == "SL") {
1005 NewInt = ReadInt32();
1008 for (int i=1; i < nbInt; i++) {
1010 NewInt = ReadInt32();
1015 ElVal->SetValue(s.str());
1019 // We need an additional byte for storing \0 that is not on disk
1020 char* NewValue = (char*)malloc(length+1);
1022 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
1025 NewValue[length]= 0;
1027 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
1028 if ( item_read != 1 ) {
1030 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
1031 ElVal->SetValue("gdcm::UnRead");
1034 ElVal->SetValue(NewValue);
1039 * \ingroup gdcmHeader
1040 * \brief Loads the element while preserving the current
1041 * underlying file position indicator as opposed to
1042 * to LoadElementValue that modifies it.
1043 * @param ElVal Element whose value shall be loaded.
1046 void gdcmHeader::LoadElementValueSafe(gdcmElValue * ElVal) {
1047 long PositionOnEntry = ftell(fp);
1048 LoadElementValue(ElVal);
1049 fseek(fp, PositionOnEntry, SEEK_SET);
1053 * \ingroup gdcmHeader
1054 * \brief Reads a supposed to be 16 Bits integer
1055 * \ (swaps it depending on processor endianity)
1057 * @return integer acts as a boolean
1059 guint16 gdcmHeader::ReadInt16(void) {
1062 item_read = fread (&g, (size_t)2,(size_t)1, fp);
1063 if ( item_read != 1 ) {
1064 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
1066 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
1068 dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
1078 * \ingroup gdcmHeader
1079 * \brief Reads a supposed to be 32 Bits integer
1080 * \ (swaps it depending on processor endianity)
1084 guint32 gdcmHeader::ReadInt32(void) {
1087 item_read = fread (&g, (size_t)4,(size_t)1, fp);
1088 if ( item_read != 1 ) {
1089 //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
1091 // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
1093 dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
1103 * \ingroup gdcmHeader
1108 gdcmElValue* gdcmHeader::GetElValueByNumber(guint16 Group, guint16 Elem) {
1110 gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
1112 dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
1113 "failed to Locate gdcmElValue");
1114 return (gdcmElValue*)0;
1120 * \ingroup gdcmHeader
1121 * \brief Build a new Element Value from all the low level arguments.
1122 * Check for existence of dictionary entry, and build
1123 * a default one when absent.
1124 * @param Group group of the underlying DictEntry
1125 * @param Elem element of the underlying DictEntry
1127 gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) {
1128 // Find out if the tag we encountered is in the dictionaries:
1129 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
1131 NewTag = new gdcmDictEntry(Group, Elem);
1133 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
1135 dbg.Verbose(1, "gdcmHeader::NewElValueByNumber",
1136 "failed to allocate gdcmElValue");
1137 return (gdcmElValue*)0;
1143 * \ingroup gdcmHeader
1148 * \return integer acts as a boolean
1150 int gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
1151 guint16 Group, guint16 Elem ) {
1152 // TODO : FIXME JPRx
1154 // on (je) cree une Elvalue ne contenant pas de valeur
1155 // on l'ajoute au ElValSet
1156 // on affecte une valeur a cette ElValue a l'interieur du ElValSet
1157 // --> devrait pouvoir etre fait + simplement ???
1158 if (CheckIfExistByNumber(Group, Elem) == 0) {
1159 gdcmElValue* a =NewElValueByNumber(Group, Elem);
1164 PubElValSet.SetElValueByNumber(Value, Group, Elem);
1170 * \ingroup gdcmHeader
1171 * \brief Modify (or Creates if not found) an element
1172 * @param Value new value
1175 * \return integer acts as a boolean
1178 int gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
1180 gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
1181 PubElValSet.Add(nvElValue);
1182 std::string v = Value;
1183 PubElValSet.SetElValueByNumber(v, Group, Elem);
1189 * \ingroup gdcmHeader
1190 * \brief Set a new value if the invoked element exists
1191 * Seems to be useless !!!
1195 * \return integer acts as a boolean
1197 int gdcmHeader::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) {
1199 //gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
1200 std::string v = Value;
1201 PubElValSet.SetElValueByNumber(v, Group, Elem);
1207 * \ingroup gdcmHeader
1208 * \brief Checks if a given ElValue (group,number)
1209 * \ exists in the Public ElValSet
1212 * @return integer acts as a boolean
1215 int gdcmHeader::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
1216 return (PubElValSet.CheckIfExistByNumber(Group, Elem));
1220 * \ingroup gdcmHeader
1221 * \brief Build a new Element Value from all the low level arguments.
1222 * Check for existence of dictionary entry, and build
1223 * a default one when absent.
1224 * @param Name Name of the underlying DictEntry
1226 gdcmElValue* gdcmHeader::NewElValueByName(std::string Name) {
1228 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
1230 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
1232 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
1234 dbg.Verbose(1, "gdcmHeader::ObtainElValueByName",
1235 "failed to allocate gdcmElValue");
1236 return (gdcmElValue*)0;
1242 * \ingroup gdcmHeader
1243 * \brief Read the next tag but WITHOUT loading it's value
1244 * @return On succes the newly created ElValue, NULL on failure.
1246 gdcmElValue * gdcmHeader::ReadNextElement(void) {
1249 gdcmElValue * NewElVal;
1255 // We reached the EOF (or an error occured) and header parsing
1256 // has to be considered as finished.
1257 return (gdcmElValue *)0;
1259 NewElVal = NewElValueByNumber(g, n);
1261 FindLength(NewElVal);
1265 return (gdcmElValue *)0;
1267 NewElVal->SetOffset(ftell(fp));
1268 //if ( (g==0x7fe0) && (n==0x0010) )
1273 * \ingroup gdcmHeader
1274 * \brief Apply some heuristics to predict wether the considered
1275 * element value contains/represents an integer or not.
1276 * @param ElVal The element value on which to apply the predicate.
1277 * @return The result of the heuristical predicate.
1279 bool gdcmHeader::IsAnInteger(gdcmElValue * ElVal) {
1280 guint16 element = ElVal->GetElement();
1281 std::string vr = ElVal->GetVR();
1282 guint32 length = ElVal->GetLength();
1284 // When we have some semantics on the element we just read, and if we
1285 // a priori know we are dealing with an integer, then we shall be
1286 // able to swap it's element value properly.
1287 if ( element == 0 ) { // This is the group length of the group
1291 dbg.Error("gdcmHeader::IsAnInteger",
1292 "Erroneous Group Length element length.");
1295 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1302 * \ingroup gdcmHeader
1303 * \brief Recover the offset (from the beginning of the file) of the pixels.
1305 size_t gdcmHeader::GetPixelOffset(void) {
1306 // If this file complies with the norm we should encounter the
1307 // "Image Location" tag (0x0028, 0x0200). This tag contains the
1308 // the group that contains the pixel data (hence the "Pixel Data"
1309 // is found by indirection through the "Image Location").
1310 // Inside the group pointed by "Image Location" the searched element
1311 // is conventionally the element 0x0010 (when the norm is respected).
1312 // When the "Image Location" is absent we default to group 0x7fe0.
1315 std::string ImageLocation = GetPubElValByName("Image Location");
1316 if ( ImageLocation == GDCM_UNFOUND ) {
1319 grPixel = (guint16) atoi( ImageLocation.c_str() );
1321 if (grPixel != 0x7fe0)
1322 // This is a kludge for old dirty Philips imager.
1327 gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel,
1330 return PixelElement->GetOffset();
1336 * \ingroup gdcmHeader
1337 * \brief Recover the pixel area length (in Bytes) .
1339 size_t gdcmHeader::GetPixelAreaLength(void) {
1340 // If this file complies with the norm we should encounter the
1341 // "Image Location" tag (0x0028, 0x0200). This tag contains the
1342 // the group that contains the pixel data (hence the "Pixel Data"
1343 // is found by indirection through the "Image Location").
1344 // Inside the group pointed by "Image Location" the searched element
1345 // is conventionally the element 0x0010 (when the norm is respected).
1346 // When the "Image Location" is absent we default to group 0x7fe0.
1349 std::string ImageLocation = GetPubElValByName("Image Location");
1350 if ( ImageLocation == GDCM_UNFOUND ) {
1353 grPixel = (guint16) atoi( ImageLocation.c_str() );
1355 if (grPixel != 0x7fe0)
1356 // This is a kludge for old dirty Philips imager.
1361 gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel,
1364 return PixelElement->GetLength();
1370 * \ingroup gdcmHeader
1371 * \brief Searches both the public and the shadow dictionary (when they
1372 * exist) for the presence of the DictEntry with given
1373 * group and element. The public dictionary has precedence on the
1375 * @param group group of the searched DictEntry
1376 * @param element element of the searched DictEntry
1377 * @return Corresponding DictEntry when it exists, NULL otherwise.
1379 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
1381 gdcmDictEntry * found = (gdcmDictEntry*)0;
1382 if (!RefPubDict && !RefShaDict) {
1383 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1384 "we SHOULD have a default dictionary");
1387 found = RefPubDict->GetTagByNumber(group, element);
1392 found = RefShaDict->GetTagByNumber(group, element);
1400 * \ingroup gdcmHeader
1401 * \brief Searches both the public and the shadow dictionary (when they
1402 * exist) for the presence of the DictEntry with given name.
1403 * The public dictionary has precedence on the shadow one.
1404 * @param Name name of the searched DictEntry
1405 * @return Corresponding DictEntry when it exists, NULL otherwise.
1407 gdcmDictEntry * gdcmHeader::GetDictEntryByName(std::string Name) {
1408 gdcmDictEntry * found = (gdcmDictEntry*)0;
1409 if (!RefPubDict && !RefShaDict) {
1410 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1411 "we SHOULD have a default dictionary");
1414 found = RefPubDict->GetTagByName(Name);
1419 found = RefShaDict->GetTagByName(Name);
1427 * \ingroup gdcmHeader
1428 * \brief Searches within the public dictionary for element value of
1430 * @param group Group of the researched tag.
1431 * @param element Element of the researched tag.
1432 * @return Corresponding element value when it exists, and the string
1433 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1435 std::string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
1436 return PubElValSet.GetElValueByNumber(group, element);
1440 * \ingroup gdcmHeader
1441 * \brief Searches within the public dictionary for element value
1442 * representation of a given tag.
1444 * Obtaining the VR (Value Representation) might be needed by caller
1445 * to convert the string typed content to caller's native type
1446 * (think of C++ vs Python). The VR is actually of a higher level
1447 * of semantics than just the native C++ type.
1448 * @param group Group of the researched tag.
1449 * @param element Element of the researched tag.
1450 * @return Corresponding element value representation when it exists,
1451 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1453 std::string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
1454 gdcmElValue* elem = PubElValSet.GetElementByNumber(group, element);
1456 return GDCM_UNFOUND;
1457 return elem->GetVR();
1461 * \ingroup gdcmHeader
1462 * \brief Searches within the public dictionary for element value of
1464 * @param TagName name of the searched element.
1465 * @return Corresponding element value when it exists, and the string
1466 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1468 std::string gdcmHeader::GetPubElValByName(std::string TagName) {
1469 return PubElValSet.GetElValueByName(TagName);
1473 * \ingroup gdcmHeader
1474 * \brief Searches within the elements parsed with the public dictionary for
1475 * the element value representation of a given tag.
1477 * Obtaining the VR (Value Representation) might be needed by caller
1478 * to convert the string typed content to caller's native type
1479 * (think of C++ vs Python). The VR is actually of a higher level
1480 * of semantics than just the native C++ type.
1481 * @param TagName name of the searched element.
1482 * @return Corresponding element value representation when it exists,
1483 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1485 std::string gdcmHeader::GetPubElValRepByName(std::string TagName) {
1486 gdcmElValue* elem = PubElValSet.GetElementByName(TagName);
1488 return GDCM_UNFOUND;
1489 return elem->GetVR();
1493 * \ingroup gdcmHeader
1494 * \brief Searches within elements parsed with the SHADOW dictionary
1495 * for the element value of a given tag.
1496 * @param group Group of the searched tag.
1497 * @param element Element of the searched tag.
1498 * @return Corresponding element value representation when it exists,
1499 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1501 std::string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
1502 return ShaElValSet.GetElValueByNumber(group, element);
1506 * \ingroup gdcmHeader
1507 * \brief Searches within the elements parsed with the SHADOW dictionary
1508 * for the element value representation of a given tag.
1510 * Obtaining the VR (Value Representation) might be needed by caller
1511 * to convert the string typed content to caller's native type
1512 * (think of C++ vs Python). The VR is actually of a higher level
1513 * of semantics than just the native C++ type.
1514 * @param group Group of the searched tag.
1515 * @param element Element of the searched tag.
1516 * @return Corresponding element value representation when it exists,
1517 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1519 std::string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
1520 gdcmElValue* elem = ShaElValSet.GetElementByNumber(group, element);
1522 return GDCM_UNFOUND;
1523 return elem->GetVR();
1527 * \ingroup gdcmHeader
1528 * \brief Searches within the elements parsed with the shadow dictionary
1529 * for an element value of given tag.
1530 * @param TagName name of the searched element.
1531 * @return Corresponding element value when it exists, and the string
1532 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1534 std::string gdcmHeader::GetShaElValByName(std::string TagName) {
1535 return ShaElValSet.GetElValueByName(TagName);
1539 * \ingroup gdcmHeader
1540 * \brief Searches within the elements parsed with the shadow dictionary for
1541 * the element value representation of a given tag.
1543 * Obtaining the VR (Value Representation) might be needed by caller
1544 * to convert the string typed content to caller's native type
1545 * (think of C++ vs Python). The VR is actually of a higher level
1546 * of semantics than just the native C++ type.
1547 * @param TagName name of the searched element.
1548 * @return Corresponding element value representation when it exists,
1549 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1551 std::string gdcmHeader::GetShaElValRepByName(std::string TagName) {
1552 gdcmElValue* elem = ShaElValSet.GetElementByName(TagName);
1554 return GDCM_UNFOUND;
1555 return elem->GetVR();
1559 * \ingroup gdcmHeader
1560 * \brief Searches within elements parsed with the public dictionary
1561 * and then within the elements parsed with the shadow dictionary
1562 * for the element value of a given tag.
1563 * @param group Group of the searched tag.
1564 * @param element Element of the searched tag.
1565 * @return Corresponding element value representation when it exists,
1566 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1568 std::string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
1569 std::string pub = GetPubElValByNumber(group, element);
1572 return GetShaElValByNumber(group, element);
1576 * \ingroup gdcmHeader
1577 * \brief Searches within elements parsed with the public dictionary
1578 * and then within the elements parsed with the shadow dictionary
1579 * for the element value representation of a given tag.
1581 * Obtaining the VR (Value Representation) might be needed by caller
1582 * to convert the string typed content to caller's native type
1583 * (think of C++ vs Python). The VR is actually of a higher level
1584 * of semantics than just the native C++ type.
1585 * @param group Group of the searched tag.
1586 * @param element Element of the searched tag.
1587 * @return Corresponding element value representation when it exists,
1588 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1590 std::string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
1591 std::string pub = GetPubElValRepByNumber(group, element);
1594 return GetShaElValRepByNumber(group, element);
1598 * \ingroup gdcmHeader
1599 * \brief Searches within elements parsed with the public dictionary
1600 * and then within the elements parsed with the shadow dictionary
1601 * for the element value of a given tag.
1602 * @param TagName name of the searched element.
1603 * @return Corresponding element value when it exists,
1604 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1606 std::string gdcmHeader::GetElValByName(std::string TagName) {
1607 std::string pub = GetPubElValByName(TagName);
1610 return GetShaElValByName(TagName);
1614 * \ingroup gdcmHeader
1615 * \brief Searches within elements parsed with the public dictionary
1616 * and then within the elements parsed with the shadow dictionary
1617 * for the element value representation of a given tag.
1619 * Obtaining the VR (Value Representation) might be needed by caller
1620 * to convert the string typed content to caller's native type
1621 * (think of C++ vs Python). The VR is actually of a higher level
1622 * of semantics than just the native C++ type.
1623 * @param TagName name of the searched element.
1624 * @return Corresponding element value representation when it exists,
1625 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1627 std::string gdcmHeader::GetElValRepByName(std::string TagName) {
1628 std::string pub = GetPubElValRepByName(TagName);
1631 return GetShaElValRepByName(TagName);
1635 * \ingroup gdcmHeader
1636 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1637 * through it's (group, element) and modifies it's content with
1639 * @param content new value to substitute with
1640 * @param group group of the ElVal to modify
1641 * @param element element of the ElVal to modify
1643 int gdcmHeader::SetPubElValByNumber(std::string content, guint16 group,
1646 //TODO : homogeneiser les noms : SetPubElValByNumber
1647 // qui appelle PubElValSet.SetElValueByNumber
1648 // pourquoi pas SetPubElValueByNumber ??
1651 return ( PubElValSet.SetElValueByNumber (content, group, element) );
1655 * \ingroup gdcmHeader
1656 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1657 * through tag name and modifies it's content with the given value.
1658 * @param content new value to substitute with
1659 * @param TagName name of the tag to be modified
1661 int gdcmHeader::SetPubElValByName(std::string content, std::string TagName) {
1662 return ( PubElValSet.SetElValueByName (content, TagName) );
1666 * \ingroup gdcmHeader
1667 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1668 * through it's (group, element) and modifies it's length with
1670 * \warning Use with extreme caution.
1671 * @param length new length to substitute with
1672 * @param group group of the ElVal to modify
1673 * @param element element of the ElVal to modify
1674 * @return 1 on success, 0 otherwise.
1677 int gdcmHeader::SetPubElValLengthByNumber(guint32 length, guint16 group,
1679 return ( PubElValSet.SetElValueLengthByNumber (length, group, element) );
1683 * \ingroup gdcmHeader
1684 * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
1685 * through it's (group, element) and modifies it's content with
1687 * @param content new value to substitute with
1688 * @param group group of the ElVal to modify
1689 * @param element element of the ElVal to modify
1690 * @return 1 on success, 0 otherwise.
1692 int gdcmHeader::SetShaElValByNumber(std::string content,
1693 guint16 group, guint16 element) {
1694 return ( ShaElValSet.SetElValueByNumber (content, group, element) );
1698 * \ingroup gdcmHeader
1699 * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
1700 * through tag name and modifies it's content with the given value.
1701 * @param content new value to substitute with
1702 * @param ShadowTagName name of the tag to be modified
1704 int gdcmHeader::SetShaElValByName(std::string content, std::string ShadowTagName) {
1705 return ( ShaElValSet.SetElValueByName (content, ShadowTagName) );
1709 * \ingroup gdcmHeader
1710 * \brief Parses the header of the file but WITHOUT loading element values.
1712 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1713 gdcmElValue * newElValue = (gdcmElValue *)0;
1717 while ( (newElValue = ReadNextElement()) ) {
1718 SkipElementValue(newElValue);
1719 PubElValSet.Add(newElValue);
1724 * \ingroup gdcmHeader
1725 * \brief This predicate, based on hopefully reasonable heuristics,
1726 * decides whether or not the current gdcmHeader was properly parsed
1727 * and contains the mandatory information for being considered as
1728 * a well formed and usable image.
1729 * @return true when gdcmHeader is the one of a reasonable Dicom file,
1732 bool gdcmHeader::IsReadable(void) {
1733 if ( GetElValByName("Image Dimensions") != GDCM_UNFOUND
1734 && atoi(GetElValByName("Image Dimensions").c_str()) > 4 ) {
1737 if ( GetElValByName("Bits Allocated") == GDCM_UNFOUND )
1739 if ( GetElValByName("Bits Stored") == GDCM_UNFOUND )
1741 if ( GetElValByName("High Bit") == GDCM_UNFOUND )
1743 if ( GetElValByName("Pixel Representation") == GDCM_UNFOUND )
1749 * \ingroup gdcmHeader
1750 * \brief Small utility function that creates a new manually crafted
1751 * (as opposed as read from the file) gdcmElValue with user
1752 * specified name and adds it to the public tag hash table.
1753 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
1754 * @param NewTagName The name to be given to this new tag.
1755 * @param VR The Value Representation to be given to this new tag.
1756 * @ return The newly hand crafted Element Value.
1758 gdcmElValue* gdcmHeader::NewManualElValToPubDict(std::string NewTagName,
1760 gdcmElValue* NewElVal = (gdcmElValue*)0;
1761 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
1762 guint32 FreeElem = 0;
1763 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
1765 FreeElem = PubElValSet.GenerateFreeTagKeyInGroup(StuffGroup);
1766 if (FreeElem == UINT32_MAX) {
1767 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
1768 "Group 0xffff in Public Dict is full");
1769 return (gdcmElValue*)0;
1771 NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
1772 VR, "GDCM", NewTagName);
1773 NewElVal = new gdcmElValue(NewEntry);
1774 PubElValSet.Add(NewElVal);
1779 * \ingroup gdcmHeader
1780 * \brief Loads the element values of all the elements present in the
1781 * public tag based hash table.
1783 void gdcmHeader::LoadElements(void) {
1786 // We don't use any longer the HashTable, since a lot a stuff is missing
1787 // when SeQuences were encountered
1789 //TagElValueHT ht = PubElValSet.GetTagHt();
1790 //for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1791 // LoadElementValue(tag->second);
1794 for (std::list<gdcmElValue*>::iterator i = GetListElem().begin();
1795 i != GetListElem().end();
1797 LoadElementValue(*i);
1802 // Load 'non string' values
1803 std::string PhotometricInterpretation = GetPubElValByNumber(0x0028,0x0004);
1804 if( PhotometricInterpretation == "PALETTE COLOR " ){
1805 LoadElementVoidArea(0x0028,0x1200); // gray LUT
1806 LoadElementVoidArea(0x0028,0x1201); // R LUT
1807 LoadElementVoidArea(0x0028,0x1202); // G LUT
1808 LoadElementVoidArea(0x0028,0x1203); // B LUT
1810 LoadElementVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1811 LoadElementVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1812 LoadElementVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1815 // --------------------------------------------------------------
1816 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1818 // if recognition code tells us we deal with a LibIDO image
1819 // we switch lineNumber and columnNumber
1821 std::string RecCode;
1822 RecCode = GetPubElValByNumber(0x0008, 0x0010);
1823 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1824 RecCode == "CANRME_AILIBOD1_1." ) {
1825 filetype = ACR_LIBIDO;
1826 std::string rows = GetPubElValByNumber(0x0028, 0x0010);
1827 std::string columns = GetPubElValByNumber(0x0028, 0x0011);
1828 SetPubElValByNumber(columns, 0x0028, 0x0010);
1829 SetPubElValByNumber(rows , 0x0028, 0x0011);
1831 // ----------------- End of Special Patch ----------------
1835 * \ingroup gdcmHeader
1839 void gdcmHeader::PrintPubElVal(std::ostream & os) {
1840 PubElValSet.Print(os);
1844 * \ingroup gdcmHeader
1848 void gdcmHeader::PrintPubDict(std::ostream & os) {
1849 RefPubDict->Print(os);
1853 * \ingroup gdcmHeader
1855 * @return integer, acts as a Boolean
1857 int gdcmHeader::Write(FILE * fp, FileType type) {
1859 // TODO : move the following lines (and a lot of others, to be written)
1860 // to a future function CheckAndCorrectHeader
1862 if (type == ImplicitVR) {
1863 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
1864 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
1866 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1867 // values with a VR of UI shall be padded with a single trailing null
1868 // Dans le cas suivant on doit pader manuellement avec un 0
1870 PubElValSet.SetElValueLengthByNumber(18, 0x0002, 0x0010);
1873 if (type == ExplicitVR) {
1874 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
1875 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
1877 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1878 // values with a VR of UI shall be padded with a single trailing null
1879 // Dans le cas suivant on doit pader manuellement avec un 0
1881 PubElValSet.SetElValueLengthByNumber(20, 0x0002, 0x0010);
1884 return PubElValSet.Write(fp, type);
1888 // ------------------------ 'non string' elements related functions
1892 * \ingroup gdcmHeader
1893 * \brief Loads (from disk) the element content
1894 * when a string is not suitable
1896 void * gdcmHeader::LoadElementVoidArea(guint16 Group, guint16 Elem) {
1897 gdcmElValue * Element= PubElValSet.GetElementByNumber(Group, Elem);
1900 size_t o =(size_t)Element->GetOffset();
1901 fseek(fp, o, SEEK_SET);
1902 int l=Element->GetLength();
1903 void * a = malloc(l);
1905 std::cout << "Big Broblem (LoadElementVoidArea, malloc) "
1906 << std::hex << Group << " " << Elem << std::endl;
1909 /* int res = */ PubElValSet.SetVoidAreaByNumber(a, Group, Elem);
1910 // TODO check the result
1911 size_t l2 = fread(a, 1, l ,fp);
1913 std::cout << "Big Broblem (LoadElementVoidArea, fread) "
1914 << std::hex << Group << " " << Elem << std::endl;
1922 * \ingroup gdcmHeader
1923 * \brief Gets (from Header) the offset of a 'non string' element value
1924 * \ (LoadElementValue has already be executed)
1927 * @return File Offset of the Element Value
1929 size_t gdcmHeader::GetPubElValOffsetByNumber(guint16 Group, guint16 Elem) {
1930 gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
1932 dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
1933 "failed to Locate gdcmElValue");
1936 return elValue->GetOffset();
1940 * \ingroup gdcmHeader
1941 * \brief Gets (from Header) a 'non string' element value
1942 * \ (LoadElementValue has already be executed)
1945 * @return Pointer to the 'non string' area
1948 void * gdcmHeader::GetPubElValVoidAreaByNumber(guint16 Group, guint16 Elem) {
1949 gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
1951 dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
1952 "failed to Locate gdcmElValue");
1955 return elValue->GetVoidArea();
1960 // =============================================================================
1961 // Heuristics based accessors
1962 //==============================================================================
1965 // TODO : move to an other file.
1969 * \ingroup gdcmHeader
1970 * \brief Retrieve the number of columns of image.
1971 * @return The encountered size when found, 0 by default.
1973 int gdcmHeader::GetXSize(void) {
1974 // We cannot check for "Columns" because the "Columns" tag is present
1975 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
1976 std::string StrSize = GetPubElValByNumber(0x0028,0x0011);
1977 if (StrSize == GDCM_UNFOUND)
1979 return atoi(StrSize.c_str());
1983 * \ingroup gdcmHeader
1984 * \brief Retrieve the number of lines of image.
1985 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
1986 * @return The encountered size when found, 1 by default.
1988 int gdcmHeader::GetYSize(void) {
1989 // We cannot check for "Rows" because the "Rows" tag is present
1990 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
1991 std::string StrSize = GetPubElValByNumber(0x0028,0x0010);
1992 if (StrSize != GDCM_UNFOUND)
1993 return atoi(StrSize.c_str());
1997 // The Rows (0028,0010) entry is optional for ACR/NEMA. It might
1998 // hence be a signal (1d image). So we default to 1:
2003 * \ingroup gdcmHeader
2004 * \brief Retrieve the number of planes of volume or the number
2005 * of frames of a multiframe.
2006 * \warning When present we consider the "Number of Frames" as the third
2007 * dimension. When absent we consider the third dimension as
2008 * being the "Planes" tag content.
2009 * @return The encountered size when found, 1 by default.
2011 int gdcmHeader::GetZSize(void) {
2012 // Both in DicomV3 and ACR/Nema the consider the "Number of Frames"
2013 // as the third dimension.
2014 std::string StrSize = GetPubElValByNumber(0x0028,0x0008);
2015 if (StrSize != GDCM_UNFOUND)
2016 return atoi(StrSize.c_str());
2018 // We then consider the "Planes" entry as the third dimension [we
2019 // cannot retrieve by name since "Planes tag is present both in
2020 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
2021 StrSize = GetPubElValByNumber(0x0028,0x0012);
2022 if (StrSize != GDCM_UNFOUND)
2023 return atoi(StrSize.c_str());
2028 * \ingroup gdcmHeader
2029 * \brief Retrieve the number of Bits Stored
2030 * (as opposite to number of Bits Allocated)
2032 * @return The encountered number of Bits Stored, 0 by default.
2034 int gdcmHeader::GetBitsStored(void) {
2035 std::string StrSize = GetPubElValByNumber(0x0028,0x0101);
2036 if (StrSize == GDCM_UNFOUND)
2038 return atoi(StrSize.c_str());
2042 * \ingroup gdcmHeader
2043 * \brief Retrieve the number of Bits Allocated
2044 * (8, 12 -compacted ACR-NEMA files, 16, ...)
2046 * @return The encountered number of Bits Allocated, 0 by default.
2048 int gdcmHeader::GetBitsAllocated(void) {
2049 std::string StrSize = GetPubElValByNumber(0x0028,0x0100);
2050 if (StrSize == GDCM_UNFOUND)
2052 return atoi(StrSize.c_str());
2056 * \ingroup gdcmHeader
2057 * \brief Retrieve the number of Samples Per Pixel
2058 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
2060 * @return The encountered number of Samples Per Pixel, 1 by default.
2062 int gdcmHeader::GetSamplesPerPixel(void) {
2063 std::string StrSize = GetPubElValByNumber(0x0028,0x0002);
2064 if (StrSize == GDCM_UNFOUND)
2065 return 1; // Well, it's supposed to be mandatory ...
2066 return atoi(StrSize.c_str());
2070 * \ingroup gdcmHeader
2071 * \brief Retrieve the Planar Configuration for RGB images
2072 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
2074 * @return The encountered Planar Configuration, 0 by default.
2076 int gdcmHeader::GetPlanarConfiguration(void) {
2077 std::string StrSize = GetPubElValByNumber(0x0028,0x0006);
2078 if (StrSize == GDCM_UNFOUND)
2080 return atoi(StrSize.c_str());
2084 * \ingroup gdcmHeader
2085 * \brief Return the size (in bytes) of a single pixel of data.
2086 * @return The size in bytes of a single pixel of data.
2089 int gdcmHeader::GetPixelSize(void) {
2090 std::string PixelType = GetPixelType();
2091 if (PixelType == "8U" || PixelType == "8S")
2093 if (PixelType == "16U" || PixelType == "16S")
2095 if (PixelType == "32U" || PixelType == "32S")
2097 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
2102 * \ingroup gdcmHeader
2103 * \brief Build the Pixel Type of the image.
2104 * Possible values are:
2105 * - 8U unsigned 8 bit,
2106 * - 8S signed 8 bit,
2107 * - 16U unsigned 16 bit,
2108 * - 16S signed 16 bit,
2109 * - 32U unsigned 32 bit,
2110 * - 32S signed 32 bit,
2111 * \warning 12 bit images appear as 16 bit.
2112 * \ 24 bit images appear as 8 bit
2115 std::string gdcmHeader::GetPixelType(void) {
2116 std::string BitsAlloc;
2117 BitsAlloc = GetElValByName("Bits Allocated");
2118 if (BitsAlloc == GDCM_UNFOUND) {
2119 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
2120 BitsAlloc = std::string("16");
2122 if (BitsAlloc == "12") // It will be unpacked
2123 BitsAlloc = std::string("16");
2124 else if (BitsAlloc == "24") // (in order no to be messed up
2125 BitsAlloc = std::string("8"); // by old RGB images)
2128 Signed = GetElValByName("Pixel Representation");
2129 if (Signed == GDCM_UNFOUND) {
2130 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
2131 BitsAlloc = std::string("0");
2134 Signed = std::string("U");
2137 std::cout << "GetPixelType : " << BitsAlloc + Signed << std::endl;
2138 Signed = std::string("S");
2140 return( BitsAlloc + Signed);
2144 * \ingroup gdcmHeader
2145 * \brief gets the info from 0002,0010 : Transfert Syntax
2147 * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
2149 std::string gdcmHeader::GetTransferSyntaxName(void) {
2150 std::string TransfertSyntax = GetPubElValByNumber(0x0002,0x0010);
2151 if (TransfertSyntax == GDCM_UNFOUND) {
2152 dbg.Verbose(0, "gdcmHeader::GetTransferSyntaxName: unfound Transfert Syntax (0002,0010)");
2153 return "Uncompressed ACR-NEMA";
2155 // we do it only when we need it
2156 gdcmTS * ts = gdcmGlobal::GetTS();
2157 std::string tsName=ts->GetValue(TransfertSyntax);
2158 //delete ts; // Seg Fault when deleted ?!
2162 // -------------------------------- Lookup Table related functions ------------
2165 * \ingroup gdcmHeader
2166 * \brief tells us if LUT are used
2167 * \warning Right now, Segmented xxx Palette Color Lookup Table Data
2168 * \ are NOT considered as LUT, since nobody knows
2169 *\ how to deal with them
2170 * @return int acts as a Boolean
2173 int gdcmHeader::HasLUT(void) {
2175 // Check the presence of the LUT Descriptors
2176 if (GetPubElValByNumber(0x0028,0x1101) == GDCM_UNFOUND)
2178 // LutDescriptorGreen
2179 if (GetPubElValByNumber(0x0028,0x1102) == GDCM_UNFOUND)
2181 // LutDescriptorBlue
2182 if (GetPubElValByNumber(0x0028,0x1103) == GDCM_UNFOUND)
2186 if (GetPubElValByNumber(0x0028,0x1201) == GDCM_UNFOUND)
2188 if (GetPubElValByNumber(0x0028,0x1202) == GDCM_UNFOUND)
2190 if (GetPubElValByNumber(0x0028,0x1203) == GDCM_UNFOUND)
2196 * \ingroup gdcmHeader
2197 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
2199 * @return Lookup Table nBit
2200 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
2203 int gdcmHeader::GetLUTNbits(void) {
2204 std::vector<std::string> tokens;
2208 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
2209 // Consistency already checked in GetLUTLength
2210 std::string LutDescription = GetPubElValByNumber(0x0028,0x1101);
2211 if (LutDescription == GDCM_UNFOUND)
2213 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
2214 Tokenize (LutDescription, tokens, "\\");
2215 //LutLength=atoi(tokens[0].c_str());
2216 //LutDepth=atoi(tokens[1].c_str());
2217 LutNbits=atoi(tokens[2].c_str());
2223 * \ingroup gdcmHeader
2224 * \brief builts Red/Green/Blue/Alpha LUT from Header
2225 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
2226 * \ and (0028,1101),(0028,1102),(0028,1102)
2227 * \ - xxx Palette Color Lookup Table Descriptor - are found
2228 * \ and (0028,1201),(0028,1202),(0028,1202)
2229 * \ - xxx Palette Color Lookup Table Data - are found
2230 * \warning does NOT deal with :
2231 * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
2232 * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
2233 * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
2234 * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data
2235 * \ no known Dicom reader deails with them :-(
2236 * @return Lookup Table RGBA
2239 void * gdcmHeader::GetLUTRGBA(void) {
2240 // Not so easy : see
2241 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
2242 // and OT-PAL-8-face.dcm
2244 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
2246 if (gdcmHeader::GetPubElValByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
2250 int lengthR, debR, nbitsR;
2251 int lengthG, debG, nbitsG;
2252 int lengthB, debB, nbitsB;
2254 // Get info from Lut Descriptors
2255 // (the 3 LUT descriptors may be different)
2257 std::string LutDescriptionR = GetPubElValByNumber(0x0028,0x1101);
2258 if (LutDescriptionR == GDCM_UNFOUND)
2260 std::string LutDescriptionG = GetPubElValByNumber(0x0028,0x1102);
2261 if (LutDescriptionG == GDCM_UNFOUND)
2263 std::string LutDescriptionB = GetPubElValByNumber(0x0028,0x1103);
2264 if (LutDescriptionB == GDCM_UNFOUND)
2267 std::vector<std::string> tokens;
2269 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
2270 Tokenize (LutDescriptionR, tokens, "\\");
2271 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
2272 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
2273 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
2276 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
2277 Tokenize (LutDescriptionG, tokens, "\\");
2278 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
2279 debG =atoi(tokens[1].c_str());
2280 nbitsG =atoi(tokens[2].c_str());
2283 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
2284 Tokenize (LutDescriptionB, tokens, "\\");
2285 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
2286 debB =atoi(tokens[1].c_str());
2287 nbitsB =atoi(tokens[2].c_str());
2290 // Load LUTs into memory, (as they were stored on disk)
2292 unsigned char *lutR =(unsigned char *)
2293 GetPubElValVoidAreaByNumber(0x0028,0x1201);
2294 unsigned char *lutG =(unsigned char *)
2295 GetPubElValVoidAreaByNumber(0x0028,0x1202);
2296 unsigned char *lutB =(unsigned char *)
2297 GetPubElValVoidAreaByNumber(0x0028,0x1203);
2299 if (!lutR || !lutG || !lutB ) {
2302 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
2304 unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha)
2308 memset(LUTRGBA, 0, 1024);
2311 std::string str_nb = GetPubElValByNumber(0x0028,0x0100);
2312 if (str_nb == GDCM_UNFOUND ) {
2315 nb = atoi(str_nb.c_str() );
2319 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
2320 mult=2; // high byte must be = low byte
2321 else // See PS 3.3-2003 C.11.1.1.2 p 619
2325 // if we get a black image, let's just remove the '+1'
2326 // from 'i*mult+1' and check again
2327 // if it works, we shall have to check the 3 Palettes
2328 // to see which byte is ==0 (first one, or second one)
2330 // We give up the checking to avoid some overhead
2335 for(i=0;i<lengthR;i++) {
2336 *a = lutR[i*mult+1];
2340 for(i=0;i<lengthG;i++) {
2341 *a = lutG[i*mult+1];
2345 for(i=0;i<lengthB;i++) {
2346 *a = lutB[i*mult+1];
2350 for(i=0;i<256;i++) {
2351 *a = 1; // Alpha component
2355 //How to free the now useless LUTs?
2357 //free(LutR); free(LutB); free(LutG);