在mac中读取文件

时间:2014-04-12 20:50:07

标签: c++ opengl file-io volume-rendering

我是openGL的新手,正在开发一个卷渲染项目。因此,项目需要首先从文件中读取帧,然后构造2D纹理。在示例代码中,程序员使用CFile.read()函数执行此操作。但是,我正在研究我的MacPro,似乎我们只能在Windows下使用CFile。所以请告诉我如何才能成功读取图像数据。

P.S。:示例代码是基于3D纹理的,而我的代码是基于2D纹理的。所以可能会有一些差异。但是,您应该只关注在此处读取图像数据的函数,而不是生成纹理的语句。

示例代码:

bool CRawDataProcessor::ReadFile( LPCTSTR lpDataFile_i, int nWidth_i, int nHeight_i, int nSlices_i )
{
    CFile Medfile;
    if( !Medfile.Open( lpDataFile_i ,CFile::modeRead ))
    {
        return false;
    }

    // File has only image data. The dimension of the data should be known.
    m_uImageCount = nSlices_i;
    m_uImageWidth = nWidth_i;
    m_uImageHeight = nHeight_i;

    // Holds the luminance buffer.
    char* chBuffer = new char[ m_uImageWidth*m_uImageHeight*m_uImageCount ];
    if( !chBuffer )
    {
        return false;
    }
    // Holds the RGBA buffer.
    char* pRGBABuffer = new char[ m_uImageWidth*m_uImageHeight*m_uImageCount*4 ];
    if( !pRGBABuffer )
    {
        return false;
    }

    Medfile.Read( chBuffer, m_uImageWidth*m_uImageHeight*m_uImageCount );

    // Convert the data to RGBA data.
    // Here we are simply putting the same value to R, G, B and A channels.
    // Usually for raw data, the alpha value will be constructed by a threshold value  given by the user.

    for( int nIndx = 0; nIndx < m_uImageWidth*m_uImageHeight*m_uImageCount; ++nIndx )
    {
        pRGBABuffer[nIndx*4] = chBuffer[nIndx];
        pRGBABuffer[nIndx*4+1] = chBuffer[nIndx];
        pRGBABuffer[nIndx*4+2] = chBuffer[nIndx];
        pRGBABuffer[nIndx*4+3] = chBuffer[nIndx];
    }

    // If this function is getting called again for another data file.
    // Deleting and creating texture is not a good idea, 
    // we can use the glTexSubImage3D for better performance for such scenario.
    // I am not using that now :-)
    if( 0 != m_nTexId )
    {
        glDeleteTextures( 1, (GLuint*)&m_nTexId );
    }
    glGenTextures( 1, (GLuint*)&m_nTexId );

    glBindTexture( GL_TEXTURE_3D, m_nTexId );
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

    glTexImage3D( GL_TEXTURE_3D, 0, GL_RGBA, m_uImageWidth, m_uImageHeight, m_uImageCount, 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, pRGBABuffer );
    glBindTexture( GL_TEXTURE_3D, 0 );

    delete[] chBuffer;
    delete[] pRGBABuffer;
    return true;
}

我试图在这里使用fstream,但它不起作用。我的代码:

bool InitTextures2D( const char* filePath )
{
    std::fstream myFile;
    myFile.open( filePath, std::ios::out | std::ios::binary );

    m_uImageCount = 109;
    m_uImageWidth = 256;
    m_uImageHeight = 256;

    // Holds the texuture IDs.
    m_puTextureIDs = new int[m_uImageCount];

    // Holds the luminance buffer.
    char* chBuffer = new char[256 * 256];
    glGenTextures( m_uImageCount, (GLuint*)m_puTextureIDs );

    // Read each frames and construct the texture.
    for( int nIndx = 0; nIndx < m_uImageCount; ++nIndx )
    {
        // Read the frame.
        myFile.read(chBuffer, m_uImageWidth*m_uImageHeight);

        // Set the properties of the texture.
        glBindTexture( GL_TEXTURE_2D, m_puTextureIDs[nIndx] );
        glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

        glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, m_uImageWidth, m_uImageHeight, 0,
                      GL_LUMINANCE, GL_UNSIGNED_BYTE,(GLvoid *) chBuffer );
        glBindTexture( GL_TEXTURE_2D, 0 );
    }

    delete[] chBuffer;
    return true;
}

1 个答案:

答案 0 :(得分:4)

你打开文件写:“ std :: ios :: out ”,然后从中读取..这是行不通的。

变化:

myFile.open(filePath, std::ios::out | std::ios::binary);

myFile.open(filePath, std::ios::in | std::ios::binary);

请注意,如果您执行了之前编写的代码,则会打开文件进行编写。哪个截断了文件,现在很可能是0个字节。