Get color values in byte stream- ios

半世苍凉 提交于 2019-12-11 06:39:12

问题


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>

If color 9BD6AE was there byte chunk will be 3f1b9b9c 3f56d6d7 3f2eaeaf

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?


回答1:


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;
}

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.



来源:https://stackoverflow.com/questions/14880570/get-color-values-in-byte-stream-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!