您的位置:首页 > 移动开发

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 refocus

fseek(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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: