[cpp]
/*========================================================================*/
//
// Description: 针对RAW图像的读入和存取
//
// Arguments:
//
// Returns:
//
// Notes: none
//
// Time: none
//
// Memory: none
//
// Example: none
//
// History: 1. wangyi 2014-4-19 22:46 Verison1.00 create
/*========================================================================*/
#include
#include
#include
#define height 256
#define width 256
typedef unsigned char BYTE; // 定义BYTE类型,占1个字节
int main()
{
FILE *fp = NULL;
BYTE B[height][width];
BYTE *ptr;
char path[256];
char outpath[256];
int i,j;
// 输入源路径并打开raw图像文件
printf("Input the raw image path: ");
scanf("%s",path);
if((fp = fopen( path, "rb" )) == NULL)
{
printf("can not open the raw image " );
return;
}
else
{
printf("read OK");
}
// 分配内存并将图像读到二维数组中
ptr = (BYTE*)malloc( width * height * sizeof(BYTE) );
for( i = 0; i
{
for( j = 0; j
{
fread( ptr, 1, 1, fp );
B[i][j]= *ptr; // 把图像输入到2维数组中,变成矩阵型式
printf("%d ",B[i][j]);
ptr++;
}
}
fclose(fp);
// 这里可以对二维数组中的图像数据进行处理
// 将处理后的图像数据输出至文件
printf("Input the raw_image path for save: ");
scanf("%s",outpath);
if( ( fp = fopen( outpath, "wb" ) ) == NULL )
{
printf("can not create the raw_image : %s\n", outpath );
return;
}
for( i = 0; i
{
for( j = 0; j
{
fwrite( &B[i][j], 1 , 1, fp );
}
}
fclose(fp);
}
上述程序实现了读入和存取的功能,中间可以自己加入对图像数据的处理算法,如注释中所述即可。