本文主要对图像的data 数据写入到文件(包括文本格式,和二进制文件)
#include <io.h>
#include <direct.h>
//首先在桌面建立一个CR_Result1文件夹
static char* dumpRoot = "C:\\Users\\seven\\Desktop\\CR_Result1\\";
static char file2open[1024];
// 创建文件夹
int dump_init_temp(){
//create dump folder
#if OS_ALVA==OS_Windows
if(_access(dumpRoot, 00) != 0){
_mkdir(dumpRoot);
#else
if(access(dumpRoot, 00) != 0){
mkdir(dumpRoot, S_IRUSR | S_IWUSR);
#endif
}
return 0;
}
//保存为二进制的文件
//filename 保存文件的名字
// buf 要保存的数据
// size 要保存文件的大小
int dump_temp(char* filename, char* buf, long long size){
FILE* file;
sprintf(file2open, "%s%s", dumpRoot, filename);
file = fopen(file2open, "wb");
if(file == NULL){
LOGE("can't open file: %s\n", file2open);
return -1;
}
fwrite(buf, 1, (size_t)size, file);
fclose(file);
return 0;
}
// 保存float 类型的图片数据
// filename 如1.txt
// buf 数据指针
// mWidth 数据宽
// mHeight 数据高
int DumpFloat(char * filename, char * buf, int mWidth, int mHeight)
{
FILE* file;
int w, h;
float * temp = (float *)buf;
sprintf(file2open, "%s%s", dumpRoot, filename);
file = fopen(file2open, "w+");
//fwrite(temp, mWidth * mHeight * sizeof(float), 1, file);
for(h = 0; h < mHeight; h ++)
{
for(w = 0; w < mWidth - 1; w ++)
{
fprintf_s(file, "%f,", temp[h * mWidth + w]);
}
fprintf_s(file, "%f\n", temp[h * mWidth + w]);
}
fclose(file);
return 0;
}
// 保存unsigned char 类型数据
// 参数上同
int DumpChar(char * filename, char * buf, int mWidth, int mHeight)
{
FILE* file;
int w, h;
unsigned char * temp = (unsigned char *)buf;
sprintf(file2open, "%s%s", dumpRoot, filename);
file = fopen(file2open, "w+");
for(h = 0; h < mHeight; h ++)
{
for(w = 0; w < mWidth - 1; w ++)
{
fprintf_s(file, "%u,", temp[h * mWidth + w]);
}
fprintf_s(file, "%u\n", temp[h * mWidth + w]);
}
fclose(file);
return 0;
}
int DumpInt(char * filename, char * buf, int mWidth, int mHeight)
{
FILE* file;
int w, h;
int * temp = (int *)buf;
sprintf(file2open, "%s%s", dumpRoot, filename);
file = fopen(file2open, "w+");
for(h = 0; h < mHeight; h ++)
{
for(w = 0; w < mWidth - 1; w ++)
{
fprintf_s(file, "%d,", temp[h * mWidth + w]);
}
fprintf_s(file, "%d\n", temp[h * mWidth + w]);
}
fclose(file);
return 0;
}
//文件名格式为xxx{W=4}{H=4}
// 将文件读到内存
float * ReadFromFileFloat(char * fileName)
{
char * prt;
char * prtTemp;
int width = 0;
int height = 0;
FILE * fp;
float * tempFloatPrt = NULL;
char * tempCharPrt = NULL;
int lengthChar = 0;
int ret = 0;
int i, j;
char fName[1024];
int fNameLength = strlen(fileName);
memcpy(fName, fileName, fNameLength);
fName[fNameLength] = '\0';
prt = fName;
prt = strstr(prt, "{W=");
prt = prt + strlen("{W=");
prtTemp = strstr(prt, "}");
* prtTemp = '\0';
sscanf(prt, "%d", &width);
* prtTemp = '}';
prt = fName;
prt = strstr(prt, "{H=");
prt = prt + strlen("{H=");
prtTemp = strstr(prt, "}");
* prtTemp = '\0';
sscanf(prt, "%d", &height);
* prtTemp = '}';
fp = fopen(fileName, "rb");
if(NULL == fp)
{
printf("fopen err \n");
}
fseek(fp, 0L, SEEK_END);
lengthChar = ftell(fp);
fseek(fp, 0L, SEEK_SET);
tempCharPrt = (char *)calloc(lengthChar, sizeof(char));
ret = fread(tempCharPrt, lengthChar, 1, fp);
if(1 != ret){ printf("fread err \n");}
fclose(fp);
tempFloatPrt = (float *)calloc(width * height, sizeof(float));
prt = tempCharPrt;
prtTemp = tempCharPrt;
for(i = 0; i < height; i ++)
{
for(j = 0; j < width - 1; j ++)
{
prtTemp = strstr(prt, ",");
*prtTemp = '\0';
sscanf(prt, "%f", &tempFloatPrt[i * width + j]);
prt = prtTemp + 1;
}
prtTemp = strstr(prt, "\n");
*prtTemp = '\0';
sscanf(prt, "%f", &tempFloatPrt[i * width + j]);
if(i != (height - 1))
{
prt = prtTemp + 1;
}
}
free(tempCharPrt);
return tempFloatPrt;
}
//将文件数据读到内存
#define OS_Android 0
#define OS_Windows 1
#define OS_XXX OS_Windows
int readData(const char *filename, char ** sources, int * fileLengths)
{
int fileLength;
int ret;
FILE *file = NULL;
#if OS_XXX == OS_Android
file = fopen(filename, "rb");
#endif
#if OS_XXX == OS_Windows
fopen_s(&file, filename, "rb");
#endif
if (file == NULL)
return -4;
fseek(file, 0, SEEK_END);
fileLength = ftell(file);
*fileLengths = fileLength;
*sources = (char*)malloc(fileLength + 1);
rewind(file);
ret = fread(*sources, fileLength, 1, file);
(*sources)[fileLength] = '\0';
if (ret == 0)
return -5;
fclose(file);
return 0;
}