最近学习pack_ward与raw的相互转化,主要的是要知道pack ward储存方式
比如pack里的储存方式为F6 41 31 我们会转成F6 01 14 03
虽然排列方式是这个F6 41 实际的16进制为 0X41 F6 ,F6为低八位,41为高八位(“4为高八位中的高四位”),for循环中当i=0时的val1 =0x01f6,val2=0x0314
void packed_word2raw()
{
// 打开.packed_word文件
std::ifstream input("E:/test/packed_word2raw/1920x1080_12_3.packed_word", std::ios::binary);
// 读取.packed_word文件的内容到一个vector中
std::vector<unsigned short> buffer(std::istreambuf_iterator<char>(input), {});
// 创建一个vector来存储转换后的16位数据
std::vector<unsigned short> data;
// 解析12位的数据
for (size_t i = 0; i < buffer.size(); i += 3)
{
unsigned short val1 = buffer[i]&0x00ff |( buffer[i + 1] &0x0f)<<8;
unsigned short val2 = (buffer[i + 1]&0xf0)>>4| buffer[i + 2]<<4;
data.push_back(val1);
data.push_back(val2);
}
// 打开raw文件
std::ofstream output("E:/test/packed_word2raw/result/1920x1080_test2_12_3.raw", std::ios::binary);
// 将16位的数据写入raw文件
output.write(reinterpret_cast<const char*>(data.data()), data.size() * sizeof(unsigned short));
}
将12bit raw压缩成16bit的pack raw
void raw2packed_word()
{
// 打开.Raw文件
std::ifstream input(“E:/test/packed_word2raw/1920x1080_12_3.raw”, std::ios::binary);
// 读取.Raw文件的内容到一个vector中
std::vector<unsigned short> buffer(std::istreambuf_iterator<char>(input), {});
// 创建一个vector来存储转换后的16位数据
std::vector<unsigned short> data;
// 解析16位的数据,12*4=16*3,那么在raw中我们需要四对16bit的数据(但是这16bit的数据只有12bit是有效的),写入到pack中(pack中数据16bit吃满了)
for (size_t i = 0; i < buffer.size(); i += 8)
{
unsigned short val1 =(buffer[i] & 0x00ff ) | (buffer[i + 1] & 0x0f) << 8 | (buffer[i + 2])<<12;
unsigned short val2 =(buffer[i + 2] & 0xf0) >> 4 | buffer[i + 3] << 4 | buffer[i + 4] << 8;
unsigned short val3 =(buffer[i + 5] & 0x0f)| buffer[i + 6] << 4 | buffer[i + 7] << 12;
data.push_back(val1);
data.push_back(val2);
data.push_back(val3);
}
// 打开raw文件
std::ofstream output("E:/test/packed_word2raw/result/1920x1080_test031124_12_3.packed_word", std::ios::binary);
// 将16位的数据写入raw文件
output.write(reinterpret_cast<const char*>(data.data()), data.size() * sizeof(unsigned short));
}