Append data to file end
2015-08-11 16:11
411 查看
Background
Gallery普通图片的显示需要根据文件路径读取图片数据显示在手机显示屏上。如果不是普通图片显示,有一些额外的图片信息需要在显示的时候从图片数据中读取出来,那么这时候需要我们对文件进行读写操作。
Data Format Definition
该数据格式用于存储自定义数据格式段到文件末尾,自定义数据格式段简称ADTFD,从后往前解析该段结构。
1.Append data definition
开始标志: 0xFFEE 结束标志: 0xFFEF
2.Append data structure
+-------------+ | 0xFFEE | 2bytes +------------+| Item | | +------------+ Item | | +------------+ Length | 4bytes | +------------+ 0xFFEF | 2bytes | +------------+
length: 指item的总长度
length=ItemCount*10
3.Item definition
| ItemID | ItemOffset | ItemLength| | 2 bytes | 4 bytes | 4 bytes | +------------+------------+----------+
ItemID: Item的标识.标识这个ITEM是什么 ItemOffset: 显示这个Item在文件中的Offset|起始位置 ItemLength: 显示这个Item的内容长度
Gallery file instance
Gallery图片需要存储原始图片数据(orig),深度数据(depth),bokeh参数信息(bokeh),那么存储格式如下:文件依次存下原始图片数据,深度数据,bokeh参数,然后再存下自定义的数据段。
存储数据
fseek(fp, 0, SEEK_END);//write depth
int depthpOffset = (int) ftell(fp);
vector<uchar> buf;
imencode(".png", depth, buf);
int dlen = (int) buf.size();
fwrite(&buf[0], dlen * sizeof(uchar), 1, fp);
//write refocus
int refocusOffset = (int) ftell(fp);
int rlen = (*refocusLen) * sizeof(float);
fwrite(refocusInfo, rlen, 1, fp);
//write append data
ushort start = DP_SEG_START;
fwrite(&start, sizeof(ushort), 1, fp);
//Item depth
ushort depthId = DP_DEPTH_ID;
fwrite(&depthId, sizeof(ushort), 1, fp);
fwrite(&depthpOffset, sizeof(int), 1, fp);
fwrite(&dlen, sizeof(int), 1, fp);
//Item refocus
ushort refocusId = DP_REFOCUS_ID;
fwrite(&refocusId, sizeof(ushort), 1, fp);
fwrite(&refocusOffset, sizeof(int), 1, fp);
fwrite(&rlen, sizeof(int), 1, fp);
//length
int length = 10 * 2;
fwrite(&length, sizeof(int), 1, fp);
ushort end = DP_SEG_END;
fwrite(&end, sizeof(ushort), 1, fp);
解析数据
//read refocusfseek(fp, -6 - 1*10, SEEK_END);
int offset = 0;
int rlen = 0;
fread(&sid, sizeof(ushort), 1, fp);
fread(&offset, sizeof(int), 1, fp);
fread(&rlen, sizeof(int), 1, fp);
fseek(fp, offset, SEEK_SET);
memset(refocusInfo, 0, rlen);
fread(refocusInfo, rlen, 1, fp);
//read depth
fseek(fp, -6 - 2*10, SEEK_END);
fread(&sid, sizeof(ushort), 1, fp);
if (sid == DP_DEPTH_ID) {
offset = 0;
int dplen = 0;
fread(&offset, sizeof(int), 1, fp);
fread(&dplen, sizeof(int), 1, fp);
fseek(fp, offset, SEEK_SET);
vector<uchar> buf;
buf.resize(dplen);
fread(&buf[0], dplen * sizeof(uchar), 1, fp);
相关文章推荐
- android自定义控件属性详解
- android minSdkVersion和targetSdkVersion含义
- iOS入门(十七)继承
- Android:spinner
- 【原】iOS触摸事件深度解析
- iOS开发之GCD使用总结
- public static T CloneObject<T>(this T obj) where T:class{}
- Android 6.0 Phone MO(去电)流程分析(应用层)
- android scrollview listview 滚动冲突解决方案
- MAC中设置android adb环境变量
- Android项目中如何用好构建神器Gradle?
- 一个帖子学会Android开发四大组件
- Android项目中如何用好构建神器Gradle?
- Android项目中如何用好构建神器Gradle?
- Android 自定义ImageView实现圆角/圆形 附加OnTouchListener详细注释以及Button圆角
- iOS入门(十六)实例变量可见度和方法
- Android 自定义ImageView实现圆角/圆形 附加OnTouchListener详细注释以及Button圆角
- Android:控件WebView显示网页
- oc 和swift混编的时候打包ipa安装不了
- SpriteKit游戏开发系列教程一(swift版)学习前的准备工作