BitMiracle.LibTiff.Classic.FieldValue.ToIntArray C# (CSharp) Method

ToIntArray() public method

Retrieves value converted to array of int values.

If value is array of int values then it retrieved unaltered.

If value is array of bytes then each 4 bytes are converted to int and added to resulting array. If value contains amount of bytes that can't be divided by 4 without remainder, then null is returned.

If value is array of short, ushort or uint values then each element of field value gets converted to int and added to resulting array.

If value is of any other type then null is returned.

public ToIntArray ( ) : int[]
return int[]
        public int[] ToIntArray()
        {
            if (m_value == null)
                return null;

            Type t = m_value.GetType();
            if (t.IsArray)
            {
                if (m_value is int[])
                    return m_value as int[];
                else if (m_value is byte[])
                {
                    byte[] temp = m_value as byte[];
                    if (temp.Length % sizeof(int) != 0)
                        return null;

                    int totalInts = temp.Length / sizeof(int);
                    int[] result = new int[totalInts];

                    int byteOffset = 0;
                    for (int i = 0; i < totalInts; i++)
                    {
                        int s = BitConverter.ToInt32(temp, byteOffset);
                        result[i] = s;
                        byteOffset += sizeof(int);
                    }

                    return result;
                }
                else if (m_value is short[])
                {
                    short[] temp = m_value as short[];
                    int[] result = new int[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (int)temp[i];

                    return result;
                }
                else if (m_value is ushort[])
                {
                    ushort[] temp = m_value as ushort[];
                    int[] result = new int[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (int)temp[i];

                    return result;
                }
                else if (m_value is uint[])
                {
                    uint[] temp = m_value as uint[];
                    int[] result = new int[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (int)temp[i];

                    return result;
                }
            }

            return null;
        }