简体   繁体   中英

Big-Endian Conversion from a texture

I am trying to extract the height from a file like this: http://visibleearth.nasa.gov/view.php?id=73934

The pixels are loaded into an Int32 array

    private Int16[] heights;
    private int Width, Height;

    public TextureData(Texture2D t)
    {
        Int32[] data = new Int32[t.Width * t.Height];
        t.GetData<Int32>(data);
        Width = t.Width;
        Height = t.Height;
        t.Dispose();
        heights= new Int16[t.Width * t.Height];
        for (int i = 0; i < data.Length; ++i)
        {
            heights[i] = ReverseBytes(data[i]);
        }
    }

    // reverse byte order (16-bit)
    public static Int16 ReverseBytes(Int32 value)
    {
        return (Int16)( ((value << 8) | (value >> 8)) );
    }

I dont know why but the heights are not correct... I think the Big Endian conversion is wrong, can you help me please?

this is the result, the heights are higher than expected... http://i.imgur.com/FukdmLF.png

EDIT:

    public static int ReverseBytes(int value)
    {
        int sign = (value & 0x8000) >> 15;
        int msb = (value & 0x7F) >> 7;
        int lsb = (value & 0xFF) << 8;
        return (msb | lsb | sign);
    }

is this ok? I don't know why but it is still wrong...

int指的是32位带符号整数,但是您的字节反转器是为16位带符号整数编写的,因此它仅适用于正值,最高为32767。如果您有更高的值,则需要移位然后屏蔽一个一次将每个字节“累加”在一起。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM