uint32_t WavReader_R32::SeekToChunk(int8_t cCode[4], uint8_t uiCodeLen) {
    if (m_pFileHandle == NULL)
        return false;
    fseek(m_pFileHandle, 0, FILE_END);
    uint32_t uiFileLength = ftell(m_pFileHandle);
    fseek(m_pFileHandle, 12, FILE_BEGIN);
    uint32_t uiCurrentPos = ftell(m_pFileHandle);
    while (true) {
        int8_t cReadCode[4] = { 0, 0, 0, 0 };
        if (!ReadFOURCC(cReadCode))
            return 0;
        if (memcmp(cReadCode, cCode, uiCodeLen) == 0) {
            uint32_t nChunkLength = ReadUINT32();
            uint32_t nRestBytes = uiFileLength - ftell(m_pFileHandle);
            if (nChunkLength > nRestBytes) {
                uiCurrentPos++;
                fseek(m_pFileHandle, uiCurrentPos, FILE_BEGIN);
                continue;
            }
            return nChunkLength;
        }
        uiCurrentPos++;
        fseek(m_pFileHandle, uiCurrentPos, FILE_BEGIN);
    }
    return 0;
}
Example #2
0
// Parse the box from the stream
CNCSError CNCSJP2File::CNCSJP2SignatureBox::Parse(class CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
	CNCSError Error;

	NCSJP2_CHECKIO_BEGIN(Error, Stream);
//		UINT8 buf[4];
		// Read in the signature
//		NCSJP2_CHECKIO(Read(buf, 4));
		UINT32 buf;
		NCSJP2_CHECKIO(ReadUINT32(buf));
		
		if(memcmp(&sm_JP2Signature, &buf, sizeof(buf)) == 0) {
			// Signature is valid
			m_bValid = true;
		} else {
			Error = NCS_FILE_INVALID;
		}
	NCSJP2_CHECKIO_END();
	return(Error);
}
Example #3
0
void vtkDCMParser::ReadElement(DCMDataElementStruct *des)
{
  if(this->file_in)
    {
      PrevFilePos = ftell(file_in);
      PrevFileIOMessage = FileIOMessage;

      switch(TransferSyntax)
      {
      case TFS_IVRLE:
      case TFS_IVRBE:
        
        des->GroupCode = ReadUINT16();
        des->ElementCode = ReadUINT16();
        des->Length = ReadUINT32();

        des->NextBlock = ftell(file_in);
        if(des->Length != 0xffffffff)
          des->NextBlock += des->Length;

        sprintf(des->VR, "??");
        
        break;
        
      case TFS_EVRLE:
      case TFS_EVRBE:
        
        des->GroupCode = ReadUINT16();
        des->ElementCode = ReadUINT16();
        
        if((des->GroupCode) == 0xfffe)
          {
            if((des->ElementCode == 0xe000) ||
             (des->ElementCode == 0xe00d) ||
             (des->ElementCode == 0xe0dd))
            { // must be implicit VR always
              des->Length = ReadUINT32();

              des->NextBlock = ftell(file_in);
              if(des->Length != 0xffffffff)
                des->NextBlock += des->Length;

              sprintf(des->VR, "??");

              return;
            }
          }
        
        ReadText(des->VR, 2); // getting VR
        
        if((strcmp(des->VR, "OB") == 0) ||
           (strcmp(des->VR, "OW") == 0) ||
           (strcmp(des->VR, "SQ") == 0) ||
           (strcmp(des->VR, "UN") == 0) ||
           (strcmp(des->VR, "UT") == 0)
           )
          {
            des->Length = ReadUINT16(); // reserved field
            des->Length = ReadUINT32();
            des->NextBlock = ftell(file_in);
            if(des->Length != 0xffffffff)
              des->NextBlock += des->Length;
          } else
          {
            des->Length = ReadUINT16();
            des->NextBlock = ftell(file_in);
            if(des->Length != 0xffffffff)
              des->NextBlock += des->Length;
          }
        
        break;
      }
    }
}
bool WavReader_R32::OpenWavFile(const char *pszFilePathName) {
    if (pszFilePathName == NULL)
        return false;
    if (strlen(pszFilePathName) <= 0)
        return false;
    if (m_fpSamples != NULL)
        delete[] m_fpSamples;
    if (m_pFileHandle != NULL)
        fclose(m_pFileHandle);
    m_fpSamples = NULL;
    m_pFileHandle = NULL;
    m_uiFrameCount = 0;
    m_uiSamplingRate = 0;
    m_uiChannels = 0;

    m_pFileHandle = fopen(pszFilePathName, "rb");
    if (m_pFileHandle == NULL)
        return false;

    int8_t cCode[4];
    if (!ReadFOURCC(cCode))
        return false;
    if ((cCode[0] != 'R') || (cCode[1] != 'I') || (cCode[2] != 'F')
            || (cCode[3] != 'F'))
        return false;
    uint32_t uiRiffChunkDataSize = ReadUINT32();
    if (uiRiffChunkDataSize == 0)
        return false;
    if (!ReadFOURCC(cCode))
        return false;
    if ((cCode[0] != 'W') || (cCode[1] != 'A') || (cCode[2] != 'V')
            || (cCode[3] != 'E'))
        return false;

    cCode[0] = 'f';
    cCode[1] = 'm';
    cCode[2] = 't';
    uint32_t nFormatLength = SeekToChunk(cCode, 3);
    if (nFormatLength < 16)
        return false;
    uint16_t nFormatTag = ReadUINT16();
    if ((nFormatTag != 0x0001) && (nFormatTag != 0x0003))
        return false;

    uint16_t uiChannels = ReadUINT16();
    if ((uiChannels != 1) && (uiChannels != 2))
        return false;
    uint32_t uiSampleRate = ReadUINT32();
    ReadUINT32();
    ReadUINT16();
    uint16_t uiBitsPerSample = ReadUINT16();
    if ((uiBitsPerSample != 8) && (uiBitsPerSample != 16)
            && (uiBitsPerSample != 24) && (uiBitsPerSample != 32))
        return false;

    cCode[0] = 'd';
    cCode[1] = 'a';
    cCode[2] = 't';
    cCode[3] = 'a';
    uint32_t uiDataSize = SeekToChunk(cCode, 4);
    uint32_t uiFrameCount = uiDataSize / (uiBitsPerSample / 8) / uiChannels;
    if (uiFrameCount == 0)
        return false;

    if (nFormatTag == 0x0003) {
        m_fpSamples = new float[uiFrameCount * uiChannels];
        if (m_fpSamples == NULL)
            return false;
        uint32_t nReadFrameCount = (uint32_t) fread(m_fpSamples, sizeof(float),
                uiFrameCount * uiChannels,
                m_pFileHandle);
        if (nReadFrameCount != uiFrameCount * uiChannels) {
            delete[] m_fpSamples;
            return false;
        }
        m_uiFrameCount = uiFrameCount;
        m_uiSamplingRate = uiSampleRate;
        m_uiChannels = uiChannels;
        return true;
    } else {
        uint32_t uiReadDataLength = uiFrameCount * uiChannels
                * (uiBitsPerSample / 8);
        uint8_t *ucFrameBuffer = new uint8_t[uiReadDataLength];
        if (ucFrameBuffer == NULL)
            return false;
        uint32_t nReadFrameCount = (uint32_t) fread(ucFrameBuffer,
                sizeof(uint8_t),
                uiReadDataLength,
                m_pFileHandle);
        if (nReadFrameCount != uiReadDataLength) {
            delete[] ucFrameBuffer;
            return false;
        }
        m_fpSamples = new float[uiFrameCount * uiChannels];
        if (m_fpSamples == NULL) {
            delete[] ucFrameBuffer;
            return false;
        }
        switch (uiBitsPerSample) {
            case 8:
                ConvertInt8ToFloat32(ucFrameBuffer, uiFrameCount, uiChannels,
                        m_fpSamples);
                break;

            case 16:
                ConvertInt16ToFloat32((int16_t*) ucFrameBuffer, uiFrameCount,
                        uiChannels, m_fpSamples);
                break;

            case 24:
                ConvertInt24ToFloat32(ucFrameBuffer, uiFrameCount, uiChannels,
                        m_fpSamples);
                break;

            case 32:
                ConvertInt32ToFloat32((int32_t*) ucFrameBuffer, uiFrameCount,
                        uiChannels, m_fpSamples);
                break;
        }
        delete[] ucFrameBuffer;
        m_uiFrameCount = uiFrameCount;
        m_uiSamplingRate = uiSampleRate;
        m_uiChannels = uiChannels;
        return true;
    }

    return false;
}