xp_comm_proj/rd_dicom/Papyrus3/PapyFiles3.c
/********************************************************************************/
/*										*/
/*	ComputeUndefinedGroupLength3 : compute the size of a group of undefined	*/
/*	length, starting from the current file position.			*/
/*	return : the size of the group if OK					*/
/*		 else standard error message					*/
/*										*/
/********************************************************************************/
ComputeUndefinedGroupLength3 : compute the size of a group of undefined
length, starting from the current file position.
return : the size of the group if OK
else standard error message
ComputeUndefinedGroupLength3 : compute the size of a group of undefined length starting from the current file position . return : the size of the group if OK else standard error message
PapyULong
ComputeUndefinedGroupLength3 (PapyShort inFileNb, PapyLong inMaxSize)
{
  PapyULong	theGroupLength, theElemLength, theBufPos, theFileStartPos, i;
  PapyUShort	theGrNb, theCmpGrNb, theElemNb, theCmpElemNb;
  PapyShort	theErr;
  char		theVR [3];
  unsigned char	*theBuffP, theBuff [8];
  int		OK;
  strcpy (theVR, " ");
  if (inMaxSize == -1) inMaxSize = 1000000; 
  theGroupLength  = 0L;
  Papy3FTell (gPapyFile [inFileNb], (PapyLong *) &theFileStartPos);
  theBuffP = (unsigned char *) &theBuff [0];
  i = 4L;
  if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theBuffP)) < 0)
  {
    theErr = (PapyShort) Papy3FClose (&(gPapyFile [inFileNb]));
    RETURN (papReadFile)
  } 
  theBufPos 	= 0L;
  theGrNb       = Extract2Bytes (theBuffP, &theBufPos);
  theElemNb     = Extract2Bytes (theBuffP, &theBufPos);
  theCmpGrNb   	= theGrNb;
  theCmpElemNb 	= theElemNb;
  Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_SET, (PapyLong) theFileStartPos);
  OK = FALSE;
  while (!OK)
  {
    i = 8L;
    if ((theErr = (PapyShort) Papy3FRead (gPapyFile [inFileNb], &i, 1L, theBuffP)) < 0)
    {
      theErr = (PapyShort) Papy3FClose (&(gPapyFile [inFileNb]));
      return 0;
    } 
    theGroupLength += 8L;
    theBufPos = 0L;
    theGrNb       = Extract2Bytes (theBuffP, &theBufPos);
    theElemNb     = Extract2Bytes (theBuffP, &theBufPos);
    theVR [0] = 'A'; theVR [1] = 'A'; theVR [2] = '\0';
    if ((gArrTransfSyntax [inFileNb] == LITTLE_ENDIAN_IMPL && theGrNb != 0x0002) ||
        (theGrNb == 0xFFFE && theElemNb == 0xE000) ||
        (theGrNb == 0xFFFE && theElemNb == 0xE00D))
       theElemLength = Extract4Bytes (theBuffP, &theBufPos);
    else
    {
      theBuffP  += theBufPos;
      theVR [0]  = (char) *theBuffP;
      theVR [1]  = (char) *(theBuffP + 1);
      theVR [2]  = '\0';
      theBuffP   = (unsigned char *) &theBuff [0];
      theBufPos += 2;
      if (strcmp (theVR, "OB") == 0 ||
          strcmp (theVR, "OW") == 0 || 
          strcmp (theVR, "SQ") == 0 || 
          strcmp (theVR, "UN") == 0 || 
          strcmp (theVR, "UT") == 0)
      {
        if (theGrNb == 0x0004 && theElemNb == 0x1220)
        {
          i = 6L;
          if (Papy3FRead (gPapyFile [inFileNb], &i, 1L, theBuffP) < 0)
          {
            Papy3FClose (&(gPapyFile [inFileNb]));
            RETURN (papReadFile)
          } 
          theGroupLength -= 8L;
          theElemLength = 0L;
          OK = TRUE;
        } 
        else
        {
          i = 4L;
          if (Papy3FRead (gPapyFile [inFileNb], &i, 1L, theBuffP) < 0)
          {
            Papy3FClose (&(gPapyFile [inFileNb]));
            RETURN (papReadFile)
          } 
          theGroupLength += 4L;
          theBufPos       = 0L;
          theElemLength   = Extract4Bytes (theBuffP, &theBufPos);
        } 
      } 
      else
        theElemLength = (PapyULong) Extract2Bytes (theBuffP, &theBufPos); 
    } 
    if (theCmpGrNb == theGrNb && theCmpElemNb <= theElemNb && theGroupLength < (PapyULong) inMaxSize)
    {
      if (theElemLength == 0xFFFFFFFF)
      {
        theElemLength = 0L;
        if ((theErr = ComputeUndefinedSequenceLength3 (inFileNb, &theElemLength)) < 0)
          RETURN ((PapyULong) theErr);
      } 
      theGroupLength += theElemLength;
      if ((theErr = Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_CUR, (PapyLong) theElemLength)) < 0)
        RETURN ((PapyULong) theErr);
    } 
    else
    {
      OK = TRUE;
      theGroupLength -= 8L;
      if (strcmp (theVR, "OB") == 0 || strcmp (theVR, "OW") == 0 || strcmp (theVR, "SQ") == 0)
        theGroupLength -= 4L;
    } 
  } 
  Papy3FSeek (gPapyFile [inFileNb], (int) SEEK_SET, (PapyLong) theFileStartPos);
  return theGroupLength;
}
PapyULong ComputeUndefinedGroupLength3 ( PapyShort inFileNb , PapyLong inMaxSize ) { PapyULong theGroupLength , theElemLength , theBufPos , theFileStartPos , i ; PapyUShort theGrNb , theCmpGrNb , theElemNb , theCmpElemNb ; PapyShort theErr ; char theVR [ 3 ] ; unsigned char * theBuffP , theBuff [ 8 ] ; int OK ; strcpy ( theVR , " " ) ; if ( inMaxSize == -1 ) inMaxSize = 1000000 ; theGroupLength = 0L ; Papy3FTell ( gPapyFile [ inFileNb ] , ( PapyLong * ) & theFileStartPos ) ; theBuffP = ( unsigned char * ) & theBuff [ 0 ] ; i = 4L ; if ( ( theErr = ( PapyShort ) Papy3FRead ( gPapyFile [ inFileNb ] , & i , 1L , theBuffP ) ) < 0 ) { theErr = ( PapyShort ) Papy3FClose ( & ( gPapyFile [ inFileNb ] ) ) ; RETURN ( papReadFile ) } theBufPos = 0L ; theGrNb = Extract2Bytes ( theBuffP , & theBufPos ) ; theElemNb = Extract2Bytes ( theBuffP , & theBufPos ) ; theCmpGrNb = theGrNb ; theCmpElemNb = theElemNb ; Papy3FSeek ( gPapyFile [ inFileNb ] , ( int ) SEEK_SET , ( PapyLong ) theFileStartPos ) ; OK = FALSE ; while ( ! OK ) { i = 8L ; if ( ( theErr = ( PapyShort ) Papy3FRead ( gPapyFile [ inFileNb ] , & i , 1L , theBuffP ) ) < 0 ) { theErr = ( PapyShort ) Papy3FClose ( & ( gPapyFile [ inFileNb ] ) ) ; return 0 ; } theGroupLength += 8L ; theBufPos = 0L ; theGrNb = Extract2Bytes ( theBuffP , & theBufPos ) ; theElemNb = Extract2Bytes ( theBuffP , & theBufPos ) ; theVR [ 0 ] = ' ' ; theVR [ 1 ] = ' ' ; theVR [ 2 ] = ' \0 ' ; if ( ( gArrTransfSyntax [ inFileNb ] == LITTLE_ENDIAN_IMPL && theGrNb != 0x0002 ) || ( theGrNb == 0xFFFE && theElemNb == 0xE000 ) || ( theGrNb == 0xFFFE && theElemNb == 0xE00D ) ) theElemLength = Extract4Bytes ( theBuffP , & theBufPos ) ; else { theBuffP += theBufPos ; theVR [ 0 ] = ( char ) * theBuffP ; theVR [ 1 ] = ( char ) * ( theBuffP + 1 ) ; theVR [ 2 ] = ' \0 ' ; theBuffP = ( unsigned char * ) & theBuff [ 0 ] ; theBufPos += 2 ; if ( strcmp ( theVR , " " ) == 0 || strcmp ( theVR , " " ) == 0 || strcmp ( theVR , " " ) == 0 || strcmp ( theVR , " " ) == 0 || strcmp ( theVR , " " ) == 0 ) { if ( theGrNb == 0x0004 && theElemNb == 0x1220 ) { i = 6L ; if ( Papy3FRead ( gPapyFile [ inFileNb ] , & i , 1L , theBuffP ) < 0 ) { Papy3FClose ( & ( gPapyFile [ inFileNb ] ) ) ; RETURN ( papReadFile ) } theGroupLength -= 8L ; theElemLength = 0L ; OK = TRUE ; } else { i = 4L ; if ( Papy3FRead ( gPapyFile [ inFileNb ] , & i , 1L , theBuffP ) < 0 ) { Papy3FClose ( & ( gPapyFile [ inFileNb ] ) ) ; RETURN ( papReadFile ) } theGroupLength += 4L ; theBufPos = 0L ; theElemLength = Extract4Bytes ( theBuffP , & theBufPos ) ; } } else theElemLength = ( PapyULong ) Extract2Bytes ( theBuffP , & theBufPos ) ; } if ( theCmpGrNb == theGrNb && theCmpElemNb <= theElemNb && theGroupLength < ( PapyULong ) inMaxSize ) { if ( theElemLength == 0xFFFFFFFF ) { theElemLength = 0L ; if ( ( theErr = ComputeUndefinedSequenceLength3 ( inFileNb , & theElemLength ) ) < 0 ) RETURN ( ( PapyULong ) theErr ) ; } theGroupLength += theElemLength ; if ( ( theErr = Papy3FSeek ( gPapyFile [ inFileNb ] , ( int ) SEEK_CUR , ( PapyLong ) theElemLength ) ) < 0 ) RETURN ( ( PapyULong ) theErr ) ; } else { OK = TRUE ; theGroupLength -= 8L ; if ( strcmp ( theVR , " " ) == 0 || strcmp ( theVR , " " ) == 0 || strcmp ( theVR , " " ) == 0 ) theGroupLength -= 4L ; } } Papy3FSeek ( gPapyFile [ inFileNb ] , ( int ) SEEK_SET , ( PapyLong ) theFileStartPos ) ; return theGroupLength ; }
ComputeUndefinedGroupLength3 : compute the size of a group of undefined
length, starting from the current file position.
ComputeUndefinedGroupLength3 : compute the size of a group of undefined length starting from the current file position .
/* initializations */ /* arbitrary value */ /* read the group number and the element number from the file */ /* if */ /* get the group number and the element number */ /* set some comparison variables */ /* reset the file pointer to its previous position */ /* loop on the elements of the group */ /* read the file until the element length value */ /* Modif DRD
      RETURN (papReadFile) */ /* if */ /* get the group number, the element number and the element length */ /* reset the VR ... */ /* extract the element length depending on the syntax */ /* extract the VR */ /* get the element length depending on the VR */ /* DICOMDIR : here is the record sequence*/ /* read 6 more bytes: 2(elem) 4(value unuseful) */ /* if */ /* if ...elem = 0x0004, 0x1220 */ /* read 4 more bytes */ /* if */ /* else ...elem not 0x0004, 0x1220 */ /* if ...VR = OB, OW, SQ, Un or UT */ /* else ...EXPLICIT VR */ /* makes sure the element belongs to the group */ /* if the element has an undefined length (i.e. VR = SQ) */ /* if */ /* add the size of the element to the group size */ /* move the file pointer */ /* if ...element in group */ /* else ...end of group reached */ /* while ...loop on the elem of the group */ /* reset the file position to the begining of the group */