C语言读3D数据.raw文件

三维图像经常会以.raw文件的形式提供。Raw文件其实是存图像最简单的方式,是一种对图像无压缩的存储。实际上,这种格式就是把图像一维化的数组存储成文件,所以可以轻易的写出读取Raw文件的代码。不过由于这个文件不带自描述信息,所以在读的时候一定要清楚所读的文件是多少位的图像,长宽高是多少。假如知道数据的大小为301×324×56,8位,所以采用C++语言将其读为unsigned char数组的代码如下:

void InitArrayFromVolFile(unsigned char * &data,const char* fileName,int length)
{
    long dataSize = length;
    std::FILE* file = fopen(fileName,"rb");
    if( file == NULL )
    {
        printf("open the file failed\n");
    }
    fread( data,sizeof(unsigned char),dataSize,file );
    fclose(file);
}

再将其输出为文件的代码如下,都是比较简单的读写调用,由此同时可以看出,无论是2维还是3维图像,使用一维数组的方式存数据:

void OutPutVolume(const char* fileName,unsigned char* pointer,int width,int height,int depth)
{    
     int length=width*height*depth;
     FILE *const nfile = fopen(fileName,"wb");
     fwrite(pointer,sizeof(unsigned char),length,nfile);
     fclose(nfile);
}

猜你喜欢

转载自blog.csdn.net/qq_35007834/article/details/84374039