|
楼主 |
发表于 2008-7-4 17:54:48
|
显示全部楼层
EmCV增加efs_cvSaveImage函数
ti 提供的efs可以看作是一个文件堆栈,是一个单层目录结构,后创建的文件在堆栈的顶部。
其中efs_createfile在创建文件时有一个问题。如果已经存在重名的文件,那么原来的文件
依然存在,新创建的文件在栈的顶部。
efs_fopen和efs_destroyfile在查找文件的时候都是从栈顶开始,找到文件就返回。
因此,访问的文件是靠近栈顶的那个(如有有重名的文件将不能访问)。
现在对efs_cvSaveImage进行改进,如果图像已经存在,则覆盖原来的图像内容。
如果图像不存在,则重新创建,避免出现多个同名的文件同时存在于文件堆栈中。
- CV_IMPL int
- efs_cvSaveImage( const char* filename, const IplImage * image )
- {
- int channels;
- EFS_FILE * p;
- BmpFileHead bmpfilehead;
- BmpInfoHead bmpinfohead;
- RGBA rgba;
- int step, modbytes, idx;
-
- int imageLength;
- char *pData;
- CV_FUNCNAME( "efs_cvSaveImage" );
- __BEGIN__;
- channels = image->nChannels;
- if( channels != 3 && channels != 1)
- CV_ERROR( CV_BadNumChannels, "Only 1 or 3-channel image is supported" );
- if( !filename || strlen(filename) == 0 ||
- strlen(filename) >= EFS_FILENAME_MAX)
- {
- CV_ERROR( CV_StsNullPtr, "null filename" );
- }
-
- //
-
- modbytes = (image->width*channels)%4;
- step = modbytes ? ((image->width*channels)-modbytes+4) : (image->width*channels);
-
- // image size
-
- imageLength = 0;
- {
- // header
-
- imageLength += sizeof(BmpFileHead);
- imageLength += sizeof(BmpInfoHead);
-
- // color table
- if(channels==1)
- {
- imageLength += sizeof(RGBA)*256;
- }
-
- // data
-
- imageLength += step*image->height;
- }
-
- // try open file
-
- p = efs_fopen((char*)filename, "rb");
-
- if(p)
- {
- if(efs_getfilesize(p) != imageLength)
- {
- // destroy file
-
- efs_fclose(p);
- efs_destroyfile((char*)filename);
-
- // create a new file
-
- pData = (char*)cvAlloc(imageLength);
- efs_createfilecb((char*)filename, imageLength, (UINT8*)pData,
- (EFSFUN)cvFree_, (UINT32)pData );
-
- // open file again
-
- p = efs_fopen((char*)filename, "rb");
- }
- }
- else
- {
- // create a new file
-
- pData = (char*)cvAlloc(imageLength);
- efs_createfilecb((char*)filename, imageLength, (UINT8*)pData,
- (EFSFUN)cvFree_, (UINT32)pData );
-
- // open file again
-
- p = efs_fopen((char*)filename, "rb");
- }
-
- if(!p)
- CV_ERROR( CV_StsNullPtr, "Can not create file" );
- // data point
-
- pData = (char*)efs_loadfunction((char*)filename);
-
- //bmpfilehead = (BmpFileHead *)cvAlloc(sizeof(BmpFileHead));
- //bmpinfohead = (BmpInfoHead *)cvAlloc(sizeof(BmpInfoHead));
-
- memset(&bmpfilehead, 0, sizeof(BmpFileHead));
- memset(&bmpinfohead, 0, sizeof(BmpInfoHead));
- memset(&rgba, 0, sizeof(RGBA));
-
- bmpfilehead.type1=\'B\';
- bmpfilehead.type2=\'M\';
-
- bmpinfohead.imageSize = ( 2+sizeof(BmpInfoHead)+image->height*step );
- bmpinfohead.startPosition = (channels==3) ? (2+sizeof(BmpInfoHead)) : (2+sizeof(BmpInfoHead)+1024);
- bmpinfohead.length = 40;
- bmpinfohead.width = (image->width);
- bmpinfohead.height = (image->height);
- bmpinfohead.colorPlane = (1);
- bmpinfohead.bitColor = (channels==3) ?(24) : 8;
- bmpinfohead.realSize = (image->height*step);
- //write header
- efs_fwrite_(&bmpfilehead, sizeof(BmpFileHead), 1, p, pData);
- efs_fwrite_(&bmpinfohead, sizeof(BmpInfoHead), 1, p, pData);
- //write
- if(channels==1)
- {
- for(idx =0; idx<256; idx++)
- {
- rgba.R=idx;
- rgba.G=idx;
- rgba.B=idx;
- rgba.A = 0;
- efs_fwrite_(&rgba, sizeof(RGBA), 1, p, pData);
- }
- }
-
- for(idx = image->height-1; idx >=0 ; idx--)
- efs_fwrite_(image->imageData+image->widthStep*idx, step, 1, p, pData);
-
- efs_fclose(p);
- __END__;
-
- return 0;
- }
复制代码 |
|