获取字节流中的颜色值 - ios [英] Get color values in byte stream- ios
问题描述
我从二进制文件 (.ase) 中读取这个字节流 粗体值代表颜色:
I read this byte stream from a binary file (.ase) Bold values represent colors:
< 41534546 00010000 00000005 c0010000 00140008 004f0070 0061006c 0074006f 006e0065 00000001 00000024 00080023 00320042 00420034 00300046 00005247 4220 * 3e2c acad3f34 b4b53d70 F0F1 * 0002 00010000 00240008 00230046 00460035 00420033 00310000 52474220的 3f800000 3eb6b6b7 3e44c4c5 强> 00020001 00000024 00080023 00460046 00380037 00340038 00005247 4220 * 3f80 00003f07 87883e90 9091 * 0002 00010000 00240008 00230030 00330038 00340043 00410000 52474220的 3c40c0c1 3f048485 3f4acacb 强> 0002>
<41534546 00010000 00000005 c0010000 00140008 004f0070 0061006c 0074006f 006e0065 00000001 00000024 00080023 00320042 00420034 00300046 00005247 4220*3e2c acad3f34 b4b53d70 f0f1*0002 00010000 00240008 00230046 00460035 00420033 00310000 52474220 3f800000 3eb6b6b7 3e44c4c5 00020001 00000024 00080023 00460046 00380037 00340038 00005247 4220*3f80 00003f07 87883e90 9091*0002 00010000 00240008 00230030 00330038 00340043 00410000 52474220 3c40c0c1 3f048485 3f4acacb 0002>
如果有颜色9BD6AE
,字节块将是3f1b9b9c 3f56d6d7 3f2eaeaf
If color 9BD6AE
was there byte chunk will be 3f1b9b9c 3f56d6d7 3f2eaeaf
每个块代表每个颜色分量的浮点值.(红色:0.607843 绿色:0.839216 蓝色:0.682353)
Each block represents the float values of each color component. (red:0.607843 green:0.839216 blue:0.682353)
如何从上述数据流中获取这些浮点值?
How do I get those float values from the above data stream?
推荐答案
假设您拥有包含文件内容的 NSData,您可以使用如下代码将这些值转换为 float
:>
Assuming you have NSData with the contents of the file, you can convert those values to float
with code like the following:
NSData *data = ... // contents of the ASE file
const void *bytes = [data bytes];
int offset = ... // offset into the data stream (in bytes)
// assuming offset is pointing to the start of 4 bytes that represent a float value:
float colorComponent = [self floatFromBytes:bytes + offset];
// Helper method to convert bytes to a float
// In ASE file, float values are 4-byte values in big-endian format
- (float)floatFromBytes:(const void *)bytes {
uint_32_t beVal = *(uint_32_t *)bytes; // data is in big-endian format
uint_32_t locVal = CFSwapInt32BigToHost(beVal); // data in host format
float res = *(float *)&locVal; // convert bytes to float
return res;
}
这远不是一个完整的 ASE 解析算法.但这显示了如何将 4 字节值转换为 float
值.您需要处理 ASE 文件格式的许多其他方面.
This is far from a full ASE parsing algorithm. But this shows how to convert 4-byte values into float
values. You need to deal with lots of other aspects of the ASE file format.
这篇关于获取字节流中的颜色值 - ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!